:root {
  --bg: #F5F5F7;
  --ink: #1D1D1F;
  --ink-2: #6E6E73;
  --ink-3: #A1A1A6;
  --hair: rgba(0, 0, 0, 0.08);
  --fill: rgba(118, 118, 128, 0.12);
  --accent: #0071E3;
  --accent-ink: #FFFFFF;
  --warn: #C25E00;

  /* The easter egg: vermilion seal on paper. */
  --paper: #F3EEE5;
  --paper-ink: #2A2320;
  --paper-ink-2: #74685E;
  --seal: #C23B2A;
  --seal-shadow: 0 1px 1px rgba(80, 20, 10, 0.22), 0 18px 36px -16px rgba(110, 30, 15, 0.55);

  --sea: #E4E9ED;
  --sea-dot: #B8C2CA;
  --black: #1D1D1F;
  --white: #FFFFFF;
  --ghost-black: color-mix(in srgb, var(--black) 42%, var(--sea));
  --ghost-white: color-mix(in srgb, var(--white) 70%, var(--sea));

  --sheet: rgba(255, 255, 255, 0.82);
  --thumb: #FFFFFF;
  --thumb-shadow: 0 3px 8px rgba(0, 0, 0, 0.12), 0 0 0 0.5px rgba(0, 0, 0, 0.04);

  --shadow-black: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.22));
  --shadow-white: drop-shadow(0 0.6px 0.4px rgba(0, 0, 0, 0.32)) drop-shadow(0 2px 4px rgba(40, 55, 70, 0.16));

  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease: cubic-bezier(0.25, 0.1, 0.25, 1);
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Inter", "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", "Microsoft YaHei", sans-serif;
  --font-display: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Inter Display", "Inter", "PingFang SC", sans-serif;
  --font-serif: "Songti SC", "STSong", "Noto Serif CJK SC", "Source Han Serif SC", serif;

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --ink: #F5F5F7;
    --ink-2: #98989D;
    --ink-3: #636366;
    --hair: rgba(255, 255, 255, 0.1);
    --fill: rgba(118, 118, 128, 0.24);
    --accent: #0A84FF;
    --warn: #FF9F0A;

    --paper: #13110F;
    --paper-ink: #EEE6DA;
    --paper-ink-2: #A3978A;
    --seal: #CC4535;
    --seal-shadow: 0 1px 1px rgba(0, 0, 0, 0.5), 0 18px 36px -14px rgba(0, 0, 0, 0.8);

    --sea: #2E363D;
    --sea-dot: #4F5B65;
    --black: #0B0C0D;
    --white: #ECECEF;
    --ghost-black: color-mix(in srgb, var(--black) 55%, var(--sea));
    --ghost-white: color-mix(in srgb, var(--white) 38%, var(--sea));

    --sheet: rgba(30, 30, 32, 0.78);
    --thumb: #636366;
    --thumb-shadow: 0 3px 8px rgba(0, 0, 0, 0.3);

    --shadow-black: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.5));
    --shadow-white: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.55));
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  min-height: 100vh;
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- Header ---------- */

.bar {
  width: 100%;
  max-width: 1040px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(14px, env(safe-area-inset-top)) 20px 6px;
}

.wordmark {
  display: inline-flex;
  align-items: center;
  gap: 1px;
  margin-left: -8px;
  padding: 4px 10px 4px 4px;
  border: 0;
  border-radius: 10px;
  background: none;
  cursor: pointer;
  font-family: var(--font-display);
  font-size: 21px;
  font-weight: 600;
  letter-spacing: 0.04em;
  transition: background-color 0.2s var(--ease), transform 0.2s var(--ease);
}
.wordmark svg {
  width: 20px; height: 20px;
  fill: none; stroke: var(--ink-2); stroke-width: 2.2;
  stroke-linecap: round; stroke-linejoin: round;
  transition: transform 0.3s var(--ease-out);
}
.wordmark:active { transform: scale(0.96); }
@media (hover: hover) {
  .wordmark:hover { background: var(--fill); }
  .wordmark:hover svg { transform: translateX(-2px); }
}

.actions { display: flex; align-items: center; gap: 8px; }

.icon-btn, .pill {
  border: 0;
  cursor: pointer;
  background: var(--fill);
  transition: background-color 0.2s var(--ease), opacity 0.2s var(--ease), transform 0.2s var(--ease);
}
.icon-btn:active:not(:disabled), .pill:active, .btn:active { transform: scale(0.96); }

