:root {
  --bg: #f2f5f3;
  --card: #ffffff;
  --text: #1b2320;
  --muted: #67716c;
  --line: #e3e8e5;
  --accent: #15803d;
  --accent-strong: #166534;
  --accent-weak: #e6f4eb;
  --warn: #b45309;
  --danger: #b91c1c;
  --on-warn: #ffffff;
  --die-face: #ffffff;
  --die-pip: #1b2320;
  --die-border: #c9d2cd;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 16px rgb(0 0 0 / 0.05);
  --page: 560px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1311;
    --card: #18201c;
    --text: #e6ebe8;
    --muted: #97a39d;
    --line: #28322d;
    --accent: #1f9d4f;
    --accent-strong: #4ade80;
    --accent-weak: #15301f;
    --warn: #f59e0b;
    --danger: #f87171;
    --on-warn: #1b1300;
    --die-face: #f3f5f4;
    --die-pip: #18201c;
    --die-border: #46524c;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: calc(var(--dock-h, 0px) + 16px);
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}
button:disabled { cursor: default; }
button:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Kopfzeile */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) max(16px, calc((100% - var(--page)) / 2 + 16px)) 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
}
.brand { font-weight: 750; font-size: 18px; }
.expected { color: var(--muted); font-size: 13px; text-align: right; }
.expected strong { color: var(--text); font-size: 18px; font-variant-numeric: tabular-nums; }

main { max-width: var(--page); margin: 0 auto; padding: 0 8px; }

.card {
  background: var(--card);
  border-radius: 18px;
  padding: 12px;
  margin: 0 0 10px;
  box-shadow: var(--shadow);
}

/* Würfelbereich */
.turn-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 18px; }
.tabs { display: flex; background: var(--bg); border-radius: 11px; padding: 3px; }
.tab { padding: 8px 12px; border-radius: 8px; font-size: 14px; color: var(--muted); }
.tab.active { background: var(--card); color: var(--text); font-weight: 650; box-shadow: 0 1px 3px rgb(0 0 0 / 0.14); }
.link { color: var(--muted); font-size: 14px; padding: 8px 4px; }

.face { fill: var(--die-face); stroke: var(--die-border); stroke-width: 3; }
.pip { fill: var(--die-pip); }

.dice-row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.die {
  position: relative;
  min-width: 0;
  aspect-ratio: 1;
  border-radius: 20%;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.die svg { display: block; width: 100%; height: 100%; }
.die.empty { border: 2px dashed var(--die-border); }
/* Nächster freier Platz: pulsiert, bis er gefüllt ist */
.die.next {
  border-style: solid;
  border-color: var(--accent);
  background: var(--accent-weak);
  animation: next-pulse 1.4s ease-in-out infinite;
}
@keyframes next-pulse {
  50% { box-shadow: 0 0 0 5px color-mix(in srgb, var(--accent) 28%, transparent); }
}
/* Neu würfeln: angehoben, grüner Ring, ↻-Plakette – Augenzahl bleibt lesbar */
.die.reroll {
  transform: translateY(-5px);
  box-shadow: 0 0 0 3px var(--accent), 0 8px 14px rgb(0 0 0 / 0.18);
}
.die.reroll::after {
  content: "↻";
  position: absolute;
  top: -10px;
  right: -8px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 800;
  line-height: 1;
  box-shadow: 0 0 0 2px var(--card);
}
@media (prefers-reduced-motion: reduce) {
  .die.next { animation: none; }
  .die, .key { transition: none; }
}
.dice-hint { text-align: center; font-size: 13px; color: var(--muted); margin: 10px 0 0; min-height: 18px; }

/* Empfehlung */
.advice { margin-top: 14px; min-height: 70px; }
.rec { background: var(--accent-weak); border-radius: 14px; padding: 12px 14px; }
.rec-label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--accent-strong); font-weight: 700; }
.rec-title { font-size: 20px; font-weight: 750; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 2px; }
.rec-sub { color: var(--muted); margin-top: 4px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; font-size: 15px; }
.mini-dice { display: inline-flex; gap: 4px; vertical-align: middle; }
.mini-die { width: 28px; height: 28px; display: inline-block; }
.mini-die svg { width: 100%; height: 100%; display: block; }
.rec-sub .mini-die, .alts .mini-die { width: 21px; height: 21px; }
.choice { margin-top: 8px; font-size: 14px; }
.warn { color: var(--warn); font-weight: 600; }
.hint { color: var(--muted); text-align: center; margin: 10px 0; }
.error { color: var(--danger); text-align: center; }
.error button { text-decoration: underline; margin-left: 6px; }

