:root {
  --bg: #f1efe9;
  --surface: #fbfaf7;
  --surface-2: #e8e5dd;
  --card: #ffffff;
  --ink: #16171a;
  --muted: #6c6d72;
  --line: #d8d4ca;
  --line-strong: #16171a;
  --sel: #16171a;
  --good: #1f9d55;
  --bad: #e5484d;
  --c0: #e5484d;
  --c1: #3e63dd;
  --c2: #d99a1e;
  --shadow: 0 1px 0 rgba(0,0,0,.04), 0 6px 18px -10px rgba(0,0,0,.25);
  --ease: cubic-bezier(.2,.8,.2,1);
  --spring: cubic-bezier(.34,1.56,.64,1);
  --font: "Space Grotesk", system-ui, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e0f11; --surface: #15161a; --surface-2: #1f2025; --card: #1b1c21;
    --ink: #ecebe6; --muted: #8b8d94; --line: #2a2c32; --line-strong: #ecebe6; --sel: #ecebe6;
    --good: #3ccf7a; --bad: #ff6369;
    --c0: #ff6369; --c1: #7b93ff; --c2: #f2bd4b;
    --shadow: 0 1px 0 rgba(255,255,255,.03), 0 8px 20px -12px rgba(0,0,0,.8);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e0f11; --surface: #15161a; --surface-2: #1f2025; --card: #1b1c21;
  --ink: #ecebe6; --muted: #8b8d94; --line: #2a2c32; --line-strong: #ecebe6; --sel: #ecebe6;
  --good: #3ccf7a; --bad: #ff6369;
  --c0: #ff6369; --c1: #7b93ff; --c2: #f2bd4b;
  --shadow: 0 1px 0 rgba(255,255,255,.03), 0 8px 20px -12px rgba(0,0,0,.8);
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; border-radius: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  background: var(--bg); color: var(--ink); font-family: var(--font);
  font-size: 15px; line-height: 1.45; -webkit-font-smoothing: antialiased;
  transition: background .35s var(--ease), color .35s var(--ease);
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; }
svg { display: block; }

.app { height: 100dvh; display: grid; grid-template-rows: auto minmax(0, 1fr); }

/* ---------- header ---------- */
.top {
  display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 16px;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--surface);
  transition: background .35s var(--ease), border-color .35s var(--ease);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; letter-spacing: -.02em; }
.logo { display: flex; gap: 3px; }
.logo i { width: 7px; height: 18px; background: var(--c0); }
.logo i:nth-child(2) { background: var(--c1); height: 14px; margin-top: 2px; }
.logo i:nth-child(3) { background: var(--c2); height: 10px; margin-top: 4px; }
.name em { font-style: normal; font-weight: 500; color: var(--muted); margin-left: 6px; font-size: 14px; }

.stats { display: flex; justify-content: center; gap: 0; }
.stat { display: flex; flex-direction: column; align-items: center; padding: 0 18px; border-left: 1px solid var(--line); }
.stat:first-child { border-left: 0; }
.stat span { font-size: 10.5px; text-transform: uppercase; letter-spacing: .12em; color: var(--muted); }
.stat b { font-family: var(--mono); font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.2; }
.stat b.bump { animation: bump .45s var(--spring); }
@keyframes bump { 40% { transform: scale(1.35); } }

.actions { display: flex; gap: 6px; }
.btn {
  display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 12px;
  border: 1px solid var(--line); background: var(--surface); font-size: 13.5px; font-weight: 600;
  transition: background .2s var(--ease), border-color .2s var(--ease), transform .15s var(--ease), color .2s;
  position: relative;
}
.btn:hover { border-color: var(--line-strong); }
.btn:active { transform: translateY(1px); }
.btn.icon { width: 36px; padding: 0; justify-content: center; }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: square; stroke-linejoin: miter; }
.btn.primary { background: var(--ink); color: var(--bg); border-color: var(--ink); }
#themeBtn .moon { display: none; }
:root[data-theme="dark"] #themeBtn .sun { display: none; }
:root[data-theme="dark"] #themeBtn .moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) #themeBtn .sun { display: none; }
  :root:not([data-theme="light"]) #themeBtn .moon { display: block; }
}
#themeBtn svg { transition: transform .5s var(--spring); }
#themeBtn:active svg { transform: rotate(60deg); }
.chat-toggle { display: none !important; }
.dot { position: absolute; top: 5px; right: 5px; width: 7px; height: 7px; background: var(--bad); transform: scale(0); transition: transform .3s var(--spring); }
.dot.on { transform: scale(1); }

