/* ============================================================
   ONCF AL ATLAS — Modern Transit Theme
   ============================================================ */

:root {
    color-scheme: light;

    /* Clean, airy light mode */
    --bg: #f3f4f6;
    --surface: #ffffff;
    --surface-hover: #f9fafb;
    --surface-alt: #f3f4f6;
    
    --text: #111827;
    --text-soft: #4b5563;
    --muted: #9ca3af;
    
    --border: #e5e7eb;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);

    /* Accessible Brand Colors */
    --accent: #2563eb; 
    --on-accent: #ffffff;
    --red: #e17c1d;       /* AL ATLAS */
    --blue: #c72002;      /* TNR */
    --violet: #7c3aed;    /* AL BORAQ */
    --grey: #64748b;
    --amber: #3654db;     /* Stopped */
    --on-amber: #fff;
    --success: #10b981;

    --station-dot: #ffffff;
    --map-label: #1f2937;
    --track-way: #cbd5e1;
    --track-leg: #3b82f6;
    --sk-sheen: rgba(0, 0, 0, 0.04);
    
    --radius-lg: 16px;
    --radius-md: 12px;
    --radius-sm: 8px;
    --radius-pill: 999px;

    --mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

:root[data-theme="dark"] {
    color-scheme: dark;

    /* Deep, soft slate dark mode */
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-hover: #334155;
    --surface-alt: #0f172a;
    
    --text: #f8fafc;
    --text-soft: #cbd5e1;
    --muted: #64748b;
    
    --border: #334155;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.5);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.5);

    /* Brightened Brand Colors for Dark Mode Contrast */
    --accent: #3b82f6; 
    --red: #e17c1d;       /* AL ATLAS */
    --blue: #c72002;      /* TNR */
    --violet: #7c3aed;    /* AL BORAQ */
    --grey: #94a3b8;
    --amber: #3654db;
    --on-amber: #000;

    --station-dot: #1e293b;
    --map-label: #f1f5f9;
    --track-way: #1e293b;
    --track-leg: #60a5fa;
    --sk-sheen: rgba(255, 255, 255, 0.05);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; padding: 0; overflow: hidden; }

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

#map { position: absolute; inset: 0; z-index: 1; background: var(--bg); }
.hidden { display: none !important; }

/* Transitions */
body, .panel, .train-card, .status-pill, .list-count, .type-tag, .station-label {
    transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease, box-shadow 0.3s ease;
}

/* ---------------- OVERLAY & PANEL ---------------- */

.overlay {
    position: absolute;
    inset: 0;
    z-index: 1000;
    pointer-events: none;
    padding: 24px;
    display: flex;
}

.panel {
    pointer-events: auto;
    width: 100%;
    max-width: 400px;
    height: calc(100vh - 48px);
    background: var(--surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* ---------------- HEADER ---------------- */

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}

.header-title {
    font-size: 18px;
    font-weight: 700;
    letter-spacing: -0.5px;
    color: var(--text);
}

.header-actions {
    display: flex;
    gap: 8px;
}

.icon-btn {
    background: var(--surface-alt);
    border: 1px solid var(--border);
    color: var(--text-soft);
    width: 36px;
    height: 36px;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
}
.icon-btn:hover { background: var(--surface-hover); color: var(--text); }
.icon-btn svg { width: 18px; height: 18px; }

.theme-icon-moon { display: none; }
:root[data-theme="dark"] .theme-icon-sun { display: none; }
:root[data-theme="dark"] .theme-icon-moon { display: block; }

/* Drag Handle (Hidden on Desktop) */
.drag-handle { display: none; }

/* Search Box */
.search-box {
    display: flex;
    align-items: center;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    border-radius: var(--radius-pill);
    padding: 6px 12px;
    flex: 1;
    margin-right: 12px;
    transition: border-color 0.2s, box-shadow 0.2s;
}
.search-box:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.2);
}
.search-box svg { width: 16px; height: 16px; color: var(--muted); margin-right: 8px; flex-shrink: 0; }
.search-box input {
    border: none;
    background: transparent;
    color: var(--text);
    font-family: var(--mono);
    font-size: 14px;
    font-weight: 600;
    width: 100%;
    outline: none;
}
.search-box input::placeholder { color: var(--muted); font-weight: 500; font-family: 'Inter', sans-serif; }

/* ---------------- LIST AREA & FILTER ---------------- */

.panel-list-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 16px 20px;
}

/* Segmented Control iOS Style */
.filter-bar {
    display: flex;
    background: var(--surface-alt);
    padding: 4px;
    border-radius: var(--radius-md);
    margin-bottom: 20px;
    border: 1px solid var(--border);
}

.filter-btn {
    flex: 1;
    border: none;
    background: transparent;
    color: var(--text-soft);
    font-size: 12px;
    font-weight: 600;
    padding: 8px 4px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s;
}

.filter-btn:hover { color: var(--text); }
.filter-btn.active {
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-sm);
}

.list-caption {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 12px;
    color: var(--muted);
}

