:root {
  --bg: #f1f3f5;
  --panel: #ffffff;
  --ink: #1d2733;
  --ink-soft: #445;
  --accent: #264653;
  --accent-ink: #ffffff;
  --good: #2a9d8f;
  --good-bg: #e8f8f2;
  --good-ink: #17795f;
  --bad: #e76f51;
  --bad-bg: #fdeeea;
  --bad-ink: #b3491f;
  --cell-border: rgba(0,0,0,0.08);
  --tile-border: #cfd8dc;
  --tile-bg: #ffffff;
  --hex-line: #264653;
  --hex-face-bg: #f8f9fa;
  --overlay: rgba(10,15,20,0.92);
  --shadow: 0 2px 10px rgba(0,0,0,0.06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #10161c;
    --panel: #1b232c;
    --ink: #e8ecef;
    --ink-soft: #aab4bd;
    --accent: #16232c;
    --accent-ink: #e8ecef;
    --good: #34b39a;
    --good-bg: #12332c;
    --good-ink: #6fe0c6;
    --bad: #e0765a;
    --bad-bg: #3a2019;
    --bad-ink: #ff9d80;
    --cell-border: rgba(255,255,255,0.10);
    --tile-border: #3a4652;
    --tile-bg: #232d37;
    --hex-line: #cfd8dc;
    --hex-face-bg: #232d37;
    --overlay: rgba(0,0,0,0.92);
    --shadow: 0 2px 10px rgba(0,0,0,0.3);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #10161c;
  --panel: #1b232c;
  --ink: #e8ecef;
  --ink-soft: #aab4bd;
  --accent: #16232c;
  --accent-ink: #e8ecef;
  --good: #34b39a;
  --good-bg: #12332c;
  --good-ink: #6fe0c6;
  --bad: #e0765a;
  --bad-bg: #3a2019;
  --bad-ink: #ff9d80;
  --cell-border: rgba(255,255,255,0.10);
  --tile-border: #3a4652;
  --tile-bg: #232d37;
  --hex-line: #cfd8dc;
  --hex-face-bg: #232d37;
  --overlay: rgba(0,0,0,0.92);
  --shadow: 0 2px 10px rgba(0,0,0,0.3);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overscroll-behavior: none; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: 100vh;
  overflow: hidden;
  transition: background 0.2s, color 0.2s;
}

/* ---- top bar ---- */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  background: var(--accent);
  color: var(--accent-ink);
  flex: 0 0 auto;
  z-index: 45;
}
.icon-btn { background: none; border: none; cursor: pointer; padding: 6px; color: inherit; font-size: 1.1rem; line-height: 1; }
.burger { display: inline-flex; flex-direction: column; gap: 4px; width: 22px; }
.burger span { display: block; height: 3px; background: currentColor; border-radius: 2px; }
.title { font-size: 1.05rem; margin: 0; flex: 1; }
.hud { display: flex; align-items: center; gap: 8px; }
.difficulty-badge { background: rgba(255,255,255,0.18); border-radius: 12px; padding: 3px 10px; font-size: 0.78rem; }
.coin-total { display: flex; align-items: center; gap: 4px; font-weight: 700; font-size: 1rem; }
.coin-icon { display: inline-flex; align-items:center; justify-content:center; width:20px; height:20px; background:#f4a261; color:#5c3d00; border-radius:50%; font-size:0.8rem; }

/* ---- drawer ---- */
.drawer {
  position: fixed; top: 0; left: -280px; width: 260px; height: 100%;
  background: var(--panel); box-shadow: 2px 0 12px rgba(0,0,0,0.2);
  padding: 60px 16px 16px; transition: left 0.25s ease; z-index: 40; overflow-y: auto;
  color: var(--ink);
}
.drawer.open { left: 0; }
.drawer h2 { font-size: 1rem; margin: 0 0 10px; }
.net-family-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.net-family-list li { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 8px; cursor: pointer; border: 2px solid transparent; }
.net-family-list li:hover { background: var(--bg); }
.net-family-list li.selected { border-color: var(--good); background: var(--good-bg); }
.family-thumb { width: 36px; height: 28px; flex-shrink: 0; color: #457b9d; }
.family-thumb svg { width: 100%; height: 100%; }
.drawer-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.3); opacity: 0; pointer-events: none; transition: opacity 0.25s; z-index: 30; }
.drawer-overlay.open { opacity: 1; pointer-events: auto; }

