/* History timeline mockup.
   Variables follow the devkit pattern (devkit/templates/css/variables.css)
   trimmed to what this page uses. */

:root {
    --font-primary: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --font-size-base: 16px;
    --font-size-small: 0.833rem;
    --font-size-title: 1.44rem;
    --font-weight-normal: 500;
    --font-weight-semibold: 600;

    --color-bg: #faf9f7;
    --color-surface: #ffffff;
    --color-text: #34495e;
    --color-text-light: #7a8a9a;
    --color-primary: #9B8174;
    --color-border: rgba(0, 0, 0, 0.1);

    /* Ink drawn on top of entity/kind data colours (theme-independent swatches) */
    --color-ink-on-swatch: rgba(0, 0, 0, 0.65);
    /* Hairline outlines around dots/bars; --color-swatch-edge is a dark-mode
       aid that keeps dark entity colours legible (transparent in light mode) */
    --color-outline: rgba(0, 0, 0, 0.2);
    --color-outline-soft: rgba(0, 0, 0, 0.12);
    --color-swatch-edge: transparent;

    --color-period-bg: rgba(155, 129, 116, 0.18);
    --color-period-border: rgba(155, 129, 116, 0.35);
    --color-period-hover: rgba(155, 129, 116, 0.35);

    --color-viz-track: #e4e9ec;
    --color-viz-accent: #c0392b;
    --color-viz-band: rgba(192, 57, 43, 0.35);

    --color-map-water: #dbe7ee;
    --color-map-land: #b9c4c9;
    --color-map-land-stroke: #9fadb4;
    --color-map-hot: rgba(192, 57, 43, 0.55);
    --color-map-hot-stroke: #c0392b;

    --color-cursor-guide: rgba(52, 73, 94, 0.25);

    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-6: 24px;

    --radius-small: 4px;
    --radius-medium: 8px;
    --shadow-medium: 0 4px 12px rgba(0, 0, 0, 0.15);
    --transition-fast: all 0.2s ease;
}

/* Dark theme. Applied explicitly via data-theme="dark" (user toggle), or by
   OS preference when no explicit choice has been made. The two blocks must
   stay identical -- CSS has no way to share them without a preprocessor. */
[data-theme="dark"] {
    --color-bg: #1d1b19;
    --color-surface: #262422;
    --color-text: #d5d9dd;
    --color-text-light: #8b96a0;
    --color-primary: #b59a8b;
    --color-border: rgba(255, 255, 255, 0.12);
    --shadow-medium: 0 4px 12px rgba(0, 0, 0, 0.5);

    --color-ink-on-swatch: rgba(255, 255, 255, 0.85);
    --color-outline: rgba(255, 255, 255, 0.35);
    --color-outline-soft: rgba(255, 255, 255, 0.22);
    --color-swatch-edge: rgba(255, 255, 255, 0.28);

    --color-period-bg: rgba(181, 154, 139, 0.2);
    --color-period-border: rgba(181, 154, 139, 0.4);
    --color-period-hover: rgba(181, 154, 139, 0.38);

    --color-viz-track: #3a3f43;
    --color-viz-accent: #d0453a;
    --color-viz-band: rgba(208, 69, 58, 0.4);

    --color-map-water: #22303a;
    --color-map-land: #45525a;
    --color-map-land-stroke: #5b6a73;
    --color-map-hot: rgba(208, 69, 58, 0.55);
    --color-map-hot-stroke: #d0453a;

    --color-cursor-guide: rgba(213, 217, 221, 0.3);
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --color-bg: #1d1b19;
        --color-surface: #262422;
        --color-text: #d5d9dd;
        --color-text-light: #8b96a0;
        --color-primary: #b59a8b;
        --color-border: rgba(255, 255, 255, 0.12);
        --shadow-medium: 0 4px 12px rgba(0, 0, 0, 0.5);

        --color-ink-on-swatch: rgba(255, 255, 255, 0.85);
        --color-outline: rgba(255, 255, 255, 0.35);
        --color-outline-soft: rgba(255, 255, 255, 0.22);
        --color-swatch-edge: rgba(255, 255, 255, 0.28);

        --color-period-bg: rgba(181, 154, 139, 0.2);
        --color-period-border: rgba(181, 154, 139, 0.4);
        --color-period-hover: rgba(181, 154, 139, 0.38);

        --color-viz-track: #3a3f43;
        --color-viz-accent: #d0453a;
        --color-viz-band: rgba(208, 69, 58, 0.4);

        --color-map-water: #22303a;
        --color-map-land: #45525a;
        --color-map-land-stroke: #5b6a73;
        --color-map-hot: rgba(208, 69, 58, 0.55);
        --color-map-hot-stroke: #d0453a;

        --color-cursor-guide: rgba(213, 217, 221, 0.3);
    }
}

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

