/* ============================================
   CHRONOS — Race Timing Dashboard
   ============================================ */

/* ============================================
   CUSTOM PROPERTIES
   ============================================ */
:root {
    --c-bg: #0f1117;
    --c-surface: #181c27;
    --c-surface2: #1f2433;
    --c-border: rgba(255, 255, 255, 0.08);
    --c-border-act: rgba(255, 0, 0, 0.5);

    --c-primary: #ff0000;
    --c-primary-dk: #cc0000;
    --c-secondary: #004e89;
    --c-success: #00d084;
    --c-warning: #ffb703;
    --c-danger: #e63946;
    --c-danger-dk: #c1121f;

    --c-text: #e8eaf0;
    --c-text-muted: #8892a4;
    --c-white: #ffffff;

    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    --radius-pill: 9999px;

    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;
    --space-7: 48px;

    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
    --shadow-xl: 0 16px 40px rgba(0, 0, 0, 0.6);

    --font-sans: "Segoe UI", system-ui, -apple-system, sans-serif;
    --font-mono: "JetBrains Mono", "Fira Code", "Courier New", monospace;

    --transition: 150ms ease;
    --transition-slow: 300ms ease;
}

/* ============================================
   RESET & BASE
   ============================================ */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    scroll-behavior: smooth;
}

body {
    font-family: var(--font-sans);
    background-color: var(--c-bg);
    color: var(--c-text);
    line-height: 1.6;
    min-height: 100dvh;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
}

/* ============================================
   UTILITIES
   ============================================ */
.mono {
    font-family: var(--font-mono);
}
.hidden {
    display: none !important;
}

/* ============================================
   MODAL
   ============================================ */
.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 900;
    padding: var(--space-4);
    animation: fadeIn var(--transition-slow) both;
}

.modal.hidden {
    display: none;
}

.modal-card {
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-xl);
    padding: var(--space-6);
    width: 100%;
    max-width: 420px;
    box-shadow: var(--shadow-xl);
    animation: slideUp var(--transition-slow) both;
}

.modal-card h2 {
    font-size: 1.375rem;
    font-weight: 700;
    color: var(--c-white);
    margin-bottom: var(--space-3);
}

.modal-info {
    font-size: 0.875rem;
    color: var(--c-text-muted);
    background: var(--c-surface2);
    border-radius: var(--radius-md);
    padding: var(--space-3) var(--space-4);
    margin-bottom: var(--space-5);
    line-height: 1.5;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin-bottom: var(--space-5);
}

.form-group label {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--c-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.6px;
}

.form-group input {
    width: 100%;
    padding: var(--space-3) var(--space-4);
    background: var(--c-bg);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-md);
    color: var(--c-white);
    font-family: var(--font-mono);
    font-size: 0.9rem;
    transition:
        border-color var(--transition),
        box-shadow var(--transition);
}

.form-group input:focus {
    outline: none;
    border-color: var(--c-primary);
    box-shadow: 0 0 0 3px rgba(255, 0, 0, 0.2);
}

.field-error {
    font-size: 0.8125rem;
    color: var(--c-danger);
    min-height: 1.2em;
}

.modal-actions {
    display: flex;
    gap: var(--space-3);
}

/* ============================================
   FAB (Floating Action Button)
   ============================================ */
.fab {
    position: fixed;
    bottom: var(--space-5);
    right: var(--space-5);
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: none;
    background: var(--c-primary);
    color: var(--c-white);
    font-size: 1.25rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-md);
    transition:
        background var(--transition),
        transform var(--transition);
    z-index: 800;
}

.fab:hover {
    background: var(--c-primary-dk);
    transform: scale(1.08);
}

/* ============================================
   BUTTONS
   ============================================ */
.btn {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-4);
    border: none;
    border-radius: var(--radius-md);
    font-family: var(--font-sans);
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition:
        background var(--transition),
        transform var(--transition),
        box-shadow var(--transition);
}

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

.btn-primary {
    background: var(--c-primary);
    color: var(--c-white);
}
.btn-primary:hover {
    background: var(--c-primary-dk);
    box-shadow: var(--shadow-sm);
}

