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

html,
body {
    height: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #1a1a2e;
    color: #eee;
    overflow: hidden;
}

/* App shell: header + map + panels.
   overflow-y is auto so that a very short window scrolls instead of hiding the map. */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    overflow-x: hidden;
    overflow-y: auto;
    padding-bottom: env(safe-area-inset-bottom);
}

.header {
    flex: 0 0 auto;
    background: linear-gradient(135deg, #16213e, #0f3460);
    padding: 10px 15px;
    padding-top: max(10px, env(safe-area-inset-top));
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.3);
    position: relative;
    z-index: 10;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 0 0 auto;
}

.icon-btn {
    flex: 0 0 auto;
    width: 34px;
    height: 34px;
    border: 0;
    border-radius: 50%;
    background: rgba(255,255,255,0.12);
    color: #fff;
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
}

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

.icon-btn.active {
    background: rgba(78,204,163,0.25);
}

.icon-btn.small {
    width: 24px;
    height: 24px;
    font-size: 0.7rem;
    vertical-align: middle;
    margin-left: 4px;
}

.header h1 {
    flex: 1 1 auto;
    min-width: 0;
    font-size: clamp(0.95rem, 3.6vw, 1.4rem);
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.status-indicator {
    flex: 0 0 auto;
    max-width: 46vw;
    padding: 6px 12px;
    border-radius: 20px;
    font-weight: bold;
    font-size: clamp(0.7rem, 2.8vw, 0.9rem);
    background: rgba(255,255,255,0.1);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Demo data warning: keeps test data from being mistaken for real alerts */
.demo-banner {
    flex: 0 0 auto;
    padding: 7px 15px;
    background: #4a2f0d;
    border-bottom: 1px solid #ff9f1a;
    color: #ffcf8b;
    font-size: 0.78rem;
    text-align: center;
}

.demo-banner[hidden] {
    display: none;
}

/* Panels wrapper.
   Phones: `display: contents` — the panels behave as direct children of the
   column layout. Wide screens: a real scrollable sidebar (see the media
   queries at the end of this file). */
.sidebar {
    display: contents;
}

/* Leaflet map.
   min-height: 0 is required: without it a flex item cannot shrink below its
   content size, so the map overflows and pushes the panels off-screen.
   z-index: 0 keeps Leaflet panes (z-index up to 800) below the header. */
.map-container {
    flex: 1 1 0;
    min-height: 140px;
    position: relative;
    z-index: 0;
    background: #e0e0e0;
}

#map {
    width: 100%;
    height: 100%;
}

/* Leaflet needs a sized container; the flex item above provides it. */
.leaflet-container {
    width: 100%;
    height: 100%;
    font-family: inherit;
}

.threat-panel, .user-panel, .alert-panel {
    flex: 0 0 auto;
    padding: 12px 15px;
    background: #16213e;
    max-height: 26vh;
    overflow-y: auto;
    border-top: 1px solid rgba(255,255,255,0.06);
}

/* The location bar needs one line, the alerts panel only shows short toasts */
.user-panel {
    max-height: 18vh;
    padding: 10px 15px;
}

.alert-panel {
    max-height: 22vh;
}

/* Two data panels: cap them so the map keeps a usable height */
#alerts-region-list {
    max-height: 18vh;
    overflow-y: auto;
}

#targets-list {
    max-height: 12vh;
    overflow-y: auto;
}

.region-status {
    margin-bottom: 8px;
    font-size: 0.85rem;
    color: #cfd8e3;
}

.panel-note {
    display: block;
    margin-top: 6px;
    font-size: 0.72rem;
    font-weight: normal;
    color: #8fa0b5;
    line-height: 1.3;
}

.threat-panel h2 .panel-note {
    display: inline;
    margin: 0;
}

