:root {
    --primary-bg: #0f172a;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --accent-bus: #ef4444;
    --accent-metro: #3b82f6;
    --glass-bg: rgba(15, 23, 42, 0.85);
    --glass-border: rgba(255, 255, 255, 0.1);
    --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
    --font-family: 'Outfit', sans-serif;
}

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

body {
    font-family: var(--font-family);
    background-color: var(--primary-bg);
    color: var(--text-primary);
    overflow: hidden;
}

.app-container {
    position: relative;
    width: 100vw;
    height: 100vh; /* Fallback for older browsers */
    height: 100dvh; /* Fix for mobile browser toolbars (like Vivaldi/Safari) */
}

.glass-panel {
    background: var(--glass-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--glass-border);
    border-radius: 20px;
    box-shadow: var(--glass-shadow);
}

.main-header {
    position: absolute;
    top: 24px;
    left: 24px;
    z-index: 1000;
    width: 340px;
    padding: 24px;
    animation: slideIn 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.header-content {
    display: flex;
    flex-direction: column;
}

.header-top {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 12px;
    position: relative;
    cursor: pointer;
}

.logo-wrapper {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.logo-wrapper h1 {
    display: none; /* Hide title to save space for search bar */
}

.header-body {
    display: flex;
    flex-direction: column;
    gap: 12px;
    transition: max-height 0.3s ease, opacity 0.3s ease, margin-top 0.3s ease;
    max-height: 500px;
    opacity: 1;
    margin-top: 12px;
    overflow: hidden;
}

.header-body.collapsed {
    max-height: 0;
    opacity: 0;
    margin-top: 0;
}

.toggle-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    font-size: 0.8rem;
    transition: transform 0.3s ease;
    padding: 4px;
}

.header-body.collapsed ~ .header-top .toggle-icon,
.header-top.collapsed .toggle-icon {
    transform: rotate(-90deg);
}

.search-container {
    position: relative;
    width: 100%;
    flex-grow: 1;
}

#search-input {
    width: 100%;
    padding: 10px 12px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.2);
    color: var(--text-primary);
    font-family: var(--font-family);
    font-size: 0.9rem;
    outline: none;
    transition: border-color 0.2s;
}

#search-input:focus {
    border-color: var(--accent-metro);
}

.search-results {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    margin-top: 8px;
    background: var(--glass-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    box-shadow: var(--glass-shadow);
    max-height: 200px;
    overflow-y: auto;
    z-index: 1001;
}

.search-results.hidden {
    display: none;
}

.search-result-item {
    padding: 10px 12px;
    cursor: pointer;
    font-size: 0.9rem;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    transition: background 0.2s;
}

.search-result-item:hover {
    background: rgba(255, 255, 255, 0.1);
}

.search-result-item:last-child {
    border-bottom: none;
}

.logo-wrapper {
    display: flex;
    align-items: center;
    gap: 12px;
}

.logo-icon {
    width: 32px;
    height: 32px;
    background-image: url('../images/logo.png?v=2');
    background-size: cover;
    background-position: center;
    border-radius: 8px;
    flex-shrink: 0;
    box-shadow: 0 2px 10px rgba(0,0,0,0.2);
}

h1 {
    font-size: 1.5rem;
    font-weight: 700;
    letter-spacing: -0.5px;
}

.subtitle {
    font-size: 0.9rem;
    color: var(--text-secondary);
    line-height: 1.4;
}

.legend {
    margin-top: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: rgba(0,0,0,0.2);
    padding: 12px;
    border-radius: 12px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    transition: opacity 0.3s ease, filter 0.3s ease;
    user-select: none;
}

.legend-item.inactive {
    opacity: 0.4;
    filter: grayscale(100%);
}

.legend-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
}

.bus-dot { background-color: var(--accent-bus); box-shadow: 0 0 10px var(--accent-bus); }
.metro-dot { background-color: var(--accent-metro); box-shadow: 0 0 10px var(--accent-metro); }

.status-indicator {
    margin-top: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.pulse {
    width: 8px;
    height: 8px;
    background-color: #10b981;
    border-radius: 50%;
    animation: pulse 2s infinite;
}

#map {
    width: 100%;
    height: 100%;
    z-index: 1;
}

/* Custom Popup Styling */
.leaflet-popup-content-wrapper {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    color: var(--text-primary);
    border-radius: 12px;
    box-shadow: var(--glass-shadow);
}

.leaflet-popup-tip {
    background: var(--glass-bg);
    border-top: 1px solid var(--glass-border);
    border-left: 1px solid var(--glass-border);
}

.popup-title {
    font-family: var(--font-family);
    font-weight: 600;
    font-size: 1.1rem;
    margin-bottom: 2px;
}

.popup-type {
    font-family: var(--font-family);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

/* Arrivals styling */
.arrivals-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 8px;
}

.arrival-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 10px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px;
    font-family: var(--font-family);
}

.arrival-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.arrival-dest {
    font-size: 0.85rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 130px;
}

.eta-time {
    font-weight: 600;
    font-size: 0.9rem;
    color: #10b981;
}

