/* Design tokens - single source of truth for popup colors/spacing */
:root {
    /* Popup shell */
    --popup-bg: #ffffff;
    --popup-border: #e5e9f0;
    --popup-shadow: 0 6px 24px rgba(15, 23, 42, 0.16), 0 1px 3px rgba(15, 23, 42, 0.10);
    --popup-radius: 10px;

    /* Text */
    --text-strong: #1e293b;
    --text-muted: #64748b;
    --text-body: #475569;

    /* Accent */
    --accent: #1e40af;
    --accent-hover: #2563eb;

    /* Surfaces */
    --surface-soft: #f8fafc;
    --surface-soft-hover: #f1f5f9;

    /* Pills (base) */
    --pill-bg: #f1f5f9;
    --pill-text: #475569;
    --pill-radius: 5px;

    /* Status pairs (bg / text / border) */
    --free-bg: #dcfce7;   --free-text: #15803d;  --free-border: #86efac;
    --paid-bg: #dbeafe;   --paid-text: #1d4ed8;  --paid-border: #93c5fd;
    --err-bg: #fef2f2;    --err-text: #dc2626;   --err-border: #fecaca;
    --warn-bg: #fef3c7;   --warn-text: #d97706;
    --info-bg: #dbeafe;   --info-text: #1d4ed8;
}

body {
    margin: 0;
    padding: 0;
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Ubuntu, Cantarell, 'Noto Sans', Helvetica, Arial, sans-serif;
}

#map {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 100%;
    background: radial-gradient(circle at center, #19304f, #0c1729);
}

/* Simulator filter styling */
.simulator-filter {
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 1000;
    background: rgba(255, 255, 255, 0.92);
    backdrop-filter: saturate(180%) blur(4px);
    border-radius: 6px;
    padding: 8px 10px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}

.simulator-filter label {
    font-size: 12px;
    font-weight: 600;
    margin-right: 6px;
    color: #374151;
}

.simulator-filter select {
    border: none;
    background: transparent;
    font-size: 12px;
    outline: none;
}

/* ===== MapLibre popup shell (was unstyled defaults) ===== */

/* maplibre-gl.css sets `will-change: transform` on the popup, which promotes it to a GPU
   layer whose bitmap is rastered ONCE and never re-rastered under transform updates
   (Chrome: "re-rastering composited layers"). The popup's fractional height makes
   `translate(-100%)` land the layer on a sub-pixel Y offset, so the compositor samples that
   locked bitmap at a fractional position and the whole popup (text + image) renders blurry
   on first creation in WebView2. Dropping will-change keeps the popup in the main raster, so
   it stays crisp at any sub-pixel offset. (Verified: dpr=1, no ancestor transform/scale.) */
.maplibregl-popup {
    will-change: auto;
}

.maplibregl-popup-content {
    padding: 12px 14px;
    border-radius: var(--popup-radius);
    border: 1px solid var(--popup-border);
    background: var(--popup-bg);
    box-shadow: var(--popup-shadow);
}

/* Tip triangle matches the white shell + border */
.maplibregl-popup-anchor-top .maplibregl-popup-tip,
.maplibregl-popup-anchor-top-left .maplibregl-popup-tip,
.maplibregl-popup-anchor-top-right .maplibregl-popup-tip {
    border-bottom-color: var(--popup-bg);
}
.maplibregl-popup-anchor-bottom .maplibregl-popup-tip,
.maplibregl-popup-anchor-bottom-left .maplibregl-popup-tip,
.maplibregl-popup-anchor-bottom-right .maplibregl-popup-tip {
    border-top-color: var(--popup-bg);
}
.maplibregl-popup-anchor-left .maplibregl-popup-tip {
    border-right-color: var(--popup-bg);
}
.maplibregl-popup-anchor-right .maplibregl-popup-tip {
    border-left-color: var(--popup-bg);
}

/* Close button (only shown on locked/clicked popups) */
.maplibregl-popup-close-button {
    top: 6px;
    right: 6px;
    width: 22px;
    height: 22px;
    padding: 0;
    border-radius: 50%;
    font-size: 17px;
    line-height: 1;
    color: var(--text-muted);
    background: transparent;
    transition: background-color 0.15s ease, color 0.15s ease;
}

.maplibregl-popup-close-button:hover {
    background: var(--surface-soft-hover);
    color: var(--text-strong);
}

/* ===== Popup content - Compact Modern Design ===== */
.airport-popup {
    max-width: 280px;
    font-family: system-ui, -apple-system, sans-serif;
}