/* Data source status line */
.sources-status {
    flex: 0 0 auto;
    padding: 6px 12px;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
    background: #101a2e;
    border-top: 1px solid rgba(255,255,255,0.06);
    font-size: 0.72rem;
    color: #8fa0b5;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.source-ok { color: #4ecca3; }
.source-bad { color: #e94560; }

/* Short viewports: keep the map usable instead of letting panels eat it */
@media (max-height: 560px) {
    .threat-panel, .user-panel, .alert-panel {
        max-height: 20vh;
        padding: 8px 12px;
    }

    #alerts-region-list,
    #targets-list {
        max-height: 14vh;
    }

    .threat-panel h2, .user-panel h3, .alert-panel h3 {
        font-size: 0.95rem;
        margin-bottom: 6px;
    }
}

.threat-panel h2, .user-panel h3, .alert-panel h3 {
    margin-bottom: 10px;
    color: #4ecca3;
    font-size: 1.05rem;
}

.user-panel p {
    font-size: 0.82rem;
    color: #cfd8e3;
    margin: 0;
    flex: 1 1 auto;
    min-width: 0;
}

.panel-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.panel-row + .panel-row {
    margin-top: 8px;
}

.action-btn {
    flex: 0 0 auto;
    border: 0;
    border-radius: 8px;
    padding: 8px 12px;
    background: rgba(78,204,163,0.18);
    color: #4ecca3;
    font-weight: bold;
    font-size: 0.8rem;
    cursor: pointer;
}

.action-btn:hover {
    background: rgba(78,204,163,0.3);
}

/* Full width button in its own row (Telegram notifications) */
.action-btn.wide {
    flex: 1 1 auto;
    width: 100%;
}

.action-btn.on {
    background: rgba(78,204,163,0.4);
}

/* Hidden while the user already receives Telegram alerts: nothing to advertise */
.action-btn[hidden] {
    display: none;
}

.action-btn:disabled {
    opacity: 0.6;
    cursor: default;
}

/* Crosshair while waiting for a click on the map */
.leaflet-container.picking-location {
    cursor: crosshair;
}

.threat-item {
    background: rgba(255,255,255,0.05);
    margin: 8px 0;
    padding: 10px;
    border-radius: 8px;
    border-left: 4px solid #e94560;
    font-size: 0.9rem;
    cursor: pointer;
}

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

/* Keyboard users on desktop need to see which row is focused */
.threat-item:focus-visible {
    outline: 2px solid #4ecca3;
    outline-offset: 2px;
}

.threat-item .threat-meta {
    display: block;
    color: #b9c4d2;
}

.threat-row {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
}

.threat-distance {
    flex: 0 0 auto;
    font-weight: bold;
    color: #4ecca3;
}

.threat-item.jeopardy .threat-distance {
    color: #ff9f1a;
}

.threat-item.danger .threat-distance {
    color: #e94560;
}

/* Data quality flag from the source (e.g. impossible speed in ADS-B) */
.threat-item.suspect {
    border-left-style: dotted;
    opacity: 0.8;
}

.threat-item .threat-age {
    display: block;
    margin-top: 4px;
    color: #8fa0b5;
    font-size: 0.78rem;
}

/* Target was not updated by the source for a while: dim it and drop from the alert maths */
.threat-item.stale {
    opacity: 0.55;
    border-left-style: dashed;
}

.threat-item.stale .threat-age {
    color: #ff9f1a;
}

.empty-state {
    color: #8fa0b5;
    font-size: 0.9rem;
    padding: 6px 0;
}

.threat-item.safe {
    border-left-color: #4ecca3;
}

.threat-item.jeopardy {
    border-left-color: #ff9f1a;
    background: rgba(255,159,26,0.1);
}

.threat-item.danger {
    border-left-color: #e94560;
    background: rgba(233,69,96,0.12);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.8; }
}

.alert-item {
    padding: 8px;
    margin: 5px 0;
    border-radius: 6px;
    background: rgba(233,69,96,0.1);
    animation: slideIn 0.3s ease;
}

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

/* .user-panel stays visible (manual location picker), the alert panel uses [hidden] */

