:root {
    --viewport-height: 100vh;
    --map-size: min(100vw, var(--viewport-height));
}

@supports (height: 100dvh) {
    :root {
        --viewport-height: 100dvh;
    }
}

* { box-sizing: border-box; }

html,
body {
    margin: 0;
    width: 100%;
    height: var(--viewport-height);
    overflow: hidden;
    background: #f8f4e5;
}

body {
    background-image: url("images/maptile.webp?v=20261013b");
    background-repeat: repeat-x;
    background-position: center top;
    background-size:
        calc(var(--map-size) * 0.81659)
        calc(var(--map-size) * 2.39234);
}

#world {
    position: relative;
    width: 100vw;
    height: var(--viewport-height);
}

#map {
    position: absolute;
    top: 0;
    left: 50%;
    width: var(--map-size);
    height: var(--map-size);
    transform: translateX(-50%);
    background-image: url("images/map.webp?v=20261013b");
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
}

#resident-layer {
    position: absolute;
    inset: 0;
    z-index: 2;
    pointer-events: none;
}

/* Keep resident data and markup intact while the graph editor owns the map. */
#map.is-graph-editing #resident-layer,
#map.is-graph-editing #residents-overlay {
    visibility: hidden;
}

/* Same 1:1 box as the artwork. It is visible only when the JS debug switch is on. */
#navigation-overlay {
    position: absolute;
    z-index: 1;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
    pointer-events: none;
}

/* The brighter central island is Zodogo Park. */
#park-link {
    --park-label-height: 22px;
    position: absolute;
    z-index: 1;
    left: 44.4%;
    top: 37.4%;
    width: 11.2%;
    height: calc(11.2% + var(--park-label-height));
    cursor: pointer;
}

#park-link::before,
#park-link::after {
    position: absolute;
    left: 0;
    width: 100%;
    opacity: 0;
    pointer-events: none;
    transition: opacity 140ms ease;
}

#park-link::before {
    content: "";
    top: 0;
    bottom: var(--park-label-height);
    border: 1px solid rgba(34, 34, 34, 0.84);
    border-radius: 4px 4px 0 0;
    box-shadow:
        0 0 0 2px rgba(255, 255, 255, 0.96),
        0 1px 4px rgba(0, 0, 0, 0.16);
}

#park-link::after {
    content: "ZODOGO PARK";
    bottom: 0;
    height: var(--park-label-height);
    display: grid;
    place-items: center;
    border: 1px solid rgba(34, 34, 34, 0.84);
    border-top: 0;
    border-radius: 0 0 4px 4px;
    background: #fff;
    color: #222;
    font: 600 10px/1 ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    letter-spacing: 0.08em;
}

#park-link:hover::before,
#park-link:hover::after,
#park-link:focus-visible::before,
#park-link:focus-visible::after,
#park-link.is-previewing::before,
#park-link.is-previewing::after {
    opacity: 1;
}

#park-link:focus-visible {
    outline: none;
}

#navigation-overlay .navigation-route {
    fill: none;
    stroke: #ff42e1;
    stroke-width: 4;
    stroke-linecap: round;
    stroke-linejoin: round;
    opacity: 0;
    pointer-events: none;
}

/* Optional Dijkstra route preview; enabled by NAVIGATION_ROUTE_DEBUG in map.js. */
#navigation-overlay .navigation-route.is-route-highlighted {
    stroke: #00bcd4;
    stroke-width: 8;
    opacity: 1;
}

#navigation-overlay .navigation-node,
#navigation-overlay .navigation-node-label { opacity: 0; }

#navigation-overlay.is-debug .navigation-route,
#navigation-overlay.is-debug .navigation-node { opacity: 1; }

#navigation-overlay.is-debug.has-debug-labels .navigation-node-label { opacity: 1; }

/* Degree-zero nodes need attention even when ordinary debug labels are hidden. */
#navigation-overlay.is-debug .navigation-node-label.is-isolated { opacity: 1; }

/* Calibration dots must remain visible where Fetch is positioned. */
#navigation-overlay.is-debug { z-index: 5; }

#navigation-overlay .navigation-node {
    fill: #fff;
    stroke: #ff42e1;
    stroke-width: 3;
    pointer-events: none;
}

