:root {
  --bg: #f6f1e7;
  --paper: #fffdf8;
  --ink: #1e1c1a;
  --ink-2: #5c5651;
  --ink-3: #9a938b;
  --line: #e4dccd;
  --line-2: #d3c9b6;
  --accent: #c8322b;
  --accent-ink: #fff;
  --good: #2f8a4c;
  --pill: #ede6d8;
  --sheet-bg: #fbf8f1;
  --shadow: 0 8px 30px rgba(40, 30, 10, .18);
  --die-face: #fff;
  --die-dot: #1e1c1a;
  --p0: #c8322b; --p1: #2b6fc8; --p2: #2f8a4c; --p3: #c88a2b; --p4: #7d3fc1;
  --radius: 14px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17161a;
    --paper: #221f24;
    --ink: #f1ece4;
    --ink-2: #b8b0a6;
    --ink-3: #746d66;
    --line: #322e35;
    --line-2: #46414a;
    --accent: #e0483f;
    --good: #4fbf72;
    --pill: #2d2930;
    --sheet-bg: #26232a;
    --shadow: 0 8px 30px rgba(0, 0, 0, .5);
    --die-face: #f4efe6;
    --die-dot: #1e1c1a;
    --p1: #4d8ee6; --p2: #45a866; --p3: #dba043; --p4: #a066df;
  }
}
:root[data-theme="dark"] {
  --bg: #17161a;
  --paper: #221f24;
  --ink: #f1ece4;
  --ink-2: #b8b0a6;
  --ink-3: #746d66;
  --line: #322e35;
  --line-2: #46414a;
  --accent: #e0483f;
  --good: #4fbf72;
  --pill: #2d2930;
  --sheet-bg: #26232a;
  --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  --die-face: #f4efe6;
  --die-dot: #1e1c1a;
  --p1: #4d8ee6; --p2: #45a866; --p3: #dba043; --p4: #a066df;
}
:root[data-theme="dark"] { color-scheme: dark; }
:root[data-theme="light"] { color-scheme: light; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", Inter, "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; touch-action: manipulation; }
button:disabled { cursor: default; }
input { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.screen { min-height: 100dvh; display: flex; flex-direction: column; }

/* ---------- Dice (CSS) ---------- */
.die {
  --s: 44px;
  position: relative; display: inline-grid; width: var(--s); height: var(--s);
  grid-template: repeat(3, 1fr) / repeat(3, 1fr); padding: calc(var(--s) * .16);
  background: var(--die-face); border-radius: calc(var(--s) * .22);
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.18);
}
.die i { display: block; width: 100%; height: 100%; border-radius: 50%; background: var(--die-dot); transform: scale(.72); }
.die.d1 i:nth-child(1) { grid-area: 2/2; }
.die.d2 i:nth-child(1) { grid-area: 1/1; } .die.d2 i:nth-child(2) { grid-area: 3/3; }
.die.d3 i:nth-child(1) { grid-area: 1/1; } .die.d3 i:nth-child(2) { grid-area: 2/2; } .die.d3 i:nth-child(3) { grid-area: 3/3; }
.die.d4 i:nth-child(1) { grid-area: 1/1; } .die.d4 i:nth-child(2) { grid-area: 1/3; } .die.d4 i:nth-child(3) { grid-area: 3/1; } .die.d4 i:nth-child(4) { grid-area: 3/3; }
.die.d5 i:nth-child(1) { grid-area: 1/1; } .die.d5 i:nth-child(2) { grid-area: 1/3; } .die.d5 i:nth-child(3) { grid-area: 2/2; } .die.d5 i:nth-child(4) { grid-area: 3/1; } .die.d5 i:nth-child(5) { grid-area: 3/3; }
.die.d6 i:nth-child(1) { grid-area: 1/1; } .die.d6 i:nth-child(2) { grid-area: 1/3; } .die.d6 i:nth-child(3) { grid-area: 2/1; } .die.d6 i:nth-child(4) { grid-area: 2/3; } .die.d6 i:nth-child(5) { grid-area: 3/1; } .die.d6 i:nth-child(6) { grid-area: 3/3; }
.die.d0 i { opacity: 0; }
.die.spin { transform: rotate(-12deg); }

