:root {
  --bg: #161A20;
  --panel: #1D222A;
  --line: #2C333D;
  --text: #E8EDF2;
  --muted: #8E99A8;
  --unknown: #242A33;
  --wall: #4A5361;
  --open: #DFE9E0;
  --visited: #B9D6C0;
  --player: #21834E;
  --exit: #F2C14E;
  --accent: #2FA866;
  --danger: #E0675A;
  --gap: 3px;
  --board: min(calc(100vw - 24px), 420px, calc(100dvh - 330px));
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.35 ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
#app {
  max-width: 460px; margin: 0 auto; min-height: 100dvh;
  display: flex; flex-direction: column;
  padding: max(8px, env(safe-area-inset-top)) 12px max(10px, env(safe-area-inset-bottom));
}

/* header */
.top { display: flex; align-items: center; justify-content: space-between; height: 48px; }
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 28px; height: 28px; border-radius: 8px; background: url(/favicon.svg) center/cover; }
h1 { margin: 0; font-size: 20px; letter-spacing: .5px; font-weight: 800; }
.sub { margin: 0; font-size: 12px; color: var(--muted); }
.top-right { display: flex; align-items: center; gap: 8px; }
.moves {
  display: flex; align-items: baseline; gap: 4px; padding: 5px 11px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--line);
}
.moves b { font-size: 18px; font-variant-numeric: tabular-nums; }
.moves span { font-size: 11px; color: var(--muted); }
.moves.low { border-color: var(--danger); }
.moves.low b { color: var(--danger); }
.icon-btn { width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel); display: grid; place-items: center; }

main { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 8px; padding-top: 4px; }
.mode-tag { color: var(--exit); white-space: nowrap; }
.link { background: none; border: 0; color: var(--text); text-decoration: underline; padding: 0; font-size: 12px; }
@media (max-height: 680px) { .dpad { grid-template-rows: repeat(3, 42px); } .echo { min-height: 50px; padding: 6px 10px; } }