.list-count {
    background: var(--accent);
    color: var(--on-accent);
    padding: 2px 8px;
    border-radius: var(--radius-pill);
    font-size: 11px;
    font-weight: 700;
}

.train-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding-right: 4px;
}

.train-list::-webkit-scrollbar { width: 6px; }
.train-list::-webkit-scrollbar-track { background: transparent; }
.train-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

/* ---------------- TRAIN CARDS ---------------- */

.train-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
    cursor: pointer;
    box-shadow: var(--shadow-sm);
    transition: transform 0.2s, box-shadow 0.2s, border-color 0.2s;
}

.train-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
    border-color: var(--muted);
}

.train-card.active {
    border-color: var(--accent);
    box-shadow: 0 0 0 1px var(--accent), var(--shadow-md);
}

/* ---------------- STATION VIEW (station schedule picker) ---------------- */

.station-view-head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 2px 2px 4px;
}

.sv-back {
    flex: none;
    width: 32px;
    height: 32px;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--surface);
    color: var(--text);
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    transition: border-color 0.2s, color 0.2s;
}

.sv-back:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.sv-title {
    font-size: 14px;
    font-weight: 700;
    color: var(--text);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sv-count {
    margin-left: auto;
    background: var(--accent);
    color: var(--on-accent);
    padding: 2px 8px;
    border-radius: var(--radius-pill);
    font-size: 11px;
    font-weight: 700;
}

.station-card {
    padding: 12px 16px;
}

.station-card .sv-time {
    font-family: var(--mono);
    font-size: 16px;
    font-weight: 700;
    color: var(--accent);
}

.station-card .sv-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.station-card .sv-arrdep {
    font-family: var(--mono);
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
}

.status-pill.sv-passed {
    opacity: 0.45;
}

.station-view-head + .train-card.station-card { margin-top: 4px; }

.card-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

.train-num {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 18px;
    font-weight: 700;
    color: var(--text);
}

.type-tag { font-size: 12px; font-weight: 700; color: var(--muted); }

.status-pill {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 4px 10px;
    border-radius: var(--radius-pill);
    background: var(--surface-alt);
    color: var(--text-soft);
}
.status-pill.stopped { background: var(--amber); color: var(--on-amber); }
.status-pill.boarding { background: var(--accent); color: var(--on-accent); }
.status-pill.arrived { background: var(--success); color: #fff; }

.route-line {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    font-weight: 500;
    color: var(--text-soft);
    margin-bottom: 16px;
}

.route-eta { color: var(--text); font-weight: 600; }

.progress-block {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}

.progress-track {
    flex: 1;
    height: 8px;
    background: var(--surface-alt);
    border-radius: var(--radius-pill);
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: var(--radius-pill);
    transition: width 1s linear;
}

.progress-pct, .speed-val {
    font-family: var(--mono);
    font-size: 12px;
    font-weight: 600;
}
.progress-pct { color: var(--text); min-width: 32px; text-align: right; }
.speed-val { color: var(--muted); min-width: 60px; text-align: right; }

.stop-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    margin-top: 8px;
}

.stop-name {
    display: flex;
    align-items: center;
    color: var(--text-soft);
    font-weight: 500;
}

.stop-name .marker {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--muted);
    margin-right: 8px;
}

.stop-time { font-family: var(--mono); color: var(--text); font-weight: 600; }
.stop-cd { margin-left: 8px; color: var(--muted); font-size: 11px; }
.stop-cd.live { color: var(--amber); font-weight: 700; }

/* ---------------- SKELETON ---------------- */

.skeleton-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
}

.sk-line, .sk-track {
    background-color: var(--surface-alt);
    border-radius: var(--radius-pill);
    position: relative;
    overflow: hidden;
}

.sk-line { height: 12px; margin-bottom: 12px; }
.sk-wide { width: 40%; height: 16px; }
.sk-med { width: 100%; }
.sk-short { width: 30%; }
.sk-track { height: 8px; width: 100%; margin-top: 16px; }

.sk-line::after, .sk-track::after {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, transparent, var(--sk-sheen), transparent);
    animation: sk-shimmer 1.5s infinite;
}

@keyframes sk-shimmer {
    0% { transform: translateX(-100%); }
    100% { transform: translateX(100%); }
}

/* ---------------- TIMELINE ---------------- */

.trip-details {
    margin-top: 16px;
    padding-top: 16px;
    border-top: 1px solid var(--border);
}

.tl-list { margin: 0; padding: 0; list-style: none; }
.tl-stop {
    position: relative;
    display: flex;
    align-items: center;
    padding: 8px 0 8px 20px;
    font-size: 12px;
}

.tl-stop::before {
    content: '';
    position: absolute;
    left: 4px;
    top: 0;
    bottom: 0;
    width: 2px;
    background: var(--border);
}
.tl-stop:first-child::before { top: 50%; }
.tl-stop:last-child::before { bottom: 50%; }

.tl-dot {
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 10px;
    height: 10px;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: 50%;
}

.tl-name { flex: 1; font-weight: 500; color: var(--text); }
.tl-time { font-family: var(--mono); font-weight: 600; color: var(--text-soft); }

