/* FormLoop design tokens and shared primitives
 *
 * Type:    Atkinson Hyperlegible Next (UI) + Atkinson Hyperlegible Mono (live numbers only).
 *          Five sizes. 14px is the floor; body is 16px.
 * Space:   8px grid (4 / 8 / 16 / 24 / 32 / 48).
 * Shape:   8px controls, 12px panels. One shadow for primary panels, one for overlays.
 * Color:   neutral surfaces ~90%; --primary is the only action color;
 *          success / warn / danger / info carry meaning only.
 * Themes:  light by default; dark follows the system setting.
 */
:root {
    color-scheme: light;

    /* Type */
    --font-sans: "Atkinson Hyperlegible Next", "Atkinson Hyperlegible", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    --font-display: var(--font-sans);
    --font-mono: "Atkinson Hyperlegible Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;

    --fs-sm: 14px;
    --fs-base: 16px;
    --fs-lg: 20px;
    --fs-xl: clamp(28px, 3vw, 36px);
    --fs-display: clamp(48px, 7vh, 76px);

    --lh-body: 1.5;
    --lh-tight: 1.15;
    --measure: 70ch;

    /* Space (8px grid) */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 16px;
    --space-4: 24px;
    --space-5: 32px;
    --space-6: 48px;

    /* Light palette */
    --ink: #1f2f27;
    --ink-2: #33463c;
    --muted: #566359;
    --muted-2: #5f6c62;

    --paper: #f4f6ef;
    --surface: #ffffff;
    --surface-2: #edf1e5;

    --line: #dce3d4;
    --line-strong: #c4cfb8;

    --primary: #2f5a45;
    --primary-strong: #234536;
    --primary-soft: #e6eee1;
    --on-primary: #ffffff;

    --success: #2a6243;
    --success-bg: #e7f2ea;
    --warn: #8a5f1f;
    --warn-bg: #f7eedd;
    --danger: #9c3325;
    --danger-bg: #fbeeea;
    --info: #1f5a8a;
    --info-bg: #e7f0f8;

    /* Legacy aliases: amber now means "warning" only */
    --accent: var(--warn);
    --accent-ink: var(--warn);
    --accent-soft: var(--warn-bg);

    --ring: #5d7d5f;
    --focus: #1f5a8a;
    --focus-soft: rgba(31, 90, 138, 0.18);

    /* Shape */
    --radius-sm: 8px;
    --radius: 12px;
    --radius-lg: 12px;
    --radius-pill: 999px;

    --shadow-sm: 0 1px 2px rgba(31, 47, 39, 0.06), 0 4px 16px rgba(31, 47, 39, 0.05);
    --shadow-md: 0 12px 32px rgba(31, 47, 39, 0.14);

    /* Layout */
    --tap: 44px;
    --topbar-h: 64px;
    --bottom-nav-h: 64px;
    --sidebar-w: 240px;
    --content-max: 1400px;
    --dur: 180ms;
    --ease: cubic-bezier(0.2, 0.7, 0.3, 1);
}

/* Dark palette. Applies when the system is dark (unless the user forced light in
 * Settings), or whenever the user chose dark. Keep the two blocks in sync. */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;

        --ink: #e4ede7;
        --ink-2: #cbd8d0;
        --muted: #9fb2aa;
        --muted-2: #93a69e;

        --paper: #121b17;
        --surface: #1a2520;
        --surface-2: #213029;

        --line: #2d3d35;
        --line-strong: #3c5046;

        --primary: #b6e2c8;
        --primary-strong: #cdeedb;
        --primary-soft: #233a2f;
        --on-primary: #10271b;

        --success: #8fd3aa;
        --success-bg: #1c3427;
        --warn: #e3b56a;
        --warn-bg: #3a2f1c;
        --danger: #f0a08f;
        --danger-bg: #3d211c;
        --info: #8cc4ef;
        --info-bg: #1b2d3c;

        --ring: #8fb79c;
        --focus: #8cc4ef;
        --focus-soft: rgba(140, 196, 239, 0.25);

        --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.25);
        --shadow-md: 0 16px 40px rgba(0, 0, 0, 0.5);
    }
}

:root[data-theme="dark"] {
    color-scheme: dark;

    --ink: #e4ede7;
    --ink-2: #cbd8d0;
    --muted: #9fb2aa;
    --muted-2: #93a69e;

    --paper: #121b17;
    --surface: #1a2520;
    --surface-2: #213029;

    --line: #2d3d35;
    --line-strong: #3c5046;

    --primary: #b6e2c8;
    --primary-strong: #cdeedb;
    --primary-soft: #233a2f;
    --on-primary: #10271b;

    --success: #8fd3aa;
    --success-bg: #1c3427;
    --warn: #e3b56a;
    --warn-bg: #3a2f1c;
    --danger: #f0a08f;
    --danger-bg: #3d211c;
    --info: #8cc4ef;
    --info-bg: #1b2d3c;

    --ring: #8fb79c;
    --focus: #8cc4ef;
    --focus-soft: rgba(140, 196, 239, 0.25);

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 16px 40px rgba(0, 0, 0, 0.5);
}

