@import url("https://fonts.googleapis.com/css2?family=Figtree:ital,wght@0,300..900;1,300..900&display=swap");

/* 0. Theme Tokens */
:root {
    --accent: #236192;
    --accent-deep: #003770;
    --accent-hover: #1b4d75;
    --inactive: #F2A900;

    --header-bg: #236192;
    --header-text: #ffffff;

    --surface: #ffffff;
    --surface-2: #f4f6f8;
    --text: #1a2733;
    --text-muted: #5b6b7a;
    --border: #e2e6ea;

    --input-bg: #ffffff;
    --input-text: #236192;
    --input-placeholder: #8aa1b4;

    --map-bg: #e5e3df;
    --shadow: rgba(0, 0, 0, 0.15);
    --shadow-strong: rgba(0, 0, 0, 0.22);

    --link-bg: #eef3f8;
    --link-text: #236192;
    --link-hover-bg: #236192;
    --link-hover-text: #ffffff;
}

body.theme-dark {
    --accent: #5aa0d6;
    --accent-deep: #0b1a2b;
    --accent-hover: #6fb0e0;

    --header-bg: #131c25;
    --header-text: #e9eef3;

    --surface: #1b2733;
    --surface-2: #243240;
    --text: #e6edf3;
    --text-muted: #9aa7b3;
    --border: #2c3a47;

    --input-bg: #243240;
    --input-text: #e6edf3;
    --input-placeholder: #7d8a96;

    --map-bg: #0e1419;
    --shadow: rgba(0, 0, 0, 0.5);
    --shadow-strong: rgba(0, 0, 0, 0.6);

    --link-bg: #243240;
    --link-text: #8fc3ee;
    --link-hover-bg: #5aa0d6;
    --link-hover-text: #0b1a2b;
}

/* 1. Global Reset & Base Styles */
body, html {
    margin: 0;
    padding: 0;
    height: 100%;
    width: 100%;
    overflow: hidden;
    font-family: 'Figtree', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
    background: var(--surface);
    color: var(--text);
}

.main-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
}

/* 2. Header & Navigation */
.search-header {
    padding: 10px 16px;
    background: var(--header-bg);
    box-shadow: 0 2px 12px var(--shadow);
    z-index: 1000;
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 12px;
    transition: background 0.25s ease;
}

.header-branding {
    display: flex;
    align-items: center;
    color: var(--header-text);
    gap: 8px;
}

.header-logo {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.header-title {
    font-size: 18px;
    font-weight: 700;
    white-space: nowrap;
    letter-spacing: 0.2px;
}

/* 3. Search Box */
.search-wrapper {
    position: relative;
    width: 100%;
    max-width: 600px;
    justify-self: center;
    display: flex;
    align-items: center;
}

.search-icon-container {
    position: absolute;
    left: 10px;
    display: flex;
    align-items: center;
    z-index: 10;
    pointer-events: none;
}

#location-search {
    width: 100%;
    padding: 11px 70px 11px 42px;
    border-radius: 10px;
    border: 1px solid transparent;
    font-size: 16px;
    font-family: 'Figtree';
    box-sizing: border-box;
    background: var(--input-bg);
    color: var(--input-text);
    -webkit-appearance: none;
    transition: box-shadow 0.2s ease, border-color 0.2s ease;
}

#location-search::placeholder {
    color: var(--input-placeholder);
}

#location-search:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(90, 160, 214, 0.25);
}

.input-actions {
    position: absolute;
    right: 8px;
    display: flex;
    align-items: center;
    gap: 5px;
    z-index: 11;
}

.clear-btn {
    background: none;
    border: none;
    cursor: pointer;
    display: flex;
    padding: 0;
}

/* 4. Map & Overlays */
#map-area {
    position: relative;
    flex-grow: 1;
}

#map {
    height: 100%;
    width: 100%;
    background: var(--map-bg);
    transition: background 0.25s ease;
}

#zoom-notice {
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--accent-deep);
    color: #fff;
    padding: 10px 20px;
    border-radius: 30px;
    font-size: 14px;
    z-index: 1002;
    pointer-events: none;
    white-space: nowrap;
    box-shadow: 0 4px 14px var(--shadow);
}

/* 5. Autocomplete Dropdown */
.autocomplete-items {
    position: absolute;
    border: 1px solid var(--border);
    z-index: 1001;
    top: calc(100% + 6px);
    left: 0;
    right: 0;
    background-color: var(--surface);
    max-height: 250px;
    overflow-y: auto;
    border-radius: 10px;
    box-shadow: 0 8px 24px var(--shadow);
    overflow: hidden;
}

.autocomplete-item {
    padding: 12px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    font-size: 14px;
    color: var(--text);
}

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

.autocomplete-item:hover {
    background-color: var(--surface-2);
    color: var(--accent);
}

/* 6. Header Action Buttons */
.header-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
}

.icon-btn {
    background: none;
    border: none;
    color: var(--header-text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 8px;
    border-radius: 50%;
    transition: background 0.2s;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.18);
}

.icon-btn svg {
    width: 24px;
    height: 24px;
    fill: currentColor;
}