body {
    font-family: var(--font-primary);
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-normal);
    color: var(--color-text);
    background: var(--color-bg);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* ======= Masthead ======= */

.masthead {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-3) var(--space-6);
    border-bottom: 1px solid var(--color-border);
    background: var(--color-surface);
}

.masthead h1 {
    font-size: var(--font-size-title);
    font-weight: var(--font-weight-semibold);
    color: var(--color-primary);
}

/* Deploy timestamp: element exists only in deployed copies -- injected
   into the staged index.html by deploy/deploy.py, never present locally. */
.deploy-stamp {
    font-size: var(--font-size-small);
    color: var(--color-text-light);
    margin-left: var(--space-3);
}

.masthead-controls {
    display: flex;
    gap: var(--space-3);
    align-items: center;
}

.masthead-controls button {
    font: inherit;
    font-size: var(--font-size-small);
    color: var(--color-text);
    background: none;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-small);
    padding: var(--space-1) var(--space-3);
    cursor: pointer;
    transition: var(--transition-fast);
}

.masthead-controls button:hover { border-color: var(--color-primary); }

.scale-toggle { display: inline-flex; }
.scale-toggle button { border-radius: 0; }
.scale-toggle button:first-child { border-radius: var(--radius-small) 0 0 var(--radius-small); }
.scale-toggle button:last-child { border-radius: 0 var(--radius-small) var(--radius-small) 0; border-left: none; }
.scale-toggle button.active {
    background: var(--color-primary);
    border-color: var(--color-primary);
    color: white;
}

/* ======= Layout ======= */

.layout {
    display: flex;
    flex: 1;
    align-items: flex-start;
}

.panel {
    width: 270px;
    flex-shrink: 0;
    border-right: 1px solid var(--color-border);
    background: var(--color-surface);
    padding: var(--space-4);
}

.panel-toggle {
    display: block;
    margin-left: auto;
    margin-bottom: var(--space-2);
    width: 26px;
    height: 26px;
    line-height: 1;
    font: inherit;
    font-size: var(--font-size-small);
    color: var(--color-text-light);
    background: none;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-small);
    cursor: pointer;
    transition: var(--transition-fast);
}

.panel-toggle:hover {
    border-color: var(--color-primary);
    color: var(--color-text);
}

.panel.collapsed {
    width: auto;
    padding: var(--space-2);
}
.panel.collapsed .panel-section { display: none; }
.panel.collapsed .panel-toggle { margin-bottom: 0; }

.panel-section + .panel-section { margin-top: var(--space-6); }

.panel-section > summary {
    cursor: pointer;
    list-style: none;
    user-select: none;
}
.panel-section > summary::-webkit-details-marker { display: none; }
.panel-section > summary h2 { display: inline-block; margin-bottom: 0; }
.panel-section[open] > summary h2 { margin-bottom: var(--space-2); }
.panel-section > summary h2::before {
    content: '\25B8';
    display: inline-block;
    margin-right: var(--space-1);
    color: var(--color-text-light);
}
.panel-section[open] > summary h2::before { content: '\25BE'; }

.panel h2 {
    font-size: var(--font-size-small);
    font-weight: var(--font-weight-semibold);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-light);
    margin-bottom: var(--space-2);
}

.panel-hint {
    font-size: var(--font-size-small);
    color: var(--color-text-light);
    margin-bottom: var(--space-3);
}

.panel-option {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--font-size-small);
    color: var(--color-text-light);
    margin-top: var(--space-2);
    cursor: pointer;
}

/* ======= Culture list ======= */

.culture-list, .kind-list { list-style: none; }

.culture-list li {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-1) 0;
    font-size: var(--font-size-small);
    cursor: pointer;
}

.culture-list li.off { opacity: 0.35; }

.culture-list .swatch {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    box-shadow: 0 0 0 1px var(--color-swatch-edge);
    flex-shrink: 0;
}

.culture-list .name { flex: 1; }
.culture-list .name .kind-tag {
    color: var(--color-text-light);
    font-size: 0.7rem;
    margin-left: var(--space-1);
}


