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

:root {
    --bg: #0f0f1a;
    --bg2: #1a1a2e;
    --bg3: #16213e;
    --surface: #1e2a4a;
    --surface-hover: #253560;
    --text: #e8e8f0;
    --text-dim: #8890a8;
    --accent: #4fc3f7;
    --accent2: #00bcd4;
    --danger: #ef5350;
    --danger-glow: rgba(239, 83, 80, 0.3);
    --warning: #ffa726;
    --warning-glow: rgba(255, 167, 38, 0.3);
    --success: #66bb6a;
    --radius: 12px;
    --radius-sm: 8px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100dvh;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ===== LOADING ===== */
#loadingOverlay {
    position: fixed; inset: 0;
    background: rgba(15, 15, 26, 0.85);
    backdrop-filter: blur(4px);
    display: none; justify-content: center; align-items: center;
    flex-direction: column; gap: 16px;
    z-index: 9999; color: var(--text-dim);
}
#loadingOverlay.active { display: flex; }

.loader-ring { display: inline-block; position: relative; width: 48px; height: 48px; }
.loader-ring div {
    position: absolute; width: 38px; height: 38px;
    border: 3px solid transparent; border-top-color: var(--accent);
    border-radius: 50%; animation: spin 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
}
.loader-ring div:nth-child(1) { animation-delay: -0.45s; }
.loader-ring div:nth-child(2) { animation-delay: -0.3s; }
.loader-ring div:nth-child(3) { animation-delay: -0.15s; }
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

/* ===== HEADER ===== */
.app-header {
    background: var(--bg2);
    border-bottom: 1px solid rgba(255,255,255,0.06);
    padding: var(--safe-top) 0 0;
    position: sticky; top: 0; z-index: 100;
}
.header-content {
    display: flex; align-items: center; justify-content: space-between;
    padding: 12px 16px; max-width: 600px; margin: 0 auto;
}
.logo { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; color: var(--accent); }

.icon-btn {
    background: none; border: none; color: var(--text-dim);
    cursor: pointer; padding: 8px; border-radius: 50%;
    transition: background 0.2s;
}
.icon-btn:hover { background: rgba(255,255,255,0.08); }
.icon-btn-light { color: rgba(255,255,255,0.8); }

/* ===== ROUTE FORM ===== */
.route-form-container { max-width: 600px; margin: 0 auto; padding: 16px; }

.info-banner {
    background: var(--bg3);
    border: 1px solid rgba(79, 195, 247, 0.15);
    border-radius: var(--radius);
    padding: 16px; margin-bottom: 20px; text-align: center;
}
.info-banner p { font-size: 14px; line-height: 1.5; }
.info-sub { color: var(--text-dim); margin-top: 6px; font-size: 12px !important; }

.form-grid { display: flex; flex-direction: column; gap: 12px; }

.form-section {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 14px; border: 1px solid rgba(255,255,255,0.05);
}
.section-label {
    font-size: 11px; font-weight: 700; letter-spacing: 1.5px;
    margin-bottom: 10px; display: flex; align-items: center; gap: 6px;
}
.from-label { color: var(--accent); }
.from-label::before { content: "📍"; }
.to-label { color: var(--warning); }
.to-label::before { content: "🎯"; }

.select-group { display: flex; flex-direction: column; gap: 8px; }

.form-select {
    width: 100%; padding: 10px 12px;
    background: var(--bg2); color: var(--text);
    border: 1px solid rgba(255,255,255,0.1); border-radius: var(--radius-sm);
    font-size: 14px; font-family: inherit;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%238890a8' viewBox='0 0 16 16'%3E%3Cpath d='M8 11L3 6h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 12px center;
    transition: border-color 0.2s;
}
.form-select:focus { outline: none; border-color: var(--accent); }
.form-select:disabled { opacity: 0.4; cursor: not-allowed; }

.location-toggle {
    display: flex; align-items: center; gap: 8px;
    margin-bottom: 10px; cursor: pointer;
    font-size: 13px; color: var(--accent);
    user-select: none;
}
.location-toggle input { accent-color: var(--accent); width: 16px; height: 16px; cursor: pointer; }
.location-toggle svg { flex-shrink: 0; }