.icon-btn {
  width: 34px; height: 34px;
  border-radius: 50%;
  display: grid; place-items: center;
}
.icon-btn svg {
  width: 18px; height: 18px;
  fill: none; stroke: currentColor; stroke-width: 2;
  stroke-linecap: round; stroke-linejoin: round;
}
.icon-btn:disabled { opacity: 0.35; cursor: default; }

.pill {
  height: 34px;
  padding: 0 16px;
  border-radius: 17px;
  font-size: 14px;
  font-weight: 500;
}

@media (hover: hover) {
  .icon-btn:hover:not(:disabled), .pill:hover { background: color-mix(in srgb, var(--fill), var(--ink) 6%); }
}

:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent);
  outline-offset: 2px;
}

/* ---------- Stage ---------- */

.stage {
  --board: max(288px, min(calc(100vw - 32px), calc(100svh - 236px), 600px));
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 8px 16px max(28px, env(safe-area-inset-bottom));
}

.banner {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: var(--board);
  padding: 8px 8px 8px 16px;
  border-radius: 14px;
  background: var(--fill);
  font-size: 14px;
  animation: drop-in 0.4s var(--ease-out);
}
.banner[hidden] { display: none; }
.banner button {
  flex: none;
  border: 0;
  border-radius: 10px;
  padding: 5px 12px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}

@keyframes drop-in {
  from { opacity: 0; transform: translateY(-6px); }
}

/* ---------- Scoreboard ---------- */

.scoreboard {
  width: var(--board);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: end;
  padding: 0 4px;
}

.side {
  display: flex;
  flex-direction: column;
  gap: 2px;
  opacity: 0.42;
  transition: opacity 0.35s var(--ease);
}
.side.active { opacity: 1; }
.side.right { align-items: flex-end; }

.who {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 500;
}

.chip {
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--black);
}
.chip[data-color="2"] {
  background: var(--white);
  box-shadow: 0 0 0 0.5px rgba(0, 0, 0, 0.25), 0 1px 2px rgba(0, 0, 0, 0.15);
}
@media (prefers-color-scheme: dark) {
  .chip[data-color="1"] { box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.35); }
}
.side.thinking .chip { animation: breathe 1.4s var(--ease) infinite; }

@keyframes breathe {
  50% { transform: scale(0.72); opacity: 0.55; }
}

.score {
  display: flex;
  align-items: baseline;
  gap: 4px;
  font-family: var(--font-display);
}
.num {
  display: inline-block;
  min-width: 1.15ch;
  font-size: clamp(40px, 9vw, 56px);
  font-weight: 600;
  line-height: 1;
  letter-spacing: -0.03em;
  font-variant-numeric: tabular-nums;
}
.side.right .num { text-align: right; }
.num.bump { animation: bump 0.45s var(--spring); }
@keyframes bump {
  from { transform: translateY(6px); opacity: 0.3; }
}
.unit { font-size: 14px; color: var(--ink-2); font-weight: 500; }

.delta {
  min-width: 2.2em;
  font-family: var(--font);
  font-size: 14px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  opacity: 0;
  transform: translateY(3px);
  transition: opacity 0.18s var(--ease), transform 0.25s var(--ease-out);
}
.side.right .delta { text-align: right; }
.delta.show { opacity: 1; transform: none; }
.delta.down { color: var(--ink-2); }

