/* =====================================================================
   Score desk — Run › Scores. Match rail + one match grid, autosaving
   game boxes and the inline frame strip. Uses the --bo-* tokens and the
   --bo-hub-* status colors from tournament-hub.css.
   ===================================================================== */

:root,
html.bo-light {
    --bo-sd-error: #B42318;
    --bo-sd-error-soft: #FDECEA;
}

html.bo-dark {
    --bo-sd-error: #FF8A80;
    --bo-sd-error-soft: rgba(255, 138, 128, 0.12);
}

.sd {
    display: grid;
    grid-template-columns: 250px minmax(0, 1fr);
    gap: 12px;
    align-items: start;
}

.sd-panel {
    background: var(--bo-surface-solid);
    border: 1px solid var(--bo-border);
    border-radius: var(--bo-radius-lg);
}

.sd-empty {
    padding: 24px 16px;
    color: var(--bo-text-muted);
    font-size: 0.85rem;
    text-align: center;
}

.sd-spacer {
    flex: 1;
}

.sd-visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

kbd,
.sd kbd {
    font: 500 0.68rem/1.4 inherit;
    padding: 0 5px;
    border: 1px solid var(--bo-border-strong);
    border-radius: 4px;
    background: var(--bo-surface-solid);
    color: var(--bo-text-muted);
}

/* ---- Rail ------------------------------------------------------------ */

.sd-rail {
    position: sticky;
    top: 12px;
    background: var(--bo-surface-solid);
    border: 1px solid var(--bo-border);
    border-radius: var(--bo-radius-lg);
    overflow: hidden;
}

.sd-rail__head {
    padding: 8px 10px;
    border-bottom: 1px solid var(--bo-border);
}

.sd-select {
    width: 100%;
    height: 30px;
    padding: 0 8px;
    border: 1px solid var(--bo-border-strong);
    border-radius: var(--bo-radius);
    background: var(--bo-surface-solid);
    color: var(--bo-text);
    font: inherit;
    font-size: 0.82rem;
}

    .sd-select:focus-visible {
        outline: 2px solid var(--bo-primary);
        outline-offset: 1px;
    }

.sd-rail__progress {
    padding: 8px 12px 10px;
    border-bottom: 1px solid var(--bo-border);
    font-size: 0.75rem;
    color: var(--bo-text-muted);
}

.sd-bar {
    height: 4px;
    margin-top: 6px;
    border-radius: 2px;
    background: var(--bo-border-strong);
    overflow: hidden;
}

    .sd-bar > span {
        display: block;
        height: 100%;
        background: var(--bo-primary);
    }

.sd-rail__list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.sd-match {
    display: grid;
    grid-template-columns: 54px minmax(0, 1fr) auto;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 8px 12px;
    border: 0;
    border-bottom: 1px solid var(--bo-border);
    background: transparent;
    color: var(--bo-text);
    font: inherit;
    text-align: left;
    cursor: pointer;
}

    .sd-match:hover {
        background: var(--bo-hub-row-hover);
    }

    .sd-match.is-selected {
        background: var(--bo-primary-soft);
        box-shadow: inset 3px 0 0 var(--bo-primary);
    }

    .sd-match:focus-visible {
        outline: 2px solid var(--bo-primary);
        outline-offset: -2px;
    }

.sd-match__lane {
    font-size: 0.75rem;
    color: var(--bo-text-muted);
    font-variant-numeric: tabular-nums;
}

