/* ---------------------------------------------------------------- tokens -- */

:root {
    color-scheme: light;

    --plane: #f2f2ef;
    --surface: #fcfcfb;
    --surface-sunken: #f6f6f3;

    --ink: #0b0b0b;
    --ink-2: #52514e;
    --ink-muted: #898781;

    --grid: #e1e0d9;
    --axis: #c3c2b7;
    --border: rgba(11, 11, 11, 0.10);
    --shadow: 0 1px 2px rgba(11, 11, 11, 0.06), 0 8px 24px rgba(11, 11, 11, 0.06);

    /* Categorical slots, in the fixed order they must be assigned. */
    --series-1: #2a78d6;
    --series-2: #eb6834;
    --series-3: #1baf7a;
    --series-4: #eda100;
    --series-5: #e87ba4;
    --series-6: #008300;

    --accent: #2a78d6;
    --accent-ink: #184f95;
    --land: #2a78d6;
    --land-edge: #184f95;
    --land-b: #eb6834;
    --land-b-edge: #a8431a;

    --radius: 14px;
    --radius-sm: 9px;
}

@media (prefers-color-scheme: dark) {
    :root:where(:not([data-theme="light"])) {
        color-scheme: dark;

        --plane: #0d0d0d;
        --surface: #1a1a19;
        --surface-sunken: #141413;

        --ink: #ffffff;
        --ink-2: #c3c2b7;
        --ink-muted: #898781;

        --grid: #2c2c2a;
        --axis: #383835;
        --border: rgba(255, 255, 255, 0.10);
        --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.4);

        --series-1: #3987e5;
        --series-2: #d95926;
        --series-3: #199e70;
        --series-4: #c98500;
        --series-5: #d55181;
        --series-6: #008300;

        --accent: #3987e5;
        --accent-ink: #86b6ef;
        --land: #3987e5;
        --land-edge: #86b6ef;
        --land-b: #d95926;
        --land-b-edge: #f0946c;
    }
}

:root[data-theme="dark"] {
    color-scheme: dark;

    --plane: #0d0d0d;
    --surface: #1a1a19;
    --surface-sunken: #141413;

    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --ink-muted: #898781;

    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.4);

    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;

    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --land: #3987e5;
    --land-edge: #86b6ef;
    --land-b: #d95926;
    --land-b-edge: #f0946c;
}

/* ----------------------------------------------------------------- base -- */

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

body {
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    background: var(--plane);
    color: var(--ink);
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
}

a {
    color: inherit;
    text-decoration: none;
}

button,
select,
input {
    font: inherit;
    color: inherit;
}

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.container {
    max-width: 1280px;
    margin: 0 auto;
    padding: 0 20px 72px;
}

/* --------------------------------------------------------------- header -- */

.site-header {
    position: sticky;
    top: 0;
    z-index: 40;
    background: color-mix(in srgb, var(--plane) 88%, transparent);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
}

.site-header-inner {
    max-width: 1280px;
    margin: 0 auto;
    padding: 12px 20px;
    display: flex;
    align-items: center;
    gap: 20px;
}

.brand {
    font-weight: 650;
    font-size: 1.05rem;
    letter-spacing: -0.01em;
    white-space: nowrap;
}

.site-nav {
    display: flex;
    gap: 2px;
    flex-wrap: wrap;
    margin-left: auto;
}

.site-nav a {
    padding: 7px 13px;
    border-radius: 999px;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--ink-2);
    transition: background 0.15s ease, color 0.15s ease;
}

.site-nav a:hover {
    background: var(--surface-sunken);
    color: var(--ink);
}

.site-nav a[aria-current="page"] {
    background: var(--ink);
    color: var(--surface);
}

.theme-toggle {
    width: 34px;
    height: 34px;
    display: grid;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    cursor: pointer;
    font-size: 0.95rem;
    line-height: 1;
}

.theme-toggle:hover {
    background: var(--surface-sunken);
}

/* ------------------------------------------------------------ page head -- */

.page-head {
    padding: 40px 0 24px;
}

.page-head h1 {
    font-size: clamp(1.75rem, 4vw, 2.5rem);
    letter-spacing: -0.025em;
    line-height: 1.1;
}

.lede {
    margin-top: 8px;
    color: var(--ink-2);
    max-width: 62ch;
    font-size: 1rem;
}

/* -------------------------------------------------------------- toolbar -- */

.toolbar {
    position: sticky;
    top: 59px;
    z-index: 30;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
    padding: 12px 0 16px;
    background: var(--plane);
}

.search-input {
    flex: 1 1 240px;
    min-width: 0;
    padding: 9px 14px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    outline: none;
}

.search-input::placeholder {
    color: var(--ink-muted);
}

.control {
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    cursor: pointer;
}

