/* ==========================================================================
   GranSabio SaaS: base layer
   Tokens, reset, layout, header, footer and shared components. Every page loads
   this file plus one area file (studio, texts, advanced, billing, account,
   admin). Typography follows the public landing: Zen Kaku Gothic New for the
   interface and Lora for reading produced texts.
   ========================================================================== */

:root {
    color-scheme: light;

    /* Typography */
    --font-ui: "Zen Kaku Gothic New", "Hiragino Sans", "Segoe UI", system-ui, sans-serif;
    --font-reading: "Lora", Georgia, "Times New Roman", serif;
    --font-mono: ui-monospace, "Cascadia Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
    --text-xs: 0.75rem;
    --text-sm: 0.875rem;
    --text-md: 1rem;
    --text-lg: 1.125rem;
    --text-xl: 1.375rem;
    --text-2xl: 1.75rem;
    --text-3xl: clamp(1.875rem, 1.45rem + 1.6vw, 2.625rem);
    --text-reading: clamp(1.125rem, 1.05rem + 0.35vw, 1.3125rem);
    --leading-tight: 1.2;
    --leading-normal: 1.6;
    --leading-reading: 1.75;
    --weight-regular: 400;
    --weight-medium: 500;
    --weight-bold: 700;
    --weight-black: 900;

    /* Spacing scale */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;
    --space-7: 48px;
    --space-8: 64px;

    /* Layout */
    --page-width: 1200px;
    --page-gutter: 16px;
    --measure: 68ch;
    --header-height: 64px;
    --radius: 6px;
    --radius-sm: 4px;
    --radius-pill: 999px;

    /* Light theme: the landing's ivory paper, green ink and peach accent */
    --bg: #f7f7f1;
    --surface: #ffffff;
    --surface-soft: #e9eee4;
    --ink: #24312b;
    --ink-soft: #435247;
    --muted: #58645b;
    --line: #bdc6b9;
    --line-strong: #7d8a7c;
    --primary: #24312b;
    --on-primary: #f7f7f1;
    --accent: #eb9963;
    --accent-strong: #dc8956;
    --accent-soft: #f6e4d7;
    --on-accent: #24312b;
    --success: #3f6947;
    --success-soft: #e7efdf;
    --warning: #7a551c;
    --warning-soft: #f6ead5;
    --danger: #a33f32;
    --danger-soft: #f9e6df;
    --info: #3f6170;
    --info-soft: #e7eef0;
    --code-bg: #19231e;
    --code-ink: #f7f7f1;
    --focus-ring: #24312b;
    --current-indicator: #24312b;
    --overlay-shadow: 0 16px 40px rgba(36, 49, 43, 0.16);
    --backdrop: rgba(25, 35, 30, 0.55);
}

html[data-theme="dark"] {
    color-scheme: dark;
    /* Neutral dark shared with the landing and the embedded chat's dark theme */
    --bg: #1e1e1e;
    --surface: #252526;
    --surface-soft: #2d2d30;
    --ink: #ededed;
    --ink-soft: #cccccc;
    --muted: #aaa9a7;
    --line: #454545;
    --line-strong: #858585;
    --primary: #ededed;
    --on-primary: #1e1e1e;
    --accent: #eb9963;
    --accent-strong: #f1ac7e;
    --accent-soft: #433329;
    --on-accent: #24312b;
    --success: #a9ceae;
    --success-soft: #29352c;
    --warning: #ebc17d;
    --warning-soft: #3d3529;
    --danger: #f3aaa0;
    --danger-soft: #402d2b;
    --info: #aec8e4;
    --info-soft: #29343f;
    --code-bg: #141414;
    --code-ink: #e3e3e3;
    --focus-ring: #ededed;
    --current-indicator: #eb9963;
    --overlay-shadow: 0 18px 44px rgba(0, 0, 0, 0.45);
    --backdrop: rgba(0, 0, 0, 0.62);
}

@media (min-width: 640px) {
    :root {
        --page-gutter: 24px;
    }
}

@media (min-width: 1024px) {
    :root {
        --page-gutter: 40px;
    }
}