.btn-confirm {
    background: var(--c-success);
    color: #002b1a;
    flex: 1;
}
.btn-confirm:hover {
    filter: brightness(1.1);
}

.btn-danger {
    background: var(--c-danger);
    color: var(--c-white);
    flex: 1;
}
.btn-danger:hover {
    background: var(--c-danger-dk);
}

.btn-ghost {
    background: transparent;
    color: var(--c-text-muted);
    border: 1px solid var(--c-border);
}
.btn-ghost:hover {
    background: var(--c-surface2);
    color: var(--c-text);
    border-color: rgba(255, 255, 255, 0.15);
}

/* ============================================
   SHELL LAYOUT
   ============================================ */
.shell {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
}

/* ============================================
   HEADER
   ============================================ */
.site-header {
    background: linear-gradient(135deg, #0d1b2a 0%, #1a2744 50%, #0d1b2a 100%);
    border-bottom: 1px solid var(--c-border);
    position: sticky;
    top: 0;
    z-index: 200;
    box-shadow: var(--shadow-md);
}

.header-inner {
    max-width: 1920px;
    margin: 0 auto;
    padding: var(--space-4) var(--space-5);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-5);
}

.brand {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    flex-shrink: 0;
}

.brand-icon {
    font-size: 1.75rem;
    line-height: 1;
}

.brand-name {
    font-size: 1.625rem;
    font-weight: 800;
    letter-spacing: 3px;
    color: var(--c-white);
    text-transform: uppercase;
    background: linear-gradient(90deg, #fff 0%, var(--c-primary) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

/* Status bar */
.status-bar {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    flex-wrap: wrap;
}

.status-chip {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-pill);
    padding: var(--space-1) var(--space-3);
    font-size: 0.8125rem;
}

.status-label {
    color: var(--c-text-muted);
    font-weight: 500;
}

.status-value {
    color: var(--c-white);
    font-weight: 700;
}

/* Status dot */
.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--c-danger);
    flex-shrink: 0;
    transition: background var(--transition-slow);
}

.status-dot.connected {
    background: var(--c-success);
    box-shadow: 0 0 6px var(--c-success);
    animation: pulse 2s infinite;
}

#statusText {
    font-weight: 600;
    font-size: 0.8125rem;
    color: var(--c-danger);
    transition: color var(--transition-slow);
}

#statusText.connected {
    color: var(--c-success);
}

/* ============================================
   MAIN
   ============================================ */
.site-main {
    flex: 1;
    max-width: 1920px;
    width: 100%;
    margin: 0 auto;
    padding: var(--space-5);
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
}

/* ============================================
   CONTROLS BAR
   ============================================ */
.controls-bar {
    display: flex;
    align-items: flex-end;
    gap: var(--space-4);
    flex-wrap: wrap;
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-lg);
    padding: var(--space-4) var(--space-5);
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}

.control-group label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--c-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.ctrl-input {
    padding: var(--space-2) var(--space-3);
    background: var(--c-bg);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-md);
    color: var(--c-text);
    font-family: var(--font-mono);
    font-size: 0.875rem;
    min-width: 160px;
    transition:
        border-color var(--transition),
        box-shadow var(--transition);
    appearance: none;
    -webkit-appearance: none;
}

select.ctrl-input {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%238892a4' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 10px center;
    padding-right: 28px;
    cursor: pointer;
}

.ctrl-input:focus {
    outline: none;
    border-color: var(--c-primary);
    box-shadow: 0 0 0 3px rgba(255, 0, 0, 0.15);
}

.ctrl-input::placeholder {
    color: var(--c-text-muted);
}

.control-actions {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin-left: auto;
}

/* ============================================
   TABLE SECTION
   ============================================ */
.table-section {
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-lg);
    overflow: hidden;
}

.table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.race-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

/* Sticky header */
.race-table thead {
    position: sticky;
    top: 0;
    z-index: 10;
    background: var(--c-surface2);
    border-bottom: 2px solid var(--c-primary);
}