.clock {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding-bottom: 6px;
}
.moves {
  font-size: 13px;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.moves #move-no { color: var(--ink); font-weight: 600; margin-right: 3px; }

.balance {
  position: relative;
  width: 72px;
  height: 3px;
  border-radius: 2px;
  background: var(--fill);
  overflow: hidden;
}
.balance[hidden] { display: none; }
.balance::after {
  content: "";
  position: absolute; left: 50%; top: -1px; bottom: -1px;
  width: 1px; background: var(--ink-3);
}
.balance i {
  position: absolute; top: 0; bottom: 0; left: 50%;
  width: 50%;
  background: var(--ink);
  transform-origin: left center;
  transform: scaleX(0);
  transition: transform 0.6s var(--ease-out);
}

/* ---------- Board ---------- */

.board {
  position: relative;
  width: var(--board);
  aspect-ratio: 1;
  padding: calc(var(--board) * 0.028);
  border-radius: calc(var(--board) * 0.05);
  background: var(--sea);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  outline: none;
  transition: box-shadow 0.25s var(--ease);
}
.board:focus-visible { box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 45%, transparent); }
.board.locked { cursor: default; }
.board.nope { animation: nope 0.32s var(--ease); }
@keyframes nope {
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

#svg { display: block; width: 100%; height: 100%; overflow: visible; }

.dot {
  fill: var(--sea-dot);
  transform-box: fill-box;
  transform-origin: center;
  transition: transform 0.3s var(--ease-out), opacity 0.2s var(--ease);
}
.dot.off { transform: scale(0); opacity: 0; }
.board.intro .dot { animation: dot-in 0.6s var(--ease-out) backwards; animation-delay: calc(var(--d) * var(--dot-step, 28ms)); }
@keyframes dot-in {
  from { transform: scale(0); opacity: 0; }
}

.layer { transition: opacity 0.3s var(--ease); }
.layer.l1 { filter: var(--shadow-black); }
.layer.l2 { filter: var(--shadow-white); }
.l1 .stone, .l1 .bridge { fill: var(--black); }
.l2 .stone, .l2 .bridge { fill: var(--white); }

.stone, .bridge {
  transform-box: fill-box;
  transform-origin: center;
  opacity: 0;
  transition: transform 0.2s var(--ease), opacity 0.16s var(--ease), fill 0.3s var(--ease);
}
.stone { transform: scale(0.35); }
.bridge.h { transform: scaleX(0); }
.bridge.v { transform: scaleY(0); }

.stone.on, .stone.ghost {
  opacity: 1;
  transform: none;
  transition: transform 0.5s var(--spring), opacity 0.14s var(--ease), fill 0.3s var(--ease);
}
.bridge.on, .bridge.ghost {
  opacity: 1;
  transform: none;
  transition: transform 0.42s var(--ease-out) 0.08s, opacity 0.1s var(--ease) 0.08s, fill 0.3s var(--ease);
}
.stone.ghost { transform: scale(0.94); }
.l1 .ghost { fill: var(--ghost-black); }
.l2 .ghost { fill: var(--ghost-white); }

.l1 .lit, .l2 .lit { fill: var(--accent); transition-duration: 0.12s; }

.board[data-spot="1"] .l2,
.board[data-spot="2"] .l1 { opacity: 0.16; }

.mark { pointer-events: none; }
.ring {
  fill: none;
  stroke: var(--accent);
  stroke-width: 5;
  transform-box: fill-box;
  transform-origin: center;
  opacity: 0;
  transform: scale(0.7);
  transition: opacity 0.18s var(--ease), transform 0.4s var(--spring);
}
.ring.show { opacity: 1; transform: none; }
.focus-ring { stroke: color-mix(in srgb, var(--accent) 60%, transparent); stroke-width: 4; }
.focus-wrap { transition: transform 0.18s var(--ease-out); }

.last {
  fill: var(--accent);
  opacity: 0;
  transform-box: fill-box;
  transform-origin: center;
  transform: scale(0);
  transition: opacity 0.2s var(--ease), transform 0.35s var(--spring);
}
.last.show { opacity: 1; transform: none; transition-delay: 0.2s; }

/* ---------- Hint ---------- */

.hint {
  margin: 0;
  min-height: 1.45em;
  max-width: var(--board);
  text-align: center;
  font-size: 15px;
  color: var(--ink-2);
  transition: opacity 0.18s var(--ease);
}
.hint.swap { opacity: 0; }
.hint .ellipsis::after {
  content: "";
  display: inline-block;
  width: 1.2em;
  text-align: left;
  animation: ellipsis 1.2s steps(4) infinite;
}
@keyframes ellipsis {
  0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; }
}

/* ---------- Dialogs ---------- */

dialog {
  width: min(440px, calc(100vw - 32px));
  max-width: none;
  padding: 0;
  border: 0;
  border-radius: 22px;
  color: var(--ink);
  background: var(--sheet);
  -webkit-backdrop-filter: blur(30px) saturate(180%);
  backdrop-filter: blur(30px) saturate(180%);
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.22), 0 0 0 0.5px var(--hair);
}
dialog[open] { animation: sheet-in 0.48s var(--ease-out); }
dialog.closing { animation: sheet-out 0.22s var(--ease) forwards; }
dialog::backdrop {
  background: rgba(0, 0, 0, 0.16);
  animation: fade-in 0.3s var(--ease);
}
dialog.closing::backdrop { animation: fade-out 0.22s var(--ease) forwards; }