/* ---- Reset ---- */
*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    background: var(--bg);
    color: var(--ink);
    -webkit-text-size-adjust: 100%;
    scroll-padding-top: calc(var(--header-height) + var(--space-4));
}

body {
    display: flex;
    min-height: 100vh;
    flex-direction: column;
    margin: 0;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--font-ui);
    font-size: var(--text-md);
    font-synthesis: none;
    line-height: var(--leading-normal);
    overflow-wrap: break-word;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
}

[hidden] {
    display: none !important;
}

a {
    color: inherit;
    text-decoration-thickness: 1px;
    text-underline-offset: 4px;
}

a:hover {
    text-decoration-thickness: 2px;
}

button,
input,
select,
textarea {
    font: inherit;
    color: inherit;
}

button,
summary {
    cursor: pointer;
}

img,
svg {
    max-width: 100%;
}

:focus-visible {
    outline: 3px solid var(--focus-ring);
    outline-offset: 3px;
}

::selection {
    background: var(--accent);
    color: var(--on-accent);
}

h1,
h2,
h3,
h4 {
    margin: 0;
    color: var(--ink);
    font-weight: var(--weight-bold);
    line-height: var(--leading-tight);
    letter-spacing: -0.02em;
    text-wrap: balance;
}

h1 {
    font-size: var(--text-3xl);
    font-weight: var(--weight-medium);
    letter-spacing: -0.035em;
}

h2 {
    margin: var(--space-6) 0 var(--space-3);
    font-size: var(--text-xl);
}

h3 {
    margin: var(--space-5) 0 var(--space-3);
    font-size: var(--text-lg);
}

p {
    margin: 0 0 var(--space-3);
}

code {
    font-family: var(--font-mono);
    font-size: 0.92em;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.skip-link {
    position: fixed;
    z-index: 40;
    top: var(--space-3);
    left: var(--page-gutter);
    padding: var(--space-2) var(--space-4);
    background: var(--ink);
    color: var(--bg);
    font-weight: var(--weight-bold);
    text-decoration: none;
    transform: translateY(calc(-100% - 24px));
}

.skip-link:focus {
    transform: translateY(0);
}

/* ---- Header ---- */
.site-header {
    position: sticky;
    top: 0;
    z-index: 20;
    border-bottom: 1px solid var(--line);
    background: var(--bg);
}

.site-header__inner,
.page,
.site-footer__inner {
    width: min(var(--page-width), 100% - 2 * var(--page-gutter));
    margin-inline: auto;
}

.site-header__inner {
    display: flex;
    min-height: var(--header-height);
    align-items: center;
    gap: var(--space-5);
}

.brand {
    display: inline-flex;
    flex-shrink: 0;
    align-items: center;
    min-height: 44px;
    color: var(--ink);
    font-size: 1.5rem;
    font-weight: var(--weight-black);
    line-height: 1;
    letter-spacing: -0.055em;
    text-decoration: none;
    white-space: nowrap;
}

.brand span {
    font-weight: var(--weight-regular);
}

.primary-nav {
    min-width: 0;
    margin-right: auto;
}

.primary-nav ul {
    display: flex;
    gap: var(--space-1);
    margin: 0;
    padding: 0;
    list-style: none;
}

.primary-nav a {
    position: relative;
    display: inline-flex;
    min-height: 44px;
    align-items: center;
    padding: 0 var(--space-3);
    color: var(--ink-soft);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    text-decoration: none;
    white-space: nowrap;
}

.primary-nav a:hover {
    color: var(--ink);
    text-decoration: underline;
}

.primary-nav a[aria-current="page"] {
    color: var(--ink);
    font-weight: var(--weight-bold);
}

.primary-nav a[aria-current="page"]::after {
    content: "";
    position: absolute;
    right: var(--space-3);
    bottom: 6px;
    left: var(--space-3);
    height: 2px;
    background: var(--current-indicator);
}

.header-tools {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    gap: var(--space-2);
    margin-left: auto;
}

.primary-nav + .header-tools {
    margin-left: 0;
}

/* Theme toggle (same control as the landing) */
.theme-toggle {
    display: inline-flex;
    flex-shrink: 0;
    width: 44px;
    height: 44px;
    align-items: center;
    justify-content: center;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--ink-soft);
}

