/* Score-sheet look: warm paper in light mode, slate in dark mode, green accent. */
:root {
  --bg: #f4f1ea;
  --surface: #fffdf8;
  --surface-2: #efe9dc;
  --line: #d8cfbc;
  --line-strong: #b9ac92;
  --text: #23201a;
  --muted: #6f675a;
  --accent: #1b5e20;
  --accent-text: #ffffff;
  --good: #2e7d32;
  --bad: #c62828;
  --warn: #b26a00;
  --shadow: 0 2px 6px rgba(0, 0, 0, .10);
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171a;
    --surface: #1e2226;
    --surface-2: #262b30;
    --line: #363c42;
    --line-strong: #4d555c;
    --text: #e8e6e1;
    --muted: #9aa2a8;
    --accent: #4caf50;
    --accent-text: #0d1a0e;
    --good: #66bb6a;
    --bad: #ef5350;
    --warn: #ffb74d;
    --shadow: 0 2px 8px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* ---------- top bar ---------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(var(--safe-top) + 8px) 10px 8px;
  background: var(--accent);
  color: var(--accent-text);
  box-shadow: var(--shadow);
  position: relative;
  z-index: 5;
}
#topbar h1 {
  font-size: 19px;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.spacer { flex: 1; }
#topbar-actions { display: flex; gap: 2px; }

.icon-btn {
  background: none;
  border: 0;
  color: inherit;
  padding: 8px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.icon-btn:active { background: rgba(255, 255, 255, .18); }

#view {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
}

.page {
  padding: 14px 14px calc(var(--safe-bottom) + 14px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
}

/* ---------- generic bits ---------- */
.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius);
  padding: 13px 16px;
  cursor: pointer;
}
.btn:active { background: var(--surface-2); }
.btn.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: transparent;
}
.btn.danger { color: var(--bad); border-color: var(--bad); background: transparent; }
.btn.wide { width: 100%; }
.btn[disabled] { opacity: .45; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.hint { color: var(--muted); font-size: 13px; margin: 0; }
.section-title {
  font-size: 12px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 8px 2px 0;
}
.empty { color: var(--muted); text-align: center; padding: 28px 12px; }

.dot {
  width: 14px; height: 14px;
  border-radius: 999px;
  flex: none;
  display: inline-block;
}

/* ---------- home ---------- */
.home-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 18px 0 6px;
}
.home-hero .dice { font-size: 46px; line-height: 1; }
.home-hero .sub { color: var(--muted); font-size: 14px; }
.home-actions { display: flex; flex-direction: column; gap: 10px; }
.resume {
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 3px;
  border-color: var(--accent);
  border-width: 2px;
}
.resume .small { font-weight: 400; font-size: 13px; color: var(--muted); }

/* ---------- player list ---------- */
.player-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.player-row:last-child { border-bottom: 0; }
.player-row .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-row.selected { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.check {
  width: 24px; height: 24px;
  border: 2px solid var(--line-strong);
  border-radius: 6px;
  flex: none;
  display: grid;
  place-items: center;
  color: transparent;
}
.player-row.selected .check {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.row-btn {
  background: none; border: 0; color: var(--muted);
  padding: 7px; border-radius: 8px; cursor: pointer; font: inherit;
  display: inline-flex;
}
.row-btn:active { background: var(--surface-2); }
.row-btn[disabled] { opacity: .3; }

.sticky-footer {
  position: sticky;
  bottom: 0;
  padding-top: 10px;
  padding-bottom: calc(var(--safe-bottom) + 4px);
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}

/* ---------- game ---------- */
.turnbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.turnbar .who {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.turnbar .arrow {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  width: 36px; height: 36px;
  display: grid; place-items: center;
  color: var(--text);
  cursor: pointer;
}

.sheet-scroll {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 4px;
}

.sheet {
  display: grid;
  min-width: 100%;
  width: max-content;
  border-collapse: collapse;
}

.cell {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 0 6px;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  background: var(--surface);
  font-variant-numeric: tabular-nums;
}

.cell.label {
  justify-content: flex-start;
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--surface-2);
  font-size: 14px;
  font-weight: 600;
  border-right: 1px solid var(--line-strong);
}
.cell.label.done { color: var(--muted); font-weight: 500; }
.cell.head {
  position: sticky;
  top: 0;
  z-index: 3;
  font-weight: 700;
  color: #fff;
  min-height: 46px;
  cursor: pointer;
  flex-direction: column;
  gap: 0;
  line-height: 1.1;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .35);
}
.cell.head .tot { font-size: 12px; font-weight: 600; opacity: .95; }
.cell.corner { position: sticky; left: 0; top: 0; z-index: 4; background: var(--surface-2); }

.cell.score { cursor: pointer; }
.cell.score.empty-cell { color: var(--muted); }
.cell.score.filled { font-weight: 600; }
.cell.calc { background: var(--surface-2); font-weight: 600; color: var(--muted); }
.cell.grand { background: var(--surface-2); font-weight: 800; font-size: 17px; color: var(--text); border-top: 2px solid var(--line-strong); }
.cell.sep { border-top: 2px solid var(--line-strong); }
.cell.best { color: var(--good); }
.cell.worst { color: var(--bad); }
.cell.progress { color: var(--warn); }

/* active player column — `.act` is set per cell, columns aren't wrapped elements */
.cell.act { background: color-mix(in srgb, var(--accent) 9%, var(--surface)); }
.cell.act.calc, .cell.act.grand { background: color-mix(in srgb, var(--accent) 14%, var(--surface-2)); }
.cell.act.score.empty-cell {
  box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--accent) 45%, transparent);
  color: var(--accent);
}
.cell.act.head { box-shadow: inset 0 0 0 3px var(--text); }