/* Sun shows in dark mode (click -> light); moon shows in light mode (click -> dark) */
.icon-sun { display: none; }
.icon-moon { display: block; }
body.theme-dark .icon-sun { display: block; }
body.theme-dark .icon-moon { display: none; }

/* 7. MapLibre Popup */
.maplibregl-popup-content {
    /* MapLibre sets a Helvetica/Arial font on .maplibregl-map, and the popup is
       a child of it - so `inherit` would pull Helvetica, not Hanken Grotesk.
       Set the stack explicitly. */
    font-family: 'Figtree', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    padding: 0;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 8px 28px var(--shadow-strong);
    background: var(--surface);
    color: var(--text);
}

.maplibregl-popup-close-button {
    color: #fff;
    font-size: 18px;
    padding: 4px 10px;
    right: 0;
    top: 0;
    border-radius: 0 12px 0 0;
    transition: background 0.2s;
}

.maplibregl-popup-close-button:hover {
    background-color: rgba(0, 0, 0, 0.2);
}

.maplibregl-popup-tip {
    border-top-color: var(--surface) !important;
    border-bottom-color: var(--surface) !important;
}

.stop-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 11px 34px 11px 14px;
    font-size: 14px;
    font-weight: 700;
    color: #fff;
}

.stop-title {
    line-height: 1.3;
}

.stop-header.status-active { background-color: #236192; }
.stop-header.status-inactive { background-color: #F2A900; }
.stop-header.status-rail { background-color: #C81E1E; }

.status-pill {
    flex-shrink: 0;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.4px;
    padding: 2px 8px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.22);
    color: #fff;
}

.popup-container {
    padding: 12px 14px 14px;
}

.stop-locality {
    color: var(--accent);
    display: block;
    margin-bottom: 10px;
    font-weight: 600;
    font-size: 13px;
}

.detail-grid {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.detail-row {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    font-size: 13px;
    line-height: 1.4;
    padding: 5px 0;
    border-bottom: 1px solid var(--border);
}

.detail-row:last-child {
    border-bottom: none;
}

.detail-label {
    color: var(--text-muted);
    font-weight: 600;
    flex-shrink: 0;
}

.detail-value {
    color: var(--text);
    text-align: right;
    word-break: break-word;
}

/* Notes can be long; stack label above value and left-align. */
.detail-row-note {
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
}

.detail-row-note .detail-value {
    text-align: left;
}

.detail-label {
    color: var(--text-muted);
    font-weight: 600;
    flex-shrink: 0;
}

.detail-value {
    color: var(--text);
    text-align: right;
    word-break: break-word;
}

/* Notes can be long; stack label above value and left-align. */
.detail-row-note {
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
}

.detail-row-note .detail-value {
    text-align: left;
}

.copyable {
    cursor: pointer;
    transition: color 0.2s ease;
    text-decoration: underline dotted;
}

.copyable:hover {
    color: var(--accent);
}

.atco-tag {
    padding: 2px 6px;
    border-radius: 4px;
    font-weight: bold;
    background: var(--link-bg);
    color: var(--accent);
}

.popup-links {
    display: flex;
    gap: 8px;
    margin-top: 14px;
}

.popup-link {
    flex: 1;
    text-align: center;
    padding: 8px 10px;
    border-radius: 8px;
    background: var(--link-bg);
    color: var(--link-text);
    font-size: 12.5px;
    font-weight: 700;
    text-decoration: none;
    transition: background 0.2s ease, color 0.2s ease;
}

.popup-link:hover {
    background: var(--link-hover-bg);
    color: var(--link-hover-text);
}

/* 8. Help Modal */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 2000;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(2px);
}

.modal-content {
    background: var(--surface);
    border-radius: 12px;
    width: 90%;
    max-width: 460px;
    box-shadow: 0 12px 40px var(--shadow-strong);
    animation: modalFadeIn 0.3s ease;
    display: flex;
    flex-direction: column;
    color: var(--text);
    overflow: hidden;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 20px;
    background: var(--header-bg);
    color: var(--header-text);
}

.modal-header h2 {
    margin: 0;
    font-size: 18px;
}

.close-modal-btn {
    background: none;
    border: none;
    color: var(--header-text);
    font-size: 26px;
    cursor: pointer;
    line-height: 1;
    padding: 0;
}

.modal-body {
    padding: 20px;
    max-height: 70vh;
    overflow-y: auto;
}

.help-section {
    margin-bottom: 20px;
}

.help-section:last-child {
    margin-bottom: 0;
}

.help-section h3 {
    margin: 0 0 8px 0;
    font-size: 16px;
    color: var(--accent);
}

.help-section p {
    margin: 0 0 10px 0;
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
}

.help-section ul {
    margin: 0;
    padding-left: 0;
    list-style: none;
}

.help-section li {
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    font-size: 14px;
    gap: 10px;
}

.legend-dot {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    display: inline-block;
    border: 2px solid #fff;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.2);
}

.legend-dot.active { background-color: #236192; }
.legend-dot.inactive { background-color: #F2A900; }
.legend-dot.rail { background-color: #C81E1E; }

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

/* 9. Responsive */
@media (max-width: 600px) {
    .header-title {
        display: none;
    }
    .search-header {
        grid-template-columns: auto 1fr auto;
    }
}
