:root {
  color-scheme: light;
  --page-pad: clamp(8px, 2vmin, 24px);
  --felt: #176b4f;
  --felt-dark: #0f3e33;
  --ink: #18201e;
  --muted: #66736f;
  --paper: #f6f4ee;
  --panel: #ffffff;
  --line: #d9ded8;
  --gold: #e1aa37;
  --red: #b83b32;
  --shadow: 0 18px 40px rgba(17, 35, 29, 0.16);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  overscroll-behavior: none;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 20% 0%, rgba(225, 170, 55, 0.22), transparent 28rem),
    linear-gradient(145deg, #f7f3ea, #e7eee8 52%, #d9e8df);
  color: var(--ink);
  overflow-x: hidden;
}

button,
input {
  font: inherit;
}

.app-shell {
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding:
    max(var(--page-pad), env(safe-area-inset-top))
    max(var(--page-pad), env(safe-area-inset-right))
    max(var(--page-pad), env(safe-area-inset-bottom))
    max(var(--page-pad), env(safe-area-inset-left));
}

.view {
  display: none;
  min-height: calc(100vh - (var(--page-pad) * 2));
  min-height: calc(100dvh - (var(--page-pad) * 2));
}

.view.is-active {
  display: block;
}

#scoreView.is-active {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  align-content: stretch;
  gap: clamp(8px, 1.5vmin, 16px);
  height: calc(100vh - (var(--page-pad) * 2));
  height: calc(100dvh - (var(--page-pad) * 2));
  min-height: 0;
  overflow: hidden;
}

#scoreView > .topbar {
  margin-bottom: 0;
}

#scoreView > .scoreboard {
  min-height: 0;
}

.home-view {
  min-height: calc(100vh - (var(--page-pad) * 2));
  min-height: calc(100dvh - (var(--page-pad) * 2));
  display: none;
  place-content: center;
  text-align: center;
}

.home-view.is-active {
  display: grid;
}

.brand-mark {
  position: relative;
  width: clamp(118px, 22vw, 168px);
  height: clamp(118px, 22vw, 168px);
  margin: 0 auto 24px;
  border: clamp(8px, 1.6vw, 12px) solid #38261f;
  border-radius: 50%;
  background: radial-gradient(circle at 42% 38%, #26956f, var(--felt));
  box-shadow: var(--shadow), inset 0 0 0 8px rgba(255, 255, 255, 0.16);
}

.brand-ball {
  position: absolute;
  width: 27%;
  height: 27%;
  border-radius: 50%;
  box-shadow: inset -8px -10px 18px rgba(0, 0, 0, 0.18), 0 8px 14px rgba(0, 0, 0, 0.16);
}

.brand-ball.cue {
  left: 16%;
  top: 27%;
  background: #fffaf0;
}

.brand-ball.amber {
  right: 17%;
  top: 32%;
  background: #edae36;
}

.brand-ball.red {
  left: 36%;
  bottom: 15%;
  background: #bd3f35;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: clamp(2rem, 7vw, 4.8rem);
  line-height: 0.95;
  margin-bottom: 28px;
}

h2 {
  font-size: clamp(1.7rem, 4vw, 2.5rem);
}

.discipline-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 220px));
  gap: 12px;
  justify-content: center;
}

.primary-action,
.secondary-action,
.icon-button,
.keypad button,
.ball-button {
  border: 0;
  cursor: pointer;
  transition: transform 140ms ease, box-shadow 140ms ease, background 140ms ease;
}

.primary-action,
.secondary-action {
  min-height: clamp(48px, 7vh, 58px);
  border-radius: 8px;
  padding: 0 18px;
  font-weight: 800;
}

.primary-action {
  background: var(--felt-dark);
  color: white;
  box-shadow: 0 10px 22px rgba(15, 62, 51, 0.22);
}

.secondary-action {
  background: #fff;
  color: var(--felt-dark);
  border: 1px solid var(--line);
}

.compact {
  min-height: 42px;
  padding: 0 14px;
  font-size: 0.95rem;
}

.full {
  width: 100%;
}

button:hover {
  transform: translateY(-1px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: clamp(8px, 2vh, 20px);
}

.topbar > div {
  flex: 1;
  text-align: center;
}

.score-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.topbar-spacer {
  flex: 0 0 clamp(38px, 5.5vmin, 46px);
}

.powered-by {
  align-self: end;
  color: var(--muted);
  font-size: clamp(0.78rem, 1.8vmin, 0.95rem);
  font-weight: 800;
  text-align: center;
}

.icon-button {
  width: clamp(38px, 5.5vmin, 46px);
  height: clamp(38px, 5.5vmin, 46px);
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--felt-dark);
  font-size: 1.5rem;
}