/* board */
.board {
  width: var(--board); height: var(--board);
  display: grid; grid-template-columns: repeat(9, 1fr); gap: var(--gap);
  padding: var(--gap); border-radius: 14px; background: #12151A; border: 1px solid var(--line);
  touch-action: none; user-select: none;
}
.cell { position: relative; border-radius: 4px; background: var(--unknown); transition: background-color .18s; }
.cell.w { background: var(--wall); }
.cell.w::after { /* white marker: shape cue, not color-only */
  content: ""; position: absolute; inset: 30%; border-radius: 2px;
  background: linear-gradient(45deg, transparent 42%, rgba(255,255,255,.55) 42% 58%, transparent 58%),
              linear-gradient(-45deg, transparent 42%, rgba(255,255,255,.55) 42% 58%, transparent 58%);
}
.cell.o { background: var(--open); }
.cell.v { background: var(--visited); }
.cell.v::after { content: ""; position: absolute; inset: 40%; border-radius: 50%; background: #6E9B7B; }
.cell.x { background: var(--exit); }
.cell.x::after { content: "★"; position: absolute; inset: 0; display: grid; place-items: center; color: #5b4300; font-size: calc(var(--board) / 16); }
.cell.p { background: var(--player); box-shadow: 0 0 0 2px rgba(47,168,102,.45), 0 0 14px rgba(47,168,102,.55); z-index: 1; }
.cell.p::after { content: ""; position: absolute; inset: 24%; border-radius: 50%; background: #fff; }
.cell.bump { animation: bump .28s; }
.cell.ping::before { content: ""; position: absolute; inset: -2px; border-radius: 6px; border: 2px solid var(--accent); animation: ping .9s ease-out; }
@keyframes bump { 0%,100% { transform: none } 30% { transform: scale(.82) } }
@keyframes ping { from { transform: scale(1); opacity: 1 } to { transform: scale(2.6); opacity: 0 } }
.board.shake { animation: shake .25s; }
@keyframes shake { 25% { transform: translateX(-4px) } 75% { transform: translateX(4px) } }

/* stats */
.stats { width: var(--board); display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); padding: 0 4px; }
.stats b { color: var(--text); font-variant-numeric: tabular-nums; font-weight: 700; }

#play, .result { width: var(--board); min-width: 296px; }
#play { display: flex; flex-direction: column; gap: 8px; }

/* echo */
.echo { display: flex; gap: 10px; align-items: center; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; min-height: 56px; }
.echo-icon { flex: none; width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; background: #26303A; color: var(--muted); transition: transform .4s; }
.echo.has .echo-icon { background: #1f3a2b; color: #7CE0A6; }
.echo-icon.idle svg { transform: none; opacity: .5; }
.echo-body { min-width: 0; flex: 1; }
.echo-head { display: flex; justify-content: space-between; align-items: center; font-weight: 700; font-size: 13px; }
.echo p { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); }
.echo.has p { color: var(--text); }
.bars { display: inline-flex; gap: 2px; align-items: flex-end; height: 12px; }
.bars i { width: 4px; border-radius: 1px; background: #38414d; }
.bars i:nth-child(1) { height: 5px } .bars i:nth-child(2) { height: 8px } .bars i:nth-child(3) { height: 12px }
.bars[data-s="1"] i:nth-child(-n+1), .bars[data-s="2"] i:nth-child(-n+2), .bars[data-s="3"] i { background: #7CE0A6; }

/* controls */
.controls { display: grid; grid-template-columns: auto 1fr; gap: 10px; align-items: stretch; }
.dpad { display: grid; grid-template-columns: repeat(3, 50px); grid-template-rows: repeat(3, 46px); gap: 4px; }
.dir {
  border: 1px solid var(--line); background: #262D37; border-radius: 12px; font-size: 16px; color: #cfd7e0;
  transition: transform .06s, background .15s, opacity .15s;
}
.dir:active, .dir.press { transform: scale(.93); background: #31404c; }
.dir.blocked { opacity: .42; }
.dir.edge { opacity: .18; }
.up { grid-column: 2; grid-row: 1 } .left { grid-column: 1; grid-row: 2 } .right { grid-column: 3; grid-row: 2 } .down { grid-column: 2; grid-row: 3 }
.dpad-center { grid-column: 2; grid-row: 2; display: grid; place-items: center; font-weight: 800; color: var(--muted); font-variant-numeric: tabular-nums; }
.investigate {
  border: 0; border-radius: 14px; background: linear-gradient(180deg, #279458, var(--player)); color: #fff;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  box-shadow: 0 4px 0 #165c37; transition: transform .06s, box-shadow .06s;
}
.investigate:active, .investigate.press { transform: translateY(3px); box-shadow: 0 1px 0 #165c37; }
.inv-icon { font-size: 22px; }
.inv-label { font-weight: 800; font-size: 17px; }
.inv-cost { font-size: 11.5px; opacity: .85; }
button:disabled { opacity: .45; cursor: default; }
button:focus-visible { outline: 2px solid #7CE0A6; outline-offset: 2px; }

/* result */
.result { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; animation: rise .35s ease-out; }
@keyframes rise { from { transform: translateY(10px); opacity: 0 } }
.result-head { display: flex; justify-content: space-between; align-items: center; }
.result h2 { margin: 0; font-size: 20px; }
.score { text-align: right; }
.score b { font-size: 30px; line-height: 1; color: #7CE0A6; font-variant-numeric: tabular-nums; }
.score span { font-size: 12px; color: var(--muted); margin-left: 3px; }
.result.fail .score b { color: var(--text); }
.badges { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.badges li { font-size: 12px; padding: 3px 9px; border-radius: 999px; background: #1f3a2b; color: #bff0d3; border: 1px solid #2c5a40; }
.breakdown { list-style: none; margin: 0; padding: 0; font-size: 12.5px; color: var(--muted); display: grid; grid-template-columns: 1fr 1fr; gap: 1px 14px; }
.breakdown li { display: flex; justify-content: space-between; }
.breakdown b { color: var(--text); }
.result-actions { display: grid; grid-template-columns: 2fr 1fr; gap: 8px; }
.primary, .ghost { height: 44px; border-radius: 12px; font-weight: 700; }
.primary { border: 0; background: var(--player); color: #fff; }
.ghost { border: 1px solid var(--line); background: transparent; }

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

/* sheet */
.sheet { border: 0; padding: 0; background: transparent; color: var(--text); width: min(460px, 100vw); max-width: 100vw; margin: auto auto 0; max-height: 88dvh; }
.sheet::backdrop { background: rgba(5,7,10,.6); }
.sheet-inner { background: var(--panel); border: 1px solid var(--line); border-bottom: 0; border-radius: 18px 18px 0 0; padding: 14px 16px max(16px, env(safe-area-inset-bottom)); }
.sheet[open] .sheet-inner { animation: rise .25s ease-out; }
.tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; background: #151920; padding: 4px; border-radius: 12px; }
.tabs button { border: 0; background: transparent; border-radius: 9px; height: 34px; font-size: 13px; color: var(--muted); }
.tabs button[aria-selected="true"] { background: #2a323d; color: var(--text); font-weight: 700; }
.tab { padding: 12px 2px; min-height: 230px; max-height: 56dvh; overflow: auto; }
.howto { margin: 0; padding-left: 20px; display: grid; gap: 8px; font-size: 14px; }
.chip { display: inline-block; width: 14px; height: 14px; border-radius: 3px; vertical-align: -2px; }
.chip.wall { background: var(--wall); }
.sheet-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.grid-stats div { background: #151920; border-radius: 12px; padding: 10px 6px; text-align: center; }
.grid-stats b { display: block; font-size: 22px; }
.grid-stats span { font-size: 11px; color: var(--muted); }
.ach { list-style: none; padding: 0; margin: 12px 0 0; display: grid; gap: 6px; }
.ach li { display: flex; gap: 8px; align-items: center; font-size: 13px; opacity: .45; }
.ach li.got { opacity: 1; }
.ach small { color: var(--muted); margin-left: auto; }
.lb { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.lb td { padding: 6px 4px; border-bottom: 1px solid var(--line); }
.lb td:last-child, .lb td:nth-child(3) { text-align: right; font-variant-numeric: tabular-nums; }
.lb tr.me td { color: #7CE0A6; font-weight: 700; }

.toast { position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); background: #2a323d; padding: 8px 14px; border-radius: 999px; font-size: 13px; z-index: 10; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

@media (min-height: 760px) { :root { --board: min(calc(100vw - 24px), 420px, calc(100dvh - 340px)); } .dpad { grid-template-rows: repeat(3, 52px); grid-template-columns: repeat(3, 56px); } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
