:root {
  color-scheme: dark;
  --bg: #120507;
  --bg-soft: #28090f;
  --panel: rgba(36, 10, 15, 0.88);
  --panel-strong: rgba(19, 4, 8, 0.94);
  --line: rgba(255, 255, 255, 0.13);
  --text: #fff8f0;
  --muted: #d0bfc0;
  --accent: #d6b25e;
  --accent-strong: #f2ce76;
  --felt: #3a0711;
  --felt-deep: #170105;
  --success: #88d498;
  --danger: #e07a7a;
  --shadow: 0 18px 42px rgba(0, 0, 0, 0.34);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f3f5f4;
  --bg-soft: #e7ece9;
  --panel: rgba(255, 255, 255, 0.92);
  --panel-strong: rgba(255, 255, 255, 0.98);
  --line: rgba(19, 69, 39, 0.18);
  --text: #14261a;
  --muted: #52675a;
  --accent: #b38b34;
  --accent-strong: #8a661e;
  --felt: #257342;
  --felt-deep: #124b2a;
  --success: #1c7c43;
  --danger: #a83636;
  --shadow: 0 16px 36px rgba(21, 52, 33, 0.14);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  font-family: var(--sd-font-sans, Inter, system-ui, sans-serif);
  color: var(--text);
  background:
    radial-gradient(circle at 15% 0%, color-mix(in srgb, var(--bg-soft) 78%, transparent), transparent 42%),
    var(--bg);
}

body.is-rolling {
  overflow: hidden;
}

body.is-rolling .game-shell {
  filter: blur(5px);
  transform: scale(.995);
  pointer-events: none;
  user-select: none;
}

button, select { font: inherit; }
button { cursor: pointer; }

.game-shell {
  width: min(1180px, calc(100vw - 20px));
  margin: 0 auto;
  padding: 14px 0 84px;
}

.game-header,
.play-panel,
.score-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.game-header {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px 16px;
  align-items: center;
  justify-items: center;
  padding: 12px 14px;
  text-align: center;
}

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.45rem, 5vw, 2rem); letter-spacing: 0; }
h2 { margin-bottom: 10px; font-size: 1.1rem; letter-spacing: 0; }

.game-banner {
  display: block;
  width: min(100%, 320px);
  max-height: 96px;
  object-fit: cover;
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(0,0,0,.22);
  margin-inline: auto;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}

.icon-button,
.text-button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  background: color-mix(in srgb, var(--panel-strong) 84%, transparent);
}

.icon-button { width: 40px; padding: 0; }
.text-button { padding: 0 14px; font-weight: 800; }

.auth-status {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 0.86rem;
  max-width: 760px;
}

.game-layout {
  display: grid;
  gap: 10px;
  margin-top: 10px;
}

.play-panel {
  min-height: 340px;
  padding: 14px;
  background:
    radial-gradient(circle at 20% 0%, color-mix(in srgb, var(--felt) 70%, transparent), transparent 48%),
    linear-gradient(145deg, var(--felt), var(--felt-deep));
}

.round-line,
.settings-row,
.score-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.round-line { color: #fff; }
.round-line span { color: rgba(255,255,255,.78); }

.dice-tray {
  display: grid;
  grid-template-columns: repeat(5, minmax(48px, 1fr));
  gap: clamp(5px, 2vw, 12px);
  margin: 30px auto 22px;
  max-width: 620px;
}

.die {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: #171717;
  box-shadow: none;
  padding: 0;
  transition: transform .16s ease, outline-color .16s ease, opacity .16s ease;
}

.die:hover { transform: translateY(-2px); }
.die.held {
  outline: 4px solid var(--accent-strong);
  transform: translateY(-8px);
}
.die.held::after {
  content: "Pfote";
  position: absolute;
  left: 50%;
  bottom: -24px;
  transform: translateX(-50%);
  color: #fff;
  font-size: 0.68rem;
  font-weight: 800;
}
.die.empty { opacity: .48; }
.die.rolling { animation: dice-roll .55s ease; }

.die-image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  filter: drop-shadow(0 8px 12px rgba(0,0,0,.22));
}

.message {
  min-height: 24px;
  margin-bottom: 12px;
  color: rgba(255,255,255,.84);
  text-align: center;
}