.line-badge {
    padding: 4px 8px;
    border-radius: 6px;
    font-family: var(--font-family);
    font-size: 0.8rem;
    font-weight: 600;
    min-width: 32px;
    text-align: center;
}

.bus-line {
    background-color: rgba(239, 68, 68, 0.2);
    color: #fca5a5;
    border: 1px solid rgba(239, 68, 68, 0.3);
}

.metro-line {
    background-color: rgba(59, 130, 246, 0.2);
    color: #93c5fd;
    border: 1px solid rgba(59, 130, 246, 0.3);
}

.tram-line {
    background-color: rgba(249, 115, 22, 0.2);
    color: #fdba74;
    border: 1px solid rgba(249, 115, 22, 0.3);
}

.metrobus-line {
    background-color: rgba(255, 184, 28, 0.2);
    color: #FFB81C;
    border: 1px solid rgba(255, 184, 28, 0.3);
}

.loading-pulse {
    font-size: 0.9rem;
    color: var(--text-secondary);
    animation: textPulse 1.5s infinite;
}

.no-data, .error-msg {
    font-size: 0.9rem;
    color: #fca5a5;
    margin-top: 8px;
}

/* Map Controls */
@keyframes slideIn {
    from { opacity: 0; transform: translateY(-20px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes pulse {
    0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7); }
    70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(16, 185, 129, 0); }
    100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); }
}

@keyframes textPulse {
    0% { opacity: 0.5; }
    50% { opacity: 1; }
    100% { opacity: 0.5; }
}

.leaflet-layer,
.leaflet-control-zoom-in,
.leaflet-control-zoom-out,
.leaflet-control-attribution {
    filter: invert(100%) hue-rotate(180deg) brightness(95%) contrast(90%);
}

/* Locate Button */
.locate-btn {
    position: absolute;
    bottom: 30px;
    right: 20px;
    z-index: 9999;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-primary);
    transition: transform 0.2s, background 0.2s;
    border: 1px solid var(--glass-border);
    background: var(--glass-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    box-shadow: var(--glass-shadow);
}

.locate-btn:hover {
    transform: scale(1.05);
    background: rgba(255, 255, 255, 0.1);
}

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

/* PWA Install Button */
.pwa-install-btn {
    position: absolute;
    bottom: calc(40px + env(safe-area-inset-bottom, 0px));
    left: 16px;
    z-index: 1000;
    padding: 10px 16px;
    border-radius: 24px;
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    transition: transform 0.2s ease, opacity 0.3s ease, background 0.2s ease;
    animation: slideUpFadeIn 0.5s ease;
    
    /* Glassmorphism missing styles */
    border: 1px solid var(--glass-border);
    background: var(--glass-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    box-shadow: var(--glass-shadow);
}

.pwa-install-btn:hover {
    transform: translateY(-2px);
    background: var(--accent-metro);
    color: white;
    border-color: transparent;
}

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


/* Mobile Adjustments */
@media (max-width: 600px) {
    .main-header {
        top: 10px;
        left: 10px;
        right: 10px;
        width: auto;
        padding: 12px;
    }
    #search-input {
        padding: 8px 10px;
        font-size: 0.95rem; /* Larger font to prevent iOS zoom */
    }
    .locate-btn {
        bottom: calc(40px + env(safe-area-inset-bottom, 0px)); /* Lift slightly above Leaflet attribution, safe from toolbars */
        right: 16px;
    }
    
    /* Route Mode adjustments for Mobile */
    .main-header.route-mode-active {
        right: 65px; /* Shrink header to make room for exit button */
        transition: right 0.3s ease;
    }
    
    .exit-route-btn {
        padding: 8px; /* Square button on mobile */
        border-radius: 50%; /* Round button */
        width: 40px;
        height: 40px;
        justify-content: center;
        margin-top: 10px; /* Align with header */
    }
    
    .exit-route-btn .text {
        display: none; /* Hide text on mobile */
    }
}

/* Light Mode System Preference */
@media (prefers-color-scheme: light) {
    :root {
        --primary-bg: #f8fafc;
        --text-primary: #0f172a;
        --text-secondary: #475569;
        --glass-bg: rgba(255, 255, 255, 0.85);
        --glass-border: rgba(0, 0, 0, 0.1);
        --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.1);
    }

    /* Remove the dark-mode map inversion filter */
    .leaflet-layer,
    .leaflet-control-zoom-in,
    .leaflet-control-zoom-out,
    .leaflet-control-attribution {
        filter: none;
    }

    /* Adjust specific input and border styles for light mode */
    #search-input {
        background: rgba(0, 0, 0, 0.05);
        border: 1px solid rgba(0, 0, 0, 0.1);
        color: var(--text-primary);
    }
    
    .search-result-item:hover {
        background: rgba(0, 0, 0, 0.05);
    }
    
    .search-result-item {
        border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    }
    
    .locate-btn:hover {
        background: rgba(0, 0, 0, 0.05);
    }
}

/* Route Mode Dimming */
.route-mode-active .leaflet-tile-pane {
    filter: brightness(0.4) saturate(0.5) contrast(1.2);
    transition: filter 0.5s ease;
}