.gamebar {
  display: flex;
  gap: 8px;
  padding: 8px 10px calc(var(--safe-bottom) + 8px);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.gamebar .btn { flex: 1; padding: 12px 8px; }
.badge-all-filled {
  background: color-mix(in srgb, var(--good) 18%, transparent);
  color: var(--good);
  font-size: 13px;
  font-weight: 600;
  padding: 6px 10px;
  text-align: center;
}

/* ---------- overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
  padding: 0;
}
@media (min-width: 560px) { .overlay { align-items: center; padding: 20px; } }

.modal {
  background: var(--surface);
  color: var(--text);
  width: 100%;
  max-width: 460px;
  max-height: 88vh;
  overflow: auto;
  border-radius: 18px 18px 0 0;
  padding: 16px 16px calc(var(--safe-bottom) + 16px);
  box-shadow: 0 -4px 24px rgba(0, 0, 0, .3);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
@media (min-width: 560px) { .modal { border-radius: 18px; padding-bottom: 16px; } }

.modal h2 { margin: 0; font-size: 18px; }
.modal p { margin: 0; color: var(--muted); font-size: 14px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.modal-actions .btn { padding: 11px 16px; }

.value-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 8px;
}
.value-btn {
  font: inherit;
  font-size: 18px;
  font-weight: 700;
  padding: 14px 4px;
  border-radius: 10px;
  border: 1px solid var(--line-strong);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.value-btn:active { background: var(--accent); color: var(--accent-text); }
.value-btn.current { border-color: var(--accent); border-width: 2px; color: var(--accent); }
.value-btn.zero { color: var(--bad); }

input[type="text"] {
  font: inherit;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--line-strong);
  background: var(--bg);
  color: var(--text);
  width: 100%;
}
label.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }

.swatches { display: grid; grid-template-columns: repeat(auto-fill, minmax(38px, 1fr)); gap: 8px; }
.swatch {
  aspect-ratio: 1;
  border-radius: 999px;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
}
.swatch.sel { border-color: var(--text); transform: scale(1.08); }

/* ---------- lists (scores/stats) ---------- */
.tabs { display: flex; gap: 6px; }
.tab {
  flex: 1;
  font: inherit;
  font-weight: 600;
  padding: 9px 6px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
}
.tab.on { background: var(--accent); color: var(--accent-text); border-color: transparent; }

.list-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
}
.list-row:last-child { border-bottom: 0; }
.list-row .rank { width: 26px; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.list-row .grow { flex: 1; min-width: 0; }
.list-row .score { font-weight: 700; font-variant-numeric: tabular-nums; }
.list-row .sub { font-size: 12px; color: var(--muted); }
.medal-1 .score { color: var(--good); }

.game-entry { display: flex; flex-wrap: wrap; gap: 6px 10px; font-size: 13px; color: var(--muted); }
.game-entry b { color: var(--text); }

.statbar { display: flex; height: 8px; border-radius: 999px; overflow: hidden; background: var(--surface-2); }
.statbar i { display: block; height: 100%; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 24px);
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
  z-index: 100;
  max-width: 88vw;
  text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
