/* 10 Years of Estonian Theatre — a printed theatre programme on light grey paper: Epilogue headings,
   Baskervville body text, one stage-red accent for the chrome. Chart colours are kept apart from the
   accent (see the chart section).

   The page is read through one of two lenses, set as data-lens on <html> by script.js: visitors
   (a blue ramp) or revenue (a green ramp). The ramp colours are registered properties, so
   switching lens fades every bar and highlight, and the page ground, from one lens to the other. */

@property --rank-1 { syntax: "<color>"; inherits: true; initial-value: #0a3a80; }
@property --rank-2 { syntax: "<color>"; inherits: true; initial-value: #2f7fdc; }
@property --rank-3 { syntax: "<color>"; inherits: true; initial-value: #8cbcf2; }
@property --lens-tint { syntax: "<color>"; inherits: true; initial-value: #dbe8f9; }
@property --page { syntax: "<color>"; inherits: true; initial-value: #ececec; }

:root {
    color-scheme: light;
    /* page ground: a cool neutral grey under the visitors lens, a green-grey under revenue */
    --page: #ececec;
    --surface-1: #ffffff;
    --ink: #191919;
    --ink-2: #5c5c5c;
    --rule: #dcdcdc;
    --rule-strong: #a8a8a8;
    --band: #f2f2f2;
    --accent: #9e1b24;
    /* audience age, youngest → oldest: warm-to-cool hue sweep. Every pair clears the
       validator's colour-blind (ΔE ≥ 8) and normal-vision (ΔE ≥ 15) separation. */
    --audience-1: #f2a600;
    --audience-2: #ec4647;
    --audience-3: #b23a9a;
    --audience-4: #5b3fd1;
    --audience-5: #0e3d7e;
    /* Lens ramp, darkest = the year's top production. Visitors lens: blue; adjacent steps clear
       ΔE 13.7 (colour-blind) and 16.1 (normal vision). "Others" stays a recessive neutral grey. */
    --rank-1: #0a3a80;
    --rank-2: #2f7fdc;
    --rank-3: #8cbcf2;
    --lens-tint: #dbe8f9;
    --others: #e2e2e2;
    /* each lens's own darkest step, used where a lens is named whichever lens is chosen (picker hover) */
    --visitors-ink: #0a3a80;
    --revenue-ink: #0a5228;
    --visitors-tint: #dbe8f9;
    --revenue-tint: #cdecd5;
    transition: --rank-1 600ms ease, --rank-2 600ms ease, --rank-3 600ms ease, --lens-tint 600ms ease,
        --page 600ms ease;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 8px 24px rgb(0 0 0 / 0.08);

    /* Font pairing (fontpair.co): Epilogue for headings, Baskervville for body text. Small chart and
       control text uses Epilogue too, where a text serif gets hard to read at 12px. */
    --font-display: "Epilogue", sans-serif;
    --font-body: "Baskervville", serif;
    --font-ui: "Epilogue", sans-serif;
}

/* Revenue lens: a green ramp; adjacent steps clear ΔE 15.8 (colour-blind) and 18.2 (normal vision). */
:root[data-lens="revenue"] {
    --rank-1: #0a5228;
    --rank-2: #1f8c45;
    --rank-3: #6cc381;
    --lens-tint: #cdecd5;
    --page: #e4ebe5;
}

@media (prefers-reduced-motion: reduce) {
    :root {
        transition: none;
    }
}

/* ---------- Base ---------- */

*,
*::before,
*::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: var(--font-body);
    font-weight: 400;
    font-size: 1.0625rem;
    /* Baskervville defaults to old-style numerals; lining ones read better in a story full of figures. */
    font-variant-numeric: lining-nums;
    line-height: 1.55;
    color: var(--ink);
    background: var(--page);
}

/* Form controls and code would otherwise take the browser's own fonts. */
button,
input,
select,
textarea,
output {
    font: inherit;
}

code {
    font-family: var(--font-body);
    font-size: 1em;
}

/* Chart panels, controls, the lens picker and captions: small text, set in the sans. */
.chart,
.chart-status,
.tooltip,
.lens-picker,
.hero figcaption {
    font-family: var(--font-ui);
}

h1,
h2,
h3 {
    font-family: var(--font-display);
    font-weight: 700;
    line-height: 1.15;
    text-wrap: balance;
}

a {
    color: var(--accent);
    text-underline-offset: 0.15em;
}

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

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

/* ---------- Page columns: the masthead, the about section and the colophon share one text column ---------- */

.masthead,
.about,
.colophon {
    max-width: 44rem;
    margin: 0 auto;
    padding: 0 1.25rem;
}

/* ---------- Masthead ---------- */

.masthead {
    padding-top: clamp(0.75rem, 3svh, 2rem);
    text-align: center;
}

/* Hero photo: wider than the text column, centred on the page, with its credit underneath.
   Its width is also capped by the screen height (the photo is 1.4:1) so the title stays in view;
   the photo is never cropped, which keeps the photographer's mark in its corner. */
.hero {
    position: relative;
    left: 50%;
    width: min(64rem, 100vw - 1.5rem, 64svh * 1.4);
    margin: 0 0 clamp(2rem, 6svh, 4rem);
    translate: -50% 0;
}

.hero img {
    display: block;
    width: 100%;
    height: auto;
    box-shadow: var(--shadow);
}

.hero figcaption {
    margin-top: 0.5rem;
    color: var(--ink-2);
    font-size: 0.8125rem;
    text-align: left;
}

.masthead h1 {
    margin: 0;
    font-size: clamp(2.25rem, 8vw, 4.25rem);
    line-height: 1.1;
    letter-spacing: -0.02em;
}

.masthead__lede {
    margin: 1.25rem auto 0;
    max-width: 32rem;
    color: var(--ink-2);
    font-size: clamp(1.125rem, 2.6vw, 1.375rem);
}

/* ---------- Pick a lens: a small title, then the picker, which sticks to the top of the screen
   once scrolled up to it and stays there to the end of the story ---------- */

/* A small title above the picker; the picker is its sibling, not inside it, so it can stick past it. */
.lens-title {
    margin: clamp(2.5rem, 8svh, 4.5rem) 0 0;
    text-align: center;
    color: var(--ink-2);
    font-size: 0.9375rem;
    font-weight: 600;
}

/* Sticky: it scrolls with the page until it reaches the top of the screen, then stays there. It is a
   child of .lens-scope, so it lets go when the story ends. */
.lens-picker {
    --picker-height: 2.5rem;
    position: sticky;
    z-index: 20;
    top: calc(0.875rem + env(safe-area-inset-top));
    width: fit-content;
    min-inline-size: 0;
    margin: 0.5rem auto clamp(3rem, 9svh, 5.5rem);
    padding: 0;
    border: 0;
    font-size: 0.9375rem;
}

/* Material Design Icons by Pictogrammers (Pictogrammers Free License), copied unchanged from
   github.com/Templarian/MaterialDesign/tree/master/svg into icons/. Each file's root <svg> carries an
   id, so <use href="icons/NAME.svg#mdi-NAME"> draws it in the text colour. */
.icon {
    flex: none;
    width: 1.25em;
    height: 1.25em;
    fill: currentColor;
}

/* The track holds two equal halves; the thumb (the lens colour) sits under the chosen half. script.js
   sets --thumb: 0 for visitors, 1 for revenue, and anything in between while the thumb is dragged. */
.lens-picker__track {
    --thumb: 0;
    position: relative;
    height: var(--picker-height);
    /* a grey groove joins the two options into one control */
    background: #f3f3f3;
    box-shadow: var(--shadow);
    display: grid;
    grid-template-columns: 1fr 1fr;
    touch-action: pan-y; /* horizontal drags move the thumb; vertical ones still scroll the page */
    user-select: none;
    -webkit-user-select: none;
}

:root[data-lens="revenue"] .lens-picker__track {
    --thumb: 1;
}

.lens-picker__thumb {
    position: absolute;
    z-index: 1; /* over the labels; clicks pass through to them */
    inset: 0 auto 0 0;
    width: 50%;
    overflow: hidden;
    background: var(--rank-1);
    pointer-events: none;
    translate: calc(var(--thumb) * 100%) 0;
}

/* The white labels move the opposite way to the thumb, so they stay lined up with the real ones. */
.lens-picker__thumb-text {
    position: absolute;
    inset: 0 auto 0 0;
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 200%;
    color: var(--surface-1);
    translate: calc(var(--thumb) * -50%) 0;
}

/* a small overshoot, so the thumb lands with a little give */
.lens-picker__thumb,
.lens-picker__thumb-text {
    transition: translate 420ms cubic-bezier(0.34, 1.3, 0.5, 1), opacity 300ms ease;
}

.lens-picker__track.is-dragging {
    cursor: grabbing;
}

.lens-picker__track.is-dragging :is(.lens-picker__thumb, .lens-picker__thumb-text) {
    transition: none;
}

.lens-picker label {
    position: relative;
    display: grid;
    cursor: pointer;
}

.lens-picker input {
    position: absolute;
    inset: 0;
    margin: 0;
    opacity: 0;
    cursor: inherit;
}

.lens-picker__option {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.375rem;
    padding: 0 1.125rem;
    font-weight: 600;
    white-space: nowrap;
}

.lens-picker input + .lens-picker__option {
    color: var(--ink);
    transition: color 200ms ease, background-color 200ms ease;
}

/* Hovering the other option previews its own lens: its colour on a light tint of it. */
.lens-picker input[value="visitors"]:hover:not(:checked) + .lens-picker__option {
    background: var(--visitors-tint);
    color: var(--visitors-ink);
}

.lens-picker input[value="revenue"]:hover:not(:checked) + .lens-picker__option {
    background: var(--revenue-tint);
    color: var(--revenue-ink);
}

.lens-picker input:focus-visible + span {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    .lens-picker__thumb,
    .lens-picker__thumb-text {
        transition: none;
    }
}

/* ---------- Lens switch: text for one lens only, and the crossfade between lenses ---------- */

/* Text written for one lens only. After the reader's first switch, newly shown text fades in. */
:root[data-lens="visitors"] [data-lens-only="revenue"],
:root[data-lens="revenue"] [data-lens-only="visitors"] {
    display: none;
}

@keyframes lens-in {
    from { opacity: 0; }
}

@keyframes lens-in-block {
    from { opacity: 0; translate: 0 0.375rem; }
}

.lens-switched [data-lens-only] {
    animation: lens-in 450ms ease-out;
}

.lens-switched p[data-lens-only] {
    animation-name: lens-in-block;
}

@media (prefers-reduced-motion: reduce) {
    .lens-switched [data-lens-only] {
        animation: none;
    }
}

/* Lens switch crossfade (View Transitions API, started in script.js). Each story text block and chart
   caption is captured on its own and crossfades between lenses; the rest of the page, the charts
   included, is not captured, so the D3 transitions play live underneath. */
::view-transition-old(root) {
    display: none;
}

::view-transition-new(root) {
    animation: none;
}

/* Old text fades out, then the new text fades in, so the two never sit on top of each other. */
@keyframes lens-text-out {
    to { opacity: 0; }
}

@keyframes lens-text-in {
    from { opacity: 0; }
}

::view-transition-group(*) {
    animation-duration: 450ms;
    animation-timing-function: ease;
}

::view-transition-old(*) {
    animation: lens-text-out 180ms ease-in both;
}

::view-transition-new(*) {
    animation: lens-text-in 270ms ease-out 180ms both;
}

/* ---------- Story: one CSS grid row per chart. Text sits above its chart on phones and in the
   top-left column beside it on wider screens. ---------- */

.story {
    display: grid;
    gap: clamp(3rem, 9svh, 5.5rem);
    max-width: 76rem;
    margin: 0 auto;
    padding: 0 1.25rem;
}

.story__row {
    scroll-margin-top: 4.5rem; /* clear of the sticky lens picker */
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 1.25rem;
    align-items: start;
}

/* Each step opens with the same thin rule as the about section; figures that change with the lens sit on a
   light patch of the lens tint. */
.story__text {
    padding-top: 0.875rem;
    border-top: 1px solid var(--rule-strong);
}

.story__text strong {
    padding: 0 0.15em;
    background: var(--lens-tint);
    box-decoration-break: clone;
    -webkit-box-decoration-break: clone;
}

/* Figures that stay the same in both lenses (counts of productions) are bold but unmarked. */
.story__text strong.stat--fixed {
    padding: 0;
    background: none;
}

.story__text h2 {
    margin: 0 0 0.625rem;
    font-size: 1.375rem;
}

.story__text p {
    margin: 0 0 0.625rem;
}

.story__text p:last-child {
    margin-bottom: 0;
}

.chart-status {
    margin: 0;
    color: var(--ink-2);
    text-align: center;
}

.chart-status[hidden] {
    display: none;
}

.chart {
    display: flex;
    flex-direction: column;
    min-width: 0;
    margin: 0;
    padding: 0.875rem 0.875rem 0.75rem;
    background: var(--surface-1);
    border: 1px solid var(--rule);
}

@media (min-width: 52rem) {
    .story {
        padding: 0 1.5rem;
    }

    /* The text column scales with the row (30%) between 13rem and 22rem, so on tablets the chart
       keeps most of the width instead of the text holding its full 22rem. */
    .story__row {
        grid-template-columns: clamp(13rem, 30%, 22rem) minmax(0, 1fr);
        gap: clamp(1.5rem, 3vw, 2.5rem);
    }

    .chart {
        padding: 1.5rem 1.5rem 1rem;
    }
}

.chart__title {
    margin: 0;
    font-size: clamp(1rem, 2.4vw, 1.3rem);
}

.chart__sub {
    margin: 0.125rem 0 0;
    color: var(--ink-2);
    font-size: 0.8125rem;
}

.legend {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 1rem;
    margin: 0.5rem 0 0;
    padding: 0;
    list-style: none;
    font-size: 0.8125rem;
}

.legend li {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.legend__swatch {
    width: 0.625rem;
    height: 0.625rem;
    border-radius: 50%;
    background: var(--swatch);
}

.legend__swatch--bar {
    border-radius: 0;
}


/* Controls under a chart: they wrap onto separate lines on narrow screens. */

.chart__controls {
    display: flex;
    flex: none;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem 1.5rem;
    margin-top: 0.625rem;
    font-size: 0.8125rem;
}

.year-slider {
    display: flex;
    flex: 1 1 16rem;
    align-items: center;
    gap: 0.625rem;
}

.year-slider__label {
    color: var(--ink-2);
}

.year-slider input {
    flex: 1 1 auto;
    min-width: 6rem;
    margin: 0;
    accent-color: var(--rank-2);
    cursor: pointer;
}

.year-slider input:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 4px;
}

/* Fixed width, so the slider doesn't shift as the year changes. */
.year-slider__output {
    min-width: 2.5rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

/* Each chart gets its own fixed-height plot, sized to the screen; the SVG fills it absolutely,
   so a redraw can never push the panel taller. */
.chart__plot {
    position: relative;
    /* Pressing and holding a chart picks a mark (charts.js); it shouldn't select label text, open the
       iOS callout or flash a tap highlight. */
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
    height: clamp(18rem, 58svh, 32rem);
    margin-top: 0.5rem;
}

.chart__plot svg {
    position: absolute;
    inset: 0;
    display: block;
    overflow: visible;
    font-family: var(--font-ui);
}

/* ---------- Chart marks (classes set in charts.js) ---------- */

.axis {
    color: var(--ink-2);
    font-size: 12px;
    font-family: var(--font-ui);
    font-variant-numeric: tabular-nums;
}

.axis-label {
    fill: var(--ink-2);
    font-size: 12px;
    font-variant-numeric: tabular-nums;
}

.grid line {
    stroke: var(--rule);
    stroke-width: 1;
}

.grid line.grid__baseline {
    stroke: var(--rule-strong);
}

.band rect {
    fill: var(--band);
}

.band text {
    fill: var(--ink-2);
    font-size: 11px;
}

/* Visitors or revenue per year: one column per year in the lens's darkest step, the same colour as
   the picker's thumb. Hovering a column keeps it in colour while the other years step back. */
.trend-column {
    fill: var(--rank-1);
    transition: opacity 150ms ease;
}

.trend-columns.has-hover .trend-column:not(.is-hovered) {
    opacity: 0.35;
}

.axis-title {
    fill: var(--ink);
    font-size: 13px;
    font-weight: 600;
}

.dot.audience-1 { fill: var(--audience-1); }
.dot.audience-2 { fill: var(--audience-2); }
.dot.audience-3 { fill: var(--audience-3); }
.dot.audience-4 { fill: var(--audience-4); }
.dot.audience-5 { fill: var(--audience-5); }

.bar--rank-1 { fill: var(--rank-1); }
.bar--rank-2 { fill: var(--rank-2); }
.bar--rank-3 { fill: var(--rank-3); }
.bar--others { fill: var(--others); }
.bar--audience-1 { fill: var(--audience-1); }
.bar--audience-2 { fill: var(--audience-2); }
.bar--audience-3 { fill: var(--audience-3); }
.bar--audience-4 { fill: var(--audience-4); }
.bar--audience-5 { fill: var(--audience-5); }

.dot {
    stroke: var(--surface-1);
    stroke-width: 0.6;
}

.dot-highlight {
    fill: none;
    stroke: var(--ink);
    stroke-width: 1.5;
    pointer-events: none;
}

.annotation {
    fill: var(--ink);
    font-size: 12px;
    font-weight: 600;
    paint-order: stroke;
    stroke: var(--surface-1);
    stroke-width: 3px;
    stroke-linejoin: round;
    pointer-events: none;
}

.bar-label {
    fill: var(--ink);
    font-weight: 500;
    pointer-events: none;
}

.hit-area {
    fill: transparent;
}

/* Stacked bars: the hovered segment is named above its bar, the rest of that bar steps back. */
.bar-row__label {
    opacity: 0;
    paint-order: stroke;
    stroke: var(--surface-1);
    stroke-width: 3px;
    stroke-linejoin: round;
    transition: opacity 150ms ease;
}

.bar-row__label.is-visible {
    opacity: 1;
}

.bar-row .bar {
    transition: opacity 150ms ease;
}

.bar-row.has-focus .bar:not(.is-focused) {
    opacity: 0.35;
}

/* Bars with a tooltip (top production per year): the hovered row keeps its colour and name, the
   other rows step back. */
.bar-row__name {
    transition: opacity 150ms ease;
}

.has-hover .bar-row:not(.is-hovered) :is(.bar, .bar-row__name) {
    opacity: 0.35;
}

/* ---------- Tooltip ---------- */

.tooltip {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 10;
    max-width: 18rem;
    padding: 0.5rem 0.75rem;
    background: var(--surface-1);
    color: var(--ink);
    border: 1px solid var(--rule-strong);
    box-shadow: var(--shadow);
    font-size: 0.8125rem;
    pointer-events: none;
}

.tooltip[hidden] {
    display: none;
}

.tooltip__title {
    margin: 0 0 0.25rem;
    font-weight: 600;
}

.tooltip dl {
    display: grid;
    grid-template-columns: auto auto;
    gap: 0.125rem 0.75rem;
    margin: 0;
}

/* Labels stay on one line; a long value (a genre, say) wraps instead. */
.tooltip dt {
    color: var(--ink-2);
    white-space: nowrap;
}

.tooltip dd {
    margin: 0;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* ---------- About the data: closes the story, above the colophon ---------- */

.about {
    padding-top: clamp(2rem, 6svh, 3.5rem);
    font-size: 1.1875rem;
}

.about p {
    margin: 0;
    padding-top: 1.5rem;
    border-top: 1px solid var(--rule-strong);
}

/* ---------- Colophon ---------- */

.colophon {
    padding-block: 2rem 4rem;
    color: var(--ink-2);
    font-size: 0.875rem;
}
