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

/* CSS Custom Properties */
:root {
    /* Colors - Dark Terminal Theme (default) */
    --bg: #0d1117;
    --bg-elevated: #161b22;
    --fg: #c9d1d9;
    --fg-muted: #8b949e;
    --border: #30363d;
    --border-hover: #58a6ff;
    --accent: #58a6ff;
    --accent-hover: #79b8ff;
    --success: #3fb950;
    --warning: #d29922;
    --error: #f85149;
    --hover: #21262d;
    --hover-elevated: #30363d;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);

    /* Spacing */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 12px;
    --space-lg: 16px;
    --space-xl: 20px;
    --space-2xl: 40px;

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-base: 200ms ease;
    --transition-slow: 300ms ease;

    /* Border Radius */
    --radius-sm: 4px;
    --radius-md: 6px;
    --radius-lg: 8px;

    /* Shadows */
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
}

/* Light Mode */
[data-theme="light"] {
    --bg: #ffffff;
    --bg-elevated: #f6f8fa;
    --fg: #24292f;
    --fg-muted: #6e7781;
    --border: #d0d7de;
    --border-hover: #0969da;
    --accent: #0969da;
    --accent-hover: #0550ae;
    --success: #1a7f37;
    --warning: #9a6700;
    --error: #cf222e;
    --hover: #f3f4f6;
    --hover-elevated: #eaeef2;
    --shadow-sm: 0 1px 2px rgba(31, 35, 40, 0.04);
    --shadow-md: 0 4px 12px rgba(31, 35, 40, 0.1);
    --shadow-lg: 0 8px 24px rgba(31, 35, 40, 0.15);
}

/* Transport Mode Toggle */
.mode-toggle {
    display: flex;
    gap: var(--space-sm);
    margin-bottom: var(--space-lg);
}

.mode-btn {
    flex: 1;
    padding: var(--space-sm) var(--space-md);
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--fg-muted);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
}

.mode-btn:hover {
    background: var(--hover);
    border-color: var(--border-hover);
}

.mode-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--bg);
}

.mode-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Prevent child elements from intercepting clicks in Safari */
.mode-btn * {
    pointer-events: none;
}

.mode-icon {
    font-size: 16px;
}

/* Theme Toggle Button */
.theme-toggle {
    position: fixed;
    top: 20px;
    right: 20px;
    padding: 8px;
    font-size: 18px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--fg);
    cursor: pointer;
    transition: all var(--transition-fast);
    z-index: 1000;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.theme-toggle:hover {
    background: var(--hover);
    border-color: var(--border-hover);
    transform: scale(1.05);
}

.theme-toggle:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Input focus shadow adjustment for light mode */
[data-theme="light"] input[type="text"]:focus,
[data-theme="light"] input[type="date"]:focus {
    box-shadow: 0 0 0 3px rgba(9, 105, 218, 0.15);
}

/* Button in light mode */
[data-theme="light"] button {
    color: #ffffff;
}

/* Swap button in light mode */
[data-theme="light"] .swap-btn {
    background: var(--bg-elevated);
    color: var(--fg);
}

/* Base Styles */
html {
    scroll-behavior: smooth;
}

/* Screen Reader Only - Visually hidden but accessible to screen readers */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

body {
    font-family: 'JetBrains Mono', monospace;
    background: var(--bg);
    color: var(--fg);
    padding: var(--space-2xl) var(--space-lg);
    font-size: 14px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Container */
.container {
    max-width: 900px;
    margin: 0 auto;
}

/* Input Row */
.input-row {
    display: flex;
    gap: var(--space-md);
    align-items: flex-end;
    margin-bottom: var(--space-lg);
    flex-wrap: wrap;
}

.field {
    flex: 1;
    min-width: 140px;
}

.field-small {
    flex: 0 0 140px;
}

.field-button {
    flex: 0 0 auto;
}

.field-swap {
    flex: 0 0 auto;
    min-width: auto;
}

.field-swap label {
    visibility: hidden;
    height: 16px;
}

/* Swap Button */
.swap-btn {
    padding: var(--space-sm) var(--space-md);
    background: transparent;
    border: 1px solid var(--border);
    color: var(--fg);
    font-size: 16px;
    height: 35px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: 
        background var(--transition-fast),
        border-color var(--transition-fast),
        transform var(--transition-slow);
    will-change: transform;
}

.swap-btn:hover {
    background: var(--hover);
    border-color: var(--accent);
}

.swap-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

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

/* Labels */
label {
    display: block;
    color: var(--fg-muted);
    font-size: 11px;
    margin-bottom: var(--space-xs);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 500;
}

/* Form Inputs */
input[type="text"],
input[type="date"] {
    width: 100%;
    padding: var(--space-sm) var(--space-md);
    font-family: inherit;
    font-size: 14px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--fg);
    outline: none;
    transition: 
        border-color var(--transition-fast),
        box-shadow var(--transition-fast),
        background var(--transition-fast);
}