/* ---------- main ---------- */
.main { display: grid; grid-template-columns: 1fr clamp(320px, 28vw, 400px); min-height: 0; }
.play { display: grid; grid-template-rows: minmax(0, 1fr) auto; min-height: 0; min-width: 0; padding: 18px 18px 12px; gap: 12px; }
.board-wrap { position: relative; min-height: 0; min-width: 0; display: grid; place-items: center; }
.board { display: grid; gap: var(--gap, 12px); grid-template-columns: repeat(var(--cols, 4), var(--cw, 150px)); grid-auto-rows: var(--ch, 100px); }

.card {
  position: relative; background: var(--card); border: 1px solid var(--line); box-shadow: var(--shadow);
  display: grid; place-items: center; cursor: pointer; outline: none;
  transition: transform .28s var(--spring), border-color .2s var(--ease), box-shadow .25s var(--ease), background .35s var(--ease), opacity .3s;
  touch-action: manipulation; user-select: none;
}
.card svg { width: 100%; height: 100%; }
.card .shape { stroke-width: 3.2; transition: stroke .35s, fill .35s; }
.card .num { position: absolute; top: 5px; left: 7px; font-family: var(--mono); font-size: 10px; color: var(--muted); opacity: .7; }
@media (hover: hover) { .card:hover { transform: translateY(-2px); border-color: var(--muted); } }
.card:focus-visible { border-color: var(--ink); }
.card.sel { transform: translateY(-5px) scale(1.02); border-color: var(--sel); box-shadow: 0 0 0 2px var(--sel), 0 14px 26px -14px rgba(0,0,0,.45); }
.card.hint { animation: hint 1.2s var(--ease) infinite; }
@keyframes hint { 0%,100% { box-shadow: 0 0 0 2px var(--c2), var(--shadow); } 50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--c2) 30%, transparent), var(--shadow); } }
.card.bad { animation: shake .45s var(--ease); border-color: var(--bad); box-shadow: 0 0 0 2px var(--bad); }
@keyframes shake { 20% { transform: translateX(-6px); } 40% { transform: translateX(6px); } 60% { transform: translateX(-4px); } 80% { transform: translateX(3px); } }
.card.good { border-color: var(--good); box-shadow: 0 0 0 2px var(--good); animation: leave .5s .18s var(--ease) forwards; }
@keyframes leave { to { transform: translateY(-16px) scale(.85); opacity: 0; } }
.card.enter { animation: enter .5s var(--spring) both; animation-delay: var(--d, 0ms); }
@keyframes enter { from { transform: translateY(14px) scale(.92); opacity: 0; } }

.selbar { display: flex; align-items: center; justify-content: center; gap: 12px; font-size: 13.5px; color: var(--muted); min-height: 22px; }
.slots { display: flex; gap: 4px; }
.slots i { width: 10px; height: 10px; border: 1.5px solid var(--muted); transition: background .2s, border-color .2s, transform .3s var(--spring); }
.slots i.on { background: var(--ink); border-color: var(--ink); transform: scale(1.15); }

/* ---------- teacher ---------- */
.teacher { display: grid; grid-template-rows: auto 1fr auto auto; min-height: 0; border-left: 1px solid var(--line); background: var(--surface); transition: background .35s var(--ease), border-color .35s; }
.t-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.t-head b { display: block; font-size: 15px; }
.t-sub { font-size: 12px; color: var(--muted); }
.t-close { display: none !important; }
.log { overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; scrollbar-width: thin; overscroll-behavior: contain; }
.msg { max-width: 92%; padding: 9px 12px; font-size: 14px; animation: pop .35s var(--spring) both; overflow-wrap: anywhere; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } }
.msg.ai { background: var(--surface-2); align-self: flex-start; border-left: 3px solid var(--ink); }
.msg.user { background: var(--ink); color: var(--bg); align-self: flex-end; }
.msg.note { align-self: center; max-width: 100%; font-size: 12px; color: var(--muted); padding: 2px 0; text-align: center; }
.msg.good { border-left-color: var(--good); }
.msg.bad { border-left-color: var(--bad); }
.move { align-self: flex-end; display: flex; align-items: center; gap: 4px; padding: 6px; background: var(--surface-2); border: 1px solid var(--line); animation: pop .35s var(--spring) both; }
.move .mini { width: 56px; height: 38px; background: var(--card); border: 1px solid var(--line); }
.move .mark { width: 22px; height: 22px; display: grid; place-items: center; font-weight: 700; color: #fff; margin-left: 4px; font-size: 13px; }
.move.good .mark { background: var(--good); }
.move.bad .mark { background: var(--bad); }
.typing { display: flex; gap: 4px; padding: 12px 14px; }
.typing i { width: 6px; height: 6px; background: var(--muted); animation: blink 1s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; } .typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%,100% { opacity: .25; transform: translateY(0); } 50% { opacity: 1; transform: translateY(-3px); } }

