@font-face { font-family: "Fredoka"; font-weight: 500; font-display: swap; src: url("/fonts/fredoka-500.woff2") format("woff2"); }
@font-face { font-family: "Fredoka"; font-weight: 600; font-display: swap; src: url("/fonts/fredoka-600.woff2") format("woff2"); }
@font-face { font-family: "Andika"; font-weight: 400; font-display: swap; src: url("/fonts/andika-400.woff2") format("woff2"); }
@font-face { font-family: "Andika"; font-weight: 700; font-display: swap; src: url("/fonts/andika-700.woff2") format("woff2"); }

:root {
  --bg: #e9f0f6;
  --paper: #ffffff;
  --grid: #d3e2ee;
  --grid-strong: #9fbdd6;
  --ink: #233140;
  --muted: #5a6b7c;
  --pen: #1b5ccf;
  --red: #d23a2b;
  --band: rgba(255, 205, 70, 0.30);
  --sun: #f4b000;
  --good: #188a55;
  --btn: #ffffff;
  --btn-line: #c5d5e3;
  --shadow: 0 1px 0 rgba(35, 49, 64, 0.06), 0 10px 28px rgba(35, 49, 64, 0.10);
  --display: "Fredoka", "Trebuchet MS", "Segoe UI", sans-serif;
  --body: "Andika", "Segoe UI", system-ui, sans-serif;
  --touch: 48px;
  --gutter-left: max(16px, env(safe-area-inset-left, 0px));
  --gutter-right: max(16px, env(safe-area-inset-right, 0px));
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1620; --paper: #15202c; --grid: #22313f; --grid-strong: #3a5670; --ink: #e6eef6; --muted: #9db0c3;
    --pen: #7fb3ff; --red: #ff8070; --band: rgba(255, 200, 70, 0.13); --sun: #ffc93c; --good: #4fd394;
    --btn: #1b2835; --btn-line: #2f4459; --shadow: 0 10px 28px rgba(0, 0, 0, 0.35); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1620; --paper: #15202c; --grid: #22313f; --grid-strong: #3a5670; --ink: #e6eef6; --muted: #9db0c3;
  --pen: #7fb3ff; --red: #ff8070; --band: rgba(255, 200, 70, 0.13); --sun: #ffc93c; --good: #4fd394;
  --btn: #1b2835; --btn-line: #2f4459; --shadow: 0 10px 28px rgba(0, 0, 0, 0.35); color-scheme: dark;
}

html, body { margin: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  background: var(--bg); color: var(--ink); font-family: var(--body); font-size: 17px; line-height: 1.45;
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 28px 28px;
}
button { font-family: inherit; color: inherit; touch-action: manipulation; }
button:focus-visible, a:focus-visible { outline: 3px solid var(--pen); outline-offset: 2px; }
[hidden] { display: none !important; }

.wrap {
  max-width: 820px; margin: 0 auto;
  padding-block: 12px max(32px, env(safe-area-inset-bottom, 0px));
  padding-left: var(--gutter-left);
  padding-right: var(--gutter-right);
  display: grid; gap: 14px;
  box-sizing: border-box;
}