.theme-toggle:hover {
    border-color: var(--ink);
    background: var(--surface-soft);
    color: var(--ink);
}

.theme-toggle svg {
    width: 20px;
    height: 20px;
}

.theme-toggle .icon-sun,
html[data-theme="dark"] .theme-toggle .icon-moon {
    display: none;
}

html[data-theme="dark"] .theme-toggle .icon-sun {
    display: block;
}

/* Language switch */
.language-switch {
    margin: 0;
}

.language-switch fieldset {
    display: flex;
    gap: var(--space-1);
    margin: 0;
    padding: 0;
    border: 0;
}

.language-switch button {
    min-width: 44px;
    min-height: 44px;
    padding: 0 var(--space-2);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--muted);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
}

.language-switch button:hover {
    border-color: var(--line);
    color: var(--ink);
}

.language-switch button[aria-pressed="true"] {
    color: var(--ink);
    font-weight: var(--weight-bold);
    text-decoration: underline;
    text-decoration-color: var(--current-indicator);
    text-decoration-thickness: 2px;
    text-underline-offset: 6px;
}

/* Account menu: a native disclosure, enhanced by core.js */
.account-menu {
    position: relative;
}

.account-menu > summary {
    display: inline-flex;
    min-width: 44px;
    min-height: 44px;
    align-items: center;
    gap: var(--space-1);
    padding: 3px var(--space-2) 3px 3px;
    border: 1px solid var(--line);
    border-radius: var(--radius-pill);
    list-style: none;
}

.account-menu > summary::-webkit-details-marker {
    display: none;
}

.account-menu > summary:hover,
.account-menu[open] > summary {
    border-color: var(--ink);
}

.account-menu__chevron {
    width: 8px;
    height: 8px;
    margin: 0 2px 3px 4px;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: rotate(45deg);
}

.account-menu[open] .account-menu__chevron {
    margin-bottom: -3px;
    transform: rotate(-135deg);
}

.avatar {
    display: inline-grid;
    width: 36px;
    height: 36px;
    flex: 0 0 auto;
    place-items: center;
    border-radius: 50%;
    background: var(--accent);
    color: var(--on-accent);
    font-weight: var(--weight-bold);
}

.account-menu__panel {
    position: absolute;
    z-index: 30;
    top: calc(100% + var(--space-2));
    right: 0;
    width: min(300px, 100vw - 2 * var(--page-gutter));
    padding: var(--space-2);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--overlay-shadow);
}

.account-menu__identity {
    margin: 0;
    padding: var(--space-2) var(--space-3) var(--space-3);
    border-bottom: 1px solid var(--line);
    color: var(--muted);
    font-size: var(--text-sm);
}

.account-menu__identity strong {
    display: block;
    overflow: hidden;
    color: var(--ink);
    text-overflow: ellipsis;
    white-space: nowrap;
}

.account-menu__links {
    margin: var(--space-2) 0;
    padding: 0;
    list-style: none;
}

.account-menu__links a,
.account-menu__signout button {
    display: flex;
    width: 100%;
    min-height: 44px;
    align-items: center;
    padding: 0 var(--space-3);
    border: 0;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--ink);
    font-size: var(--text-sm);
    text-align: left;
    text-decoration: none;
}

.account-menu__links a:hover,
.account-menu__signout button:hover {
    background: var(--surface-soft);
}

.account-menu__links a[aria-current="page"] {
    font-weight: var(--weight-bold);
}

.account-menu__section {
    padding: var(--space-2) var(--space-3);
    border-top: 1px solid var(--line);
}

.account-menu__section-label {
    margin: 0 0 var(--space-1);
    color: var(--muted);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
}

.account-menu__section .language-switch fieldset {
    margin-left: calc(-1 * var(--space-2));
}

.account-menu__signout {
    margin: 0;
    padding-top: var(--space-2);
    border-top: 1px solid var(--line);
}

/* ---- Page shell ---- */
.page {
    flex: 1 0 auto;
    min-width: 0;
    padding-block: var(--space-7) var(--space-8);
}

.page-header {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: space-between;
    gap: var(--space-4) var(--space-5);
    margin-bottom: var(--space-6);
}

