:root {
    --bg: #ece9e2;
    --card: #ffffff;
    --ink: #1b2430;
    --muted: #6b7686;
    --line: #e2ddd2;
    --accent: #e40520;
    --accent-dark: #b80319;
    --navy: #0f1b2d;
    --navy-2: #16283f;
    --ok: #1c7c3c;
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 16px;
    line-height: 1.45;
    -webkit-text-size-adjust: 100%;
    overscroll-behavior-y: none;
}

#app {
    min-height: 100dvh;
}

/* ---------------- boot loading + error UI ---------------- */

.loading-progress {
    position: relative;
    display: block;
    width: 8rem;
    height: 8rem;
    margin: 6rem auto;
}

.loading-progress circle {
    fill: none;
    stroke: #e0ddd5;
    stroke-width: 0.6rem;
    stroke-dasharray: 251.2;
    animation: loading 1.5s ease-in-out infinite;
    stroke-linecap: round;
    transform-origin: 50% 50%;
}

.loading-progress circle:last-of-type {
    stroke: var(--accent);
    stroke-dasharray: 100 400;
    animation: loading 1s ease-in-out infinite;
}

.loading-progress-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 0.9rem;
    color: var(--muted);
    text-align: center;
    width: max-content;
}

@keyframes loading {
    0% {
        stroke-dashoffset: 0;
        transform: rotate(0);
    }
    100% {
        stroke-dashoffset: -502;
        transform: rotate(360deg);
    }
}

#blazor-error-ui {
    color-scheme: light only;
    display: none;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1000;
    padding: 0.8rem 1rem;
    background: #fff3cd;
    color: #664d00;
    border-top: 2px solid #e2b100;
    font-size: 0.95rem;
}

#blazor-error-ui .reload {
    color: inherit;
}

#blazor-error-ui .dismiss {
    cursor: pointer;
    float: right;
}

@media (prefers-color-scheme: dark) {
    #blazor-error-ui {
        background: #3a2f05;
        color: #ffe28a;
        border-top-color: #8a6d00;
    }
}

.app {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

/* ---------------- topbar (GPS clock) ---------------- */

.topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 56px;
    background: var(--navy);
    color: #e8edf5;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1rem;
    z-index: 50;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.topbar .brand {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 700;
    font-size: 1.05rem;
    letter-spacing: 0.02em;
}

.topbar .gps {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.1rem;
}

.gps-clock {
    font-family: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    font-variant-numeric: tabular-nums;
}

.gps-status {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.75rem;
    color: #a0aec0;
}

.gps-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: currentColor;
}

.gps-status.good {
    color: #34d399;
}

.gps-status.stale {
    color: #fbbf24;
}

.gps-status.bad {
    color: #f87171;
}

.gps-status.none {
    color: #6b7280;
}

.gps-text {
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-weight: 600;
}

/* ---------------- sidebar / bottom-nav ---------------- */

.sidebar {
    position: fixed;
    left: 0;
    top: 56px;
    bottom: 0;
    width: 220px;
    height: calc(100dvh - 56px);
    background: linear-gradient(180deg, var(--navy) 0%, var(--navy-2) 100%);
    color: #e8edf5;
    display: flex;
    flex-direction: column;
    z-index: 40;
}

.navlinks {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    padding: 0.8rem 0.6rem 0.5rem;
}

.navlink {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    padding: 0.7rem 0.8rem;
    border-radius: 10px;
    color: #c6d0de;
    text-decoration: none;
    font-weight: 500;
    min-height: 48px;
    cursor: pointer;
}

.navlink .ico {
    font-size: 1.1rem;
}

.navlink:hover {
    background: rgba(255, 255, 255, 0.08);
    color: #fff;
}

.navlink.active,
.navlink.active:hover {
    background: var(--accent);
    color: #fff;
}

.content {
    flex: 1;
    min-width: 0;
    margin-left: 220px;
    margin-top: 56px;
    display: flex;
    flex-direction: column;
}

.page {
    padding: 1.25rem;
    flex: 1;
    max-width: 1180px;
    width: 100%;
    margin: 0 auto;
}

.foot {
    text-align: center;
    color: var(--muted);
    font-size: 0.8rem;
    padding: 0.9rem 1rem 2.2rem;
}

/* ---------------- update banner ---------------- */

.update-banner {
    position: fixed;
    top: 56px;
    left: 220px;
    right: 0;
    z-index: 45;
    background: var(--accent);
    color: #fff;
    padding: 0.6rem 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    font-size: 0.95rem;
    font-weight: 600;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
}

.update-btn {
    background: #fff;
    color: var(--accent);
    border: none;
    border-radius: 8px;
    padding: 0.35rem 0.75rem;
    font-weight: 700;
    cursor: pointer;
}

.update-btn:hover {
    background: #f3f4f6;
}

