/* DefenseClaw Spaces: base layer.
   Reset, type, links, focus, shell grid, utility bar, switcher, rail,
   progress line, chapter anatomy, reduced motion, print.
   Requires tokens.css. All transitions use the --dur tokens, which drop to
   0ms under prefers-reduced-motion (see tokens.css); the universal rule at
   the end is a backstop for anything that does not. */

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

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    scroll-padding-top: calc(var(--header-offset) + var(--sp-4));
}

body,
h1, h2, h3, h4, h5, h6,
p, figure, blockquote, dl, dd,
ul, ol {
    margin: 0;
}

ul,
ol {
    padding-left: 1.25em;
}

img,
svg,
canvas,
video {
    display: block;
    max-width: 100%;
}

svg {
    overflow: visible;
}

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

button {
    cursor: pointer;
}

[hidden] {
    display: none;
}

table {
    border-collapse: collapse;
}

::selection {
    background: var(--selection);
}

/* ---------- Page ---------- */
body {
    min-height: 100vh;
    background-color: var(--page);
    background-image: var(--page-pattern);
    background-size: 24px 24px;
    background-position: -11px -11px;
    color: var(--ink);
    font-family: var(--font-text);
    font-size: var(--fs-body);
    line-height: var(--lh-body);
    font-weight: 400;
    font-kerning: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ---------- Type ---------- */
h1,
h2 {
    font-family: var(--font-display);
    font-weight: 700;
    line-height: var(--lh-display);
    color: var(--ink);
    text-wrap: balance;
}

h1 {
    font-size: var(--fs-h1-mobile);
    font-variation-settings: "opsz" 96;
    letter-spacing: -0.02em;
    line-height: 1.05;
}

h2 {
    font-size: var(--fs-h2);
    letter-spacing: -0.01em;
    line-height: 1.15;
}

h3 {
    font-family: var(--font-text);
    font-size: var(--fs-h3);
    font-weight: 600;
    line-height: 1.3;
}

h4 {
    font-size: var(--fs-body);
    font-weight: 600;
    line-height: 1.4;
}

@media (min-width: 640px) {
    h1 {
        font-size: var(--fs-h1);
    }
}

p {
    text-wrap: pretty;
}

.lede {
    font-size: var(--fs-lede);
    line-height: 1.55;
    color: var(--ink-2);
    max-width: var(--measure);
}

small,
.small {
    font-size: var(--fs-small);
}

.caption {
    font-size: var(--fs-caption);
    line-height: 1.45;
    color: var(--muted);
}

strong,
b {
    font-weight: 600;
}

code,
kbd,
samp,
pre {
    font-family: var(--font-mono);
    font-size: 0.9em;
}

code {
    overflow-wrap: anywhere;
}

pre {
    overflow-x: auto;
    padding: var(--sp-3) var(--sp-4);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--r-control);
    line-height: 1.5;
}

hr {
    border: 0;
    border-top: 1px solid var(--line);
    margin: var(--sp-6) 0;
}

.num,
.tabular {
    font-variant-numeric: tabular-nums;
}

/* ---------- Links ---------- */
a {
    color: var(--link);
    text-decoration-line: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 0.18em;
    text-decoration-color: color-mix(in srgb, var(--link) 45%, transparent);
    transition: text-decoration-color var(--dur-fast) var(--ease);
}

a:hover {
    text-decoration-color: currentColor;
}

/* ---------- Focus ---------- */
:focus {
    outline: none;
}

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

/* Programmatic focus targets (chapters after a rail jump) need no ring */
[tabindex="-1"]:focus-visible {
    outline: none;
}

/* ---------- Accessibility helpers ---------- */
.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;
}

.skip-link {
    position: absolute;
    top: var(--sp-2);
    left: var(--sp-2);
    z-index: 100;
    padding: var(--sp-2) var(--sp-4);
    background: var(--surface);
    color: var(--ink);
    border: 1px solid var(--line);
    border-radius: var(--r-control);
    font-weight: 600;
    text-decoration: none;
    transform: translateY(-200%);
}

.skip-link:focus,
.skip-link:focus-visible {
    transform: none;
}

/* ---------- Generic controls used by the bar ---------- */
.btn,
.toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    min-height: 32px;
    padding: 0 var(--sp-3);
    border: 1px solid var(--line);
    border-radius: var(--r-control);
    background: var(--surface);
    color: var(--ink-2);
    font-size: var(--fs-small);
    font-weight: 500;
    line-height: 1.2;
    white-space: nowrap;
    transition: background-color var(--dur-fast) var(--ease),
        border-color var(--dur-fast) var(--ease),
        color var(--dur-fast) var(--ease);
}

