:root {
  color-scheme: dark light;
  --bg: #12141a;
  --card: #1c202b;
  --fg: #e8eaf0;
  --muted: #8a91a3;
  --border: #2a3040;
  --accent: #ffb02e;
  --accent-fg: #1a1205;
  --tray: #1d3325;
  --die: #f6f2e9;
  --pip: #191919;
}
:root[data-theme="light"] { color-scheme: light; --bg: #eef0f4; --card: #fff; --fg: #171a22; --muted: #5c6373; --border: #d3d8e2; --tray: #dcefe2; --die: #fffdf8; }
:root[data-theme="dark"] { color-scheme: dark; }
@media (prefers-color-scheme: light) {
  :root[data-theme="auto"] { --bg: #eef0f4; --card: #fff; --fg: #171a22; --muted: #5c6373; --border: #d3d8e2; --tray: #dcefe2; --die: #fffdf8; }
}
* { box-sizing: border-box; }
/* Kein Doppeltipp-Zoom beim Bedienen; Scrollen und Pinch-Zoom bleiben möglich. */
html, body, dialog { touch-action: manipulation; }
/* iOS-PWA-Vollbild wie in tabubu: height: 100% hebelt viewport-fit=cover aus, und in der
   installierten App sind 100vh/100dvh zu kurz. --app-height misst app.js (fitViewport). */
html { height: var(--app-height, 100vh); }
html, body { margin: 0; }
body {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--app-height, 100vh); overflow: hidden;
  background: var(--bg); color: var(--fg);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
main {
  position: relative;
  display: flex; flex-direction: column;
  height: 100%; max-width: 32rem; margin: 0 auto;
  padding: max(1rem, env(safe-area-inset-top)) 1rem max(1rem, env(safe-area-inset-bottom));
}
/* Kopfzeile: Schnellwahl links, Einstellungen rechts – beide als große Tippflächen. */
header { display: flex; justify-content: space-between; align-items: center; gap: .6rem; }
header button {
  display: flex; align-items: center; gap: .5rem;
  min-height: 3.5rem; padding: 0 1.1rem;
  font: inherit; font-size: 1.05rem; font-weight: 700; color: var(--fg);
  background: var(--card); border: 1px solid var(--border); border-radius: 1rem; cursor: pointer;
}
header button:active { transform: scale(.97); }
#open { width: 3.5rem; padding: 0; justify-content: center; }
#open svg { width: 1.9rem; height: 1.9rem; }

/* Tisch: 3 große Würfel pro Reihe */
#dice {
  flex: 1; min-height: 0; overflow: hidden;
  display: flex; flex-wrap: wrap; justify-content: center; align-content: center;
  gap: 1rem; padding: .8rem .2rem .5rem;
  border: 2px dashed transparent; border-radius: 1rem;
}
#dice .die { width: calc((100% - 2rem) / 3); }
#dice .ghost { visibility: hidden; }

/* Ablage: kleine Würfel */
.trays { display: flex; gap: .5rem; }
.tray {
  flex: 1; min-width: 0;
  display: flex; flex-wrap: wrap; align-content: flex-start; gap: .55rem;
  min-height: 5.2rem; padding: .7rem; margin: .8rem 0 .4rem;
  background: var(--tray); border: 2px dashed #3f9d58; border-radius: 1rem;
}
#toptrays .tray { margin: .6rem 0 .6rem; }
.tray[hidden] { display: none; }
.tray:empty { align-content: center; }
.tray:empty::before { content: "Ablage"; margin: auto; color: #3f9d58; font-size: .9rem; }
.tray .die { width: 3.6rem; }
/* Einfügelücke beim Ziehen: ein leerer Würfelplatz an der Stelle, an der der Würfel landet. */
.tray .ins {
  flex: none; width: 3.6rem; aspect-ratio: 1; border-radius: 18%;
  border: 2px dashed var(--accent); background: color-mix(in srgb, var(--accent) 18%, transparent);
}
#dice.over, .tray.over { border-color: var(--accent); }

.die {
  position: relative; aspect-ratio: 1;
  display: grid; place-items: center;
  background: var(--die); color: var(--pip);
  border-radius: 18%;
  box-shadow: 0 4px 0 #0004, inset 0 -3px 0 #0001;
  cursor: grab; user-select: none; -webkit-user-select: none; touch-action: none;
}
/* Feste Setfarben; gilt auch für Schnellwahl, Einstellungen und Ablagen. */
.die[data-color="blue"] { --die: #2f61df; --pip: #fff; }
.die[data-color="yellow"] { --die: #ffd94a; --pip: #1f2430; }
.die[data-color="green"] { --die: #26d17c; --pip: #082312; }
.die[data-color="orange"] { --die: #ff9d3b; --pip: #2b1200; }
.die[data-color="purple"] { --die: #8457f7; --pip: #fff; }
.die[data-color="white"] { --die: #fff; --pip: #000; }
.die:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.die .num { font-size: 2.7rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.die small { position: absolute; bottom: 5%; font-size: .7rem; opacity: .55; }
.tray .die .num { font-size: 1.35rem; }
.tray .die small { font-size: .5rem; bottom: 2%; }
.pips {
  display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  width: 100%; height: 100%; aspect-ratio: 1; padding: 13%;
}
.pips i { margin: 13%; border-radius: 50%; }
.pips i.on { background: currentColor; }
/* Klappern beim Handy-Schütteln; versetzte Phasen, damit nicht alle im Gleichtakt wackeln. */
.die.rattle { animation: rattle .09s infinite alternate ease-in-out; }
.die.rattle:nth-child(2n) { animation-duration: .075s; animation-direction: alternate-reverse; }
.die.rattle:nth-child(3n) { animation-duration: .105s; }
@keyframes rattle {
  from { transform: translate(-3px, 2px) rotate(-7deg); }
  to { transform: translate(3px, -2px) rotate(7deg); }
}
.rolling.classic #dice .die:not(.ghost) { animation: wob .12s infinite alternate; }
@keyframes wob {
  from { transform: rotate(-9deg) scale(.96); }
  to { transform: rotate(9deg) scale(1.02); }
}
@media (prefers-reduced-motion: reduce) { .die.rattle { animation: none; } }
/* Anheben beim Drauftippen; beim Ziehen folgt der Würfel dem Finger ohne Verzögerung. */
.die.held {
  z-index: 1;
  transform: scale(1.08) translateY(-2px); box-shadow: 0 14px 30px #000a;
  transition: transform .12s ease-out, box-shadow .12s ease-out;
}
.die.drag { box-shadow: 0 14px 30px #000a; cursor: grabbing; transition: none; }

#total { min-height: 1.6em; text-align: right; padding-right: .7rem; color: var(--muted); font-variant-numeric: tabular-nums; }
#actions { display: flex; gap: .7rem; margin-top: .3rem; }
#actions button {
  min-height: 4rem; padding: 1rem 1.1rem;
  font: inherit; font-size: 1.25rem; font-weight: 700;
  border-radius: 1rem; cursor: pointer;
}
#reset { flex: none; font-size: 1rem; background: var(--card); color: var(--fg); border: 2px solid var(--border); }
#roll { flex: 1; background: var(--accent); color: var(--accent-fg); border: 0; }
#roll:disabled { opacity: .7; }
/* Während des Wurfs: Knopf bleibt kräftig (er ist der Becher), die Summe verrät nichts vorab. */
.rolling #roll:disabled { opacity: 1; }
.rolling #total { visibility: hidden; }
#actions button:active { transform: scale(.98); }

/* Einstellungen */
dialog {
  width: min(32rem, calc(100% - 1rem)); max-height: 92dvh; padding: 0;
  background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); border-radius: 1.2rem;
  overscroll-behavior: contain;
}
dialog::backdrop { background: #000a; }
dialog > div { padding: 0 1rem calc(1.2rem + env(safe-area-inset-bottom)); }
dialog .head {
  position: sticky; top: 0; z-index: 1;
  display: flex; justify-content: space-between; align-items: center;
  padding: .8rem 0; background: var(--bg);
}
dialog h2 { margin: 0; font-size: 1.3rem; }
dialog h3 { font-size: 1rem; margin: 1.2rem 0 .55rem; display: flex; gap: .5rem; align-items: baseline; }
dialog h3 small { color: var(--muted); font-weight: 400; font-size: .85rem; }
/* Alle Bedienelemente mind. 48 px hoch; Inputs ≥16 px Schrift (sonst zoomt iOS). */
dialog button, dialog input {
  min-height: 3rem; font: inherit; font-size: 1.05rem; color: var(--fg);
  background: var(--card); border: 1px solid var(--border); border-radius: .8rem;
  padding: .5rem .6rem; cursor: pointer; white-space: nowrap;
}
dialog button:active { transform: scale(.97); }
dialog button { display: flex; align-items: center; justify-content: center; gap: .35rem; }
/* Würfel-Icons als Maske: Farbe folgt dem Text (Dark/Light). */
.ico { flex: none; width: 1.4rem; height: 1.4rem; background: currentColor; -webkit-mask: center / contain no-repeat; mask: center / contain no-repeat; }
.ico.d6 { -webkit-mask-image: url(icons/d6.svg); mask-image: url(icons/d6.svg); }
.ico.dx { -webkit-mask-image: url(icons/dx.svg); mask-image: url(icons/dx.svg); }
.head button { width: 3rem; padding: 0; font-size: 1.3rem; }
.grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: .6rem; }
#animation { grid-template-columns: repeat(2, 1fr); }
#list:empty::before { content: "keine – unten Würfel hinzufügen"; color: var(--muted); }
.grid button[aria-pressed="true"] { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); font-weight: 700; }

/* Würfel in den Dialogen: gleiche Optik wie auf dem Tisch, nur kleiner. */
dialog button.die { min-height: 0; padding: 0; border: 0; background: var(--die); color: var(--pip); cursor: pointer; touch-action: manipulation; }
.dicegrid { display: flex; flex-wrap: wrap; gap: .7rem; }
.dicegrid .die { width: 3.4rem; }
.dicegrid .die .num { font-size: 1.35rem; }
.dicegrid .die small, .minis .die small { font-size: .5rem; bottom: 3%; }
#list .die::after, #addrow .die::after {
  position: absolute; top: -.45rem; right: -.45rem;
  display: grid; place-items: center; width: 1.35rem; height: 1.35rem;
  font-size: .75rem; font-weight: 700; border-radius: 50%;
  background: var(--card); color: var(--fg); border: 1px solid var(--border);
}
#list .die::after { content: "✕"; }
#addrow .die::after { content: "+"; }

/* Schnellwahl: je Vorlage eine Würfelreihe plus Name. */
#sets { display: grid; gap: .7rem; padding-bottom: .6rem; }
dialog .preset { flex-direction: column; align-items: flex-start; gap: .55rem; width: 100%; padding: .85rem; font-weight: 700; }
.minis { display: flex; flex-wrap: wrap; gap: .45rem; }
.minis .die { width: 2.6rem; box-shadow: 0 3px 0 #0004; cursor: inherit; touch-action: manipulation; }
.minis .die .num { font-size: 1.05rem; }
.line { display: flex; gap: .6rem; align-items: center; margin-top: .6rem; }
.line label { color: var(--muted); }
.line input { flex: 1; min-width: 0; text-align: center; cursor: text; }
#customrow button { flex: 1.4; }
#modminus, #modplus { width: 3.5rem; font-size: 1.5rem; padding: 0; }
.wide { width: 100%; margin-top: .6rem; }
#history { margin: 0; padding-left: 1.6rem; max-height: 10rem; overflow-y: auto; color: var(--muted); }
#history:empty { display: none; }
.hint { color: var(--muted); font-size: .85rem; margin: 1.2rem 0 0; }