.kicker {
  color: var(--muted);
  font-weight: 800;
  text-transform: uppercase;
  font-size: 0.75rem;
}

.form-grid,
.timer-panel,
.table-wrap,
.ranking-rules {
  background: rgba(255, 255, 255, 0.86);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.form-grid {
  display: grid;
  gap: clamp(10px, 2vmin, 18px);
  max-width: min(1100px, 100%);
  margin: 0 auto;
  padding: clamp(12px, 2.5vmin, 28px);
}

label,
legend {
  color: var(--muted);
  font-weight: 800;
}

input[type="text"],
input[type="number"] {
  width: 100%;
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  min-height: clamp(40px, 5.5vmin, 48px);
  padding: 0 14px;
  background: white;
  color: var(--ink);
}

fieldset {
  margin: 0;
  padding: clamp(10px, 1.8vmin, 14px);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.choice-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 12px;
}

.ball-choice {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  font-weight: 700;
}

.scoreboard {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 360px) minmax(0, 1fr);
  gap: clamp(10px, 2vmin, 18px);
  align-items: stretch;
  min-height: 0;
}

.player-panel,
.pending-box {
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--shadow);
}

.player-panel {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: clamp(6px, 1.5vmin, 14px);
  padding: clamp(10px, 2.2vmin, 24px);
  min-height: 0;
}

.player-name {
  min-height: 1.5em;
  color: var(--muted);
  font-weight: 900;
  text-align: center;
}

.score-value {
  font-size: clamp(3rem, 11vmin, 7rem);
  line-height: 0.9;
  font-variant-numeric: tabular-nums;
}

.pending-box {
  align-self: center;
  min-width: 110px;
  padding: clamp(8px, 1.8vmin, 14px);
  text-align: center;
}

.pending-box p {
  color: var(--muted);
  font-weight: 800;
}

.pending-box strong {
  display: block;
  margin-top: 6px;
  font-size: clamp(1.55rem, 4vmin, 2.1rem);
}

.score-controls {
  display: grid;
  align-content: start;
  gap: clamp(8px, 1.6vmin, 14px);
  min-width: 0;
  min-height: 0;
}

.score-ball {
  display: inline-block;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid rgba(0, 0, 0, 0.18);
  box-shadow: inset -6px -7px 12px rgba(0, 0, 0, 0.14), 0 5px 12px rgba(0, 0, 0, 0.18);
}

.score-ball.white {
  background: #fff9eb;
}

.score-ball.amber {
  background: #e6a22e;
}

.score-ball.large {
  width: clamp(104px, 15vmin, 152px);
  height: clamp(104px, 15vmin, 152px);
}

.ball-button {
  position: relative;
  width: clamp(128px, 18vmin, 184px);
  height: clamp(128px, 18vmin, 184px);
  border-radius: 50%;
  background: #f8f6ef;
  display: grid;
  place-items: center;
  box-shadow: inset 0 0 0 1px var(--line), 0 10px 24px rgba(0, 0, 0, 0.12);
}

.ball-plus {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: rgba(15, 62, 51, 0.88);
  font-size: clamp(3.4rem, 7vmin, 4.6rem);
  font-weight: 900;
  line-height: 1;
  text-shadow: 0 2px 0 rgba(255, 255, 255, 0.65);
  pointer-events: none;
}

.innings-box {
  display: grid;
  justify-items: center;
  gap: 2px;
  min-width: clamp(110px, 16vmin, 150px);
  padding: clamp(8px, 1.4vmin, 12px) clamp(10px, 1.8vmin, 16px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.78);
}

.innings-box span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 900;
  text-transform: uppercase;
}

