/* ============================================================
   Foundation — the layout contract every surface builds on.

   Loaded by both shells (core/base.html, studio/app_base.html) after the
   page sheets, so a pattern here wins a tie against an older page rule.
   Colours, radii and motion still come from the tokens in style.css; this
   file adds the one thing those never had — a spacing scale — and the
   small set of shared patterns built on it. See docs/DESIGN_PATTERNS.md.

   Rules of the road
     1. Spacing comes from --space-*. No new raw px gaps or paddings.
     2. Rows of a list or table share ONE grid: fixed column tracks,
        text left, numbers right, everything centred on the row's axis.
     3. One destination, one link. Group related links under a heading;
        nest them rather than repeat them.
     4. Detail that is not needed to scan is collapsed, never deleted.
   ============================================================ */

:root {
    /* 4px base. Use the step, not the number. */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;
    --space-7: 48px;
    --space-8: 64px;

    --row-h: 48px;            /* one scannable row: tables, menus, rails */
    --row-h-compact: 40px;
    --indent: 24px;           /* one level of nesting, everywhere */
    --gutter: var(--space-5); /* page side padding */
    --measure: 68ch;          /* longest comfortable line of prose */
}

/* ----- Layout primitives ---------------------------------------------- */

/* Vertical rhythm: children spaced by one step, no margins to collapse. */
.l-stack { display: flex; flex-direction: column; gap: var(--l-gap, var(--space-4)); }
.l-stack--tight { --l-gap: var(--space-2); }
.l-stack--loose { --l-gap: var(--space-6); }

/* A row of things that wraps and stays centred on one axis. */
.l-cluster { display: flex; flex-wrap: wrap; align-items: center; gap: var(--l-gap, var(--space-3)); }

/* Title on the left, actions on the right, baseline shared. */
.l-split { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: var(--space-4) var(--space-5); }

/* Equal columns that reflow by themselves. */
.l-grid { display: grid; gap: var(--l-gap, var(--space-4)); grid-template-columns: repeat(auto-fit, minmax(var(--l-min, 220px), 1fr)); }

.l-page { padding-block: var(--space-6) var(--space-8); }

/* ----- Page head -------------------------------------------------------- */

.page-head { padding-block: var(--space-6) var(--space-5); }
.page-head-kicker {
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-muted);
    margin-bottom: var(--space-2);
}
.page-head h1 {
    font-size: clamp(1.6rem, 1.2rem + 1.4vw, 2.1rem);
    font-weight: 800;
    letter-spacing: -0.03em;
    line-height: 1.15;
}
.page-head-sub {
    margin-top: var(--space-2);
    max-width: var(--measure);
    color: var(--text-secondary);
    font-size: 0.95rem;
}

/* Figures strip: equal cells, hairline dividers, numbers on one baseline. */
.stat-strip {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(104px, 1fr);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    background: var(--bg-card);
    overflow: hidden;
}
.stat-strip-cell { padding: var(--space-3) var(--space-4); }
.stat-strip-cell + .stat-strip-cell { border-left: 1px solid var(--border-subtle); }
.stat-strip-value {
    font-family: var(--font-display);
    font-size: 1.35rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    line-height: 1.2;
    font-variant-numeric: tabular-nums;
}
.stat-strip-label {
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-muted);
}

/* ----- Section head ----------------------------------------------------- */

.section-head { margin-bottom: var(--space-4); }
.section-head h2 { font-size: 1.15rem; font-weight: 700; letter-spacing: -0.02em; }
.section-head p { margin-top: var(--space-1); color: var(--text-secondary); font-size: 0.85rem; }

.text-btn {
    background: none;
    border: 0;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    transition: background var(--transition), color var(--transition);
}
.text-btn:hover { background: var(--ink-06); color: var(--text-primary); }

/* ----- Navigation menus (hover + click + keyboard) -----------------------
   A group is a real link (its first destination) plus a caret button. The
   menu opens on hover and on focus for pointer and keyboard users, and the
   caret toggles .is-open for touch and for the drawer, where hover does not
   exist. main.js initNavMenus() owns .is-open and aria-expanded. */

.nav-group { position: relative; display: flex; align-items: center; }
.navbar-links .nav-group > a { padding-right: var(--space-2); }
.nav-caret {
    display: inline-grid;
    place-items: center;
    width: 24px;
    height: 32px;
    margin-left: -6px;
    padding: 0;
    border: 0;
    border-radius: var(--radius-sm);
    background: none;
    color: var(--ink-70);
    transition: color var(--transition), background var(--transition);
}
.nav-caret svg { transition: transform var(--dur-med, 240ms) var(--ease-out, ease); }
.nav-caret:hover { color: var(--text-primary); background: var(--ink-06); }
.nav-group.is-open > .nav-caret svg { transform: rotate(180deg); }

