:root {
  --green: #0a5c36;
  --green-dark: #073d24;
  --green-light: #137a48;
  --felt: #0b6e3f;
  --red: #c0392b;
  --gold: #e8b923;
  --bg: #f3f4f3;
  --card: #ffffff;
  --text: #16201a;
  --muted: #6b7770;
  --border: #e2e6e3;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

.hidden { display: none !important; }

/* Header */
header {
  position: sticky; top: 0; z-index: 10;
  background: var(--green);
  color: #fff;
  padding: calc(env(safe-area-inset-top) + 14px) 16px 14px;
  display: flex; align-items: center; justify-content: space-between;
  box-shadow: var(--shadow);
}
header h1 { font-size: 18px; margin: 0; font-weight: 650; letter-spacing: .2px; }
header .who { font-size: 13px; opacity: .85; }
.link-btn { background: none; border: none; color: #fff; font-size: 14px; opacity: .9; padding: 6px; }

main { padding: 16px 16px 96px; max-width: 640px; margin: 0 auto; }

/* Cards */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}

/* Forms */
label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 6px; font-weight: 600; }
input, select, textarea {
  width: 100%; padding: 13px 12px; font-size: 16px;
  border: 1px solid var(--border); border-radius: 10px; background: #fff;
  color: var(--text); appearance: none;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--green-light); border-color: var(--green-light); }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }

button {
  font-family: inherit; font-size: 16px; font-weight: 650;
  border-radius: 10px; border: none; padding: 14px 16px;
  background: var(--green); color: #fff; width: 100%;
  transition: opacity .15s; cursor: pointer;
}
button:active { opacity: .8; }
button.secondary { background: #eef1ef; color: var(--text); }
button.danger { background: var(--red); }
button.small { width: auto; padding: 9px 12px; font-size: 14px; }
.btn-add { background: #eef1ef; color: var(--green-dark); }

.error { color: var(--red); font-size: 14px; margin-top: 10px; min-height: 18px; }
.muted { color: var(--muted); font-size: 14px; }
h2 { font-size: 16px; margin: 4px 0 12px; }

/* Frame editor */
.frame {
  border: 1px solid var(--border); border-radius: 12px; padding: 12px; margin-bottom: 10px;
  background: #fafbfa;
}
.frame-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.frame-head .n { font-weight: 700; color: var(--green-dark); }
.break-row, .foul-row { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.break-row select { flex: 2; }
.break-row input { flex: 1; }
.break-row .x, .foul-row .x { flex: 0 0 auto; width: 40px; background: #eef1ef; color: var(--red); padding: 10px; }
.foul-row .foul-tag { flex: 0 0 auto; font-size: 13px; color: var(--muted); font-weight: 600; }
.foul-row .foul-player { flex: 2; }
.foul-row .foul-value { flex: 1; }
/* read-only auto-calculated points */
input[readonly] { background: #eef1ef; color: var(--green-dark); font-weight: 700; }

/* History list */
.match-item, .admin-user { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.admin-user button { flex: 0 0 auto; }
.match-item .score { font-weight: 750; font-size: 20px; color: var(--green-dark); white-space: nowrap; }
.match-item .meta { font-size: 12px; color: var(--muted); margin-top: 3px; }
.match-item .names { font-weight: 600; }
.winner { color: var(--green); }
.chip { display: inline-block; background: var(--gold); color: #4a3a00; font-size: 11px; font-weight: 700;
  padding: 2px 7px; border-radius: 20px; margin-left: 6px; }

/* Stats table */
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: right; padding: 9px 6px; border-bottom: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; }
th { color: var(--muted); font-weight: 600; font-size: 12px; }
td.name { font-weight: 650; }
/* Stats cards */
.stat-card { padding: 14px 16px; }
.stat-name { font-weight: 750; font-size: 16px; color: var(--green-dark); margin-bottom: 12px; }
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px 8px; }
.stat-grid > div { display: flex; flex-direction: column; }
.stat-grid b { font-size: 18px; font-weight: 800; color: var(--text); line-height: 1.1; }
.stat-grid span { font-size: 11px; color: var(--muted); margin-top: 3px; }

table.summary { margin-bottom: 16px; }
table.summary th:not(:first-child), table.summary td:not(:first-child) { text-align: right; width: 26%; }

/* frame rows in the match summary are clickable (open a per-frame stats pop-up) */
tr.frame-row { cursor: pointer; }
tr.frame-row:hover { background: var(--bg); }

/* ---- Live scorer ---- */
.scoreboard { display: flex; align-items: stretch; gap: 8px; padding: 14px; }
.sb-player { flex: 1; text-align: center; border-radius: 12px; padding: 10px 6px; border: 2px solid transparent; }
.sb-player.striking { border-color: var(--green); background: #f0f8f3; }
.sb-name { font-size: 13px; font-weight: 700; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sb-score { font-size: 44px; font-weight: 800; color: var(--green-dark); line-height: 1.05; }
.sb-frames { font-size: 13px; color: var(--muted); font-weight: 700; }
.sb-frames::before { content: "frames "; font-weight: 600; }
.sb-mid { flex: 0 0 auto; align-self: center; text-align: center; min-width: 80px; }
.sb-frameno { font-size: 13px; font-weight: 700; color: var(--green-dark); }
.sb-clock { font-size: 20px; font-weight: 800; color: var(--text); font-variant-numeric: tabular-nums; margin-top: 2px; }
.sb-break { font-size: 12px; color: var(--muted); margin-top: 4px; }
.sb-break span { font-weight: 800; color: var(--text); font-size: 16px; }
.sb-reds { font-size: 12px; color: var(--muted); margin-top: 2px; }
.sb-reds span { font-weight: 800; color: var(--red); }
.sb-target { font-size: 11px; color: var(--muted); margin-top: 4px; }
.ball:disabled { opacity: .25; }
.ball:disabled:active { transform: none; }
#on-ball { color: var(--green-dark); font-weight: 700; text-transform: capitalize; }

/* Sync status bar */
.sync-status { font-size: 12px; font-weight: 600; text-align: center; padding: 5px 12px; }
.sync-status.online { background: #eef6f0; color: var(--green); }
.sync-status.offline { background: #fdecea; color: var(--red); }
.sync-status.syncing { background: #fff6e0; color: #8a6d00; }

.turn-label { text-align: center; font-size: 14px; color: var(--muted); margin-bottom: 12px; }
.turn-label strong { color: var(--green-dark); }

.balls { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.ball { display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 64px; border-radius: 12px; font-weight: 700; padding: 0; gap: 2px; }
.ball .bval { font-size: 22px; font-weight: 800; line-height: 1; }
.ball .bname { font-size: 11px; opacity: .9; }
.ball:active { transform: scale(.96); }

.match-state { text-align: center; font-size: 13px; font-weight: 700; color: var(--green-dark);
  margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--border); }

.multi-red { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.multi-red .mr-label { font-size: 13px; color: var(--muted); font-weight: 600; }
.multi-red .mr-btn { flex: 1; height: 40px; font-size: 17px; font-weight: 800;
  background: var(--red); color: #fff; }
.multi-red .mr-btn:disabled { opacity: .25; }

.actions { display: flex; gap: 8px; margin-top: 12px; }
.actions > button { flex: 1; }
.act.foul { background: var(--red); }
.act.miss { background: #6b7770; }
.act.safety { background: #2a6f97; }

.foul-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.foul-pick { height: 56px; font-size: 24px; font-weight: 800; background: var(--red); }
.reds-pick { height: 48px; font-size: 20px; font-weight: 800; background: #eef1ef; color: var(--text); }
.foul-pick.sel, .reds-pick.sel { outline: 3px solid var(--green-dark); outline-offset: 1px; }
.foul-section-label { font-size: 12px; font-weight: 700; color: var(--muted); text-align: left; margin: 14px 0 6px; }
.foul-outcomes { display: flex; flex-direction: column; gap: 8px; }
.outcome { background: var(--green); }
.outcome[data-o="again"] { background: #2a6f97; }
.outcome[data-o="free"] { background: var(--gold); color: #4a3a00; }
.free-note { background: var(--gold); color: #4a3a00; font-size: 13px; font-weight: 700;
  text-align: center; padding: 8px 10px; border-radius: 10px; margin-bottom: 10px; }

/* Bottom tab bar */
nav.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; background: rgba(255,255,255,.92);
  backdrop-filter: saturate(180%) blur(12px);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
nav.tabs button {
  flex: 1; background: none; color: var(--muted); border-radius: 0;
  font-size: 11px; font-weight: 600; padding: 9px 4px 8px;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
}
nav.tabs button .ico { font-size: 20px; line-height: 1; }
nav.tabs button.active { color: var(--green); }

/* Login */
.login-wrap { min-height: 100vh; display: flex; flex-direction: column; justify-content: center; padding: 24px; max-width: 420px; margin: 0 auto; }
.brand { text-align: center; margin-bottom: 24px; }
.brand .ball { font-size: 52px; }
.brand h1 { color: var(--green-dark); margin: 8px 0 2px; }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(80px + env(safe-area-inset-bottom));
  background: #16201a; color: #fff; padding: 11px 18px; border-radius: 24px; font-size: 14px;
  opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 50;
}
.toast.show { opacity: .96; }