.race-table th {
    padding: var(--space-3) var(--space-4);
    text-align: left;
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.6px;
    color: var(--c-text-muted);
    white-space: nowrap;
}

.race-table tbody tr {
    border-bottom: 1px solid var(--c-border);
    transition: background var(--transition);
}

.race-table tbody tr:last-child {
    border-bottom: none;
}

.race-table tbody tr:hover {
    background: rgba(255, 0, 0, 0.06);
}

.race-table td {
    padding: var(--space-3) var(--space-4);
    font-family: var(--font-mono);
    font-size: 0.8125rem;
    white-space: nowrap;
}

/* New row flash */
.race-table tbody tr.new-row {
    animation: rowFlash 0.6s ease both;
}

/* Column widths */
.col-pos {
    width: 48px;
    text-align: center;
    color: var(--c-text-muted);
}
.col-trans {
    width: 140px;
    font-weight: 700;
    color: var(--c-primary);
}
.col-bib {
    width: 80px;
    text-align: center;
}
.col-loop {
    width: 80px;
    text-align: center;
}
.col-time {
    width: 160px;
}
.col-delta {
    width: 110px;
}
.col-signal {
    width: 90px;
    text-align: center;
}
.col-batt {
    width: 90px;
    text-align: center;
}

/* ── Badges ──────────────────────────── */
.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: var(--radius-pill);
    font-size: 0.75rem;
    font-weight: 600;
    font-family: var(--font-mono);
}

/* signal */
.badge-signal {
    background: rgba(0, 208, 132, 0.15);
    color: var(--c-success);
}
.badge-signal.low {
    background: rgba(255, 183, 3, 0.15);
    color: var(--c-warning);
}
.badge-signal.very-low {
    background: rgba(230, 57, 70, 0.15);
    color: var(--c-danger);
}

/* battery */
.badge-battery {
    background: rgba(0, 208, 132, 0.15);
    color: var(--c-success);
}
.badge-battery.medium {
    background: rgba(255, 183, 3, 0.15);
    color: var(--c-warning);
}
.badge-battery.low {
    background: rgba(230, 57, 70, 0.15);
    color: var(--c-danger);
}

/* delta time */
.delta-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: var(--radius-pill);
    background: rgba(255, 0, 0, 0.12);
    color: var(--c-primary);
    font-family: var(--font-mono);
    font-size: 0.8rem;
    font-weight: 600;
}

.delta-badge.first {
    background: transparent;
    color: var(--c-text-muted);
}

/* ── Empty state ──────────────────────── */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-3);
    padding: var(--space-7) var(--space-5);
    color: var(--c-text-muted);
}

.empty-state.hidden {
    display: none;
}

.empty-icon {
    font-size: 2.5rem;
    opacity: 0.6;
    animation: float 3s ease-in-out infinite;
}

.empty-state p {
    font-size: 1rem;
}

/* ============================================
   PANELS (Stats / Debug)
   ============================================ */
.panel {
    background: var(--c-surface);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-lg);
    padding: var(--space-5);
}

.panel-title {
    font-size: 1rem;
    font-weight: 700;
    color: var(--c-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: var(--space-4);
}

/* Debug panel toggle */
.panel-debug {
    padding: 0;
    overflow: hidden;
}

.panel-toggle {
    width: 100%;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-4) var(--space-5);
    background: none;
    border: none;
    color: var(--c-text-muted);
    font-family: var(--font-sans);
    font-size: 0.875rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    cursor: pointer;
    transition:
        color var(--transition),
        background var(--transition);
    text-align: left;
}

.panel-toggle:hover {
    color: var(--c-text);
    background: var(--c-surface2);
}

.toggle-chevron {
    margin-left: auto;
    transition: transform var(--transition-slow);
    font-size: 0.75rem;
}

.panel-toggle[aria-expanded="true"] .toggle-chevron {
    transform: rotate(90deg);
}

.panel-body {
    padding: var(--space-4) var(--space-5) var(--space-5);
    border-top: 1px solid var(--c-border);
}

/* Stats grid */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: var(--space-4);
}