.top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; }
.home {
  display: grid; place-items: center; width: var(--touch); height: var(--touch); flex: 0 0 auto;
  border-radius: 14px; background: var(--btn); border: 2px solid var(--btn-line); box-sizing: border-box; color: var(--pen);
}
.home svg { width: 28px; height: 28px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.brand { display: grid; gap: 0; min-width: 0; flex: 1 1 140px; }
.brand h1 { margin: 0; font-family: var(--display); font-weight: 600; font-size: clamp(26px, 6vw, 42px); line-height: 1.05; letter-spacing: 0.005em; text-wrap: balance; }
.brand p { margin: 0; color: var(--muted); font-size: 15px; }
.stats { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px; box-sizing: border-box;
  background: var(--btn); border: 1px solid var(--btn-line); font-weight: 700; font-variant-numeric: tabular-nums; font-size: 16px;
}
.chip .star { width: 20px; height: 20px; }
.chip.lang { cursor: pointer; min-height: var(--touch); min-width: var(--touch); }

.gear { display: none; cursor: pointer; min-height: var(--touch); min-width: var(--touch); justify-content: center; }
.gear svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.gear[aria-expanded="true"] { background: var(--ink); color: var(--paper); }

.star path { fill: transparent; stroke: var(--ink); stroke-width: 1.2; stroke-linejoin: round; }
.star-on path { fill: var(--sun); }

.settings { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; }
.seg { display: inline-flex; background: var(--btn); border: 1px solid var(--btn-line); border-radius: 12px; padding: 3px; gap: 2px; }
.seg button {
  border: 0; background: transparent; padding: 0 12px; min-height: var(--touch); min-width: var(--touch); border-radius: 9px; font-size: 16px; font-weight: 700; cursor: pointer; color: var(--muted);
}
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.seg-label { font-size: 14px; color: var(--muted); margin-right: 4px; }
.group { display: inline-flex; align-items: center; gap: 6px; }
.toggle { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 15px; color: var(--muted); user-select: none; min-height: var(--touch); }
.toggle input { width: 24px; height: 24px; accent-color: var(--pen); }

.picker { flex-wrap: wrap; }
.picker button { padding: 0 6px; }
.settings[data-level="table"] .only-column, .settings[data-level="column"] .only-table { display: none; }
.settings .group { flex-wrap: wrap; }

.stage { display: grid; gap: 14px; min-width: 0; }
.book {
  --cols: 4;
  --cell: min(clamp(48px, 13vw, 76px), calc((100vw - var(--gutter-left) - var(--gutter-right)) / (var(--cols) + 1)));
  width: fit-content; max-width: 100%; margin-inline: auto; box-sizing: border-box;
  padding: calc(var(--cell) * 0.5);
  background-color: var(--paper);
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: var(--cell) var(--cell);
  background-position: calc(var(--cell) * 0.5) 0;
  border-radius: 6px; box-shadow: var(--shadow);
  position: relative;
}
.book::before {
  content: ""; position: absolute; top: 0; bottom: 0; left: calc(var(--cell) * 0.5 - 2px); width: 2px; background: var(--red); opacity: 0.35;
}
.sheet { display: grid; grid-auto-flow: row; }
.row { display: grid; grid-template-columns: repeat(var(--cols), var(--cell)); }
.labels .cell { height: calc(var(--cell) * 0.5); font-size: calc(var(--cell) * 0.24); color: var(--muted); font-weight: 700; letter-spacing: 0.06em; align-items: end; padding-bottom: 2px; box-sizing: border-box; }
.cell { width: var(--cell); height: var(--cell); display: grid; place-items: center; position: relative; font-variant-numeric: tabular-nums; }
.cell.on { background: var(--band); }
.digit { font-size: calc(var(--cell) * 0.72); line-height: 1; font-weight: 400; color: var(--ink); position: relative; }
.op { font-size: calc(var(--cell) * 0.62); color: var(--ink); }
.ansrow .cell { box-shadow: inset 0 3px 0 var(--ink); }

.box {
  width: 80%; height: 80%; border: 2px dashed var(--grid-strong); border-radius: 10px; display: grid; place-items: center; box-sizing: border-box;
  font-size: calc(var(--cell) * 0.66); color: var(--pen); line-height: 1; position: relative;
}
.box.filled { border-color: transparent; }
.box.active { border: 3px solid var(--pen); background: var(--paper); box-shadow: 0 0 0 5px color-mix(in srgb, var(--pen) 20%, transparent); }
.box.active::after { content: ""; width: 3px; height: 55%; background: var(--pen); border-radius: 2px; animation: blink 1s steps(1) infinite; }
.cbox {
  width: 52%; height: 52%; align-self: end; margin-bottom: 4%; border: 2px dashed color-mix(in srgb, var(--red) 55%, transparent); border-radius: 8px; box-sizing: border-box;
  display: grid; place-items: center; font-size: calc(var(--cell) * 0.38); color: var(--red); font-weight: 700; line-height: 1; background: var(--paper);
}
.cbox.filled { border-style: solid; border-color: transparent; background: transparent; }
.cbox.active { border: 3px solid var(--red); box-shadow: 0 0 0 4px color-mix(in srgb, var(--red) 20%, transparent); }
.cbox.active::after { content: ""; width: 2px; height: 55%; background: var(--red); animation: blink 1s steps(1) infinite; }
.hist { align-self: end; margin-bottom: 6%; display: flex; gap: 4px; align-items: baseline; color: var(--red); font-weight: 700; font-size: calc(var(--cell) * 0.36); white-space: nowrap; }
.hist s { opacity: 0.6; text-decoration-thickness: 2px; }

.struck::after {
  content: ""; position: absolute; left: -8%; right: -8%; top: 52%; border-top: 3px solid var(--red); transform: rotate(-24deg);
}
.tap {
  border: 0; background: transparent; padding: 0; width: 100%; height: 100%; display: grid; place-items: center; cursor: default; position: relative;
}
.tap.lendable { cursor: pointer; }
.tap.lendable .digit { color: var(--red); }
.tap.lendable::before {
  content: ""; position: absolute; inset: 8%; border: 3px dashed var(--red); border-radius: 12px; animation: pulse 1.4s ease-in-out infinite;
}

.pop { animation: pop 0.45s ease-out; }
.shake { animation: shake 0.42s ease-in-out; }
@keyframes blink { 50% { opacity: 0; } }
@keyframes pop { 0% { transform: scale(0.4); opacity: 0; } 70% { transform: scale(1.15); opacity: 1; } 100% { transform: scale(1); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-7px); } 40% { transform: translateX(7px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(4px); } }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.35; } }