/* ======= Kind list ======= */

.kind-list li {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-1) 0;
    font-size: var(--font-size-small);
    cursor: pointer;
}

.kind-list li.off { opacity: 0.35; }

.kind-list .swatch {
    width: 10px;
    height: 10px;
    border-radius: 2px;
    box-shadow: 0 0 0 1px var(--color-swatch-edge);
    flex-shrink: 0;
}

/* ======= Stage / timeline ======= */

.stage {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    padding: var(--space-4);
}

.timeline {
    position: relative;
    display: flex;
    flex-direction: column;
    cursor: grab;
    user-select: none;
    /* pan-y leaves vertical swipes to the browser so the page can still
       scroll on touch in the horizontal orientation; horizontal drags and
       pinches reach the timeline's gesture handlers. Vertical orientation
       flips this (see .timeline.vertical). */
    touch-action: pan-y;
    overflow: hidden;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-medium);
}

.timeline.panning { cursor: grabbing; }

.stage-hint {
    font-size: var(--font-size-small);
    color: var(--color-text-light);
    text-align: center;
    padding-top: var(--space-2);
}

/* Stripes band: pinned culture lifespans */
.stripes {
    position: relative;
    flex: 0 0 auto;
    min-height: 30px;
    padding: var(--space-3) 0 var(--space-2) 0;
}

.stripe {
    position: absolute;
    height: 20px;
    border-radius: var(--radius-small);
    box-shadow: 0 0 0 1px var(--color-swatch-edge);
    opacity: 0.75;
    display: flex;
    align-items: center;
    overflow: visible;
    white-space: nowrap;
}

.stripe .stripe-label {
    font-size: 0.7rem;
    font-weight: var(--font-weight-semibold);
    color: var(--color-ink-on-swatch);
    padding-left: var(--space-2);
    position: sticky;
    left: 0;
}

/* Trunk: the single event line. Height is set by the renderer to fit lanes. */
.trunk {
    position: relative;
    min-height: 80px;
}

.trunk-line {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 6px;
    height: 2px;
    background: var(--color-text-light);
    opacity: 0.5;
}

.event {
    position: absolute;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: var(--space-1);
    height: 18px;
}

.event:hover { z-index: 5; }

.event .dot {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 2px solid var(--color-surface);
    box-shadow: 0 0 0 1px var(--color-outline);
    flex-shrink: 0;
    transition: var(--transition-fast);
}

.event .bar {
    height: 12px;
    border-radius: 6px;
    opacity: 0.9;
    box-shadow: 0 0 0 1px var(--color-outline-soft);
    flex-shrink: 0;
    transition: var(--transition-fast);
}

.event:hover .dot, .event:hover .bar { transform: scale(1.15); }

.event .event-label {
    font-size: 0.72rem;
    font-weight: var(--font-weight-semibold);
    white-space: nowrap;
    color: var(--color-text);
}

/* Axis */
.axis {
    position: relative;
    flex: 0 0 26px;
    border-top: 1px solid var(--color-border);
    background: var(--color-bg);
}

.tick {
    position: absolute;
    top: 0;
    font-size: 0.7rem;
    color: var(--color-text-light);
    transform: translateX(-50%);
    padding-top: var(--space-1);
}

.tick::before {
    content: '';
    position: absolute;
    top: -5px;
    left: 50%;
    width: 1px;
    height: 5px;
    background: var(--color-text-light);
}

/* Periods ribbon */
.periods {
    position: relative;
    flex: 0 0 auto;
    min-height: 34px;
    padding: var(--space-2) 0 var(--space-3) 0;
    background: var(--color-bg);
}

.period {
    position: absolute;
    height: 18px;
    background: var(--color-period-bg);
    border: 1px solid var(--color-period-border);
    border-radius: var(--radius-small);
    font-size: 0.7rem;
    color: var(--color-text);
    display: flex;
    align-items: center;
    overflow: hidden;
    white-space: nowrap;
    cursor: pointer;
    transition: var(--transition-fast);
}

.period:hover { background: var(--color-period-hover); }

.period .period-label {
    padding-left: var(--space-2);
    position: sticky;
    left: 0;
}

/* ======= Hover card ======= */

.hover-card {
    position: fixed;
    z-index: 10;
    max-width: 320px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-medium);
    box-shadow: var(--shadow-medium);
    padding: var(--space-3) var(--space-4);
    pointer-events: none;
    font-size: var(--font-size-small);
}