.innings-box strong {
  font-size: clamp(1.6rem, 4vmin, 2.3rem);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.timer-panel {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  justify-items: stretch;
  align-items: center;
  gap: clamp(8px, 1.6vmin, 14px);
  margin-top: 0;
  padding: clamp(8px, 1.8vmin, 14px);
}

.timer-ring {
  width: clamp(70px, 10vmin, 104px);
  height: clamp(70px, 10vmin, 104px);
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: conic-gradient(var(--gold) calc(var(--progress) * 360deg), #e6e8e1 0);
  box-shadow: inset 0 0 0 10px white, var(--shadow);
}

.timer-ring span {
  font-size: clamp(1.35rem, 3.2vmin, 1.9rem);
  font-weight: 900;
}

.timer-controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  align-items: end;
  width: 100%;
}

.timer-controls label {
  grid-column: 1 / -1;
}

.keypad {
  width: 100%;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: clamp(6px, 1.2vmin, 10px);
}

.keypad button {
  min-height: clamp(38px, 7vh, 58px);
  aspect-ratio: auto;
  border-radius: 8px;
  background: #24342f;
  color: white;
  font-size: clamp(1.15rem, 3vmin, 1.6rem);
  font-weight: 900;
}

.ranking-rules {
  padding: 14px 16px;
  margin-bottom: 14px;
  color: var(--muted);
  font-weight: 700;
}

.table-wrap {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  white-space: nowrap;
}

th {
  color: var(--muted);
  font-size: 0.82rem;
  text-transform: uppercase;
}

td:nth-child(1),
td:nth-child(3),
td:nth-child(4),
td:nth-child(5) {
  font-weight: 900;
}

dialog {
  border: 0;
  border-radius: 8px;
  padding: 0;
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(15, 31, 27, 0.45);
}

.dialog-body {
  width: min(420px, calc(100vw - 32px));
  padding: 22px;
}

.dialog-body p {
  margin: 10px 0 18px;
  color: var(--muted);
  font-weight: 700;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

@media (max-width: 760px) {
  .discipline-grid,
  .scoreboard {
    grid-template-columns: 1fr;
  }

  #scoreView.is-active {
    height: auto;
    min-height: calc(100dvh - (var(--page-pad) * 2));
    overflow: visible;
  }

  .score-controls {
    order: 2;
  }

  .topbar {
    align-items: flex-start;
  }

  .topbar .compact {
    max-width: 118px;
  }

  .score-actions {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 760px) {
  .form-grid {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    max-width: min(1100px, 100%);
  }

  .form-grid .full {
    grid-column: 1 / -1;
  }
}

@media (min-width: 900px) and (orientation: landscape) {
  #scoreView.is-active {
    display: grid;
  }

  #scoreView > .topbar {
    margin-bottom: 0;
  }
}

@media (min-width: 960px) {
  .discipline-grid {
    grid-template-columns: repeat(4, minmax(0, 190px));
  }

  .table-wrap,
  .ranking-rules {
    max-width: min(1180px, 100%);
    margin-left: auto;
    margin-right: auto;
  }
}

@media (orientation: landscape) and (max-height: 560px) {
  .app-shell {
    padding: 10px 16px;
  }

  .home-view {
    min-height: calc(100dvh - 20px);
  }

  .brand-mark {
    width: 94px;
    height: 94px;
    margin-bottom: 12px;
  }

  h1 {
    font-size: clamp(1.8rem, 7vh, 2.7rem);
    margin-bottom: 14px;
  }

  .discipline-grid {
    grid-template-columns: repeat(4, minmax(110px, 1fr));
    max-width: 680px;
  }

  .primary-action,
  .secondary-action {
    min-height: 44px;
  }

  .topbar {
    margin-bottom: 10px;
  }

  .player-panel {
    gap: 8px;
    padding: 12px;
  }

  .score-value {
    font-size: clamp(3rem, 17vh, 4.7rem);
  }

  .score-ball.large {
    width: 108px;
    height: 108px;
  }

  .ball-button {
    width: 132px;
    height: 132px;
  }

  .ball-plus {
    font-size: 3.4rem;
  }

  .timer-panel {
    padding: 12px;
  }

  .timer-ring {
    width: 82px;
    height: 82px;
  }

  .timer-ring span {
    font-size: 1.55rem;
  }

  .keypad {
    margin-top: 0;
  }

  .keypad button {
    min-height: 42px;
    aspect-ratio: 1.75;
    font-size: 1.25rem;
  }
}

@media (min-width: 760px) and (max-width: 980px) {
  .scoreboard {
    grid-template-columns: minmax(0, 1fr) minmax(220px, 300px) minmax(0, 1fr);
    gap: 12px;
  }

  .pending-box {
    padding: 12px;
  }

  .pending-box strong {
    font-size: 1.8rem;
  }

  .keypad button {
    min-height: 52px;
  }
}

@media (max-width: 420px) {
  .app-shell {
    padding: 12px;
  }

  .topbar {
    gap: 10px;
  }

  .icon-button {
    width: 42px;
    height: 42px;
  }

  .player-panel {
    padding: 16px 12px;
  }

  .score-ball.large {
    width: 118px;
    height: 118px;
  }

  .ball-button {
    width: 146px;
    height: 146px;
  }

  .ball-plus {
    font-size: 3.8rem;
  }

  th,
  td {
    padding: 12px 10px;
  }
}
