:root {
  --paper: #e9edf4; --grid: #d6dde9; --ink: #262a33; --ink-soft: #5d6574; --cell: #f7f9fc; --cell-line: #3a404c; --slot: #c3cbd8;
  --hi: #f4c430; --hi-ink: #2a2200; --ok: #2e8b57; --bad: #c8414f;
  --c0: #e4572e; --c1: #1f8a8a; --c2: #f2b134; --c3: #6c4ab6;
  --f-num: "Martian Mono", ui-monospace, Menlo, monospace;
  --f-body: "Figtree", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #15181e; --grid: #1f242d; --ink: #e6e9ef; --ink-soft: #9aa3b2; --cell: #1d2129; --cell-line: #aeb6c4; --slot: #3a414e;
    --ok: #4fbf83; --bad: #ec6b78;
    --c0: #f0704a; --c1: #2fb3b0; --c2: #f5c04f; --c3: #9a7ae0;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #15181e; --grid: #1f242d; --ink: #e6e9ef; --ink-soft: #9aa3b2; --cell: #1d2129; --cell-line: #aeb6c4; --slot: #3a414e;
  --ok: #4fbf83; --bad: #ec6b78;
  --c0: #f0704a; --c1: #2fb3b0; --c2: #f5c04f; --c3: #9a7ae0;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* game.js blendet die Karte über das hidden-Attribut aus; ohne diese Regel gewinnt display:flex von .overlay */
[hidden] { display: none !important; }

/* Seite = exakt der sichtbare Bereich, nichts scrollt (ausser der Karte, falls sie zu hoch wird).
   svh statt dvh: bleibt stabil, während mobile Browserleisten ein- und ausfahren. */
html, body { margin: 0; height: 100%; height: 100svh; overflow: hidden; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.4 var(--f-body);
  background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 22px 22px;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
}
button { font: inherit; color: inherit; }
button:focus-visible { outline: 3px solid var(--hi); outline-offset: 2px; }

/* ---------- Layout ----------
   HUD (fix) / Spielfeld (nimmt den Rest, SVG skaliert hinein) / Leiste (fix) */
.wrap {
  height: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding:
    max(12px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ---------- HUD ---------- */
.hud { display: grid; grid-template-columns: auto 1fr auto auto; gap: 14px; align-items: center; flex: none; }
.time { font-family: var(--f-num); font-weight: 800; font-size: clamp(1.5rem, 7vw, 2rem); font-variant-numeric: tabular-nums; min-width: 3.2ch; line-height: 1; }
.time.low { color: var(--bad); }
.bar { height: 10px; border-radius: 5px; background: var(--slot); overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--ink); width: 100%; transition: width .2s linear; }
.bar.low i { background: var(--bad); }
.score { text-align: right; font-family: var(--f-num); font-variant-numeric: tabular-nums; line-height: 1.1; }
.score b { display: block; font-size: clamp(1.25rem, 5.5vw, 1.6rem); font-weight: 800; }
.quit {
  width: 40px; height: 40px; padding: 0; margin-right: -4px;
  border: 1.5px solid var(--cell-line); border-radius: 50%; background: var(--cell); cursor: pointer;
  display: grid; place-items: center;
}
.quit svg { width: 18px; height: 18px; stroke: var(--ink); stroke-width: 2.6; stroke-linecap: round; fill: none; }
.quit:active { background: var(--grid); }
.score small { font-size: .68rem; color: var(--ink-soft); letter-spacing: .06em; text-transform: uppercase; font-family: var(--f-body); }

/* ---------- Spielfeld ---------- */
.board { flex: 1 1 auto; min-height: 0; width: 100%; position: relative; display: flex; }
.board svg { flex: 1; width: 100%; height: 100%; display: block; touch-action: manipulation; overflow: visible; }

/* ---------- Leiste ----------
   Breite an die Höhe gekoppelt, damit die Kacheln im Querformat nicht das Feld verdrängen */
.dock {
  flex: none;
  width: min(100%, 84svh);
  align-self: center;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: clamp(4px, 1.6vw, 8px);
  align-items: center;
  padding-top: 4px;
}
.tile {
  aspect-ratio: 1;
  min-width: 0;
  border: 2px solid transparent;
  background: var(--cell);
  border-radius: 12px;
  padding: 3px;
  cursor: pointer;
  transition: transform .12s ease-out, background-color .12s;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .08);
}
.tile svg { width: 100%; height: 100%; display: block; pointer-events: none; }
.tile[aria-pressed="true"] { border-color: var(--hi); background: var(--hi); transform: translateY(-4px); }
.tile.dead { opacity: .45; }
.tile.new { animation: drop .22s ease-out; }
@keyframes drop { from { transform: translateY(14px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- Karte (Menü, Regeln, Einstellungen, Bestenliste, Rundenende) ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: safe center;
  justify-content: center;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding:
    max(16px, env(safe-area-inset-top))
    max(16px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--paper) 82%, transparent);
  -webkit-backdrop-filter: blur(3px); backdrop-filter: blur(3px);
}
.card {
  margin: auto 0;
  background: var(--cell);
  border: 2px solid var(--cell-line);
  border-radius: 16px;
  padding: 22px 20px;
  max-width: 380px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .12);
}
.card h1 { font-family: var(--f-num); font-weight: 800; font-size: 1.5rem; margin: 0; }
.card p { margin: 0; color: var(--ink-soft); font-size: .95rem; line-height: 1.45; }
.card .big { font-family: var(--f-num); font-weight: 800; font-size: 2.6rem; color: var(--ink); line-height: 1.1; font-variant-numeric: tabular-nums; }
.card .big.low { color: var(--bad); }
.card p.mode { font-family: var(--f-num); font-size: .8rem; font-weight: 600; letter-spacing: .02em; margin-top: -6px; }
.filters { display: flex; flex-direction: column; gap: 10px; }
.filters .seg button { min-height: 38px; padding: 6px 4px; font-size: .88rem; }

.menu { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.go { border: 0; background: var(--ink); color: var(--paper); font: 700 1.05rem var(--f-body); min-height: 50px; padding: 12px; border-radius: 12px; cursor: pointer; }
.go:active { transform: scale(.98); }
.sec { border: 1.5px solid var(--cell-line); background: var(--cell); color: var(--ink); font: 600 .95rem var(--f-body); min-height: 46px; padding: 10px; border-radius: 12px; cursor: pointer; }
.sec:active { background: var(--grid); }
@media (hover: hover) { .sec:hover { background: var(--grid); } }

.card .rules { text-align: left; display: flex; flex-direction: column; gap: 10px; }
.card .rules p { color: var(--ink); }
.card .rules b { font-family: var(--f-num); font-size: .85em; }

.set { display: flex; flex-direction: column; gap: 6px; text-align: left; }
.set label { font-size: .72rem; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-soft); }
.seg { display: flex; border: 1.5px solid var(--cell-line); border-radius: 10px; overflow: hidden; }
.seg button { flex: 1; border: 0; background: var(--cell); color: var(--ink); font: 600 .95rem var(--f-body); min-height: 44px; padding: 9px 6px; cursor: pointer; }
.seg button + button { border-left: 1.5px solid var(--cell-line); }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.chk { display: flex; gap: 10px; align-items: center; font-size: .95rem; text-align: left; cursor: pointer; min-height: 44px; }
.chk input { flex: none; width: 20px; height: 20px; margin: 0; accent-color: var(--ink); }

.tswrap { overflow-x: auto; }
table.hs { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: .88rem; }
table.hs th { font-size: .66rem; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--ink-soft); text-align: right; padding: 4px; border-bottom: 1.5px solid var(--cell-line); }
table.hs td { text-align: right; padding: 7px 4px; border-bottom: 1px solid var(--grid); }
table.hs th:first-child, table.hs td:first-child { text-align: left; }
table.hs td.pts { font-family: var(--f-num); font-weight: 800; }
table.hs tr.me td { background: color-mix(in srgb, var(--hi) 35%, transparent); }
.empty { color: var(--ink-soft); font-size: .95rem; }
.link { background: none; border: 0; color: var(--ink-soft); font: 500 .85rem var(--f-body); text-decoration: underline; cursor: pointer; padding: 8px; }