.coach { display: flex; gap: 12px; align-items: flex-end; max-width: 640px; margin-inline: auto; width: 100%; min-width: 0; }
.alice {
  flex: 0 0 auto; width: clamp(84px, 24vw, 170px); aspect-ratio: 3 / 4; position: relative; display: grid; place-items: center;
}
.alice canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.alice .sun { width: 60%; height: auto; }
.alice[data-alice="ready"] .sun { display: none; }
.sun-face { fill: var(--sun); }
.sun-eye { fill: var(--ink); }
.sun-mouth { fill: none; stroke: var(--ink); stroke-width: 3; stroke-linecap: round; }

.bubble {
  flex: 1 1 auto; min-width: 0; background: var(--paper); border: 2px solid var(--btn-line); border-radius: 18px 18px 18px 4px;
  padding: 10px 14px; font-size: 17px; line-height: 1.4; overflow-wrap: anywhere;
}
.bubble b { color: var(--pen); font-weight: 700; }
.bubble .r { color: var(--red); font-weight: 700; }
.bubble .good { color: var(--good); font-weight: 700; }
.bubble .actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.stars-earned { display: inline-flex; gap: 4px; vertical-align: middle; margin-left: 6px; }
.stars-earned .star { width: 26px; height: 26px; }

.lower { display: flex; flex-wrap: wrap; gap: 12px 16px; justify-content: center; align-items: flex-start; }
.pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; width: min(100%, 300px); }
.pad button {
  min-height: 54px; font-size: 30px; font-weight: 700; border-radius: 14px; border: 2px solid var(--btn-line); background: var(--btn);
  cursor: pointer; box-shadow: 0 3px 0 var(--btn-line); transition: transform 0.06s;
  font-variant-numeric: tabular-nums;
}
.pad button:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--btn-line); }
.pad .zero { grid-column: 2; grid-row: 4; }
.pad .borrowkey { grid-column: 1; grid-row: 4; font-size: 18px; color: var(--red); border-color: var(--red); box-shadow: 0 3px 0 color-mix(in srgb, var(--red) 50%, transparent); }
.side { display: grid; gap: 8px; width: min(100%, 300px); grid-template-columns: repeat(3, 1fr); }
.big {
  min-height: 52px; border-radius: 14px; font-size: 16px; font-weight: 700; cursor: pointer; border: 2px solid var(--btn-line); background: var(--btn);
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 0 8px; line-height: 1.15;
}
.big:disabled { opacity: 0.5; cursor: default; }
.big.primary { background: var(--pen); border-color: var(--pen); color: var(--paper); }
.big.redbtn { border-color: var(--red); color: var(--red); padding-inline: 18px; }
.big.wiggle { animation: pulse 1.4s ease-in-out infinite; }

