:root {
  --bg0: #090e1a;
  --bg1: #121a2e;
  --surface: #17213a;
  --surface-2: #1e2a48;
  --ink: #f5efe2;
  --muted: #cfd6e3;
  --line: rgba(245, 239, 226, 0.2);
  --amber: #ffb04a;
  --amber-text: #ffcf87;
  --amber-ink: #241606;
  --ok: #7ee0a1;
  --hi: #ffb3a1;
  --lo: #9cc8ff;
  --cobble: #283142;
  --brick: #3a2326;
  --door: #4a3322;
  --serif: Palatino, "Palatino Linotype", "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --st: env(safe-area-inset-top, 0px);
  --sb: env(safe-area-inset-bottom, 0px);
  --sl: env(safe-area-inset-left, 0px);
  --sr: env(safe-area-inset-right, 0px);
  --art-bg: none;
  --art-cobble: none;
  --art-brick: none;
  --art-door: none;
  --art-lamp: none;
  --cell: 44px;
  --gap: 2px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg0); color: var(--ink); font-family: var(--sans); overflow: hidden; touch-action: manipulation; }
body {
  background: var(--art-bg), radial-gradient(ellipse 90% 50% at 50% -10%, #24335b 0%, transparent 60%), linear-gradient(180deg, #10172b 0%, #090e1a 100%);
  background-size: cover, auto, auto; background-position: center;
}
h1, h2, h3 { font-family: var(--serif); font-weight: 600; margin: 0; }
p { margin: 0; line-height: 1.45; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.hidden { display: none !important; }
:focus-visible { outline: 3px solid #fff; outline-offset: 2px; box-shadow: 0 0 0 5px var(--amber); }
.skip { position: absolute; left: 8px; top: -60px; z-index: 50; background: var(--amber); color: var(--amber-ink); padding: 12px 16px; border-radius: 10px; font-weight: 700; }
.skip:focus { top: 8px; }

#app { height: 100%; height: 100dvh; max-width: 560px; margin: 0 auto; position: relative; }
#main { position: absolute; inset: 0; outline: none; }
.screen { position: absolute; inset: 0; display: flex; flex-direction: column; padding: calc(10px + var(--st)) calc(10px + var(--sr)) calc(10px + var(--sb)) calc(10px + var(--sl)); visibility: hidden; opacity: 0; transition: opacity .18s ease, visibility .18s; }
.screen.active { visibility: visible; opacity: 1; }
.scroll-body { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }

/* Buttons */
.btn { min-height: 48px; min-width: 48px; padding: 10px 16px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--surface); color: var(--ink); font: 600 1rem var(--sans); cursor: pointer; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--amber); border-color: var(--amber); color: var(--amber-ink); }
.btn.primary:hover { background: #ffc06b; }
.btn:disabled { opacity: 1; background: #2a3247; border-color: #3a435a; color: #c3cad8; cursor: not-allowed; }
.btn.big { width: 100%; min-height: 52px; font-size: 1.05rem; }
.icon-btn { width: 48px; height: 48px; flex: 0 0 48px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--surface); color: var(--ink); font: 700 1.25rem var(--sans); cursor: pointer; }
.icon-spacer { width: 48px; flex: 0 0 48px; }

/* Home */
.home-body { display: flex; flex-direction: column; gap: 16px; padding: 8px 4px 4px; }
.brand { text-align: center; padding-top: 6px; }
.brand-mark { width: 68px; height: 68px; margin: 0 auto 10px; border-radius: 18px; background: var(--art-lamp) center/80% no-repeat, radial-gradient(circle at 50% 55%, #ffcf87 0 14%, #ff9b2e 15% 24%, transparent 25%), #142042; border: 1.5px solid var(--line); }
.brand-title { font-family: var(--serif); font-weight: 600; font-size: 2rem; line-height: 1.1; }
.daily-word { color: var(--amber-text); }
.tagline { color: var(--muted); margin-top: 6px; }
.today-card { background: rgba(11, 16, 29, .88); border: 1.5px solid var(--line); border-radius: 16px; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.today-meta { font-weight: 700; font-size: 1.05rem; }
.today-level, .today-status { color: var(--muted); }
.today-meta, .today-level, .today-status, .streak-line { min-height: 1.45em; }
.today-card .btn { margin-top: 8px; }
.streak-line { text-align: center; color: var(--amber-text); font-weight: 600; }
.home-nav { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.home-nav .btn:last-child { grid-column: span 2; }
.foot-note { text-align: center; color: var(--muted); font-size: .9rem; }

/* Bars */
.bar { display: flex; align-items: center; gap: 8px; }
.bar-title { flex: 1; min-width: 0; text-align: center; }
.bar-title h2 { font-size: 1.2rem; }
.bar-sub { color: var(--muted); font-size: .85rem; }

/* HUD */
.hud { display: flex; justify-content: space-between; align-items: center; padding: 8px 4px 6px; gap: 8px; }
.hud-item { font-weight: 600; }
.checks { display: flex; gap: 5px; align-items: center; }
.pip { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--amber); }
.pip.used { background: transparent; border-color: #8a93a8; border-style: dashed; }
.pip.left { background: var(--amber); }

/* Board */
.board-wrap { flex: 1 1 auto; min-height: 0; display: flex; align-items: center; justify-content: center; }
.board { display: grid; gap: var(--gap); grid-template-columns: repeat(var(--n, 7), var(--cell)); grid-auto-rows: var(--cell); padding: 6px; border-radius: 14px; background: rgba(5, 8, 16, .7); border: 1.5px solid var(--line); }
.cell { position: relative; width: var(--cell); height: var(--cell); border-radius: 6px; border: 0; padding: 0; margin: 0; font: 700 calc(var(--cell) * .4) var(--sans); color: var(--ink); display: flex; align-items: center; justify-content: center; }
.cell.wall { background: var(--art-brick), repeating-linear-gradient(0deg, #2c1a1d 0 7px, #3f262a 7px 9px), var(--brick); background-size: cover, auto, auto; }
.cell.street { background: var(--art-cobble), var(--cobble); background-size: cover; cursor: pointer; border: 1.5px solid rgba(255, 255, 255, .08); }
.cell.street:hover { border-color: rgba(255, 207, 135, .55); }
.cell.street .glow { position: absolute; inset: 0; border-radius: 5px; background: rgba(255, 176, 74, var(--a, 0)); pointer-events: none; }
.cell.street .lv { position: relative; font-size: calc(var(--cell) * .32); color: var(--amber-text); font-weight: 600; }
.cell.lamp { background: var(--art-lamp) center/76% no-repeat, radial-gradient(circle at 50% 50%, #fff2d0 0 16%, #ffb04a 17% 34%, rgba(255, 176, 74, .25) 35% 60%, transparent 61%), #3a2c16; border: 2px solid var(--amber); }
.cell.lamp::after { content: ""; position: absolute; width: 22%; height: 22%; right: 6%; top: 6%; transform: rotate(45deg); background: var(--amber-ink); border: 2px solid #fff2d0; }
.cell.door { background: var(--art-door), linear-gradient(180deg, #5a3d27, #3b2717); background-size: cover; border: 2px solid #c9a57a; border-radius: 10px 10px 4px 4px; flex-direction: column; line-height: 1; }
.cell.door .tg { font-family: var(--serif); font-size: calc(var(--cell) * .46); color: #fff4df; text-shadow: 0 1px 2px #000; }
.cell.door .fb { position: absolute; left: 50%; bottom: 2px; transform: translateX(-50%); font-size: calc(var(--cell) * .26); min-width: 60%; padding: 1px 3px; border-radius: 6px; background: #0b101d; border: 1.5px solid currentColor; white-space: nowrap; }
.cell.door.ok .tg, .cell.door.hi .tg, .cell.door.lo .tg { transform: translateY(-22%); }
.cell.door.ok { border-color: var(--ok); box-shadow: 0 0 0 2px var(--ok) inset; }
.cell.door.ok .fb { color: var(--ok); }
.cell.door.hi .fb { color: var(--hi); }
.cell.door.lo .fb { color: var(--lo); }
.cell.door.zero .tg { color: #c4cbe0; }
.cell.reveal { outline: 3px dashed var(--ok); outline-offset: -3px; }

.hint { text-align: center; color: var(--muted); min-height: 1.4em; padding: 6px 4px 2px; font-size: .95rem; }
.controls { display: grid; grid-template-columns: 1fr 2fr; gap: 8px; padding-top: 6px; }
.history { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 4px; max-height: 22vh; overflow-y: auto; }
.history li { display: flex; gap: 8px; align-items: center; font-size: .92rem; color: var(--muted); }
.history .hn { min-width: 64px; font-weight: 600; color: var(--ink); }
.history .chips { display: flex; gap: 3px; flex-wrap: wrap; }
.chip { min-width: 26px; height: 24px; padding: 0 3px; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: .8rem; border: 1.5px solid currentColor; background: #0b101d; }
.chip.ok { color: var(--ok); } .chip.hi { color: var(--hi); } .chip.lo { color: var(--lo); }

/* Overlays */
.overlay { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; background: rgba(3, 5, 10, .72); visibility: hidden; opacity: 0; transition: opacity .18s ease, visibility .18s; }
.overlay.open { visibility: visible; opacity: 1; }
.sheet { width: 100%; max-width: 560px; max-height: 92dvh; display: flex; flex-direction: column; background: var(--bg1); border: 1.5px solid var(--line); border-bottom: 0; border-radius: 18px 18px 0 0; padding: 18px calc(16px + var(--sr)) calc(16px + var(--sb)) calc(16px + var(--sl)); }
.sheet .scroll-body { display: flex; flex-direction: column; gap: 12px; }
.sheet h2 { font-size: 1.45rem; }
.small { font-size: .9rem; color: var(--muted); }
.howto-steps { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 14px; }
.howto-steps li { background: var(--surface); border: 1.5px solid var(--line); border-radius: 14px; padding: 12px; display: flex; flex-direction: column; gap: 6px; }
.howto-steps h3 { font-size: 1.1rem; display: flex; align-items: center; gap: 8px; }
.step-num { width: 28px; height: 28px; border-radius: 50%; background: var(--amber); color: var(--amber-ink); display: inline-flex; align-items: center; justify-content: center; font: 700 .95rem var(--sans); }
.mini { display: flex; gap: 3px; margin-top: 4px; }
.m { width: 38px; height: 38px; border-radius: 6px; display: inline-flex; align-items: center; justify-content: center; font-weight: 700; background: var(--cobble); color: var(--amber-text); }
.m.wall { background: var(--brick); }
.m.lamp { background: #3a2c16; border: 2px solid var(--amber); color: #fff2d0; }
.m.lit { background: #4a3a22; }
.m.turn { background: #33291c; border-bottom: 3px solid var(--amber); }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: .92rem; }
.sym { font-weight: 700; }
.sym.ok { color: var(--ok); } .sym.hi { color: var(--hi); } .sym.lo { color: var(--lo); }
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 0; }
.stat-grid div { background: var(--surface); border-radius: 12px; padding: 10px 6px; text-align: center; }
.stat-grid dt { font-size: .82rem; color: var(--muted); order: 2; }
.stat-grid dd { margin: 0; font: 700 1.5rem var(--serif); }
.stat-grid div { display: flex; flex-direction: column-reverse; }
.dist-title { font-size: 1.05rem; }
.dist { display: flex; flex-direction: column; gap: 5px; }
.dist-row { display: grid; grid-template-columns: 86px 1fr 34px; gap: 8px; align-items: center; font-size: .92rem; }
.dist-bar { height: 18px; border-radius: 5px; background: var(--surface-2); overflow: hidden; }
.dist-bar span { display: block; height: 100%; background: var(--amber); min-width: 3px; }
.stars { font-size: 2rem; color: var(--amber); letter-spacing: 4px; text-align: center; }
.result-sub { text-align: center; color: var(--muted); }
.share-box { margin: 0; padding: 12px; background: #0b101d; border: 1.5px solid var(--line); border-radius: 12px; font: 1rem/1.5 var(--sans); white-space: pre-wrap; text-align: center; user-select: text; }
.row-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.toggle { display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 6px 10px; background: var(--surface); border-radius: 12px; cursor: pointer; }
.toggle input { width: 26px; height: 26px; accent-color: var(--amber); margin: 0; flex: 0 0 26px; }
.archive-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.archive-list .btn { width: 100%; display: flex; justify-content: space-between; align-items: center; text-align: left; gap: 8px; }
.archive-list .a-meta { color: var(--muted); font-weight: 500; font-size: .9rem; }
.toast { position: fixed; left: 50%; top: calc(14px + var(--st)); transform: translateX(-50%); background: var(--ink); color: var(--bg0); padding: 10px 16px; border-radius: 10px; font-weight: 600; z-index: 40; visibility: hidden; opacity: 0; transition: opacity .2s; max-width: 90vw; text-align: center; }
.toast.show { visibility: visible; opacity: 1; }

/* Landscape phones: board left, controls right */
@media (orientation: landscape) and (max-height: 520px) {
  #app { max-width: none; }
  #screen-play { display: grid; grid-template-columns: auto minmax(220px, 1fr); grid-template-rows: auto auto 1fr auto auto; column-gap: 12px; }
  #screen-play .board-wrap { grid-column: 1; grid-row: 1 / 6; }
  #screen-play .bar, #screen-play .hud, #screen-play .hint, #screen-play .controls, #screen-play .history { grid-column: 2; }
  .history { max-height: none; }
  .sheet { max-height: 100dvh; border-radius: 14px 14px 0 0; }
  .home-nav { grid-template-columns: repeat(3, 1fr); }
  .home-nav .btn:last-child { grid-column: auto; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { transition: none !important; animation: none !important; } }
.reduce-motion *, .reduce-motion *::before, .reduce-motion *::after { transition: none !important; animation: none !important; }
@keyframes pop { 0% { transform: scale(.85); } 100% { transform: scale(1); } }
.cell.lamp { animation: pop .14s ease-out; }
