/* ─── Design Tokens ─────────────────────────────────────────────────────────── */
:root {
    --primary: #fc4c02;
    --bg: #f4f5f7;
    --card-bg: #ffffff;
    --text: #2d3748;
    --text-muted: #718096;
    --border: #e2e8f0;
    --danger: #ef4444;
    --warning: #eab308;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #16161a;
        --card-bg: #242429;
        --text: #e2e8f0;
        --text-muted: #94a3b8;
        --border: #383848;
    }
}

/* ─── Reset & Base ───────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background-color: var(--bg);
    color: var(--text);
    height: 100dvh;
    overflow: hidden;
    display: flex;
    justify-content: center;
}

/* ─── App Shell ──────────────────────────────────────────────────────────────── */
.app-container {
    width: 100%;
    max-width: 480px;
    height: 100dvh;
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* ─── Map ────────────────────────────────────────────────────────────────────── */
#map {
    flex: 1;
    width: 100%;
    z-index: 1;
    min-height: 0;    /* required for flex shrink to work correctly */
    position: relative;
}

/* Follow-mode toggle button overlaid on the map */
.follow-btn {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 500;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    border: 2px solid rgba(0, 0, 0, 0.2);
    background: white;
    font-size: 1.1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, border-color 0.2s, transform 0.15s;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}
.follow-btn.active {
    background: var(--primary);
    border-color: var(--primary);
}
.follow-btn:hover { transform: scale(1.08); }
.follow-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Compass toggle — top-left of map, mirrors follow-btn on the right */
.compass-btn {
    position: absolute;
    top: 12px;
    left: 12px;
    z-index: 500;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    border: 2px solid rgba(0, 0, 0, 0.2);
    background: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 5px;
    transition: background 0.2s, border-color 0.2s, transform 0.15s;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}
.compass-btn.heading-up {
    background: var(--primary);
    border-color: var(--primary);
}
.compass-btn:hover  { transform: scale(1.08); }
.compass-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* The SVG rose counter-rotates to always point to true north */
.compass-rose {
    width: 100%;
    height: 100%;
    transition: transform 0.4s ease;
    transform-origin: center;
}


/* ─── Dashboard ──────────────────────────────────────────────────────────────── */
.dashboard {
    flex-shrink: 0;
    background: var(--card-bg);
    padding: 16px;
    padding-bottom: calc(16px + var(--safe-bottom));
    border-radius: 24px 24px 0 0;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.12);
    z-index: 10;
    display: flex;
    flex-direction: column;
    gap: 8px;   /* tighter gap — banners + body must share the panel comfortably */
}

/* Scrollable inner section — banners above this never cause a scrollbar */
.dashboard-body {
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-height: clamp(180px, 38dvh, 260px);
    overflow-y: auto;
    min-height: 0;  /* required for flex children to honour overflow */
}

.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    text-align: center;
}

.stat-box {
    background: var(--bg);
    padding: 10px;
    border-radius: 12px;
    border: 1px solid var(--border);
}
.stat-val { font-size: 1.5rem; font-weight: 800; }
.stat-lbl { font-size: 0.75rem; text-transform: uppercase; color: var(--text-muted); }

/* ─── Activity Type Selector ─────────────────────────────────────────────────── */
.activity-type-selector {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    justify-content: center;
}

.activity-type-btn {
    padding: 8px 12px;
    border: 2px solid var(--border);
    border-radius: 8px;
    background: var(--card-bg);
    color: var(--text);
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 600;
    transition: background 0.2s, border-color 0.2s;
}
.activity-type-btn:hover {
    border-color: var(--primary);
    background: #fff3e0;
}
.activity-type-btn.selected {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}
.activity-type-btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

