:root {
  --bg: #f4f6fa;
  --card: #fff;
  --text: #1b2330;
  --muted: #5d6879;
  --border: #d6dce6;
  --line: #b9c0cc;
  --ink: #1b2330;
  --primary: #1f4e8c;
  --primary-ink: #fff;
  --a: #1f6fd1;
  --a-soft: #eaf2fd;
  --a-tint: rgb(31 111 209 / 0.04);
  --b: #c8541b;
  --b-soft: #fcefe7;
  --b-tint: rgb(200 84 27 / 0.04);
  --good: #1d8a4a;
  --bad: #b3261e;
  --warn: #9a6400;
  --radius: 10px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

#app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 16px;
}

body.full {
  background: #fff;
  overflow: hidden;
}
body.full #app {
  max-width: none;
  padding: 0;
}

h1,
h2,
h3 {
  margin: 0 0 0.5em;
  line-height: 1.2;
}
h1 {
  font-size: 1.5rem;
}
h2 {
  font-size: 1.15rem;
}

a {
  color: var(--primary);
}

.hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0.5em 0;
}

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

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

.team-letter {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.6em;
  height: 1.6em;
  border-radius: 50%;
  color: #fff;
  font-weight: 800;
  font-size: 0.85em;
  flex: none;
}
.team-a .team-letter {
  background: var(--a);
}
.team-b .team-letter {
  background: var(--b);
  /* Different shape from Team A's circle so teams are distinguishable without color. */
  border-radius: 0.15em;
}

/* Buttons ---------------------------------------------------------------- */

.btn {
  font: inherit;
  font-weight: 600;
  min-height: 42px;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
}
.btn:hover:not(:disabled) {
  border-color: var(--primary);
}
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.btn-small {
  min-height: 34px;
  padding: 4px 12px;
  font-size: 0.9rem;
}
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-ink);
}
.btn-danger {
  color: var(--bad);
}

/* Page header, forms, home ---------------------------------------------- */

.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}
.page-header h1 {
  margin: 0;
  flex: 1;
}
.page-header .back {
  text-decoration: none;
  font-weight: 600;
}
.header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.banner-error {
  padding: 12px 16px;
  border-radius: var(--radius);
  background: #fde8e7;
  border: 1px solid #f1a7a2;
  color: var(--bad);
  font-weight: 600;
  margin-bottom: 16px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.9rem;
}
.field[hidden] {
  display: none;
}
.field > span,
.field > legend {
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}
.field small {
  color: var(--muted);
}
input[type='text'],
input[type='number'] {
  font: inherit;
  padding: 8px 10px;
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 6px;
  width: 100%;
}
input[type='checkbox'],
input[type='radio'] {
  width: 20px;
  height: 20px;
  accent-color: var(--primary);
}
.radio-group {
  border: 0;
  padding: 0;
  margin: 0;
}
.radio-group label,
.checkbox-line {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 0;
}
.team-field input {
  font-size: 1.15rem;
}
.grid-2,
.grid-3 {
  display: grid;
  gap: 16px;
}
.grid-2 {
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}
.grid-3 {
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}
.form-error {
  color: var(--bad);
  font-weight: 600;
  min-height: 1.2em;
}
.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.match-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.match-item {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
}
.match-item.is-live {
  border-left: 5px solid var(--good);
}
.match-link {
  display: flex;
  flex-direction: column;
  gap: 2px;
  text-decoration: none;
  color: inherit;
  flex: 1;
  min-width: 240px;
}
.match-link strong {
  font-size: 1.1rem;
  color: var(--primary);
}
.match-link .meta {
  color: var(--muted);
  font-size: 0.85rem;
}
.match-link .status {
  font-weight: 600;
}
.empty {
  text-align: center;
  padding: 40px;
}
.footer-note {
  text-align: center;
  margin-top: 24px;
}

/* Modal ------------------------------------------------------------------ */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgb(15 23 42 / 0.5);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 8vh 16px;
  overflow-y: auto;
  z-index: 10;
}
.modal {
  background: #fff;
  border-radius: var(--radius);
  padding: 20px;
  width: min(560px, 100%);
}
.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 16px;
}
.edit-preview {
  background: var(--bg);
  border-radius: 8px;
  padding: 8px 12px;
}
.edit-recorder .mini {
  font-size: 0.95rem;
  min-height: 36px;
}
.shortcuts th {
  text-align: left;
  padding: 4px 16px 4px 0;
  white-space: nowrap;
}
kbd {
  font-family: ui-monospace, monospace;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 6px;
}

