:root {
  --bg: #FFF6E9;
  --ink: #23304A;
  --ink-soft: #6B7590;
  --card: #FFFFFF;
  --line: #EADFCF;
  --coral: #FF7A59;
  --coral-dk: #E35E3D;
  --blue: #3E7BFA;
  --blue-dk: #2A62D8;
  --green: #2FBF71;
  --green-dk: #22A05D;
  --sun: #FFC93C;
  --lv0: #EFE6D8;
  --lv1: #FFD9C7;
  --lv2: #FFC08F;
  --lv3: #FFE07A;
  --lv4: #B9EBB0;
  --lv5: #2FBF71;
  --radius: 24px;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #18202F;
    --ink: #F4EFE6;
    --ink-soft: #A6AFC4;
    --card: #232D40;
    --line: #34405A;
    --lv0: #2E3850;
    --lv1: #5A3A35;
    --lv2: #7A4E2E;
    --lv3: #7A6A2A;
    --lv4: #2F6A45;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  -webkit-user-select: none;
  user-select: none;
  -webkit-text-size-adjust: 100%;
}

body { overscroll-behavior: none; }

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

#app {
  max-width: 640px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
           max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.screen { display: none; flex-direction: column; min-height: calc(100dvh - 32px); gap: 18px; }
.screen[data-active] { display: flex; animation: in .25s ease-out; }
@keyframes in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- Home ---------- */
.hero { text-align: center; padding-top: 12px; }
h1 { font-size: clamp(40px, 11vw, 64px); margin: 0; letter-spacing: -0.02em; font-weight: 800; }
h1::after {
  content: "";
  display: block;
  width: 72px; height: 8px; margin: 8px auto 0;
  border-radius: 8px;
  background: linear-gradient(90deg, var(--coral), var(--sun), var(--green), var(--blue));
}
.sub { color: var(--ink-soft); font-size: 20px; margin: 6px 0 0; font-weight: 600; }
.center { text-align: center; margin: 0; }

.tables { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.tables button {
  aspect-ratio: 1.15;
  font-size: clamp(26px, 7.5vw, 40px);
  font-weight: 800;
  background: var(--card);
  border: 3px solid var(--line);
  border-radius: 20px;
  box-shadow: 0 4px 0 var(--line);
  transition: transform .08s, background .15s, border-color .15s;
}
.tables button:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--line); }
.tables button[aria-pressed="true"] {
  background: var(--sun);
  border-color: #E8AE15;
  box-shadow: 0 4px 0 #E8AE15;
  color: #23304A;
}

.quick { display: flex; justify-content: center; gap: 10px; }
.chip {
  padding: 10px 22px;
  border-radius: 999px;
  background: var(--card);
  border: 2px solid var(--line);
  font-weight: 700;
  font-size: 18px;
}

.modes { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: auto; }
.big-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  min-height: 96px;
  border-radius: var(--radius);
  font-size: 22px;
  font-weight: 800;
  color: #fff;
  transition: transform .08s;
}
.big-btn .ico { font-size: 32px; }
.big-btn:active { transform: translateY(4px); }
.big-btn.flash { background: var(--blue); box-shadow: 0 6px 0 var(--blue-dk); }
.big-btn.quiz  { background: var(--coral); box-shadow: 0 6px 0 var(--coral-dk); }
.big-btn:active.flash { box-shadow: 0 2px 0 var(--blue-dk); }
.big-btn:active.quiz  { box-shadow: 0 2px 0 var(--coral-dk); }
.big-btn:disabled { opacity: .4; }

.link-btn {
  align-self: center;
  padding: 12px 20px;
  font-size: 20px;
  font-weight: 700;
  color: var(--ink-soft);
}
.link-btn.danger { font-size: 16px; opacity: .7; }

/* ---------- Top bar ---------- */
.topbar { display: grid; grid-template-columns: 52px 1fr 64px; align-items: center; gap: 10px; }
.back {
  width: 52px; height: 52px;
  border-radius: 16px;
  background: var(--card);
  border: 2px solid var(--line);
  font-size: 34px; line-height: 1; font-weight: 700;
  padding-bottom: 4px;
}
.bar { height: 14px; background: var(--lv0); border-radius: 99px; overflow: hidden; }
.fill { height: 100%; width: 0; background: var(--green); border-radius: 99px; transition: width .3s ease; }
.count { text-align: right; font-weight: 800; font-size: 18px; color: var(--ink-soft); }
.topbar-title { margin: 0; text-align: center; font-size: 26px; }

/* ---------- Cards ---------- */
.card-wrap { flex: 1; display: flex; align-items: center; justify-content: center; perspective: 1200px; min-height: 240px; }
.card {
  position: relative;
  width: 100%;
  max-width: 520px;
  aspect-ratio: 1.45;
  transform-style: preserve-3d;
  transition: transform .45s cubic-bezier(.3,1.3,.5,1);
  cursor: pointer;
  touch-action: manipulation;
}
.card.flipped { transform: rotateY(180deg); }
.face {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--card);
  border-radius: 32px;
  border: 3px solid var(--line);
  box-shadow: 0 10px 0 var(--line), 0 18px 40px rgba(35,48,74,.08);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  font-size: clamp(64px, 19vw, 120px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}
