:root {
  --bg: #0f1720;
  --card: #18232f;
  --line: #2a3a4b;
  --text: #e8eef4;
  --muted: #8fa1b3;
  --accent: #35c46a;
  --warn: #f5b942;
  --danger: #e5534b;
  --btn: #223142;
  --danger-text: #ffb4af;
  --danger-line: #6b2b28;
  --danger-bg: #3a1e1d;
  --notice-text: #f3dc97;
  --notice-line: #6b5a1f;
  --notice-bg: #2e2810;
  --goal-row: rgba(255, 196, 0, 0.12);
  --shadow: rgba(0, 0, 0, 0.55);
  --grass: #2f8f46;
  --grass-dark: #2a8340;
  --chalk: rgba(255, 255, 255, 0.85);
  color-scheme: dark;
}

/* light theme: chosen explicitly, or following the system until the user picks one */
:root[data-theme='light'] {
  --bg: #f2f5f8;
  --card: #ffffff;
  --line: #d3dbe4;
  --text: #15202b;
  --muted: #5b6b7b;
  --accent: #2ab860;
  --warn: #e9a91f;
  --danger: #d63d35;
  --btn: #e7ecf2;
  --danger-text: #b3261e;
  --danger-line: #efb8b4;
  --danger-bg: #fdecea;
  --notice-text: #6b5200;
  --notice-line: #e4cd84;
  --notice-bg: #fff6d8;
  --goal-row: rgba(255, 196, 0, 0.2);
  --shadow: rgba(30, 45, 60, 0.25);
  color-scheme: light;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme='dark']) {
    --bg: #f2f5f8;
    --card: #ffffff;
    --line: #d3dbe4;
    --text: #15202b;
    --muted: #5b6b7b;
    --accent: #2ab860;
    --warn: #e9a91f;
    --danger: #d63d35;
    --btn: #e7ecf2;
    --danger-text: #b3261e;
    --danger-line: #efb8b4;
    --danger-bg: #fdecea;
    --notice-text: #6b5200;
    --notice-line: #e4cd84;
    --notice-bg: #fff6d8;
    --goal-row: rgba(255, 196, 0, 0.2);
    --shadow: rgba(30, 45, 60, 0.25);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body.no-scroll { overflow: hidden; overscroll-behavior: none; position: fixed; inset: 0; }

h1 { font-size: 1.5rem; margin: 0 0 16px; }
h2 { font-size: 1rem; margin: 24px 0 10px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.card h2 { margin-top: 0; }
.muted { color: var(--muted); }
.spacer { flex: 1; }

.page { max-width: 640px; margin: 0 auto; padding: 20px 16px 48px; }
.page.wide { max-width: 1100px; }
.page.narrow { max-width: 440px; }
.page-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { flex: 1; margin: 0; min-width: 0; }
.page-head > h1 { flex: 1 0 auto; }
.stack { display: grid; gap: 8px; margin-top: 16px; }

/* ---------- language switch ---------- */
.lang { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; flex: none; }
.lang button { font: inherit; font-size: 0.75rem; font-weight: 700; padding: 6px 9px; border: 0; background: none; color: var(--muted); cursor: pointer; }
.lang button.active { background: var(--text); color: var(--bg); }
.prefs { display: inline-flex; align-items: center; gap: 6px; flex: none; }
.theme-toggle { font: inherit; font-size: 0.9375rem; line-height: 1; padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; background: none; cursor: pointer; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }

input {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  width: 100%;
  min-width: 0;
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-weight: 600;
  color: var(--text);
  background: var(--btn);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 16px;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #06230f; }
.btn.danger { color: var(--danger-text); border-color: var(--danger-line); background: var(--danger-bg); }
.btn.small { padding: 6px 10px; font-size: 0.875rem; }
.btn.big { padding: 14px 16px; font-size: 1.0625rem; width: 100%; }
.btn.icon { padding: 10px 12px; }
.btn.danger-solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.active { background: var(--text); color: var(--bg); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--text);
  background: none;
  border: 0;
  border-radius: 8px;
  text-decoration: none;
  cursor: pointer;
  flex: none;
}

/* ---------- list ---------- */
.new-match { display: grid; gap: 12px; }
.matches { display: grid; gap: 10px; }
.match { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.match-info { flex: 1 1 220px; display: grid; gap: 2px; min-width: 0; }
.match-info strong { overflow: hidden; text-overflow: ellipsis; }
.live { color: var(--accent); font-weight: 600; margin-left: 6px; }
.match.deleted { opacity: 0.75; }
@media (max-width: 520px) {
  .match-info { flex-basis: 100%; }
  .match .btn:not(.icon):not(.small) { flex: 1; }
}
.trash { margin-top: 24px; }
.trash summary { cursor: pointer; color: var(--muted); font-weight: 600; padding: 6px 0; }
.trash .matches { margin-top: 8px; }
.tag { display: inline-block; margin-left: 4px; padding: 1px 8px; border-radius: 999px; font-size: 0.6875rem; font-weight: 600; background: var(--accent); color: #06230f; vertical-align: middle; }
.tag.subtle { background: var(--line); color: var(--text); }

/* ---------- sign in ---------- */
.auth-card { display: grid; gap: 14px; text-align: center; padding: 24px 20px; }
.auth-card p { margin: 0; }
.auth-icon { font-size: 2.5rem; line-height: 1; }
.enroll-form { display: grid; gap: 14px; text-align: left; }
.field { display: grid; gap: 4px; font-size: 0.875rem; color: var(--muted); }
.notice { padding: 12px; border: 1px solid var(--notice-line); border-radius: 8px; background: var(--notice-bg); color: var(--notice-text); font-size: 0.9375rem; overflow-wrap: anywhere; }
.notice a { color: inherit; font-weight: 700; }

/* ---------- dialogs ---------- */
.dialog { width: min(420px, calc(100vw - 32px)); padding: 0; border: 1px solid var(--line); border-radius: 14px; background: var(--card); color: var(--text); box-shadow: 0 24px 60px var(--shadow); }
.dialog::backdrop { background: rgba(4, 8, 12, 0.65); backdrop-filter: blur(2px); }
.dialog[open] { animation: dialog-in 0.14s ease-out; }
@keyframes dialog-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
.dialog-card { display: grid; gap: 12px; padding: 20px; }
.dialog-card h2 { margin: 0; font-size: 1.125rem; color: var(--text); text-transform: none; letter-spacing: 0; }
.dialog-card p { margin: 0; color: var(--muted); overflow-wrap: anywhere; }
.dialog-actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; margin-top: 4px; }

/* ---------- sync badge ---------- */
.sync { font-size: 0.75rem; white-space: nowrap; color: var(--muted); }
.sync.bad { color: var(--danger-text); cursor: pointer; }
.sync::before { content: ''; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--accent); }
.sync.wait::before { background: var(--warn); }
.sync.bad::before { background: var(--danger); }

/* ---------- recording ---------- */
.rec {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.rec-top { display: flex; flex-direction: column; }
.rec-nav { display: flex; align-items: center; gap: 8px; padding: 6px 8px 0; min-width: 0; }
.rec-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.rec-ctl { display: flex; align-items: center; gap: 10px; padding: 8px 12px; }
.ctl-stack { flex: none; display: grid; gap: 6px; }
.ctl-stack .btn { padding: 8px 12px; }
.swap { opacity: 0.7; }
.clock {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-size: clamp(2.5rem, 14vw, 4.5rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--muted);
}
.is-running .clock { color: var(--text); }

.play {
  position: relative;
  flex: none;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 0;
  background: var(--accent);
  overflow: hidden;
  cursor: pointer;
  touch-action: none;
}
.play-icon {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-40%, -50%);
  border-style: solid;
  border-width: 14px 0 14px 24px;
  border-color: transparent transparent transparent #06230f;
}
.play.armed { background: var(--warn); animation: pulse 0.7s ease-in-out infinite; }
.play.armed .play-icon { border: 0; transform: translate(-50%, -50%); font-weight: 800; font-size: 0.8125rem; color: #3a2a00; }
.play.armed .play-icon::after { content: attr(data-armed); }
.play.running { background: var(--danger); }
.play.running .play-icon { border: 0; width: 24px; height: 24px; border-radius: 4px; background: #fff; transform: translate(-50%, -50%); }
.play-fill { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.45); transform: scaleY(0); transform-origin: bottom; }
.play.stopping .play-fill { transform: scaleY(1); transition: transform 1s linear; }
@keyframes pulse { 50% { transform: scale(1.08); } }

.pitch {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  margin: 4px 10px;
  border: 3px solid var(--chalk);
  border-radius: 6px;
  overflow: hidden;
  touch-action: none;
  background: repeating-linear-gradient(90deg, var(--grass) 0 12.5%, var(--grass-dark) 12.5% 25%);
  filter: saturate(0.35) brightness(0.75);
  transition: filter 0.3s;
}
.is-running .pitch { filter: none; }
.half {
  position: relative;
  flex: 1;
  min-width: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: #fff;
  font: inherit;
  cursor: pointer;
  touch-action: none;
  overflow: hidden;
}
.half.left { border-right: 1.5px solid var(--chalk); }
.half.right { border-left: 1.5px solid var(--chalk); }
.box { position: absolute; top: 20%; bottom: 20%; width: 34%; border: 3px solid var(--chalk); pointer-events: none; }
.box.small { top: 36%; bottom: 36%; width: 14%; }
.half.left .box { left: 0; border-left: 0; }
.half.right .box { right: 0; border-right: 0; }
.circle {
  position: absolute;
  left: 50%;
  top: 50%;
  width: min(26%, 30vh);
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border: 3px solid var(--chalk);
  border-radius: 50%;
  pointer-events: none;
}
.half-label {
  position: absolute;
  inset: auto 0 10px;
  display: grid;
  gap: 2px;
  font-weight: 800;
  letter-spacing: 0.12em;
  opacity: 0.75;
  pointer-events: none;
}
.half-label small { font-weight: 500; letter-spacing: 0; font-size: 0.6875rem; opacity: 0.85; }
.fill { position: absolute; inset: 0; background: rgba(255, 214, 64, 0.45); transform: scaleY(0); transform-origin: bottom; pointer-events: none; }
.half.holding .fill { transform: scaleY(1); transition: transform 0.6s linear; }
.flash {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  font-size: clamp(1.1rem, 5vw, 2rem);
  font-weight: 800;
  text-align: center;
  opacity: 0;
  pointer-events: none;
}
.flash-highlight .flash { background: rgba(255, 255, 255, 0.35); animation: flash 1.2s ease-out; }
.flash-goal .flash { background: rgba(255, 196, 0, 0.75); color: #2b1d00; animation: flash 1.8s ease-out; }
@keyframes flash { 0%, 55% { opacity: 1; } 100% { opacity: 0; } }

.rec-last { display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 4px 12px 8px; font-size: 0.875rem; }
.last-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* landscape phone: one compact top row */
@media (orientation: landscape) and (max-height: 520px) {
  .rec-top { flex-direction: row; align-items: center; }
  .rec-nav { flex: 1; padding: 0 0 0 8px; }
  .rec-ctl { padding: 4px 12px; }
  .ctl-stack { grid-auto-flow: column; }
  .clock { flex: none; font-size: 2.5rem; min-width: 3.2em; }
  .play { width: 56px; height: 56px; }
  .rec-last { min-height: 36px; padding-bottom: 4px; }
}

/* ---------- correction sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 10; display: flex; align-items: flex-end; justify-content: center; background: rgba(0, 0, 0, 0.55); }
.sheet[hidden] { display: none; }
.sheet-card {
  width: 100%;
  max-width: 520px;
  max-height: 100dvh;
  overflow: auto;
  display: grid;
  gap: 12px;
  padding: 14px 16px calc(16px + env(safe-area-inset-bottom));
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px 16px 0 0;
}
.sheet-card p { margin: 0; font-size: 0.875rem; }
.sheet-head { display: flex; align-items: center; gap: 12px; }
.sheet-clock { flex: 1; text-align: right; font-size: 1.75rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.adj-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.adj-grid .btn { padding: 16px 0; font-size: 1.125rem; }
.set-row { display: flex; align-items: center; gap: 8px; font-size: 0.875rem; }
.set-row input { width: 4.2em; text-align: center; }
.set-row .btn { margin-left: auto; }

/* ---------- table ---------- */
.table-title { flex: 1; min-width: 0; }
.table-title h1 { margin: 0; overflow-wrap: anywhere; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 16px 0; }
.seg { display: flex; gap: 4px; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 12px; background: var(--card); }
table { width: 100%; border-collapse: collapse; font-size: 0.9375rem; }
th, td { padding: 8px 10px; text-align: left; white-space: nowrap; border-bottom: 1px solid var(--line); }
th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.sort-btn { display: inline-flex; align-items: center; gap: 4px; padding: 0; border: 0; background: none; font: inherit; text-transform: inherit; letter-spacing: inherit; color: inherit; cursor: pointer; }
.sort-btn:hover, th[aria-sort='ascending'] .sort-btn, th[aria-sort='descending'] .sort-btn { color: var(--text); }
.sort-arrow { display: inline-block; width: 0.9em; font-size: 0.625rem; }
tr:last-child td { border-bottom: 0; }
tr.goal td { background: var(--goal-row); }
tr.goal td:first-child { box-shadow: inset 3px 0 0 #ffc400; }
td.time { font-weight: 700; font-size: 1.0625rem; font-variant-numeric: tabular-nums; }
td .note-input { min-width: 180px; padding: 6px 8px; }
td.actions { text-align: right; }
td.actions .btn { height: 32px; min-width: 36px; vertical-align: middle; }
td.actions .btn[data-act='type'] { width: 3.75rem; }
td.empty { text-align: center; padding: 28px; }

/* ---------- toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 8px);
  z-index: 20;
  transform: translate(-50%, -20px);
  max-width: 90vw;
  padding: 10px 16px;
  border-radius: 999px;
  background: #000c;
  color: #fff;
  font-size: 0.875rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
