:root{
  color-scheme: dark;
  --bg:#0b1220;
  --bg2:#0f1a2e;
  --panel:#121f38;
  --panel2:#0e1830;
  --border:#1e2c4a;
  --border2:#27395e;
  --text:#e8edf7;
  --muted:#8da2c4;
  --accent:#5eead4;
  --accent2:#38bdf8;
  --shadow:0 10px 30px rgba(0,0,0,.45);
  --cell-bg:#15233f;
  --cell-bg2:#11203b;
  --sel:#fbbf24;
}
html[data-theme="light"]{
  color-scheme: light;
  --bg:#eef2f9;
  --bg2:#e3e9f4;
  --panel:#ffffff;
  --panel2:#f4f7fc;
  --border:#d8e0ee;
  --border2:#c3cee2;
  --text:#16203a;
  --muted:#5a6b8a;
  --accent:#0d9488;
  --accent2:#0284c7;
  --shadow:0 10px 30px rgba(20,40,80,.12);
  --cell-bg:#ffffff;
  --cell-bg2:#f0f4fb;
  --sel:#d97706;
}
@media (prefers-color-scheme: light){
  html:not([data-theme="dark"]):not([data-theme="light"]){
    color-scheme: light;
    --bg:#eef2f9; --bg2:#e3e9f4; --panel:#ffffff; --panel2:#f4f7fc;
    --border:#d8e0ee; --border2:#c3cee2; --text:#16203a; --muted:#5a6b8a;
    --accent:#0d9488; --accent2:#0284c7;
    --shadow:0 10px 30px rgba(20,40,80,.12);
    --cell-bg:#ffffff; --cell-bg2:#f0f4fb; --sel:#d97706;
  }
}

*{ box-sizing:border-box; }
html,body{ margin:0; padding:0; height:100%; }
body{
  background:radial-gradient(1200px 700px at 80% -10%, var(--bg2), var(--bg)) fixed;
  color:var(--text);
  font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
  overflow:hidden;
  touch-action:manipulation;
  -webkit-font-smoothing:antialiased;
}

#app{
  height:100%;
  display:flex;
  flex-direction:column;
  max-width:1240px;
  margin:0 auto;
  padding:14px 16px 16px;
  gap:12px;
}