.face small { font-size: 16px; font-weight: 700; color: var(--ink-soft); letter-spacing: 0; margin-top: 6px; }
.face.reverse { transform: rotateY(180deg); background: var(--sun); border-color: #E8AE15; box-shadow: 0 10px 0 #E8AE15; color: #23304A; }

.answer-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; transition: opacity .2s; }
.answer-row.hidden { opacity: 0; pointer-events: none; }
.ans {
  min-height: 84px;
  border-radius: var(--radius);
  font-size: 24px; font-weight: 800; color: #fff;
}
.ans:active { transform: translateY(4px); }
.ans.no  { background: var(--coral); box-shadow: 0 6px 0 var(--coral-dk); }
.ans.yes { background: var(--green); box-shadow: 0 6px 0 var(--green-dk); }

/* ---------- Quiz ---------- */
.quiz-card { aspect-ratio: auto; min-height: 220px; }
.quiz-card .face { position: relative; inset: auto; min-height: 220px; padding: 16px; font-size: clamp(54px, 15vw, 96px); }
.entry {
  min-width: 2.6em;
  margin-top: 6px;
  padding: 0 .3em;
  border-bottom: 6px solid var(--line);
  text-align: center;
  color: var(--blue);
  font-size: .85em;
  line-height: 1.15;
}
.hint { min-height: 26px; font-size: 22px; font-weight: 700; color: var(--ink-soft); letter-spacing: 0; margin-top: 6px; }
.quiz-card.right .face { background: #E3F8EC; border-color: var(--green); box-shadow: 0 10px 0 var(--green); }
.quiz-card.right .entry { color: var(--green-dk); border-color: var(--green); }
.quiz-card.wrong .face { background: #FFE9E2; border-color: var(--coral); box-shadow: 0 10px 0 var(--coral); }
.quiz-card.wrong .entry { color: var(--coral-dk); border-color: var(--coral); text-decoration: line-through; }
@media (prefers-color-scheme: dark) {
  .quiz-card.right .face { background: #1E4A33; }
  .quiz-card.wrong .face { background: #4D2A24; }
}
.shake { animation: shake .4s; }
@keyframes shake { 20%,60% { transform: translateX(-10px); } 40%,80% { transform: translateX(10px); } }
.pop { animation: pop .35s; }
@keyframes pop { 50% { transform: scale(1.06); } }

.streak { text-align: center; font-size: 22px; font-weight: 800; min-height: 28px; color: var(--coral); }

.keypad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.keypad button {
  min-height: 68px;
  border-radius: 20px;
  background: var(--card);
  border: 3px solid var(--line);
  box-shadow: 0 4px 0 var(--line);
  font-size: 34px; font-weight: 800;
}
.keypad button:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--line); }
.keypad .soft { color: var(--ink-soft); font-size: 28px; }
.keypad .go { background: var(--green); border-color: var(--green-dk); box-shadow: 0 4px 0 var(--green-dk); color: #fff; }

/* ---------- Done ---------- */
.done-box { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; text-align: center; }
.stars { font-size: 64px; letter-spacing: 6px; min-height: 80px; }
.stars span { display: inline-block; animation: star .5s backwards; }
.stars span.off { filter: grayscale(1); opacity: .25; }
@keyframes star { from { transform: scale(0) rotate(-90deg); } }
h2 { font-size: 40px; margin: 0; }
.tricky { width: 100%; }
.tricky .label { font-weight: 800; color: var(--ink-soft); margin: 10px 0 8px; font-size: 18px; }
.tricky-list { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.tricky-list span {
  background: var(--card); border: 2px solid var(--line);
  padding: 8px 14px; border-radius: 14px; font-weight: 800; font-size: 22px;
}
.done-box .modes { width: 100%; margin-top: 12px; }

/* ---------- Progress grid ---------- */
.grid { display: grid; grid-template-columns: repeat(13, 1fr); gap: 3px; }
.grid div {
  aspect-ratio: 1;
  display: flex; align-items: center; justify-content: center;
  border-radius: 6px;
  font-size: clamp(9px, 2.6vw, 15px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.grid .hd { background: none; color: var(--ink-soft); font-size: clamp(11px, 3vw, 17px); }
.lv0 { background: var(--lv0); color: transparent; }
.lv1 { background: var(--lv1); }
.lv2 { background: var(--lv2); }
.lv3 { background: var(--lv3); color: #23304A; }
.lv4 { background: var(--lv4); }
.lv5 { background: var(--lv5); color: #fff; }
.grid div.sel { outline: 3px solid var(--ink); outline-offset: 1px; }
.legend { display: flex; flex-wrap: wrap; justify-content: center; gap: 14px; font-weight: 700; color: var(--ink-soft); }
.legend span { display: flex; align-items: center; gap: 6px; }
.legend i { width: 18px; height: 18px; border-radius: 5px; display: inline-block; }
.grid-peek { text-align: center; font-size: 30px; font-weight: 800; min-height: 40px; margin: 0; }

/* ---------- Confetti ---------- */
.burst { position: fixed; inset: 0; pointer-events: none; overflow: hidden; }
.burst i {
  position: absolute; width: 12px; height: 12px; border-radius: 3px;
  animation: fall 1.1s ease-out forwards;
}
@keyframes fall {
  from { transform: translate(0,0) rotate(0); opacity: 1; }
  to { transform: translate(var(--dx), var(--dy)) rotate(540deg); opacity: 0; }
}

/* iPad: roomier layout */
@media (min-width: 700px) {
  #app { max-width: 760px; }
  .keypad button { min-height: 84px; font-size: 40px; }
  .tables { gap: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