.roll-button {
  width: 100%;
  min-height: 52px;
  border: 1px solid color-mix(in srgb, var(--accent) 76%, #fff);
  border-radius: 6px;
  color: #1f1608;
  background: linear-gradient(180deg, var(--accent-strong), var(--accent));
  font-weight: 950;
  box-shadow: 0 8px 18px rgba(0,0,0,.22);
}
.roll-button:disabled { cursor: not-allowed; opacity: .5; }

.settings-row { margin-top: 16px; color: #fff; }
.settings-row label { display: grid; gap: 4px; font-size: .82rem; }
.settings-row select {
  min-height: 38px;
  border: 1px solid rgba(255,255,255,.2);
  border-radius: 6px;
  color: #fff;
  background: rgba(0,0,0,.28);
}
.total-badge { font-weight: 900; }

.score-panel { padding: 12px; }
.score-heading span { color: var(--muted); font-size: .78rem; text-align: right; }
.score-table { display: grid; gap: 3px; }
.score-section {
  margin-top: 9px;
  padding: 5px 8px;
  color: var(--muted);
  font-size: .76rem;
  font-weight: 900;
  text-transform: uppercase;
}
.score-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  width: 100%;
  min-height: 42px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 5px;
  color: var(--text);
  background: color-mix(in srgb, var(--panel-strong) 82%, transparent);
  text-align: left;
}
.score-row.free:hover,
.score-row.preview { border-color: color-mix(in srgb, var(--accent) 70%, var(--line)); }
.score-row.preview .score-value { color: var(--accent-strong); }
.score-row.filled { cursor: default; opacity: .72; }
.score-row.total { font-weight: 900; }
.score-value { min-width: 34px; text-align: right; font-weight: 900; }
.score-help { display: block; color: var(--muted); font-size: .72rem; font-weight: 400; }

dialog {
  width: min(520px, calc(100vw - 24px));
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: var(--panel-strong);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0,0,0,.58); }
.dialog-content { position: relative; padding: 16px; }
.dialog-close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  background: transparent;
}
.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; }
.stat-item { padding: 9px; border: 1px solid var(--line); border-radius: 5px; }
.stat-item strong { display: block; font-size: 1.18rem; }
.highscore-list { width: 100%; border-collapse: collapse; }
.highscore-list th, .highscore-list td { padding: 7px 5px; border-bottom: 1px solid var(--line); text-align: left; }
.highscore-list td:last-child, .highscore-list th:last-child { text-align: right; }

.roll-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  gap: 12px;
  padding: 24px;
  background: rgba(3, 3, 4, .22);
  backdrop-filter: blur(3px);
}

.roll-overlay[hidden] {
  display: none;
}

.roll-overlay img {
  width: min(62vw, 360px);
  max-height: 48vh;
  object-fit: contain;
  filter: drop-shadow(0 22px 30px rgba(0,0,0,.44));
  animation: cup-shake .42s linear infinite;
  transform-origin: 50% 82%;
  user-select: none;
}

.roll-overlay span {
  display: inline-flex;
  align-items: center;
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid rgba(255,255,255,.2);
  border-radius: 999px;
  color: #fff7e8;
  background: rgba(0,0,0,.36);
  font-weight: 900;
  letter-spacing: 0;
  box-shadow: 0 10px 24px rgba(0,0,0,.24);
}

@keyframes dice-roll {
  0% { transform: rotate(0) scale(1); }
  35% { transform: rotate(14deg) scale(.86); }
  70% { transform: rotate(-12deg) scale(1.08); }
  100% { transform: rotate(0) scale(1); }
}

@keyframes cup-shake {
  0%, 100% { transform: translate3d(0, 0, 0) rotate(0deg); }
  12% { transform: translate3d(-10px, 2px, 0) rotate(-6deg); }
  24% { transform: translate3d(9px, -3px, 0) rotate(5deg); }
  36% { transform: translate3d(-7px, 4px, 0) rotate(-4deg); }
  48% { transform: translate3d(8px, 1px, 0) rotate(5deg); }
  60% { transform: translate3d(-5px, -2px, 0) rotate(-3deg); }
  72% { transform: translate3d(6px, 3px, 0) rotate(4deg); }
  84% { transform: translate3d(-3px, 1px, 0) rotate(-2deg); }
}

@media (min-width: 850px) {
  .game-layout { grid-template-columns: minmax(0, 1fr) minmax(390px, .82fr); align-items: start; }
  .play-panel { position: sticky; top: 10px; min-height: 520px; display: flex; flex-direction: column; justify-content: center; }
  .dice-tray { width: 100%; }
}

@media (max-width: 520px) {
  .game-header { grid-template-columns: 1fr; }
  .header-actions { justify-content: flex-start; }
  .auth-status { grid-column: auto; }
  .game-shell { width: min(100% - 12px, 1180px); padding-top: 6px; }
  .dice-tray { margin-top: 24px; gap: 4px; }
  .die { border-radius: 7px; }
  .score-row { min-height: 46px; }
}