#navigation-overlay .navigation-node.is-selected {
    fill: #ff42e1;
    stroke: #fff;
    stroke-width: 5;
}

#navigation-overlay .navigation-node.is-isolated {
    fill: #ffc107;
    stroke: #8a1b00;
    stroke-width: 5;
}

#navigation-overlay .navigation-node-label.is-isolated {
    fill: #8a1b00;
    font-weight: 700;
}

/* Optional domain inspection view. Neutral nodes remain deliberately muted. */
#navigation-overlay.is-domain-debug .navigation-node.is-domain-assigned:not(.is-isolated) {
    fill: var(--domain-colour);
    stroke: #1b1020;
    stroke-width: 4;
}

#navigation-overlay.is-domain-debug .navigation-node.is-domain-neutral:not(.is-isolated) {
    fill: #b7bdc4;
    stroke: #48515a;
    stroke-width: 4;
}

#navigation-overlay.is-domain-debug .navigation-node.is-selected {
    fill: #ff42e1;
    stroke: #fff;
    stroke-width: 5;
}

/* Only these transparent circles receive clicks; the map background does not. */
#navigation-overlay .navigation-node-hit-target {
    fill: transparent;
    pointer-events: all;
    cursor: pointer;
}

#navigation-overlay .navigation-node-label {
    fill: #1b1020;
    font: 16px/1 sans-serif;
    paint-order: stroke;
    stroke: #fff;
    stroke-width: 5px;
    stroke-linejoin: round;
    pointer-events: none;
}

/* Domain assignment makes node hit targets interactive; map geometry stays fixed. */
#navigation-overlay.is-editing {
    pointer-events: all;
    cursor: default;
}

#navigation-overlay.is-editing .navigation-node-hit-target {
    cursor: pointer;
}

#graph-editor-panel {
    position: fixed;
    z-index: 6;
    top: 12px;
    right: 0;
    width: min(340px, calc(100% - 24px));
    padding: 8px;
    background: rgba(248, 244, 229, 0.95);
    border: 1px solid #222;
    font: 12px/1.3 sans-serif;
}

#graph-editor-panel[hidden] { display: none; }

#graph-selected-node {
    margin: 0 0 8px;
    font-weight: 600;
}

#graph-domain-options {
    width: 100%;
    margin-top: 3px;
    margin-bottom: 8px;
    padding: 6px;
    border: 1px solid #777;
}

#graph-domain-options label {
    display: block;
    margin: 3px 0;
    font-weight: 400;
}

#graph-export-button {
    display: block;
    margin: 0 0 6px auto;
}

#graph-export {
    display: block;
    width: 100%;
    height: 160px;
    resize: vertical;
    font: 11px/1.3 monospace;
}

@media (max-width: 560px) {
    #graph-editor-panel {
        top: auto;
        right: 0;
        bottom: 12px;
        left: 12px;
        width: auto;
    }

    #graph-export { height: 100px; }
}

#graph-edit-toggle {
    position: absolute;
    z-index: 21;
    top: 12px;
    left: 12px;
    padding: 4px 7px;
    border: 1px solid #222;
    border-radius: 3px;
    background: rgba(248, 244, 229, 0.9);
    color: #222;
    font: 11px/1.2 sans-serif;
    cursor: pointer;
}

#graph-edit-toggle[aria-pressed="true"] {
    background: #ff42e1;
}