.tl-passed { color: var(--muted); }
.tl-passed .tl-dot { background: var(--border); }
.tl-passed .tl-name, .tl-passed .tl-time { color: var(--muted); }

.tl-current .tl-dot { border-color: var(--amber); background: var(--amber); box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.2); }
.tl-current .tl-name { font-weight: 700; color: var(--text); }

.tl-next .tl-dot { border-color: var(--accent); background: var(--surface); }
.tl-next .tl-name { font-weight: 700; color: var(--accent); }

.empty-state { text-align: center; padding: 48px 20px; }
.empty-mark { color: var(--border); margin-bottom: 16px; }
.empty-mark svg { width: 48px; height: 48px; }
.empty-state { font-size: 14px; font-weight: 600; color: var(--text-soft); }
.empty-state .dim { font-size: 12px; font-weight: 400; color: var(--muted); margin-top: 8px; display: block; }

/* ---------------- FOOTER & MOBILE ---------------- */

.panel-footer {
    padding: 12px 20px;
    background: var(--surface-alt);
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 12px;
}

.fleet-mark { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex-shrink: 0; }
.footer-scroll-text {
    font-size: 11px;
    font-weight: 500;
    color: var(--text-soft);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.panel-handle {
    position: absolute;
    left: 24px;
    top: 24px;
    z-index: 1001;
    pointer-events: auto;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-md);
    width: 44px;
    height: 44px;
    border-radius: var(--radius-pill);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
}
.panel-handle:hover { transform: scale(1.05); }
.panel-handle svg { width: 20px; height: 20px; }

/* ---------------- MAP CHROME & MARKERS ---------------- */

.train-marker { background: transparent !important; border: 0 !important; }
.train-head { position: relative; width: 36px; height: 36px; }
.train-heading {
    position: absolute; inset: 0;
    display: flex; align-items: center; justify-content: center;
    transform-origin: center; will-change: transform;
}
.train-heading svg { width: 36px; height: 36px; filter: drop-shadow(0 2px 4px rgba(0,0,0,0.3)); }

.t-body { fill: var(--red); }
.t-window { fill: var(--surface); }
.t-stripe { fill: rgba(255,255,255,0.5); }
.t-wheel { fill: #1f2937; }

.fleet-tnr .t-body { fill: var(--blue); }
.fleet-tnr .t-panto { stroke: #1f2937; }
.fleet-boraq .t-body { fill: var(--grey); }

.train-marker.stopped .t-body { fill: var(--amber); }

.station-marker {
    background: var(--station-dot);
    border: 2px solid var(--border);
    border-radius: 50%;
    box-shadow: var(--shadow-sm);
}

.station-label {
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    font-size: 11px;
    font-weight: 600;
    color: var(--map-label);
    text-shadow: 0 1px 2px var(--bg), 0 -1px 2px var(--bg), 1px 0 2px var(--bg), -1px 0 2px var(--bg);
}
.station-label::before { display: none; }

.track-way { stroke: var(--track-way) !important; fill: none; }
.track-leg { stroke: var(--track-leg) !important; fill: none; }
.track-leg-tnr { stroke: var(--blue) !important; }
.track-leg-boraq { stroke: var(--violet) !important; }

.leaflet-control-zoom { border: none !important; box-shadow: var(--shadow-md) !important; border-radius: var(--radius-md) !important; overflow: hidden; }
.leaflet-control-zoom a { background: var(--surface) !important; color: var(--text) !important; border: none !important; }
.leaflet-control-zoom a:hover { background: var(--surface-hover) !important; }
.leaflet-control-attribution { background: rgba(255, 255, 255, 0.7) !important; backdrop-filter: blur(4px); color: var(--muted) !important; }
:root[data-theme="dark"] .leaflet-control-attribution { background: rgba(15, 23, 42, 0.7) !important; }

@media (max-width: 768px) {
    .overlay { padding: 0; pointer-events: none; }
    
    .panel { 
        position: fixed; 
        bottom: 0; left: 0; right: 0; 
        width: 100%; max-width: none;
        height: 85vh; /* Fixed maximum height for bottom sheet */
        border-radius: 24px 24px 0 0; 
        border-bottom: none;
        transform: translateY(45vh); /* Default start position (half screen) */
        transition: transform 0.3s cubic-bezier(0.25, 1, 0.5, 1);
        will-change: transform;
    }
    
    /* Reveal drag handle on mobile */
    .drag-handle {
        display: flex;
        justify-content: center;
        padding: 10px 0;
        cursor: grab;
        touch-action: none;
        background: var(--surface);
        border-radius: 24px 24px 0 0;
    }
    .drag-handle:active { cursor: grabbing; }
    .drag-bar {
        width: 40px;
        height: 5px;
        background: var(--muted);
        border-radius: 5px;
        opacity: 0.5;
    }
    
    .panel-header { padding-top: 4px; border-bottom: none; }
    .hide-on-mobile { display: none !important; }
    .panel-handle { display: none !important; /* Managed by drag physics now */ }
}