* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-light: #ffffff;
    --bg-panel: #f5f5f5;
    --bg-card: #ffffff;
    --lugano-blue: #3d5a9c;
    --lugano-blue-light: #5a7bc4;
    --lugano-blue-dark: #2d4275;
    --porsche-red: #d5001c;
    --text-dark: #1a1a1a;
    --text-medium: #4a4a4a;
    --text-light: #888888;
    --border: #e0e0e0;
    --shadow: rgba(0, 0, 0, 0.08);
}

/* Porsche-style font */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-light);
    color: var(--text-dark);
    height: 100vh;
    overflow: hidden;
}

.screen {
    height: 100vh;
    width: 100vw;
}

.hidden {
    display: none !important;
}

/* Login Screen */
#login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    background: url('/static/debtstar.webp') no-repeat center 15%;
    background-size: cover;
    background-color: #000;
}

/* Mobile: use portrait-oriented image */
@media (max-width: 768px) {
    #login-screen {
        background: url('/static/debtstar-mobile.webp') no-repeat center center;
        background-size: cover;
        background-color: #000;
    }
}

.login-overlay {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.4);
}

.login-content {
    text-align: center;
    padding: 2.5rem 3rem;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(10px);
}

.login-content h1 {
    font-size: 1.6rem;
    font-weight: 600;
    margin-bottom: 1.5rem;
    color: var(--text-dark);
    letter-spacing: 0.5px;
}

.login-content h1 .gold {
    color: #c9a227;
    font-weight: 700;
}

#login-form {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    align-items: stretch;
}

#login-form .remember-me {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    font-size: 0.85rem;
    color: var(--text-medium);
}

#login-form .remember-me label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
}

#login-form .remember-me input[type="checkbox"] {
    width: auto;
    margin: 0;
}

#login-form input[type="text"],
#login-form input[type="password"] {
    padding: 0.8rem 1rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-panel);
    color: var(--text-dark);
    font-size: 0.95rem;
    width: 100%;
    min-width: 250px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

#login-form input:focus {
    outline: none;
    border-color: var(--lugano-blue);
    box-shadow: 0 0 0 3px rgba(61, 90, 156, 0.1);
}

#login-form button {
    padding: 0.8rem 1rem;
    background: var(--lugano-blue);
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
    width: 100%;
    margin-top: 0.5rem;
}

#login-form button:hover {
    background: var(--lugano-blue-light);
}

#login-form button:active {
    transform: scale(0.98);
}

.error {
    color: #d32f2f;
    margin-top: 1rem;
    font-size: 0.9rem;
}

/* Dashboard Layout */
#dashboard-screen {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background: var(--bg-panel);
}

header {
    display: flex;
    align-items: center;
    gap: 2rem;
    padding: 0.8rem 1.5rem;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    box-shadow: 0 2px 8px var(--shadow);
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.header-brand img {
    height: 40px;
    width: auto;
}

header h1 {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--lugano-blue);
    white-space: nowrap;
    letter-spacing: 0.3px;
}

header .vehicle-subtitle {
    font-size: 0.75rem;
    color: var(--text-light);
    font-weight: 400;
    margin-top: 2px;
}

.trip-selector {
    display: flex;
    gap: 0.5rem;
}

.trip-selector select {
    padding: 0.5rem 1rem;
    background: var(--bg-panel);
    color: var(--text-dark);
    border: 1px solid var(--border);
    border-radius: 6px;
    min-width: 450px;
    max-width: 600px;
    font-size: 0.9rem;
}

.trip-selector select:focus {
    outline: none;
    border-color: var(--lugano-blue);
}

.trip-selector button {
    padding: 0.5rem 0.8rem;
    background: var(--bg-panel);
    color: var(--text-medium);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    transition: background 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 0.85rem;
}

.trip-selector button:hover {
    background: var(--border);
}

.trip-selector button .refresh-icon {
    display: inline-block;
    line-height: 1;
}

.trip-selector.loading button .refresh-icon {
    animation: spin 0.8s linear infinite;
}

/* Desktop: icon-only is fine, but label gives clarity for new users.
   Mobile shows the full "Reload" word — see media query. */
.trip-selector button .refresh-label {
    font-weight: 500;
}

.trip-info {
    color: var(--text-light);
    font-size: 0.85rem;
    font-weight: 400;
}

/* Main Content - Video-first layout */
main.video-first-layout {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding: 0.75rem;
    overflow: hidden;
}

.top-row {
    display: flex;
    gap: 0.75rem;
    flex: 0 0 auto;
    height: 45vh;
    min-height: 280px;
    max-height: 450px;
}

.video-panel {
    flex: 3;
    position: relative;
    background: #000;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 12px var(--shadow);
}