.btn:hover,
.toggle:hover {
    background: var(--raised);
    color: var(--ink);
}

.toggle {
    border-radius: var(--r-pill);
}

/* the switch dot inside a text toggle */
.toggle::before {
    content: "";
    width: 8px;
    height: 8px;
    border-radius: 50%;
    border: 1.5px solid currentColor;
}

.toggle[aria-pressed="true"] {
    color: var(--ink);
    border-color: var(--confirm);
}

.toggle[aria-pressed="true"]::before {
    background: var(--confirm);
    border-color: var(--confirm);
}

.toggle--icon {
    width: 32px;
    padding: 0;
}

.toggle--icon::before {
    content: none;
}

/* Sun / moon drawn in CSS. Pressed = light theme on = sun. */
.theme-icon {
    position: relative;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    box-shadow: inset -4px -3px 0 0 currentColor;
    transform: rotate(-20deg);
}

.toggle[aria-pressed="true"] .theme-icon {
    width: 8px;
    height: 8px;
    background: currentColor;
    transform: none;
    box-shadow:
        0 -7px 0 -2.5px currentColor,
        0 7px 0 -2.5px currentColor,
        7px 0 0 -2.5px currentColor,
        -7px 0 0 -2.5px currentColor,
        5px 5px 0 -2.5px currentColor,
        -5px -5px 0 -2.5px currentColor,
        5px -5px 0 -2.5px currentColor,
        -5px 5px 0 -2.5px currentColor;
}

/* ---------- Utility bar ---------- */
.utility-bar {
    position: sticky;
    top: 0;
    z-index: 20;
    height: var(--bar-h);
    background: var(--bar-bg);
    backdrop-filter: blur(12px) saturate(1.2);
    border-bottom: 1px solid var(--line);
}

.utility-bar__inner {
    display: flex;
    align-items: center;
    gap: var(--sp-4);
    height: 100%;
    max-width: calc(var(--shell-max) + 2 * var(--shell-pad));
    margin: 0 auto;
    padding: 0 var(--shell-pad);
}

.utility-bar__title {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-family: var(--font-display);
    font-size: var(--fs-lede);
    font-weight: 700;
    letter-spacing: -0.01em;
    color: var(--ink);
    text-decoration: none;
}

.utility-bar__actions {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    margin-left: auto;
    flex: none;
}

/* ---------- Switcher (other DefenseClaw Spaces) ---------- */
.switcher {
    position: relative;
    flex: none;
}

.switcher__summary {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-2);
    min-height: 32px;
    padding: 0 var(--sp-3);
    border-radius: var(--r-pill);
    border: 1px solid var(--line);
    color: var(--ink-2);
    font-size: var(--fs-small);
    font-weight: 500;
    list-style: none;
    cursor: pointer;
    white-space: nowrap;
}

.switcher__summary::-webkit-details-marker {
    display: none;
}

.switcher__summary::after {
    content: "";
    width: 6px;
    height: 6px;
    margin-top: -3px;
    border-right: 1.5px solid currentColor;
    border-bottom: 1.5px solid currentColor;
    transform: rotate(45deg);
    transition: transform var(--dur-fast) var(--ease);
}

.switcher__summary:hover {
    background: var(--raised);
    color: var(--ink);
}

.switcher[open] .switcher__summary::after {
    margin-top: 3px;
    transform: rotate(-135deg);
}

.switcher__menu {
    position: absolute;
    top: calc(100% + var(--sp-2));
    left: 0;
    z-index: 25;
    min-width: 260px;
    margin: 0;
    padding: var(--sp-2);
    list-style: none;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--r-card);
    box-shadow: var(--shadow);
}

.switcher__link {
    display: block;
    padding: var(--sp-2) var(--sp-3);
    border-radius: var(--r-control);
    color: var(--ink);
    text-decoration: none;
    font-size: var(--fs-small);
    line-height: 1.4;
}

.switcher__link:hover {
    background: var(--raised);
}

.switcher__link[aria-current="page"] {
    color: var(--muted);
    cursor: default;
}

.switcher__hint {
    display: block;
    color: var(--muted);
    font-size: var(--fs-caption);
}

