:root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-2: #eef1f5;
    --text: #1b2330;
    --muted: #667085;
    --border: #dde2ea;
    --accent: #1d4ed8;
    --accent-soft: #e0e8ff;
    --pilot: #2563eb;
    --pilot-soft: #e3ecff;
    --atc: #c2410c;
    --atc-soft: #fff0e6;
    --traffic: #4b5563;
    --traffic-soft: #eef0f3;
    --track: #7c3aed;
    --ok: #16a34a;
    --danger: #b91c1c;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
    color-scheme: light;
}

/* dark theme: the system's choice, unless overridden by the theme button (data-theme on <html>) */

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0f141b;
        --surface: #171d26;
        --surface-2: #1f2733;
        --text: #e6e9ee;
        --muted: #8b95a5;
        --border: #2a3441;
        --accent: #7aa2ff;
        --accent-soft: #1c2a4a;
        --pilot: #7aa2ff;
        --pilot-soft: #1b2944;
        --atc: #fb923c;
        --atc-soft: #3a2416;
        --traffic: #a3acb9;
        --traffic-soft: #232b36;
        --shadow: none;
        color-scheme: dark;
    }
}

:root[data-theme="dark"] {
    --bg: #0f141b;
    --surface: #171d26;
    --surface-2: #1f2733;
    --text: #e6e9ee;
    --muted: #8b95a5;
    --border: #2a3441;
    --accent: #7aa2ff;
    --accent-soft: #1c2a4a;
    --pilot: #7aa2ff;
    --pilot-soft: #1b2944;
    --atc: #fb923c;
    --atc-soft: #3a2416;
    --traffic: #a3acb9;
    --traffic-soft: #232b36;
    --shadow: none;
    color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    display: flex;
    flex-direction: column;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0; }
h2 { font-size: 1.05rem; }

.muted { color: var(--muted); }
.error, .form-error { color: var(--danger); }
.form-error:empty { display: none; }
.empty { padding: 2rem 0; text-align: center; }

button { font: inherit; cursor: pointer; }
button.link { background: none; border: 0; color: var(--accent); padding: 0; }
button.primary {
    background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: .6rem 1rem; font-weight: 600;
}
input {
    font: inherit; color: var(--text); background: var(--surface);
    border: 1px solid var(--border); border-radius: 8px; padding: .5rem .7rem;
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* top bar */

.topbar {
    display: flex; align-items: center; justify-content: space-between; gap: 1rem;
    padding: .7rem 1.25rem; background: var(--surface); border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 1000;
}
.topbar-left { display: flex; align-items: center; gap: 1.75rem; min-width: 0; }
.brand { display: flex; align-items: center; gap: .75rem; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand-logo { height: 32px; width: auto; display: block; }
/* the logo's grey letters need a light version on a dark background */
.logo-dark { display: none; }
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .logo-light { display: none; }
    :root:not([data-theme="light"]) .logo-dark { display: block; }
}
:root[data-theme="dark"] .logo-light { display: none; }
:root[data-theme="dark"] .logo-dark { display: block; }
.brand-name { font-weight: 600; font-size: 1.05rem; white-space: nowrap; }
.menu a { color: var(--muted); font-weight: 500; }
.menu a:hover { color: var(--text); text-decoration: none; }
.topbar-right { display: flex; align-items: center; gap: 1rem; min-width: 0; }
.theme-button {
    display: grid; place-items: center; width: 34px; height: 34px; padding: 0; flex: none;
    background: none; border: 1px solid var(--border); border-radius: 8px; color: var(--muted);
}
.theme-button:hover { color: var(--text); border-color: var(--muted); }
.theme-button svg { width: 18px; height: 18px; }
.topbar-user { display: flex; gap: 1rem; align-items: center; color: var(--muted); font-size: .9rem; min-width: 0; }
.topbar-user span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

main { flex: 1; min-height: 0; display: flex; flex-direction: column; }

.page { min-width: 0; width: 100%; max-width: 980px; margin: 0 auto; padding: 1.5rem 1rem 3rem; }

/* login */

.login-page { flex: 1; display: grid; place-items: center; padding: 2rem 1rem; }
.login {
    width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: .9rem;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 1.75rem; box-shadow: var(--shadow);
}
.login p { margin: 0; }
.login label { display: flex; flex-direction: column; gap: .3rem; font-size: .9rem; color: var(--muted); }

/* flight list */

.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: .5rem; }
.toggle { display: flex; gap: .4rem; align-items: center; color: var(--muted); font-size: .9rem; }
.month { color: var(--muted); font-weight: 600; margin: 1.5rem 0 .5rem; text-transform: none; }

