/*
 * Microframe Sports — tennis court scoring page, v2
 * Install at: /wwwroot/css/tennis-court.css
 * Replaces the inline <style> block on the court scoring view. Everything is scoped under .court-app.
 * Colors come from the site's existing --mfs-* variables (site.css); the fallbacks are only for the preview.
 */
.court-app {
    --ct-home: var(--mfs-primary, #1b2d5c);
    --ct-guest: var(--mfs-secondary, #a8323e);
    --ct-accent: var(--mfs-accent, #2f5dff);
    --ct-text: var(--mfs-text, #1e1e24);
    --ct-muted: var(--mfs-muted, #686c74);
    --ct-bg: var(--mfs-bg, #f3f3f1);
    --ct-line: #dcdbd8;
    --ct-ball: #d4e34a;
    --ct-live: #1f8a3b;
    --ct-warn: #985a00;   /* ≥ 4.5:1 on white for the small "Saving…" text */
    --ct-error: #c0262d;
    --ct-cell-h: 88px;

    max-width: 860px;
    margin: 0 auto;
    min-height: calc(100vh - 56px);
    min-height: calc(100dvh - 56px);   /* iOS: 100vh includes the browser bars */
    display: flex;
    flex-direction: column;
    color: var(--ct-text);
    background: var(--ct-bg);
}

.ct-sr {
    position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.court-app button { font: inherit; }
.court-app :focus-visible { outline: 3px solid var(--ct-accent); outline-offset: 2px; }

/* ── Top row: back link, connection status, options ── */
.ct-nav {
    display: flex;
    align-items: center;
    gap: .75rem;
    padding: .9rem 1.25rem .25rem;
}
.ct-back {
    color: var(--ct-muted);
    font-size: .9rem;
    font-weight: 600;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: .3rem;
    min-height: 44px;
}
.ct-back:hover { color: var(--ct-home); }
.ct-back svg { width: 16px; height: 16px; }

.ct-status {
    margin-left: auto;
    display: inline-flex;
    align-items: center;
    gap: .45rem;
    font-size: .82rem;
    font-weight: 700;
    color: var(--ct-muted);
    padding: .3rem .7rem;
    border-radius: 999px;
    background: #fff;
    border: 1px solid var(--ct-line);
}
.ct-status-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--ct-muted); }
.ct-status.is-live { color: var(--ct-live); }
.ct-status.is-live .ct-status-dot { background: var(--ct-live); }
.ct-status.is-saving { color: var(--ct-warn); }
.ct-status.is-saving .ct-status-dot { background: var(--ct-warn); animation: ct-blink 1s ease-in-out infinite; }
.ct-status.is-error { color: var(--ct-error); border-color: var(--ct-error); }
.ct-status.is-error .ct-status-dot { background: var(--ct-error); }
@keyframes ct-blink { 50% { opacity: .25; } }

.ct-icon-btn {
    width: 44px; height: 44px;
    border: none; background: none; border-radius: 10px;
    color: var(--ct-muted);
    display: inline-flex; align-items: center; justify-content: center;
    cursor: pointer;
}
.ct-icon-btn:hover { background: rgba(0,0,0,.06); color: var(--ct-text); }
.ct-icon-btn svg { width: 22px; height: 22px; }

/* ── Court name ── */
.ct-title { padding: .25rem 1.25rem .75rem; text-align: center; }
#courtName {
    width: 100%;
    font-size: clamp(1.6rem, 6vw, 2.8rem);
    font-weight: 800;
    color: var(--ct-home);
    text-align: center;
    border: none;
    background: transparent;
    padding: .1rem .25rem;
    border-radius: 8px;
}
#courtName:hover { background: rgba(0,0,0,.03); }
#courtName:focus { background: #fff; outline: 2px solid var(--ct-accent); }

/* ── Match finished banner ── */
.ct-banner {
    margin: 0 1.25rem .75rem;
    padding: .85rem 1rem;
    border-radius: 10px;
    background: #e8f4eb;
    border: 1px solid #b9dcc2;
    color: #14562a;
    font-weight: 600;
    text-align: center;
}

/* ── Scoreboard grid ── */
.ct-board {
    --sets: 3;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 104px repeat(var(--sets), 88px);
    gap: 6px;
    padding: 0 1.25rem 1.25rem;
    align-items: stretch;
}
.ct-board [hidden] { display: none !important; }

.ct-h {
    background: var(--ct-home);
    color: #fff;
    font-size: .78rem;
    font-weight: 700;
    letter-spacing: .03em;
    text-align: center;
    padding: 7px 4px;
    border-radius: 6px;
    white-space: nowrap;
    overflow: hidden;
}
.ct-h-name { text-align: left; padding-left: 16px; }
.ct-board.is-tiebreak .ct-h-game { background: var(--ct-live); }

.ct-name {
    background: #fff;
    border-radius: 10px;
    border-left: 6px solid var(--ct-home);
    display: flex;
    align-items: center;
    min-height: var(--ct-cell-h);
    min-width: 0;
    box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.ct-name.ct-p1 { border-left-color: var(--ct-guest); }

.ct-serve {
    flex-shrink: 0;
    width: 44px; height: 44px;
    margin-left: 4px;
    border: none; background: none; border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    cursor: pointer;
}
.ct-serve:disabled { cursor: default; }   /* read-only viewers: shows who serves, can't change it */
.ct-ball {
    width: 20px; height: 20px; border-radius: 50%;
    border: 2px dashed #b8bab3;
    transition: background .2s, border-color .2s, transform .2s;
}
.ct-serve.is-serving .ct-ball {
    background: var(--ct-ball);
    border: 2px solid #9fb02a;
    transform: scale(1.15);
}
@media (prefers-reduced-motion: no-preference) {
    .ct-serve.is-serving .ct-ball { animation: ct-serve 1.6s ease-in-out infinite; }
}
@keyframes ct-serve { 50% { transform: scale(1.3); } }

.ct-name-input {
    flex: 1;
    min-width: 0;
    border: none;
    background: transparent;
    font-size: clamp(1.1rem, 3.4vw, 1.6rem);
    font-weight: 700;
    color: var(--ct-home);
    padding: .5rem .6rem .5rem .25rem;
    border-radius: 6px;
}
.ct-p1 .ct-name-input { color: var(--ct-guest); }
.ct-name-input::placeholder { color: inherit; opacity: .55; }
.ct-name-input:focus { background: rgba(0,0,0,.035); outline: 2px solid var(--ct-accent); }

.ct-cell {
    min-height: var(--ct-cell-h);
    border: 1.5px solid var(--ct-line);
    border-radius: 10px;
    background: #fff;
    color: var(--ct-text);
    font-size: clamp(1.6rem, 5vw, 2.8rem);
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    line-height: 1;
    display: flex; align-items: center; justify-content: center;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.ct-cell:disabled { cursor: default; opacity: 1; }
.ct-cell.ct-game { background: #f4f9e6; border-color: #c5dc86; color: #3f5a10; }
.ct-board.is-tiebreak .ct-cell.ct-game { background: #e6f4ea; border-color: #8cc79d; color: var(--ct-live); }
.ct-cell.is-current { border: 2px solid var(--ct-home); }
.ct-cell.is-won { color: var(--ct-home); background: #eef1f8; }

/* Adjust mode: cells become tappable */
.ct-board.is-adjusting .ct-cell { cursor: pointer; border-style: dashed; }
.ct-board.is-adjusting .ct-cell:hover { border-color: var(--ct-accent); }
.ct-cell.is-selected {
    border: 3px solid var(--ct-accent) !important;
    background: #eef2ff !important;
    color: var(--ct-accent) !important;
}

/* ── Controls ── */
.ct-controls {
    margin-top: auto;
    background: #fff;
    border-top: 1px solid var(--ct-line);
    padding: 1rem 1.25rem 1.25rem;
    display: flex;
    flex-direction: column;
    gap: .75rem;
}

.ct-points { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }

.ct-point {
    min-height: 96px;
    border: none;
    border-radius: 14px;
    color: #fff;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: .15rem;
    padding: .5rem .75rem;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    user-select: none;
    box-shadow: 0 4px 0 rgba(0,0,0,.25);
    transition: transform .08s, box-shadow .08s, filter .15s;
}
.ct-point.ct-p0 { background: var(--ct-home); }
.ct-point.ct-p1 { background: var(--ct-guest); }
.ct-point:hover { filter: brightness(1.08); }
.ct-point:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,.25); }
.ct-point:disabled { filter: grayscale(1); opacity: .45; cursor: not-allowed; transform: none; }
.ct-point-name {
    font-size: clamp(1.25rem, 4.2vw, 1.75rem);
    font-weight: 800;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.ct-point-sub { font-size: .85rem; font-weight: 600; opacity: .8; }

.ct-secondary { display: flex; gap: .75rem; }
.ct-ghost {
    flex: 1;
    min-height: 48px;
    border: 1.5px solid var(--ct-line);
    border-radius: 10px;
    background: #fff;
    color: var(--ct-text);
    font-weight: 700;
    cursor: pointer;
    display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
}
.ct-ghost:hover { border-color: var(--ct-muted); }
.ct-ghost:disabled { color: #aaa; cursor: not-allowed; border-color: #eee; }
.ct-ghost svg { width: 18px; height: 18px; }
.ct-ghost[aria-expanded="true"] { background: var(--ct-text); color: #fff; border-color: var(--ct-text); }

.ct-adjust {
    border: 1.5px dashed var(--ct-accent);
    border-radius: 12px;
    padding: .75rem;
    background: #f6f8ff;
}
.ct-adjust-hint { margin: 0 0 .6rem; font-size: .88rem; color: var(--ct-muted); text-align: center; }
.ct-adjust-row { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: .6rem; }
.ct-step {
    min-height: 56px;
    border: none;
    border-radius: 10px;
    font-size: 1.6rem;
    font-weight: 800;
    cursor: pointer;
    background: var(--ct-text);
    color: #fff;
    touch-action: manipulation;
}
.ct-step.ct-up { background: var(--ct-accent); }
.ct-tb {
    min-height: 56px;
    border: 1.5px solid var(--ct-line);
    border-radius: 10px;
    background: #fff;
    color: var(--ct-muted);
    font-weight: 800;
    cursor: pointer;
}
.ct-tb[aria-pressed="true"] { background: var(--ct-live); border-color: var(--ct-live); color: #fff; }

.ct-keys { margin: 0; text-align: center; font-size: .8rem; color: var(--ct-muted); }
.ct-keys kbd {
    font: inherit; font-weight: 700; padding: 0 .35rem;
    border: 1px solid var(--ct-line); border-radius: 4px; background: var(--ct-bg);
}

/* ── Options dialog ── */
.ct-dialog {
    border: none;
    border-radius: 16px;
    padding: 0;
    width: min(440px, calc(100vw - 2rem));
    color: var(--ct-text);
    box-shadow: 0 20px 60px rgba(0,0,0,.3);
}
.ct-dialog::backdrop { background: rgba(20,20,30,.45); }
.ct-dialog-body { padding: 1.25rem 1.25rem 1rem; }
.ct-dialog-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: .5rem; }
.ct-dialog h2 { font-size: 1.2rem; font-weight: 800; margin: 0; }
.ct-dialog fieldset { border: none; padding: 0; margin: 0 0 1rem; }
.ct-dialog legend { font-weight: 700; font-size: .95rem; margin-bottom: .35rem; }
.ct-radio {
    display: flex; align-items: flex-start; gap: .6rem;
    padding: .55rem .6rem;
    border-radius: 8px;
    cursor: pointer;
}
.ct-radio:hover { background: var(--ct-bg); }
.ct-radio input { margin-top: .25rem; width: 18px; height: 18px; accent-color: var(--ct-home); }
.ct-radio small { display: block; color: var(--ct-muted); }
.ct-note { font-size: .82rem; color: var(--ct-muted); margin: 0 0 1rem; }
.ct-dialog-actions { display: flex; flex-direction: column; gap: .5rem; border-top: 1px solid var(--ct-line); padding-top: 1rem; }
.ct-danger {
    min-height: 48px; border-radius: 10px; border: 1.5px solid var(--ct-error);
    background: #fff; color: var(--ct-error); font-weight: 700; cursor: pointer;
}
.ct-danger:hover { background: #fdf0f0; }
.ct-link {
    display: block; padding: .6rem; border-radius: 8px;
    color: var(--ct-text); text-decoration: none; font-weight: 600; text-align: center;
}
.ct-link:hover { background: var(--ct-bg); }

/* ── "Not saved" banner ── */
.ct-stale {
    position: fixed; top: 0; left: 0; right: 0; z-index: 2000;
    display: flex; align-items: center; gap: .75rem;
    padding: .75rem 1rem;
    padding-top: calc(.75rem + env(safe-area-inset-top, 0px));
    background: #fff3cd; color: #664d03; font-weight: 600;
    border-bottom: 1px solid #e6cf7a;
}
.ct-stale button {
    margin-left: auto; flex-shrink: 0;
    border: none; border-radius: 8px; padding: .5rem 1rem;
    background: #f0b400; color: #3a2c00; font-weight: 800; cursor: pointer;
}

/* ── Optional sections kept from the old view (radar courts, tournament courts) ── */
.court-app .radar-section {
    background: #fff; border-top: 1px solid var(--ct-line); padding: 1rem 1.5rem;
    display: flex; flex-direction: column; align-items: center; gap: .75rem;
}
.court-app .radar-speed {
    font-size: clamp(4rem, 15vw, 7rem); font-weight: 900; color: #e63946;
    line-height: 1; font-variant-numeric: tabular-nums;
}
.court-app .radar-mph { color: rgba(230,57,70,.6); font-size: 1.4rem; font-weight: 600; }
.court-app .radar-controls { display: flex; flex-wrap: wrap; gap: 1rem; justify-content: center; }
.court-app .radar-ctrl label { color: var(--ct-muted); font-size: .82rem; margin-bottom: .2rem; display: block; }
.court-app .radar-ctrl select, .court-app .radar-ctrl input[type=range] { max-width: 200px !important; border-radius: 5px; }
.court-app .done-section { background: #fff; border-top: 1px solid var(--ct-line); padding: .75rem 1.5rem; text-align: center; }
.court-app .btn-done {
    background: #28a745; color: #fff; border: none; border-radius: 10px;
    padding: .75rem 3rem; font-size: 1.1rem; font-weight: 700; cursor: pointer;
}
.court-app .btn-done:hover { background: #218838; }

/* ── Phones ── */
@media (max-width: 640px) {
    .court-app { --ct-cell-h: 60px; }
    .ct-nav { padding: .5rem .75rem 0; gap: .25rem; }
    .ct-title { padding: 0 .75rem .5rem; }
    .ct-banner { margin: 0 .5rem .5rem; }
    .ct-board {
        grid-template-columns: minmax(0, 1fr) 62px repeat(var(--sets), 46px);
        gap: 4px;
        padding: 0 .5rem .75rem;
    }
    .ct-h { font-size: .66rem; padding: 5px 2px; letter-spacing: 0; }
    .ct-h-name { padding-left: 10px; }
    .ct-name { border-left-width: 5px; }
    .ct-serve { width: 36px; height: 36px; margin-left: 0; }
    .ct-ball { width: 16px; height: 16px; }
    .ct-name-input { font-size: 1.05rem; padding: .35rem .3rem .35rem 0; }
    .ct-cell { font-size: clamp(1.25rem, 6.5vw, 1.8rem); }

    /* Point buttons stay within thumb reach at the bottom of the screen */
    .ct-controls {
        position: sticky;
        bottom: 0;
        z-index: 10;
        padding: .75rem .75rem calc(.75rem + env(safe-area-inset-bottom, 0px));
        gap: .6rem;
        box-shadow: 0 -6px 18px rgba(0,0,0,.08);
    }
    .ct-points { gap: .5rem; }
    .ct-point { min-height: 84px; border-radius: 12px; }
    .ct-ghost { min-height: 44px; }
    .ct-keys { display: none; }
}

@media (hover: none) { .ct-keys { display: none; } }
