:root {
  --felt-0: #061b12;
  --felt-1: #0b2e1f;
  --felt-2: #12452f;
  --felt-3: #1a5a3e;
  --panel: rgba(255, 255, 255, 0.045);
  --panel-2: rgba(255, 255, 255, 0.08);
  --line: rgba(212, 175, 55, 0.22);
  --line-soft: rgba(255, 255, 255, 0.09);
  --gold: #d4af37;
  --gold-hi: #f0d77f;
  --gold-lo: #9c7a1e;
  --ink: #f6f0dd;
  --ink-2: #c9c3ae;
  --muted: #8fa597;
  --red: #e0505a;
  --green-ok: #6fd39b;
  --card: #fbf7ec;
  --radius: 18px;
  --tabs-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --serif: 'Playfair Display', Georgia, 'Times New Roman', serif;
  --sans: 'Manrope', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { background: var(--felt-0); }
body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.4;
  background:
    radial-gradient(120% 70% at 50% -10%, var(--felt-3) 0%, var(--felt-2) 35%, var(--felt-1) 70%, var(--felt-0) 100%) fixed;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.num, .total, input.score { font-variant-numeric: tabular-nums; }

.app { max-width: 600px; margin: 0 auto; padding: 0 16px calc(var(--tabs-h) + var(--safe-b) + 24px); }

/* ---- Top bar ---- */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-t) + 10px) 0 10px;
  background: linear-gradient(var(--felt-1) 70%, rgba(11, 46, 31, 0));
}
.brand { display: flex; align-items: center; gap: 10px; font-family: var(--serif); font-weight: 700; font-size: 24px; letter-spacing: 0.3px; color: var(--gold-hi); }
.brand img { border-radius: 9px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4); }
.top-actions { display: flex; gap: 8px; }
.icon-btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 40px; padding: 0 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  font-weight: 600; font-size: 15px;
}
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:disabled { opacity: 0.35; }