.nav-menu {
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 200;
    /* The padding is the hover bridge: the pointer never crosses dead space
       between the trigger and the panel. */
    padding-top: var(--space-2);
    min-width: 248px;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-4px);
    transition: opacity 140ms ease, transform 140ms ease, visibility 0s linear 140ms;
}
.nav-menu-panel {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    padding: var(--space-2);
    background: var(--bg-menu);
    border: 1px solid var(--ink-10);
    border-radius: var(--radius-lg);
    box-shadow: 0 1px 0 var(--gloss) inset, 0 24px 48px var(--shade-50);
}
.nav-menu-heading {
    margin-top: var(--space-1);
    padding: var(--space-2) var(--space-3) var(--space-1);
    border-top: 1px solid var(--ink-08);
    font-size: 0.68rem;
    font-weight: 600;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--text-muted);
}
.nav-menu ul { list-style: none; display: flex; flex-direction: column; gap: 1px; }

/* Beats `.navbar-links a` (pill + underline) without !important. */
.navbar-links .nav-menu a {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
    width: 100%;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    font-size: 0.84rem;
    font-weight: 600;
    color: var(--text-primary);
    white-space: normal;
    box-shadow: none;
}
.navbar-links .nav-menu a::after { display: none; }
.navbar-links .nav-menu a:hover,
.navbar-links .nav-menu a:focus-visible { background: var(--accent-purple-dim); box-shadow: none; }
.navbar-links .nav-menu a.active { background: var(--ink-06); box-shadow: none; }
.nav-menu-label { display: inline-flex; align-items: center; gap: var(--space-2); }
.nav-menu-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dot, var(--accent-purple)); flex-shrink: 0; }
.nav-menu-blurb { font-size: 0.75rem; font-weight: 400; line-height: 1.35; color: var(--text-muted); }

@media (min-width: 1101px) {
    .nav-group:hover > .nav-menu,
    .nav-group:focus-within > .nav-menu,
    .nav-group.is-open > .nav-menu {
        opacity: 1;
        visibility: visible;
        transform: none;
        transition-delay: 0s;
    }
    /* Escape / a click on a link drops the menu even while still hovered. */
    .nav-group.is-dismissed > .nav-menu { opacity: 0; visibility: hidden; }
}

/* Drawer: the same tree, inline. Collapsed until its caret is pressed. */
@media (max-width: 1100px) {
    .nav-group { flex-wrap: wrap; }
    .navbar-links.open .nav-group > a { flex: 1; width: auto; }
    .nav-caret { width: 44px; height: 44px; margin-left: 0; }
    .nav-menu {
        position: static;
        flex-basis: 100%;
        display: none;
        min-width: 0;
        padding: 0 0 var(--space-2) var(--indent);
        opacity: 1;
        visibility: visible;
        transform: none;
    }
    .nav-group.is-open > .nav-menu { display: block; }
    .nav-menu-panel { padding: 0; background: none; border: 0; box-shadow: none; border-left: 1px solid var(--border-subtle); padding-left: var(--space-2); }
    .navbar-links.open .nav-menu a { padding: 10px var(--space-3); font-size: 0.92rem; }
    .nav-menu-blurb { display: none; }
}

/* ----- Tree table --------------------------------------------------------
   A hierarchy shown as a table: one name column that indents by level, then
   fixed-width numeric columns that line up down the whole table whatever the
   depth. Rows are --row-h tall; descriptions are clamped to one line and the
   full text sits in the title attribute. main.js initTreeTables() toggles
   .is-hidden on descendants; without JS every row is simply visible. */

.tree-card {
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    background: var(--bg-card);
    overflow: hidden;
}
.tree-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--border-color);
}
.tree-toolbar .search-input { flex: 1 1 220px; max-width: 340px; }
.tree-toolbar-spacer { flex: 1; }

.tree-table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.tree-table col.tree-col-num { width: 132px; }
.tree-table th {
    height: var(--row-h-compact);
    padding: 0 var(--space-4);
    text-align: left;
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-muted);
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-color);
}
.tree-table td {
    height: var(--row-h);
    padding: 0 var(--space-4);
    font-size: 0.875rem;
    border-bottom: 1px solid var(--border-subtle);
    vertical-align: middle;
}
.tree-table tbody tr:last-child td { border-bottom: 0; }
.tree-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.tree-table td.num { color: var(--text-secondary); }
.tree-table tbody tr { transition: background var(--transition); }
.tree-table tbody tr:hover { background: var(--ink-03); }
html.js .tree-table tr.is-hidden { display: none; }

