:root {
  --bg: #0b1020;
  --bg2: #111832;
  --panel: #151c33;
  --cell: #1b2444;
  --cell-hi: #26315c;
  --line: #2b3660;
  --text: #eef1fb;
  --muted: #8e98c0;
  --accent: #8b6cff;
  --accent2: #22d3ee;
  --good: #34d399;
  --bad: #fb7185;
  --warn: #fbbf24;
  --radius: 14px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  background: radial-gradient(1200px 700px at 20% -10%, #1b2450 0%, var(--bg) 55%) fixed, var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; -webkit-tap-highlight-color: transparent; }
button:focus-visible { outline: 2px solid var(--accent2); outline-offset: 2px; }
b { font-weight: 800; }
.hidden { display: none !important; }

#app { max-width: 1100px; margin: 0 auto; padding: 16px clamp(10px, 2vw, 24px) 24px; min-height: 100%; }
.screen { animation: fade .25s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ---------- MENU ---------- */
.title { text-align: center; padding: 28px 0 10px; }
.logo { font-size: 64px; line-height: 1; filter: drop-shadow(0 8px 24px rgba(139,108,255,.45)); animation: bob 2.4s ease-in-out infinite; }
@keyframes bob { 0%,100% { transform: translateY(0) } 50% { transform: translateY(-6px) } }
h1 { font-size: clamp(36px, 6vw, 56px); margin: 8px 0 4px; letter-spacing: -0.02em; font-weight: 900; }
h1 span { background: linear-gradient(90deg, var(--accent), var(--accent2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.tag { color: var(--muted); margin: 0 0 18px; font-size: 17px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 22px clamp(16px, 3vw, 30px); max-width: 720px; margin: 0 auto; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .14em; color: var(--muted); margin: 18px 0 10px; font-weight: 700; }
.card h2:first-child { margin-top: 0; }
.link { color: var(--accent2); text-transform: none; letter-spacing: 0; font-size: 13px; padding: 0 2px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.seg button { background: var(--cell); border: 2px solid transparent; border-radius: 12px; padding: 12px 8px; text-align: center; transition: .15s; }
.seg button small { display: block; color: var(--muted); font-size: 12px; margin-top: 3px; }
.seg button.on { border-color: var(--accent); background: #241f4d; }
.seg button:hover { background: var(--cell-hi); }
.hint { color: var(--muted); font-size: 13px; margin: 10px 0 0; line-height: 1.45; }
.topics { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.topics label { display: flex; align-items: center; gap: 8px; background: var(--cell); border: 2px solid transparent; border-radius: 12px; padding: 10px 12px; cursor: pointer; user-select: none; transition: .15s; }
.topics label:hover { background: var(--cell-hi); }
.topics label.on { border-color: var(--accent2); background: #16304a; }
.topics input { display: none; }
.topics .ic { font-size: 20px; }
.topics small { display: block; color: var(--muted); font-size: 11px; margin-top: 1px; }
.trogs label.on { border-color: var(--bad); background: #3a1f33; }
.primary { display: block; width: 100%; margin-top: 22px; padding: 16px; font-size: 20px; font-weight: 800; border-radius: 14px; color: #fff;
  background: linear-gradient(135deg, var(--accent), #5b8cff); box-shadow: 0 10px 30px rgba(139,108,255,.35); transition: transform .1s, filter .15s; }
.primary:hover { filter: brightness(1.08); }
.primary:active { transform: translateY(1px) scale(.99); }
.primary:disabled { filter: grayscale(.7); opacity: .6; cursor: not-allowed; }
.keys { text-align: center; }
.best { text-align: center; color: var(--warn); margin: 8px 0 0; font-weight: 700; min-height: 1.2em; }
.guide { max-width: 720px; margin: 18px auto 0; color: var(--muted); font-size: 14px; }
.guide summary { cursor: pointer; color: var(--text); font-weight: 600; padding: 8px 4px; }
.guide ul { list-style: none; padding: 0; margin: 8px 0 0; display: grid; gap: 8px; }
.guide li { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; line-height: 1.45; }
.guide li b { color: var(--text); }

/* ---------- HUD ---------- */
.hud { display: grid; grid-template-columns: auto 1fr auto; gap: 14px; align-items: center; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 16px; margin-bottom: 10px; }
.hud-left, .hud-right { display: flex; flex-direction: column; gap: 2px; white-space: nowrap; }
.hud-right { text-align: right; align-items: flex-end; }
.lvl, .score { font-size: 15px; color: var(--muted); }
.lvl b, .score b { color: var(--text); font-size: 20px; }
.stage { font-size: 12px; text-transform: uppercase; letter-spacing: .12em; color: var(--accent2); font-weight: 700; }
.hud-mid { text-align: center; min-width: 0; }
.topicchip { display: inline-block; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .1em; font-weight: 700; margin-bottom: 3px; }
.rule { font-size: clamp(15px, 2.2vw, 20px); line-height: 1.3; }
.rule b { color: var(--warn); }
.lives { font-size: 17px; letter-spacing: 2px; }
.lives .lost { opacity: .22; filter: grayscale(1); }
.left { font-size: 12px; color: var(--muted); }
.help { background: #1a2a3d; border: 1px solid #24507a; color: #cfe7ff; border-radius: var(--radius); padding: 10px 16px; margin-bottom: 10px; font-size: 14px; line-height: 1.5; animation: fade .2s; }

/* ---------- BOARD ---------- */
.board-wrap { position: relative; }
.board { display: grid; grid-template-columns: repeat(var(--cols, 6), minmax(0, 1fr)); grid-template-rows: repeat(var(--rows, 5), minmax(0, 1fr)); gap: 8px; --cell-font: clamp(15px, 2.6vw, 30px); height: clamp(300px, min(calc(min(100vw, 1100px) * 0.6), calc(100vh - 270px)), 720px); }
.cell { position: relative; min-height: 0; background: var(--cell); border: 1px solid var(--line); border-radius: 12px; display: flex; align-items: center; justify-content: center; padding: 4px 6px; text-align: center; font-weight: 700; font-size: var(--cell-font); line-height: 1.15; word-break: break-word; cursor: pointer; transition: background .15s, transform .15s; user-select: none; }
.cell:hover { background: var(--cell-hi); }
.cell.sm { font-size: calc(var(--cell-font) * .72); }
.cell.xs { font-size: calc(var(--cell-font) * .56); }
.board.sym .cell { font-size: calc(var(--cell-font) * 1.5); font-family: Helvetica, Arial, sans-serif; }
.board.wrap { outline: 2px dashed rgba(34,211,238,.4); outline-offset: 6px; border-radius: 14px; }
.board.blocks .cell { font-family: ui-monospace, Menlo, monospace; letter-spacing: 1px; font-size: calc(var(--cell-font) * .8); }
.cell.eaten { background: transparent; border-style: dashed; border-color: #1f2845; color: transparent; cursor: default; }
.cell.eaten:hover { background: transparent; }
.cell.burnt::after { content: '✗'; position: absolute; inset: 0; display: grid; place-items: center; color: var(--bad); opacity: .5; font-size: var(--cell-font); }
.cell.swap { animation: swap .5s ease; }
@keyframes swap { 0% { transform: rotateX(90deg); background: #3b2f5e; } 100% { transform: none; } }
.cell.pop { animation: pop .35s ease; }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(.85); } 100% { transform: scale(1); } }
.cell.shake { animation: shake .4s ease; }
@keyframes shake { 0%,100% { transform: translateX(0) } 25% { transform: translateX(-6px) } 75% { transform: translateX(6px) } }

/* ---------- SPRITES ---------- */
.sprites { position: absolute; inset: 0; pointer-events: none; }
.sprite { position: absolute; left: 0; top: 0; width: calc((100% - (var(--cols) - 1) * 8px) / var(--cols)); height: calc((100% - (var(--rows) - 1) * 8px) / var(--rows));
  transform: translate(calc(var(--c) * (100% + 8px)), calc(var(--r) * (100% + 8px))); transition: transform .13s ease-out; display: grid; place-items: center; }
.sprite .e { font-size: clamp(28px, 4.2vw, 52px); line-height: 1; filter: drop-shadow(0 4px 10px rgba(0,0,0,.5)); transition: transform .1s; }
.muncher { place-items: end start; }
.muncher .e { font-size: clamp(20px, 2.6vw, 32px); margin: 0 0 3px 4px; }
.muncher.chomp .e { animation: chomp .25s ease; }
@keyframes chomp { 0% { transform: scale(1) } 50% { transform: scale(1.35) rotate(-10deg) } 100% { transform: scale(1) } }
.muncher::before { content: ''; position: absolute; inset: 0; border-radius: 12px; border: 3px solid var(--accent2); box-shadow: 0 0 0 3px rgba(34,211,238,.18), inset 0 0 20px rgba(34,211,238,.12); }
.muncher.hurt .e { animation: hurt .6s ease; }
@keyframes hurt { 0%,100% { filter: none } 25%,75% { filter: brightness(3) saturate(0) } }
.troggle .e { animation: wobble 1.1s ease-in-out infinite; font-size: clamp(26px, 3.6vw, 44px); }
.troggle { place-items: start end; }
.troggle .e { margin: 3px 4px 0 0; }
@keyframes wobble { 0%,100% { transform: rotate(-6deg) } 50% { transform: rotate(6deg) } }
.troggle.bashful .e { filter: hue-rotate(120deg) drop-shadow(0 4px 10px rgba(0,0,0,.5)); }
.troggle.smartie .e { filter: hue-rotate(300deg) brightness(1.1) drop-shadow(0 4px 10px rgba(0,0,0,.5)); }
.troggle.warn::before { content: ''; position: absolute; inset: 2px; border-radius: 12px; border: 3px dashed var(--warn); animation: blink .3s steps(2) infinite; }
.troggle.warn .e { opacity: .25; transform: scale(.7); animation: none; }
@keyframes blink { to { opacity: .2 } }
.float { position: absolute; font-weight: 900; font-size: clamp(16px, 2.4vw, 26px); color: var(--good); text-shadow: 0 2px 8px rgba(0,0,0,.6); animation: floatUp .9s ease-out forwards; pointer-events: none; }
@keyframes floatUp { from { transform: translateY(0); opacity: 1 } to { transform: translateY(-50px); opacity: 0 } }

/* ---------- TOAST / OVERLAY ---------- */
.status { min-height: 54px; display: flex; align-items: center; justify-content: center; margin-top: 10px; }
.toast { max-width: min(100%, 760px); background: rgba(15,20,40,.96); border: 1px solid var(--line); border-left: 5px solid var(--accent2); border-radius: 12px; padding: 10px 16px; font-size: 15px; line-height: 1.4; animation: toastIn .2s ease; }
.toast.bad { border-left-color: var(--bad); }
.toast.good { border-left-color: var(--good); }
.toast.warn { border-left-color: var(--warn); }
.toast b { color: var(--warn); }
@keyframes toastIn { from { opacity: 0; transform: translateY(6px) } to { opacity: 1; transform: none } }
.overlay { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(8,12,26,.72); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); border-radius: var(--radius); z-index: 10; animation: fade .2s; }
.ov { background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 26px 30px; text-align: center; max-width: min(92%, 520px); box-shadow: 0 30px 80px rgba(0,0,0,.6); }
.ov h2 { margin: 0 0 6px; font-size: clamp(24px, 4vw, 34px); letter-spacing: -0.01em; }
.ov p { margin: 6px 0; color: var(--muted); line-height: 1.45; }
.ov p.big { color: var(--text); font-size: 18px; }
.ov .rule { margin: 10px 0 0; font-size: 18px; color: var(--text); }
.ov .btns { display: flex; gap: 10px; justify-content: center; margin-top: 18px; flex-wrap: wrap; }
.ov .btns button { padding: 12px 20px; border-radius: 12px; font-weight: 700; background: var(--cell); border: 1px solid var(--line); }
.ov .btns button.go { background: linear-gradient(135deg, var(--accent), #5b8cff); border-color: transparent; color: #fff; }
.ov .bonus { color: var(--good); font-weight: 800; font-size: 22px; }
.ov .icon { font-size: 56px; line-height: 1; margin-bottom: 6px; }

/* ---------- CONTROLS ---------- */
.controls { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-top: 6px; }
.dpad { display: grid; grid-template-columns: repeat(3, 52px); grid-template-rows: repeat(2, 52px); gap: 6px; }
.dpad button { background: var(--cell); border: 1px solid var(--line); border-radius: 12px; font-size: 18px; touch-action: manipulation; }
.dpad button:active { background: var(--cell-hi); }
.dpad [data-dir=up] { grid-column: 2; grid-row: 1; }
.dpad [data-dir=left] { grid-column: 1; grid-row: 2; }
.dpad [data-dir=down] { grid-column: 2; grid-row: 2; }
.dpad [data-dir=right] { grid-column: 3; grid-row: 2; }
.munch { flex: 1; max-width: 260px; height: 64px; border-radius: 16px; font-weight: 900; font-size: 20px; letter-spacing: .08em; color: #0b1020; background: linear-gradient(135deg, var(--good), var(--accent2)); box-shadow: 0 10px 30px rgba(52,211,153,.3); touch-action: manipulation; }
.munch:active { transform: scale(.97); }
.misc { display: flex; gap: 6px; }
.misc button { width: 44px; height: 44px; border-radius: 12px; background: var(--cell); border: 1px solid var(--line); font-size: 16px; font-weight: 800; }
.misc button:hover { background: var(--cell-hi); }
.misc button.on { border-color: var(--accent2); color: var(--accent2); }

@media (hover: hover) and (pointer: fine) and (min-width: 720px) {
  .dpad, .munch { display: none; }
  .controls { justify-content: flex-end; }
}
@media (max-width: 720px) {
  #app { padding: 8px 8px 16px; }
  .hud { grid-template-columns: 1fr; text-align: center; gap: 6px; padding: 10px; }
  .hud-left, .hud-right { flex-direction: row; justify-content: center; gap: 14px; align-items: center; }
  .board { gap: 5px; --cell-font: clamp(13px, 4vw, 22px); }
  .sprite { width: calc((100% - (var(--cols) - 1) * 5px) / var(--cols)); height: calc((100% - (var(--rows) - 1) * 5px) / var(--rows)); transform: translate(calc(var(--c) * (100% + 5px)), calc(var(--r) * (100% + 5px))); }
  .sprite .e { font-size: clamp(24px, 7vw, 40px); }
  .cell { border-radius: 9px; }
  .board { height: clamp(320px, calc(100vh - 300px), 560px); }
  .toast { font-size: 13px; }
  .status { min-height: 46px; }
  .controls { flex-wrap: wrap; justify-content: center; }
  .munch { flex: 1 1 160px; max-width: none; }
  .misc { width: 100%; justify-content: center; }
}