/* ---- Bottom tabs ---- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabs-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: rgba(6, 27, 18, 0.94); border-top: 1px solid var(--line);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.tabs button {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  border: 0; background: none; color: var(--muted); font-size: 12px; font-weight: 600;
}
.tabs svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabs button[aria-current="page"] { color: var(--gold-hi); }

/* ---- Generic blocks ---- */
.view:focus { outline: none; }
h1, h2, h3 { font-family: var(--serif); font-weight: 700; margin: 0; }
h1 { font-size: 28px; color: var(--ink); }
h2 { font-size: 21px; color: var(--ink); }
h3 { font-size: 17px; }
.section-title { display: flex; align-items: baseline; justify-content: space-between; margin: 26px 2px 10px; }
.section-title h2 { font-size: 19px; }
.section-title .hint { color: var(--muted); font-size: 13px; }
.card {
  background: var(--panel); border: 1px solid var(--line-soft); border-radius: var(--radius);
  padding: 16px;
}
.card + .card { margin-top: 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.row { display: flex; align-items: center; gap: 10px; }
.spacer { flex: 1; }
.stack > * + * { margin-top: 12px; }
p { margin: 0; }
.lead { color: var(--ink-2); margin-top: 6px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 52px; padding: 0 20px; border-radius: 14px;
  border: 1px solid var(--line); background: var(--panel-2); color: var(--ink);
  font-weight: 700; font-size: 17px;
}
.btn.block { display: flex; width: 100%; }
.btn.gold {
  border: 0; color: #1c1405;
  background: linear-gradient(180deg, var(--gold-hi), var(--gold) 55%, #b8932a);
  box-shadow: 0 6px 18px rgba(212, 175, 55, 0.25), inset 0 1px 0 rgba(255, 255, 255, 0.5);
}
.btn.danger { color: #ffd6d9; border-color: rgba(224, 80, 90, 0.45); background: rgba(224, 80, 90, 0.12); }
.btn.ghost { background: none; }
.btn.small { min-height: 40px; font-size: 15px; padding: 0 14px; border-radius: 11px; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.4; }
.btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn-row { display: flex; gap: 10px; }
.btn-row > * { flex: 1; }
.link { background: none; border: 0; color: var(--gold-hi); font-weight: 600; padding: 6px 2px; font-size: 14px; }

label.field { display: block; }
label.field > span { display: block; font-size: 13px; color: var(--ink-2); margin-bottom: 6px; font-weight: 600; }
input.text, select, textarea {
  width: 100%; min-height: 48px; padding: 10px 14px; border-radius: 12px;
  border: 1px solid var(--line-soft); background: rgba(0, 0, 0, 0.25); color: var(--ink);
  font-size: 17px;
}
textarea { min-height: 84px; resize: vertical; }
select { appearance: none; -webkit-appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--gold) 50%), linear-gradient(135deg, var(--gold) 50%, transparent 50%); background-position: calc(100% - 20px) 21px, calc(100% - 14px) 21px; background-size: 6px 6px; background-repeat: no-repeat; padding-right: 36px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--gold); outline-offset: 1px; }
.err { color: #ffb3b8; font-size: 14px; }
.err ul { margin: 6px 0 0; padding-left: 18px; }

/* ---- Setup ---- */
.hero { display: flex; align-items: center; gap: 14px; padding: 6px 2px 0; }
.hero img { width: 56px; height: 56px; border-radius: 14px; box-shadow: 0 8px 20px rgba(0, 0, 0, 0.45); }
.hero h1 { font-size: 26px; }
.hero .lead { margin-top: 2px; font-size: 14px; }
.team-setup { position: relative; }
.team-setup { padding: 12px 14px; }
.team-setup .team-label { font-family: var(--serif); color: var(--gold-hi); font-size: 16px; margin-bottom: 8px; }
.team-setup .pair { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; }
.team-setup .amp { color: var(--gold); font-family: var(--serif); font-size: 22px; }
.vs { text-align: center; font-family: var(--serif); color: var(--muted); font-size: 13px; margin: 6px 0; letter-spacing: 2px; }
.rules-chip {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  width: 100%; padding: 14px 16px; border-radius: 14px; border: 1px dashed var(--line); background: none; text-align: left;
}
.rules-chip b { color: var(--gold-hi); }
.notice { display: flex; gap: 12px; align-items: flex-start; padding: 12px 14px; border-radius: 14px; background: rgba(212, 175, 55, 0.08); border: 1px solid var(--line); font-size: 14px; color: var(--ink-2); }
.notice b { color: var(--ink); }
.notice .x { margin-left: auto; background: none; border: 0; color: var(--muted); font-size: 20px; line-height: 1; padding: 0 4px; }

/* ---- Scoreboard ---- */
.board { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 4px; }
.team {
  position: relative; padding: 14px 12px 12px; border-radius: var(--radius);
  background: var(--panel); border: 1px solid var(--line-soft); text-align: center; overflow: hidden;
}
.team.leading { border-color: var(--gold); background: linear-gradient(180deg, rgba(212, 175, 55, 0.16), rgba(212, 175, 55, 0.04)); box-shadow: 0 0 0 1px rgba(212, 175, 55, 0.25), 0 10px 26px rgba(0, 0, 0, 0.25); }
.team .names { font-weight: 700; font-size: 15px; line-height: 1.25; min-height: 38px; padding: 0 12px; display: flex; align-items: center; justify-content: center; }
.team .total { font-weight: 800; font-size: clamp(38px, 12vw, 54px); line-height: 1.05; letter-spacing: -1px; margin: 6px 0 8px; }
.team .total.neg { color: #ffb3b8; }
.team .crown { position: absolute; top: 8px; right: 10px; width: 18px; height: 18px; fill: var(--gold); }
.meld {
  display: inline-flex; flex-direction: column; align-items: center;
  padding: 6px 12px; border-radius: 12px; background: rgba(0, 0, 0, 0.28); border: 1px solid var(--line);
}
.meld .k { font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); font-weight: 700; }
.meld .v { font-size: 22px; font-weight: 800; color: var(--gold-hi); }
.facts { margin-top: 10px; display: grid; gap: 6px; text-align: left; }
.fact { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
.fact b { font-size: 16px; font-weight: 800; color: var(--ink); font-variant-numeric: tabular-nums; }
.fact b.up { color: var(--green-ok); }
.fact b.down { color: #ffb3b8; }
.facts .bar { margin-top: 2px; }
.leadline { margin: 12px 0 0; text-align: center; font-size: 17px; font-weight: 600; color: var(--ink-2); }
.leadline b { color: var(--gold-hi); font-weight: 800; }
.bar { height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.08); overflow: hidden; }
.bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--gold-lo), var(--gold-hi)); border-radius: 3px; }
.target { text-align: center; color: var(--muted); font-size: 13px; margin-top: 6px; }

.banner {
  margin-top: 14px; padding: 16px; border-radius: var(--radius); text-align: center;
  background: linear-gradient(180deg, rgba(212, 175, 55, 0.22), rgba(212, 175, 55, 0.08)); border: 1px solid var(--gold);
}
.banner h2 { color: var(--gold-hi); }
.banner p { margin: 6px 0 12px; color: var(--ink-2); }