.hover-card h3 {
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-semibold);
    margin-bottom: var(--space-1);
}

.hover-card .dates {
    color: var(--color-text-light);
    margin-bottom: var(--space-2);
}

.hover-card .meta {
    color: var(--color-text-light);
    font-size: 0.75rem;
    margin-top: var(--space-2);
}

/* Death-toll visual inside the hover card */
.hover-card .death-viz {
    margin-top: var(--space-2);
    border-top: 1px solid var(--color-border);
    padding-top: var(--space-2);
}

.death-viz .waffle,
.death-viz .range-bar {
    display: block;
    width: 100%;
    height: auto;
}

.death-viz .viz-caption {
    color: var(--color-text-light);
    font-size: 0.7rem;
    margin: 2px 0 var(--space-2);
}

.death-viz .waffle-cell {
    fill: var(--color-viz-track);
}

.death-viz .waffle-fill {
    fill: var(--color-viz-accent);
}

.death-viz .range-track {
    fill: var(--color-viz-track);
}

.death-viz .range-band {
    fill: var(--color-viz-band);
}

.death-viz .range-mid {
    fill: var(--color-viz-accent);
}

/* Region mini-map inside the hover card */
.hover-card .card-map {
    margin-top: var(--space-2);
    border-top: 1px solid var(--color-border);
    padding-top: var(--space-2);
}

.region-map {
    display: block;
    width: 100%;
    height: auto;
    background: var(--color-map-water);
    border-radius: var(--radius-small);
}

.region-map .region-land {
    fill: var(--color-map-land);
    stroke: var(--color-map-land-stroke);
    stroke-width: 0.3;
}

.region-map .region-hot {
    fill: var(--color-map-hot);
    stroke: var(--color-map-hot-stroke);
    stroke-width: 0.8;
    stroke-linejoin: round;
}

/* ======= View range readout ======= */

.view-range {
    font-size: var(--font-size-small);
    color: var(--color-text-light);
    margin-right: var(--space-3);
}

/* ======= Cursor year guide ======= */

.cursor-guide {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 1px;
    background: var(--color-cursor-guide);
    pointer-events: none;
    z-index: 4;
}

.cursor-year {
    position: absolute;
    bottom: 2px;
    left: 4px;
    font-size: 0.7rem;
    color: var(--color-text);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-small);
    padding: 0 var(--space-1);
    white-space: nowrap;
}

/* ======= Panel bulk toggles ======= */

.panel h2 .bulk {
    float: right;
    display: inline-flex;
    gap: var(--space-1);
}

.panel h2 .bulk button {
    font: inherit;
    font-size: 0.65rem;
    text-transform: none;
    letter-spacing: normal;
    color: var(--color-text-light);
    background: none;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-small);
    padding: 0 var(--space-1);
    cursor: pointer;
}

.panel h2 .bulk button:hover { border-color: var(--color-primary); color: var(--color-text); }

/* ======= Density (small screens): renderer adds .no-labels when the
   visible events are too crowded for text ======= */

.trunk.no-labels .event-label { display: none; }

/* ======= Bottom-sheet handle (portrait phones only; hidden elsewhere) ======= */

.sheet-handle { display: none; }

/* ======= Pinned detail card (tap-to-open on touch screens) ======= */

.hover-card.pinned {
    left: 50%;
    right: auto;
    bottom: 52px;
    top: auto;
    transform: translateX(-50%);
    width: min(420px, calc(100vw - 2 * var(--space-2)));
    max-width: none;
    max-height: 50vh;
    overflow-y: auto;
    pointer-events: auto;
}

/* ======= Vertical orientation (renderer adds .vertical on portrait
   phones): time runs top-to-bottom, the bands become thin columns
   ordered axis | periods | stripes | trunk across the width ======= */

.timeline.vertical {
    flex-direction: row;
    touch-action: pan-x;
}

.timeline.vertical .axis {
    order: 0;
    flex: 0 0 42px;
    border-top: none;
    border-right: 1px solid var(--color-border);
}

.timeline.vertical .periods {
    order: 1;
    min-height: 0;
    padding: 0;
    flex: 0 0 auto;
}

.timeline.vertical .stripes {
    order: 2;
    min-height: 0;
    padding: 0;
    flex: 0 0 auto;
}

.timeline.vertical .trunk {
    order: 3;
    flex: 1;
    min-height: 0;
    min-width: 40px;
}