@keyframes sheet-in { from { opacity: 0; transform: translateY(14px) scale(0.97); } }
@keyframes sheet-out { to { opacity: 0; transform: translateY(8px) scale(0.98); } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }

@media (max-width: 560px) {
  dialog {
    width: 100%;
    margin: auto 0 0;
    border-radius: 22px 22px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  @keyframes sheet-in { from { transform: translateY(100%); } }
  @keyframes sheet-out { to { transform: translateY(100%); } }
}

dialog form, .result-body { padding: 26px 24px 20px; }

dialog h2 {
  margin: 0 0 6px;
  font-family: var(--font-display);
  font-size: 24px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.rules {
  margin: 0 0 22px;
  color: var(--ink-2);
  font-size: 14px;
  line-height: 1.6;
}

.field { display: grid; gap: 8px; margin-bottom: 18px; }
.label { font-size: 13px; font-weight: 500; color: var(--ink-2); }

.seg {
  --count: 2;
  --index: 0;
  position: relative;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  padding: 2px;
  border-radius: 10px;
  background: var(--fill);
}
.seg label {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  height: 32px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label:has(input:focus-visible) { outline: 3px solid color-mix(in srgb, var(--accent) 55%, transparent); }
.seg label:has(input:checked) { font-weight: 600; }
.seg-thumb {
  position: absolute;
  top: 2px; bottom: 2px; left: 2px;
  width: calc((100% - 4px) / var(--count));
  border-radius: 8px;
  background: var(--thumb);
  box-shadow: var(--thumb-shadow);
  transform: translateX(calc(100% * var(--index)));
  transition: transform 0.38s var(--ease-out);
}

.collapse {
  display: grid;
  grid-template-rows: 1fr;
  transition: grid-template-rows 0.35s var(--ease-out), opacity 0.25s var(--ease);
}
.collapse.closed { grid-template-rows: 0fr; opacity: 0; }
.collapse-inner { overflow: hidden; min-height: 0; }

.buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 8px;
}
.buttons.single { grid-template-columns: 1fr; }
.btn {
  height: 46px;
  border: 0;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: transform 0.2s var(--ease), filter 0.2s var(--ease);
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.secondary { background: var(--fill); }
.btn[hidden] { display: none; }
@media (hover: hover) {
  .btn:hover { filter: brightness(1.06); }
}

.result-body { text-align: center; padding-top: 34px; }
.result-body h2 { font-size: 30px; }
.final {
  margin: 0 0 26px;
  color: var(--ink-2);
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 420px) {
  .bar { padding-left: 16px; padding-right: 16px; }
  .stage { gap: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    transition-delay: 0s !important;
  }
}

dialog h2:focus { outline: none; }

@media (max-width: 560px) and (min-height: 700px) {
  .stage { justify-content: flex-start; padding-top: 7vh; }
}

/* ---------- Views ---------- */

::selection { background: color-mix(in srgb, var(--accent) 24%, transparent); }

.view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100svh;
}
.view[hidden] { display: none; }
.view-enter { animation: view-in 0.5s var(--ease-out); }
@keyframes view-in { from { opacity: 0; transform: scale(1.012); filter: blur(6px); } }

::view-transition-old(root) { animation: vt-out 0.24s var(--ease) both; }
::view-transition-new(root) { animation: vt-in 0.5s var(--ease-out) both; }
@keyframes vt-out { to { opacity: 0; transform: scale(0.985); filter: blur(4px); } }
@keyframes vt-in { from { opacity: 0; transform: scale(1.012); filter: blur(6px); } }

/* The sea under every board morphs from one view to the next. */
#title-board, #board, .lesson-board .board, #egg-seal { view-transition-name: sea; }
::view-transition-group(sea) { animation-duration: 0.6s; animation-timing-function: var(--ease-out); }
::view-transition-old(sea), ::view-transition-new(sea) {
  width: 100%;
  height: 100%;
  object-fit: cover;
  animation-duration: 0.45s;
}

.link {
  display: inline-flex;
  align-items: center;
  gap: 1px;
  padding: 6px 2px;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 17px;
  cursor: pointer;
}
.link svg {
  width: 17px; height: 17px;
  fill: none; stroke: currentColor; stroke-width: 2.2;
  stroke-linecap: round; stroke-linejoin: round;
  transition: transform 0.3s var(--ease-out);
}
.link.small { font-size: inherit; padding: 0; }
.link.small svg { width: 14px; height: 14px; }
@media (hover: hover) {
  .link:hover span, .link.small:hover { text-decoration: underline; text-underline-offset: 3px; }
  .link:hover svg { transform: translateX(2px); }
}

/* ---------- Home ---------- */

.home {
  align-items: center;
  justify-content: center;
  padding: max(40px, env(safe-area-inset-top)) 16px max(32px, env(safe-area-inset-bottom));
}

.home-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

/* The title is a board: 赠岛 written in stones, sized so the whole screen still fits. */
.masthead {
  display: grid;
  justify-items: center;
  animation: fade-in 0.5s var(--ease-out);
}

.board.title-board {
  --cell: max(8px, min(
    calc((100vw - 32px) / (var(--cols) + 0.84)),
    calc((100svh - var(--reserve)) / (var(--rows) + 0.84)),
    30px));
  --pad: calc(var(--cell) * 0.42);
  --dot-step: 12ms;
  width: calc(var(--cols) * var(--cell) + 2 * var(--pad));
  height: calc(var(--rows) * var(--cell) + 2 * var(--pad));
  aspect-ratio: auto;
  padding: var(--pad);
  border-radius: calc(var(--cell) * 0.9);
}

/* Island counts, what to do next, and a way back to the bare title. */
.title-caption {
  width: 0;
  min-width: 100%;
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas: "tally hint restore";
  align-items: center;
  column-gap: 16px;
  min-height: 40px;
  margin-top: 6px;
  padding: 0 6px;
  font-size: 14px;
  color: var(--ink-2);
}
.title-board[data-layout="down"] ~ .title-caption {
  grid-template-columns: 1fr auto;
  grid-template-areas: "tally restore" "hint hint";
  row-gap: 2px;
}
.title-tally, .title-hint, .title-restore { opacity: 0; transition: opacity 0.6s var(--ease); }
.masthead.ready .title-tally, .masthead.ready .title-hint, .masthead.ready .title-restore { opacity: 1; }
.title-tally { grid-area: tally; }
.title-tally .tally { gap: 18px; animation: none; }
.title-tally .tally-num { font-size: 20px; }
.title-tally .delta { min-width: 1.8em; font-size: 13px; }
.title-hint {
  grid-area: hint;
  min-height: 1.45em;
  margin: 0;
  text-align: right;
  transition: opacity 0.16s var(--ease);
}
.title-board[data-layout="down"] ~ .title-caption .title-hint { text-align: left; }
.title-hint.swap { opacity: 0; }
.title-restore { grid-area: restore; gap: 4px; }
.title-restore[hidden] { display: none; }
.masthead.ready .title-restore:disabled { opacity: 0.4; cursor: default; }
.title-restore svg { width: 15px; height: 15px; }
.title-tally .tally-label { color: var(--ink); font-weight: 500; }
.title-hint.thinking::after {
  content: "";
  display: inline-block;
  width: 1.2em;
  text-align: left;
  animation: ellipsis 1.2s steps(4) infinite;
}

/* Once the title has been beaten, the seal sits on its corner like a chop on a painting. */
.masthead { position: relative; }
.corner-seal {
  position: absolute;
  right: -10px;
  bottom: 40px;
  z-index: 1;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: none;
  cursor: pointer;
  transform: rotate(-6deg);
  filter: drop-shadow(0 2px 3px rgba(90, 20, 10, 0.28));
  transition: transform 0.4s var(--spring), filter 0.3s var(--ease);
}
.corner-seal[hidden] { display: none; }
.corner-seal .seal { --cell: 2px; transform: none; box-shadow: none; transition: none; }
.corner-seal:active { transform: rotate(-4deg) scale(0.95); }
.corner-seal.stamping { animation: stamp-on 0.75s var(--spring) backwards; animation-delay: 0.55s; }
@keyframes stamp-on { from { opacity: 0; transform: rotate(-18deg) scale(2.4); } }
@media (hover: hover) {
  .corner-seal:hover { transform: rotate(-2deg) translateY(-3px) scale(1.06); filter: drop-shadow(0 6px 8px rgba(90, 20, 10, 0.3)); }
}
@media (hover: hover) {
  .link.title-restore:hover svg { transform: rotate(-50deg); }
}

.home-lede {
  margin: 18px 0 0;
  font-size: clamp(19px, 2.4vw, 23px);
  font-weight: 500;
  line-height: 1.45;
  color: var(--ink-2);
  text-wrap: balance;
}

.home-ctas {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 14px 26px;
  margin-top: 30px;
}

.cta {
  height: 46px;
  min-width: 132px;
  padding: 0 26px;
  border: 0;
  border-radius: 23px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 17px;
  font-weight: 500;
  cursor: pointer;
  transition: transform 0.2s var(--ease), filter 0.2s var(--ease);
}
.cta:active { transform: scale(0.96); }
@media (hover: hover) {
  .cta:hover { filter: brightness(1.08); }
}

.home-meta {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin: 14px 0 0;
  font-size: 13px;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
}
.home-meta[hidden] { display: none; }

.rise { animation: rise 0.9s var(--ease-out) backwards; animation-delay: calc(var(--i) * 80ms + 60ms); }
@keyframes rise { from { opacity: 0; transform: translateY(14px); filter: blur(8px); } }

/* ---------- Seal ---------- */

/* White stones carved into vermilion: 岛 above 主, sized by --cell. */
.seal {
  --cell: 10px;
  --pad: calc(var(--cell) * 0.95);
  --white: #FFF5E8;
  --shadow-white: none;
  position: relative;
  display: block;
  width: calc(var(--cols, 13) * var(--cell) + 2 * var(--pad));
  height: calc(var(--rows, 27) * var(--cell) + 2 * var(--pad));
  padding: var(--pad);
  border-radius: calc(var(--cell) * 0.9);
  background: var(--seal);
  box-shadow: var(--seal-shadow);
  transform: perspective(900px) rotateX(var(--rx, 0deg)) rotateY(var(--ry, 0deg)) rotate(-2deg);
  transition: transform 0.6s var(--ease-out);
}
.seal svg { display: block; width: 100%; height: 100%; overflow: visible; }

/* Carving: the strokes are cut in writing order, then the seal is pressed and the ink spreads. */
.seal { --carve-start: 0.55s; --carve-step: 13ms; --pressed: calc(var(--carve-start) + var(--stones, 99) * var(--carve-step) + 0.3s); }
.seal.carve .stone { animation: carve-in 0.45s var(--ease-out) backwards; animation-delay: calc(var(--carve-start) + var(--k) * var(--carve-step)); }
.seal.carve .bridge.h { animation: carve-h 0.36s var(--ease-out) backwards; animation-delay: calc(var(--carve-start) + 60ms + var(--k) * var(--carve-step)); }
.seal.carve .bridge.v { animation: carve-v 0.36s var(--ease-out) backwards; animation-delay: calc(var(--carve-start) + 60ms + var(--k) * var(--carve-step)); }
.seal.carve { animation: press 0.7s var(--ease-out); animation-delay: var(--pressed); }
.seal::after {
  content: "";
  position: absolute;
  inset: -16%;
  z-index: -1;
  border-radius: 50%;
  background: radial-gradient(closest-side, color-mix(in srgb, var(--seal) 30%, transparent), transparent);
  opacity: 0;
  pointer-events: none;
}
.seal.carve::after { animation: bloom 1.1s var(--ease-out); animation-delay: var(--pressed); }
@keyframes carve-in { from { opacity: 0; transform: scale(0.2); } }
@keyframes carve-h { from { transform: scaleX(0); } }
@keyframes carve-v { from { transform: scaleY(0); } }
@keyframes press {
  0% { transform: perspective(900px) rotate(-2deg) translateY(-8px) scale(1.03); }
  35% { transform: perspective(900px) rotate(-2deg) scale(0.955); }
}
@keyframes bloom {
  from { opacity: 1; transform: scale(0.8); }
  to { opacity: 0; transform: scale(1.18); }
}

/* ---------- Easter egg ---------- */

.egg {
  align-items: center;
  justify-content: center;
  padding: max(40px, env(safe-area-inset-top)) 24px max(32px, env(safe-area-inset-bottom));
  background: var(--paper);
  color: var(--paper-ink);
}
.egg ::selection { background: color-mix(in srgb, var(--seal) 22%, transparent); }

.egg-inner {
  width: 100%;
  max-width: 860px;
  display: grid;
  justify-items: center;
  gap: 34px;
  text-align: center;
}

.egg-art { display: flex; align-items: flex-start; gap: 16px; }
.egg .seal { --cell: max(6px, min(calc((100svh - 480px) / 28.9), 13px)); }
.egg-inscription {
  margin: 4px 0 0;
  writing-mode: vertical-rl;
  font-family: var(--font-serif);
  font-size: 15px;
  letter-spacing: 0.3em;
  color: var(--paper-ink-2);
}

.egg-title {
  margin: 0;
  font-family: var(--font-serif);
  font-size: clamp(34px, 5vw, 52px);
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: 0.02em;
}
.egg-body {
  max-width: 26em;
  margin: 14px auto 0;
  font-size: 16px;
  line-height: 1.75;
  color: var(--paper-ink-2);
  text-wrap: pretty;
}
.egg-score {
  margin: 14px 0 0;
  font-size: 13px;
  letter-spacing: 0.04em;
  color: var(--paper-ink-2);
  font-variant-numeric: tabular-nums;
}
.egg-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 14px 26px;
  margin-top: 28px;
}
.egg .cta { background: var(--paper-ink); color: var(--paper); }
.egg .link { color: var(--seal); }
.egg-text > * { animation: rise 0.9s var(--ease-out) backwards; }
.egg-text > :nth-child(1) { animation-delay: 0.7s; }
.egg-text > :nth-child(2) { animation-delay: 0.8s; }
.egg-text > :nth-child(3) { animation-delay: 0.9s; }
.egg-text > :nth-child(4) { animation-delay: 1s; }
.egg-inscription { animation: fade-in 1.2s var(--ease) backwards; animation-delay: 2.2s; }

@media (min-width: 880px) and (min-height: 560px) {
  .egg-inner {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    justify-items: start;
    column-gap: 96px;
    text-align: left;
  }
  .egg-art { order: 2; }
  .egg .seal { --cell: max(8px, min(calc((100svh - 200px) / 28.9), 15px)); }
  .egg-body { margin-left: 0; }
  .egg-actions { justify-content: flex-start; }
}

/* ---------- Learn ---------- */

.learn { padding: 0 20px max(24px, env(safe-area-inset-bottom)); }

.learn-bar {
  width: 100%;
  max-width: 1040px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 34px 1fr 34px;
  align-items: center;
  padding: max(14px, env(safe-area-inset-top)) 0 6px;
}

.progress { display: flex; justify-content: center; gap: 6px; }
.capsule {
  position: relative;
  width: 8px;
  height: 8px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: color-mix(in srgb, var(--ink-3) 40%, transparent);
  cursor: pointer;
  transition: width 0.5s var(--ease-out), background-color 0.3s var(--ease);
}
.capsule::after { content: ""; position: absolute; inset: -12px -3px; }
.capsule.done { background: var(--ink-2); }
.capsule.current { width: 28px; background: var(--ink); }
@media (hover: hover) {
  .capsule:not(.current):hover { background: var(--ink-2); }
}

.lesson {
  --board: max(232px, min(calc(100vw - 40px), calc(100svh - 440px), 380px));
  flex: 1;
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
  display: grid;
  grid-template-areas: "text" "stage" "feedback" "actions";
  justify-items: center;
  align-content: center;
  gap: 18px;
  padding: 12px 0 8px;
  text-align: center;
}
.lesson.leaving { animation: lesson-out 0.2s var(--ease) forwards; }
.lesson.entering { animation: lesson-in 0.55s var(--ease-out); }
@keyframes lesson-out { to { opacity: 0; transform: translateX(var(--dx)); filter: blur(6px); } }
@keyframes lesson-in { from { opacity: 0; transform: translateX(var(--dx)); filter: blur(8px); } }

.lesson-text { grid-area: text; }
.lesson-text h2 {
  margin: 0 0 8px;
  font-family: var(--font-display);
  font-size: clamp(28px, 5vw, 40px);
  font-weight: 600;
  line-height: 1.15;
  letter-spacing: -0.01em;
  text-wrap: balance;
}
.lesson-text p {
  margin: 0 auto;
  max-width: 28em;
  font-size: 16px;
  line-height: 1.65;
  color: var(--ink-2);
  text-wrap: pretty;
}

.lesson-stage { grid-area: stage; display: grid; justify-items: center; }
.lesson-board { display: grid; justify-items: center; gap: 10px; }
.board-head {
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tally {
  display: flex;
  align-items: center;
  gap: 26px;
  font-size: 14px;
  color: var(--ink-2);
  animation: drop-in 0.4s var(--ease-out);
}
.tally-side { display: inline-flex; align-items: center; gap: 6px; }
.tally-side .chip { margin-right: 2px; }
.tally-num {
  display: inline-block;
  min-width: 1ch;
  font-family: var(--font-display);
  font-size: 26px;
  font-weight: 600;
  line-height: 1;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}
.tally-num.bump { animation: bump 0.45s var(--spring); }
.tally .delta { min-width: 2em; text-align: left; }
.tally.counter { gap: 6px; font-size: 15px; }
.tally.counter .tally-num { font-size: 22px; margin-left: 2px; }

.lesson-feedback {
  grid-area: feedback;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 8px;
  max-width: min(100%, 30em);
  min-height: 3em;
  margin: 0;
  font-size: 15px;
  line-height: 1.55;
  color: var(--ink-2);
  text-wrap: pretty;
  transition: opacity 0.16s var(--ease), color 0.3s var(--ease);
}
.lesson-feedback.swap { opacity: 0; }
.lesson-feedback[data-tone="ok"], .lesson-feedback[data-tone="warn"] { color: var(--ink); }
.lesson-feedback svg {
  flex: none;
  width: 18px; height: 18px;
  margin-top: 2px;
  fill: none; stroke: var(--accent); stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}
.lesson-feedback[data-tone="warn"] svg { stroke: var(--warn); }
.lesson-feedback.again { animation: nudge 0.32s var(--ease); }
@keyframes nudge {
  30% { transform: translateX(-3px); }
  70% { transform: translateX(3px); }
}

.lesson-actions { grid-area: actions; display: flex; gap: 10px; }
.lesson-actions .btn { min-width: 132px; padding: 0 24px; }
.lesson-actions .btn:disabled { opacity: 0.32; cursor: default; filter: none; transform: none; }
.lesson-actions .btn.ready { animation: ready 0.6s var(--spring); }
@keyframes ready { from { transform: scale(0.9); } }

/* Islands found in lesson 2, and the island under the cursor (lesson 2 and the home title). */
.l1 .found { fill: var(--accent); }
.l1 .peek { fill: color-mix(in srgb, var(--black) 62%, var(--accent)); }
.l2 .peek { fill: color-mix(in srgb, var(--white) 70%, var(--accent)); }

.recap {
  display: grid;
  gap: 22px;
  width: min(100%, 460px);
  margin: 0;
  padding: 0;
  list-style: none;
  text-align: left;
}
.recap li {
  display: grid;
  grid-template-columns: 64px 1fr;
  gap: 16px;
  align-items: center;
  animation: rise 0.7s var(--ease-out) backwards;
}
.recap li:nth-child(2) { animation-delay: 90ms; }
.recap li:nth-child(3) { animation-delay: 180ms; }
.recap svg {
  width: 64px; height: 40px;
  padding: 4px 6px;
  border-radius: 10px;
  background: var(--sea);
  box-sizing: border-box;
}
.recap .s1 { fill: var(--black); }
.recap .s2 { fill: var(--white); }
.recap b { display: block; font-size: 17px; font-weight: 600; }
.recap span { font-size: 15px; color: var(--ink-2); }

@media (min-width: 880px) and (min-height: 560px) {
  .lesson {
    --board: max(300px, min(calc(100svh - 230px), 46vw, 460px));
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: 1fr auto 1fr;
    grid-template-areas: "text stage" "feedback stage" "actions stage";
    justify-items: start;
    align-content: center;
    column-gap: 72px;
    row-gap: 22px;
    text-align: left;
  }
  .lesson-text { align-self: end; }
  .lesson-text p { margin: 0; }
  .lesson-feedback { justify-content: flex-start; }
  .lesson-actions { align-self: start; }
  .lesson-stage { align-self: center; }
}

@media (max-width: 420px) {
  .learn { padding-left: 16px; padding-right: 16px; }
  .lesson { gap: 14px; }
  .lesson-actions { width: 100%; }
  .lesson-actions .btn { flex: 1; min-width: 0; }
}