/* ---- play area: fills remaining viewport, never scrolls ---- */
.play-area {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 8px 8px 10px;
  max-width: 920px;
  margin: 0 auto;
  width: 100%;
  overflow: hidden;
}
.net-panel, .candidate-panel { width: 100%; background: var(--panel); border-radius: 16px; box-shadow: var(--shadow); box-sizing: border-box; transition: background 0.2s; }
.net-panel {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  overflow: hidden;
}
.candidate-panel { flex: 0 0 auto; padding: 10px 14px; display: flex; flex-direction: column; align-items: center; }

.net-grid {
  position: relative;
  margin: 0 auto;
  touch-action: none;
}
.grid-cell {
  position: absolute;
  border: 1px dashed var(--cell-border);
  border-radius: 6px;
  box-sizing: border-box;
}
/* When a tile is selected, its legal single-step pivot/slide destinations
   are highlighted so the player can find and trigger those move types
   directly, instead of only ever landing on the (structurally much more
   available) whole-net shift by clicking a plain adjacent cell. */
.grid-cell.dest-pivot, .grid-cell.dest-slide {
  border-style: solid;
  cursor: pointer;
}
.grid-cell.dest-pivot { background: rgba(233, 196, 106, 0.35); border-color: #e9c46a; }
.grid-cell.dest-slide { background: rgba(69, 123, 157, 0.22); border-color: #457b9d; }
.grid-cell.dest-pivot::after, .grid-cell.dest-slide::after {
  content: '';
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 34%; height: 34%; border-radius: 50%;
}
.grid-cell.dest-pivot::after { border: 2px solid #b7791f; border-top-color: transparent; }
.grid-cell.dest-slide::after { background: #457b9d; opacity: 0.6; border-radius: 4px; width: 40%; height: 22%; }
.tile {
  position: absolute;
  border-radius: 8px;
  background: var(--tile-bg);
  border: 2px solid var(--tile-border);
  box-shadow: 0 2px 4px rgba(0,0,0,0.08);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  will-change: transform;
}
.tile.selected { border-color: var(--good); box-shadow: 0 0 0 3px rgba(42,157,143,0.3); }
.tile svg { width: 82%; height: 82%; pointer-events: none; }
/* Patterns that paint their own full-tile background (colored/black/white)
   render edge-to-edge instead of inset with the card-like white margin,
   otherwise a "colored background" style would show as a smaller square
   floating inside a border, defeating the point of that style. */
.tile.pattern-bg svg { width: 100%; height: 100%; border-radius: 6px; }
.tile.rejected { animation: shakeTile 0.35s; }
@keyframes shakeTile { 0%,100%{ transform: translateX(0);} 25%{ transform: translateX(-6px);} 75%{ transform: translateX(6px);} }

.candidate-hex { display: flex; justify-content: center; flex: 0 0 auto; padding: 6px 0; }
.candidate-corner-stage { perspective: 500px; width: clamp(90px, 20vh, 150px); height: clamp(90px, 20vh, 150px); }
.candidate-corner {
  position: relative; margin: 0 auto; transform-style: preserve-3d;
  transform: rotateX(-28deg) rotateY(-38deg);
}
.candidate-face { position: absolute; top: 0; left: 0; }

/* answer-phase and result-phase occupy the SAME slot in the layout (only
   one shown at a time), so answering never grows the page height — no
   scrolling needed to reach Fold it / Next after a coin/result panel would
   otherwise have been appended below. */
.answer-phase, .result-phase { width: 100%; text-align: center; }

.question-text { font-size: clamp(0.85rem, 2.6vh, 1.05rem); font-weight: 600; margin: 4px 0 8px; }

.timer-bar { height: 6px; background: var(--cell-border); border-radius: 4px; overflow: hidden; margin-bottom: 8px; }
.timer-fill { height: 100%; background: var(--good); width: 100%; transition: width 0.1s linear, background 0.3s; }

.answer-row { display: flex; gap: 10px; justify-content: center; }
.answer-btn { flex: 1; max-width: 200px; padding: clamp(8px, 1.6vh, 14px) 0; font-size: clamp(0.9rem, 2.4vh, 1.1rem); font-weight: 700; border: none; border-radius: 12px; cursor: pointer; color: white; transition: transform 0.1s; }
.answer-btn:active { transform: scale(0.96); }
.true-btn { background: var(--good); }
.false-btn { background: var(--bad); }
.answer-btn:disabled { opacity: 0.5; cursor: default; }

/* ---- result phase ---- */
.result-banner { font-size: clamp(1rem, 2.8vh, 1.3rem); font-weight: 800; padding: 8px; border-radius: 10px; margin-bottom: 6px; }
.result-banner.correct { background: var(--good-bg); color: var(--good-ink); }
.result-banner.incorrect { background: var(--bad-bg); color: var(--bad-ink); }
.result-banner.shake { animation: shakeTile 0.4s; }
.coin-anim { font-weight: 700; color: #d4a017; }
.result-explain { color: var(--ink-soft); margin: 4px 0 8px; font-size: clamp(0.8rem, 2.2vh, 0.95rem); }
.result-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.fold-btn, .next-btn { padding: clamp(8px, 1.6vh, 12px) 20px; border-radius: 10px; border: none; font-weight: 700; cursor: pointer; }
.fold-btn { background: #ffe8cc; color: #7a4a00; }
.next-btn { background: var(--accent); color: var(--accent-ink); }

/* ---- floating coin particles ---- */
.coin-particle {
  position: fixed; font-weight: 800; color: #d4a017; pointer-events: none; z-index: 60;
  font-size: 2rem; text-shadow: 0 2px 6px rgba(0,0,0,0.25);
  animation: coinFly 0.9s ease-out forwards;
}
@keyframes coinFly {
  0% { opacity: 1; transform: translate(0,0) scale(1); }
  100% { opacity: 0; transform: translate(var(--dx,0), var(--dy,-80px)) scale(0.6); }
}

/* ---- fold-in-place 3D cube (lives INSIDE net-panel, not a full-screen
   overlay, so the candidate hex/question stay visible for comparison) ---- */
.cube-stage {
  position: absolute; inset: 0; z-index: 10;
  background: var(--overlay);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  border-radius: inherit;
  perspective: 900px;
  padding: 8px;
}
.cube-stage[hidden] { display: none; }
.close-fold-btn {
  position: absolute; top: 8px; right: 10px; background: rgba(255,255,255,0.12); border: none;
  color: white; font-size: 0.85rem; cursor: pointer; line-height: 1; padding: 6px 10px; border-radius: 8px;
}
.cube3d { position: relative; transform-style: preserve-3d; cursor: grab; }
.cube3d:active { cursor: grabbing; }
.cube-face {
  position: absolute; width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  background: var(--hex-face-bg); border: 3px solid #1d2733; overflow: hidden;
  backface-visibility: hidden;
  box-shadow: inset 0 0 0 1px white;
}
.cube-face svg { width: 80%; height: 80%; }
.fold-hint { color: #cfd8dc; font-size: 0.78rem; flex: 0 0 auto; }

.hint-toast {
  position: fixed; bottom: 14px; left: 50%; transform: translateX(-50%);
  background: #1d2733; color: white; padding: 8px 16px; border-radius: 20px;
  font-size: 0.9rem; z-index: 45; opacity: 0.95;
}

@media (max-width: 480px) {
  .title { font-size: 0.95rem; }
}