.help { grid-column: 1 / -1; background: var(--paper); border: 2px solid var(--btn-line); border-radius: 14px; padding: 12px 14px; display: grid; gap: 8px; }
.help p { margin: 0; font-size: 16px; }
.help svg { max-width: 100%; height: auto; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 15px; color: var(--muted); }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }
.frame { fill: none; stroke: var(--grid-strong); stroke-width: 2; }
.dot-empty { fill: none; stroke: var(--grid); stroke-width: 2; }
.dot-cross { stroke: var(--red); stroke-width: 3; stroke-linecap: round; fill: none; }
.fill-red { fill: var(--red); }
.fill-ink { fill: var(--ink); }
.fill-pen { fill: var(--pen); }
.bg-red { background: var(--red); }
.bg-ink { background: var(--ink); }
.bg-pen { background: var(--pen); }
.ten-bar { fill: var(--red); stroke: var(--paper); stroke-width: 1.5; }
.swap-arrow { fill: none; stroke: var(--ink); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }

.foot { text-align: center; color: var(--muted); font-size: 14px; margin: 0; }
#fx { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 20; }

.front { max-width: 900px; }
.hero { display: flex; gap: 14px; align-items: flex-end; }
.alice-big { width: clamp(130px, 38vw, 260px); }
.hero .bubble { font-family: var(--display); font-weight: 500; font-size: clamp(19px, 4vw, 26px); margin-bottom: 18px; }
.cards { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 240px), 1fr)); }
.card {
  display: grid; gap: 4px; padding: 16px 18px; border-radius: 20px; text-decoration: none; color: var(--ink); background: var(--paper);
  border: 3px solid var(--btn-line); box-shadow: var(--shadow); min-height: 150px; box-sizing: border-box; transition: transform 0.1s;
}
.card:hover { transform: translateY(-2px); border-color: var(--pen); }
.card h2 { margin: 0; font-family: var(--display); font-weight: 600; font-size: 26px; line-height: 1.1; }
.card p { margin: 0; color: var(--muted); font-size: 16px; }
.card-picture {
  justify-self: start; font-family: var(--display); font-weight: 500; font-size: 22px; color: var(--pen); padding: 4px 12px; border-radius: 10px; background: var(--band);
  font-variant-numeric: tabular-nums;
}