@media (max-width: 639.98px) {
    .utility-bar__inner {
        gap: var(--sp-2);
    }

    .switcher__prefix {
        display: none;
    }

    .switcher__menu {
        left: auto;
        right: 0;
        min-width: min(260px, calc(100vw - 32px));
    }
}

/* Phones: the page h1 carries the title; the bar keeps the switcher and toggles */
@media (max-width: 479.98px) {
    .utility-bar__title {
        display: none;
    }
}

/* ---------- Reading progress (place inside .utility-bar) ---------- */
.progress {
    position: absolute;
    left: 0;
    right: 0;
    bottom: -1px;
    height: 2px;
    background: var(--confirm);
    transform-origin: 0 50%;
    transform: scaleX(var(--progress, 0));
    pointer-events: none;
}

/* ---------- Shell ---------- */
.shell {
    display: grid;
    grid-template-columns: var(--rail-w) minmax(0, var(--content-max));
    column-gap: var(--rail-gap);
    align-items: start;
    max-width: calc(var(--shell-max) + 2 * var(--shell-pad));
    margin: 0 auto;
    padding: 0 var(--shell-pad) var(--sp-9);
}

.main {
    min-width: 0;
    grid-column: 2;
}

.page-head {
    padding: var(--sp-8) 0 var(--sp-6);
    max-width: 900px;
}

.page-head .lede {
    margin-top: var(--sp-4);
}

@media (max-width: 959.98px) {
    .shell {
        grid-template-columns: minmax(0, 1fr);
    }

    .main {
        grid-column: 1;
    }

    .page-head {
        padding-top: var(--sp-6);
    }
}

/* ---------- Rail (desktop: sticky numbered chapter list) ---------- */
.rail {
    position: sticky;
    top: calc(var(--bar-h) + var(--sp-6));
    max-height: calc(100vh - var(--bar-h) - var(--sp-7));
    overflow-y: auto;
    margin-top: var(--sp-8);
    padding: 0 0 var(--sp-4);
    font-size: var(--fs-small);
}

.rail__title {
    margin: 0 0 var(--sp-3) var(--sp-4);
    color: var(--muted);
    font-size: var(--fs-caption);
    font-weight: 500;
}

.rail__list {
    position: relative;
    margin: 0;
    padding: 0;
    list-style: none;
    border-left: 1px solid var(--line);
}

.rail__item {
    display: grid;
    grid-template-columns: 2ch 1fr;
    gap: var(--sp-2);
    padding: 6px var(--sp-3) 6px var(--sp-4);
    color: var(--muted);
    text-decoration: none;
    line-height: 1.35;
    transition: color var(--dur-fast) var(--ease);
}

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

.rail__item[aria-current="true"] {
    color: var(--ink);
}

.rail__num {
    font-variant-numeric: tabular-nums;
    color: var(--muted);
    text-align: right;
}

.rail__item[aria-current="true"] .rail__num {
    color: var(--confirm-text);
    font-weight: 600;
}

/* The sliding 2px indicator (nav.js creates it if absent). Without JS,
   or before the first measure, the current item draws its own bar. */
.rail__indicator {
    position: absolute;
    left: -1px;
    top: 0;
    width: 2px;
    height: var(--indicator-h, 0);
    background: var(--confirm);
    border-radius: 2px;
    transform: translateY(var(--indicator-y, 0));
    transition: transform var(--dur) var(--ease), height var(--dur) var(--ease);
    pointer-events: none;
}

/* ---------- Rail as a sticky pill bar (under 960px, or forced with .rail--bar) ---------- */
.rail--bar {
    position: sticky;
    top: var(--bar-h);
    z-index: 15;
    max-height: none;
    height: var(--rail-bar-h);
    margin: 0 calc(-1 * var(--shell-pad));
    padding: 0;
    overflow: hidden;
    background: var(--bar-bg);
    backdrop-filter: blur(12px) saturate(1.2);
    border-bottom: 1px solid var(--line);
}

.rail--bar .rail__title {
    display: none;
}

.rail--bar .rail__list {
    display: flex;
    align-items: center;
    gap: var(--sp-2);
    height: 100%;
    padding: 0 var(--shell-pad);
    overflow-x: auto;
    scrollbar-width: none;
    border-left: 0;
}

.rail--bar .rail__item {
    display: inline-flex;
    gap: 6px;
    padding: 4px var(--sp-3);
    border: 1px solid var(--line);
    border-radius: var(--r-pill);
    white-space: nowrap;
}

