:root {
  color-scheme: light dark;
  --bg: #f4f1ea;
  --surface: #fffdf8;
  --surface-2: #ebe6da;
  --text: #1d1b26;
  --muted: #6b6677;
  --line: #ddd6c7;
  --accent: #3b3f9e;
  --accent-text: #ffffff;
  --good: #1f7a3f;
  --good-bg: #e3f2e7;
  --bad: #b3261e;
  --bad-bg: #fbe5e2;
  --warn: #8a5a00;
  --radius: 14px;
  --gutter: 16px;
  --top-h: calc(env(safe-area-inset-top) + 61px); /* feste Höhe, damit der Tabellenkopf darunter andocken kann */
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12142b;
    --surface: #1c1f3d;
    --surface-2: #2a2e57;
    --text: #ecebf5;
    --muted: #9a9cc0;
    --line: #30345f;
    --accent: #e0b44a;
    --accent-text: #1a1400;
    --good: #6ad796;
    --good-bg: rgba(106, 215, 150, 0.13);
    --bad: #ff8f86;
    --bad-bg: rgba(255, 143, 134, 0.13);
    --warn: #f0c870;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  -webkit-tap-highlight-color: transparent;
}

#app {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  max-width: 560px;
  margin: 0 auto;
}

main {
  flex: 1;
  padding: 12px var(--gutter) 16px;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  cursor: pointer;
  touch-action: manipulation; /* kein Doppeltipp-Zoom beim schnellen Plus/Minus */
}

button:disabled { cursor: default; opacity: 0.35; }

/* ---------- Kopfzeile ---------- */

.top {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 8px;
  height: var(--top-h);
  padding: calc(env(safe-area-inset-top) + 10px) var(--gutter) 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}

.title { flex: 1; display: flex; align-items: baseline; gap: 6px; white-space: nowrap; }
.title strong { font-size: 1.25rem; }
.title span { color: var(--muted); }

.tabs {
  display: flex;
  background: var(--surface-2);
  border-radius: 999px;
  padding: 3px;
}

.tabs button {
  background: none;
  padding: 7px 12px;
  border-radius: 999px;
  font-size: 0.9rem;
  color: var(--muted);
}

.tabs button[aria-pressed="true"] { background: var(--surface); color: var(--text); font-weight: 600; }

.icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: none;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--muted);
}

@media (max-width: 360px) {
  .top { gap: 4px; }
  .title span { display: none; }
  .tabs button { padding: 7px 10px; }
  .icon { width: 34px; }
}

/* ---------- Aktionsleiste unten ---------- */

.action {
  position: sticky;
  bottom: 0;
  padding: 10px var(--gutter) calc(env(safe-area-inset-bottom) + 12px);
  background: var(--bg);
  border-top: 1px solid var(--line);
}

.status { margin: 0 0 10px; text-align: center; font-size: 0.95rem; color: var(--muted); }
.status b { color: var(--text); }
.good { color: var(--good); font-weight: 600; }
.bad { color: var(--bad); font-weight: 600; }
.warn { color: var(--warn); }

.buttons { display: flex; gap: 10px; }
.buttons > * { flex: 1; }
.buttons .secondary { flex: 0 0 auto; }

.primary, .secondary, .ghost {
  min-height: 52px;
  padding: 0 18px;
  border-radius: var(--radius);
  font-weight: 600;
}

.action > .primary { width: 100%; }
.primary { background: var(--accent); color: var(--accent-text); font-size: 1.05rem; }
.secondary { background: var(--surface-2); }
.ghost { background: none; color: var(--accent); }

/* ---------- Runde ---------- */

.round-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.cards { font-weight: 600; }