.flight-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .5rem; }
.flight-card {
    display: grid; grid-template-columns: 13rem 1fr auto; gap: 1rem; align-items: center;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: .8rem 1rem; color: var(--text); box-shadow: var(--shadow);
    transition: border-color .15s;
}
.flight-card:hover { border-color: var(--accent); text-decoration: none; }
.flight-date { font-weight: 600; }
.flight-time { color: var(--muted); font-size: .9rem; font-variant-numeric: tabular-nums; }
.flight-services { display: flex; flex-wrap: wrap; gap: .35rem; }
.flight-stats { display: flex; gap: .9rem; align-items: center; color: var(--muted); font-size: .9rem; white-space: nowrap; }
.flight-stats strong { color: var(--text); }

.chip {
    display: inline-block; max-width: 100%; overflow: hidden; text-overflow: ellipsis; padding: .1rem .55rem; border-radius: 999px; font-size: .82rem;
    background: var(--surface-2); border: 1px solid var(--border); color: var(--text); white-space: nowrap;
}
.chip-link:hover { border-color: var(--accent); text-decoration: none; }
.chip-link.active { background: var(--accent-soft); border-color: var(--accent); }
.chips { display: flex; gap: .35rem; flex-wrap: wrap; margin-top: .4rem; }

.badge { font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: .1rem .45rem; border-radius: 4px; }
.badge.track { color: var(--track); background: color-mix(in srgb, var(--track) 14%, transparent); }
.badge.admin { color: #fff; background: var(--danger); }

.admin-bar { display: flex; flex-direction: column; gap: .6rem; padding: .8rem 1rem; margin-bottom: 1rem;
    background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }
.admin-row { display: flex; gap: .6rem; align-items: center; }
.admin-row input { flex: 1; min-width: 0; width: 100%; }
.admin-search { display: flex; flex-direction: column; gap: .5rem; padding-top: .6rem; border-top: 1px dashed var(--border); }
.search-results:empty { display: none; }
.search-results { font-size: .9rem; }
.flight-user { font-size: .8rem; color: var(--accent); overflow: hidden; text-overflow: ellipsis; }
button.copy-id { font-size: inherit; }
button.copy-id:hover { text-decoration: underline; }
.admin-recent { display: flex; flex-wrap: wrap; gap: .35rem; align-items: center; font-size: .85rem; }

@media (max-width: 720px) {
    .flight-card { grid-template-columns: minmax(0, 1fr); gap: .5rem; }
    .flight-stats { flex-wrap: wrap; }
    .menu { display: none; }
    .flight-head { grid-template-columns: minmax(0, 1fr); }
}

/* one flight */

.flight-page { max-width: none; flex: 1; min-height: 0; display: flex; flex-direction: column; padding-bottom: 1rem; }
.flight-head { display: grid; grid-template-columns: 1fr auto; gap: .3rem 1rem; align-items: end; margin-bottom: 1rem; }
.flight-head .back { grid-column: 1 / -1; font-size: .9rem; }
.flight-title .muted { font-variant-numeric: tabular-nums; }

.segmented { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 2px; }
.segmented button { background: none; border: 0; padding: .35rem .8rem; border-radius: 6px; color: var(--muted); }
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }

.flight-body { flex: 1; min-height: 0; display: grid; gap: 1rem; }
.flight-body[data-view="both"] { grid-template-columns: minmax(340px, 2fr) 3fr; }
.flight-body[data-view="journal"] .map-panel,
.flight-body[data-view="map"] .journal-panel { display: none; }
.flight-body[data-view="journal"] .journal-panel { max-width: 860px; width: 100%; justify-self: center; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); min-height: 0; }
.journal-panel { overflow-y: auto; }
.map-panel { position: relative; overflow: hidden; min-height: 420px; }
.map { position: absolute; inset: 0; }
.map-empty {
    position: absolute; z-index: 500; top: 1rem; left: 50%; transform: translateX(-50%);
    background: var(--surface); padding: .5rem .9rem; border-radius: 8px; box-shadow: var(--shadow);
}

@media (min-width: 960px) {
    .flight-page { height: calc(100vh - 52px); }
}
@media (max-width: 959px) {
    .flight-body[data-view="both"] { grid-template-columns: 1fr; }
    .map-panel { min-height: 60vh; }
}

/* journal */

.journal { list-style: none; margin: 0; padding: .75rem; display: flex; flex-direction: column; gap: .55rem; }
.event { border-radius: 8px; padding: .25rem .4rem; cursor: pointer; scroll-margin: 1rem; }
.event.selected { background: var(--accent-soft); }
.event-meta { display: flex; align-items: center; gap: .5rem; font-size: .8rem; color: var(--muted); margin-bottom: .15rem; }
.event-meta time { font-variant-numeric: tabular-nums; }
.who { font-weight: 600; }
.kind { font-size: .72rem; text-transform: uppercase; letter-spacing: .04em; }
.bubble { padding: .5rem .75rem; border-radius: 10px; max-width: 92%; white-space: pre-wrap; }

.msg.from-pilot { align-self: stretch; display: flex; flex-direction: column; align-items: flex-end; }
.msg.from-pilot .bubble { background: var(--pilot-soft); border-top-right-radius: 3px; }
.msg.from-pilot .who { color: var(--pilot); }
.msg.from-atc .bubble { background: var(--atc-soft); border-top-left-radius: 3px; }
.msg.from-atc .who { color: var(--atc); }
.msg.from-traffic .bubble { background: var(--traffic-soft); border-top-left-radius: 3px; font-style: italic; }
.msg.from-traffic .who { color: var(--traffic); }

.context { font-size: .8rem; color: var(--muted); margin-top: .25rem; max-width: 92%; }
.context summary { cursor: pointer; }
.context p { margin: .25rem 0 0; }

.sys { text-align: center; }
.sys .event-meta { justify-content: center; }
.sys-text { font-size: .85rem; color: var(--muted); }
.sys.connected .sys-text strong { color: var(--ok); }

.located { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.located.track { background: var(--track); }
.located.service { border: 1.5px solid var(--muted); }

/* map */

.leaflet-container { font: inherit; background: var(--surface-2); }
.service-icon span {
    display: block; width: 14px; height: 14px; transform: rotate(45deg);
    background: #fff; border: 3px solid #0f172a; border-radius: 2px;
}
.group-icon span {
    display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%;
    background: var(--atc); color: #fff; font-weight: 700; font-size: .8rem;
    border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .35);
}
.fade-control {
    display: flex; flex-direction: column; align-items: center; gap: .1rem;
    background: #fff; color: #333; padding: .3rem .4rem; font-size: .7rem; font-weight: 600;
}
.fade-control input { width: 90px; margin: 0; accent-color: #7c3aed; }
.popup { font-size: .85rem; }
.popup-line + .popup-line { margin-top: .45rem; padding-top: .45rem; border-top: 1px solid #e5e7eb; }
.popup-line.clickable { cursor: pointer; }
.popup-line.clickable:hover { background: #f3f4f6; }
.popup-time { color: #6b7280; font-variant-numeric: tabular-nums; }
.popup-text { margin-top: .15rem; }
.popup-note { color: #6b7280; font-style: italic; margin-bottom: .5rem; }
.popup-group { max-height: 300px; overflow-y: auto; }

/* narrow screens (after the rules they override) */

@media (max-width: 720px) {
    .flight-head { grid-template-columns: minmax(0, 1fr); align-items: start; }
    .flight-head .segmented { justify-self: start; }
    .page { padding-top: 1rem; }
}
