:root {
    color-scheme: dark;

    /* ---- design tokens -------------------------------------------------
       One place for the visual language so every surface agrees. The glass
       panels share a single radius, border, blur and shadow instead of the
       ad-hoc values the first build scattered around. */
    --nyc-bg: #050505;
    --nyc-fg: #f2f2f2;
    /* Contrast: the old muted/faint values (0.55 / 0.16) put the attribution
       line — required by the data licences — at ~1.6:1 on the near-black
       background. These keep the visual hierarchy and clear 4.5:1. */
    --nyc-muted: rgba(255, 255, 255, 0.72);
    --nyc-faint: rgba(255, 255, 255, 0.42);
    --nyc-line: rgba(255, 255, 255, 0.28);
    --nyc-accent: #ffe6b0;
    --nyc-panel: rgba(8, 8, 10, 0.86);
    --nyc-panel-solid: rgba(10, 10, 12, 0.96);

    --nyc-radius: 14px;
    --nyc-radius-sm: 9px;
    --nyc-blur: blur(12px) saturate(120%);
    --nyc-shadow: 0 18px 44px rgba(0, 0, 0, 0.55);
    --nyc-ring: 0 0 0 1px var(--nyc-faint);

    --nyc-gap: 10px;
    --nyc-pad: 10px;
    --nyc-ease: cubic-bezier(0.22, 1, 0.36, 1);
    --nyc-fast: 0.16s var(--nyc-ease);

    /* Minimum comfortable touch target (Apple HIG 44px, Material 48px). */
    --nyc-touch: 44px;
    --nyc-safe-t: env(safe-area-inset-top, 0px);
    --nyc-safe-b: env(safe-area-inset-bottom, 0px);
    --nyc-safe-l: env(safe-area-inset-left, 0px);
    --nyc-safe-r: env(safe-area-inset-right, 0px);

    /* Signal overlay palette (used by the legend swatches + the heat shader). */
    --nyc-sig-affluence: #7fd1ae;
    --nyc-sig-safety: #ff8a7a;
    --nyc-sig-popular: #b39dff;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    background: var(--nyc-bg);
    color: var(--nyc-fg);
    font-family: "Inter", "Helvetica Neue", Arial, sans-serif;
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-font-smoothing: antialiased;
}

/* Visible to screen readers only. */
.nyc-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
}

#nyc-stage {
    position: fixed;
    inset: 0;
    background:
        radial-gradient(circle at 50% 42%, rgba(255, 255, 255, 0.05), rgba(0, 0, 0, 0) 62%),
        var(--nyc-bg);
}

#nyc-canvas {
    position: absolute;
    inset: 0;
    display: block;
    width: 100%;
    height: 100%;
    cursor: grab;
    touch-action: none;
}
#nyc-canvas.is-dragging { cursor: grabbing; }
/* The canvas is the keyboard surface; make focus visible. */
#nyc-canvas:focus-visible {
    outline: 2px solid var(--nyc-accent);
    outline-offset: -3px;
}

/* Visible focus for every control. */
.nyc-nav-btn:focus-visible,
.nyc-layer:focus-visible,
.nyc-compass:focus-visible,
.nyc-back:focus-visible,
.nyc-sheet-close:focus-visible,
.nyc-coach-ok:focus-visible,
.nyc-sel-close:focus-visible {
    outline: 2px solid var(--nyc-accent);
    outline-offset: 2px;
}