/* Top bar */
.topbar{
  display:flex; align-items:center; justify-content:space-between; gap:16px;
  flex-wrap:wrap;
}
.brand{ display:flex; align-items:center; gap:12px; }
.logo{
  width:42px; height:42px; border-radius:12px;
  display:grid; place-items:center; font-size:22px;
  background:linear-gradient(135deg,#1f2d4d,#13203d);
  border:1px solid var(--border2);
  box-shadow:inset 0 1px 0 rgba(255,255,255,.05);
}
.brand-text h1{ margin:0; font-size:20px; font-weight:800; letter-spacing:-.01em; }
.tag{ margin:2px 0 0; font-size:12px; color:var(--muted); }

.progress-wrap{ display:flex; flex-direction:column; align-items:flex-end; gap:5px; min-width:180px; }
.progress-text{ font-size:12px; color:var(--muted); font-variant-numeric:tabular-nums; }
.progress-text span{ color:var(--text); font-weight:700; }
.progress-bar{ width:180px; height:7px; background:var(--panel2); border:1px solid var(--border); border-radius:999px; overflow:hidden; }
.progress-fill{ height:100%; width:0%; background:linear-gradient(90deg,var(--accent),var(--accent2)); border-radius:999px; transition:width .4s cubic-bezier(.2,.8,.2,1); }

/* Topics */
.topics{ display:flex; gap:8px; overflow-x:auto; padding:2px 1px 6px; scrollbar-width:thin; }
.topics::-webkit-scrollbar{ height:6px; }
.topics::-webkit-scrollbar-thumb{ background:var(--border2); border-radius:999px; }
.topic{
  flex:0 0 auto;
  display:flex; flex-direction:column; align-items:flex-start; gap:1px;
  padding:8px 14px; border-radius:11px;
  background:var(--panel); border:1px solid var(--border);
  color:var(--text); cursor:pointer; transition:all .18s ease;
  min-width:120px;
}
.topic:hover{ border-color:var(--border2); transform:translateY(-1px); }
.topic.active{ border-color:var(--accent); background:linear-gradient(180deg,rgba(94,234,212,.12),transparent); box-shadow:0 0 0 1px var(--accent) inset; }
.tcat{ font-size:10px; text-transform:uppercase; letter-spacing:.08em; color:var(--muted); font-weight:700; }
.ttitle{ font-size:13px; font-weight:700; }

/* Play area — grid dominant, word list compact sidebar */
.play{
  flex:1; min-height:0;
  display:grid;
  grid-template-columns:1fr 224px;
  gap:14px;
}
.board{
  position:relative;
  background:var(--panel); border:1px solid var(--border); border-radius:18px;
  padding:16px; box-shadow:var(--shadow);
  display:flex; flex-direction:column; gap:10px;
  min-width:0; min-height:0;
}
.grid-wrap{ position:relative; flex:1; min-height:0; display:flex; align-items:center; justify-content:center; }
/* grid-frame is exactly the size of the grid so the SVG overlay aligns precisely */
.grid-frame{
  position:relative;
  width:100%; height:100%;
  max-width:min(100%, calc(100vh - 220px));
  max-height:min(100%, calc(100vh - 220px));
  aspect-ratio:1/1;
}
.grid{
  display:grid; gap:3px;
  width:100%; height:100%;
  user-select:none;
}
.cell{
  background:var(--cell-bg);
  border:1px solid var(--border);
  border-radius:6px;
  display:grid; place-items:center;
  font-weight:700; font-size:clamp(11px, 2.1vw, 20px);
  color:var(--text);
  transition:background .12s ease, transform .08s ease, color .12s ease;
  cursor:pointer;
}
.cell:nth-child(2n){ background:var(--cell-bg2); }
.cell.selecting{ background:var(--sel); color:#1a1300; transform:scale(.94); box-shadow:0 0 0 2px rgba(251,191,36,.4); z-index:2; }
.cell.found{ font-weight:800; }

.overlay{ position:absolute; inset:0; width:100%; height:100%; pointer-events:none; }
.overlay line{ animation:pop .3s ease; }
@keyframes pop{ from{ stroke-width:.2; opacity:0; } to{ opacity:.42; } }

.hint{ margin:0; font-size:11.5px; color:var(--muted); text-align:center; }

/* Word panel — compact sidebar */
.wordpanel{
  background:var(--panel); border:1px solid var(--border); border-radius:18px;
  padding:14px; box-shadow:var(--shadow);
  display:flex; flex-direction:column; gap:10px; min-height:0;
}
.topic-head{ display:flex; flex-direction:column; gap:3px; }
.cat{ align-self:flex-start; font-size:9.5px; text-transform:uppercase; letter-spacing:.1em; font-weight:800; color:var(--accent); padding:3px 8px; border-radius:6px; background:rgba(94,234,212,.1); border:1px solid rgba(94,234,212,.25); }
.topic-head h2{ margin:0; font-size:16px; font-weight:800; letter-spacing:-.01em; line-height:1.15; }
.topic-sub{ margin:0; font-size:11px; color:var(--muted); }

.wordlist{
  list-style:none; margin:0; padding:0;
  display:flex; flex-direction:column; gap:3px;
  overflow-y:auto; flex:1; min-height:0;
  scrollbar-width:thin;
}
.wordlist::-webkit-scrollbar{ width:5px; }
.wordlist::-webkit-scrollbar-thumb{ background:var(--border2); border-radius:999px; }
.word{
  display:flex; align-items:center; gap:7px;
  padding:5px 8px; border-radius:8px;
  background:var(--panel2); border:1px solid var(--border);
  font-size:11.5px; font-weight:600; transition:all .2s ease;
}
.word.found{ opacity:.62; }
.word.found .wtext{ text-decoration:line-through; }
.dot{ width:8px; height:8px; border-radius:50%; border:1px solid var(--border2); flex:0 0 auto; }
.wtext{ flex:1; letter-spacing:.02em; }
.check{ color:var(--accent); font-weight:900; font-size:12px; }

.actions{ display:flex; gap:8px; }
.btn{
  flex:1; padding:8px 8px; border-radius:9px; cursor:pointer;
  font-size:11.5px; font-weight:700; border:1px solid var(--accent);
  background:var(--accent); color:#08221d; transition:all .15s ease;
}
.btn:hover{ filter:brightness(1.08); transform:translateY(-1px); }
.btn:active{ transform:translateY(0); }
.btn.ghost{ background:transparent; color:var(--accent); }
.btn.ghost:hover{ background:rgba(94,234,212,.1); }

/* Toast */
.toast{
  position:absolute; top:14px; left:50%; transform:translate(-50%,-12px);
  background:var(--text); color:var(--bg);
  padding:8px 16px; border-radius:999px; font-size:13px; font-weight:800;
  opacity:0; pointer-events:none; transition:all .25s ease;
  box-shadow:0 8px 22px rgba(0,0,0,.35); z-index:5;
  white-space:nowrap;
}
.toast.show{ opacity:1; transform:translate(-50%,0); }

/* Responsive — stack on narrow screens */
@media (max-width:900px){
  .play{ grid-template-columns:1fr; grid-template-rows:1fr auto; }
  .wordpanel{ max-height:42vh; }
  .grid-frame{ max-width:min(100%, calc(58vh)); max-height:min(100%, calc(58vh)); }
}
@media (max-width:560px){
  #app{ padding:10px 10px 12px; gap:10px; }
  .brand-text h1{ font-size:17px; }
  .progress-wrap{ min-width:140px; }
  .progress-bar{ width:140px; }
  .topic{ min-width:104px; padding:7px 11px; }
  .board{ padding:11px; }
  .grid-frame{ max-width:min(100%, calc(54vh)); max-height:min(100%, calc(54vh)); }
  .grid{ gap:2px; }
  .cell{ border-radius:5px; }
  .wordpanel{ padding:12px; gap:8px; max-height:40vh; }
}
@media (max-width:380px){
  .wordpanel{ max-height:36vh; }
  .grid-frame{ max-width:min(100%, calc(50vh)); max-height:min(100%, calc(50vh)); }
}
