:root {
  --felt: #1d3a31;
  --felt-2: #24493d;
  --panel: #f6f3ec;
  --ink: #1f2522;
  --muted: #5d6862;
  --accent: #e0893a;
  --accent-ink: #fff;
  --line: #d9d3c6;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root { --panel: #18201c; --ink: #ecefe9; --muted: #a2ada6; --line: #2f3a34; }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--felt);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }

/* ---------- Menu ---------- */
#menu { min-height: 100%; background: var(--panel); }
.menu-inner {
  max-width: 560px; margin: 0 auto;
  padding: calc(20px + var(--safe-top)) 16px calc(28px + var(--safe-bottom));
}
.menu-head { display: flex; gap: 14px; align-items: center; margin-bottom: 18px; }
.logo { width: 56px; height: 56px; border-radius: 14px; box-shadow: 0 2px 8px rgba(0,0,0,.2); }
h1 { margin: 0; font-size: 28px; letter-spacing: -.02em; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 22px 0 10px; }
.sub { margin: 2px 0 0; color: var(--muted); }
.hint, .credit { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.credit a { color: inherit; }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile {
  aspect-ratio: 4 / 3; border: 0; border-radius: 10px; padding: 0;
  background: var(--line) center / cover no-repeat;
  color: var(--ink); font: inherit; font-size: 13px; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  outline: 3px solid transparent; outline-offset: 2px; cursor: pointer;
}
.tile svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tile.selected { outline-color: var(--accent); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-width: 56px; padding: 10px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: transparent; color: var(--ink); font: inherit; font-weight: 600; cursor: pointer;
}
.chip.selected { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.switch { display: flex; gap: 14px; align-items: flex-start; cursor: pointer; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch .track {
  flex: none; width: 50px; height: 30px; border-radius: 15px; background: var(--line);
  position: relative; transition: background .2s; margin-top: 2px;
}
.switch .thumb {
  position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .2s;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track .thumb { transform: translateX(20px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch .lbl { font-weight: 600; }
.switch small { display: block; font-weight: 400; color: var(--muted); font-size: 13px; }

.btn {
  display: block; border: 1px solid var(--line); background: transparent; color: var(--ink);
  font: inherit; font-weight: 700; padding: 14px 18px; border-radius: 12px; cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.wide { width: 100%; margin-top: 12px; }
#btn-start { margin-top: 26px; }

/* ---------- Game ---------- */
#game { position: fixed; inset: 0; overflow: hidden; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; }
#hud {
  position: absolute; left: 0; right: 0; top: 0;
  padding: calc(8px + var(--safe-top)) 10px 8px;
  display: flex; align-items: center; gap: 6px;
  background: linear-gradient(rgba(0,0,0,.45), rgba(0,0,0,0));
  pointer-events: none;
}
#hud > * { pointer-events: auto; }
.icon-btn {
  width: 42px; height: 42px; border-radius: 50%; border: 0; flex: none;
  background: rgba(0,0,0,.35); color: #fff; display: grid; place-items: center; cursor: pointer;
}
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn[aria-pressed="true"] { background: var(--accent); }
#status {
  flex: 1; color: #fff; font-weight: 700; display: flex; flex-direction: column; align-items: center;
  font-variant-numeric: tabular-nums; text-shadow: 0 1px 2px rgba(0,0,0,.5); line-height: 1.2;
}
#st-time { font-weight: 500; font-size: 13px; opacity: .85; }

#preview {
  position: absolute; inset: 0; background: rgba(0,0,0,.8);
  display: grid; place-items: center; padding: 16px; cursor: pointer;
}
#preview img { max-width: 100%; max-height: 100%; border-radius: 8px; box-shadow: 0 10px 40px rgba(0,0,0,.5); }

#win { position: absolute; inset: 0; display: grid; place-items: end center; padding: 16px 16px calc(24px + var(--safe-bottom)); background: rgba(0,0,0,.25); }
#win .card {
  width: 100%; max-width: 400px; background: var(--panel); color: var(--ink);
  border-radius: 18px; padding: 20px; box-shadow: 0 10px 40px rgba(0,0,0,.4);
}
#win h2 { color: var(--ink); text-transform: none; letter-spacing: 0; font-size: 24px; margin: 0 0 6px; }
#win p { margin: 0; color: var(--muted); }

#busy {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); color: #fff; z-index: 10;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
}
.spinner { width: 40px; height: 40px; border-radius: 50%; border: 4px solid rgba(255,255,255,.25); border-top-color: #fff; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(1turn); } }

#toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-bottom)); transform: translateX(-50%);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 11;
  max-width: calc(100% - 32px); text-align: center; font-size: 14px;
}