input[type="text"]:hover,
input[type="date"]:hover {
    border-color: var(--hover-elevated);
}

input[type="text"]:focus,
input[type="date"]:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(88, 166, 255, 0.1);
    background: var(--bg-elevated);
}

input::placeholder {
    color: var(--fg-muted);
    opacity: 0.7;
}

/* Buttons */
button {
    padding: var(--space-sm) var(--space-xl);
    font-family: inherit;
    font-size: 14px;
    background: var(--accent);
    border: none;
    border-radius: var(--radius-sm);
    color: var(--bg);
    cursor: pointer;
    font-weight: 600;
    letter-spacing: 0.3px;
    transition: 
        background var(--transition-fast),
        transform var(--transition-fast),
        box-shadow var(--transition-fast);
    will-change: transform;
}

button:hover {
    background: var(--accent-hover);
    box-shadow: var(--shadow-sm);
}

button:focus-visible {
    outline: 2px solid var(--fg);
    outline-offset: 2px;
}

button:active {
    transform: translateY(1px);
}

/* Autocomplete */
.autocomplete-wrapper {
    position: relative;
}

.autocomplete-list {
    position: absolute;
    top: calc(100% + var(--space-xs));
    left: 0;
    right: 0;
    max-height: 240px;
    overflow-y: auto;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    z-index: 1000;
    display: none;
    box-shadow: var(--shadow-md);
    animation: slideDown var(--transition-fast);
}

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

.autocomplete-list.active {
    display: block;
}

.autocomplete-item {
    padding: var(--space-sm) var(--space-md);
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    transition: background var(--transition-fast);
}

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

.autocomplete-item:hover,
.autocomplete-item.selected {
    background: var(--hover);
}

.autocomplete-item.selected {
    background: var(--hover-elevated);
}

.autocomplete-item-highlight {
    color: var(--accent);
    font-weight: 600;
}

/* Error Message */
.error {
    color: var(--error);
    padding: var(--space-sm) 0;
    font-size: 13px;
    animation: shake var(--transition-base);
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-4px); }
    75% { transform: translateX(4px); }
}