/* Title */
.airport-popup .popup-title {
    margin: 0 0 4px 0;
    padding-right: 18px; /* clear the close button on locked popups */
    font-size: 14px;
    font-weight: 600;
    line-height: 1.3;
    color: var(--text-strong);
    display: flex;
    align-items: flex-start;
    gap: 5px;
}

/* Type icon (airport/landscape) */
.airport-popup .popup-type-icon {
    width: 13px;
    height: 13px;
    flex-shrink: 0;
    opacity: 0.55;
    margin-top: 2px;
}

/* External-link package name (shared title + subtitle) */
.airport-popup .package-name-link {
    color: var(--accent);
    text-decoration: none;
    cursor: pointer;
    transition: color 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.airport-popup .package-name-link::after {
    content: "";
    display: inline-block;
    width: 11px;
    height: 11px;
    background-image: url('external-link.svg');
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
    opacity: 0.55;
    transition: opacity 0.2s ease;
    flex-shrink: 0;
}

.airport-popup .package-name-link:hover {
    color: var(--accent-hover);
}

.airport-popup .package-name-link:hover::after {
    opacity: 1;
}

/* ===== Unified pill base (codes, price, sim badges) ===== */
.airport-popup .popup-codes .code,
.airport-popup .popup-subtitle .price,
.airport-popup .popup-footer .simulator-badge {
    display: inline-flex;
    align-items: center;
    padding: 2px 6px;
    border-radius: var(--pill-radius);
    font-size: 10.5px;
    font-weight: 600;
    line-height: 1.4;
    border: 1px solid transparent;
}

/* Airport codes (own row below title) */
.airport-popup .popup-codes {
    display: flex;
    gap: 4px;
    margin-bottom: 4px;
}

/* Lazy-loaded package preview thumbnail (constant-size source). */
.airport-popup .popup-thumbnail {
    margin: 6px 0 2px 0;
}

.airport-popup .popup-thumbnail img {
    display: block;
    width: 100%;
    max-height: 120px;
    object-fit: cover;
    border-radius: 6px;
}

/* Mobile: render the popup as a centered overlay instead of anchored (avoids off-screen popups near
   edges). Stylesheet !important overrides MapLibre's inline transform/left/top (rewritten on map move). */
.maplibregl-popup.m2-popup-centered {
    position: fixed !important;
    left: 50% !important;
    top: 50% !important;
    bottom: auto !important;
    transform: translate(-50%, -50%) !important;
    max-width: 92vw !important;
    z-index: 5;
}

.m2-popup-centered .maplibregl-popup-tip {
    display: none !important;
}

.m2-popup-centered .maplibregl-popup-content {
    max-height: 80vh;
    overflow-y: auto;
}

.m2-popup-centered .airport-popup {
    max-width: none;
}

/* Picture gallery strip in the popup. */
.airport-popup .popup-gallery {
    display: flex;
    gap: 4px;
    margin: 6px 0 2px 0;
}

.airport-popup .popup-gallery .m2-gallery-thumb {
    position: relative;
    flex: 1 1 0;
    min-width: 0;
    height: 54px;
    padding: 0;
    border: none;
    border-radius: 6px;
    overflow: hidden;
    cursor: pointer;
    background: var(--surface-soft);
}

.airport-popup .popup-gallery .m2-gallery-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.airport-popup .popup-gallery .m2-gallery-more {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(15, 23, 42, 0.62);
    color: #fff;
    font-size: 13px;
    font-weight: 600;
}

/* In-map lightbox (created once, appended to body). */
.m2-lightbox {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: none;            /* toggled to flex by JS */
    align-items: center;
    justify-content: center;
}

.m2-lightbox .m2-lightbox-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(8, 12, 22, 0.82);
}

.m2-lightbox .m2-lightbox-figure {
    position: relative;
    z-index: 1;
    margin: 0;
    max-width: 92vw;
    max-height: 92vh;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.m2-lightbox .m2-lightbox-img {
    max-width: 92vw;
    max-height: 84vh;
    object-fit: contain;
    border-radius: 8px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}

.m2-lightbox .m2-lightbox-caption {
    margin-top: 10px;
    color: #f1f5f9;
    font-size: 14px;
    text-align: center;
    max-width: 80vw;
}

.m2-lightbox .m2-lightbox-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 2;
    width: 48px;
    height: 48px;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    font-size: 28px;
    line-height: 1;
    color: #fff;
    background: var(--accent);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
}

.m2-lightbox .m2-lightbox-nav:hover {
    background: var(--accent-hover);
}

.m2-lightbox .m2-lightbox-prev { left: 24px; }
.m2-lightbox .m2-lightbox-next { right: 24px; }