.video-panel .video-container {
    height: 100%;
    border-radius: 12px;
    position: relative;
}

.map-panel-small {
    flex: 1;
    min-width: 250px;
    max-width: 350px;
    background: var(--bg-card);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 12px var(--shadow);
    border: 1px solid var(--border);
}

.map-panel-small #map {
    width: 100%;
    height: 100%;
}

.graphs-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg-card);
    border-radius: 12px;
    border: 1px solid var(--border);
    box-shadow: 0 4px 12px var(--shadow);
    min-height: 200px;
}

/* Camera Toggle Buttons */
.camera-toggle {
    display: flex;
    gap: 4px;
}

.camera-btn {
    padding: 4px 10px;
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 4px;
    color: white;
    font-size: 11px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.camera-btn:hover {
    background: rgba(255, 255, 255, 0.3);
}

.camera-btn.active {
    background: var(--lugano-blue);
    border-color: var(--lugano-blue);
}

.volume-controls {
    display: flex;
    align-items: center;
    gap: 6px;
}

/* Video controls - updated layout */
.video-panel .video-controls {
    position: absolute;
    bottom: 12px;
    left: 12px;
    right: 12px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(0, 0, 0, 0.7);
    padding: 8px 12px;
    border-radius: 8px;
    opacity: 0;
    transition: opacity 0.2s;
}

.video-panel:hover .video-controls {
    opacity: 1;
}

/* Speed overlay on video */
.speed-overlay {
    position: absolute;
    top: 12px;
    right: 12px;
    background: rgba(0, 0, 0, 0.7);
    padding: 8px 16px;
    border-radius: 8px;
    display: flex;
    align-items: baseline;
    gap: 4px;
}

.speed-overlay #current-speed {
    font-size: 2rem;
    font-weight: 700;
    color: white;
}

.speed-overlay .speed-unit {
    font-size: 0.9rem;
    color: rgba(255, 255, 255, 0.7);
    font-weight: 500;
}

/* Frame scrubbing overlay */
.frame-overlay {
    position: absolute;
    top: 12px;
    left: 12px;
    background: rgba(0, 0, 0, 0.85);
    padding: 8px 14px;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    z-index: 20;
    border: 1px solid var(--lugano-blue);
}

.frame-overlay.hidden {
    display: none;
}

.frame-overlay #frame-number {
    font-size: 1.1rem;
    font-weight: 600;
    color: white;
    font-family: 'Inter', monospace;
}

.frame-overlay .frame-timestamp {
    font-size: 0.85rem;
    color: #7dd3fc;
    font-family: 'Inter', monospace;
}

.frame-overlay .frame-calib {
    font-size: 0.75rem;
    color: #fbbf24;  /* Yellow for calibration info */
    font-family: 'Inter', monospace;
    margin-top: 4px;
    padding-top: 4px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.frame-overlay .frame-hint {
    font-size: 0.7rem;
    color: rgba(255, 255, 255, 0.6);
}

/* Legacy layout support */
main:not(.video-first-layout) {
    flex: 1;
    display: flex;
    gap: 1rem;
    padding: 1rem;
    overflow: hidden;
}

.panel {
    background: var(--bg-card);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 12px var(--shadow);
    border: 1px solid var(--border);
}

.map-panel {
    flex: 1;
    position: relative;
    min-width: 400px;
}

#map {
    width: 100%;
    height: 100%;
}

.map-overlay {
    position: absolute;
    top: 12px;
    right: 12px;
    background: rgba(255, 255, 255, 0.95);
    padding: 0.8rem 1.2rem;
    border-radius: 10px;
    z-index: 1000;
    box-shadow: 0 4px 12px var(--shadow);
    border: 1px solid var(--border);
}

#current-speed {
    font-size: 2rem;
    font-weight: 700;
    color: var(--lugano-blue);
}

.data-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    min-width: 400px;
}

.video-container {
    height: 220px;
    background: #000;
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid var(--border);
    position: relative;
}

/* Dual video elements for seamless camera switching */
.dashcam-video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: #000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease-in-out;
}

.dashcam-video.active {
    opacity: 1;
    pointer-events: auto;
}

/* Side-by-side view mode */
.video-container.side-by-side .dashcam-video {
    width: 50%;
    opacity: 1;
    pointer-events: auto;
}

.video-container.side-by-side #dashcam-video-front {
    left: 0;
}

.video-container.side-by-side #dashcam-video-rear {
    left: 50%;
}

/* Labels for side-by-side view */
.video-container.side-by-side::before,
.video-container.side-by-side::after {
    position: absolute;
    top: 8px;
    font-size: 11px;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.8);
    background: rgba(0, 0, 0, 0.5);
    padding: 2px 8px;
    border-radius: 4px;
    z-index: 10;
}