.steps { display: flex; gap: 6px; margin: 0; padding: 0; list-style: none; font-size: 0.85rem; }
.steps li { padding: 4px 10px; border-radius: 999px; color: var(--muted); background: var(--surface-2); }
.steps li.active { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.steps li.done::after { content: " ✓"; }

.players { margin: 0; padding: 0; list-style: none; display: grid; gap: 8px; }

.player {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px 10px 10px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.players.tricks .player { grid-template-columns: 1fr auto 3.2rem; }
.player.hit { border-color: color-mix(in srgb, var(--good) 40%, var(--line)); }
.player.miss { border-color: color-mix(in srgb, var(--bad) 40%, var(--line)); }

.who { min-width: 0; display: flex; flex-direction: column; }
.name { font-weight: 600; font-size: 1.05rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta { color: var(--muted); font-size: 0.85rem; white-space: nowrap; }
.meta b { color: var(--text); }

.tag {
  display: inline-block;
  margin-right: 6px;
  padding: 0 6px;
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 0.75rem;
  font-weight: 600;
}

.points { text-align: right; font-weight: 700; }
.hit .points { color: var(--good); }
.miss .points { color: var(--bad); }

.stepper { display: flex; align-items: center; gap: 4px; }

.stepper button {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: var(--surface-2);
  font-size: 1.6rem;
  line-height: 1;
}

.stepper button:active:not(:disabled) { background: var(--accent); color: var(--accent-text); }

.stepper output {
  min-width: 2.2ch;
  text-align: center;
  font-size: 1.6rem;
  font-weight: 700;
}

/* ---------- Tabelle ---------- */

.table-view { padding-left: 0; padding-right: 0; }

/* Kein overflow an Tabelle oder Wrapper: das würde den klebenden Tabellenkopf abschalten.
   Feste Spaltenbreiten sorgen dafür, dass auch 6 Spieler auf 320px passen. */
.table-wrap { padding: 0 var(--gutter); }

.score {
  width: 100%;
  table-layout: fixed;
  border-collapse: separate;
  border-spacing: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.score th, .score td {
  padding: 6px 4px;
  text-align: center;
  border-bottom: 1px solid var(--line);
}

.score thead th {
  position: sticky;
  top: var(--top-h);
  z-index: 1;
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: var(--surface-2);
}

.score thead th:first-child { width: 2rem; border-top-left-radius: calc(var(--radius) - 1px); }
.score thead th:last-child { border-top-right-radius: calc(var(--radius) - 1px); }
.score tfoot th { border-bottom-left-radius: calc(var(--radius) - 1px); }
.score tfoot td:last-child { border-bottom-right-radius: calc(var(--radius) - 1px); }

.score tbody th, .score tfoot th { color: var(--muted); font-weight: 500; }
.score td b { display: block; font-size: min(1.05rem, 4.2vw); }
.score td small { display: block; font-size: 0.75rem; color: var(--muted); }
.score td.hit small { color: var(--good); }
.score td.miss { background: var(--bad-bg); }
.score td.miss small { color: var(--bad); }
.score tbody tr[data-action] { cursor: pointer; }
.score tr.now { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.score tr.future th { opacity: 0.4; }
.score tr.future td, .score tr.future th { height: 2.2rem; }
.score tfoot td, .score tfoot th { border-bottom: 0; font-weight: 700; font-size: 1.1rem; background: var(--surface-2); }

.note { color: var(--muted); font-size: 0.85rem; text-align: center; padding: 0 var(--gutter); }

/* ---------- Ergebnis ---------- */

.ranking { margin: 8px 0; padding: 0; list-style: none; display: grid; gap: 8px; }

.ranking li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-size: 1.1rem;
}

.ranking .place { width: 2ch; color: var(--muted); font-weight: 700; }
.ranking .name { flex: 1; }
.ranking .pts { font-weight: 700; }
.ranking .place-1 { border-color: var(--accent); font-size: 1.35rem; }
.ranking .place-1 .place { color: var(--accent); }

/* ---------- Spielstart ---------- */

.setup h2 { font-size: 1rem; margin: 18px 0 8px; }
.setup h2:first-child { margin-top: 4px; }
.setup h2 small { display: block; color: var(--muted); font-weight: 400; font-size: 0.85rem; }

.names { margin: 0 0 6px; padding: 0; list-style: none; display: grid; gap: 8px; counter-reset: seat; }
.names li { display: flex; align-items: center; gap: 6px; }

.names input {
  flex: 1;
  min-width: 0;
  height: 50px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1.05rem; /* ≥16px verhindert Auto-Zoom auf iOS */
}

.names input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip input { position: absolute; opacity: 0; pointer-events: none; }

.chip span {
  display: block;
  padding: 9px 14px;
  border-radius: 999px;
  background: var(--surface-2);
  cursor: pointer;
}

.chip input:checked + span { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.chip input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.toggle {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin-top: 20px;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.toggle input { width: 22px; height: 22px; margin: 2px 0 0; accent-color: var(--accent); flex: none; }
.toggle small { display: block; color: var(--muted); font-size: 0.85rem; margin-top: 2px; }

/* ---------- Bottom-Sheet ---------- */

dialog {
  width: 100%;
  max-width: 560px;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 20px 20px 0 0;
  background: var(--bg);
  color: var(--text);
}

dialog::backdrop { background: rgba(0, 0, 0, 0.45); }

.sheet { padding: 18px var(--gutter) calc(env(safe-area-inset-bottom) + 16px); }
.sheet h2 { margin: 0 0 12px; font-size: 1.15rem; }
.sheet h2 small { color: var(--muted); font-weight: 400; font-size: 0.9rem; margin-left: 6px; }

.menu { display: grid; gap: 10px; }

.edit-head, .edit li {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 8px;
}

.edit-head { color: var(--muted); font-size: 0.8rem; text-align: center; }
.edit-head span:not(:first-child) { width: 112px; }
.edit { margin: 4px 0 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.edit .stepper { gap: 2px; }
.edit .stepper button { width: 38px; height: 42px; font-size: 1.4rem; }
.edit .stepper output { font-size: 1.3rem; min-width: 32px; }
.sheet .status { margin: 14px 0 10px; }
