/*
 * ADVx base layer — element defaults for the semantic-HTML idioms the views
 * actually use (see the migration census in the PR). Everything consumes the
 * tokens tier; no literal colors in this file. Radix Themes visual language:
 * calm neutral surfaces, 1px hairlines, solid amber only for primary actions,
 * crisp 2px focus rings, compact type with tight tracking.
 */

@layer reset {
    *,
    *::before,
    *::after {
        box-sizing: border-box;
    }
    * {
        margin: 0;
    }
    img,
    picture,
    video,
    canvas,
    svg {
        display: block;
        max-width: 100%;
    }
    input,
    button,
    textarea,
    select {
        font: inherit;
        letter-spacing: inherit;
    }
}

@layer base {
    /* ── Document ── */
    html {
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
        /* Reserve the vertical scrollbar's space on every page, so short pages
           (Waypoints, Groups) lay out at the same width as the tall ones that
           actually scroll — the container's centering and its max-width
           breakpoint both key off the layout viewport, which the scrollbar
           otherwise shifts page to page. No-op under overlay scrollbars. */
        scrollbar-gutter: stable;
    }
    body {
        min-height: 100vh;
        background-color: var(--advx-bg);
        color: var(--advx-text);
        font-family: var(--advx-font-body);
        font-size: var(--advx-text-3);
        line-height: 1.55;
    }

    /* ── Layout: container (Pico-compatible breakpoints) ── */
    .container {
        width: 100%;
        margin-inline: auto;
        padding-inline: var(--advx-space-4);
    }
    @media (min-width: 576px) {
        .container {
            max-width: 510px;
            padding-inline: 0;
        }
    }
    @media (min-width: 768px) {
        .container {
            max-width: 700px;
        }
    }
    @media (min-width: 1024px) {
        .container {
            max-width: 950px;
        }
    }
    @media (min-width: 1280px) {
        .container {
            max-width: 1200px;
        }
    }
    @media (min-width: 1536px) {
        .container {
            max-width: 1450px;
        }
    }

    /* ── Vertical rhythm ── */
    p,
    ul,
    ol,
    dl,
    table,
    figure,
    blockquote,
    pre,
    form,
    fieldset,
    article,
    details,
    hgroup {
        margin-bottom: var(--advx-space-4);
    }
    ul,
    ol {
        padding-inline-start: var(--advx-space-5);
    }
    li {
        margin-bottom: var(--advx-space-1);
    }

    /* ── Headings ── */
    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {
        margin-bottom: var(--advx-space-3);
        color: var(--advx-text);
        font-family: var(--advx-font-heading);
        font-weight: 600;
        line-height: 1.15;
        text-transform: var(--advx-heading-transform);
        text-wrap: balance;
    }
    h1 {
        font-size: var(--advx-text-7);
        font-weight: 700;
    }
    h2 {
        font-size: var(--advx-text-6);
    }
    h3 {
        font-size: var(--advx-text-5);
    }
    h4 {
        font-size: var(--advx-text-4);
    }
    h5,
    h6 {
        font-size: var(--advx-text-3);
    }
    /* headings after content get breathing room */
    :is(p, ul, ol, dl, table, figure, article, section)
        + :is(h1, h2, h3, h4, h5, h6) {
        margin-top: var(--advx-space-6);
    }
    hgroup > * {
        margin-bottom: 0;
    }
    hgroup > :not(:first-child):last-child {
        color: var(--advx-text-muted);
        font-family: var(--advx-font-body);
        font-size: var(--advx-text-3);
        font-weight: 400;
        letter-spacing: normal;
        text-transform: none;
    }

    small {
        font-size: var(--advx-text-2);
    }
    mark {
        padding: 0.125rem 0.25rem;
        border-radius: var(--advx-radius-1);
        background-color: var(--amber-a4);
        color: var(--advx-text);
    }
    hr {
        border: none;
        border-top: var(--advx-border-width) solid var(--advx-border);
        margin-block: var(--advx-space-5);
    }
    del {
        color: var(--advx-danger);
    }

    /* ── Links ── */
    a {
        color: var(--advx-accent-text);
        text-decoration: underline;
        text-decoration-color: var(--amber-a6);
        text-underline-offset: 0.15em;
        transition: color var(--advx-transition);
    }
    a:hover {
        color: var(--advx-accent-text-hover);
        text-decoration-color: currentColor;
    }

    /* ── Focus (Radix ring) ── */
    :focus-visible {
        outline: 2px solid var(--advx-focus-ring);
        outline-offset: 2px;
    }

    /* ── Buttons ── */
    button,
    [type="submit"],
    [type="reset"],
    [type="button"],
    [role="button"] {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: var(--advx-space-2);
        padding: var(--advx-space-2) var(--advx-space-4);
        border: var(--advx-border-width) solid transparent;
        border-radius: var(--advx-radius-2);
        background-color: var(--advx-accent);
        color: var(--advx-accent-contrast);
        font-family: var(--advx-font-body);
        font-size: var(--advx-text-3);
        font-weight: 500;
        line-height: 1.5;
        text-align: center;
        text-decoration: none;
        cursor: pointer;
        user-select: none;
        transition:
            background-color var(--advx-transition),
            border-color var(--advx-transition),
            color var(--advx-transition),
            box-shadow var(--advx-transition);
    }
    :is(button, [type="submit"], [type="reset"], [type="button"], [role="button"]):hover {
        background-color: var(--advx-accent-hover);
        color: var(--advx-accent-contrast);
    }
    :is(button, [type="submit"], [type="reset"], [type="button"], [role="button"]):is(:disabled, [aria-disabled="true"]) {
        opacity: 0.5;
        pointer-events: none;
    }

    /* soft (secondary): tinted neutral, Radix "soft" */
    :is(button, [type="submit"], [type="button"], [role="button"]).secondary {
        background-color: var(--gray-a3);
        color: var(--advx-text);
    }
    :is(button, [type="submit"], [type="button"], [role="button"]).secondary:hover {
        background-color: var(--gray-a4);
        color: var(--advx-text);
    }

    /* outline */
    :is(button, [type="submit"], [type="button"], [role="button"]).outline {
        border-color: var(--gray-a7);
        background-color: transparent;
        color: var(--advx-text);
    }
    :is(button, [type="submit"], [type="button"], [role="button"]).outline:hover {
        background-color: var(--gray-a2);
        border-color: var(--gray-a8);
        color: var(--advx-text);
    }
    :is(button, [type="submit"], [type="button"], [role="button"]).outline.secondary {
        color: var(--advx-text-muted);
    }
    :is(button, [type="submit"], [type="button"], [role="button"]).outline.secondary:hover {
        color: var(--advx-text);
    }

    /* ghost: bare glyph/label, surfaces on hover — toolbar icon buttons */
    :is(button, [type="submit"], [type="button"], [role="button"]).ghost {
        border-color: transparent;
        background-color: transparent;
        color: var(--advx-text-muted);
    }
    :is(button, [type="submit"], [type="button"], [role="button"]).ghost:hover {
        background-color: var(--gray-a3);
        color: var(--advx-text);
    }

    /* contrast: high-emphasis neutral */
    :is(button, [type="submit"], [type="button"], [role="button"]).contrast {
        background-color: var(--gray-12);
        color: var(--gray-1);
    }
    :is(button, [type="submit"], [type="button"], [role="button"]).contrast:hover {
        background-color: var(--gray-11);
        color: var(--gray-1);
    }

    /* pressed/selected toggle state (planner tools, view switches) */
    :is(button, [role="button"])[aria-pressed="true"],
    :is(button, [role="button"])[aria-current] {
        background-color: var(--amber-a4);
        border-color: var(--amber-a7);
        color: var(--advx-accent-text);
    }

    /* ── Button groups (action rows, form footers) ──
       Gap-spaced, never fused — buttons keep their own borders and radii.
       The joined segments were a Pico idiom; the segmented control covers
       the one case that genuinely wants fusing. */
    [role="group"] {
        display: inline-flex;
        align-items: center;
        gap: var(--advx-space-2);
        margin-bottom: var(--advx-space-4);
        vertical-align: middle;
    }
    [role="group"] > *,
    [role="group"] [role="button"],
    [role="group"] button,
    [role="group"] > form {
        margin-bottom: 0;
    }

    /* ── Forms ── */
    label,
    fieldset legend {
        display: block;
        margin-bottom: var(--advx-space-1);
        color: var(--advx-text);
        font-size: var(--advx-text-2);
        font-weight: 500;
    }
    fieldset {
        border: none;
        padding: 0;
    }
    input:not([type="checkbox"], [type="radio"], [type="range"], [type="file"], [type="submit"], [type="reset"], [type="button"]),
    select,
    textarea {
        display: block;
        width: 100%;
        appearance: none;
        padding: var(--advx-space-2) var(--advx-space-3);
        border: var(--advx-border-width) solid var(--advx-field-border);
        border-radius: var(--advx-radius-2);
        background-color: var(--advx-field-bg);
        color: var(--advx-field-text);
        font-size: var(--advx-text-3);
        line-height: 1.5;
        transition:
            border-color var(--advx-transition),
            box-shadow var(--advx-transition);
    }
    input:not([type="checkbox"], [type="radio"], [type="file"], [type="submit"], [type="reset"], [type="button"]),
    select,
    textarea {
        margin-bottom: var(--advx-space-4);
    }
    ::placeholder {
        color: var(--advx-field-placeholder);
        opacity: 1;
    }
    :is(input, select, textarea):focus-visible {
        outline: 2px solid var(--advx-focus-ring);
        outline-offset: -1px;
    }
    :is(input, select, textarea)[aria-invalid="true"] {
        border-color: var(--advx-danger-border);
    }
    :is(input, select, textarea)[aria-invalid="false"] {
        border-color: var(--advx-valid-border);
    }
    :is(input, select, textarea) + small {
        display: block;
        margin-top: calc(var(--advx-space-3) * -1);
        margin-bottom: var(--advx-space-4);
        color: var(--advx-text-muted);
    }
    :is(input, select, textarea)[aria-invalid="true"] + small {
        color: var(--advx-danger);
    }
    select:not([multiple]) {
        padding-right: var(--advx-space-6);
        background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
        background-position: center right var(--advx-space-3);
        background-repeat: no-repeat;
    }
    input[type="checkbox"],
    input[type="radio"] {
        appearance: none;
        display: inline-block;
        width: 1.15em;
        height: 1.15em;
        margin-right: var(--advx-space-2);
        border: var(--advx-border-width) solid var(--advx-field-border);
        border-radius: var(--advx-radius-1);
        background-color: var(--advx-field-bg);
        vertical-align: -0.15em;
        cursor: pointer;
        transition:
            background-color var(--advx-transition),
            border-color var(--advx-transition);
    }
    input[type="radio"] {
        border-radius: var(--advx-radius-full);
    }
    input[type="checkbox"]:checked,
    input[type="radio"]:checked {
        border-color: var(--advx-accent);
        background-color: var(--advx-accent);
        background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23000' stroke-width='3.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='20 6 9 17 4 12'%3E%3C/polyline%3E%3C/svg%3E");
        background-position: center;
        background-size: 0.85em;
        background-repeat: no-repeat;
    }
    input[type="radio"]:checked {
        background-image: none;
        box-shadow: inset 0 0 0 0.22em var(--advx-field-bg);
    }
    /* switch (role=switch checkbox) */
    input[type="checkbox"][role="switch"] {
        width: 2.1em;
        height: 1.2em;
        border-radius: var(--advx-radius-full);
        background-color: var(--gray-6);
        border-color: transparent;
        position: relative;
    }
    input[type="checkbox"][role="switch"]::before {
        content: "";
        position: absolute;
        top: 50%;
        left: 0.12em;
        width: 0.95em;
        height: 0.95em;
        border-radius: var(--advx-radius-full);
        background-color: light-dark(#fff, var(--gray-12));
        transform: translateY(-50%);
        transition: left var(--advx-transition);
    }
    input[type="checkbox"][role="switch"]:checked {
        background-color: var(--advx-accent);
        background-image: none;
    }
    input[type="checkbox"][role="switch"]:checked::before {
        left: calc(100% - 0.95em - 0.12em);
        background-color: #fff;
    }
    input[type="file"] {
        width: 100%;
        margin-bottom: var(--advx-space-4);
        color: var(--advx-text-muted);
        font-size: var(--advx-text-2);
    }
    input[type="file"]::file-selector-button {
        margin-right: var(--advx-space-3);
        padding: var(--advx-space-1) var(--advx-space-3);
        border: var(--advx-border-width) solid var(--gray-a7);
        border-radius: var(--advx-radius-2);
        background-color: var(--gray-a3);
        color: var(--advx-text);
        font-weight: 500;
        cursor: pointer;
    }

    /* ── Tables ── */
    table {
        width: 100%;
        border-collapse: collapse;
        font-size: var(--advx-text-2);
    }
    th,
    td {
        padding: var(--advx-space-3);
        border-bottom: var(--advx-border-width) solid var(--advx-border);
        text-align: left;
        vertical-align: middle;
    }
    thead th {
        color: var(--advx-text-muted);
        font-size: var(--advx-text-1);
        font-weight: 600;
        letter-spacing: var(--advx-eyebrow-tracking);
        text-transform: var(--advx-eyebrow-transform);
    }
    table.striped tbody tr:nth-child(odd) {
        background-color: var(--gray-a2);
    }
    figure {
        overflow-x: auto;
    }

    /* ── details / summary ── */
    details {
        border-bottom: var(--advx-border-width) solid var(--advx-border);
        padding-bottom: var(--advx-space-2);
    }
    details summary {
        cursor: pointer;
        color: var(--advx-text-muted);
        font-weight: 500;
        line-height: 1.5;
        list-style: none;
        transition: color var(--advx-transition);
    }
    details summary::-webkit-details-marker {
        display: none;
    }
    details summary::after {
        content: "";
        display: block;
        float: right;
        width: 1rem;
        height: 1.5rem;
        background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
        background-position: right center;
        background-repeat: no-repeat;
        transition: transform var(--advx-transition);
    }
    details[open] > summary::after {
        transform: rotate(180deg);
    }
    details summary:hover {
        color: var(--advx-text);
    }
    details[open] > summary {
        margin-bottom: var(--advx-space-3);
    }

    /* ── Dropdown (details.dropdown) ── */
    details.dropdown {
        position: relative;
        border-bottom: none;
        padding-bottom: 0;
    }
    details.dropdown > summary {
        margin-bottom: 0;
    }
    details.dropdown > summary:not([role]) {
        display: inline-flex;
        align-items: center;
        gap: var(--advx-space-1);
        padding: var(--advx-space-1) var(--advx-space-3);
        border: var(--advx-border-width) solid var(--gray-a7);
        border-radius: var(--advx-radius-2);
        background-color: transparent;
        color: var(--advx-text);
        font-size: var(--advx-text-2);
        user-select: none;
        transition:
            background-color var(--advx-transition),
            border-color var(--advx-transition);
    }
    details.dropdown > summary:not([role]):hover,
    details.dropdown[open] > summary:not([role]) {
        background-color: var(--gray-a3);
    }
    details.dropdown > summary::after {
        float: none;
        height: 1rem;
        background-position: center;
    }
    details.dropdown[open] > summary::after {
        transform: rotate(180deg);
    }
    details.dropdown > summary + ul {
        position: absolute;
        z-index: 99;
        left: 0;
        min-width: max-content;
        width: 100%;
        margin: var(--advx-space-1) 0 0;
        padding: var(--advx-space-1);
        display: flex;
        flex-direction: column;
        border: var(--advx-border-width) solid var(--advx-border);
        border-radius: var(--advx-radius-2);
        background-color: var(--advx-surface);
        box-shadow: var(--advx-shadow-2);
    }
    details.dropdown > summary + ul[dir="rtl"] {
        right: 0;
        left: auto;
    }
    details.dropdown > summary + ul li {
        width: 100%;
        margin: 0;
        padding: 0;
        list-style: none;
    }
    details.dropdown > summary + ul li a {
        display: block;
        padding: var(--advx-space-2) var(--advx-space-3);
        border-radius: var(--advx-radius-1);
        color: var(--advx-text);
        text-decoration: none;
        white-space: nowrap;
    }
    details.dropdown > summary + ul li a:hover {
        background-color: var(--gray-a3);
        color: var(--advx-text);
    }

    /* ── Dialog ── */
    dialog {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        position: fixed;
        inset: 0;
        z-index: 999;
        width: 100%;
        height: 100%;
        max-width: none;
        max-height: none;
        padding: var(--advx-space-4);
        border: none;
        background: none;
        color: var(--advx-text);
    }
    dialog:not([open]) {
        display: none;
    }
    dialog::backdrop {
        background-color: light-dark(rgba(33, 32, 28, 0.5), rgba(0, 0, 0, 0.65));
        backdrop-filter: blur(1px);
    }
    dialog > article {
        width: 100%;
        max-width: 32rem;
        max-height: calc(100vh - var(--advx-space-6) * 2);
        overflow: auto;
        margin: 0;
        padding: var(--advx-space-5);
        border: var(--advx-border-width) solid var(--advx-border);
        border-radius: var(--advx-radius-3);
        background-color: var(--advx-surface);
        box-shadow: var(--advx-shadow-3);
    }
    dialog > article > header {
        margin: calc(var(--advx-space-5) * -1);
        margin-bottom: var(--advx-space-4);
        padding: var(--advx-space-4) var(--advx-space-5);
        border-bottom: var(--advx-border-width) solid var(--advx-border);
    }
    dialog > article > header > * {
        margin-bottom: 0;
    }
    /* Dialog close — the empty rel="prev" button idiom: a quiet × in the
       header's top-right corner (without this it renders as a blank solid
       primary button). */
    dialog > article > header > button[rel="prev"] {
        float: right;
        width: 1.75rem;
        height: 1.75rem;
        min-width: 0;
        padding: 0;
        border: none;
        border-radius: var(--advx-radius-1);
        background-color: transparent;
        background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2.5' stroke-linecap='round'%3E%3Cline x1='18' y1='6' x2='6' y2='18'/%3E%3Cline x1='6' y1='6' x2='18' y2='18'/%3E%3C/svg%3E");
        background-position: center;
        background-repeat: no-repeat;
        background-size: 1rem;
    }
    dialog > article > header > button[rel="prev"]:hover {
        background-color: var(--gray-a3);
    }
    dialog > article > footer {
        display: flex;
        justify-content: flex-end;
        gap: var(--advx-space-2);
        margin: calc(var(--advx-space-5) * -1);
        margin-top: var(--advx-space-4);
        padding: var(--advx-space-3) var(--advx-space-5);
        border-top: var(--advx-border-width) solid var(--advx-border);
    }
    dialog > article > footer :is(button, [role="button"]) {
        margin-bottom: 0;
    }

    /* ── article (plain card surface) ── */
    article {
        padding: var(--advx-space-5);
        border: var(--advx-border-width) solid var(--advx-border);
        border-radius: var(--advx-radius-3);
        background-color: var(--advx-surface);
        box-shadow: var(--advx-shadow-1);
    }
    article > header,
    article > footer {
        margin: calc(var(--advx-space-5) * -1);
        padding: var(--advx-space-3) var(--advx-space-5);
        background-color: var(--advx-surface-2);
    }
    article > header {
        margin-bottom: var(--advx-space-5);
        border-bottom: var(--advx-border-width) solid var(--advx-border);
        border-radius: var(--advx-radius-3) var(--advx-radius-3) 0 0;
    }
    article > footer {
        margin-top: var(--advx-space-5);
        border-top: var(--advx-border-width) solid var(--advx-border);
        border-radius: 0 0 var(--advx-radius-3) var(--advx-radius-3);
    }

    /* ── Grid (Pico-compatible: 1 col below 768px, equal cols above) ── */
    .grid {
        display: grid;
        grid-template-columns: 1fr;
        gap: var(--advx-space-4);
    }
    @media (min-width: 768px) {
        .grid {
            grid-template-columns: repeat(auto-fit, minmax(0%, 1fr));
        }
    }
    .grid > * {
        min-width: 0;
    }

    /* ── aria-busy spinner ── */
    [aria-busy="true"]:not(input, select, textarea) {
        color: var(--advx-text-muted);
        white-space: nowrap;
    }
    [aria-busy="true"]:not(input, select, textarea)::before {
        content: "";
        display: inline-block;
        width: 1em;
        height: 1em;
        margin-right: var(--advx-space-2);
        border: 0.1875em solid currentColor;
        border-right-color: transparent;
        border-radius: var(--advx-radius-full);
        vertical-align: -0.125em;
        animation: advx-spin 0.75s linear infinite;
    }
    @keyframes advx-spin {
        to {
            transform: rotate(360deg);
        }
    }
}