/* ---- HUD (live status only) ---- */
.nyc-hud {
    position: absolute;
    top: calc(18px + var(--nyc-safe-t));
    left: calc(18px + var(--nyc-safe-l));
    z-index: 5;
    max-width: min(78vw, 400px);
    pointer-events: none;
    transition: opacity 0.5s var(--nyc-ease);
}
.nyc-live {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    font-size: 11px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--nyc-muted);
}
.nyc-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #f2f2f2;
    box-shadow: 0 0 8px rgba(255, 255, 255, 0.8);
    animation: nyc-pulse 2.4s ease-in-out infinite;
}
@keyframes nyc-pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.35; transform: scale(0.8); }
}
.nyc-stale .nyc-dot { background: #d4a14a; box-shadow: 0 0 8px rgba(212, 161, 74, 0.8); animation: none; }

/* ---- Control rail (bottom-left): one compact horizontal row ---- */
.nyc-nav {
    position: absolute;
    left: calc(18px + var(--nyc-safe-l));
    bottom: calc(18px + var(--nyc-safe-b));
    z-index: 7;
    display: flex;
    flex-direction: row;
    gap: 6px;
    padding: 6px;
    border: 1px solid var(--nyc-faint);
    border-radius: var(--nyc-radius);
    background: var(--nyc-panel);
    backdrop-filter: var(--nyc-blur);
    -webkit-backdrop-filter: var(--nyc-blur);
    box-shadow: var(--nyc-shadow);
    transition: opacity 0.5s var(--nyc-ease), transform 0.5s var(--nyc-ease);
}
.nyc-nav-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: 1px solid transparent;
    border-radius: var(--nyc-radius-sm);
    background: transparent;
    color: var(--nyc-fg);
    cursor: pointer;
    transition: border-color var(--nyc-fast), background var(--nyc-fast), color var(--nyc-fast);
}
.nyc-nav-btn:hover {
    background: rgba(255, 255, 255, 0.09);
}
.nyc-nav-btn:active { transform: translateY(1px); }
.nyc-nav-btn svg { width: 19px; height: 19px; }
/* The Walk and Layers buttons carry pressed state; show it clearly. */
.nyc-nav-btn[aria-pressed="true"],
#nyc-layers-toggle[aria-expanded="true"] {
    color: #0a0a0a;
    background: #f2f2f2;
}