.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 16px 0; }
.chips button { font-size: 12.5px; padding: 5px 9px; border: 1px solid var(--line); color: var(--muted); transition: color .2s, border-color .2s, background .2s; }
.chips button:hover { color: var(--ink); border-color: var(--ink); }
.ask { display: flex; gap: 6px; padding: 10px 16px 14px; }
.ask input { flex: 1; min-width: 0; height: 40px; padding: 0 12px; border: 1px solid var(--line); background: var(--bg); outline: none; transition: border-color .2s, background .35s; font-size: 14px; }
.ask input:focus { border-color: var(--ink); }
.ask .send { height: 40px; width: 40px; padding: 0; justify-content: center; background: var(--ink); color: var(--bg); border-color: var(--ink); }

/* ---------- overlays ---------- */
.overlay { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; background: color-mix(in srgb, var(--bg) 70%, transparent); backdrop-filter: blur(6px); opacity: 0; pointer-events: none; transition: opacity .3s var(--ease); z-index: 50; }
.overlay.open { opacity: 1; pointer-events: auto; }
.sheet { width: min(520px, 100%); max-height: calc(100dvh - 32px); overflow-y: auto; background: var(--surface); border: 1px solid var(--line-strong); padding: 24px; transform: translateY(16px) scale(.98); transition: transform .4s var(--spring); }
.overlay.open .sheet { transform: none; }
.sheet h2 { font-size: 22px; letter-spacing: -.02em; margin-bottom: 10px; }
.sheet p { margin-bottom: 12px; font-size: 14.5px; }
.muted { color: var(--muted); }
.feat { list-style: none; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 12px; }
.feat li { border: 1px solid var(--line); padding: 7px 10px; font-size: 13px; color: var(--muted); }
.feat b { display: block; color: var(--ink); font-size: 13.5px; }
.examples { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 14px; }
.examples small { display: block; font-size: 11.5px; color: var(--muted); margin-top: 6px; line-height: 1.3; }
.ex { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.ex .mini { aspect-ratio: 1.45; background: var(--card); border: 1px solid var(--line); }
.endstats { display: grid; grid-template-columns: repeat(4, 1fr); border: 1px solid var(--line); margin: 4px 0 18px; }
.endstats div { padding: 12px 8px; text-align: center; border-left: 1px solid var(--line); }
.endstats div:first-child { border-left: 0; }
.endstats span { display: block; font-size: 10.5px; text-transform: uppercase; letter-spacing: .12em; color: var(--muted); }
.endstats b { font-family: var(--mono); font-size: 20px; }
.row { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }

.scrim, .toast { display: none; }

/* ---------- small screens ---------- */
@media (max-width: 900px) {
  .main { grid-template-columns: 1fr; }
  .chat-toggle { display: inline-flex !important; }
  .t-close { display: inline-flex !important; }
  .teacher {
    position: fixed; left: 0; right: 0; bottom: 0; height: min(72dvh, 560px); z-index: 40;
    border-left: 0; border-top: 1px solid var(--line-strong);
    transform: translateY(102%); transition: transform .42s var(--ease), background .35s;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .teacher.open { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; z-index: 39; background: rgba(0,0,0,.35); opacity: 0; pointer-events: none; transition: opacity .3s; }
  .scrim.open { opacity: 1; pointer-events: auto; }
  .toast {
    display: flex; align-items: center; gap: 10px; position: fixed; left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom)); z-index: 30;
    background: var(--ink); color: var(--bg); padding: 10px 10px 10px 14px; font-size: 13.5px;
    transform: translateY(140%); transition: transform .4s var(--spring);
  }
  .toast.show { transform: none; }
  .toast span { flex: 1; }
  .toast button { background: var(--bg); color: var(--ink); padding: 6px 10px; font-weight: 600; font-size: 13px; }
}
@media (max-width: 640px) {
  .top { grid-template-columns: 1fr auto; grid-template-rows: auto auto; padding: 8px 12px; gap: 8px 10px; }
  .stats { grid-column: 1 / -1; grid-row: 2; justify-content: space-between; }
  .stat { flex: 1; padding: 0 4px; }
  .stat b { font-size: 16px; }
  .btn span { display: none; }
  .btn { padding: 0; width: 36px; justify-content: center; }
  .name em { display: none; }
  .play { padding: 12px 12px 8px; gap: 8px; }
  .selbar { font-size: 12.5px; }
}
@media (max-height: 520px) and (min-width: 641px) {
  .top { padding: 6px 12px; }
  .play { padding: 10px 12px 6px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