.resident-marker {
    position: absolute;
    width: 4%;
    color: #fff;
    --resident-stroke: var(--resident-colour, #99CCff);
    --resident-avatar-circle: var(--resident-colour, #99CCff);
    /* left/top is the resident's bottom-centre ground-contact point. */
    transform: translate(-50%, -100%);
    transition:
        left 3.2s ease-in-out,
        top 3.2s ease-in-out;
    pointer-events: auto;
}

/* A visible card must clear every neighbouring resident marker. */
.resident-marker:hover,
.resident-marker:focus-within,
.resident-marker.is-card-open {
    z-index: 10;
}

.resident-marker.is-at-domain {
    color: var(--resident-colour, #99CCff);
    --resident-stroke: #fff;
    --resident-avatar-circle: #fff;
}

/* Graph travel is rendered by requestAnimationFrame, never CSS chords. */
.resident-marker.is-graph-travelling { transition: none; }

.resident-pin {
    position: relative;
    display: block;
    width: 100%;
    color: inherit;
    aspect-ratio: 100 / 140;
    padding: 0;
    border: 0;
    background: transparent;
    cursor: pointer;
    transition: transform 0.2s ease;
}

.resident-pin:focus-visible {
    outline: 3px solid #ff42e1;
    outline-offset: 3px;
}

.resident-pin-shape {
    position: absolute;
    inset: 0;
    display: block;
    width: 100%;
    height: 100%;
    overflow: visible;
}

.resident-pin-shape path {
    stroke: var(--resident-stroke, #99CCff);
    stroke-width: 5;
    stroke-linejoin: round;
    paint-order: stroke;
}

.resident-avatar-circle {
    position: absolute;
    z-index: 1;
    left: 50%;
    top: 34%;
    width: 72%;
    aspect-ratio: 1;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background: var(--resident-avatar-circle, #fff);
    pointer-events: none;
}

.resident {
    position: absolute;
    z-index: 2;
    left: 50%;
    top: 34%;
    width: 68%;
    height: 68%;
    transform: translate(-50%, -50%);
    object-fit: contain;
    user-select: none;
    pointer-events: none;
}

.resident-marker:hover .resident-pin,
.resident-marker:focus-within .resident-pin {
    transform: translateY(-2px) scale(1.06);
}

.resident-card {
    position: absolute;
    z-index: 30;
    top: calc(100% + 7px);
    left: 50%;
    transform: translateX(-50%);
    width: max-content;
    min-width: 0;
    max-width: min(220px, calc(100vw - 24px));
    padding: 5px 7px;
    background: #fff;
    border: 0;
    border-radius: 3px;
    color: #222;
    text-align: left;
    font: 600 11px/1.2 ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    letter-spacing: 0.08em;
    opacity: 0;
    visibility: hidden;
    pointer-events: auto;
    cursor: default;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.16);
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.resident-card::before {
    content: "";
    position: absolute;
    bottom: 100%;
    left: 50%;
    width: 8px;
    height: 8px;
    background: #fff;
    border: 0;
    transform: translate(-50%, 4px) rotate(45deg);
}

@media (hover: hover) and (pointer: fine) {
    .resident-card {
        transition:
            opacity 0.15s ease 0.2s,
            transform 0.15s ease 0.2s,
            visibility 0s linear 0.2s;
    }

    .resident-marker:hover .resident-card,
    .resident-marker:focus-within .resident-card {
        opacity: 1;
        visibility: visible;
        transform: translateX(-50%) translateY(2px);
        transition-delay: 0s;
    }
}

.resident-marker.is-card-open .resident-card {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(2px);
}

.resident-card span { display: inline; }

.resident-name {
    font-weight: inherit;
}

.resident-status {
    font-weight: inherit;
}

.resident-marker.is-neutral-card .resident-card,
.resident-marker.is-neutral-card .resident-card::before {
    background: #222;
}

.resident-marker.is-neutral-card .resident-card {
    color: #fff;
    box-shadow: none;
}

.resident-marker.is-neutral-card .resident-card-link {
    color: inherit;
}

.resident-card-link {
    color: #222;
    font-weight: inherit;
    line-height: inherit;
    text-decoration: none;
}

.resident-card.is-linked-card {
    padding: 0;
}

.resident-card-link[href] {
    display: block;
    padding: 5px 7px;
    pointer-events: auto;
    cursor: pointer;
}

.resident-card-link[href]:hover {
    background: rgba(34, 34, 34, 0.06);
    opacity: 0.82;
}

.resident-card-link[href]:focus-visible {
    outline: 2px solid #222;
    outline-offset: -2px;
}

#residents-overlay {
    position: absolute;
    z-index: 3;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: contain;
    opacity: 0;
    pointer-events: none;
    user-select: none;
    transition: opacity 180ms ease;
}

#residents-overlay.is-visible { opacity: 1; }

/* Only appears if the data files fail to load. */
#map-status {
    position: absolute;
    z-index: 20;
    left: 50%;
    top: 12px;
    transform: translateX(-50%);
    padding: 6px 9px;
    background: #f8f4e5;
    border: 1px solid #222;
    font: 12px/1.3 sans-serif;
}