/* ---------- Setup ---------- */
#setup { padding: calc(var(--sat) + 24px) 20px calc(var(--sab) + 20px); max-width: 480px; margin: 0 auto; width: 100%; gap: 20px; position: relative; }
#setup .theme-btn { position: absolute; top: calc(var(--sat) + 8px); right: 10px; }
#start { margin-top: auto; }
.setup-head { display: flex; align-items: center; justify-content: center; gap: 16px; text-align: left; }
.logo .die { --s: 56px; transform: rotate(-8deg); }
.setup-head h1 { margin: 0; font-size: 30px; letter-spacing: -.02em; line-height: 1.1; }
.setup-head .sub { margin: 0; color: var(--ink-2); }

.resume {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px; text-align: left;
  background: var(--paper); border: 1px solid var(--line); border-left: 4px solid var(--accent);
  border-radius: var(--radius); padding: 12px 16px;
}
.resume-title { font-weight: 600; }
.resume-sub { color: var(--ink-2); font-size: 14px; }

.field { display: flex; flex-direction: column; gap: 8px; }
.field-label { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); }

.segmented { display: grid; grid-template-columns: repeat(5, 1fr); background: var(--pill); padding: 4px; border-radius: 12px; gap: 4px; }
.segmented button { height: 44px; border-radius: 9px; font-weight: 600; font-size: 18px; color: var(--ink-2); transition: background .12s, color .12s; }
.segmented button[aria-checked="true"] { background: var(--paper); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }

.names { display: flex; flex-direction: column; gap: 8px; }
.name-row { display: flex; align-items: center; gap: 10px; background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: 6px 6px 6px 8px; }
.name-row:focus-within { border-color: var(--line-2); }
.chip {
  flex: none; width: 36px; height: 36px; border-radius: 50%; display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: 13px; letter-spacing: .02em; background: var(--pc);
}
.name-row input { flex: 1; min-width: 0; border: 0; background: none; outline: none; height: 36px; font-size: 17px; padding: 0 4px; }
.name-row input::placeholder { color: var(--ink-3); }

.mode-cards { display: grid; grid-template-columns: 1fr; gap: 8px; }
.mode-cards button {
  display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto; column-gap: 12px; row-gap: 2px; align-items: center; text-align: left;
  background: var(--paper); border: 2px solid var(--line); border-radius: var(--radius); padding: 12px 12px;
  transition: border-color .12s, transform .1s;
}
.mode-cards button:active { transform: scale(.98); }
.mode-cards button[aria-checked="true"] { border-color: var(--accent); }
.mode-icon { grid-row: 1 / 3; display: grid; place-items: center; }
.mode-icon .die { --s: 34px; }
.mode-title { font-weight: 600; line-height: 1.2; }
.mode-sub { font-size: 13px; color: var(--ink-2); line-height: 1.25; }

.primary {
  background: var(--accent); color: var(--accent-ink); font-weight: 600; border-radius: 14px;
  height: 52px; padding: 0 20px; transition: transform .08s, filter .12s;
}
.primary:active { transform: scale(.98); filter: brightness(.94); }
.primary:disabled { opacity: .45; }
.primary.big { height: 56px; font-size: 18px; }
.ghost { height: 48px; border-radius: 12px; font-weight: 600; color: var(--ink); background: var(--pill); padding: 0 16px; }
.ghost:active { filter: brightness(.94); }
.ghost.danger { color: var(--accent); }
.ghost.muted, .menu-list .muted { color: var(--ink-2); }
.danger-bg { background: var(--accent); }

/* ---------- Game ---------- */
#game { height: 100dvh; min-height: auto; overflow: hidden; }
.topbar {
  flex: none; display: grid; grid-template-columns: 88px 1fr 88px; align-items: center;
  padding: calc(var(--sat) + 4px) 8px 4px; height: calc(var(--sat) + 52px);
}
.topbar-right { display: flex; justify-content: flex-end; }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 12px; color: var(--ink-2); }
.icon-btn:active { background: var(--pill); }
.icon-btn:disabled { opacity: .3; }
.turn { text-align: center; font-weight: 600; font-size: 16px; line-height: 1.2; }
.turn small { display: block; font-weight: 500; font-size: 12px; color: var(--ink-2); }
.turn .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--pc); margin-right: 6px; vertical-align: 1px; }

