/* Crokinole Scoreboard — DOM overlay.
   The canvas is the hero; #ui is a fixed pass-through layer and only real
   controls take pointer events. */

:root {
  --wood-1: #8a6846;
  --wood-2: #6a4f35;
  --wood-3: #56402b;
  --wood-hi: rgba(255, 235, 200, 0.14);
  --edge: rgba(0, 0, 0, 0.45);
  --ink: #f0e5d4;
  --ink-dim: #d3c1a5;
  --ink-quiet: #b9a687;
  --accent: #2e7d32;
  --accent-2: #1b5e20;
  --gold: #e8c352;
  --danger: #b3382f;
  --panel-1: rgba(138, 104, 70, 0.97);
  --panel-2: rgba(90, 68, 45, 0.98);
  --pill: linear-gradient(180deg, rgba(138, 104, 70, 0.95), rgba(90, 68, 45, 0.95));
  --radius: 14px;
  --tap: 48px;
  --primary-tap: clamp(56px, 9vh, 76px);
  --dur: 180ms;
  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
  --pad: 12px;
  --sab: env(safe-area-inset-bottom, 0px);
  --sat: env(safe-area-inset-top, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

html.hc {
  --wood-1: #0b0b0b;
  --wood-2: #000;
  --wood-3: #000;
  --wood-hi: #fff;
  --edge: #fff;
  --ink: #fff;
  --ink-dim: #fff;
  --ink-quiet: #e6e6e6;
  --accent: #005fcc;
  --accent-2: #003f8a;
  --gold: #ffd400;
  --danger: #d32f2f;
  --panel-1: #000;
  --panel-2: #000;
  --pill: #000;
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #3a2f27;
  overscroll-behavior: none;
}

body {
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Ubuntu, Cantarell, 'Noto Sans', sans-serif;
  font-size: 16px;
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

[hidden] { display: none !important; }

/* The body-wide selection lock keeps stray drags off the board, but text fields still need to
   select, and iOS needs the callout to place a caret. */
input,
textarea {
  -webkit-user-select: text;
  user-select: text;
  -webkit-touch-callout: default;
}

canvas#board {
  position: fixed;
  inset: 0;
  display: block;
  touch-action: none;
  z-index: 0;
}

#ui {
  position: fixed;
  inset: 0;
  z-index: 2;
  pointer-events: none;
}

/* Everything interactive opts back in. */
#ui button,
#ui input,
#ui select,
#ui textarea,
#ui a[href],
#ui .takes-pointer { pointer-events: auto; }

:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
  border-radius: 6px;
}
html.hc :focus-visible { outline-color: #fff; }

/* ---------------------------------------------------------------- buttons */

.btn,
.rb-btn,
.icon-btn {
  -webkit-appearance: none;
  appearance: none;
  font: inherit;
  color: var(--ink);
  background: var(--pill);
  border: 2px solid var(--edge);
  border-radius: var(--radius);
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 10px 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  box-shadow: inset 0 1px 0 var(--wood-hi), 0 2px 6px rgba(0, 0, 0, 0.35);
  transition: transform var(--dur) var(--ease), filter var(--dur) var(--ease), background-color var(--dur) var(--ease);
}
.btn:active,
.rb-btn:active,
.icon-btn:active { transform: translateY(1px); filter: brightness(1.12); }

.btn[disabled],
.rb-btn[disabled] { opacity: 0.42; cursor: default; box-shadow: none; }
html.hc .btn[disabled], html.hc .rb-btn[disabled] { opacity: 1; color: #8f8f8f; border-color: #8f8f8f; }

.btn.primary,
.rb-btn.primary {
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  font-weight: 700;
}
.btn.danger { background: linear-gradient(180deg, #b3382f, #8a271f); }
.btn.wide { width: 100%; }
.btn.big { min-height: var(--primary-tap); font-size: 20px; }

.icon-btn { padding: 10px; font-size: 20px; line-height: 1; }

/* ------------------------------------------------------------- round bar */

#roundbar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  padding: 8px calc(var(--pad) + var(--sar)) calc(10px + var(--sab)) calc(var(--pad) + var(--sal));
  pointer-events: none; /* only the controls inside catch taps */
}

.rb-readout {
  align-self: center;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 100%;
  padding: 8px 16px;
  border-radius: 999px;
  background: var(--pill);
  border: 2px solid var(--edge);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  font-size: 17px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}
.readout-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lead-dot {
  flex: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #ff7f00;
  border: 2px solid rgba(0, 0, 0, 0.5);
  box-shadow: inset 0 1px 2px rgba(255, 255, 255, 0.5);
}
html.hc .lead-dot { border-color: #fff; }
.tally {
  flex: none;
  font-size: 17px;
  font-weight: 700;
  color: var(--ink-dim);
  font-variant-numeric: tabular-nums;
  padding-left: 6px;
  border-left: 1px solid rgba(0, 0, 0, 0.35);
}
.tally:empty { display: none; }

.rb-chips {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  max-width: 100%;
}

/* The marquee interaction: one big colour-coded 20s chip per player. */
.chip20 {
  -webkit-appearance: none;
  appearance: none;
  font: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 56px;
  padding: 6px 16px 6px 10px;
  border-radius: 999px;
  color: var(--ink);
  background: var(--pill);
  border: 2px solid var(--edge);
  border-left: 8px solid var(--c, #ff7f00);
  box-shadow: inset 0 1px 0 var(--wood-hi), 0 2px 8px rgba(0, 0, 0, 0.4);
  transition: transform var(--dur) var(--ease), filter var(--dur) var(--ease);
}
.chip20:active { transform: scale(0.96); filter: brightness(1.15); }
.chip20.bump { animation: bump var(--dur) var(--ease); }
@keyframes bump { 50% { transform: scale(1.09); } }
.chip20 .chip-name {
  font-size: 17px;
  font-weight: 600;
  max-width: 8ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chip20 .chip-count {
  font-size: 26px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--gold);
}
.chip20 .chip-unit { font-size: 17px; color: var(--ink-quiet); }
/* The colour band is how a player finds their own chip — high contrast keeps it, widened. */
html.hc .chip20 { background: #000; border-color: #fff; border-left: 12px solid var(--c, #ff7f00); }
html.hc .chip20 .chip-count { color: #ffd400; }
html.hc .chip20 .chip-unit { color: #fff; }

.rb-actions {
  display: flex;
  align-items: stretch;
  gap: 10px;
}
.rb-btn {
  min-height: var(--primary-tap);
  font-size: 17px;
  flex-direction: column;
  gap: 2px;
}
.rb-btn .glyph { font-size: 22px; line-height: 1; }
.rb-btn .glyph.big { font-size: 28px; }
.rb-btn .lbl { font-size: 17px; font-weight: 600; letter-spacing: 0.02em; }
.rb-btn.primary { flex: 1 1 auto; font-size: 20px; letter-spacing: 0.01em; }
.rb-btn.square { flex: 0 0 auto; width: var(--primary-tap); padding: 0; }
#btn-undo { flex: 0 0 auto; min-width: 76px; }

/* ----------------------------------------------------------- bottom sheet */

.scrim {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  pointer-events: auto;
  opacity: 0;
  transition: opacity var(--dur) var(--ease);
}
.scrim.open { opacity: 1; }

.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  display: flex;
  flex-direction: column;
  max-height: min(88vh, 820px);
  pointer-events: auto;
  background: linear-gradient(180deg, var(--panel-1), var(--panel-2));
  border-top: 2px solid var(--edge);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.55);
  padding-left: var(--sal);
  padding-right: var(--sar);
  transform: translateY(100%);
  transition: transform var(--dur) var(--ease);
}
.sheet.open { transform: translateY(0); }
html.hc .sheet { background: #000; border: 2px solid #fff; border-bottom: 0; }

.sheet-grip {
  width: 44px;
  height: 5px;
  border-radius: 3px;
  margin: 8px auto 0;
  background: rgba(255, 255, 255, 0.28);
}
.sheet-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px 10px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.35);
}
html.hc .sheet-head { border-bottom: 2px solid #fff; }
.sheet-head h2 {
  flex: 1;
  margin: 0;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 24px;
  font-weight: 700;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}
.sheet-body {
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 12px 14px calc(20px + var(--sab));
}

/* ------------------------------------------------------- sheet components */

.group { margin: 0 0 18px; }
.group > h3 {
  margin: 0 0 8px;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 18px;
  font-weight: 700;
  color: var(--ink-dim);
  letter-spacing: 0.02em;
}
.group.card {
  background: rgba(0, 0, 0, 0.16);
  border: 1px solid rgba(0, 0, 0, 0.32);
  border-radius: var(--radius);
  padding: 12px;
}
html.hc .group.card { background: #000; border: 2px solid #fff; }

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 8px 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.22);
}
.row:last-child { border-bottom: 0; }
html.hc .row { border-bottom-color: #fff; }
.row .lbl { font-size: 17px; font-weight: 600; }
.row .hint { display: block; font-size: 14px; font-weight: 400; color: var(--ink-quiet); }
.row .ctl { display: flex; align-items: center; gap: 8px; }

.muted { color: var(--ink-quiet); font-size: 16px; margin: 6px 0; }

/* segmented control */
.seg {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: rgba(0, 0, 0, 0.28);
  border: 2px solid var(--edge);
  border-radius: 12px;
}
.seg > button {
  -webkit-appearance: none;
  appearance: none;
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  color: var(--ink);
  background: transparent;
  border: 0;
  border-radius: 9px;
  min-height: var(--tap);
  min-width: 48px;
  padding: 8px 14px;
  cursor: pointer;
  transition: background-color var(--dur) var(--ease);
}
.seg > button[aria-checked='true'] {
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2);
}
html.hc .seg > button[aria-checked='true'] { background: #fff; color: #000; }

/* switch */
.switch {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 68px;
  height: var(--tap);
  padding: 0;
  border-radius: 999px;
  border: 2px solid var(--edge);
  background: rgba(0, 0, 0, 0.32);
  cursor: pointer;
  transition: background-color var(--dur) var(--ease);
}
.switch::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 4px;
  width: 34px;
  height: 34px;
  margin-top: -17px;
  border-radius: 50%;
  background: var(--ink);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  transition: transform var(--dur) var(--ease);
}
.switch[aria-checked='true'] { background: linear-gradient(180deg, var(--accent), var(--accent-2)); }
.switch[aria-checked='true']::after { transform: translateX(26px); }
html.hc .switch { background: #000; }
html.hc .switch[aria-checked='true'] { background: #fff; }
html.hc .switch[aria-checked='true']::after { background: #000; }

/* stepper */
.stepper {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(0, 0, 0, 0.24);
  border: 2px solid var(--edge);
  border-radius: 12px;
  padding: 4px;
}
.stepper > button {
  -webkit-appearance: none;
  appearance: none;
  font: inherit;
  font-size: 26px;
  font-weight: 700;
  line-height: 1;
  color: var(--ink);
  background: var(--pill);
  border: 1px solid var(--edge);
  border-radius: 9px;
  width: var(--tap);
  height: var(--tap);
  cursor: pointer;
}
.stepper > button:active { filter: brightness(1.15); }
.stepper > input {
  font: inherit;
  font-size: 22px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-align: center;
  width: 4.5ch;
  min-height: var(--tap);
  color: var(--ink);
  background: transparent;
  border: 0;
  -moz-appearance: textfield;
}
.stepper > input::-webkit-outer-spin-button,
.stepper > input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper.small > button { width: var(--tap); height: var(--tap); font-size: 22px; }
.stepper.small > input { font-size: 19px; width: 3ch; }

/* quick chips */
.quick { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.quick > button {
  -webkit-appearance: none;
  appearance: none;
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  background: rgba(0, 0, 0, 0.24);
  border: 2px solid var(--edge);
  border-radius: 10px;
  min-height: var(--tap);
  min-width: 52px;
  cursor: pointer;
}
.quick > button:active { filter: brightness(1.2); }

/* colour swatch */
.swatch {
  -webkit-appearance: none;
  appearance: none;
  flex: none;
  width: var(--tap);
  height: var(--tap);
  border-radius: 50%;
  border: 3px solid rgba(0, 0, 0, 0.55);
  background: #ff7f00;
  box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.4), 0 2px 5px rgba(0, 0, 0, 0.4);
  cursor: pointer;
}
html.hc .swatch { border-color: #fff; }

/* text field */
.field {
  font: inherit;
  font-size: 17px;
  flex: 1 1 8ch;
  min-width: 0;
  min-height: var(--tap);
  padding: 8px 12px;
  color: var(--ink);
  background: rgba(0, 0, 0, 0.28);
  border: 2px solid var(--edge);
  border-radius: 10px;
}
html.hc .field { background: #000; border-color: #fff; }

/* round sheet */
.entrant {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.entrant .name {
  font-size: 19px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.badge {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid var(--edge);
  background: rgba(0, 0, 0, 0.28);
  color: var(--ink-dim);
}
.twenty-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 0 0;
}
.twenty-row .who { font-size: 16px; color: var(--ink-dim); }
/* Pinned to the bottom of the sheet's scrollport so the confirm action is always in reach. */
.sheet-actions {
  position: sticky;
  bottom: calc(-1 * (20px + var(--sab)));
  z-index: 1;
  margin: 4px -14px calc(-20px - var(--sab));
  padding: 10px 14px calc(14px + var(--sab));
  background: linear-gradient(180deg, rgba(0, 0, 0, 0), var(--panel-2) 26%, var(--panel-2));
}
html.hc .sheet-actions { background: #000; border-top: 2px solid #fff; }

.preview {
  margin: 4px 0 14px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: rgba(0, 0, 0, 0.28);
  border: 2px solid var(--edge);
  font-size: 19px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-align: center;
  min-height: 24px;
}
html.hc .preview { background: #000; border-color: #fff; }

.actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }

/* history */
.hist { list-style: none; margin: 0; padding: 0; }
.hist > li {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.22);
  font-variant-numeric: tabular-nums;
}
html.hc .hist > li { border-bottom-color: #fff; }
.hist .when { font-size: 14px; color: var(--ink-quiet); min-width: 11ch; }
.hist .who { font-size: 16px; flex: 1 1 12ch; }
.hist .sc { font-size: 19px; font-weight: 700; }
.hist .win { font-size: 14px; color: var(--gold); }

/* about */
.prose { font-size: 16px; line-height: 1.55; color: var(--ink); }
.prose h3 {
  margin: 18px 0 6px;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 19px;
  color: var(--ink-dim);
}
.prose p { margin: 0 0 10px; }
.prose ul { margin: 0 0 10px; padding-left: 20px; }
.prose li { margin: 0 0 6px; }
.prose em { color: var(--gold); font-style: italic; }

/* ------------------------------------------------------------ toast/banner */

.toast {
  position: absolute;
  left: 50%;
  bottom: calc(var(--sab) + 12px);
  transform: translate(-50%, 20px);
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: min(92vw, 460px);
  padding: 12px 14px;
  border-radius: var(--radius);
  background: rgba(24, 18, 12, 0.96);
  border: 2px solid var(--edge);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.55);
  font-size: 16px;
  opacity: 0;
  /* Must swallow its own taps: it sits directly over the round bar's primary controls. */
  pointer-events: auto;
  transition: opacity var(--dur) var(--ease), transform var(--dur) var(--ease);
}
.toast[hidden] { pointer-events: none; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
html.hc .toast { background: #000; border-color: #fff; }
.toast-msg { flex: 1; }
.toast-action {
  -webkit-appearance: none;
  appearance: none;
  font: inherit;
  font-weight: 700;
  color: var(--gold);
  background: transparent;
  border: 2px solid var(--gold);
  border-radius: 10px;
  min-height: var(--tap);
  padding: 8px 14px;
  cursor: pointer;
}
html.hc .toast-action { color: #fff; border-color: #fff; }

.banner {
  position: absolute;
  top: calc(var(--sat) + 10px);
  left: calc(var(--sal) + 12px);
  right: calc(var(--sar) + 12px);
  /* Opaque panel over the board: taps must stop here, not reach the canvas behind it. */
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: linear-gradient(180deg, var(--panel-1), var(--panel-2));
  border: 2px solid var(--edge);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.5);
}
html.hc .banner { background: #000; border-color: #fff; }
.banner-text { flex: 1 1 12ch; margin: 0; font-size: 16px; }
.banner-actions { display: flex; align-items: center; gap: 8px; }

/* --------------------------------------------------------- motion opt-out */

@media (prefers-reduced-motion: reduce) {
  :root { --dur: 0.01ms; }
  #ui *, #ui *::before, #ui *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}
html.reduce-motion { --dur: 0.01ms; }
html.reduce-motion #ui *,
html.reduce-motion #ui *::before,
html.reduce-motion #ui *::after {
  transition-duration: 0.01ms !important;
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
}

/* ------------------------------------------------------------- wide/short */

@media (min-width: 720px) {
  #roundbar { padding-left: calc(24px + var(--sal)); padding-right: calc(24px + var(--sar)); }
  .rb-actions { max-width: 700px; width: 100%; margin: 0 auto; }
  .sheet { left: 50%; right: auto; width: min(640px, 100vw); transform: translate(-50%, 100%); }
  .sheet.open { transform: translate(-50%, 0); }
}

@media (max-height: 560px) {
  :root { --primary-tap: 52px; }
  .chip20 { min-height: 48px; }
  .sheet { max-height: 94vh; }
}