.result-count {
    color: var(--ink-muted);
    font-size: 0.85rem;
    margin-left: auto;
    white-space: nowrap;
}

/* ---------------------------------------------------------------- cards -- */

.grid {
    display: grid;
    gap: 14px;
    grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
}

.grid--wide {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    cursor: pointer;
    text-align: left;
    width: 100%;
    display: block;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.card-flag {
    display: block;
    width: 100%;
    aspect-ratio: 4 / 3;
    object-fit: cover;
    background: var(--surface-sunken);
    border-bottom: 1px solid var(--border);
}

.card-body {
    padding: 11px 13px 13px;
}

.card-title {
    font-size: 0.92rem;
    font-weight: 600;
    letter-spacing: -0.01em;
    line-height: 1.25;
}

.card-meta {
    margin-top: 3px;
    font-size: 0.78rem;
    color: var(--ink-muted);
}

/* Shapes */

.shape-frame {
    display: grid;
    place-items: center;
    aspect-ratio: 4 / 3;
    padding: 14px;
    background: var(--surface-sunken);
    border-bottom: 1px solid var(--border);
}

.country-shape {
    width: 100%;
    height: 100%;
    fill: var(--land);
    stroke: var(--land-edge);
    /* Sub-pixel islands would vanish without a stroke to give them body. */
    stroke-width: 0.6;
    stroke-linejoin: round;
}

.shape-fallback {
    max-width: 70%;
    max-height: 70%;
    border-radius: 4px;
}

/* Data rows */

.data-card {
    padding: 14px;
}

.data-card-head {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.data-card-head img {
    width: 34px;
    height: 26px;
    object-fit: cover;
    border-radius: 3px;
    border: 1px solid var(--border);
    flex: none;
}

.data-row {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 6px 0;
    font-size: 0.86rem;
    border-top: 1px solid var(--grid);
}

.data-row dt {
    color: var(--ink-muted);
}

.data-row dd {
    font-weight: 600;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* --------------------------------------------------------- country page -- */

.breadcrumb {
    padding: 24px 0 0;
    font-size: 0.85rem;
    color: var(--ink-muted);
}

.breadcrumb a:hover {
    color: var(--ink);
}

.profile-head {
    display: flex;
    align-items: flex-start;
    gap: 18px;
    padding: 24px 0 26px;
}

.profile-head img {
    width: 96px;
    height: 72px;
    object-fit: cover;
    border-radius: 6px;
    border: 1px solid var(--border);
    flex: none;
}

.profile-head h1 {
    font-size: clamp(1.6rem, 4vw, 2.25rem);
    letter-spacing: -0.025em;
    line-height: 1.1;
}

.profile-head p {
    color: var(--ink-muted);
    font-size: 0.9rem;
    margin-top: 4px;
}

.profile-grid {
    display: grid;
    grid-template-columns: 220px 1fr;
    gap: 24px;
    align-items: start;
}

.profile-shape {
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 16px;
    display: grid;
    place-items: center;
    aspect-ratio: 1;
    min-width: 0;
}

/* ------------------------------------------------------------ stat tile -- */

.tiles {
    display: grid;
    gap: 12px;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
}

.tile {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px 16px;
}

.tile-label {
    font-size: 0.78rem;
    color: var(--ink-muted);
}

.tile-value {
    font-size: 1.45rem;
    font-weight: 650;
    letter-spacing: -0.02em;
    margin-top: 2px;
}

.tile-note {
    font-size: 0.78rem;
    color: var(--ink-2);
    margin-top: 2px;
}

.hero-figure {
    font-size: clamp(2.75rem, 7vw, 3.75rem);
    font-weight: 680;
    letter-spacing: -0.035em;
    line-height: 1;
}

/* --------------------------------------------------------------- charts -- */

.panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin-top: 20px;
}

.panel-head {
    margin-bottom: 4px;
}

.panel-head h2 {
    font-size: 1.05rem;
    letter-spacing: -0.015em;
}

.panel-head p {
    color: var(--ink-muted);
    font-size: 0.83rem;
    margin-top: 2px;
}

.bar-chart {
    margin-top: 16px;
    display: grid;
    gap: 9px;
}

.bar-row {
    display: grid;
    grid-template-columns: 150px 1fr;
    gap: 12px;
    align-items: center;
}

.bar-label {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 0.84rem;
    color: var(--ink-2);
    min-width: 0;
}

.bar-label img {
    width: 18px;
    height: 13px;
    object-fit: cover;
    border-radius: 2px;
    border: 1px solid var(--border);
    flex: none;
}

.bar-label span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* The value sits in its own column so a full-width bar can never push it out
   of the panel. */
.bar-track {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    align-items: center;
    /* The bar grows from a single baseline at the left. */
    border-left: 1px solid var(--axis);
}

.bar-fill {
    display: block;
    height: 18px;
    background: var(--series-1);
    border-radius: 0 4px 4px 0;
    min-width: 2px;
}

.bar-value {
    font-size: 0.8rem;
    color: var(--ink-2);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

/* Stacked share bar */

.stack {
    margin-top: 14px;
    display: flex;
    /* A 2px surface gap does the separating between segments. */
    gap: 2px;
    height: 26px;
    border-radius: 4px;
    overflow: hidden;
}

.stack-seg {
    min-width: 2px;
    cursor: default;
}

.legend {
    margin-top: 12px;
    display: flex;
    flex-wrap: wrap;
    gap: 6px 16px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.8rem;
    color: var(--ink-2);
}

.legend-swatch {
    width: 10px;
    height: 10px;
    border-radius: 3px;
    flex: none;
}

.table-toggle {
    margin-top: 14px;
    font-size: 0.82rem;
    color: var(--ink-muted);
}

.table-toggle summary {
    cursor: pointer;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
    font-size: 0.82rem;
}

.data-table th,
.data-table td {
    text-align: right;
    padding: 5px 8px;
    border-bottom: 1px solid var(--grid);
    font-variant-numeric: tabular-nums;
}

.data-table th:first-child,
.data-table td:first-child {
    text-align: left;
    font-variant-numeric: normal;
}

.data-table th {
    color: var(--ink-muted);
    font-weight: 500;
}

/* -------------------------------------------------------------- compare -- */

.compare-pickers {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    gap: 12px;
    align-items: center;
    margin-bottom: 20px;
}

.compare-pickers .control {
    width: 100%;
}

.versus {
    color: var(--ink-muted);
    font-size: 0.85rem;
    font-weight: 600;
}

.scale-stage {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    align-items: end;
    padding: 24px 20px;
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

.scale-cell {
    display: grid;
    justify-items: center;
    gap: 10px;
}

/* Height is fixed so the two outlines share one ruler; each shape is sized as a
   fraction of that height, which keeps it inside the stage however large it is. */
.scale-box {
    width: 100%;
    height: 240px;
    display: grid;
    place-items: center;
}

/* A grid item will not shrink below its content's intrinsic size by default, and
   an SVG's is 300px — which would override the scale we just calculated. */
.scale-box > div {
    max-width: 100%;
    min-width: 0;
    min-height: 0;
}

.scale-cell.is-b .country-shape {
    fill: var(--land-b);
    stroke: var(--land-b-edge);
}

.scale-caption {
    text-align: center;
    font-size: 0.82rem;
    color: var(--ink-2);
}

.scale-caption strong {
    display: block;
    font-size: 0.95rem;
    color: var(--ink);
}

.compare-rows {
    margin-top: 20px;
    display: grid;
    gap: 14px;
}

.compare-row {
    display: grid;
    gap: 5px;
}

.compare-row-head {
    display: flex;
    justify-content: space-between;
    font-size: 0.82rem;
    color: var(--ink-muted);
}

.compare-bars {
    display: grid;
    gap: 3px;
}

.compare-bar {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    align-items: center;
}

.compare-bar i {
    display: block;
    height: 14px;
    border-radius: 0 4px 4px 0;
    background: var(--series-1);
    min-width: 2px;
}

.compare-bar.is-b i {
    background: var(--series-2);
}

.compare-bar b {
    font-size: 0.8rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.ratio-note {
    font-size: 0.83rem;
    color: var(--ink-2);
    margin-top: 2px;
}

/* -------------------------------------------------------------- tooltip -- */

.tooltip {
    position: fixed;
    z-index: 200;
    pointer-events: none;
    background: var(--ink);
    color: var(--plane);
    padding: 6px 9px;
    border-radius: 6px;
    font-size: 0.78rem;
    white-space: nowrap;
    transform: translate(-50%, -140%);
}

.tooltip[hidden] {
    display: none;
}

/* --------------------------------------------------------------- states -- */

.empty-state {
    grid-column: 1 / -1;
    text-align: center;
    padding: 56px 20px;
    color: var(--ink-muted);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
}

/* ---------------------------------------------------------- responsive -- */

@media (max-width: 720px) {
    .site-header-inner {
        flex-wrap: wrap;
        gap: 10px;
    }

    .site-nav {
        order: 3;
        width: 100%;
        margin-left: 0;
        overflow-x: auto;
    }

    .toolbar {
        top: 0;
        position: static;
    }

    .profile-grid,
    .compare-pickers,
    .scale-stage {
        grid-template-columns: 1fr;
    }

    .bar-row {
        grid-template-columns: 110px 1fr;
    }
}

/* ------------------------------------------------------ compare presets -- */

.presets-title {
    margin: 32px 0 12px;
    font-size: 1.05rem;
    letter-spacing: -0.015em;
}

.presets {
    display: grid;
    gap: 10px;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
}

.preset {
    display: grid;
    gap: 4px;
    text-align: left;
    padding: 12px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.preset:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.preset-pair {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.88rem;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.preset-pair img {
    width: 20px;
    height: 15px;
    object-fit: cover;
    border-radius: 2px;
    border: 1px solid var(--border);
    flex: none;
}

.preset-pair img + img {
    margin-left: -4px;
    margin-right: 2px;
}

.preset-hook {
    font-size: 0.79rem;
    color: var(--ink-muted);
    line-height: 1.35;
}

.scale-hint {
    display: block;
    margin-top: 3px;
    font-size: 0.76rem;
    color: var(--ink-muted);
    font-style: normal;
}

/* ----------------------------------------------------------- similarity -- */

.similar-title {
    margin: 22px 0 10px;
    font-size: 0.9rem;
    letter-spacing: -0.01em;
}

.similar-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 10px;
}

.similar {
    display: grid;
    justify-items: center;
    gap: 3px;
    padding: 10px 8px;
    border-radius: var(--radius-sm);
}

.similar-shape {
    display: block;
    width: 46px;
    height: 46px;
}

.similar-name {
    font-size: 0.8rem;
    font-weight: 600;
    text-align: center;
    line-height: 1.2;
}

.similar-note {
    font-size: 0.72rem;
    color: var(--ink-muted);
    text-align: center;
}

/* Overlap read-out on the compare page */

.overlap {
    display: flex;
    align-items: center;
    gap: 18px;
    margin-top: 20px;
    padding: 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

.overlap-stack {
    position: relative;
    width: 92px;
    height: 92px;
    flex: none;
}

.overlap-stack > span {
    position: absolute;
    inset: 0;
    display: block;
}

/* Half-transparent so the shared area reads as the darker overlap. */
.overlap-a .country-shape {
    fill: var(--land);
    stroke: none;
    opacity: 0.65;
}

.overlap-b .country-shape {
    fill: var(--land-b);
    stroke: none;
    opacity: 0.65;
}

.overlap-score {
    font-size: 1.15rem;
    font-weight: 650;
    letter-spacing: -0.02em;
}

.overlap-note {
    font-size: 0.8rem;
    color: var(--ink-muted);
    margin-top: 3px;
    max-width: 60ch;
}

@media (max-width: 720px) {
    .overlap {
        flex-direction: column;
        align-items: flex-start;
    }
}

/* ------------------------------------------------------------- timeline -- */

.timeline-controls {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
}

.year-slider {
    flex: 1 1 auto;
    min-width: 0;
    accent-color: var(--accent);
}

.year-readout {
    display: flex;
    align-items: baseline;
    gap: 8px;
    white-space: nowrap;
}

.year-readout strong {
    font-size: 1.35rem;
    letter-spacing: -0.02em;
    font-variant-numeric: tabular-nums;
}

.year-readout span {
    font-size: 0.82rem;
    color: var(--ink-muted);
}

.map-frame {
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px;
    overflow: hidden;
}

.world-map {
    display: block;
    width: 100%;
    height: auto;
}

.world-map path {
    fill: var(--land);
    /* The gap between neighbours is the surface showing through, not a drawn line. */
    stroke: var(--surface-sunken);
    stroke-width: 0.8;
    transition: fill 0.1s ease;
}

.world-map path:hover {
    fill: var(--land-b);
}

.map-empty {
    fill: var(--ink-muted);
    font-size: 16px;
}

.entity-kind {
    font-size: 0.8rem;
    color: var(--ink-muted);
    margin-top: 2px;
}

/* Past flags, when flags-history.js has been generated */

.flag-history {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 12px;
}

.flag-era img {
    display: block;
    width: 100%;
    aspect-ratio: 4 / 3;
    object-fit: contain;
    background: var(--surface-sunken);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

.flag-era figcaption {
    margin-top: 5px;
    font-size: 0.78rem;
    color: var(--ink-muted);
    font-variant-numeric: tabular-nums;
}

/* Signed differences on the numeric matches */

.similar-row--wide {
    grid-template-columns: repeat(auto-fit, minmax(175px, 1fr));
}

.delta-list {
    display: grid;
    gap: 2px;
    width: 100%;
    margin-top: 6px;
    padding-top: 6px;
    border-top: 1px solid var(--grid);
    font-size: 0.75rem;
    font-variant-numeric: tabular-nums;
}

.delta {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    color: var(--ink-muted);
}

/* Direction is carried by the sign; colour only reinforces it. */
.delta b.is-more { color: var(--ink-2); }
.delta b.is-less { color: var(--ink-2); }