/* ---------- Effekte ---------- */
.shake { animation: shake .28s; }
@keyframes shake { 0%, 100% { transform: translateX(0); } 20% { transform: translateX(-6px); } 60% { transform: translateX(6px); } }
.fx { font-family: var(--f-num); font-weight: 800; pointer-events: none; animation: rise .9s ease-out forwards; }
@keyframes rise { from { opacity: 1; transform: translateY(0); } to { opacity: 0; transform: translateY(-26px); } }
.pop { animation: popin .18s ease-out; transform-box: fill-box; transform-origin: center; }
@keyframes popin { from { transform: scale(.4); } to { transform: scale(1); } }
.fade { animation: fadeout .35s ease-in forwards; transform-box: fill-box; transform-origin: center; }
@keyframes fadeout { to { opacity: 0; transform: scale(.7); } }
@media (prefers-reduced-motion: reduce) { .fx, .pop, .fade, .shake, .tile.new { animation: none; } .fade { opacity: 0; } .tile { transition: none; } }

/* ---------- Querformat auf dem Handy ----------
   Zu wenig Höhe für drei Zeilen: Feld links, HUD und Leiste (2 × 3) rechts daneben */
@media (orientation: landscape) and (max-height: 540px) {
  .wrap {
    max-width: 900px;
    display: grid;
    grid-template-columns: 1fr minmax(180px, 34%);
    grid-template-rows: auto 1fr;
    column-gap: 20px;
    row-gap: 12px;
  }
  .board { grid-column: 1; grid-row: 1 / span 2; }
  .hud { grid-column: 2; grid-row: 1; }
  .dock { grid-column: 2; grid-row: 2; align-self: center; width: min(100%, 52svh * 1.5); grid-template-columns: repeat(3, 1fr); }
}