.page-header > div:first-child,
.page-header__text {
    min-width: 0;
    max-width: 72ch;
}

.page-header__actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
}

.eyebrow {
    margin: 0 0 var(--space-2);
    color: var(--muted);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    letter-spacing: 0.075em;
    text-transform: uppercase;
}

.lead {
    max-width: 64ch;
    margin: var(--space-3) 0 0;
    color: var(--ink-soft);
    font-size: var(--text-lg);
}

.actions,
.admin-nav {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3);
    min-width: 0;
}

.actions {
    margin-top: var(--space-5);
}

.admin-nav form {
    display: inline-flex;
    margin: 0;
}

.run-section {
    margin-top: var(--space-6);
}

.run-section > h2:first-child {
    margin-top: 0;
}

/* Long-form reading (produced texts) */
.prose {
    max-width: var(--measure);
    color: var(--ink);
    font-family: var(--font-reading);
    font-size: var(--text-reading);
    line-height: var(--leading-reading);
    letter-spacing: -0.005em;
}

.prose > * + * {
    margin-top: 1em;
}

.prose h1,
.prose h2,
.prose h3 {
    font-family: var(--font-reading);
    font-weight: var(--weight-medium);
    letter-spacing: -0.015em;
}

/* ---- Buttons ---- */
.button {
    display: inline-flex;
    min-height: 44px;
    max-width: 100%;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: 0 var(--space-4);
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--ink);
    font-size: var(--text-sm);
    font-weight: var(--weight-bold);
    line-height: 1.2;
    text-align: center;
    text-decoration: none;
    transition: background-color 140ms ease, border-color 140ms ease, color 140ms ease;
}

.button:hover {
    border-color: var(--ink);
    text-decoration: none;
}

.button--primary,
.button--accent {
    border-color: var(--accent);
    background: var(--accent);
    color: var(--on-accent);
}

.button--primary:hover,
.button--accent:hover {
    border-color: var(--accent-strong);
    background: var(--accent-strong);
    color: var(--on-accent);
}

.button--ghost {
    border-color: transparent;
    background: transparent;
    color: var(--ink-soft);
}

.button--ghost:hover {
    border-color: var(--line-strong);
    color: var(--ink);
}

.button--danger {
    border-color: var(--danger);
    background: var(--surface);
    color: var(--danger);
}

.button--danger:hover {
    border-color: var(--danger);
    background: var(--danger-soft);
    color: var(--danger);
}

.button--lg {
    min-height: 52px;
    padding: 0 var(--space-5);
    font-size: var(--text-md);
}

.button:disabled,
.button[aria-disabled="true"] {
    cursor: not-allowed;
    opacity: 0.62;
}

.button.is-loading {
    cursor: progress;
}

.button.is-loading::before {
    content: "";
    width: 14px;
    height: 14px;
    flex: 0 0 auto;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: button-spin 720ms linear infinite;
}

form[aria-busy="true"] {
    cursor: progress;
}

form[aria-busy="true"] .button {
    pointer-events: none;
}

@keyframes button-spin {
    to {
        transform: rotate(360deg);
    }
}

/* ---- Panels ---- */
.status-panel,
.notice,
.auth-form,
.table-card,
.text-panel {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
}

.status-panel {
    padding: var(--space-5);
}

.notice {
    width: min(560px, 100%);
    margin: 0 auto;
    padding: var(--space-6);
}

.notice h1 {
    font-size: var(--text-2xl);
}

.panel-heading {
    margin-bottom: var(--space-4);
}

.panel-heading .eyebrow {
    margin-bottom: var(--space-1);
}

.panel-heading h2 {
    margin: 0;
    font-size: var(--text-lg);
}

.text-panel {
    overflow: auto;
    padding: var(--space-4);
    color: var(--ink-soft);
    line-height: 1.7;
}

.status-list {
    display: grid;
    gap: var(--space-3);
    margin: 0;
    padding: 0;
}

.status-list div {
    min-width: 0;
    padding: var(--space-4);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
}

.status-list dt {
    color: var(--muted);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
}

.status-list dd {
    margin: var(--space-1) 0 0;
    color: var(--ink);
    font-size: var(--text-lg);
    font-weight: var(--weight-bold);
    line-height: 1.3;
    overflow-wrap: anywhere;
}