.video-container.side-by-side::before {
    content: 'FRONT';
    left: 8px;
}

.video-container.side-by-side::after {
    content: 'REAR';
    left: calc(50% + 8px);
}

/* In side-by-side mode, hide the shared overlay (per-camera overlays handle status) */
.video-container.side-by-side .video-overlay {
    display: none;
}

/* Per-camera status overlays */
.video-overlay-cam {
    position: absolute;
    top: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.85);
    color: var(--text-light);
    font-size: 13px;
    z-index: 5;
    pointer-events: none;
}

.video-overlay-cam.hidden {
    display: none;
}

/* Side-by-side: each overlay covers its camera half */
.video-container.side-by-side .video-overlay-cam.cam-front {
    left: 0;
    width: 50%;
}

.video-container.side-by-side .video-overlay-cam.cam-rear {
    left: 50%;
    width: 50%;
}

/* Toggle mode: overlay covers full width */
.video-container:not(.side-by-side) .video-overlay-cam {
    left: 0;
    width: 100%;
}

/* View mode toggle buttons */
.view-mode-toggle {
    display: flex;
    gap: 4px;
    margin-right: 8px;
}

.view-btn {
    padding: 4px 8px;
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 4px;
    color: white;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.2s;
}

.view-btn:hover {
    background: rgba(255, 255, 255, 0.3);
}

.view-btn.active {
    background: var(--lugano-blue);
    border-color: var(--lugano-blue);
}

/* Legacy single video support */
#dashcam-video {
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: #000;
}

.video-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-panel);
    color: var(--text-light);
    font-size: 14px;
}

.video-overlay.hidden {
    display: none;
}

.video-controls {
    position: absolute;
    bottom: 8px;
    right: 8px;
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(0, 0, 0, 0.6);
    padding: 4px 8px;
    border-radius: 6px;
    opacity: 0;
    transition: opacity 0.2s;
}

.video-container:hover .video-controls {
    opacity: 1;
}

.mute-btn {
    background: none;
    border: none;
    font-size: 14px;
    cursor: pointer;
    padding: 2px;
}

#volume-slider {
    width: 60px;
    height: 4px;
    cursor: pointer;
}

.graphs-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg-card);
    border-radius: 12px;
    border: 1px solid var(--border);
}

.graph-controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem 0.8rem;
    padding: 0.7rem 1rem;
    background: var(--bg-panel);
    border-bottom: 1px solid var(--border);
}

#channel-checkboxes {
    display: contents;
}

.graph-controls label {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.8rem;
    color: var(--text-medium);
    cursor: pointer;
    font-weight: 400;
}

.graph-controls input[type="checkbox"] {
    cursor: pointer;
    accent-color: var(--lugano-blue);
}

#graphs {
    flex: 1;
    min-height: 120px;
    position: relative;
    overflow: hidden;
}

/* Floating "you are zoomed in" pill — visible whenever zoom != Full so the
   user can always tap to bail out (no hunting for the small ↺ button). */
.zoom-exit-pill {
    position: absolute;
    top: 8px;
    right: 12px;
    z-index: 30;
    background: var(--lugano-blue);
    color: white;
    border: none;
    border-radius: 999px;
    padding: 6px 12px;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
    display: inline-flex;
    align-items: center;
    gap: 6px;
    line-height: 1;
}

.zoom-exit-pill:hover {
    background: var(--lugano-blue-light);
}

.zoom-exit-pill:active {
    transform: scale(0.96);
}

.zoom-exit-pill.hidden {
    display: none;
}

.zoom-exit-pill .zoom-exit-x {
    font-size: 0.95rem;
    opacity: 0.85;
}

/* The graphs-panel needs to be a positioning context so the pill can pin
   to the top-right of the chart area. */
.graphs-panel {
    position: relative;
}

/* Allow overflow for clip timeline (parked sessions) */
#graphs:has(.clip-timeline) {
    overflow: visible;
}

.control-timezone {
    margin-right: 0.5rem;
}

.control-timezone select {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--bg-card);
    color: var(--text-medium);
    cursor: pointer;
}

.control-timezone select:hover {
    border-color: var(--lugano-blue);
}

.control-zoom {
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.zoom-btn {
    width: 26px;
    height: 24px;
    background: var(--bg-card);
    color: var(--text-medium);
    border: 1px solid var(--border);
    border-radius: 4px;
    cursor: pointer;
    font-size: 1rem;
    line-height: 1;
    transition: background 0.2s;
}

.zoom-btn:hover {
    background: var(--lugano-blue);
    color: white;
    border-color: var(--lugano-blue);
}

#zoom-level {
    font-size: 0.75rem;
    color: var(--text-light);
    min-width: 35px;
    text-align: center;
    font-weight: 500;
}