/* Match screen: score grid + side panel ---------------------------------- */

.sheet-screen {
  display: grid;
  grid-template-columns: minmax(0, 1fr) clamp(260px, 27vw, 460px);
  gap: 1.4vw;
  height: 100vh;
  height: 100dvh;
  padding: 1.2vh 1.2vw;
  background: #fff;
}

.grid-wrap {
  container-type: size;
  min-height: 0;
  height: 100%;
}

.score-grid {
  display: grid;
  height: 100cqh;
  grid-template-columns: repeat(4, minmax(0, 1fr)) minmax(0, 0.75fr) repeat(4, minmax(0, 1fr));
  font-size: min(calc(100cqh / var(--rows) * 0.62), 2.4cqw);
  border: 2px solid var(--ink);
  font-variant-numeric: tabular-nums;
}
.score-grid .r {
  display: contents;
}
.score-grid .c {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  line-height: 1;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.score-grid .c.team-a {
  background: var(--a-tint);
}
.score-grid .c.team-b {
  background: var(--b-tint);
}
.score-grid .c.num {
  position: relative;
  font-weight: 700;
  color: var(--muted);
  background: #f3f4f6;
  border-left: 2px solid var(--ink);
  border-right: 2px solid var(--ink);
}
.score-grid .c.run {
  font-weight: 800;
}

.score-grid .head-team {
  justify-content: flex-start;
  gap: 0.5em;
  padding: 0 0.6em;
  font-size: 1.15em;
  font-weight: 800;
  color: #fff;
  border-bottom: 0;
}
.score-grid .head-team.team-a {
  background: var(--a);
}
.score-grid .head-team.team-b {
  background: var(--b);
}
.score-grid .head-team.head-num {
  background: var(--ink);
}
.head-team .team-letter {
  background: #fff !important;
  font-size: 0.8em;
}
.head-team.team-a .team-letter {
  color: var(--a);
}
.head-team.team-b .team-letter {
  color: var(--b);
}
.head-team-name {
  overflow: hidden;
  text-overflow: ellipsis;
}
.score-grid .head-col {
  font-size: 0.55em;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  border-bottom: 2px solid var(--ink);
}
.score-grid .head-col.team-a {
  background: var(--a-soft);
}
.score-grid .head-col.team-b {
  background: var(--b-soft);
}

.score-grid .r.current > .c {
  background: #fff6cc;
}
.score-grid .r.current > .c.num {
  color: var(--ink);
}
.score-grid .r.editable > .c {
  cursor: pointer;
}
.score-grid .r.editable:hover > .c {
  background: #eef2f8;
}
.score-grid .r.discarded > .c:not(.num):not(.run) {
  color: var(--muted);
}
.score-grid .c.void {
  font-style: italic;
  font-size: 0.65em;
}
.score-grid .c.sep {
  background: var(--ink);
  color: #fff;
  font-weight: 700;
  font-size: 0.7em;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.score-grid .c.sep-half {
  background: #e3e6eb;
  color: var(--ink);
  font-size: 0.5em;
  border-top: 2px solid var(--ink);
  border-bottom: 2px solid var(--ink);
}
.score-grid .c.total {
  font-weight: 800;
  border-top: 2px solid var(--ink);
  border-bottom: 0;
  background: #f3f4f6;
}
.score-grid .c.total.num {
  font-size: 0.6em;
  text-transform: uppercase;
  color: var(--ink);
}

.side {
  display: flex;
  flex-direction: column;
  gap: 1.6vh;
  min-height: 0;
  overflow-y: auto;
}
.side-title {
  font-size: clamp(1rem, 1.6vw, 2rem);
  font-weight: 800;
  line-height: 1.15;
}
.side-details {
  margin-top: 0.3em;
  color: var(--muted);
  font-size: clamp(0.85rem, 1.15vw, 1.4rem);
}
.side-scores {
  display: flex;
  flex-direction: column;
  gap: 1.2vh;
}
.side-score {
  border-left: 0.5vw solid;
  border-radius: 6px;
  padding: 0.8vh 1vw;
}
.side-score.team-a {
  border-color: var(--a);
  background: var(--a-soft);
}
.side-score.team-b {
  border-color: var(--b);
  background: var(--b-soft);
}
.side-score.winner {
  box-shadow: 0 0 0 3px #e6b800;
}
.side-team {
  display: flex;
  align-items: center;
  gap: 0.5em;
  font-weight: 700;
  font-size: clamp(0.95rem, 1.5vw, 1.9rem);
  line-height: 1.15;
}
.side-value {
  font-size: clamp(2.5rem, 7.5vw, 9rem);
  font-weight: 900;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.side-tb {
  font-weight: 700;
  font-size: clamp(0.85rem, 1.2vw, 1.4rem);
}
.side-status {
  font-size: clamp(1rem, 1.5vw, 1.8rem);
  font-weight: 700;
}

.clock-time {
  font-size: clamp(2rem, 5vw, 6rem);
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}
.clock-time.running {
  color: var(--ink);
}
.clock-time.expired {
  color: var(--bad);
}
.clock-msg {
  color: var(--bad);
  font-weight: 600;
  font-size: 0.85rem;
}
.clock-msg:empty {
  display: none;
}
.clock-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  margin-top: 0.6vh;
}
.shot {
  min-width: 3.5em;
  text-align: center;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.shot.warning {
  color: var(--warn);
}
.shot.expired {
  color: var(--bad);
}

/* Discreet controls */
.controls {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.controls-hidden .controls {
  display: none;
}
.recorder {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.ctl-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  font-size: 0.85rem;
}
.ctl-row.done {
  opacity: 0.55;
}
.ctl-row .team-letter {
  font-size: 0.75rem;
  margin-right: 2px;
}
.ctl-label {
  color: var(--muted);
  margin-right: 4px;
}
.ctl-flow {
  margin-top: 4px;
}
.mini {
  font: inherit;
  font-size: 0.8rem;
  line-height: 1.2;
  padding: 3px 9px;
  min-height: 26px;
  border: 1px solid #d3d7de;
  border-radius: 5px;
  background: transparent;
  color: #4a5363;
  cursor: pointer;
  touch-action: manipulation;
  list-style: none;
}
.mini:hover:not(:disabled) {
  border-color: #8b93a1;
  color: var(--ink);
}
.mini:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.mini.good {
  color: var(--good);
}
.mini.bad {
  color: var(--bad);
}
.mini.tiny {
  padding: 0 6px;
  min-height: 20px;
}
.mini.attention {
  border-color: var(--warn);
  color: var(--warn);
  font-weight: 600;
}
.mini.on {
  border-color: var(--warn);
  background: #fff6e0;
  color: var(--warn);
  font-weight: 600;
}
.side-buzz {
  margin-top: 0.3em;
  font-size: 0.8em;
}
.side-buzz.team-a {
  color: var(--a);
}
.side-buzz.team-b {
  color: var(--b);
}
.score-grid .c.zero {
  color: #9aa2ae;
}
.more {
  position: relative;
  align-self: flex-start;
}
.more summary::-webkit-details-marker {
  display: none;
}
.more-items {
  position: absolute;
  z-index: 5;
  bottom: calc(100% + 4px);
  left: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 6px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.12);
  white-space: nowrap;
}
.save-error {
  color: var(--bad);
  font-weight: 700;
  font-size: 0.85rem;
}

.side-links {
  margin-top: auto;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  align-items: center;
  font-size: 0.75rem;
  color: var(--muted);
}
.side-links a,
.link {
  font: inherit;
  color: var(--muted);
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  text-decoration: none;
}
.side-links a:hover,
.link:hover:not(:disabled) {
  color: var(--ink);
  text-decoration: underline;
}
.link:disabled {
  opacity: 0.4;
  cursor: default;
}
.saved {
  margin-left: auto;
}
.saved.error {
  color: var(--bad);
  font-weight: 700;
}

@media (max-width: 700px) {
  body.full {
    overflow: auto;
  }
  .sheet-screen {
    grid-template-columns: 1fr;
    height: auto;
  }
  .grid-wrap {
    height: 90vh;
  }
}

@media print {
  @page {
    size: landscape;
    margin: 0.3in;
  }
  :root,
  body.full {
    background: #fff;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .sheet-screen {
    height: 7.8in;
    padding: 0;
  }
  .controls,
  .side-links,
  .clock {
    display: none !important;
  }
}