.admin-grid {
    grid-template-columns: repeat(auto-fit, minmax(min(190px, 100%), 1fr));
    margin-top: var(--space-5);
}

/* Code blocks (diagnostics and JSON) use the system monospace stack */
.code-block {
    max-height: 560px;
    overflow: auto;
    margin: 0;
    padding: var(--space-4);
    border-radius: var(--radius);
    background: var(--code-bg);
    color: var(--code-ink);
    font-family: var(--font-mono);
    font-size: var(--text-sm);
    line-height: 1.6;
    white-space: pre-wrap;
}

/* Funding panels shared by the studio, texts, balance and account areas until
   their Phase 2 rewrites replace them. */
.funding-panel {
    margin: var(--space-5) 0;
    padding: var(--space-5);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
}

.funding-panel summary {
    padding: var(--space-2) 0;
    font-weight: var(--weight-bold);
}

.funding-panel details > form,
.funding-panel details > p {
    margin-top: var(--space-3);
}

.funding-panel .auth-form {
    max-width: 42rem;
}

.funding-panel fieldset {
    margin: 0;
    padding: var(--space-4);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
}

.funding-metrics {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(180px, 100%), 1fr));
    gap: var(--space-4);
    margin: var(--space-4) 0;
}

.funding-metrics dt {
    color: var(--muted);
    font-size: var(--text-sm);
}

.funding-metrics dd {
    margin: var(--space-1) 0;
    font-size: var(--text-xl);
    font-weight: var(--weight-bold);
    overflow-wrap: anywhere;
}

.funding-metrics small {
    color: var(--muted);
}

.funding-key {
    padding: var(--space-4) 0;
    border-top: 1px solid var(--line);
    overflow-wrap: anywhere;
}

.funding-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: var(--space-4);
}

.funding-table {
    overflow-x: auto;
}

.funding-table table {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
}

.funding-table td,
.funding-table th {
    padding: var(--space-3);
    border-bottom: 1px solid var(--line);
    vertical-align: top;
}

.funding-notice {
    padding: var(--space-3);
    border-left: 3px solid var(--info);
    background: var(--info-soft);
}

/* ---- Forms ---- */
.auth-form {
    display: grid;
    gap: var(--space-4);
    margin-top: var(--space-5);
    padding: var(--space-5);
}

.notice .auth-form {
    margin-top: var(--space-5);
    padding: 0;
    border: 0;
}

.auth-form label,
.form-field > label {
    display: grid;
    gap: var(--space-2);
    color: var(--ink-soft);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
}

.form-field {
    display: grid;
    gap: var(--space-2);
}

.auth-form input:not([type="checkbox"]):not([type="radio"]),
.auth-form select,
.auth-form textarea {
    width: 100%;
    min-height: 44px;
    padding: 10px 12px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--ink);
    transition: border-color 140ms ease, box-shadow 140ms ease;
}

.auth-form textarea {
    min-height: 200px;
    resize: vertical;
}

.auth-form input[type="file"] {
    padding: var(--space-2);
}

.auth-form input:focus,
.auth-form select:focus,
.auth-form textarea:focus {
    border-color: var(--ink);
    outline: 3px solid var(--focus-ring);
    outline-offset: 1px;
}

.auth-form input:disabled,
.auth-form select:disabled,
.auth-form textarea:disabled {
    cursor: not-allowed;
    background: var(--surface-soft);
    color: var(--muted);
}

.auth-form ::placeholder {
    color: var(--muted);
    opacity: 1;
}

input[type="checkbox"],
input[type="radio"] {
    width: 18px;
    height: 18px;
    margin: 3px 0 0;
    flex: 0 0 auto;
    accent-color: var(--primary);
}

.field-hint {
    margin: 0;
    color: var(--muted);
    font-size: var(--text-sm);
}

.field-counter {
    justify-self: end;
    margin: 0;
    color: var(--muted);
    font-size: var(--text-xs);
}

.password-control {
    position: relative;
    display: grid;
}

.password-control input {
    padding-right: 84px !important;
}

.password-toggle {
    position: absolute;
    top: 50%;
    right: 4px;
    min-width: 64px;
    min-height: 36px;
    transform: translateY(-50%);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: var(--surface-soft);
    color: var(--ink);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
}