/* Loading State */
.loading {
    color: var(--fg-muted);
    padding: var(--space-xl) 0;
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.loading::after {
    content: '';
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* Results Section */
.results {
    animation: fadeIn var(--transition-base);
}

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

/* Route Display */
.route {
    margin-bottom: var(--space-lg);
    font-size: 16px;
    padding-bottom: var(--space-md);
    border-bottom: 1px solid var(--border);
}

.route #fromDisplay,
.route #toDisplay {
    color: var(--accent);
    font-weight: 600;
}

.route-date {
    color: var(--fg-muted);
    margin-left: var(--space-sm);
    font-size: 13px;
}

/* Timetable */
.timetable {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    margin-top: var(--space-sm);
}

.timetable th {
    text-align: left;
    padding: var(--space-md) var(--space-sm);
    color: var(--fg-muted);
    font-size: 11px;
    text-transform: uppercase;
    font-weight: 600;
    letter-spacing: 0.5px;
    border-bottom: 2px solid var(--border);
}

.timetable td {
    padding: var(--space-md) var(--space-sm);
    border-bottom: 1px solid var(--border);
    transition: background var(--transition-fast);
}

.timetable tr {
    transition: background var(--transition-fast);
}

.timetable tr:hover {
    background: var(--hover);
}

.timetable tr:last-child td {
    border-bottom: none;
}

/* Trip Status Styles */
.trip-upcoming,
.train-upcoming {
    color: var(--fg);
    font-weight: 500;
}

.trip-upcoming::before,
.train-upcoming::before {
    content: '* ';
    color: var(--success);
}

.trip-past,
.train-past {
    color: var(--fg-muted);
}

.trip-past::before,
.train-past::before {
    content: '~ ';
    color: var(--fg-muted);
}

.time-upcoming {
    color: var(--fg);
    font-weight: 500;
}

.time-past {
    color: var(--fg-muted);
}

.duration {
    color: var(--fg-muted);
}

/* Legend */
.legend {
    margin-top: var(--space-lg);
    padding-top: var(--space-lg);
    border-top: 1px solid var(--border);
    color: var(--fg-muted);
    font-size: 12px;
}

.legend span {
    margin-right: var(--space-lg);
}

/* Filter Row */
.filter-row {
    margin-bottom: var(--space-lg);
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    cursor: pointer;
    color: var(--fg-muted);
    font-size: 13px;
    text-transform: none;
    transition: color var(--transition-fast);
}

.checkbox-label:hover {
    color: var(--fg);
}

.checkbox-label input[type="checkbox"] {
    width: 16px;
    height: 16px;
    cursor: pointer;
    accent-color: var(--accent);
    border-radius: var(--radius-sm);
}

.checkbox-label span {
    color: var(--fg);
}

/* Recent Searches */
.recent-searches {
    margin-bottom: var(--space-lg);
}

.recent-searches label {
    margin-bottom: var(--space-sm);
}

.recent-list {
    display: flex;
    gap: var(--space-sm);
    flex-wrap: wrap;
}

.recent-item {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: var(--space-sm) var(--space-md);
    background: var(--hover);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 13px;
    transition: 
        border-color var(--transition-fast),
        background var(--transition-fast),
        transform var(--transition-fast);
}

.recent-item:hover {
    border-color: var(--accent);
    background: var(--bg-elevated);
    transform: translateY(-1px);
}

.recent-item .from,
.recent-item .to {
    color: var(--accent);
    font-weight: 500;
}

.recent-item .arrow {
    color: var(--fg-muted);
}

/* Scrollbar Styling */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg);
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: var(--radius-sm);
}

::-webkit-scrollbar-thumb:hover {
    background: var(--hover-elevated);
}

/* Focus Visible Styles */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Selection Styles */
::selection {
    background: rgba(88, 166, 255, 0.3);
    color: var(--fg);
}

/* Responsive Design */
@media (max-width: 640px) {
    body {
        padding: var(--space-lg) var(--space-md);
        font-size: 15px;
    }

    .container {
        padding: 0;
    }

    .input-row {
        flex-direction: column;
        align-items: stretch;
        gap: var(--space-lg);
    }

    .field,
    .field-small,
    .field-button,
    .field-swap {
        flex: 1;
        min-width: auto;
        width: 100%;
    }

    .field-swap {
        order: 2;
        display: flex;
        justify-content: center;
    }

    .field-swap label {
        display: none;
    }

    .swap-btn {
        width: 48px;
        height: 48px;
        font-size: 20px;
    }

    button {
        width: 100%;
        padding: var(--space-md) var(--space-xl);
    }

    input[type="text"],
    input[type="date"] {
        font-size: 16px;
        padding: var(--space-md);
    }

    .autocomplete-list {
        max-height: 280px;
    }

    .timetable {
        font-size: 14px;
    }

    .timetable th,
    .timetable td {
        padding: var(--space-sm) var(--space-xs);
    }

    .recent-list {
        flex-direction: column;
    }

    .recent-item {
        justify-content: center;
    }

    .route {
        font-size: 15px;
    }

    .route-date {
        display: block;
        margin-left: 0;
        margin-top: var(--space-xs);
    }
}

@media (max-width: 480px) {
    body {
        padding: var(--space-md);
    }

    .timetable th:nth-child(4),
    .timetable td:nth-child(4) {
        display: none;
    }
}

/* Print Styles */
@media print {
    body {
        background: white;
        color: black;
        padding: 0;
    }

    .input-row,
    .filter-row,
    .recent-searches,
    .error,
    .loading {
        display: none !important;
    }

    .results {
        display: block !important;
    }

    .timetable tr:hover {
        background: transparent;
    }
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* High Contrast Mode */
@media (prefers-contrast: high) {
    :root {
        --border: #666;
        --fg-muted: #999;
    }

    .train-upcoming::before,
    .train-past::before {
        font-weight: bold;
    }
}