.sd-match__teams {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.sd-match__team {
    font-size: 0.82rem;
    font-weight: 500;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sd-match__sub {
    font-size: 0.7rem;
    color: var(--bo-text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sd-chip {
    padding: 1px 7px;
    border-radius: 999px;
    background: var(--bo-surface-muted);
    color: var(--bo-text-muted);
    font-size: 0.68rem;
    font-weight: 500;
    white-space: nowrap;
}

    .sd-chip.is-done {
        background: var(--bo-hub-ok-soft);
        color: var(--bo-hub-ok);
    }

    .sd-chip.is-partial {
        background: var(--bo-hub-live-soft);
        color: var(--bo-hub-live);
    }

/* ---- Main ------------------------------------------------------------ */

.sd-main {
    display: flex;
    flex-direction: column;
    gap: 12px;
    min-width: 0;
}

.sd-head {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px 18px;
    padding: 10px 14px;
}

.sd-head__title {
    margin: 0;
    font-size: 0.98rem;
    font-weight: 600;
}

.sd-head__vs {
    font-weight: 400;
    color: var(--bo-text-muted);
}

.sd-head__meta {
    font-size: 0.75rem;
    color: var(--bo-text-muted);
}

.sd-head__total {
    display: flex;
    flex-direction: column;
    align-items: center;
    line-height: 1.15;
}

    .sd-head__total strong {
        font-size: 1.25rem;
        font-weight: 600;
        font-variant-numeric: tabular-nums;
    }

    .sd-head__total span {
        font-size: 0.68rem;
        color: var(--bo-text-muted);
    }

    .sd-head__total.is-muted strong {
        color: var(--bo-text-muted);
    }

.sd-savestate {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 0.75rem;
    white-space: nowrap;
}

    .sd-savestate::before {
        content: "";
        width: 7px;
        height: 7px;
        border-radius: 50%;
        background: currentColor;
    }

    .sd-savestate.is-saved {
        color: var(--bo-hub-ok);
    }

    .sd-savestate.is-saving {
        color: var(--bo-primary);
    }

    .sd-savestate.is-error {
        color: var(--bo-sd-error);
    }

.sd-team {
    overflow-x: auto;
}

.sd-team__head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 7px 12px;
    background: var(--bo-surface-muted);
    border-bottom: 1px solid var(--bo-border);
    font-weight: 600;
    font-size: 0.85rem;
}

.sd-team__sum {
    margin-left: auto;
    font-variant-numeric: tabular-nums;
}

    .sd-team__sum small {
        margin-left: 6px;
        font-size: 0.72rem;
        font-weight: 400;
        color: var(--bo-text-muted);
    }

.sd-grid {
    width: 100%;
    min-width: 640px;
    table-layout: fixed;
    border-collapse: collapse;
    font-size: 0.82rem;
}

    .sd-grid th {
        padding: 6px 10px;
        border-bottom: 1px solid var(--bo-border);
        color: var(--bo-text-muted);
        font-size: 0.7rem;
        font-weight: 500;
        text-align: left;
    }

    .sd-grid td {
        padding: 5px 10px;
        border-bottom: 1px solid var(--bo-border);
        vertical-align: middle;
    }

.sd-col-pos {
    width: 24px;
    color: var(--bo-text-muted);
    font-variant-numeric: tabular-nums;
}

/* The bowler column has a set width; the game columns share whatever is left, so the
   boxes grow with the screen instead of leaving dead space on the right. */
.sd-col-name {
    width: 176px;
}

.sd-col-game {
    min-width: 128px;
}

.sd-grid td.sd-col-game {
    padding-left: 4px;
    padding-right: 4px;
}

.sd-grid .sd-cell {
    max-width: 220px;
    margin: 0 auto;
}

.sd-grid th.sd-col-game {
    text-align: center;
}

.sd-col-sum {
    width: 76px;
    text-align: right;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

    .sd-col-sum small {
        display: block;
        font-size: 0.68rem;
        font-weight: 400;
        color: var(--bo-text-muted);
    }

.sd-grid th.sd-col-sum {
    text-align: right;
}

/* Score type: one picker per bowler (all games), and optionally one under each game. */
.sd-col-type {
    width: 104px;
}

.sd-type-select {
    width: 100%;
    height: 26px;
    padding: 0 4px;
    border: 1px solid var(--bo-border);
    border-radius: var(--bo-radius);
    background: var(--bo-surface-solid);
    color: var(--bo-text);
    font: inherit;
    font-size: 0.75rem;
}

    .sd-type-select:focus-visible {
        outline: 2px solid var(--bo-primary);
        outline-offset: 1px;
    }

.sd-type-select--game {
    display: block;
    max-width: 220px;
    height: 22px;
    margin: 3px auto 0;
    color: var(--bo-text-muted);
    font-size: 0.7rem;
}

.sd-name {
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

    .sd-name small {
        display: block;
        font-size: 0.7rem;
        font-weight: 400;
        color: var(--bo-text-muted);
    }

/* ---- Game box --------------------------------------------------------- */

.sd-cell {
    position: relative;
    display: flex;
    align-items: center;
    gap: 4px;
    height: 36px;
    padding: 0 6px 0 4px;
    border: 1px solid var(--bo-border-strong);
    border-radius: var(--bo-radius);
    background: var(--bo-surface-solid);
    transition: border-color 120ms ease, box-shadow 120ms ease;
}

    .sd-cell:focus-within {
        border-color: var(--bo-primary);
        box-shadow: 0 0 0 3px var(--bo-primary-soft);
    }

    .sd-cell.is-error {
        border-color: var(--bo-sd-error);
        background: var(--bo-sd-error-soft);
    }

    .sd-cell.is-framing {
        border-color: var(--bo-primary);
    }

.sd-cell__score {
    flex: 1;
    min-width: 48px;
    height: 100%;
    padding: 0 4px;
    border: 0;
    outline: none;
    background: transparent;
    color: var(--bo-text);
    font: 600 1.02rem/1 inherit;
    font-variant-numeric: tabular-nums;
    text-align: right;
    caret-color: var(--bo-primary);
}

    .sd-cell__score::placeholder {
        color: var(--bo-text-muted);
        font-weight: 400;
    }

    .sd-cell__score::selection,
    .sd-cell__mini::selection {
        background: var(--bo-primary-soft);
    }

/* Frames toggle at the box's left edge: quiet until the box is in use, solid once the game
   has frames or its strip is open. */
.sd-cell__frames {
    flex: none;
    display: grid;
    place-items: center;
    width: 20px;
    height: 24px;
    padding: 0;
    border: 0;
    border-radius: 4px;
    background: transparent;
    color: var(--bo-text-muted);
    opacity: 0.4;
    cursor: pointer;
    transition: opacity 120ms ease, background-color 120ms ease;
}

    .sd-cell:hover .sd-cell__frames,
    .sd-cell:focus-within .sd-cell__frames,
    .sd-cell__frames:focus-visible {
        opacity: 1;
    }

    .sd-cell__frames:hover {
        background: var(--bo-hub-row-hover);
        color: var(--bo-text);
    }

    .sd-cell__frames.is-open,
    .sd-cell.is-saved .sd-cell__tag.is-frames ~ .sd-cell__frames {
        opacity: 1;
        color: var(--bo-primary);
    }

    .sd-cell__frames.is-open {
        background: var(--bo-primary-soft);
    }

    .sd-cell__frames:focus-visible {
        outline: 2px solid var(--bo-primary);
        outline-offset: 0;
    }

/* Handicap and position: a narrow stacked column at the box's right edge. */
.sd-cell__extras {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 1px;
    flex: none;
    padding-left: 6px;
    border-left: 1px solid var(--bo-border);
}

.sd-cell__extra {
    display: flex;
    align-items: center;
    gap: 1px;
    height: 15px;
}

.sd-cell__extra-label {
    width: 8px;
    font-size: 0.62rem;
    color: var(--bo-text-muted);
    text-align: center;
}

.sd-cell__mini {
    width: 24px;
    height: 15px;
    padding: 0 2px;
    border: 0;
    border-radius: 3px;
    outline: none;
    background: transparent;
    color: var(--bo-text-muted);
    font: 400 0.68rem/1 inherit;
    font-variant-numeric: tabular-nums;
    text-align: right;
}

    .sd-cell__mini:focus {
        background: var(--bo-primary-soft);
        color: var(--bo-text);
    }

.sd-cell__dot {
    flex: none;
    width: 6px;
    height: 6px;
    border-radius: 50%;
}

.sd-cell.is-saved .sd-cell__dot {
    background: var(--bo-hub-ok);
}

.sd-cell.is-saving .sd-cell__dot {
    background: var(--bo-primary);
    animation: sd-pulse 1s ease-in-out infinite;
}

.sd-cell.is-error .sd-cell__dot {
    background: var(--bo-sd-error);
}

.sd-cell__tag {
    position: absolute;
    top: -7px;
    left: 6px;
    padding: 0 5px;
    border-radius: 4px;
    background: var(--bo-hub-warn-soft);
    color: var(--bo-hub-warn);
    font-size: 0.6rem;
    font-weight: 600;
    line-height: 1.35;
}

    .sd-cell__tag.is-frames {
        background: var(--bo-primary-soft);
        color: var(--bo-primary);
    }

@keyframes sd-pulse {
    50% {
        opacity: 0.3;
    }
}

.sd-error-row td {
    padding-top: 0 !important;
    border-bottom: 0 !important;
    color: var(--bo-sd-error);
    font-size: 0.75rem;
}

/* ---- Frame strip -------------------------------------------------------- */

.sd-frames-row td {
    background: var(--bo-surface-muted);
}

.sd-strip {
    padding: 4px 2px;
    border-radius: var(--bo-radius);
    outline: none;
}

    .sd-strip:focus-visible {
        box-shadow: 0 0 0 2px var(--bo-primary);
    }

.sd-strip__frames {
    display: flex;
    align-items: stretch;
    gap: 3px;
    overflow-x: auto;
}

.sd-frame {
    display: block;
    padding: 0;
    font: inherit;
    color: inherit;
    text-align: inherit;
    cursor: default;
    width: 46px;
    border: 1px solid var(--bo-border-strong);
    border-radius: 5px;
    background: var(--bo-surface-solid);
    overflow: hidden;
}

    .sd-frame.is-tenth {
        width: 66px;
    }

    .sd-frame.is-current {
        border-color: var(--bo-primary);
        box-shadow: 0 0 0 3px var(--bo-primary-soft);
    }

    /* Bowled frames can be picked to correct. */
    .sd-frame.is-bowled {
        cursor: pointer;
    }

        .sd-frame.is-bowled:hover {
            border-color: var(--bo-primary);
        }

    .sd-frame.is-editing {
        border-color: var(--bo-hub-warn);
        box-shadow: 0 0 0 3px var(--bo-hub-warn-soft);
    }

        .sd-frame.is-editing .sd-frame__balls span.is-caret {
            background: var(--bo-hub-warn-soft);
        }

.sd-frame__no {
    display: block;
    padding: 1px 4px;
    font-size: 0.6rem;
    color: var(--bo-text-muted);
    border-bottom: 1px solid var(--bo-border);
}

.sd-frame__balls {
    display: flex;
    border-bottom: 1px solid var(--bo-border);
}

    .sd-frame__balls span {
        flex: 1;
        height: 22px;
        display: grid;
        place-items: center;
        border-left: 1px solid var(--bo-border);
        font-weight: 600;
        font-variant-numeric: tabular-nums;
    }

        .sd-frame__balls span:first-child {
            border-left: 0;
        }

        .sd-frame__balls span.is-caret {
            background: var(--bo-primary-soft);
        }

        .sd-frame__balls span.is-split {
            color: var(--bo-hub-live);
        }

.sd-frame__total {
    height: 22px;
    display: grid;
    place-items: center;
    font-size: 0.78rem;
    color: var(--bo-text-muted);
    font-variant-numeric: tabular-nums;
}

.sd-strip__score {
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 10px;
    line-height: 1.2;
}

    .sd-strip__score strong {
        font-size: 1.2rem;
        font-variant-numeric: tabular-nums;
    }

    .sd-strip__score span {
        font-size: 0.68rem;
        color: var(--bo-text-muted);
    }

.sd-strip__meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 14px;
    margin-top: 8px;
    font-size: 0.75rem;
    color: var(--bo-text-muted);
}

.sd-strip__flag {
    color: var(--bo-hub-live);
    font-weight: 500;
}

.sd-strip__warn {
    color: var(--bo-sd-error);
}

.sd-strip__keys {
    margin-left: auto;
}

/* ---- Keys + footer ------------------------------------------------------ */

.sd-keys {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 16px;
    padding: 8px 12px;
    font-size: 0.75rem;
    color: var(--bo-text-muted);
}

.sd-foot {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 14px;
}

.sd-foot__note {
    font-size: 0.75rem;
    color: var(--bo-text-muted);
}

.sd-result {
    padding: 2px 9px;
    border-radius: 999px;
    background: var(--bo-surface-muted);
    color: var(--bo-text-muted);
    font-size: 0.75rem;
    font-weight: 500;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

    .sd-result.is-current {
        background: var(--bo-hub-live-soft);
        color: var(--bo-hub-live);
    }

    .sd-result.is-final {
        background: var(--bo-hub-ok-soft);
        color: var(--bo-hub-ok);
    }

    .sd-result.is-stale {
        background: color-mix(in srgb, var(--bo-sd-error) 12%, transparent);
        color: var(--bo-sd-error);
    }

@media (max-width: 1100px) {
    .sd {
        grid-template-columns: minmax(0, 1fr);
    }

    .sd-rail {
        position: static;
    }
}

@media (prefers-reduced-motion: reduce) {
    .sd-cell {
        transition: none;
    }

    .sd-cell.is-saving .sd-cell__dot {
        animation: none;
    }
}