.alts-toggle { margin-top: 6px; font-size: 14px; color: var(--accent-strong); padding: 8px 0; font-weight: 600; }
.alts { list-style: none; margin: 0; padding: 0; }
.alts button {
  width: 100%;
  min-height: 44px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 8px 4px;
  border-top: 1px solid var(--line);
  text-align: left;
}
.alts .opt { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.alts .d { font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; }
.alts .is-best .d { color: var(--accent-strong); font-weight: 700; }

/* Block */
.sheet-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; gap: 8px; }
.sheet-head h2 { font-size: 17px; margin: 0; }
.sheet-tools { display: flex; gap: 6px; }
.chip { font-size: 13px; padding: 8px 11px; border-radius: 999px; background: var(--bg); }
.chip:disabled { opacity: 0.4; }
.chip.on { background: var(--accent); color: #fff; }

.sheet { list-style: none; margin: 0; padding: 0; }
.row {
  display: grid;
  grid-template-columns: 1fr auto 3em;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 46px;
  padding: 0 8px;
  border-bottom: 1px solid var(--line);
  text-align: left;
}
.row .value { text-align: right; font-variant-numeric: tabular-nums; font-weight: 650; }
.row .delta { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.row.filled .label { color: var(--muted); }
.row .potential { color: var(--muted); font-weight: 500; }
.row .muted { color: var(--muted); font-weight: 400; }
button.row:active { background: var(--bg); }
button.row.best { background: var(--accent-weak); }
button.row.best .delta { color: var(--accent-strong); font-weight: 700; }
button.row.selected { box-shadow: inset 0 0 0 2px var(--accent); border-radius: 10px; }
.row.summary { background: var(--bg); font-size: 14px; border-radius: 10px; border-bottom: 0; margin: 4px 0; }
.row.summary .delta { font-size: 13px; }
.row.total { font-weight: 750; font-size: 17px; border-bottom: 0; }
.row select {
  grid-column: 2 / 4;
  justify-self: end;
  font: inherit;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  min-width: 5.5em;
}

.new-game {
  margin-top: 14px;
  width: 100%;
  padding: 12px;
  border-radius: 12px;
  color: var(--danger);
  background: var(--bg);
  font-weight: 650;
}
.new-game.armed { background: var(--danger); color: #fff; }

.footnote { color: var(--muted); font-size: 12px; text-align: center; margin: 4px 12px 16px; }

/* Dock unten (Daumenbereich): Tastenfeld beim Eintippen, sonst Aktionsbutton */
.actionbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  box-shadow: 0 -1px 0 var(--line);
}
.dock { max-width: calc(var(--page) - 24px); margin: 0 auto; }

.key {
  min-width: 0;
  border-radius: 14px;
  background: var(--card);
  box-shadow: inset 0 0 0 1px var(--line), 0 1px 2px rgb(0 0 0 / 0.08);
  display: grid;
  place-items: center;
  transition: transform 0.08s ease, background 0.08s ease;
}
.key:active:not(:disabled) { transform: scale(0.94); background: var(--accent-weak); }
.key:disabled { opacity: 0.35; }
.key svg { width: 44px; height: 44px; display: block; }
.key.back { font-size: 24px; color: var(--muted); }

.pad { display: grid; grid-template-columns: repeat(3, 1fr) 72px; grid-auto-rows: 60px; gap: 8px; }
.pad .back { grid-column: 4; grid-row: 1 / span 2; }

.action-row { display: flex; gap: 8px; }
.action-row .back { flex: 0 0 72px; }
.primary {
  flex: 1;
  min-width: 0;
  min-height: 58px;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 700;
  box-shadow: 0 2px 8px color-mix(in srgb, var(--accent) 35%, transparent);
}
.primary:active:not(:disabled) { background: var(--accent-strong); }
.primary:disabled { background: var(--line); color: var(--muted); box-shadow: none; }
/* Eigene Wahl ist schlechter als die Empfehlung */
.primary.costly { background: var(--warn); color: var(--on-warn); box-shadow: none; }