/* ---- Compass (top-right, below the back link) ---- */
.nyc-compass {
    position: absolute;
    top: calc(66px + var(--nyc-safe-t));
    right: calc(18px + var(--nyc-safe-r));
    z-index: 6;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--nyc-faint);
    border-radius: 50%;
    background: var(--nyc-panel);
    backdrop-filter: var(--nyc-blur);
    -webkit-backdrop-filter: var(--nyc-blur);
    color: var(--nyc-fg);
    cursor: pointer;
    transition: border-color var(--nyc-fast), opacity 0.5s var(--nyc-ease);
}
.nyc-compass:hover { border-color: rgba(255, 255, 255, 0.4); }
.nyc-compass-n {
    position: absolute;
    top: 3px;
    font-size: 8px;
    font-weight: 700;
    letter-spacing: 0.08em;
    color: var(--nyc-muted);
}
.nyc-compass-needle {
    width: 2px;
    height: 20px;
    border-radius: 1px;
    background: linear-gradient(to bottom, #ff5a5a 0 50%, #f2f2f2 50% 100%);
    transition: transform 0.12s linear;
}

/* ---- Layers & settings sheet ---- */
.nyc-sheet {
    position: absolute;
    left: calc(18px + var(--nyc-safe-l));
    bottom: calc(18px + var(--nyc-safe-b));
    z-index: 8;
    width: min(92vw, 320px);
    max-height: min(72vh, 620px);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 16px;
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: var(--nyc-radius);
    background: var(--nyc-panel-solid);
    backdrop-filter: var(--nyc-blur);
    -webkit-backdrop-filter: var(--nyc-blur);
    box-shadow: var(--nyc-shadow);
    animation: nyc-sheet-in 0.28s var(--nyc-ease);
}
.nyc-sheet[hidden] { display: none; }
@keyframes nyc-sheet-in {
    from { opacity: 0; transform: translateY(10px) scale(0.98); }
    to { opacity: 1; transform: none; }
}
.nyc-sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}
.nyc-sheet-title {
    font-size: 13px;
    font-weight: 600;
    letter-spacing: 0.02em;
}
.nyc-sheet-close {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--nyc-faint);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
}
.nyc-sheet-close:hover { color: var(--nyc-fg); background: rgba(255, 255, 255, 0.06); }
.nyc-sheet-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.nyc-sheet-label {
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--nyc-faint);
}
.nyc-layer-toggles {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}
.nyc-layer {
    border: 1px solid var(--nyc-faint);
    border-radius: 8px;
    min-height: 34px;
    padding: 8px 11px;
    font: inherit;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.02em;
    color: var(--nyc-muted);
    background: rgba(255, 255, 255, 0.03);
    cursor: pointer;
    transition: background var(--nyc-fast), color var(--nyc-fast), border-color var(--nyc-fast);
}
.nyc-layer:hover { color: var(--nyc-fg); border-color: rgba(255, 255, 255, 0.4); }
.nyc-layer.is-active { color: #0a0a0a; background: #f2f2f2; border-color: #f2f2f2; }
#nyc-layer-affluence.is-active { background: var(--nyc-sig-affluence); border-color: var(--nyc-sig-affluence); }
#nyc-layer-safety.is-active { background: var(--nyc-sig-safety); border-color: var(--nyc-sig-safety); }
#nyc-layer-popularity.is-active { background: var(--nyc-sig-popular); border-color: var(--nyc-sig-popular); }
.nyc-signal-note {
    margin: 0;
    font-size: 10.5px;
    line-height: 1.5;
    color: var(--nyc-faint);
}
.nyc-switch {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    padding: 10px;
    border: 1px solid var(--nyc-faint);
    border-radius: var(--nyc-radius-sm);
    background: rgba(255, 255, 255, 0.02);
    font-size: 11.5px;
    line-height: 1.45;
    color: var(--nyc-muted);
    cursor: pointer;
}
.nyc-switch strong { color: var(--nyc-fg); font-weight: 600; }
.nyc-switch input { margin-top: 2px; accent-color: #f2f2f2; width: 16px; height: 16px; }
.nyc-switch a { color: var(--nyc-accent); }
.nyc-sheet-foot {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding-top: 10px;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
}
.nyc-detail {
    font-size: 10px;
    letter-spacing: 0.06em;
    color: var(--nyc-faint);
    transition: color 0.25s var(--nyc-ease);
}
.nyc-detail.is-loading { color: var(--nyc-accent); }
body.nyc-detail-on .nyc-detail { color: var(--nyc-muted); }
.nyc-attrib {
    margin: 0;
    font-size: 10px;
    color: var(--nyc-faint);
    letter-spacing: 0.02em;
    line-height: 1.5;
}

/* Signal legend — visible meaning for an active signal layer. Bottom-left so
   it clears the control rail (bottom-right) and the location chip. */
.nyc-signal-legend {
    position: absolute;
    left: 14px;
    bottom: calc(14px + var(--nyc-safe-b));
    z-index: 6;
    display: flex;
    flex-wrap: wrap;
    gap: 4px 14px;
    padding: 8px 12px;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 10px;
    background: rgba(5, 5, 5, 0.72);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
}
.nyc-legend-item {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    font-size: 10.5px;
    letter-spacing: 0.03em;
    color: var(--nyc-muted, #aab3c5);
    white-space: nowrap;
}
.nyc-legend-item i {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 3px;
    opacity: 0.9;
}
.nyc-signal-legend[hidden] { display: none; }

@media (max-width: 640px) {
    .nyc-signal-legend { left: 10px; right: auto; bottom: calc(86px + var(--nyc-safe-b)); }
    .nyc-legend-item { font-size: 10px; }
}

/* Current-street readout (shown only in walk mode). */
.nyc-location {
    margin-top: 6px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.04em;
    color: #eef1f7;
}
body.nyc-walk-on #nyc-tilt { opacity: 0.4; pointer-events: none; }
body.nyc-walk-on .nyc-compass { opacity: 0.5; }

/* ---- Idle chrome: the map gets clean when the user stops interacting. The
   rail, compass and back-link fade out; a pointer/key/touch brings them back.
   Never hides while the settings sheet is open. ---- */
body.nyc-chrome-hidden:not(.nyc-sheet-open) .nyc-nav,
body.nyc-chrome-hidden:not(.nyc-sheet-open) .nyc-compass,
body.nyc-chrome-hidden:not(.nyc-sheet-open) .nyc-back,
body.nyc-chrome-hidden:not(.nyc-sheet-open) .nyc-hud {
    opacity: 0;
    pointer-events: none;
}
/* The legend explains the painted layer: it stays visible even when the
   chrome fades (it IS the data, not a control). */
body.nyc-chrome-hidden:not(.nyc-sheet-open) .nyc-signal-legend {
    opacity: 0.85;
}
body.nyc-chrome-hidden .nyc-nav { transform: translateY(6px); }

/* ---- First-run coach marks ---- */
.nyc-coach {
    position: absolute;
    left: 50%;
    bottom: calc(96px + var(--nyc-safe-b));
    transform: translateX(-50%);
    z-index: 9;
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 14px 16px;
    border: 1px solid var(--nyc-faint);
    border-radius: var(--nyc-radius);
    background: var(--nyc-panel-solid);
    backdrop-filter: var(--nyc-blur);
    -webkit-backdrop-filter: var(--nyc-blur);
    box-shadow: var(--nyc-shadow);
    transition: opacity 0.5s var(--nyc-ease), transform 0.5s var(--nyc-ease);
}
.nyc-coach.is-hidden {
    opacity: 0;
    transform: translateX(-50%) translateY(8px);
    pointer-events: none;
}
.nyc-coach-row {
    display: grid;
    grid-template-columns: 92px 1fr;
    gap: 10px;
    align-items: center;
    font-size: 12px;
    color: var(--nyc-muted);
}
.nyc-coach kbd {
    font-family: inherit;
    font-size: 10px;
    font-weight: 600;
    color: var(--nyc-fg);
    border: 1px solid var(--nyc-faint);
    border-radius: 5px;
    padding: 3px 7px;
    background: rgba(255, 255, 255, 0.05);
    justify-self: start;
    white-space: nowrap;
}
.nyc-coach-ok {
    align-self: flex-end;
    margin-top: 2px;
    min-height: 34px;
    border: 1px solid var(--nyc-faint);
    border-radius: 7px;
    padding: 7px 14px;
    font: inherit;
    font-size: 12px;
    font-weight: 600;
    color: var(--nyc-fg);
    background: rgba(255, 255, 255, 0.05);
    cursor: pointer;
}
.nyc-coach-ok:hover { border-color: rgba(255, 255, 255, 0.4); }

/* ---- Back link ---- */
.nyc-back {
    position: absolute;
    top: calc(18px + var(--nyc-safe-t));
    right: calc(18px + var(--nyc-safe-r));
    z-index: 6;
    font-size: 12px;
    color: var(--nyc-fg);
    text-decoration: none;
    border: 1px solid var(--nyc-faint);
    border-radius: var(--nyc-radius-sm);
    padding: 9px 13px;
    background: rgba(255, 255, 255, 0.05);
    transition: color var(--nyc-fast), border-color var(--nyc-fast), opacity 0.5s var(--nyc-ease);
}
.nyc-back:hover { color: #fff; border-color: rgba(255, 255, 255, 0.5); }

/* ---- Selection panel ---- */
.nyc-selection {
    position: absolute;
    right: calc(18px + var(--nyc-safe-r));
    top: calc(118px + var(--nyc-safe-t));
    z-index: 6;
    width: min(80vw, 300px);
    padding: 14px 16px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: var(--nyc-radius);
    background: rgba(10, 10, 12, 0.92);
    backdrop-filter: var(--nyc-blur);
    -webkit-backdrop-filter: var(--nyc-blur);
    box-shadow: var(--nyc-shadow);
}
.nyc-selection[hidden] { display: none; }
.nyc-sel-kicker {
    font-size: 10px;
    letter-spacing: 0.18em;
    text-transform: uppercase;
    color: var(--nyc-accent);
    margin-bottom: 5px;
}
.nyc-sel-title {
    font-size: 15px;
    font-weight: 600;
    line-height: 1.35;
    margin-bottom: 6px;
}
.nyc-sel-body {
    font-size: 12.5px;
    line-height: 1.5;
    color: var(--nyc-muted);
    max-height: 40vh;
    overflow-y: auto;
}
.nyc-sel-close {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    color: var(--nyc-faint);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
}
.nyc-sel-close:hover { color: var(--nyc-fg); }
/* Signal compare block inside the selection panel. */
.nyc-sig-compare {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.nyc-sig-row {
    display: grid;
    grid-template-columns: 78px 1fr;
    align-items: center;
    gap: 8px;
    font-size: 11px;
    color: var(--nyc-muted);
}
.nyc-sig-bar {
    height: 6px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.08);
    overflow: hidden;
}
.nyc-sig-bar > i {
    display: block;
    height: 100%;
    border-radius: 3px;
    background: var(--nyc-fg);
}
.nyc-sig-note {
    font-size: 10.5px;
    line-height: 1.4;
    color: var(--nyc-faint);
    margin: -2px 0 4px 0;
}
.nyc-sig-caveat {
    margin: 8px 0 0;
    font-size: 10.5px;
    line-height: 1.45;
    color: var(--nyc-faint);
    font-style: italic;
}

/* ---- Loading / fatal ---- */
.nyc-loading {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    background: var(--nyc-bg);
    color: var(--nyc-muted);
    font-size: 12px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    transition: opacity 0.6s var(--nyc-ease);
}
.nyc-loading.is-hidden { opacity: 0; pointer-events: none; }
.nyc-fatal {
    position: absolute;
    inset: 0;
    z-index: 11;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 32px;
    text-align: center;
    background: var(--nyc-bg);
}
.nyc-fatal[hidden] { display: none; }
.nyc-fatal h2 { margin: 0; font-size: 18px; font-weight: 600; }
.nyc-fatal p {
    margin: 0;
    max-width: 46ch;
    font-size: 13px;
    line-height: 1.6;
    color: var(--nyc-muted);
}
.nyc-fatal a {
    color: var(--nyc-accent);
    font-size: 13px;
    text-decoration: none;
    border-bottom: 1px solid currentColor;
}

@media (max-width: 720px) {
    /* On phones the rail spans the width above the safe area, and the sheet
       rises as a bottom sheet rather than a corner card. */
    .nyc-nav {
        left: calc(14px + var(--nyc-safe-l));
        right: calc(14px + var(--nyc-safe-r));
        bottom: calc(14px + var(--nyc-safe-b));
        flex-direction: row;
        align-items: stretch;
        gap: 6px;
        padding: 6px;
    }
    .nyc-nav-btn {
        width: auto;
        flex: 1 1 0;
        height: var(--nyc-touch);
    }
    .nyc-nav-btn svg { width: 22px; height: 22px; }
    .nyc-sheet {
        left: calc(10px + var(--nyc-safe-l));
        right: calc(10px + var(--nyc-safe-r));
        bottom: calc(10px + var(--nyc-safe-b));
        width: auto;
        max-height: min(78vh, 640px);
    }
    .nyc-selection {
        top: auto;
        bottom: calc(84px + var(--nyc-safe-b));
        left: calc(14px + var(--nyc-safe-l));
        right: auto;
        width: min(86vw, 320px);
    }
    .nyc-compass { top: calc(62px + var(--nyc-safe-t)); width: var(--nyc-touch); height: var(--nyc-touch); }
    .nyc-back {
        top: calc(12px + var(--nyc-safe-t));
        right: calc(12px + var(--nyc-safe-r));
        padding: 10px 13px;
        min-height: var(--nyc-touch);
        display: inline-flex;
        align-items: center;
    }
    .nyc-hud { top: calc(14px + var(--nyc-safe-t)); left: calc(14px + var(--nyc-safe-l)); max-width: 55vw; }
    .nyc-coach {
        top: 50%;
        bottom: auto;
        transform: translate(-50%, -50%);
        width: min(90vw, 360px);
        max-height: 82vh;
        overflow-y: auto;
    }
    .nyc-coach.is-hidden { transform: translate(-50%, -50%) scale(0.96); }
    .nyc-coach-row { grid-template-columns: 86px 1fr; }
    .nyc-coach-ok { min-height: var(--nyc-touch); padding: 10px 16px; font-size: 13px; }
}

/* Short landscape phones: the bottom stack would eat the whole viewport. */
@media (max-width: 720px) and (max-height: 480px) {
    .nyc-coach { top: auto; bottom: 8px; transform: translateX(-50%); }
    .nyc-coach.is-hidden { transform: translateX(-50%) translateY(8px); }
    .nyc-coach-row:nth-child(n + 6) { display: none; }
    .nyc-nav { bottom: calc(8px + var(--nyc-safe-b)); }
    .nyc-sheet { max-height: 70vh; }
}

@media (prefers-reduced-motion: reduce) {
    .nyc-dot { animation: none; }
    .nyc-compass-needle { transition: none; }
    .nyc-coach { transition: none; }
    .nyc-nav, .nyc-compass, .nyc-back, .nyc-hud { transition: none; }
    .nyc-sheet { animation: none; }
}