.m2-lightbox .m2-lightbox-close {
    position: absolute;
    top: 18px;
    right: 22px;
    z-index: 2;
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    font-size: 26px;
    line-height: 1;
    color: #fff;
    background: rgba(255, 255, 255, 0.16);
}

.m2-lightbox .m2-lightbox-close:hover {
    background: rgba(255, 255, 255, 0.28);
}

.airport-popup .popup-codes .code {
    background: var(--pill-bg);
    color: var(--pill-text);
    font-family: ui-monospace, SFMono-Regular, monospace;
    letter-spacing: 0.02em;
}

/* Subtitle (developer + price, or package + developer + price) */
.airport-popup .popup-subtitle {
    font-size: 12px;
    color: var(--text-muted);
    margin-bottom: 6px;
    line-height: 1.4;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 4px;
}

.airport-popup .popup-subtitle .package-name-link::after {
    width: 10px;
    height: 10px;
}

.airport-popup .popup-subtitle .price {
    margin-left: 2px;
}

.airport-popup .popup-subtitle .price.free,
.airport-popup .popup-subtitle .price.installed,
.airport-popup .popup-subtitle .price.enabled {
    background: var(--free-bg);
    color: var(--free-text);
}

.airport-popup .popup-subtitle .price.paid {
    background: var(--paid-bg);
    color: var(--paid-text);
}

.airport-popup .popup-subtitle .price.error {
    background: var(--err-bg);
    color: var(--err-text);
}

/* Description - clamp to 2 lines to keep popup compact */
.airport-popup .popup-description {
    font-size: 12px;
    color: var(--text-body);
    line-height: 1.4;
    margin-bottom: 6px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Footer with simulator badges only */
.airport-popup .popup-footer {
    display: flex;
    align-items: center;
    gap: 5px;
    flex-wrap: wrap;
}

.airport-popup .popup-footer .simulator-badge {
    background: var(--surface-soft);
    border-color: var(--popup-border);
    color: var(--text-muted);
    font-weight: 500;
}

.airport-popup .popup-footer .simulator-badge.msfs-2020 {
    background: var(--free-bg);
    border-color: var(--free-border);
    color: var(--free-text);
}

.airport-popup .popup-footer .simulator-badge.msfs-2024 {
    background: var(--paid-bg);
    border-color: var(--paid-border);
    color: var(--accent);
}

/* Error message */
.airport-popup .error-message {
    margin-top: 8px;
    padding: 6px 8px;
    background: var(--err-bg);
    border: 1px solid var(--err-border);
    border-radius: var(--pill-radius);
    color: var(--err-text);
    font-size: 11px;
    font-weight: 500;
}


/* Legend control styling improvements */
.simatlas-legend {
    min-width: 120px;
}

.simatlas-legend input[type="checkbox"] {
    cursor: pointer;
}

/* Package management controls styling */
.package-controls {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--popup-border);
}

.package-toggle {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 5px;
    cursor: pointer;
    font-size: 12px;
}

.package-toggle input[type="checkbox"] {
    cursor: pointer;
}

.package-toggle input[type="checkbox"]:disabled {
    cursor: not-allowed;
    opacity: 0.5;
}

.package-toggle:has(input:disabled) {
    opacity: 0.6;
    cursor: not-allowed;
}

.package-toggle:has(input:disabled) span {
    color: #9ca3af;
}

/* Primary toggle styling for the main activate checkbox */
.package-toggle.primary-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 7px;
    padding: 12px;
    background: var(--surface-soft);
    border: 1px solid var(--popup-border);
    border-radius: 6px;
    transition: all 0.2s ease;
    cursor: pointer;
}

.package-toggle.primary-toggle:hover {
    background: var(--surface-soft-hover);
    border-color: #cbd5e1;
}

.package-toggle.primary-toggle .toggle-label {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-strong);
}

.package-toggle.primary-toggle input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: #22c55e;
    cursor: pointer;
}

.package-toggle.primary-toggle:has(input:checked) {
    background: var(--free-bg);
    border-color: #22c55e;
}

.package-toggle.primary-toggle:has(input:checked) .toggle-label {
    color: var(--free-text);
}

/* Subscribe button styling - matches primary-toggle appearance */
.subscribe-button.primary-action-button {
    display: block;
    width: 100%;
    padding: 12px;
    margin-bottom: 7px;
    background: var(--paid-bg);
    border: 1px solid #3b82f6;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    color: var(--accent);
    cursor: pointer;
    transition: all 0.2s ease;
    text-align: center;
}