@media (max-width: 899px) {
    .update-banner {
        left: 0;
        top: calc(56px + env(safe-area-inset-top));
        padding: 0.55rem 0.8rem;
    }

    .update-banner span {
        font-size: 0.9rem;
    }

    .update-btn {
        padding: 0.4rem 0.7rem;
    }
}

/* ---------------- dashboard grid ---------------- */

.dashboard {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
}

.single {
    display: grid;
    grid-template-columns: minmax(0, 620px);
    justify-content: center;
}

.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 1rem 1.1rem 1.1rem;
    box-shadow: 0 1px 2px rgba(20, 28, 40, 0.05);
}

.card-title {
    margin: 0 0 0.85rem;
    font-size: 1.05rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.subtitle {
    margin: 0.6rem 0 0.4rem;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--muted);
}

.hint {
    color: var(--muted);
    font-size: 0.9rem;
    margin: 0 0 0.7rem;
}

.dim {
    color: var(--muted);
    font-weight: 400;
}

hr {
    border: none;
    border-top: 1px solid var(--line);
    margin: 1rem 0;
}

/* ---------------- form fields ---------------- */

.field {
    margin-bottom: 0.65rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    min-width: 0;
}

.field label,
.lbl {
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.input {
    width: 100%;
    padding: 0.6rem 0.7rem;
    font-size: 1rem;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #faf9f6;
    color: var(--ink);
    min-height: 46px;
}

.input:focus {
    outline: 2px solid var(--accent);
    outline-offset: -1px;
    background: #fff;
}

.input.center {
    text-align: center;
}

.input.big {
    font-size: 1.25rem;
    font-weight: 600;
}

.row-2, .row-3 {
    display: grid;
    gap: 0.6rem;
}

.row-2 {
    grid-template-columns: 1fr 1fr;
}

.row-3 {
    grid-template-columns: repeat(3, 1fr);
}

.quick {
    margin-top: 0.9rem;
    align-items: flex-start;
}

.check {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.95rem;
    margin: 0.7rem 0;
    cursor: pointer;
    min-height: 42px;
}

.check input {
    width: 1.25rem;
    height: 1.25rem;
    accent-color: var(--accent);
}

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

.btn {
    min-height: 46px;
    padding: 0.6rem 1rem;
    font-size: 0.95rem;
    font-weight: 600;
    border-radius: 10px;
    border: 1px solid var(--line);
    background: #f6f4ef;
    color: var(--ink);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    touch-action: manipulation;
    user-select: none;
}

.btn:hover {
    background: #ece9e2;
}

.btn:active {
    transform: translateY(1px);
}

.btn.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.btn.primary:hover {
    background: var(--accent-dark);
}

.btn.block {
    width: 100%;
}

.btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.btn-row {
    display: flex;
    gap: 0.6rem;
    margin: 0.55rem 0;
}

.btn-row .btn {
    flex: 1;
}

/* ---------------- clocks & pairs ---------------- */

.clock {
    font-family: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
    font-size: 2.6rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    text-align: center;
    padding: 0.55rem 0;
    color: var(--ink);
    font-variant-numeric: tabular-nums;
}

.clock.circuit {
    font-size: 3rem;
}

.clock.live {
    color: var(--accent);
}

.pair {
    display: flex;
    justify-content: space-between;
    gap: 0.5rem;
    font-size: 0.95rem;
    padding: 0.25rem 0;
    border-bottom: 1px dashed var(--line);
}

.pair span {
    color: var(--muted);
}

.pair.small strong {
    color: var(--ok);
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.55; }
}

/* ---------------- error box & table ---------------- */

.error-box {
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #faf9f6;
    margin-bottom: 0.7rem;
    padding: 0.6rem;
}

.error-box summary {
    cursor: pointer;
    font-weight: 600;
    font-size: 0.95rem;
    padding: 0.2rem 0.3rem;
    min-height: 36px;
}

.error-box[open] summary {
    margin-bottom: 0.5rem;
}

.target-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.55rem 0.7rem;
    margin: 0.7rem 0 0.4rem;
    background: var(--navy);
    color: #fff;
    border-radius: 10px;
    font-family: ui-monospace, Menlo, Consolas, monospace;
}

.target-bar .target {
    font-size: 1.05rem;
    font-weight: 700;
    letter-spacing: 0.03em;
}

.target-bar .seg {
    font-size: 0.9rem;
    opacity: 0.85;
}

.table-wrap {
    border: 1px solid var(--line);
    border-radius: 10px;
    overflow: hidden;
    max-height: min(52vh, 560px);
    display: flex;
    flex-direction: column;
}

.thead {
    display: grid;
    grid-template-columns: 0.55fr 1fr;
    background: var(--navy);
    color: #cfe0f3;
    font-size: 0.78rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    padding: 0.5rem 0.8rem;
    position: sticky;
    top: 0;
}

.tbody {
    overflow-y: auto;
    flex: 1;
}

.trow {
    display: grid;
    grid-template-columns: 0.55fr 1fr;
    padding: 0.55rem 0.8rem;
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-variant-numeric: tabular-nums;
    cursor: pointer;
    border-top: 1px solid var(--line);
}