.status-normal { color: #4ecca3 !important; }
.status-warning { color: #ff9f1a !important; }
.status-danger { color: #e94560 !important; }

/* Emoji markers (divIcon): no external images, so they always render */
.threat-marker,
.user-marker {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    line-height: 1;
    background: transparent;
    border: 0;
}

.threat-marker {
    filter: drop-shadow(0 0 5px rgba(233,69,96,0.95));
}

/* Course arrow: a triangle rotated to the target's track, with the kind emoji on
   top. A target flying west and one flying east are otherwise indistinguishable. */
.threat-marker .track-arrow {
    position: absolute;
    top: 0;
    left: 0;
    width: 26px;
    height: 26px;
    fill: #e94560;
    stroke: rgba(255,255,255,0.85);
    stroke-width: 1.1;
}

.threat-marker .marker-emoji {
    position: relative;
    z-index: 1;
}

.threat-marker.with-track .marker-emoji {
    /* The arrow needs room around the emoji to be readable */
    font-size: 15px;
}

.threat-marker.jet .track-arrow {
    fill: #ff9f1a;
}

.threat-marker.missile .track-arrow {
    fill: #ff47ab;
}

.threat-marker.aircraft .track-arrow {
    fill: #78b4ff;
}

.threat-marker.reported .track-arrow {
    fill: #ff5a78;
}

/* The course, repeated in the list as a tiny arrow next to the row title */
.track-badge {
    display: inline-block;
    width: 11px;
    height: 11px;
    margin-left: 5px;
    background: currentColor;
    vertical-align: middle;
    clip-path: polygon(50% 0%, 100% 100%, 50% 76%, 0% 100%);
}

.threat-marker.jet {
    filter: drop-shadow(0 0 5px rgba(255,159,26,0.95));
}

.threat-marker.missile {
    filter: drop-shadow(0 0 5px rgba(255,71,171,0.95));
}

.user-marker {
    font-size: 20px;
    filter: drop-shadow(0 0 6px rgba(78,204,163,0.95));
}

/* ADS-B aircraft are informational, not a threat level input */
.threat-marker.aircraft {
    font-size: 18px;
    opacity: 0.85;
    filter: drop-shadow(0 0 4px rgba(120,180,255,0.9));
}

.threat-marker.suspect {
    opacity: 0.5;
}

/* Targets reported by the official channel: area level, no precise track */
.threat-marker.reported {
    filter: drop-shadow(0 0 6px rgba(255, 90, 120, 0.95));
}

.threat-item.reported {
    border-left-style: dashed;
    border-left-color: #ff9f1a;
    background: rgba(255, 159, 26, 0.08);
}

.leaflet-popup-content {
    font-size: 0.85rem;
    line-height: 1.35;
}

/* ============================================================ wide screens ==
   Laptop/desktop and short landscape windows: the map takes the left column and
   all panels move into a scrollable sidebar on the right. */
@media (min-width: 900px), (min-width: 600px) and (orientation: landscape) {
    .app-container {
        display: grid;
        grid-template-columns: minmax(0, 1fr) clamp(280px, 30vw, 420px);
        grid-template-rows: auto auto minmax(0, 1fr);
        overflow: hidden;
        padding-bottom: 0;
    }

    .header {
        grid-column: 1 / -1;
        grid-row: 1;
        padding: 12px 20px;
    }

    .header h1 {
        font-size: 1.35rem;
    }

    .demo-banner {
        grid-column: 1 / -1;
        grid-row: 2;
        font-size: 0.85rem;
        text-align: left;
    }

    /* The map fills the left column, the sidebar scrolls on the right */
    .map-container {
        grid-column: 1;
        grid-row: 3;
        min-height: 0;
        height: 100%;
        border-right: 1px solid rgba(255, 255, 255, 0.1);
    }

    .sidebar {
        display: flex;
        flex-direction: column;
        grid-column: 2;
        grid-row: 3;
        min-height: 0;
        overflow-y: auto;
        background: #16213e;
    }

    /* Active alert toasts must stay visible: keep them on top */
    .sidebar > .alert-panel {
        order: -1;
    }

    /* Each list scrolls on its own, so no panel is pushed out of the sidebar */
    .sidebar .threat-panel,
    .sidebar .user-panel,
    .sidebar .alert-panel {
        max-height: none;
        overflow: visible;
    }

    .sidebar #alerts-region-list {
        max-height: 34vh;
        overflow-y: auto;
    }

    .sidebar #targets-list {
        max-height: 22vh;
        overflow-y: auto;
    }

    .sources-status {
        margin-top: auto;
        padding: 8px 15px;
        padding-bottom: max(8px, env(safe-area-inset-bottom));
        white-space: normal;
        text-overflow: clip;
    }
}

/* Short windows (small laptop in split screen, phone landscape): keep the
   sidebar readable without hiding the location bar and the source status. */
@media (max-height: 620px) {
    .sidebar #alerts-region-list {
        max-height: 22vh;
    }

    .sidebar #targets-list {
        max-height: 15vh;
    }

    .header {
        padding: 8px 15px;
    }
}