.subscribe-button.primary-action-button:hover {
    background: #bfdbfe;
    border-color: var(--accent-hover);
}

/* Activate row: toggle + download icon / overflow menu share one line */
.package-action-row {
    display: flex;
    align-items: stretch;
    gap: 8px;
}

.package-action-row .package-toggle.primary-toggle {
    flex: 1;
    margin-bottom: 7px;
}

/* Icon-only download button (download without activating) */
.download-icon-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    flex: 0 0 auto;
    margin-bottom: 7px;
    background: var(--surface-soft);
    border: 1px solid var(--popup-border);
    border-radius: 6px;
    color: var(--text-body);
    cursor: pointer;
    transition: all 0.2s ease;
}

.download-icon-button:hover {
    background: var(--paid-bg);
    border-color: #3b82f6;
    color: var(--accent);
}

/* Overflow ("...") menu for downloaded packages */
.package-menu-wrap {
    position: relative;
    flex: 0 0 auto;
    margin-bottom: 7px;
}

.package-menu-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 100%;
    background: var(--surface-soft);
    border: 1px solid var(--popup-border);
    border-radius: 6px;
    font-size: 18px;
    line-height: 1;
    color: var(--text-body);
    cursor: pointer;
    transition: all 0.2s ease;
}

.package-menu-toggle:hover {
    background: var(--surface-soft-hover);
    border-color: #cbd5e1;
}

.package-menu {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    right: 0;
    min-width: 140px;
    background: var(--popup-bg);
    border: 1px solid var(--popup-border);
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.12);
    padding: 4px;
    z-index: 10;
}

.package-menu.open {
    display: block;
}

.package-menu button {
    display: block;
    width: 100%;
    padding: 8px 10px;
    background: none;
    border: none;
    border-radius: 4px;
    font-size: 13px;
    color: var(--err-text);
    cursor: pointer;
    text-align: left;
}

.package-menu button:hover {
    background: #fee2e2;
}

/* Status message styling - separate box below activate */
.status-message {
    font-size: 12px;
    font-weight: 500;
    padding: 8px 12px;
    border-radius: var(--pill-radius);
    margin-top: 0;
    margin-bottom: 8px;
}

.status-message.status-success {
    background: var(--free-bg);
    color: var(--free-text);
}

.status-message.status-error {
    background: var(--err-bg);
    color: var(--err-text);
}

.status-message.status-warning {
    background: var(--warn-bg);
    color: var(--warn-text);
}

.status-message.status-info {
    background: var(--info-bg);
    color: var(--info-text);
}

.status-message.status-neutral {
    background: var(--pill-bg);
    color: var(--text-muted);
}

.download-btn {
    background: #3b82f6;
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 4px;
    font-size: 12px;
    cursor: pointer;
    width: 100%;
    transition: background-color 0.2s;
}

.download-btn:hover {
    background: var(--accent-hover);
}

.download-progress {
    margin-top: 8px;
}

.progress-bar {
    width: 100%;
    height: 8px;
    background: #f1f5f9;
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 4px;
}

.progress-fill {
    height: 100%;
    background: #3b82f6;
    transition: width 0.3s ease;
}

.progress-text {
    font-size: 11px;
    color: var(--text-muted);
}

.download-complete {
    text-align: center;
    margin-top: 8px;
}

.download-status {
    color: var(--free-text);
    font-size: 12px;
    font-weight: 500;
}

.status-active {
    background: var(--free-bg);
    color: var(--free-text);
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: 500;
}

.status-inactive {
    background: var(--err-bg);
    color: var(--err-text);
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 11px;
    font-weight: 500;
}

/* In-progress (downloading/activating) marker overlay: spinning blue half-circle arc.
   Rendered as a maplibregl.Marker DOM element on top of an invisible (transparent) circle.
   pointer-events:none so hover/click fall through to the circle below for popups. */
@keyframes simatlas-spin {
    to {
        transform: rotate(360deg);
    }
}

.simatlas-dl-spinner {
    width: 12px;
    height: 12px;
    box-sizing: border-box;
    /* Full white ring (the "rest" of the circle) with a single blue segment that
       spins, instead of a bare blue arc on transparent. */
    border: 2px solid #ffffff;
    border-top-color: #3b82f6;
    border-radius: 50%;
    pointer-events: none;
    animation: simatlas-spin 0.8s linear infinite;
}

/* Legend swatch for the "Downloading" row: arc colors are set inline by legend-control.js
   (mirrors the Downloaded swatch); this class only adds the spin animation. */
.simatlas-legend-swatch.simatlas-legend-spinner {
    animation: simatlas-spin 0.8s linear infinite;
}