:root {
  color-scheme: dark;
  font-family: system-ui, sans-serif;
  background: #fff;
  color: #f5f3e8;
  --accent: #f4ba75;
  --muted: #d3d4c9;
  --line: #666b59;
  --surface: #22251f;
}

*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; }
h1, h2, p, dl, dd { margin: 0; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

.game {
  position: relative;
  isolation: isolate;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: #fff center / cover no-repeat;
}

.game[data-background="olive"] { background-color: #34382a; background-image: url('/assets/room.webp'); }
.game[data-background="dusk"] { background-color: #2e434a; background-image: url('/assets/dusk.webp'); }
.game:fullscreen { width: 100%; height: 100%; }
.arena, canvas, .overlay { position: absolute; inset: 0; width: 100%; height: 100%; }
.arena { touch-action: none; }
.arena:focus-visible { outline-offset: -5px; }
.arena.running { cursor: none; }
canvas { display: block; pointer-events: none; }

.hud {
  position: absolute;
  z-index: 3;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 8px;
  pointer-events: none;
}

.scoreboard, .tools {
  display: flex;
  gap: 12px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #22251ff2;
}

.scoreboard { font-variant-numeric: tabular-nums; }
.scoreboard div { min-width: 3ch; }
.scoreboard dt { color: var(--muted); font-size: .75rem; line-height: 1rem; }
.scoreboard dd { font-size: 1.25rem; font-weight: 650; line-height: 1.5rem; }
.scoreboard div:first-child dd { color: var(--accent); }
.limit { color: var(--muted); font-size: .75rem; }
.tools { padding: 2px; gap: 0; pointer-events: auto; }

.tools button, dialog form button {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #f5f3e8;
  cursor: pointer;
}
.tools button:hover, dialog form button:hover { background: #ffffff20; }
.tools svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.55; stroke-linecap: round; stroke-linejoin: round; }
#sound[aria-pressed="false"] .waves, #sound[aria-pressed="true"] .muted { display: none; }

.overlay {
  z-index: 1;
  display: grid;
  place-items: center;
  padding: 72px 12px;
}
.game[data-background]:not([data-background="white"]) .overlay { background: #161a1373; }
.panel, dialog {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  color: #f5f3e8;
}
.panel {
  display: grid;
  justify-items: center;
  gap: 12px;
  width: min(100%, 360px);
  padding: 24px;
  text-align: center;
}
.panel h1, .panel h2 { font-size: 1.875rem; line-height: 2.25rem; letter-spacing: -.04em; text-wrap: balance; }
.panel p { color: var(--muted); font-size: .875rem; line-height: 1.25rem; text-wrap: pretty; }
.primary {
  min-height: 44px;
  margin-top: 4px;
  padding: 8px 16px;
  background: var(--accent);
  color: #211b11;
  border: 0;
  border-radius: 8px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: background .2s cubic-bezier(.32,.72,0,1);
}
.primary:hover { background: #ffd19a; }

.game-details {
  position: absolute;
  z-index: 2;
  bottom: max(12px, env(safe-area-inset-bottom));
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 4px 12px;
  pointer-events: none;
}
.game-details p, .unlock {
  padding: 4px 8px;
  border-radius: 8px;
  background: #22251ff2;
  color: var(--muted);
  font-size: .75rem;
  line-height: 1rem;
}
.points { display: flex; gap: 8px; }
.points b { color: var(--accent); }
.credit a { color: inherit; pointer-events: auto; text-underline-offset: 2px; }
.credit a:hover { color: var(--accent); }
.unlock {
  position: absolute;
  z-index: 3;
  top: 76px;
  left: max(12px, env(safe-area-inset-left));
  color: var(--accent);
  pointer-events: none;
}
.escape-track { position: absolute; z-index: 2; inset: auto 0 0; height: 3px; background: #181a1540; pointer-events: none; }
#escape-meter { height: 100%; transform: scaleX(0); transform-origin: left; background: #da773f; transition: transform .22s ease-out; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

dialog { width: min(390px, calc(100% - 24px)); padding: 16px; }
dialog::backdrop { background: #161a1380; }
dialog header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
dialog header h2 { font-size: 1.25rem; }
dialog form button { font-size: 1.5rem; }
.background-options { display: grid; gap: 8px; }
.background-choice {
  display: grid;
  grid-template-columns: 56px 1fr;
  align-items: center;
  gap: 12px;
  min-height: 48px;
  padding: 8px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: inherit;
  background: #30342a;
  font: inherit;
  cursor: pointer;
}
.background-choice[aria-pressed="true"] { border-color: var(--accent); }
.background-choice:disabled { opacity: .62; cursor: default; }
.background-choice .preview { width: 56px; height: 32px; border-radius: 4px; background: white center / cover; }
.background-choice[data-background="olive"] .preview { background-image: url('/assets/room.webp'); }
.background-choice[data-background="dusk"] .preview { background-image: url('/assets/dusk.webp'); }
.background-choice small { display: block; color: var(--muted); font-size: .75rem; }

@media (max-width: 540px) {
  .hud { flex-wrap: wrap; }
  .scoreboard { gap: 8px; padding-inline: 8px; }
  .tools button { width: 40px; }
  .game-details p { font-size: .75rem; }
  .unlock { top: auto; bottom: max(76px, calc(env(safe-area-inset-bottom) + 64px)); }
}

@media (max-width: 360px) {
  .tools button { width: 36px; }
  .scoreboard dd { font-size: 1rem; }
}

@media (max-height: 420px) {
  .overlay { padding: 55px 12px 42px; }
  .panel { padding: 12px 16px; gap: 8px; }
}