@media (prefers-color-scheme: dark) {
    .activity-type-btn:not(.selected) { background: #2d2e36; }
    .activity-type-btn:hover:not(.selected) { background: #3a2818; }
}

/* ─── Status Indicator ───────────────────────────────────────────────────────── */
.status-indicator {
    text-align: center;
    font-size: 0.8rem;
    padding: 6px 12px;
    border-radius: 8px;
    margin: 0 auto;
}
.status-ready    { background: #e0f2fe; color: #0369a1; }
.status-tracking { background: #fef3c7; color: #92400e; animation: pulse 2s infinite; }
.status-paused   { background: #fecaca; color: #991b1b; }

@media (prefers-color-scheme: dark) {
    .status-ready    { background: #0c2d3f; color: #7dd3fc; }
    .status-tracking { background: #3a2a00; color: #fcd34d; }
    .status-paused   { background: #3a1010; color: #fca5a5; }
}

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

/* ─── Control Buttons ────────────────────────────────────────────────────────── */
.controls { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
    padding: 12px;
    border: none;
    border-radius: 12px;
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    text-align: center;
    flex: 1;
    min-width: 80px;
    transition: opacity 0.15s, transform 0.1s;
}
.btn:active { transform: scale(0.97); }
.btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.btn-start  { background: var(--primary); color: white; }
.btn-pause  { background: var(--warning); color: #1a202c; display: none; }
.btn-resume { background: #10b981; color: white; display: none; }
.btn-stop   { background: #374151; color: white; display: none; }
.btn-reset  { background: var(--danger); color: white; display: none; font-size: 0.9rem; padding: 10px; }
.btn-panel  { background: var(--border); color: var(--text); }
.btn-danger { background: var(--danger); color: white; padding: 10px; }

/* Smaller inline action buttons (history list) */
.btn-download, .btn-delete {
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 0.75rem;
    font-weight: 700;
    cursor: pointer;
    border: none;
    transition: opacity 0.15s;
}
.btn-download:hover, .btn-delete:hover { opacity: 0.85; }
.btn-download { background: var(--primary); color: white; }
.btn-delete   { background: var(--danger);  color: white; }

/* ─── History Panel ──────────────────────────────────────────────────────────── */
.history-panel {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    height: 85dvh;
    background: var(--card-bg);
    z-index: 100;
    border-radius: 24px 24px 0 0;
    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.15);
    transition: transform 0.3s ease;
    display: flex;
    flex-direction: column;
    padding: 20px;
    padding-bottom: calc(20px + var(--safe-bottom));
}
.history-panel.open { transform: translateY(-100%); }

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border);
    padding-bottom: 10px;
    margin-bottom: 16px;
}
.close-btn {
    border: none;
    background: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-muted);
    line-height: 1;
    padding: 4px 8px;
    border-radius: 6px;
    transition: background 0.15s;
}
.close-btn:hover { background: var(--border); }
.close-btn:focus-visible { outline: 2px solid var(--primary); }

/* Groups the "Clear all" text-button and the close ✕ button on the right of the header */
.panel-header-actions { display: flex; align-items: center; gap: 6px; }

/* Compact outlined danger button — lives in the panel header, hidden until list has items */
.btn-clear-all {
    background: none;
    border: 1px solid var(--danger);
    color: var(--danger);
    border-radius: 6px;
    padding: 4px 10px;
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
    display: none;   /* shown by loadHistory() when activities exist */
}
.btn-clear-all:hover { background: var(--danger); color: white; }
.btn-clear-all:focus-visible { outline: 2px solid var(--danger); outline-offset: 2px; }

.history-list { flex-grow: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }

.history-item {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}
.history-item-info  { flex-grow: 1; display: flex; flex-direction: column; gap: 4px; }
.history-item-date  { font-size: 0.8rem; color: var(--text-muted); }
.history-item-distance { font-size: 1.1rem; font-weight: 700; }
.history-item-duration { font-size: 0.9rem; color: var(--text-muted); }
.history-item-type {
    display: inline-block;
    font-size: 0.75rem;
    font-weight: 600;
    padding: 4px 8px;
    border-radius: 6px;
    background: var(--border);
    color: var(--text);
    margin-top: 4px;
    text-transform: capitalize;
}
/* Activity type color chips */
.history-item-type.cycling  { background: #bfdbfe; color: #1e40af; }
.history-item-type.paddling { background: #a5f3fc; color: #0c4a6e; }
.history-item-type.hiking   { background: #bbf7d0; color: #065f46; }
.history-item-type.skiing   { background: #e5e7eb; color: #3d85c6; }
.history-item-type.walking  { background: #fde68a; color: #78350f; }
.history-item-type.running  { background: #fecaca; color: #991b1b; }
.history-item-type.driving  { background: #d8b4fe; color: #581c87; }
.history-item-type.other    { background: #e5e7eb; color: #374151; }

.history-item-actions { display: flex; gap: 6px; }
.history-empty { text-align: center; color: var(--text-muted); padding: 40px 20px; font-size: 0.95rem; }

/* ─── Modal ──────────────────────────────────────────────────────────────────── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    backdrop-filter: blur(3px);
}
/* Hide via hidden attribute (overrides display:flex) */
.modal-overlay[hidden] { display: none; }

.modal-box {
    background: var(--card-bg);
    border-radius: 16px;
    padding: 24px;
    max-width: 320px;
    width: 100%;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    animation: modalIn 0.2s ease;
}
@keyframes modalIn {
    from { opacity: 0; transform: scale(0.93) translateY(8px); }
    to   { opacity: 1; transform: scale(1)    translateY(0);   }
}
.modal-message {
    font-size: 1rem;
    color: var(--text);
    margin-bottom: 20px;
    line-height: 1.5;
}
.modal-actions { display: flex; gap: 8px; }

/* ─── Toast ──────────────────────────────────────────────────────────────────── */
.toast {
    position: fixed;
    bottom: calc(24px + var(--safe-bottom));
    left: 50%;
    transform: translateX(-50%);
    background: #1a202c;
    color: white;
    padding: 10px 20px;
    border-radius: 24px;
    font-size: 0.9rem;
    z-index: 1001;
    white-space: nowrap;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
    animation: toastIn 0.3s ease;
}
.toast[hidden] { display: none; }

@keyframes toastIn {
    from { opacity: 0; transform: translateX(-50%) translateY(10px); }
    to   { opacity: 1; transform: translateX(-50%) translateY(0);    }
}

/* ─── App Update Notification Banner ────────────────────────────────────────── */
.update-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    background: #1e293b;
    color: white;
    border-radius: 10px;
    padding: 8px 12px;
    animation: fadeIn 0.3s ease;
}
.update-banner[hidden] { display: none; }

.update-msg     { font-size: 0.85rem; font-weight: 600; flex: 1; }
.update-actions { display: flex; gap: 6px; flex-shrink: 0; }

.btn-update-reload {
    background: white;
    color: #1e293b;
    border: none;
    border-radius: 6px;
    padding: 4px 10px;
    font-size: 0.78rem;
    font-weight: 700;
    cursor: pointer;
    transition: opacity 0.15s;
}
.btn-update-reload:hover { opacity: 0.85; }
.btn-update-reload:focus-visible { outline: 2px solid white; outline-offset: 2px; }

.btn-update-later {
    background: transparent;
    color: #94a3b8;
    border: 1px solid #475569;
    border-radius: 6px;
    padding: 4px 10px;
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s;
}
.btn-update-later:hover { color: #cbd5e1; border-color: #64748b; }
.btn-update-later:focus-visible { outline: 2px solid #94a3b8; outline-offset: 2px; }

/* ─── Historical Track Preview Banner ───────────────────────────────────────── */

.preview-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    background: #ccfbf1;
    border: 1px solid #5eead4;
    border-radius: 10px;
    padding: 8px 12px;
    animation: fadeIn 0.25s ease;
}
.preview-banner[hidden] { display: none; }

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-4px); }
    to   { opacity: 1; transform: translateY(0); }
}

.preview-info   { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.preview-label  { font-size: 0.8rem; font-weight: 700; color: #0f766e; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview-meta   { font-size: 0.72rem; color: #134e4a; }

.btn-clear-preview {
    flex-shrink: 0;
    background: #0d9488;
    color: white;
    border: none;
    border-radius: 6px;
    padding: 5px 10px;
    font-size: 0.8rem;
    font-weight: 700;
    cursor: pointer;
    transition: opacity 0.15s;
}
.btn-clear-preview:hover { opacity: 0.85; }
.btn-clear-preview:focus-visible { outline: 2px solid #0d9488; outline-offset: 2px; }

/* Map view button in history list (teal, same height as GPX / Delete) */
.btn-map {
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 0.75rem;
    font-weight: 700;
    cursor: pointer;
    border: none;
    background: #0d9488;
    color: white;
    transition: opacity 0.15s;
}
.btn-map:hover { opacity: 0.85; }
.btn-map:focus-visible { outline: 2px solid #0d9488; outline-offset: 2px; }

@media (prefers-color-scheme: dark) {
    .preview-banner {
        background: #134e4a;
        border-color: #0f766e;
    }
    .preview-label { color: #5eead4; }
    .preview-meta  { color: #99f6e4; }
}

/* ─── Accessibility ──────────────────────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}