.stat-card {
    background: var(--c-surface2);
    border: 1px solid var(--c-border);
    border-radius: var(--radius-md);
    padding: var(--space-4);
    text-align: center;
    transition:
        border-color var(--transition),
        transform var(--transition);
}

.stat-card:hover {
    border-color: var(--c-border-act);
    transform: translateY(-2px);
}

.stat-value {
    font-size: 1.875rem;
    font-weight: 800;
    color: var(--c-primary);
    line-height: 1.1;
    margin-bottom: var(--space-1);
}

.stat-label {
    font-size: 0.75rem;
    color: var(--c-text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* ============================================
   FOOTER
   ============================================ */
.site-footer {
    border-top: 1px solid var(--c-border);
    padding: var(--space-4) var(--space-5);
    text-align: center;
    font-size: 0.8125rem;
    color: var(--c-text-muted);
}

/* ============================================
   ANIMATIONS
   ============================================ */
@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes rowFlash {
    0% {
        background: rgba(255, 0, 0, 0.18);
    }
    100% {
        background: transparent;
    }
}

@keyframes pulse {
    0%,
    100% {
        box-shadow: 0 0 0 0 rgba(0, 208, 132, 0.5);
    }
    50% {
        box-shadow: 0 0 0 5px rgba(0, 208, 132, 0);
    }
}

@keyframes float {
    0%,
    100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-6px);
    }
}

/* ============================================
   SCROLLBAR
   ============================================ */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}
::-webkit-scrollbar-track {
    background: var(--c-surface);
}
::-webkit-scrollbar-thumb {
    background: var(--c-surface2);
    border-radius: var(--radius-pill);
    border: 2px solid var(--c-surface);
}
::-webkit-scrollbar-thumb:hover {
    background: var(--c-primary);
}

/* ============================================
   RESPONSIVE — 1024px
   ============================================ */
@media (max-width: 1024px) {
    .header-inner {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-3);
    }

    .status-bar {
        width: 100%;
    }

    .control-actions {
        margin-left: 0;
    }
}

/* ============================================
   RESPONSIVE — 768px
   ============================================ */
@media (max-width: 768px) {
    html {
        font-size: 15px;
    }

    .site-main {
        padding: var(--space-4);
        gap: var(--space-3);
    }

    .controls-bar {
        flex-direction: column;
        align-items: stretch;
        gap: var(--space-3);
        padding: var(--space-4);
    }

    .control-group {
        width: 100%;
    }
    .ctrl-input {
        width: 100%;
        min-width: unset;
    }

    .control-actions {
        display: grid;
        grid-template-columns: 1fr 1fr 1fr;
        gap: var(--space-2);
        margin-left: 0;
    }

    .control-actions .btn {
        justify-content: center;
    }

    /* Hide signal & battery on mobile — cols 7 & 8 */
    .col-signal,
    .col-batt,
    .race-table th:nth-child(7),
    .race-table th:nth-child(8),
    .race-table td:nth-child(7),
    .race-table td:nth-child(8) {
        display: none;
    }

    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ============================================
   RESPONSIVE — 480px
   ============================================ */
@media (max-width: 480px) {
    html {
        font-size: 14px;
    }

    .brand-name {
        font-size: 1.25rem;
        letter-spacing: 2px;
    }

    /* Also hide loop col (col 4) */
    .col-loop,
    .race-table th:nth-child(4),
    .race-table td:nth-child(4) {
        display: none;
    }

    .stats-grid {
        grid-template-columns: 1fr;
    }

    .control-actions {
        grid-template-columns: 1fr 1fr;
    }
    .control-actions .btn:last-child {
        grid-column: span 2;
    }

    .race-table {
        min-width: 420px;
    }
}

/* ============================================
   PRINT
   ============================================ */
@media print {
    .modal,
    .fab,
    .controls-bar,
    .site-footer,
    .panel-debug {
        display: none !important;
    }

    .site-header {
        background: #fff;
        color: #000;
        print-color-adjust: exact;
    }

    .race-table {
        break-inside: avoid;
    }
}