.my-location-info {
    background: rgba(79, 195, 247, 0.1);
    border: 1px solid rgba(79, 195, 247, 0.25);
    border-radius: var(--radius-sm);
    padding: 10px 12px; margin-bottom: 8px;
    font-size: 13px; color: var(--accent);
    display: flex; align-items: center; gap: 8px;
}
.my-location-info.hidden { display: none; }
.my-location-info .loc-spinner {
    width: 14px; height: 14px;
    border: 2px solid rgba(79, 195, 247, 0.3);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.form-divider {
    display: flex; justify-content: center; color: var(--text-dim);
    padding: 4px 0;
}

.btn-primary {
    width: 100%; padding: 14px; margin-top: 16px;
    background: linear-gradient(135deg, var(--danger), #c62828);
    color: white; border: none; border-radius: var(--radius);
    font-size: 16px; font-weight: 700; letter-spacing: 1px;
    cursor: pointer; transition: all 0.2s;
    box-shadow: 0 4px 15px rgba(239, 83, 80, 0.3);
}
.btn-primary:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 6px 20px rgba(239, 83, 80, 0.4); }
.btn-primary:active:not(:disabled) { transform: translateY(1px); }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

/* ===== MAP ===== */
#mapSection { max-width: 600px; margin: 16px auto; padding: 0 16px; }
#map {
    height: 350px; border-radius: var(--radius);
    border: 1px solid rgba(255,255,255,0.08);
}

/* ===== ROUTE RESULT ===== */
#routeResult {
    max-width: 600px; margin: 0 auto; padding: 0 16px 16px;
}
.result-header {
    font-size: 16px; font-weight: 700; text-align: center;
    padding: 12px 0; color: var(--text);
}
.result-header .arrow { color: var(--accent); margin: 0 8px; }

.result-cards {
    display: grid; grid-template-columns: repeat(3, 1fr);
    gap: 10px; margin: 12px 0;
}
.result-card {
    background: var(--surface);
    border-radius: var(--radius); padding: 14px 8px;
    text-align: center; border: 1px solid rgba(255,255,255,0.05);
}
.result-card .count { font-size: 28px; font-weight: 800; }
.result-card .label { font-size: 11px; color: var(--text-dim); margin-top: 4px; }
.result-card.radar-card .count { color: var(--danger); }
.result-card.control-card .count { color: var(--success); }
.result-card.corridor-card .count { color: var(--accent); }

.city-details {
    background: var(--surface); border-radius: var(--radius);
    padding: 14px; margin-top: 12px;
    border: 1px solid rgba(255,255,255,0.05);
}
.city-row { padding: 8px 0; border-bottom: 1px solid rgba(255,255,255,0.05); }
.city-row:last-child { border-bottom: none; }
.city-name { font-weight: 600; margin-bottom: 6px; }
.city-stats { display: flex; gap: 10px; flex-wrap: wrap; }
.badge {
    display: inline-flex; align-items: center; gap: 4px;
    padding: 2px 8px; border-radius: 4px; font-size: 12px; font-weight: 600;
}
.badge-danger { background: rgba(239, 83, 80, 0.15); color: var(--danger); }
.badge-success { background: rgba(102, 187, 106, 0.15); color: var(--success); }

/* ===== START NAV ===== */
#startNavSection {
    max-width: 600px; margin: 0 auto; padding: 0 16px 32px;
}
.btn-nav {
    width: 100%; padding: 16px;
    background: linear-gradient(135deg, #1565c0, #0d47a1);
    color: white; border: none; border-radius: var(--radius);
    font-size: 16px; font-weight: 700; letter-spacing: 1px;
    cursor: pointer; display: flex; align-items: center;
    justify-content: center; gap: 10px;
    box-shadow: 0 4px 15px rgba(21, 101, 192, 0.4);
    transition: all 0.2s;
}
.btn-nav:hover { transform: translateY(-1px); box-shadow: 0 6px 20px rgba(21, 101, 192, 0.5); }

/* ===== NAVIGATION SCREEN ===== */
#navScreen {
    position: fixed; inset: 0;
    background: var(--bg); z-index: 500;
    display: flex; flex-direction: column;
}
#navScreen.hidden { display: none !important; }

.nav-top-bar {
    display: flex; align-items: center; justify-content: space-between;
    padding: var(--safe-top) 12px 0;
    background: var(--bg2);
    border-bottom: 1px solid rgba(255,255,255,0.06);
    min-height: 56px;
}
.nav-stop-btn {
    display: flex; align-items: center; gap: 6px;
    background: rgba(239, 83, 80, 0.15); color: var(--danger);
    border: 1px solid rgba(239, 83, 80, 0.3);
    border-radius: var(--radius-sm); padding: 8px 14px;
    font-weight: 600; font-size: 14px; cursor: pointer;
    font-family: inherit;
}
.nav-speed-display {
    display: flex; align-items: baseline; gap: 4px;
}
.nav-speed-display span { font-size: 32px; font-weight: 800; color: white; }
.nav-speed-display small { font-size: 12px; color: var(--text-dim); }

#navMap { flex: 1; }