.entry { margin-top: 16px; }
.entry h3 { font-family: var(--sans); font-size: 14px; text-transform: uppercase; letter-spacing: 1.2px; color: var(--muted); font-weight: 700; margin: 0 2px 8px; }
.entry-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.entry-cell .who { font-size: 13px; color: var(--ink-2); font-weight: 600; margin: 0 2px 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.score-box { display: flex; align-items: stretch; border-radius: 14px; border: 1px solid var(--line); background: rgba(0, 0, 0, 0.3); overflow: hidden; }
.score-box:focus-within { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(212, 175, 55, 0.25); }
.score-box .sign {
  width: 44px; border: 0; border-right: 1px solid var(--line-soft); background: rgba(255, 255, 255, 0.04);
  font-size: 22px; font-weight: 700; color: var(--gold-hi);
}
.score-box .sign.on { background: rgba(224, 80, 90, 0.25); color: #ffd6d9; }
input.score {
  width: 100%; min-width: 0; height: 60px; border: 0; background: none; padding: 0 12px;
  font-size: 28px; font-weight: 800; text-align: right; color: var(--ink);
}
input.score:focus { outline: none; }
input.score::placeholder { color: rgba(255, 255, 255, 0.2); }
.entry-cell .calc { display: block; margin: 4px auto 0; }
.entry .btn.gold { margin-top: 12px; }

table.rounds { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.rounds th { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); font-weight: 700; padding: 8px 6px; text-align: right; border-bottom: 1px solid var(--line); }
.rounds th:first-child, .rounds td:first-child { text-align: left; width: 44px; color: var(--muted); }
.rounds td { padding: 10px 6px; text-align: right; border-bottom: 1px solid var(--line-soft); vertical-align: top; }
.rounds td .pts { font-size: 18px; font-weight: 700; display: block; }
.rounds td .pts.neg { color: #ffb3b8; }
.rounds td .run { font-size: 12px; color: var(--muted); display: block; margin-top: 2px; }
.rounds td .need { font-size: 11px; color: var(--gold); opacity: 0.8; }
.rounds tr.tap { cursor: pointer; }
.rounds tr.tap:active td { background: rgba(255, 255, 255, 0.05); }
.rounds .edit-ico { width: 28px; text-align: center !important; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 22px 10px; }

/* ---- History ---- */
.game-item { display: block; width: 100%; text-align: left; border: 1px solid var(--line-soft); background: var(--panel); border-radius: var(--radius); padding: 14px 16px; }
.game-item + .game-item { margin-top: 10px; }
.game-item .meta { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.game-item .line { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; padding: 2px 0; }
.game-item .line .n { font-weight: 600; color: var(--ink-2); }
.game-item .line .s { font-weight: 800; font-size: 19px; font-variant-numeric: tabular-nums; }
.game-item .line.win .n, .game-item .line.win .s { color: var(--gold-hi); }
.pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 700; background: rgba(111, 211, 155, 0.15); color: var(--green-ok); }
.pill.gold { background: rgba(212, 175, 55, 0.18); color: var(--gold-hi); }

/* ---- Stats ---- */
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tile { background: var(--panel); border: 1px solid var(--line-soft); border-radius: var(--radius); padding: 14px; }
.tile .k { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); font-weight: 700; }
.tile .v { font-size: 30px; font-weight: 800; margin-top: 4px; font-variant-numeric: tabular-nums; color: var(--gold-hi); }
.tile .d { font-size: 13px; color: var(--ink-2); margin-top: 2px; }
.player-stat { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 12px 0; border-bottom: 1px solid var(--line-soft); }
.player-stat:last-child { border-bottom: 0; }
.player-stat .nm { font-weight: 700; font-size: 17px; }
.player-stat .pct { font-weight: 800; font-size: 20px; color: var(--gold-hi); font-variant-numeric: tabular-nums; text-align: right; }
.player-stat .sub { grid-column: 1 / -1; font-size: 13px; color: var(--muted); }
.player-stat .bar { grid-column: 1 / -1; }

/* ---- Settings ---- */
.menu-item { display: flex; align-items: center; gap: 14px; width: 100%; padding: 16px; border: 1px solid var(--line-soft); background: var(--panel); border-radius: var(--radius); text-align: left; }
.menu-item + .menu-item { margin-top: 10px; }
.menu-item .t { font-weight: 700; font-size: 17px; }
.menu-item .d { color: var(--muted); font-size: 13px; margin-top: 2px; }
.menu-item .chev { margin-left: auto; color: var(--gold); font-size: 22px; }
.back { display: inline-flex; align-items: center; gap: 4px; background: none; border: 0; color: var(--gold-hi); font-weight: 700; padding: 8px 0; font-size: 16px; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border-radius: 14px; background: rgba(0, 0, 0, 0.3); border: 1px solid var(--line-soft); }
.seg.three { grid-template-columns: repeat(3, 1fr); }
.seg button { min-height: 44px; border: 0; border-radius: 10px; background: none; font-weight: 700; color: var(--ink-2); }
.seg button[aria-pressed="true"] { background: var(--gold); color: #1c1405; }

.brackets { width: 100%; border-collapse: separate; border-spacing: 0 8px; }
.brackets th { font-size: 12px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); text-align: left; font-weight: 700; padding: 0 4px; }
.brackets td { padding: 0 4px; vertical-align: middle; }
.brackets input { width: 100%; min-height: 46px; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line-soft); background: rgba(0, 0, 0, 0.25); font-size: 17px; font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.brackets .fixed { color: var(--muted); font-size: 14px; padding-left: 6px; }
.brackets .to { color: var(--ink-2); font-size: 14px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.brackets .del { width: 40px; height: 40px; border-radius: 10px; border: 1px solid rgba(224, 80, 90, 0.35); background: rgba(224, 80, 90, 0.1); color: #ffb3b8; font-size: 20px; line-height: 1; }
.rules-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.rules-grid input { text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.check { display: flex; gap: 12px; align-items: flex-start; font-size: 15px; color: var(--ink-2); }
.check input { width: 22px; height: 22px; accent-color: var(--gold); margin-top: 1px; flex: none; }
.ladder { display: grid; gap: 6px; }
.ladder .rung { display: flex; justify-content: space-between; padding: 10px 12px; border-radius: 10px; background: rgba(0, 0, 0, 0.2); font-variant-numeric: tabular-nums; }
.ladder .rung b { color: var(--gold-hi); }
.player-row { display: flex; gap: 8px; align-items: center; }
.player-row + .player-row { margin-top: 8px; }
.player-row input { flex: 1; }
.player-row .cnt { color: var(--muted); font-size: 12px; width: 62px; text-align: right; }

/* ---- Sheet ---- */
.sheet-wrap { position: fixed; inset: 0; z-index: 20; display: flex; align-items: flex-end; justify-content: center; background: rgba(0, 0, 0, 0.55); animation: fade 0.15s ease-out; }
.sheet {
  width: 100%; max-width: 600px; max-height: 92vh; overflow: auto;
  padding: 10px 18px calc(18px + var(--safe-b)); border-radius: 22px 22px 0 0;
  background: linear-gradient(180deg, #154a33, #0d3424); border-top: 1px solid var(--gold);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.5); animation: rise 0.2s ease-out;
}
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: rgba(255, 255, 255, 0.2); margin: 0 auto 12px; }
.sheet h2 { margin-bottom: 6px; }
.sheet .stack { margin-top: 14px; }
.stepper { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; }
.stepper .ctl { display: flex; align-items: center; gap: 10px; }
.stepper button { width: 44px; height: 44px; border-radius: 12px; border: 1px solid var(--line); background: var(--panel-2); font-size: 22px; font-weight: 700; }
.stepper .val { min-width: 28px; text-align: center; font-weight: 800; font-size: 20px; font-variant-numeric: tabular-nums; }
.calc-lines { font-size: 14px; color: var(--ink-2); }
.calc-lines div { display: flex; justify-content: space-between; padding: 3px 0; font-variant-numeric: tabular-nums; }
.calc-total { display: flex; justify-content: space-between; align-items: baseline; border-top: 1px solid var(--line); margin-top: 6px; padding-top: 10px; font-weight: 800; font-size: 22px; }
.calc-total span:last-child { color: var(--gold-hi); }

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(30px); opacity: 0.6; } }

/* ---- Toast ---- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 30;
  bottom: calc(var(--tabs-h) + var(--safe-b) + 14px);
  display: flex; align-items: center; gap: 14px; max-width: calc(100% - 32px);
  padding: 10px 10px 10px 16px; border-radius: 14px;
  background: #f6f0dd; color: #14261c; font-weight: 600; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}
.toast button { border: 0; background: #14261c; color: var(--gold-hi); font-weight: 800; padding: 8px 14px; border-radius: 10px; }

@media (min-width: 520px) {
  .team .names { font-size: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  .sheet, .sheet-wrap { animation: none; }
}