.live-values {
    padding: 0.5rem 1rem;
    display: flex;
    flex-wrap: wrap;
    gap: 0.8rem 1.2rem;
    font-family: 'Inter', monospace;
    font-size: 0.85rem;
    background: var(--bg-panel);
    border-top: 1px solid var(--border);
}

.live-values.hidden {
    display: none;
}

.live-value {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.live-value .label {
    color: var(--text-light);
    font-weight: 400;
}

.live-value .value {
    font-weight: 600;
    min-width: 55px;
}

/* Live Mode */
body.live-mode .play-btn,
body.live-mode #playback-speed {
    opacity: 0.3;
    pointer-events: none;
}

body.live-mode .trip-info {
    color: #e53e3e;
    font-weight: 500;
}

body.live-mode .position-marker {
    animation: marker-pulse 1.5s infinite;
}

@keyframes marker-pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(61, 90, 156, 0.5); }
    50% { box-shadow: 0 0 0 10px rgba(61, 90, 156, 0); }
}

/* Timeline Footer */
footer {
    padding: 0.8rem 1.5rem;
    background: var(--bg-card);
    border-top: 1px solid var(--border);
    box-shadow: 0 -2px 8px var(--shadow);
}

.timeline-container {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.play-btn {
    width: 44px;
    height: 44px;
    background: var(--lugano-blue);
    border: none;
    border-radius: 50%;
    color: white;
    font-size: 1.1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.1s;
    box-shadow: 0 4px 12px rgba(61, 90, 156, 0.3);
}

.play-btn:hover {
    background: var(--lugano-blue-light);
}

.play-btn:active {
    transform: scale(0.95);
}

#timeline {
    flex: 1;
    height: 6px;
    -webkit-appearance: none;
    background: var(--bg-panel);
    border-radius: 3px;
    cursor: pointer;
    border: 1px solid var(--border);
}

#timeline::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    background: var(--lugano-blue);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(61, 90, 156, 0.3);
}

/* Absolute timestamp (left side) - shows video/log timestamp */
.absolute-time {
    font-family: 'Inter', monospace;
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-dark);
    min-width: 100px;
    letter-spacing: -0.02em;
}

/* Elapsed / Remaining time display (right side) */
.time-display {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    font-family: 'Inter', monospace;
    font-size: 0.85rem;
    color: var(--text-light);
    font-weight: 500;
    min-width: 120px;
    justify-content: flex-end;
}

.time-display .time-separator {
    color: var(--text-light);
    opacity: 0.5;
}

#current-time {
    color: var(--text-medium);
}

#remaining-time {
    color: var(--text-light);
}

#playback-speed {
    padding: 0.4rem 0.6rem;
    background: var(--bg-panel);
    color: var(--text-dark);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.85rem;
}

/* Leaflet customization - light theme */
.leaflet-container {
    background: var(--bg-panel);
}

.position-marker {
    background: var(--lugano-blue);
    border: 3px solid white;
    border-radius: 50%;
    width: 16px;
    height: 16px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

/* Stats Button */
.stats-btn {
    padding: 0.5rem 1rem;
    background: var(--lugano-blue);
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s;
}

.stats-btn:hover {
    background: var(--lugano-blue-light);
}

/* Home Button */
header .home-btn {
    color: var(--lugano-blue);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    margin-left: auto;
    white-space: nowrap;
}

header .home-btn:hover {
    color: var(--lugano-blue-light);
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: var(--bg-card);
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
    max-width: 800px;
    max-height: 80vh;
    width: 90%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border);
    background: var(--bg-panel);
}

.modal-header h2 {
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--lugano-blue);
}

.close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-light);
    cursor: pointer;
    padding: 0;
    line-height: 1;
}

.close-btn:hover {
    color: var(--text-dark);
}

/* Stats Grid */
.stats-grid {
    padding: 1rem;
    overflow-y: auto;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 1rem;
}

.stat-card {
    background: var(--bg-panel);
    border-radius: 10px;
    padding: 1rem;
    border: 1px solid var(--border);
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px var(--shadow);
}

.stat-card.expanded {
    grid-column: span 2;
}

.stat-card .stat-label {
    font-size: 0.75rem;
    color: var(--text-light);
    margin-bottom: 0.3rem;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stat-card .stat-avg {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--lugano-blue);
}

.stat-card .stat-unit {
    font-size: 0.8rem;
    color: var(--text-light);
    font-weight: 400;
}

.stat-details {
    display: none;
    margin-top: 0.8rem;
    padding-top: 0.8rem;
    border-top: 1px solid var(--border);
}