.password-toggle:hover {
    border-color: var(--ink);
}

.file-hint {
    display: inline-flex;
    min-height: 28px;
    align-items: center;
    color: var(--muted);
    font-size: var(--text-sm);
}

/* Choice cards: the control always sits inline, before its label text */
.choice-card,
.auth-form label.choice-card {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--space-3);
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--ink);
    font-size: var(--text-sm);
    font-weight: var(--weight-regular);
    text-align: left;
    cursor: pointer;
}

.choice-card:hover,
.choice-card:has(input:checked) {
    border-color: var(--ink);
}

.choice-card:has(input:checked) {
    background: var(--surface-soft);
}

.choice-card:has(input:focus-visible) {
    outline: 3px solid var(--focus-ring);
    outline-offset: 2px;
}

.choice-card__text {
    display: grid;
    min-width: 0;
    gap: 2px;
    overflow-wrap: anywhere;
}

.choice-card__label {
    font-weight: var(--weight-medium);
}

.choice-card__description {
    color: var(--muted);
}

.choice-card--danger {
    border-color: var(--danger);
}

.choice-card--danger:has(input:checked) {
    background: var(--danger-soft);
}

/* Form messages */
.form-error,
.form-success {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    margin: var(--space-4) 0;
    padding: var(--space-3) var(--space-4);
    border: 1px solid;
    border-radius: var(--radius-sm);
    font-weight: var(--weight-medium);
}

.form-error {
    border-color: var(--danger);
    background: var(--danger-soft);
    color: var(--danger);
}

.form-success {
    border-color: var(--success);
    background: var(--success-soft);
    color: var(--success);
}

.cf-turnstile {
    min-height: 65px;
}

/* Account pages (sign in, registration, recovery) */
.auth-links {
    display: grid;
    gap: var(--space-1);
    margin: var(--space-5) 0 0;
    padding: 0;
    list-style: none;
    font-size: var(--text-sm);
}

.auth-links a,
.auth-switch a {
    display: inline-flex;
    min-height: 44px;
    align-items: center;
    color: var(--ink-soft);
}

.auth-links a:hover,
.auth-switch a:hover {
    color: var(--ink);
}

.auth-switch {
    margin: var(--space-5) 0 0;
    padding-top: var(--space-4);
    border-top: 1px solid var(--line);
    color: var(--muted);
    font-size: var(--text-sm);
}

.auth-switch a {
    color: var(--ink);
    font-weight: var(--weight-bold);
}

/* ---- Tables ---- */
.table-card {
    overflow: auto;
    margin: var(--space-4) 0 var(--space-6);
}

.admin-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
}

.admin-table th,
.admin-table td {
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: top;
}

.admin-table th {
    background: var(--surface-soft);
    color: var(--ink-soft);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
}

.admin-table tr:last-child td {
    border-bottom: 0;
}

.admin-table td {
    color: var(--ink-soft);
    font-size: var(--text-sm);
}

.admin-table td:first-child,
.admin-table a {
    color: var(--ink);
    font-weight: var(--weight-medium);
}

.admin-table .table-empty {
    padding: var(--space-6) var(--space-4);
    color: var(--muted);
    text-align: center;
}

/* Table filter, only on tables marked with data-table-filter */
.table-tools {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: space-between;
    gap: var(--space-3);
    margin: var(--space-4) 0 var(--space-2);
}

.table-filter {
    display: grid;
    min-width: min(300px, 100%);
    gap: var(--space-1);
    color: var(--ink-soft);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
}

.table-filter input {
    min-height: 44px;
    width: 100%;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--ink);
}

.table-count {
    color: var(--muted);
    font-size: var(--text-sm);
}

.table-no-results {
    margin: calc(-1 * var(--space-4)) 0 var(--space-6);
    padding: var(--space-4);
    border: 1px dashed var(--line-strong);
    border-radius: var(--radius);
    color: var(--muted);
    text-align: center;
}

/* ---- Components ---- */

/* Status badge */
.badge {
    display: inline-flex;
    max-width: 100%;
    align-items: center;
    gap: 6px;
    padding: 2px 10px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-pill);
    background: var(--surface);
    color: var(--ink-soft);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    line-height: 1.5;
}