.rail--bar .rail__item[aria-current="true"] {
    background: var(--raised);
    border-color: var(--confirm);
}

.rail--bar .rail__indicator {
    display: none;
}

@media (max-width: 959.98px) {
    .rail {
        position: sticky;
        top: var(--bar-h);
        z-index: 15;
        max-height: none;
        height: var(--rail-bar-h);
        margin: 0 calc(-1 * var(--shell-pad));
        padding: 0;
        overflow: hidden;
        background: var(--bar-bg);
        backdrop-filter: blur(12px) saturate(1.2);
        border-bottom: 1px solid var(--line);
    }

    .rail__title {
        display: none;
    }

    .rail__list {
        display: flex;
        align-items: center;
        gap: var(--sp-2);
        height: 100%;
        padding: 0 var(--shell-pad);
        overflow-x: auto;
        scrollbar-width: none;
        border-left: 0;
    }

    .rail__item {
        display: inline-flex;
        gap: 6px;
        padding: 4px var(--sp-3);
        border: 1px solid var(--line);
        border-radius: var(--r-pill);
        white-space: nowrap;
    }

    .rail__item[aria-current="true"] {
        background: var(--raised);
        border-color: var(--confirm);
    }

    .rail__indicator {
        display: none;
    }
}

/* ---------- Chapter anatomy ---------- */
.chapter {
    position: relative;
    padding: var(--sp-8) 0;
    border-top: 1px solid var(--line);
}

.chapter__head {
    max-width: var(--measure);
    margin-bottom: var(--sp-5);
}

.chapter__num {
    display: block;
    margin-bottom: var(--sp-2);
    color: var(--muted);
    font-family: var(--font-display);
    font-size: var(--fs-small);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.chapter__title {
    font-family: var(--font-display);
    font-size: var(--fs-h2);
    font-weight: 700;
}

.takeaway {
    max-width: 30em;
    margin: var(--sp-3) 0 0;
    font-family: var(--font-display);
    font-size: clamp(22px, 1.25rem + 0.6vw, 28px);
    font-weight: 500;
    line-height: 1.2;
    letter-spacing: -0.01em;
    color: var(--ink);
    text-wrap: balance;
}

/* Prose measure: chapter text stays at 72ch, figures take the column (to 1120px) */
.prose,
.chapter > p,
.chapter > ul,
.chapter > ol,
.chapter > blockquote,
.chapter > details,
.chapter > .callout {
    max-width: var(--measure);
}

.chapter > * + * {
    margin-top: var(--sp-5);
}

.chapter > .chapter__head + * {
    margin-top: 0;
}

.prose > * + * {
    margin-top: var(--sp-4);
}

.chapter > figure,
.chapter > .figure,
.bleed {
    width: 100%;
    max-width: var(--content-max);
}

/* "How this was measured" and similar disclosures */
.measured {
    border-top: 1px solid var(--line);
    padding-top: var(--sp-3);
    color: var(--ink-2);
    font-size: var(--fs-small);
}

.measured > summary {
    cursor: pointer;
    color: var(--ink);
    font-weight: 500;
}

.measured[open] > summary {
    margin-bottom: var(--sp-3);
}

/* ---------- Reduced motion backstop ---------- */
@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms;
        animation-iteration-count: 1;
        transition-duration: 0ms;
    }
}

/* ---------- Print ---------- */
@media print {
    :root {
        --page: #FFFFFF;
        --page-pattern: none;
        --surface: #FFFFFF;
        --raised: #F2F4F6;
        --ink: #000000;
        --ink-2: #222222;
        --muted: #555555;
        --line: rgba(0, 0, 0, .2);
        --link: #000000;
        --shadow: none;
        color-scheme: light;
    }

    body {
        background: #FFFFFF;
        font-size: 11pt;
    }

    .utility-bar,
    .rail,
    .progress,
    .skip-link,
    .figure__controls,
    .figure__actions,
    .glossary-tip {
        display: none;
    }

    .shell {
        display: block;
        max-width: none;
        padding: 0;
    }

    .chapter {
        padding: 24pt 0;
        break-before: auto;
    }

    .figure,
    .tile,
    .verdict,
    .callout,
    tr {
        break-inside: avoid;
    }

    h1,
    h2,
    h3,
    .takeaway {
        break-after: avoid;
    }

    a[href^="http"]::after {
        content: " (" attr(href) ")";
        font-size: 9pt;
        color: #555555;
    }
}