.stat-card.expanded .stat-details {
    display: block;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.4rem;
    font-size: 0.85rem;
}

.stat-row .label {
    color: var(--text-light);
}

.stat-row .value {
    font-weight: 600;
    color: var(--text-dark);
}

.stat-row .timestamp {
    font-size: 0.75rem;
    color: var(--text-light);
    margin-left: 0.5rem;
}

/* Responsive - Tablet */
@media (max-width: 1024px) {
    main {
        flex-direction: column;
    }

    .map-panel, .data-panel {
        min-width: unset;
    }

    .video-container {
        height: 180px;
    }

    .trip-selector select {
        min-width: 300px;
    }
}

/* ==========================================================================
   MOBILE LAYOUT — Redesigned for Pixel (412dp wide)

   Layout (portrait, scrollable):
   ┌─────────────────────────┐
   │ Header (compact 2-row)  │  fixed top
   ├─────────────────────────┤
   │ Video (toggle mode)     │  ~56vw tall (16:9)
   │ Map (full width)        │  180px
   │ Graphs                  │  250px
   │ (all scrollable)        │
   ├─────────────────────────┤
   │ Timeline (sticky bottom)│  fixed bottom
   └─────────────────────────┘
   ========================================================================== */
@media (max-width: 768px) {
    body {
        overflow-x: hidden;
        overflow-y: hidden;
        touch-action: pan-y;
    }

    /* Hide Leaflet attribution on mobile — saves space */
    .leaflet-control-attribution {
        display: none !important;
    }

    /* ---- Login Screen ---- */
    #login-screen {
        background-position: center 30%;
        background-size: 150% auto;
    }

    .login-overlay {
        align-items: flex-end;
        padding-bottom: 15vh;
    }

    .login-content {
        padding: 1.5rem 2rem;
        margin: 0 1rem;
        border-radius: 12px;
        width: calc(100% - 2rem);
        max-width: 360px;
    }

    .login-content h1 {
        font-size: 1.3rem;
        margin-bottom: 1rem;
    }

    #login-form {
        flex-direction: column;
        gap: 0.75rem;
    }

    #login-form input {
        width: 100%;
        padding: 0.9rem 1rem;
        font-size: 16px; /* Prevents iOS zoom */
    }

    #login-form button {
        width: 100%;
        padding: 0.9rem 1rem;
    }

    /* ---- Dashboard Screen ---- */
    #dashboard-screen {
        height: 100vh;
        height: 100dvh; /* Dynamic viewport for mobile address bar */
        display: flex;
        flex-direction: column;
    }

    /* ---- Header: Compact 2-row ---- */
    header {
        flex-direction: row;
        flex-wrap: wrap;
        align-items: center;
        gap: 0.4rem 0.6rem;
        padding: 0.5rem 0.75rem;
    }

    .header-brand {
        gap: 0.5rem;
    }

    .header-brand img {
        height: 28px;
    }

    header h1 {
        font-size: 0.9rem;
    }

    header .vehicle-subtitle {
        display: none; /* Save space — redundant with brand icon */
    }

    /* Trip info, stats, home — share first row right side */
    .trip-info {
        display: none; /* Shown in trip selector text instead */
    }

    .stats-btn {
        padding: 0.3rem 0.55rem;
        font-size: 0.72rem;
        margin-left: 0;
        flex-shrink: 0;
    }

    /* Only the first action button pushes to the right; siblings sit next to it */
    #trip-stats-btn.stats-btn {
        margin-left: auto;
    }

    /* Shorter label on mobile so it fits in the action-button cluster */
    #video-maker-btn {
        font-size: 0;
        padding: 0.3rem 0.5rem;
    }
    #video-maker-btn::before {
        content: 'Clip';
        font-size: 0.72rem;
        line-height: 1.1;
    }

    header .home-btn {
        font-size: 0.78rem;
        margin-left: 0;
        flex-shrink: 0;
    }

    /* Trip selector — full width second row */
    .trip-selector {
        width: 100%;
        order: 10; /* Force to second row */
    }

    .trip-selector select {
        flex: 1 1 0;
        /* min-width: 0 prevents the select from growing to fit its widest
           option (some trip names exceed 800px). Without this the Reload
           button gets pushed off the right edge of the screen. */
        min-width: 0;
        max-width: unset;
        font-size: 13px;
        padding: 0.4rem 0.5rem;
        text-overflow: ellipsis;
    }

    .trip-selector button {
        padding: 0.5rem 0.85rem;
        font-size: 0.85rem;
        min-height: 38px;
        font-weight: 600;
        background: var(--lugano-blue);
        color: white;
        border-color: var(--lugano-blue);
    }

    .trip-selector button:active {
        transform: scale(0.96);
    }

    /* ---- Main Content: Scrollable vertical stack ---- */
    main.video-first-layout {
        flex: 1;
        display: flex;
        flex-direction: column;
        gap: 0;
        padding: 0;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
    }

    /* Override the desktop .top-row (video + map side by side) */
    .top-row {
        display: flex;
        flex-direction: column;
        height: auto;
        min-height: unset;
        max-height: unset;
        gap: 0;
        flex: none;
    }

    /* ---- Video: SHOWN, full width.
       Layout is a flex column so the camera controls sit BELOW the video
       on mobile instead of overlapping it (touchable + no occlusion). */
    .video-panel {
        flex: none;
        border-radius: 0;
        box-shadow: none;
        display: flex;
        flex-direction: column;
        height: auto;
        max-height: none;
        min-height: 0;
    }

    .video-panel .video-container {
        display: block !important; /* Override old mobile hide */
        position: relative;
        height: 42vw;
        max-height: 210px;
        min-height: 150px;
        border-radius: 0;
    }

    /* Single-camera (toggle) mode: full width per camera. The .side-by-side
       parent class still wins via higher specificity, so SBS keeps 50/50. */
    .video-container .dashcam-video {
        width: 100%;
    }

    /* Speed overlay — smaller on mobile */
    .speed-overlay {
        top: 4px;
        right: 4px;
        padding: 2px 7px;
        border-radius: 5px;
        gap: 3px;
    }

    .speed-overlay #current-speed {
        font-size: 0.95rem;
        font-weight: 600;
    }

    .speed-overlay .speed-unit {
        font-size: 0.6rem;
    }

    /* Frame overlay — compact */
    .frame-overlay {
        top: 6px;
        left: 6px;
        padding: 4px 8px;
        border-radius: 6px;
    }

    .frame-overlay #frame-number {
        font-size: 0.9rem;
    }

    .frame-overlay .frame-hint {
        display: none; /* Keyboard shortcuts irrelevant on touch */
    }

    /* Video controls — moved out of the absolute overlay to a real row
       BELOW the video, so the F/R buttons never sit on top of the picture.
       Achieved by overriding the desktop absolute positioning. */
    .video-panel .video-controls {
        position: static;
        opacity: 1;
        background: var(--bg-card);
        border-top: 1px solid var(--border);
        padding: 8px 10px;
        border-radius: 0;
        gap: 8px;
        flex-wrap: wrap;
        justify-content: space-between;
        z-index: 1;
    }

    /* Camera buttons — larger touch targets and dark text on light bg
       (since the bar is no longer over the dark video) */
    .camera-btn {
        padding: 8px 14px;
        font-size: 13px;
        min-height: 36px;
        background: var(--bg-panel);
        color: var(--text-dark);
        border-color: var(--border);
    }

    .camera-btn.active {
        background: var(--lugano-blue);
        color: white;
        border-color: var(--lugano-blue);
    }

    .view-btn {
        padding: 8px 12px;
        font-size: 13px;
        min-height: 36px;
        background: var(--bg-panel);
        color: var(--text-dark);
        border-color: var(--border);
    }

    .view-btn.active {
        background: var(--lugano-blue);
        color: white;
        border-color: var(--lugano-blue);
    }

    .mute-btn {
        background: var(--bg-panel);
        border: 1px solid var(--border);
        border-radius: 6px;
        padding: 4px 8px;
        min-height: 36px;
        min-width: 36px;
    }

    #volume-slider {
        width: 50px;
    }

    /* ---- Map: Below video, full width ---- */
    .map-panel-small {
        flex: none;
        min-width: unset;
        max-width: unset;
        width: 100%;
        height: 160px;
        border-radius: 0;
        box-shadow: none;
        border-left: none;
        border-right: none;
    }

    /* Legacy map panel (non video-first layout) */
    .map-panel {
        flex: none;
        height: 180px;
        min-width: unset;
    }

    .map-overlay {
        top: 6px;
        right: 6px;
        padding: 0.4rem 0.6rem;
    }

    /* ---- Graphs: Full width ---- */
    .graphs-panel {
        flex: none;
        border-radius: 0;
        border-left: none;
        border-right: none;
        box-shadow: none;
        min-height: unset;
    }

    .graph-controls {
        flex-wrap: wrap;
        align-items: center;
        gap: 0.3rem 0.4rem;
        padding: 0.4rem 0.6rem;
    }

    /* Force the channel checkboxes onto their own wrapping row so none get
       clipped off the right edge. `display: contents` lets the checkboxes
       participate as flex siblings of the parent — but on narrow phones
       there's no room for them all on one line. Promote to a real flex
       container that wraps. */
    #channel-checkboxes {
        display: flex;
        flex-wrap: wrap;
        gap: 0.3rem 0.4rem;
        width: 100%;
        flex-basis: 100%;
        order: -1;
    }

    .graph-controls label {
        white-space: nowrap;
        font-size: 0.7rem;
        padding: 0.15rem 0.5rem;
        background: var(--bg-card);
        border: 1px solid var(--border);
        border-radius: 999px;
        gap: 0.25rem;
    }

    .graph-controls label:has(input:checked) {
        background: rgba(61, 90, 156, 0.08);
        border-color: var(--lugano-blue-light);
    }

    /* Timezone + zoom controls sit on their own short row alongside chips */
    .control-timezone,
    .control-zoom {
        margin-left: 0;
    }

    .control-live-values {
        font-size: 0.72rem;
    }

    .control-timezone select {
        font-size: 0.7rem;
        padding: 0.2rem 0.3rem;
    }

    .zoom-btn {
        width: 36px;
        height: 36px;
        font-size: 1rem;
        font-weight: 600;
    }

    #zoom-level {
        font-size: 0.85rem;
        min-width: 36px;
        font-weight: 600;
    }

    .control-zoom {
        gap: 6px;
    }

    #graphs {
        min-height: 0;
        height: auto;
        position: relative;
    }

    .zoom-exit-pill {
        top: 6px;
        right: 8px;
        padding: 8px 14px;
        font-size: 0.85rem;
        min-height: 36px;
    }

    /* Only size graphs when Plotly has rendered */
    #graphs .plotly-graph-div {
        width: 100% !important;
        height: 190px !important;
    }

    .live-values {
        padding: 0.4rem 0.6rem;
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.3rem 0.8rem;
        font-size: 0.7rem;
    }

    .live-value {
        justify-content: space-between;
    }

    .live-value .value {
        min-width: 0;
        text-align: right;
    }

    /* Legacy panels */
    .panel {
        border-radius: 0;
    }

    .data-panel {
        flex: none;
        min-width: unset;
        min-height: unset;
    }

    .graphs-container {
        min-height: unset;
    }

    /* ---- Timeline Footer: Fixed at bottom ---- */
    footer {
        flex-shrink: 0;
        padding: 0.5rem 0.75rem;
        z-index: 50;
    }

    .timeline-container {
        gap: 0.4rem;
    }

    .play-btn {
        width: 38px;
        height: 38px;
        font-size: 0.95rem;
        flex-shrink: 0;
    }

    .absolute-time {
        font-size: 0.75rem;
        min-width: 70px;
    }

    .time-display {
        font-size: 0.7rem;
        min-width: 80px;
        gap: 0.15rem;
    }

    #playback-speed {
        padding: 0.3rem 0.35rem;
        font-size: 0.7rem;
    }

    #timeline {
        height: 10px;
        touch-action: none;
    }

    #timeline::-webkit-slider-thumb {
        width: 26px;
        height: 26px;
    }

    #timeline::-webkit-slider-runnable-track {
        height: 10px;
    }

    /* ---- Modal: Full Screen ---- */
    .modal-content {
        width: 100%;
        height: 100%;
        max-width: 100%;
        max-height: 100%;
        border-radius: 0;
    }

    .stats-grid {
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
        padding: 0.75rem;
    }

    .stat-card {
        padding: 0.75rem;
    }

    .stat-card .stat-avg {
        font-size: 1.2rem;
    }

    .stat-card.expanded {
        grid-column: span 2;
    }

    /* ---- Clip Timeline (parked sessions) ---- */
    .clip-timeline {
        padding: 8px 20px;
    }

    /* ---- Trip loading overlay ---- */
    .trip-loading-overlay .loading-text {
        font-size: 0.9rem;
    }

    .trip-loading-overlay .loading-progress {
        font-size: 0.8rem;
    }
}