.sheet-wrap { flex: 1; min-height: 0; overflow: auto; padding: 2px 8px calc(var(--sab) + 8px); -webkit-overflow-scrolling: touch; }
.sheet {
  --row-h: clamp(26px, calc((100dvh - var(--reserved, 130px) - 22px) / 16), 46px);
  width: 100%; border-collapse: separate; border-spacing: 0; table-layout: fixed;
  background: var(--paper); border-radius: var(--radius); overflow: hidden;
  box-shadow: 0 1px 0 var(--line), 0 0 0 1px var(--line);
}
.sheet th, .sheet td { height: var(--row-h); padding: 0; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.sheet tr:last-child td { border-bottom: 0; }
.sheet thead th { position: sticky; top: 0; z-index: 2; background: var(--paper); height: calc(var(--row-h) + 4px); border-bottom: 1px solid var(--line-2); }
.sheet th.label { text-align: left; padding-left: 12px; font-weight: 500; font-size: clamp(12px, calc(var(--row-h) * .4), 15px); color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width: 34%; }
.sheet.p1 th.label { width: 55%; } .sheet.p2 th.label { width: 44%; } .sheet.p3 th.label { width: 38%; }
.sheet th.label small { color: var(--ink-3); font-size: .8em; margin-left: 4px; font-weight: 500; }

.ph { text-align: center; vertical-align: middle; }
.ph .chip { width: clamp(22px, calc(var(--row-h) * .62), 28px); height: clamp(22px, calc(var(--row-h) * .62), 28px); font-size: 11px; margin: 0 auto; transition: box-shadow .15s; }
.ph.active .chip { box-shadow: 0 0 0 2px var(--paper), 0 0 0 4px var(--pc); }

td.cell { text-align: center; font-weight: 600; font-size: clamp(14px, calc(var(--row-h) * .45), 18px); color: var(--ink); position: relative; z-index: 0; }
td.cell.open::before { content: ""; position: absolute; inset: 3px 2px; border-radius: 7px; background: var(--sheet-bg); box-shadow: inset 0 0 0 1px var(--line); z-index: -1; transition: box-shadow .12s, background .12s; }
td.cell.open.active::before { background: color-mix(in srgb, var(--pc) 5%, var(--paper)); box-shadow: inset 0 0 0 1.5px color-mix(in srgb, var(--pc) 45%, transparent); }
td.cell.open { color: var(--ink-3); font-weight: 500; }
td.cell.open.preview { color: var(--ink-2); }
td.cell.open.preview.hot { color: var(--pc); font-weight: 700; }
td.cell.open.preview.zero { color: var(--ink-3); font-weight: 400; }
td.cell.zero { color: var(--ink-3); font-weight: 500; }
td.cell.zero::after { content: "—"; }
td.cell.tapable:active::before { background: var(--pill); }
td.cell.just { animation: pop .28s ease-out; }
@keyframes pop { 0% { transform: scale(.7); opacity: .3; } 60% { transform: scale(1.12); } 100% { transform: scale(1); opacity: 1; } }

tr.section td, tr.section th { background: color-mix(in srgb, var(--pill) 55%, transparent); }
tr.section th.label { font-weight: 600; color: var(--ink-2); font-size: clamp(11px, calc(var(--row-h) * .36), 13px); }
tr.section td { font-weight: 700; font-size: clamp(13px, calc(var(--row-h) * .4), 16px); color: var(--ink-2); text-align: center; }
tr.section td .delta { font-weight: 600; font-size: .85em; color: var(--ink-3); }
tr.section td .delta.pos { color: var(--good); }
tr.section td .delta.neg { color: var(--accent); }
tr.section td .got { color: var(--good); }
tr.total td { font-size: clamp(14px, calc(var(--row-h) * .46), 18px); color: var(--ink); }
.bonusbar { position: absolute; left: 6px; right: 6px; bottom: 3px; height: 3px; background: var(--line); border-radius: 2px; overflow: hidden; }
.bonusbar i { display: block; height: 100%; width: 0; background: var(--pc); border-radius: 2px; transition: width .3s; }
tr.section td { position: relative; }

/* ---------- Dice bar ---------- */
.dice-bar {
  flex: none; display: flex; flex-direction: column; gap: 10px; padding: 10px 14px calc(var(--sab) + 12px);
  background: var(--paper); border-top: 1px solid var(--line);
}
.dice { display: flex; justify-content: center; gap: 10px; }
.dice .die { --s: clamp(48px, 14vw, 60px); transition: background .15s, box-shadow .15s; }
.dice .die.held { background: color-mix(in srgb, var(--good) 22%, var(--die-face)); box-shadow: inset 0 3px 6px rgba(0,0,0,.22), inset 0 0 0 2px var(--good); }
.dice .die.held::before { content: ""; position: absolute; top: -5px; right: -5px; width: 18px; height: 18px; border-radius: 50%; background: var(--good); box-shadow: 0 0 0 2px var(--paper); background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='3.5' stroke-linecap='round' stroke-linejoin='round'><path d='M5 12.5l4.5 4.5L19 7'/></svg>"); background-size: 12px; background-position: center; background-repeat: no-repeat; }
.dice .die.rolling { animation: tumble .6s cubic-bezier(.2, .7, .3, 1) both; }
.dice .die.rolling:nth-child(2) { animation-delay: .03s; } .dice .die.rolling:nth-child(3) { animation-delay: .06s; }
.dice .die.rolling:nth-child(4) { animation-delay: .02s; } .dice .die.rolling:nth-child(5) { animation-delay: .05s; }
@keyframes tumble {
  0% { transform: translateY(0) rotate(0) scale(1); }
  20% { transform: translateY(-22px) rotate(-70deg) scale(1.08); }
  45% { transform: translateY(-14px) rotate(-190deg) scale(1.06); }
  70% { transform: translateY(2px) rotate(-330deg) scale(.98); }
  85% { transform: translateY(-4px) rotate(-352deg) scale(1.01); }
  100% { transform: translateY(0) rotate(-360deg) scale(1); }
}
.dice .die::after { content: ""; position: absolute; inset: -8px; }
.dice { padding-top: 6px; }
.roll { height: 52px; display: flex; align-items: center; justify-content: center; gap: 8px; }
.roll .pips { display: inline-flex; gap: 4px; }
.roll .pips i { width: 8px; height: 8px; border-radius: 50%; background: currentColor; opacity: .35; display: block; }
.roll .pips i.on { opacity: 1; }

/* ---------- Overlays ---------- */
.overlay { position: fixed; inset: 0; z-index: 10; background: rgba(20, 15, 10, .45); display: flex; align-items: flex-end; justify-content: center; animation: fade .15s ease-out; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.bottom-sheet {
  width: 100%; max-width: 520px; background: var(--paper); border-radius: 22px 22px 0 0;
  padding: 14px 18px calc(var(--sab) + 18px); box-shadow: var(--shadow);
  animation: rise .22s cubic-bezier(.2, .8, .2, 1);
}
@keyframes rise { from { transform: translateY(40px); opacity: .6; } to { transform: none; opacity: 1; } }
.bs-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.bs-title { font-weight: 700; font-size: 20px; }
.bs-sub { color: var(--ink-2); font-size: 14px; }
.bs-hint { background: color-mix(in srgb, var(--accent) 10%, transparent); color: var(--accent); border-radius: 10px; padding: 8px 12px; font-size: 14px; font-weight: 500; margin-bottom: 12px; }
.close { width: 36px; height: 36px; font-size: 16px; }

.values { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; }
.values.five { grid-template-columns: repeat(5, 1fr); }
.values.two { grid-template-columns: 1fr; }
.values button {
  height: 50px; border-radius: 12px; background: var(--pill); font-weight: 700; font-size: 19px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.1;
  transition: transform .08s;
}
.values button small { font-size: 11px; font-weight: 500; color: var(--ink-3); }
.values button:active { transform: scale(.94); background: var(--line-2); }
.values button.big { height: 72px; font-size: 26px; background: var(--accent); color: #fff; }
.values button.big small { color: rgba(255,255,255,.75); }
.values button.current { outline: 2px solid var(--accent); outline-offset: -2px; }
.bs-actions { display: flex; gap: 8px; margin-top: 12px; }
.bs-actions button { flex: 1; }

.menu-list { display: flex; flex-direction: column; gap: 6px; }
.menu-list button { height: 52px; text-align: left; padding: 0 14px; border-radius: 12px; font-weight: 600; background: var(--pill); }
.menu-list button:active { filter: brightness(.94); }

.bottom-sheet.tall { max-height: 88dvh; display: flex; flex-direction: column; }
.rules-scroll { overflow-y: auto; min-height: 0; padding-right: 2px; -webkit-overflow-scrolling: touch; user-select: text; -webkit-user-select: text; }
.rules-h { margin: 14px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); }
.rules-h:first-child { margin-top: 0; }
.rules-steps { margin: 0; padding-left: 22px; font-size: 15px; color: var(--ink); display: flex; flex-direction: column; gap: 4px; }
.rules-steps li::marker { color: var(--accent); font-weight: 700; }
.rules-note { margin: 10px 0 0; font-size: 14px; color: var(--ink-2); }
.rules { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 8px 14px; font-size: 14px; }
.rules dt { font-weight: 600; white-space: nowrap; }
.rules dd { margin: 0; color: var(--ink-2); }

.overlay:has(.dialog) { align-items: center; padding: 20px; }
.dialog { width: 100%; max-width: 380px; background: var(--paper); border-radius: 20px; padding: 22px 20px; box-shadow: var(--shadow); animation: rise .22s cubic-bezier(.2, .8, .2, 1); position: relative; }
.dialog-text { font-size: 17px; font-weight: 500; margin-bottom: 18px; }
.dialog-actions { display: flex; gap: 8px; }
.dialog-actions button { flex: 1; }
.dialog-actions.column { flex-direction: column; }

#confetti { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.result { text-align: center; }
.result-kicker { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-3); font-weight: 600; }
.result h2 { margin: 4px 0 16px; font-size: 26px; letter-spacing: -.02em; }
.ranking { list-style: none; margin: 0 0 18px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.ranking li { display: flex; align-items: center; gap: 10px; background: var(--pill); border-radius: 12px; padding: 8px 12px; text-align: left; }
.ranking li .place { width: 22px; font-weight: 700; color: var(--ink-3); }
.ranking li .name { flex: 1; font-weight: 600; }
.ranking li .pts { font-weight: 700; font-variant-numeric: tabular-nums; }
.ranking li.first { background: color-mix(in srgb, var(--pc) 14%, var(--pill)); }
.ranking li .chip { width: 30px; height: 30px; font-size: 12px; }

@media (min-width: 640px) {
  .sheet-wrap { padding: 0 16px calc(var(--sab) + 16px); }
  .sheet, .dice-bar > * { max-width: 640px; margin: 0 auto; }
  .dice-bar { align-items: center; }
  .dice-bar .roll { width: 100%; }
  .sheet { --row-h: clamp(30px, calc((100dvh - var(--reserved, 130px) - 22px) / 16), 50px); }
}
@media (hover: hover) {
  td.cell.tapable:hover::before { background: var(--pill); }
  .values button:hover { filter: brightness(.96); }
}

/* Überzählige Punkte je Augenzahl ausblenden */
.die.d1 i:nth-child(n+2), .die.d2 i:nth-child(n+3), .die.d3 i:nth-child(n+4),
.die.d4 i:nth-child(n+5), .die.d5 i:nth-child(n+6) { display: none; }

/* ---------- Ziffernblock ---------- */
.pad { display: flex; flex-direction: column; gap: 10px; }
.pad-display { display: flex; align-items: baseline; justify-content: space-between; background: var(--pill); border-radius: 12px; padding: 10px 16px; font-size: 32px; font-weight: 700; font-variant-numeric: tabular-nums; min-height: 60px; }
.pad-display.invalid { color: var(--ink-3); }
.pad-range { font-size: 13px; font-weight: 500; color: var(--ink-3); }
.pad-keys { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.pad-keys button { height: 52px; border-radius: 12px; background: var(--pill); font-weight: 600; font-size: 22px; transition: transform .08s; }
.pad-keys button:active { transform: scale(.95); background: var(--line-2); }
.pad-keys .key-fn { font-size: 20px; color: var(--ink-2); }
.pad-keys .key-ok { background: var(--accent); color: #fff; font-size: 18px; }
.pad-keys .key-ok:disabled { opacity: .35; }

/* ---------- Über / SEO-Inhalt ---------- */
.about { margin-top: 28px; color: var(--ink-2); font-size: 15px; user-select: text; -webkit-user-select: text; }
.about h2 { margin: 0 0 8px; font-size: 20px; color: var(--ink); letter-spacing: -.01em; }
.about details p { margin: 0 0 12px; }
.about ul { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 4px; }
.about details { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 0 16px; }
.about summary { cursor: pointer; font-weight: 600; color: var(--ink); padding: 14px 0; list-style: none; display: flex; justify-content: space-between; align-items: center; }
.about summary::-webkit-details-marker { display: none; }
.about summary::after { content: "+"; font-size: 20px; color: var(--ink-3); }
.about details[open] summary::after { content: "–"; }
.about details[open] { padding-bottom: 16px; }
.about details .rules-h:first-of-type { margin-top: 0; }
.about .rules-note { margin-top: 10px; }

/* ---------- Theme-Umschalter ---------- */
.theme-btn .sun, .theme-btn .moon { display: none; }
:root[data-theme="dark"] .theme-btn .sun { display: block; }
:root[data-theme="light"] .theme-btn .moon { display: block; }
:root:not([data-theme]) .theme-btn .moon { display: block; }
@media (prefers-color-scheme: dark) { :root:not([data-theme]) .theme-btn .moon { display: none; } :root:not([data-theme]) .theme-btn .sun { display: block; } }