.badge::before {
    content: "";
    width: 8px;
    height: 8px;
    flex: 0 0 auto;
    border-radius: 50%;
    background: currentColor;
}

.badge[data-tone="info"] {
    border-color: var(--info);
    background: var(--info-soft);
    color: var(--info);
}

.badge[data-tone="success"] {
    border-color: var(--success);
    background: var(--success-soft);
    color: var(--success);
}

.badge[data-tone="warning"] {
    border-color: var(--warning);
    background: var(--warning-soft);
    color: var(--warning);
}

.badge[data-tone="danger"] {
    border-color: var(--danger);
    background: var(--danger-soft);
    color: var(--danger);
}

/* Empty state */
.empty-state {
    padding: var(--space-6) var(--space-5);
    border: 1px dashed var(--line-strong);
    border-radius: var(--radius);
    text-align: center;
}

.empty-state__title {
    margin: 0;
    font-size: var(--text-lg);
    font-weight: var(--weight-bold);
}

.empty-state__message {
    max-width: 52ch;
    margin: var(--space-2) auto 0;
    color: var(--muted);
}

.empty-state__actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-3);
    margin-top: var(--space-4);
}

/* Copy button */
.copy-control {
    display: inline-flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
}

.copy-button svg {
    width: 18px;
    height: 18px;
}

.copy-control__status {
    color: var(--muted);
    font-size: var(--text-sm);
}

/* Info tooltip (toggletip): opens on click, tap, Enter or Space */
.info-tip {
    position: relative;
    display: inline-flex;
    vertical-align: middle;
}

.info-tip__button {
    display: inline-grid;
    width: 24px;
    height: 24px;
    place-items: center;
    padding: 0;
    border: 1px solid var(--line-strong);
    border-radius: 50%;
    background: var(--surface);
    color: var(--ink-soft);
    font-family: var(--font-reading);
    font-size: var(--text-sm);
    font-style: italic;
    font-weight: var(--weight-bold);
    line-height: 1;
}

.info-tip__button::after {
    content: "";
    position: absolute;
    inset: -10px;
}

.info-tip__button[aria-expanded="true"],
.info-tip__button:hover {
    border-color: var(--ink);
    color: var(--ink);
}

.info-tip__bubble {
    display: block;
    margin-top: var(--space-1);
    color: var(--ink-soft);
    font-size: var(--text-sm);
}

html.has-js .info-tip__bubble {
    position: absolute;
    z-index: 25;
    top: calc(100% + var(--space-2));
    left: 50%;
    width: max-content;
    max-width: min(300px, 100vw - 2 * var(--page-gutter));
    margin: 0;
    padding: var(--space-3);
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    box-shadow: var(--overlay-shadow);
    color: var(--ink);
    font-family: var(--font-ui);
    font-style: normal;
    font-weight: var(--weight-regular);
    line-height: 1.5;
    text-align: left;
    transform: translateX(-50%);
}

html.has-js .info-tip__bubble:not(.is-open) {
    display: none;
}

/* Inline two-step confirmation */
.confirm-action {
    margin: 0;
}

.confirm-action__disclosure > summary {
    width: fit-content;
    list-style: none;
}

.confirm-action__disclosure > summary::-webkit-details-marker {
    display: none;
}

.confirm-action__panel {
    max-width: 52ch;
    margin-top: var(--space-3);
    padding: var(--space-4);
    border: 1px solid var(--line-strong);
    border-radius: var(--radius);
    background: var(--surface);
}

.confirm-action__panel p {
    margin-bottom: var(--space-3);
}

.confirm-action__buttons {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
}

html:not(.has-js) [data-confirm-cancel] {
    display: none;
}

/* Dialog */
.dialog {
    width: min(42rem, 100vw - 2 * var(--page-gutter));
    max-height: min(90vh, 60rem);
    padding: 0;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--ink);
    box-shadow: var(--overlay-shadow);
}

.dialog::backdrop {
    background: var(--backdrop);
}

.dialog__header {
    position: sticky;
    top: 0;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-4);
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid var(--line);
    background: var(--surface);
}

