/* ============================================================================
   SCORECARD BOTTOM SHEET
   Additive UI layer for mobile score entry.
   ============================================================================ */

/* [mobile keyboard fix] Keyed to the JS-controlled `score-sheet-active` class (set
   exactly when score-sheet.js's isMobileMode is true: mobile width / coarse pointer /
   touch capability) — NOT the @media (pointer: coarse) block below, which can MISMATCH
   isMobileMode (sheet mode also activates on viewport width / touch capability without
   pointer:coarse). Suppressing text selection + the iOS long-press callout HERE kills
   the exact gestures that escaped the mousedown-preventDefault and popped the keyboard:
   a long-press shows the selection magnifier + caret and focuses the field. The grid
   cell is a tap-to-open-sheet tile, never a text field, so it needs no caret/selection. */
body.score-sheet-active #scorecard .score-input {
  caret-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

body.score-sheet-open {
  overscroll-behavior-y: none;
}

/* On touch screens, score cells look like tappable tiles rather than form
   inputs. !important on border-color/background is the score-sheet "tile look"
   override: it must beat the .score-input.receives-stroke / .gives-stroke
   border-color rules (spec 0,2,0) AND the @media (max-width: 768px) variants
   of those at spec 1,2,1. Removing was tried as part of a broader sweep — the
   resulting handicap-stroke regression analysis confirmed this rule is the
   canonical "in score-sheet mode the cell is a black tile, not a colored
   stroke-ring input" toggle. */
@media (pointer: coarse) {
  body.score-sheet-active #scorecard .score-input {
    border-color: var(--hi-soft) !important;
    background: #000000 !important;
    border-radius: 8px;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    font-weight: 700;
    -moz-appearance: textfield;
  }

  body.score-sheet-active #scorecard .score-input::-webkit-inner-spin-button,
  body.score-sheet-active #scorecard .score-input::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
  }
}

.score-sheet-backdrop {
  position: fixed;
  inset: 0;
  /* [perf] Flat dim, no backdrop-filter (was blur(5px), then blur(2px)) — a
     filtered layer still forces the compositor to re-sample the full viewport
     every frame of the opacity fade, even at a small radius. The dim alone
     reads the same at a glance and drops the last open-animation paint cost
     on this sheet. See notes/PERFORMANCE_UPGRADE.md §A.3. */
  background: rgba(0, 0, 0, 0.2);
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
  z-index: 9100;
}

.score-sheet-backdrop.is-open {
  opacity: 1;
  pointer-events: auto;
}
.score-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: calc(100dvh - max(env(safe-area-inset-top, 0px), 56px));
  display: flex;
  flex-direction: column;
  background: var(--panel);
  color: var(--ink);
  border-top: 2px solid var(--line);
  border-top-left-radius: var(--radius-lg);
  border-top-right-radius: var(--radius-lg);
  box-shadow: none;
  transform: translateY(100%);
  transition: transform 220ms cubic-bezier(0.2, 0.8, 0.2, 1);
  z-index: 9101;
}

.score-sheet.is-open {
  transform: translateY(0);
  box-shadow: 0 -12px 32px rgba(0, 0, 0, 0.4);
}
@media (min-width: 720px) {
  .score-sheet {
    left: 50%;
    right: auto;
    bottom: 24px;
    transform: translate(-50%, 120%);
    width: min(980px, calc(100vw - 24px));
    max-height: min(90vh, 960px);
    border-radius: var(--radius-lg);
    border: 2px solid var(--line);
  }

  .score-sheet.is-open {
    transform: translate(-50%, 0);
  }
}

.score-sheet-header {
  display: grid;
  grid-template-columns: 40px 1fr 40px 40px;
  gap: 8px;
  align-items: center;
  padding: 12px 14px;
  border-bottom: 2px solid var(--line);
  background: var(--panel-alt);
}
.score-sheet-title {
  margin: 0;
  text-align: center;
  font-size: var(--text-xl);
}

.score-sheet-navbtn,
.score-sheet-close {
  min-height: 40px;
  border: 2px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  font-size: calc(18px + var(--font-size-step));
  font-weight: 700;
}
.score-sheet-navbtn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.score-sheet-body {
  padding: 12px 14px;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.score-sheet-hole-card {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0;
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--panel-alt);
  border-top: 2px solid rgba(255, 255, 255, 0.3);
  border-bottom: 2px solid rgba(255, 255, 255, 0.3);
  border-left: 2px solid rgba(255, 255, 255, 0.15);
  border-radius: 0;
  padding: 0;
  box-shadow: none;
  overflow: hidden;
}