/* Exit Route Button Styles */
.exit-route-btn {
    background: #ef4444;
    color: white;
    font-weight: bold;
    padding: 8px 12px;
    border: none;
    cursor: pointer;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.3s ease;
}

.exit-route-btn:hover {
    background: #dc2626;
}

.exit-route-btn .icon {
    display: flex;
    align-items: center;
    justify-content: center;
}

.disclaimer {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-align: center;
    margin-top: 15px;
    padding-top: 10px;
    border-top: 1px solid var(--glass-border);
    opacity: 0.8;
}

/* Journey Planner UI */
.open-journey-btn {
    position: absolute;
    bottom: 96px;
    left: 24px;
    z-index: 1000;
    display: flex;
    align-items: center;
    padding: 12px 20px;
    color: white;
    background: #10b981; /* Emerald green for routing */
    border: none;
    cursor: pointer;
    border-radius: 30px;
    box-shadow: 0 4px 15px rgba(16, 185, 129, 0.4);
    transition: transform 0.2s, background 0.2s;
}

.open-journey-btn:hover {
    background: #059669;
    transform: translateY(-2px);
}

.journey-panel {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1001;
    border-radius: 20px 20px 0 0;
    padding: 24px;
    max-height: 85vh;
    overflow-y: auto;
    transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.journey-panel.hidden {
    transform: translateY(100%);
}

@media (min-width: 768px) {
    .journey-panel {
        bottom: 24px;
        left: 24px;
        right: auto;
        width: 380px;
        border-radius: 20px;
    }
    .journey-panel.hidden {
        transform: translateX(-150%);
    }
}

.journey-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.close-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 24px;
    cursor: pointer;
    padding: 0 8px;
}

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

.journey-inputs {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.input-group {
    display: flex;
    align-items: center;
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    padding: 4px 12px;
}

.input-group .icon {
    font-size: 1.2rem;
    margin-right: 8px;
}

.input-group input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    padding: 10px 0;
    font-size: 1rem;
    outline: none;
    font-family: var(--font-family);
}

.action-btn {
    background: var(--accent-metro);
    color: white;
    border: none;
    padding: 14px;
    border-radius: 10px;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    margin-top: 8px;
    transition: background 0.2s;
    font-family: var(--font-family);
}

.action-btn:hover {
    background: #2563eb;
}

.journey-results {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.route-card {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    padding: 16px;
    cursor: pointer;
    transition: background 0.2s;
}

.route-card:hover {
    background: rgba(255, 255, 255, 0.1);
}

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

.route-badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 20px;
    font-weight: bold;
    font-size: 0.9rem;
    color: white;
}

.route-time {
    font-size: 1.2rem;
    font-weight: 700;
    color: #10b981;
}

.route-details {
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.route-leg {
    display: flex;
    align-items: center;
    gap: 8px;
}

.realtime-indicator {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 0.8rem;
    color: #10b981;
    margin-top: 8px;
}

.realtime-indicator .pulse {
    width: 6px;
    height: 6px;
}

/* Arrivals Container with scrolling for Mobile */
.arrivals-container {
    max-height: 140px; /* Approximately 3 items */
    overflow-y: auto;
    overflow-x: hidden;
    padding-right: 5px;
    margin-top: 8px;
    border-radius: 4px;
}

/* Elegant scrollbar for arrivals container */
.arrivals-container::-webkit-scrollbar {
    width: 6px;
}

.arrivals-container::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.05);
    border-radius: 3px;
}

.arrivals-container::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 3px;
}

.arrivals-container::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.4);
}

/* Autocomplete Styles */
.autocomplete-wrapper {
    position: relative;
    width: 100%;
}

.autocomplete-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    margin-top: 4px;
    padding: 0;
    list-style: none;
    max-height: 200px;
    overflow-y: auto;
    z-index: 1000;
    box-shadow: var(--glass-shadow);
}

.autocomplete-dropdown.hidden {
    display: none;
}

.autocomplete-item {
    padding: 10px 12px;
    cursor: pointer;
    font-size: 13px;
    color: var(--text-primary);
    border-bottom: 1px solid var(--glass-border);
    transition: background 0.2s ease, color 0.2s ease;
}

.autocomplete-item:last-child {
    border-bottom: none;
}

.autocomplete-item:hover {
    background: rgba(255, 184, 28, 0.15);
    color: #FFB81C;
}

.autocomplete-item.loading {
    color: var(--text-secondary);
    text-align: center;
    cursor: default;
}

.autocomplete-item.loading:hover {
    background: none;
    color: var(--text-secondary);
}

/* Context Menu / Long Press Styles */
.map-context-menu {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 4px;
}

.context-menu-btn {
    background: var(--glass-bg);
    border: 1px solid var(--glass-border);
    color: var(--text-primary);
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 6px;
}

.context-menu-btn:hover {
    background: rgba(255, 184, 28, 0.15);
    border-color: rgba(255, 184, 28, 0.4);
    color: #e6a000;
}

@media (prefers-color-scheme: dark) {
    .context-menu-btn:hover {
        background: rgba(255, 184, 28, 0.2);
        color: #FFB81C;
    }
}