.dialog__title {
    margin: 0;
    font-size: var(--text-xl);
}

.dialog__close {
    display: inline-grid;
    width: 44px;
    height: 44px;
    flex: 0 0 auto;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    background: transparent;
    font-size: var(--text-xl);
    line-height: 1;
}

.dialog__close:hover {
    border-color: var(--ink);
}

.dialog__body {
    padding: var(--space-5);
}

/* ---- Footer ---- */
.site-footer {
    flex-shrink: 0;
    border-top: 1px solid var(--line);
}

.site-footer__inner {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: space-between;
    gap: var(--space-3) var(--space-6);
    padding-block: var(--space-6);
    color: var(--muted);
    font-size: var(--text-sm);
}

.site-footer .brand {
    min-height: auto;
    font-size: 1.25rem;
}

.site-footer__tagline {
    margin: var(--space-2) 0 0;
}

.site-footer__legal {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3);
    margin: 0;
    font-size: var(--text-xs);
}

.env-badge {
    padding: 2px var(--space-2);
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-pill);
}

/* ---- Responsive ---- */
@media (max-width: 760px) {
    .site-header__inner {
        flex-wrap: wrap;
        gap: 0 var(--space-3);
        padding-block: var(--space-2);
    }

    .brand {
        font-size: 1.375rem;
    }

    .primary-nav {
        order: 3;
        width: 100%;
        margin: 0 calc(-1 * var(--space-3));
        overflow-x: auto;
        scrollbar-width: none;
    }

    .primary-nav ul {
        width: max-content;
    }

    .header-tools,
    .primary-nav + .header-tools {
        margin-left: auto;
    }

    /* The menu panel spans the header (the sticky header is its containing block). */
    .account-menu {
        position: static;
    }

    .account-menu__panel {
        top: calc(100% - var(--space-1));
        right: var(--page-gutter);
        left: var(--page-gutter);
        width: auto;
    }

    .page {
        padding-block: var(--space-6) var(--space-7);
    }

    .notice {
        padding: var(--space-5);
    }

    .auth-form,
    .status-panel,
    .funding-panel {
        padding: var(--space-4);
    }

    .actions .button,
    .admin-nav .button,
    .admin-nav form {
        flex: 1 1 auto;
    }
}

/* Narrow screens: tables become labelled cards (labels come from core.js) */
@media (max-width: 640px) {
    html.has-js .admin-table thead {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
    }

    html.has-js .admin-table,
    html.has-js .admin-table tbody,
    html.has-js .admin-table tr,
    html.has-js .admin-table td {
        display: block;
        width: 100%;
    }

    html.has-js .admin-table tr[hidden] {
        display: none;
    }

    html.has-js .admin-table tr {
        border-bottom: 1px solid var(--line);
    }

    html.has-js .admin-table tr:last-child {
        border-bottom: 0;
    }

    html.has-js .admin-table td {
        display: grid;
        grid-template-columns: minmax(96px, 36%) minmax(0, 1fr);
        gap: var(--space-3);
        padding: var(--space-2) var(--space-3);
        border-bottom: 0;
    }

    html.has-js .admin-table td[data-label]::before {
        content: attr(data-label);
        color: var(--muted);
        font-size: var(--text-xs);
        font-weight: var(--weight-medium);
    }

    html.has-js .admin-table td.table-empty {
        display: block;
        text-align: center;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
    }

    .button.is-loading::before {
        animation: none !important;
        border-right-color: currentColor;
    }
}

@media (prefers-contrast: more) {
    :root {
        --line: #7d8a7c;
        --line-strong: #4f5c51;
    }

    html[data-theme="dark"] {
        --line: #8a8a8a;
        --line-strong: #b5b5b5;
    }
}

@media (forced-colors: active) {
    .button,
    .badge,
    .choice-card,
    .theme-toggle,
    .account-menu > summary,
    .avatar,
    .info-tip__button,
    .form-error,
    .form-success {
        forced-color-adjust: none;
        border-color: CanvasText;
    }

    .button--primary,
    .avatar {
        background: Highlight;
        color: HighlightText;
    }

    .primary-nav a[aria-current="page"]::after {
        background: Highlight;
    }
}