.timeline.vertical .trunk-line {
    left: 6px;
    right: auto;
    top: 0;
    bottom: 0;
    width: 2px;
    height: auto;
}

.timeline.vertical .tick {
    left: 0;
    transform: translateY(-50%);
    padding: 0 0 0 var(--space-1);
}

.timeline.vertical .tick::before {
    top: 50%;
    left: auto;
    right: -5px;
    width: 5px;
    height: 1px;
}

.timeline.vertical .event {
    height: auto;
    align-items: flex-start;
}

.timeline.vertical .event .bar { width: 10px; }

.timeline.vertical .event .event-label { font-size: 0.68rem; }

.timeline.vertical .stripe .stripe-label {
    writing-mode: vertical-rl;
    padding: var(--space-2) 0 0 0;
    left: auto;
    top: 0;
    font-size: 0.65rem;
}

.timeline.vertical .period .period-label {
    writing-mode: vertical-rl;
    padding: var(--space-2) 0 0 0;
    left: auto;
    top: 0;
}

.timeline.vertical .cursor-guide {
    width: auto;
    height: 1px;
    left: 0;
    right: 0;
    top: auto;
    bottom: auto;
}

.timeline.vertical .cursor-year {
    bottom: auto;
    top: 2px;
    left: 46px;
}

/* ======= Portrait phone layout: full-height stage, panel as a
   bottom sheet driven by js/sheet.js ======= */

@media (max-width: 900px) and (orientation: portrait) {
    html, body { height: 100%; }
    body { overflow: hidden; }

    .masthead {
        flex-wrap: wrap;
        gap: var(--space-1);
        padding: var(--space-2) var(--space-3);
    }

    /* The base layout top-aligns so the desktop stage takes its content
       height; the vertical timeline instead needs the full column height,
       so stretch and cap the chain here (nothing renders otherwise -- the
       trunk's clientHeight would be 0). */
    .layout {
        min-height: 0;
        align-items: stretch;
        overflow: hidden;
    }

    .stage {
        min-height: 0;
        padding: var(--space-2) var(--space-2) 48px var(--space-2);
    }

    .timeline { flex: 1; min-height: 0; }

    .stage-hint { display: none; }

    .panel {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        width: auto;
        max-height: 80vh;
        overflow-y: auto;
        z-index: 20;
        border-right: none;
        border-top: 1px solid var(--color-border);
        border-radius: var(--radius-medium) var(--radius-medium) 0 0;
        box-shadow: var(--shadow-medium);
        padding-top: 0;
        transform: translateY(calc(100% - 40px));
        transition: transform 0.25s ease;
    }

    .panel.sheet-half { transform: translateY(50%); }
    .panel.sheet-full { transform: translateY(0); }

    /* Desktop-only panel controls and states do not apply to the sheet. */
    .panel-toggle { display: none; }
    .panel.collapsed { width: auto; padding: 0 var(--space-4) var(--space-4) var(--space-4); }
    .panel.collapsed .panel-section { display: block; }

    .sheet-handle {
        display: flex;
        align-items: center;
        justify-content: center;
        position: sticky;
        top: 0;
        width: 100%;
        height: 40px;
        background: var(--color-surface);
        border: none;
        cursor: grab;
        touch-action: none;
        z-index: 1;
    }

    .sheet-grip {
        width: 36px;
        height: 4px;
        border-radius: 2px;
        background: var(--color-text-light);
        opacity: 0.6;
    }
}

.event.highlight { z-index: 5; }
.event.highlight .dot {
    transform: scale(1.35);
    box-shadow: 0 0 0 2px var(--color-text);
}
.event.highlight .bar {
    opacity: 1;
    box-shadow: 0 0 0 2px var(--color-text);
}
.stripe.highlight {
    opacity: 1;
    outline: 2px solid var(--color-text);
    outline-offset: 1px;
    z-index: 2;
}

/* ======= Touch override (must stay last: same specificity as the
   touch-action rules on .timeline and .timeline.vertical above, so
   source order decides) ======= */

/* On touch devices the browser must never claim a swipe on the timeline:
   Android Chrome enforces pan-x/pan-y strictly and cancels the pointer
   stream on diagonal drags, killing the JS drag mid-gesture. Page
   scrolling in landscape must start outside the timeline. */
@media (pointer: coarse) {
    .timeline, .timeline.vertical { touch-action: none; }
}