/* Very small phones (Pixel 4a, SE) */
.loading-spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-top-color: var(--lugano-blue);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.loading-spinner.large {
    width: 32px;
    height: 32px;
    border-width: 3px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Trip selector loading state */
.trip-selector.loading select {
    opacity: 0.6;
    pointer-events: none;
}

.trip-selector.loading button {
    opacity: 0.6;
    pointer-events: none;
}

.trip-selector .loading-spinner {
    margin-left: 8px;
}

/* Trip loading overlay */
.trip-loading-overlay {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    background: rgba(255, 255, 255, 0.95);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1500;
    gap: 16px;
}

.trip-loading-overlay.hidden {
    display: none;
}

.trip-loading-overlay .loading-text {
    font-size: 1rem;
    color: var(--text-medium);
    font-weight: 500;
}

.trip-loading-overlay .loading-progress {
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-size: 0.85rem;
    color: var(--text-light);
}

.trip-loading-overlay .loading-step {
    display: flex;
    align-items: center;
    gap: 8px;
}

.trip-loading-overlay .loading-step.done {
    color: var(--lugano-blue);
}

.trip-loading-overlay .loading-step.done::before {
    content: '✓';
    color: var(--lugano-blue);
    font-weight: bold;
}

.trip-loading-overlay .loading-step.active::before {
    content: '';
    display: inline-block;
    width: 12px;
    height: 12px;
    border: 2px solid var(--border);
    border-top-color: var(--lugano-blue);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.trip-loading-overlay .loading-step.pending {
    opacity: 0.5;
}

.trip-loading-overlay .loading-step.pending::before {
    content: '○';
}

/* Graph loading indicator */
.graphs-panel.loading #graphs::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--lugano-blue);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

/* Clip Timeline (for parked sessions) */
.clip-timeline {
    position: relative;
    width: 100%;
    height: 100%;
    min-height: 120px;
    background: #fafafa;
    padding: 10px 60px;  /* Extra horizontal padding for timestamps */
    box-sizing: border-box;
    overflow: visible;
}

.clip-timeline-track {
    position: relative;
    height: 32px;
    background: #e8e8e8;
    border-radius: 4px;
    margin-bottom: 4px;
}

.clip-segment {
    position: absolute;
    top: 4px;
    height: 24px;
    background: var(--lugano-blue);
    border-radius: 3px;
    cursor: pointer;
    opacity: 0.85;
    transition: opacity 0.15s, box-shadow 0.15s;
    min-width: 3px;
}

.clip-segment:hover {
    opacity: 1;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.clip-segment.active {
    background: var(--lugano-blue-light);  /* Lighter blue to indicate active */
    opacity: 1;
    box-shadow: 0 2px 8px rgba(90, 123, 196, 0.4);
}

.clip-segment-label {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: #fff;
    font-size: 0.65rem;
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 90%;
    pointer-events: none;
}

.clip-timeline-cursor {
    position: absolute;
    top: 0;
    height: 32px;
    width: 2px;
    background: var(--porsche-red);
    pointer-events: none;
    z-index: 100;
    transition: left 0.05s linear;
}

/* Timestamp labels below clips */
.clip-timestamps {
    position: relative;
    height: 40px;
    margin-top: 2px;
    overflow: visible;
}

.clip-timestamp {
    position: absolute;
    font-size: 0.65rem;
    color: var(--text-light);
    white-space: nowrap;
    /* Don't center - just position at left edge to prevent clipping */
}

.clip-timestamp.start {
    top: 0;
}

.clip-timestamp.end {
    top: 14px;
    color: var(--text-medium);
}

/* Main axis timestamps */
.clip-timeline-axis {
    display: flex;
    justify-content: space-between;
    padding-top: 8px;
    font-size: 0.75rem;
    color: var(--text-medium);
    border-top: 1px solid var(--border);
    margin-top: 8px;
}

.clip-timeline-axis .axis-label {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.clip-timeline-axis .axis-time {
    font-weight: 500;
}

.clip-timeline-axis .axis-date {
    font-size: 0.65rem;
    color: var(--text-light);
}

/* Very small phones */
@media (max-width: 380px) {
    .login-content h1 {
        font-size: 1.1rem;
    }

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

    .stat-card.expanded {
        grid-column: span 1;
    }

    .graph-controls label {
        font-size: 0.7rem;
    }
}

/* Landscape phone optimization */
@media (max-width: 768px) and (orientation: landscape) {
    #login-screen {
        background-position: center center;
        background-size: cover;
    }

    .login-overlay {
        align-items: center;
        padding-bottom: 0;
    }

    .login-content {
        padding: 1rem 1.5rem;
    }

    .login-content h1 {
        font-size: 1.1rem;
        margin-bottom: 0.75rem;
    }

    #login-form {
        flex-direction: row;
    }

    #login-form input {
        width: auto;
        flex: 1;
    }

    #login-form button {
        width: auto;
    }

    /* Landscape: header even more compact */
    header {
        padding: 0.3rem 0.75rem;
    }

    header h1 {
        font-size: 0.8rem;
    }

    /* Video takes more horizontal space in landscape */
    .video-panel {
        height: 50vh;
        max-height: unset;
    }

    /* Map and graphs side by side below video */
    .top-row {
        flex-direction: row;
        height: auto;
    }

    .map-panel-small {
        flex: 1;
        height: 150px;
    }

    .map-panel {
        flex: 1;
        height: 150px;
    }

    .graphs-panel {
        flex: 1;
    }

    #graphs {
        height: 150px;
        min-height: 150px;
    }

    footer {
        padding: 0.3rem 0.75rem;
    }
}