.trow:hover {
    background: #f4f1ea;
}

.trow.on {
    background: rgba(228, 5, 32, 0.10);
    box-shadow: inset 3px 0 0 var(--accent);
    font-weight: 700;
}

/* ---------------- stage timer ---------------- */

.stage-compare {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.7rem;
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 1.15rem;
    font-weight: 700;
    padding: 0.4rem 0 0.5rem;
}

.stage-compare .sep {
    color: var(--muted);
    font-weight: 400;
}

.progress {
    height: 26px;
    background: #ece9e2;
    border: 1px solid var(--line);
    border-radius: 999px;
    overflow: hidden;
    margin: 0.3rem 0 0.7rem;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--accent-dark), var(--accent));
    border-radius: 999px;
    transition: width 0.4s linear;
}

/* ---------------- advanced error ---------------- */

.pairs {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 0.5rem;
    margin-bottom: 0.7rem;
}

.pair-inputs {
    display: grid;
    grid-template-columns: 1fr 1fr 3.2rem;
    gap: 0.4rem;
    align-items: center;
}

.ratio {
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-weight: 700;
    text-align: center;
    color: var(--muted);
}

/* ---------------- calcnote ---------------- */

.ops {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.5rem;
    margin: 0.4rem 0 0.7rem;
}

.btn.op {
    font-size: 1.35rem;
    min-height: 60px;
    background: var(--navy);
    color: #fff;
    border-color: var(--navy);
}

.btn.op:hover {
    background: var(--navy-2);
}

.result {
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 1.6rem;
    font-weight: 700;
    text-align: right;
    padding: 0.7rem 0.9rem;
    border-radius: 10px;
    background: #faf9f6;
    border: 1px solid var(--line);
    margin-bottom: 0.7rem;
    min-height: 3.6rem;
    color: var(--muted);
    word-break: break-all;
}

.result.has {
    color: var(--ink);
}

.note {
    width: 100%;
    min-height: 260px;
    padding: 0.7rem;
    font-size: 1rem;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #faf9f6;
    resize: vertical;
    font-family: inherit;
}

.lbl {
    display: block;
    margin: 0.8rem 0 0.35rem;
}

/* ---------------- lap list ---------------- */

.lap-list {
    margin-top: 0.7rem;
}

.lap-list .thead {
    grid-template-columns: 0.4fr 1fr 1fr;
}

.lap-list .trow {
    grid-template-columns: 0.4fr 1fr 1fr;
    cursor: default;
}

.lap-list .trow:hover {
    background: initial;
}

/* ---------------- responsiveness ---------------- */

@media (max-width: 899px) {
    .sidebar {
        top: auto;
        right: 0;
        width: 100%;
        height: auto;
        min-height: 64px;
        background: var(--navy-2);
        border-top: 1px solid rgba(255, 255, 255, 0.1);
        padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    }

    .topbar {
        height: 56px;
        padding: 0 0.8rem;
        padding-top: env(safe-area-inset-top);
        height: calc(56px + env(safe-area-inset-top));
    }

    .topbar .brand-name {
        display: none;
    }

    .topbar .gps-clock {
        font-size: 1.15rem;
    }

    .navlinks {
        flex-direction: row;
        justify-content: space-around;
        padding: 0.4rem 0.5rem;
        gap: 0.2rem;
    }

    .navlink {
        flex: 1;
        flex-direction: column;
        gap: 0.1rem;
        padding: 0.4rem 0.3rem;
        min-height: 56px;
        border-radius: 10px;
        font-size: 0.78rem;
    }

    .navlink .ico {
        font-size: 1.25rem;
    }

    .content {
        margin-left: 0;
        margin-top: calc(56px + env(safe-area-inset-top));
    }

    .page {
        padding: 0.9rem 0.8rem 5.6rem;
    }

    .foot {
        padding-bottom: 4.6rem;
    }

    .clock {
        font-size: 2.1rem;
    }

    .clock.circuit {
        font-size: 2.4rem;
    }
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14181f;
        --card: #1c222d;
        --ink: #e6eaf1;
        --muted: #96a2b3;
        --line: #2b3542;
        --navy: #0b1320;
        --navy-2: #131d2e;
        --ok: #53d17a;
    }

    .input, .note, .result, .error-box, .pair-inputs {
        background: #161c26;
        color: var(--ink);
        border-color: var(--line);
    }

    .input:focus {
        background: #1b2330;
    }

    .btn {
        background: #232c3a;
        border-color: #2f3a4a;
        color: var(--ink);
    }

    .btn:hover {
        background: #2c3747;
    }

    .trow:hover {
        background: #222b38;
    }

    .trow.on {
        background: rgba(228, 5, 32, 0.22);
    }

    .progress {
        background: #0f1420;
    }

    .check input {
        accent-color: var(--accent);
    }
}