.score-sheet-hole-stat {
  background: transparent;
  border-left: 2px solid rgba(255, 255, 255, 0.15);
  border-right: none;
  border-top: none;
  border-bottom: none;
  border-radius: 0;
  padding: 8px 10px;
  min-width: 0;
}

.score-sheet-hole-stat:first-child {
  border-left: none;
}
.score-sheet-meta-label {
  display: block;
  font-size: calc(12px + var(--font-size-step));
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 4px;
}

.score-sheet-meta-value {
  display: block;
  font-size: var(--text-2xl);
  font-weight: 700;
  text-align: right;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.score-sheet-grid {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  min-width: 0;
}

.score-sheet-player-card {
  border: 2px solid var(--line);
  border-radius: 10px;
  background: var(--panel-alt);
  padding: 8px;
}
.score-sheet-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.score-sheet-card-name {
  font-size: var(--text-md);
  font-weight: 700;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.score-sheet-card-stroke {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 24px;
  padding: 2px 6px;
  border: 2px solid var(--line);
  border-radius: 999px;
  font-size: calc(11px + var(--font-size-step));
  font-weight: 700;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.02);
}

.score-sheet-card-stroke.is-down {
  border-color: rgba(31, 157, 85, 0.65);
  background: rgba(31, 157, 85, 0.2);
  color: #e8ecf2;
}

.score-sheet-card-stroke.is-up {
  border-color: rgba(220, 53, 69, 0.7);
  background: rgba(220, 53, 69, 0.2);
  color: #e8ecf2;
}

/* Per-player clear-score button (small × in the card header).
   Sized to fit alongside name + stroke chip without changing card height. */
.score-sheet-clearbtn {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  padding: 0;
  margin-left: auto;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.score-sheet-clearbtn:hover {
  background: rgba(220, 53, 69, 0.18);
  border-color: rgba(220, 53, 69, 0.55);
  color: #fff;
}
.score-sheet-clearbtn:active {
  transform: scale(0.94);
}
:root[data-theme="light"] .score-sheet-clearbtn {
  color: #475569;
  border-color: rgba(0, 0, 0, 0.18);
}
:root[data-theme="light"] .score-sheet-clearbtn:hover {
  background: rgba(220, 53, 69, 0.12);
  border-color: rgba(220, 53, 69, 0.55);
  color: #b91c1c;
}

.score-sheet-card-controls {
  --score-sheet-control-size: 56px;
  display: grid;
  grid-template-columns: 1fr var(--score-sheet-control-size) 1fr;
  gap: 8px;
  align-items: end;
}

.score-sheet-score-col {
  display: grid;
  gap: 4px;
  width: var(--score-sheet-control-size);
  justify-self: center;
}

.score-sheet-gross-label {
  min-height: 28px;
  padding-right: 0;
  font-size: calc(10px + var(--font-size-step));
  line-height: 1.15;
  font-weight: 700;
  color: var(--muted);
  text-align: center;
  white-space: normal;
  overflow: visible;
  text-overflow: clip;
}
.score-sheet-stepbtn {
  min-height: 56px;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: var(--panel-alt);
  color: var(--ink);
  font-size: calc(30px + var(--font-size-step));
  font-weight: 700;
  cursor: pointer;
}
.score-sheet-stepbtn-sm {
  width: 100%;
  min-height: var(--score-sheet-control-size);
  height: var(--score-sheet-control-size);
  font-size: calc(24px + var(--font-size-step));
}

.score-sheet .score-sheet-player-input {
  width: var(--score-sheet-control-size);
  min-height: var(--score-sheet-control-size);
  height: var(--score-sheet-control-size);
  appearance: textfield;
  -moz-appearance: textfield;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--ink);
  padding: 0;
  text-align: center;
  font-size: calc(24px + var(--font-size-step));
  font-weight: 700;
  line-height: 1;
}
.score-sheet .score-sheet-player-input::-webkit-outer-spin-button,
.score-sheet .score-sheet-player-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.score-sheet-footer {
  display: block;
  /* Android WebView reports env(safe-area-inset-bottom) as 0 even when the
     nav bar is taking space — layout-sync.js writes --safe-bottom-inset
     with the measured native inset, so prefer that. Falls back to env()
     for iOS/Web. Without this the OK button lands under the Android nav
     bar and can't be tapped. */
  padding: 10px 14px calc(10px + var(--safe-bottom-inset, env(safe-area-inset-bottom, 0px)));
  border-top: 2px solid var(--line);
  background: var(--panel-alt);
}
.score-sheet-footer .btn {
  min-height: 44px;
  width: 100%;
}

@media (max-width: 480px) {
  .score-sheet-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (min-width: 720px) {
  .score-sheet-header {
    grid-template-columns: 69px 1fr 69px 69px;
    gap: 14px;
    padding: 21px 24px;
  }

  .score-sheet-title {
    font-size: calc(35px + var(--font-size-step));
  }

  .score-sheet-navbtn,
  .score-sheet-close {
    min-height: 69px;
    border-radius: 14px;
    font-size: calc(31px + var(--font-size-step));
  }

  .score-sheet-body {
    padding: 21px 24px;
  }

  .score-sheet-hole-card {
    gap: 0;
    border-radius: 0;
    padding: 0;
  }

  .score-sheet-hole-stat {
    border-radius: 0;
    padding: 14px;
  }

  .score-sheet-meta-label {
    font-size: calc(17px + var(--font-size-step));
    margin-bottom: 7px;
  }

  .score-sheet-meta-value {
    font-size: calc(31px + var(--font-size-step));
  }

  .score-sheet-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 17px;
    margin-top: 21px;
  }

  .score-sheet-player-card {
    padding: 21px;
    border-radius: 17px;
  }

  .score-sheet-card-head {
    gap: 14px;
    margin-bottom: 14px;
  }

  .score-sheet-card-name {
    font-size: calc(31px + var(--font-size-step));
  }

  .score-sheet-card-stroke {
    min-height: 41px;
    padding: 4px 10px;
    font-size: calc(20px + var(--font-size-step));
  }

  .score-sheet-gross-label {
    font-size: calc(20px + var(--font-size-step));
    min-height: 56px;
    padding-right: 0;
    line-height: 1.15;
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
  }

  .score-sheet-card-controls {
    --score-sheet-control-size: 97px;
    gap: 17px;
  }

  .score-sheet-score-col {
    gap: 7px;
  }

  .score-sheet-stepbtn-sm {
    font-size: calc(48px + var(--font-size-step));
  }

  .score-sheet .score-sheet-player-input {
    border-radius: 17px;
    font-size: calc(52px + var(--font-size-step));
  }

  .score-sheet-footer {
    padding: 17px 24px calc(17px + var(--safe-bottom-inset, env(safe-area-inset-bottom, 0px)));
  }

  .score-sheet-footer .btn {
    min-height: 76px;
    font-size: calc(31px + var(--font-size-step));
  }
}

@media (min-width: 520px) and (max-width: 719px) {
  .score-sheet-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

/* =========================================================================
   Short viewports (phone landscape). Without this the sheet body gets
   ~120px while its content is ~700px: the tall sticky HOLE/PAR/HCP card
   fills the whole visible area and every player stepper sits off-screen,
   so the sheet looks empty/broken. Compact the chrome (header, hole card,
   footer) and shrink the stepper geometry so a full row of player cards
   fits. max-height gate only — portrait phones are ≥568px tall and never
   match, desktop windows shorter than 520px aren't a supported layout.
   ========================================================================= */
@media (max-height: 520px) {
  .score-sheet {
    /* var(--safe-top-inset) covers Android native, where env() resolves to 0
       and the status bar would otherwise overlap the header's nav/close row. */
    max-height: calc(100dvh - max(var(--safe-top-inset, 0px), env(safe-area-inset-top, 0px), 8px));
  }

  .score-sheet-header {
    grid-template-columns: 34px 1fr 34px 34px;
    gap: 6px;
    padding: 4px 10px;
    border-bottom-width: 1px;
  }

  .score-sheet-title {
    font-size: var(--text-md);
  }

  .score-sheet-navbtn,
  .score-sheet-close {
    min-height: 30px;
    font-size: calc(14px + var(--font-size-step));
  }

  .score-sheet-body {
    padding: 6px 10px;
  }

  /* One-line hole strip instead of the tall three-cell card. */
  .score-sheet-hole-stat {
    display: flex;
    align-items: baseline;
    justify-content: center;
    gap: 6px;
    padding: 2px 8px;
  }

  .score-sheet-meta-label {
    margin-bottom: 0;
    font-size: calc(10px + var(--font-size-step));
  }

  .score-sheet-meta-value {
    font-size: var(--text-md);
  }

  /* Four players side by side — each card is narrow but the stepper row
     still clears 40px touch targets. */
  .score-sheet-grid {
    margin-top: 6px;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }

  .score-sheet-player-card {
    padding: 6px;
  }

  .score-sheet-card-head {
    margin-bottom: 4px;
  }

  .score-sheet-card-name {
    font-size: var(--text-md);
  }

  .score-sheet-card-stroke {
    min-height: 22px;
    padding: 2px 6px;
    font-size: calc(11px + var(--font-size-step));
  }

  .score-sheet-card-controls {
    --score-sheet-control-size: 44px;
    gap: 4px;
  }

  .score-sheet-stepbtn-sm {
    font-size: calc(20px + var(--font-size-step));
  }

  .score-sheet .score-sheet-player-input {
    font-size: calc(20px + var(--font-size-step));
  }

  .score-sheet-gross-label {
    min-height: 0;
  }

  .score-sheet-footer {
    padding: 4px 10px calc(4px + var(--safe-bottom-inset, env(safe-area-inset-bottom, 0px)));
    border-top-width: 1px;
  }

  .score-sheet-footer .btn {
    min-height: 36px;
  }
}

/* Wide-but-short (landscape phones ≥720px wide): the sheet renders as the
   centered dialog from the min-width:720px block above. Tighten its outer
   margins so the compact layout gets the few extra rows of height. */
@media (min-width: 720px) and (max-height: 520px) {
  .score-sheet {
    bottom: 8px;
    width: min(980px, calc(100vw - 16px));
    max-height: calc(100dvh - max(var(--safe-top-inset, 0px), env(safe-area-inset-top, 0px), 16px));
  }
}

/* =========================================================================
   Light theme overrides — moved from light-theme-overrides.css
   ========================================================================= */

:root[data-theme="light"] .score-sheet-backdrop {
  /* [perf] This near-zero background used to be a deliberate no-op — the dim
     was entirely the base rule's backdrop-filter blur, now removed. Give
     light theme its own visible flat dim instead of inheriting nothing. */
  background: rgba(15, 23, 42, 0.12);
}

:root[data-theme="light"] .score-sheet {
  border-top-color: rgba(21, 32, 43, 0.18);
}

:root[data-theme="light"] .score-sheet.is-open {
  box-shadow: 0 -10px 28px rgba(15, 23, 42, 0.14);
}

:root[data-theme="light"] .score-sheet-header {
  background: #f8fafc;
}

:root[data-theme="light"] .score-sheet-navbtn,
:root[data-theme="light"] .score-sheet-close {
  background: #ffffff;
  border-color: var(--shadow-navy);
  color: #0f172a;
}

:root[data-theme="light"] .score-sheet-hole-card {
  background: var(--panel-alt);
  border-top-color: rgba(0, 0, 0, 0.3);
  border-bottom-color: rgba(0, 0, 0, 0.3);
  border-left-color: rgba(0, 0, 0, 0.15);
  box-shadow: none;
}

:root[data-theme="light"] .score-sheet-hole-stat {
  background: transparent;
  border-left-color: rgba(0, 0, 0, 0.15);
}

:root[data-theme="light"] .score-sheet-player-card {
  background: #ffffff;
  border-color: rgba(21, 32, 43, 0.16);
  box-shadow: 0 3px 10px rgba(15, 23, 42, 0.08);
}

:root[data-theme="light"] .score-sheet-gross-label {
  color: #475569;
}

:root[data-theme="light"] .score-sheet-stepbtn {
  background: #ffffff;
  border-color: var(--shadow-navy);
}

:root[data-theme="light"] .score-sheet .score-sheet-player-input {
  background: #ffffff;
  border-color: var(--shadow-navy);
}

:root[data-theme="light"] .score-sheet-card-stroke.is-down {
  color: #4b5563;
}

:root[data-theme="light"] .score-sheet-card-stroke.is-up {
  color: #4b5563;
}

:root[data-theme="light"] .score-sheet-footer {
  background: rgba(248, 250, 252, 0.94);
}

/* Light-theme variant of the score-sheet tile look. Same load-bearing reason
   as the dark block at line 16: must override .score-input.receives-stroke /
   .gives-stroke border-colors so cells render as plain white tiles in
   score-sheet mode regardless of handicap-stroke state. */
@media (pointer: coarse) {
  :root[data-theme="light"] body.score-sheet-active #scorecard .score-input {
    border-color: rgba(0, 0, 0, 0.08) !important;
    background: #ffffff !important;
  }
}