:root[data-theme="light"] {
    color-scheme: light;
}

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

[hidden] {
    display: none !important;
}

html {
    height: 100%;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    min-height: 100%;
    font-family: var(--font-sans);
    font-size: var(--fs-base);
    line-height: var(--lh-body);
    color: var(--ink);
    background: var(--paper);
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

h1, h2, h3, h4, p, figure {
    margin: 0;
}

h1, h2, h3, h4 {
    line-height: var(--lh-tight);
    text-wrap: balance;
}

p {
    max-width: var(--measure);
}

a {
    color: inherit;
    text-decoration: none;
}

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

button {
    cursor: pointer;
}

svg {
    display: block;
}

::selection {
    background: var(--primary-soft);
    color: var(--ink);
}

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

.skip-link {
    position: fixed;
    left: var(--space-3);
    top: -64px;
    z-index: 200;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-sm);
    background: var(--primary);
    color: var(--on-primary);
    font-size: var(--fs-base);
    font-weight: 700;
    transition: top var(--dur) var(--ease);
}

.skip-link:focus {
    top: var(--space-3);
}

.visually-hidden {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
}

/* Buttons */
.btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: var(--tap);
    padding: 0 var(--space-3);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    font-size: var(--fs-base);
    font-weight: 700;
    line-height: 1;
    white-space: nowrap;
    transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease),
        color var(--dur) var(--ease), transform var(--dur) var(--ease);
}

.btn:active {
    transform: translateY(1px);
}

.btn:disabled,
.btn[aria-disabled="true"] {
    cursor: not-allowed;
    opacity: 0.55;
}

.btn-primary {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--on-primary);
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-strong);
    border-color: var(--primary-strong);
}

.btn-outline {
    background: transparent;
    border-color: var(--line-strong);
    color: var(--ink);
}

.btn-outline:hover:not(:disabled) {
    background: var(--surface-2);
    border-color: var(--ink-2);
}

.btn-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--ink-2);
}

.btn-ghost:hover:not(:disabled) {
    background: var(--surface-2);
}

.btn-danger {
    background: var(--danger);
    border-color: var(--danger);
    color: var(--surface);
}

.btn-danger:hover:not(:disabled) {
    filter: brightness(0.92);
}

.btn-sm {
    min-height: 40px;
    padding: 0 var(--space-3);
    font-size: var(--fs-sm);
}

/* Busy state: set aria-busy="true" on a button while its request runs */
.btn[aria-busy="true"] {
    color: transparent !important;
    pointer-events: none;
}

.btn[aria-busy="true"]::after {
    content: "";
    position: absolute;
    inset: 0;
    margin: auto;
    width: 18px;
    height: 18px;
    border: 2px solid currentColor;
    border-color: var(--on-primary) var(--on-primary) var(--on-primary) transparent;
    border-radius: 50%;
    animation: btn-spin 0.7s linear infinite;
}

.btn-outline[aria-busy="true"]::after,
.btn-ghost[aria-busy="true"]::after {
    border-color: var(--ink) var(--ink) var(--ink) transparent;
}

@keyframes btn-spin {
    to { transform: rotate(360deg); }
}

.icon-btn {
    display: grid;
    place-items: center;
    width: var(--tap);
    height: var(--tap);
    padding: 0;
    border: 1px solid var(--line);
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--ink-2);
}

.icon-btn:hover {
    background: var(--surface-2);
}

.icon-sm {
    width: 18px;
    height: 18px;
    flex: none;
}

.text-link {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    border-radius: var(--radius-sm);
    color: var(--info);
    font-size: inherit;
    font-weight: 700;
    text-decoration: underline;
    text-decoration-color: transparent;
    text-underline-offset: 3px;
}

.text-link:hover {
    text-decoration-color: currentColor;
}

.badge {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    padding: var(--space-1) var(--space-2);
    border: 1px solid var(--line);
    border-radius: var(--radius-pill);
    background: var(--surface-2);
    color: var(--ink-2);
    font-size: var(--fs-sm);
    font-weight: 700;
    line-height: 1.2;
    white-space: nowrap;
}

/* Form primitives */
label {
    font-size: var(--fs-sm);
    font-weight: 700;
    color: var(--ink-2);
}

.input,
input[type="text"],
input[type="password"],
input[type="email"],
input[type="number"],
input[type="search"],
select,
textarea {
    width: 100%;
    min-height: var(--tap);
    padding: var(--space-2) 12px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--ink);
    font-size: var(--fs-base);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.mono-num {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    border-color: var(--focus);
    outline: 3px solid var(--focus-soft);
    outline-offset: 0;
}

input::placeholder {
    color: var(--muted-2);
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}