/* ===== ALERT BANNER ===== */
.alert-banner {
    position: absolute; left: 12px; right: 12px; bottom: 80px;
    background: var(--bg2);
    border-radius: var(--radius);
    padding: 14px 16px;
    display: flex; align-items: center; gap: 12px;
    z-index: 600;
    border: 2px solid var(--warning);
    box-shadow: 0 0 30px var(--warning-glow);
    animation: alertPulse 2s infinite;
    transition: all 0.3s;
}
.alert-banner.hidden { display: none !important; }
.alert-banner.alert-close {
    border-color: var(--danger);
    box-shadow: 0 0 40px var(--danger-glow);
    animation: alertPulseDanger 1s infinite;
}
.alert-icon { font-size: 28px; }
.alert-title { font-weight: 700; font-size: 16px; }
.alert-distance { font-size: 14px; color: var(--text-dim); }
.alert-speed-limit { font-size: 12px; color: var(--warning); margin-top: 2px; }

@keyframes alertPulse {
    0%, 100% { box-shadow: 0 0 20px var(--warning-glow); }
    50% { box-shadow: 0 0 40px var(--warning-glow); }
}
@keyframes alertPulseDanger {
    0%, 100% { box-shadow: 0 0 20px var(--danger-glow); }
    50% { box-shadow: 0 0 50px var(--danger-glow); }
}

/* ===== NAV BOTTOM BAR ===== */
.nav-bottom-bar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 16px;
    padding-bottom: calc(14px + var(--safe-bottom));
    background: var(--bg2);
    border-top: 1px solid rgba(255,255,255,0.06);
}
.nav-next-label { font-size: 11px; color: var(--text-dim); letter-spacing: 0.5px; }
.nav-next-value { font-size: 14px; font-weight: 600; margin-top: 2px; }
.nav-next-distance span { font-size: 20px; font-weight: 800; color: var(--accent); }

/* ===== SETTINGS MODAL ===== */
.modal-overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(4px);
    z-index: 1000;
    display: flex; align-items: flex-end; justify-content: center;
}
.modal-overlay.hidden { display: none !important; }

.modal-content {
    background: var(--bg2);
    border-radius: var(--radius) var(--radius) 0 0;
    width: 100%; max-width: 500px;
    padding: 20px 20px calc(20px + var(--safe-bottom));
    animation: slideUp 0.3s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }

.modal-header {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 20px;
}
.modal-header h3 { font-size: 18px; }

.setting-item {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 0;
    border-bottom: 1px solid rgba(255,255,255,0.05);
}
.setting-item:last-child { border-bottom: none; }
.setting-item label { font-size: 14px; }

.range-group { display: flex; align-items: center; gap: 10px; }
.range-group input[type="range"] {
    width: 120px; accent-color: var(--accent);
}
.range-group span { font-size: 14px; font-weight: 600; color: var(--accent); min-width: 44px; text-align: right; }

/* Toggle switch */
.toggle { position: relative; display: inline-block; width: 48px; height: 26px; cursor: pointer; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
    position: absolute; inset: 0;
    background: var(--surface); border-radius: 26px;
    transition: 0.3s;
}
.toggle-slider::before {
    content: ""; position: absolute;
    width: 20px; height: 20px; left: 3px; bottom: 3px;
    background: var(--text-dim); border-radius: 50%;
    transition: 0.3s;
}
.toggle input:checked + .toggle-slider { background: var(--accent); }
.toggle input:checked + .toggle-slider::before { transform: translateX(22px); background: white; }

/* ===== LEAFLET OVERRIDES ===== */
.leaflet-control-attribution { display: none !important; }
.leaflet-control-zoom a {
    background: var(--bg2) !important; color: var(--text) !important;
    border-color: rgba(255,255,255,0.1) !important;
}

/* Legend */
.map-legend {
    background: var(--bg2) !important; color: var(--text) !important;
    padding: 10px 14px !important; border-radius: var(--radius-sm) !important;
    border: 1px solid rgba(255,255,255,0.08) !important;
    font-size: 12px; line-height: 20px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.3) !important;
}
.map-legend div { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.map-legend div:last-child { margin-bottom: 0; }
.legend-line {
    display: inline-block; width: 24px; height: 3px; border-radius: 2px;
}
.legend-line-dash {
    display: inline-block; width: 24px; height: 0;
    border-top: 3px dashed; border-radius: 0;
}

/* Nav user marker pulse */
.user-marker-pulse {
    width: 20px; height: 20px;
    background: var(--accent);
    border: 3px solid white;
    border-radius: 50%;
    box-shadow: 0 0 0 0 rgba(79, 195, 247, 0.5);
    animation: userPulse 2s infinite;
}
@keyframes userPulse {
    0% { box-shadow: 0 0 0 0 rgba(79, 195, 247, 0.5); }
    70% { box-shadow: 0 0 0 14px rgba(79, 195, 247, 0); }
    100% { box-shadow: 0 0 0 0 rgba(79, 195, 247, 0); }
}

/* ===== RESPONSIVE ===== */
@media (min-width: 480px) {
    .select-group { flex-direction: row; }
    .select-group .form-select { flex: 1; }
    .form-divider svg { transform: rotate(0deg); }
}
