:root {
  --bg: #0f172a;
  --bg-2: #1e293b;
  --card: #1e293b;
  --line: #334155;
  --line-soft: #26344a;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #22c55e;
  --danger: #f87171;
  --gold: #fbbf24;
  --sticky-bg: #172033;
  --total-bg: #14233b;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  overscroll-behavior-y: none;
}

#app { max-width: 720px; margin: 0 auto; padding: 12px 12px calc(24px + var(--safe-b)); }

.hidden { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 2px 14px;
}
.topbar h1 { font-size: 20px; margin: 0; flex: 1; letter-spacing: .2px; }
.topbar .sub { color: var(--muted); font-size: 13px; }
.back-btn {
  background: var(--card); border: 1px solid var(--line);
  color: var(--text); border-radius: 10px; padding: 8px 12px;
  font-size: 15px; cursor: pointer;
}

/* ---------- buttons ---------- */
.btn {
  border: none; border-radius: 12px; padding: 14px 16px;
  font-size: 16px; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn.primary { background: var(--accent); color: #04233a; }
.btn.success { background: var(--accent-2); color: #052e13; }
.btn.ghost { background: transparent; border: 1px solid var(--line); color: var(--text); }
.btn.danger { background: transparent; border: 1px solid var(--danger); color: var(--danger); }
.btn.block { width: 100%; }
.btn:active { transform: scale(.98); }

.icon-btn {
  background: transparent; border: none; color: var(--muted);
  font-size: 20px; cursor: pointer; padding: 4px 8px;
}

/* ---------- cards ---------- */
.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px; margin-bottom: 14px;
}
.card h2 { margin: 0 0 12px; font-size: 17px; }
.section-title { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: .6px; margin: 18px 4px 8px; }

/* ---------- inputs ---------- */
.text-input, .custom-input {
  width: 100%; background: var(--bg); border: 1px solid var(--line);
  color: var(--text); border-radius: 10px; padding: 13px 14px; font-size: 16px;
  margin-bottom: 10px;
}
.text-input:focus, .custom-input:focus { outline: none; border-color: var(--accent); }

.player-row { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.player-row .idx {
  width: 30px; height: 30px; flex: none; border-radius: 8px;
  background: var(--bg-2); display: grid; place-items: center; color: var(--muted); font-size: 14px;
}
.player-row .text-input { margin-bottom: 0; }
.player-row .rm { color: var(--danger); background: transparent; border: none; font-size: 22px; cursor: pointer; padding: 0 6px; }

.toggle {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 12px 0;
}
.toggle .label { font-size: 15px; }
.toggle .label small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
.switch { position: relative; width: 52px; height: 30px; flex: none; }
.switch input { display: none; }
.switch .track {
  position: absolute; inset: 0; background: var(--line);
  border-radius: 999px; transition: background .15s;
}
.switch .thumb {
  position: absolute; top: 3px; left: 3px; width: 24px; height: 24px;
  background: #fff; border-radius: 50%; transition: transform .15s;
}
.switch input:checked + .track { background: var(--accent-2); }
.switch input:checked + .track + .thumb { transform: translateX(22px); }

/* ---------- game list ---------- */
.game-item {
  display: flex; align-items: center; gap: 12px; padding: 14px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  margin-bottom: 10px; cursor: pointer;
}
.game-item:active { border-color: var(--accent); }
.game-item .gi-main { flex: 1; min-width: 0; }
.game-item .gi-title { font-weight: 600; font-size: 15px; }
.game-item .gi-sub { color: var(--muted); font-size: 13px; margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { font-size: 11px; padding: 3px 8px; border-radius: 999px; white-space: nowrap; }
.badge.active { background: #0c4a6e; color: #7dd3fc; }
.badge.finished { background: #14532d; color: #86efac; }
.gi-del {
  flex: none; background: transparent; border: none; cursor: pointer;
  font-size: 18px; padding: 8px; border-radius: 8px; opacity: .7;
}
.gi-del:active { background: #3a1414; opacity: 1; }

.empty { color: var(--muted); text-align: center; padding: 30px 10px; }

/* ---------- score table ---------- */
.sheet-controls {
  display: flex; align-items: center; gap: 10px; margin: 4px 2px 12px;
  flex-wrap: wrap;
}
.table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--line); -webkit-overflow-scrolling: touch; }
table.score { border-collapse: collapse; width: 100%; font-size: 15px; }
table.score th, table.score td {
  border-bottom: 1px solid var(--line-soft);
  border-right: 1px solid var(--line-soft);
  padding: 0; text-align: center;
}
table.score thead th {
  position: sticky; top: 0; z-index: 3;
  background: var(--sticky-bg); padding: 10px 6px; font-size: 13px;
  min-width: 62px; max-width: 96px;
}
table.score thead th.cat-col { z-index: 5; }
.cat-col {
  position: sticky; left: 0; z-index: 2;
  background: var(--sticky-bg);
  text-align: left; padding: 10px 10px; min-width: 118px; max-width: 150px;
  font-weight: 500; font-size: 13px;
}
tbody tr td.cat-col { color: var(--text); }
.digit-badge { color: var(--muted); font-weight: 700; margin-right: 4px; }

td.cell {
  cursor: pointer; height: 46px; min-width: 62px;
  font-variant-numeric: tabular-nums; font-weight: 600;
  position: relative;
}
td.cell:active { background: #24344d; }
td.cell.empty-cell { color: #3f5170; font-weight: 400; }
td.cell .cmt-dot {
  position: absolute; top: 4px; right: 5px; width: 6px; height: 6px;
  border-radius: 50%; background: var(--gold);
}
td.cell.crossed { color: var(--danger); }

tr.total-row td { background: var(--total-bg); font-weight: 700; }
tr.total-row .cat-col { background: var(--total-bg); }
tr.grand-row td { background: #0c2a1e; color: var(--accent-2); font-weight: 800; font-size: 16px; }
tr.grand-row .cat-col { background: #0c2a1e; color: var(--accent-2); }
tr.leader td.cell.leader-cell { box-shadow: inset 0 0 0 2px var(--gold); border-radius: 4px; }

.section-sep td {
  background: #16233a; color: var(--muted); text-align: left;
  padding: 6px 10px; font-size: 12px; text-transform: uppercase; letter-spacing: .6px;
  position: sticky; left: 0;
}

/* ---------- turn tracking ---------- */
.turn-banner {
  display: flex; align-items: center; gap: 8px;
  background: #0c2a3a; border: 1px solid #0e4a63;
  border-radius: 10px; padding: 9px 14px; margin: 0 2px 12px;
  font-size: 14px;
}
.turn-banner .tb-dot {
  width: 9px; height: 9px; border-radius: 50%; background: var(--accent);
  box-shadow: 0 0 0 0 rgba(56,189,248,.6); animation: pulse 1.6s infinite;
}
@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 rgba(56,189,248,.5); }
  70%  { box-shadow: 0 0 0 8px rgba(56,189,248,0); }
  100% { box-shadow: 0 0 0 0 rgba(56,189,248,0); }
}
.turn-banner .tb-label { color: var(--muted); }
.turn-banner .tb-name { font-weight: 700; color: var(--accent); }

table.score thead th .pname { display: block; }
table.score thead th.current-turn {
  background: #0c3a52; color: var(--accent); box-shadow: inset 0 -3px 0 var(--accent);
}
table.score thead th.current-turn .turn-tag {
  display: block; font-size: 10px; font-weight: 700; color: var(--accent);
  margin-top: 2px; letter-spacing: .3px;
}
table.score thead th.last-mover { box-shadow: inset 0 3px 0 var(--gold); }
table.score thead th.current-turn.last-mover {
  box-shadow: inset 0 3px 0 var(--gold), inset 0 -3px 0 var(--accent);
}

td.cell.just-moved {
  background: rgba(251,191,36,.16);
  box-shadow: inset 0 0 0 2px var(--gold);
  border-radius: 4px;
}

/* ---------- bottom sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  z-index: 50; display: flex; align-items: flex-end;
  animation: fade .15s ease;
}
@keyframes fade { from { opacity: 0 } to { opacity: 1 } }
.sheet {
  background: var(--bg-2); width: 100%;
  border-radius: 20px 20px 0 0; padding: 8px 16px calc(16px + var(--safe-b));
  max-height: 88vh; overflow-y: auto;
  box-shadow: 0 -10px 40px rgba(0,0,0,.5);
  animation: slideup .2s ease;
}
@keyframes slideup { from { transform: translateY(100%) } to { transform: translateY(0) } }
.sheet-handle { width: 40px; height: 4px; background: var(--line); border-radius: 999px; margin: 6px auto 12px; }
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 14px; }
.sheet-title { font-size: 18px; font-weight: 700; }
.sheet-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }

.quick-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 10px; }
.quick-grid.wide { grid-template-columns: repeat(3, 1fr); }
.quick-grid.cols2 { grid-template-columns: repeat(2, 1fr); }
.opt.neg { color: var(--danger); border-color: #5b2b2b; }
.opt.neg.selected { background: var(--danger); color: #2a0a0a; border-color: var(--danger); }
.opt {
  background: var(--card); border: 1px solid var(--line); color: var(--text);
  border-radius: 10px; padding: 14px 6px; font-size: 17px; font-weight: 600; cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.opt:active { transform: scale(.96); }
.opt.selected { background: var(--accent); color: #04233a; border-color: var(--accent); }
.opt.block { width: 100%; margin-bottom: 10px; }
.opt.cross { border-color: #5b2b2b; color: var(--danger); }
.opt.cross.selected { background: var(--danger); color: #2a0a0a; border-color: var(--danger); }

.field-label { display: block; color: var(--muted); font-size: 12px; margin: 12px 2px 6px; text-transform: uppercase; letter-spacing: .5px; }
.preview { min-height: 22px; color: var(--accent); font-weight: 700; font-size: 15px; margin: 4px 2px; }

.sheet-actions { display: flex; gap: 10px; margin-top: 8px; }
.sheet-actions .btn { flex: 1; }

/* ---------- stats ---------- */
.stat-table { width: 100%; border-collapse: collapse; }
.stat-table th, .stat-table td { padding: 10px 8px; border-bottom: 1px solid var(--line-soft); text-align: center; font-size: 14px; }
.stat-table th { color: var(--muted); font-size: 12px; text-transform: uppercase; }
.stat-table td:first-child, .stat-table th:first-child { text-align: left; font-weight: 600; }

.standings-mini { margin-top: 4px; display: flex; flex-direction: column; gap: 4px; }
.standings-mini .sm-row { display: flex; justify-content: space-between; font-size: 13px; }
.standings-mini .sm-row.win { color: var(--gold); font-weight: 600; }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: calc(20px + var(--safe-b)); left: 50%; transform: translateX(-50%);
  background: #0b1220; border: 1px solid var(--line); color: var(--text);
  padding: 12px 18px; border-radius: 999px; z-index: 60; font-size: 14px;
  box-shadow: 0 8px 30px rgba(0,0,0,.4); animation: fade .15s;
}

.fab {
  position: sticky; bottom: calc(12px + var(--safe-b)); margin-top: 16px;
}

td.cell.neg { color: var(--danger) !important; }