.tree-cell {
    display: grid;
    /* toggle | name | description — the description takes what is left. */
    grid-template-columns: 28px minmax(0, max-content) minmax(0, 1fr);
    align-items: center;
    column-gap: var(--space-2);
    padding-left: calc(var(--indent) * var(--depth, 0));
    min-width: 0;
}
.tree-toggle {
    display: inline-grid;
    place-items: center;
    width: 28px;
    height: 28px;
    padding: 0;
    border: 0;
    border-radius: var(--radius-sm);
    background: none;
    color: var(--text-muted);
    transition: background var(--transition), color var(--transition);
}
.tree-toggle:hover { background: var(--ink-08); color: var(--text-primary); }
.tree-toggle svg { transition: transform var(--dur-med, 240ms) var(--ease-out, ease); }
.tree-toggle[aria-expanded="true"] svg { transform: rotate(90deg); }
.tree-leaf { width: 28px; height: 28px; display: inline-grid; place-items: center; }
.tree-leaf::before { content: ''; width: 5px; height: 5px; border-radius: 50%; background: var(--ink-20); }

.tree-name {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;
    color: var(--text-primary);
    font-weight: 500;
}
.tree-name > span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
a.tree-name:hover > span:last-child,
.tree-table td.num a:hover { color: var(--accent-purple-text); text-decoration: underline; text-underline-offset: 3px; }
.tree-table td.num a { color: inherit; }
.tree-swatch { width: 10px; height: 10px; border-radius: 3px; background: var(--sector-color, var(--accent-purple)); flex-shrink: 0; }
.tree-desc {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-muted);
    font-size: 0.8rem;
}

.tree-row--l1 td { background: var(--ink-02); }
.tree-row--l1 .tree-name { font-weight: 700; font-size: 0.92rem; }
.tree-row--l2 .tree-name { font-weight: 600; }
.tree-row--l3 td { height: var(--row-h-compact); }
.tree-row--l3 .tree-name { color: var(--text-secondary); font-weight: 500; }

.tree-empty { padding: var(--space-6) var(--space-4); text-align: center; color: var(--text-muted); font-size: 0.875rem; }
.tree-empty[hidden] { display: none; }

@media (max-width: 900px) {
    .tree-table col.tree-col-num { width: 88px; }
    .tree-table .tree-col-optional { display: none; }
    .tree-cell { grid-template-columns: 28px minmax(0, 1fr); --indent: 16px; }
    .tree-desc { display: none; }
    .tree-table th, .tree-table td { padding: 0 var(--space-3); }
    .stat-strip { grid-auto-flow: row; grid-template-columns: repeat(2, 1fr); width: 100%; }
    .stat-strip-cell:nth-child(odd) { border-left: 0; }
    .stat-strip-cell:nth-child(n+3) { border-top: 1px solid var(--border-subtle); }
}

/* ----- Table cell links ---------------------------------------------------
   A taxonomy value in a table is a link to the same table filtered by it. */
.cell-link { color: inherit; border-radius: var(--radius-sm); transition: color var(--transition); }
.cell-link:hover { color: var(--accent-purple-text); text-decoration: underline; text-underline-offset: 3px; }

/* ----- Footer link tree --------------------------------------------------- */

.footer-tree {
    display: grid;
    grid-template-columns: minmax(200px, 1.4fr) repeat(auto-fit, minmax(140px, 1fr));
    gap: var(--space-5) var(--space-6);
    align-items: start;
}
.footer-brand { display: flex; flex-direction: column; align-items: flex-start; gap: var(--space-3); }
.footer-col h3 {
    margin-bottom: var(--space-3);
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--text-muted);
}
.footer-col ul { list-style: none; display: flex; flex-direction: column; gap: var(--space-2); }
.footer-col a { font-size: 0.82rem; color: var(--text-secondary); transition: color var(--transition); }
.footer-col a:hover { color: var(--text-primary); }
@media (max-width: 640px) {
    .footer-tree { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .footer-brand { grid-column: 1 / -1; }
}

/* ----- Studio rail: collapsible sections + nested links ------------------- */

.sa-rail-section-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: var(--space-1) 10px 6px;
    border: 0;
    background: none;
    font-size: 10px;
    font-weight: 600;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--text-muted);
    transition: color var(--transition);
}
.sa-rail-section-toggle:hover { color: var(--text-secondary); }
.sa-rail-section-toggle svg { transition: transform var(--dur-med, 240ms) var(--ease-out, ease); }
.sa-rail-section.is-collapsed .sa-rail-section-toggle svg { transform: rotate(-90deg); }
.sa-rail-links { display: flex; flex-direction: column; gap: 2px; }
.sa-rail-section.is-collapsed .sa-rail-links { display: none; }
.sa-rail-section + .sa-rail-section { margin-top: var(--space-4); }
.sa-rail-link--child {
    margin-left: var(--indent);
    padding-block: 6px;
    font-size: 12.5px;
    color: var(--text-muted);
}
.sa-rail-exits { display: flex; flex-direction: column; gap: var(--space-2); }
@media (max-width: 880px) {
    .sa-rail-section-toggle { display: none; }
    .sa-rail-links,
    .sa-rail-section.is-collapsed .sa-rail-links { display: flex; flex-direction: row; gap: 6px; }
    .sa-rail-link--child { margin-left: 0; }
}