.gloser[data-view="lists"] .stage .word-card, .gloser[data-view="lists"] .word-side { display: none; }
.gloser[data-view="play"] .lists { display: none; }
.lists { display: grid; gap: 10px; grid-template-columns: repeat(auto-fill, minmax(min(100%, 140px), 1fr)); }
.list-button {
  min-height: 64px; padding: 8px 12px; border-radius: 16px; border: 3px solid var(--btn-line); background: var(--paper); cursor: pointer;
  font-family: var(--display); font-weight: 500; font-size: 20px; line-height: 1.15; box-shadow: var(--shadow);
}
.list-button:hover { border-color: var(--pen); }
.word-card {
  display: grid; gap: 8px; padding: 12px 16px 14px; border-radius: 6px; box-shadow: var(--shadow); min-width: 0;
  background-color: var(--paper);
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 28px 28px;
}
.word-progress { font-size: 14px; color: var(--muted); font-weight: 700; }
.word { font-family: var(--display); font-weight: 600; font-size: clamp(36px, 11vw, 60px); line-height: 1.1; text-align: center; overflow-wrap: anywhere; min-height: 1.1em; }
.answer-row { display: flex; gap: 8px; min-width: 0; }
.answer-row input {
  flex: 1 1 auto; min-width: 0; min-height: 52px; box-sizing: border-box; padding: 0 14px; border-radius: 14px; border: 3px solid var(--pen);
  background: var(--paper); color: var(--pen); font-family: var(--body); font-size: 24px;
}
.answer-row input:disabled { border-color: var(--btn-line); }
.answer-row .big { flex: 0 0 auto; min-width: 88px; padding-inline: 14px; font-size: 18px; }
.hint-line { min-height: 1.4em; color: var(--red); font-weight: 700; font-size: 18px; text-align: center; }
.gloser[data-phase="done"] .word, .gloser[data-phase="done"] .answer-row input, .gloser[data-phase="done"] .hint-line, .gloser[data-phase="done"] .word-progress { display: none; }
.gloser[data-phase="done"] .answer-row .big { flex: 1 1 auto; }
.word-side { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; max-width: 640px; width: 100%; margin-inline: auto; }

.errorpage { max-width: 520px; margin: 48px auto; padding: 24px; text-align: center; background: var(--paper); border-radius: 20px; box-shadow: var(--shadow); }
.errorpage h1 { font-family: var(--display); margin: 0; font-size: 48px; }
.errorpage a { display: inline-grid; place-items: center; min-height: var(--touch); padding: 0 18px; color: var(--pen); font-weight: 700; }

/* phone upright: sum, Alice and keypad on one screen; the settings open from the top row */
@media (max-width: 600px) and (orientation: portrait) {
  .wrap.game { gap: 10px; padding-top: 8px; }
  .game .brand p, .game .foot { display: none; }
  .game .top { gap: 6px 8px; }
  .game .stats { flex: 1 1 100%; justify-content: space-between; }
  .gear { display: inline-flex; }
  .game .settings { display: none; }
  .game .settings.open { display: flex; }
  .stage { gap: 10px; }
  .book { padding: calc(var(--cell) * 0.25) calc(var(--cell) * 0.5); }
  .lower:not(.word-side) { display: grid; grid-template-columns: minmax(0, 1fr) 104px; gap: 8px; align-items: stretch; }
  .pad { width: 100%; grid-row: 1 / span 3; grid-column: 1; }
  .side { display: contents; }
  .side .big { grid-column: 2; }
  .help { grid-column: 1 / -1; }
}

/* phone in landscape: sum and Alice on the left, keypad on the right */
@media (orientation: landscape) and (max-height: 520px) {
  .wrap.game:not(.gloser) { max-width: none; grid-template-columns: minmax(0, 1fr) minmax(0, 300px); align-items: start; }
  .game .top, .game .settings, .game .foot { grid-column: 1 / -1; }
  .game .lower { grid-column: 2; grid-row: 3; }
  .game .stage { grid-column: 1; grid-row: 3; }
  .book { --cell: min(44px, calc((100vw - 380px) / (var(--cols) + 1))); }
  .alice { width: 96px; }
}

/* room for two columns: the page fits one screen */
@media (min-width: 1000px) {
  .wrap.game:not(.gloser) { max-width: 1120px; grid-template-columns: minmax(0, 1fr) 320px; align-items: start; column-gap: 28px; }
  .game .top, .game .settings, .game .foot { grid-column: 1 / -1; }
  .game .side { grid-template-columns: 1fr; }
  .book { --cell: min(68px, 8.5vh); }
  .alice { width: 150px; }
  .big { font-size: 18px; }
}

@media (prefers-reduced-motion: reduce) {
  .pop, .shake, .tap.lendable::before, .big.wiggle, .box.active::after, .cbox.active::after { animation: none; }
  .card { transition: none; }
}
