/* ===================================================================
   Components. Every interactive element has hover, focus, active,
   disabled and loading covered; every list has empty and error.
   =================================================================== */

/* --- Card --------------------------------------------------------- */

.card {
    background: var(--surface-card);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-card);
}

.card__head {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid var(--line);
    flex-wrap: wrap;
}

.card__title {
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--ink-900);
}

.card__hint {
    font-size: var(--text-xs);
    color: var(--ink-500);
}

.card__tools {
    margin-left: auto;
    display: flex;
    gap: var(--space-2);
    align-items: center;
    flex-wrap: wrap;
}

.card__body {
    padding: var(--space-5);
}

.card__body--flush {
    padding: 0;
}

/* --- KPI tile ----------------------------------------------------- */

.kpi {
    background: var(--surface-card);
    border: 1px solid var(--line);
    border-radius: var(--radius-lg);
    padding: var(--space-4);
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}

.kpi__label {
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: .07em;
    text-transform: uppercase;
    color: var(--ink-500);
}

.kpi__value {
    font-size: var(--text-xl);
    font-weight: 600;
    color: var(--ink-900);
    font-variant-numeric: tabular-nums;
    line-height: 1.15;
    overflow-wrap: anywhere;
}

.kpi__meta {
    font-size: var(--text-xs);
    color: var(--ink-500);
}

.kpi--accent {
    border-left: 3px solid var(--primary);
}

.kpi--warning {
    border-left: 3px solid var(--warning);
}

.kpi--danger {
    border-left: 3px solid var(--danger);
}

.kpi--success {
    border-left: 3px solid var(--success);
}

/* --- Buttons ------------------------------------------------------ */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: 38px;
    padding: var(--space-2) var(--space-4);
    border-radius: var(--radius-md);
    border: 1px solid transparent;
    font: inherit;
    font-size: var(--text-sm);
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background var(--dur-in) var(--ease),
                border-color var(--dur-in) var(--ease),
                color var(--dur-in) var(--ease);
}

.btn svg {
    width: 15px;
    height: 15px;
    flex: 0 0 15px;
}

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

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

.btn--ghost {
    background: var(--surface-card);
    border-color: var(--line-strong);
    color: var(--ink-700);
}

.btn--ghost:hover:not(:disabled) {
    background: var(--surface-sunken);
    color: var(--ink-900);
}

.btn--subtle {
    background: transparent;
    color: var(--ink-500);
}

.btn--subtle:hover:not(:disabled) {
    background: var(--surface-sunken);
    color: var(--ink-900);
}

.btn--danger {
    background: var(--danger);
    color: #fff;
}

.btn--danger:hover:not(:disabled) {
    background: var(--danger-hover);
}

.btn--sm {
    min-height: 32px;
    padding: var(--space-1) var(--space-3);
    font-size: var(--text-xs);
}

/* Disabled: reduced opacity, a cursor change and the real attribute -
   appearance alone would leave it clickable for assistive tech. */
.btn:disabled {
    opacity: .45;
    cursor: not-allowed;
}

/* Loading buttons keep their width so the row does not jump. */
.btn.is-loading {
    position: relative;
    color: transparent !important;
}

.btn.is-loading::after {
    content: "";
    position: absolute;
    width: 15px;
    height: 15px;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: 50%;
    color: var(--primary-ink);
    animation: spin .6s linear infinite;
}

.btn--ghost.is-loading::after,
.btn--subtle.is-loading::after {
    color: var(--ink-700);
}

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

/* Icon-only controls still carry an accessible name via aria-label. */
.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: var(--radius-md);
    border: 1px solid transparent;
    background: transparent;
    color: var(--ink-500);
    cursor: pointer;
    transition: background var(--dur-in) var(--ease), color var(--dur-in) var(--ease);
}

.icon-btn:hover:not(:disabled) {
    background: var(--surface-sunken);
    color: var(--ink-900);
}

.icon-btn svg {
    width: 16px;
    height: 16px;
}

/* --- Forms -------------------------------------------------------- */

.field {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    margin-bottom: var(--space-4);
}

.field__label {
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--ink-900);
}

.field__req {
    color: var(--danger);
    margin-left: 2px;
}

.field__help {
    font-size: var(--text-xs);
    color: var(--ink-500);
}

.field__error {
    font-size: var(--text-xs);
    color: var(--danger);
    font-weight: 500;
    min-height: 0;
}

.input,
.select,
.textarea {
    width: 100%;
    min-height: 38px;
    padding: var(--space-2) var(--space-3);
    font: inherit;
    font-size: var(--text-sm);
    color: var(--ink-900);
    background: var(--surface-card);
    border: 1px solid var(--line-strong);
    border-radius: var(--radius-md);
    transition: border-color var(--dur-in) var(--ease), box-shadow var(--dur-in) var(--ease);
}

.input::placeholder,
.textarea::placeholder {
    color: var(--ink-400);
}

.input:hover:not(:disabled),
.select:hover:not(:disabled) {
    border-color: var(--ink-400);
}

.input:focus,
.select:focus,
.textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

.input:disabled,
.select:disabled,
.textarea:disabled {
    background: var(--surface-sunken);
    color: var(--ink-400);
    cursor: not-allowed;
}

/* Read-only is not disabled: the value is real and still selectable. */
.input[readonly] {
    background: var(--surface-sunken);
    color: var(--ink-700);
}

.input[aria-invalid="true"],
.select[aria-invalid="true"],
.textarea[aria-invalid="true"] {
    border-color: var(--danger);
}

.input[aria-invalid="true"]:focus {
    box-shadow: 0 0 0 3px var(--danger-soft);
}

.textarea {
    min-height: 84px;
    resize: vertical;
}

.field-row {
    display: grid;
    gap: var(--space-4);
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

.fieldset {
    border: 0;
    padding: 0;
    margin: 0 0 var(--space-5);
}

.fieldset__legend {
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: .07em;
    text-transform: uppercase;
    color: var(--ink-500);
    padding: 0 0 var(--space-3);
    border-bottom: 1px solid var(--line);
    margin-bottom: var(--space-4);
    width: 100%;
}

/* Focusable summary of everything wrong, shown after a failed submit. */
.error-summary {
    border: 1px solid var(--danger);
    background: var(--danger-soft);
    border-radius: var(--radius-md);
    padding: var(--space-3) var(--space-4);
    margin-bottom: var(--space-4);
}

.error-summary__title {
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--danger);
    margin-bottom: var(--space-2);
}

.error-summary ul {
    margin: 0;
    padding-left: var(--space-5);
    font-size: var(--text-sm);
}

.error-summary a {
    color: var(--danger);
}

/* --- Badges ------------------------------------------------------- */

/* Dot plus word. Colour alone never carries the meaning. */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px var(--space-2);
    border-radius: var(--radius-sm);
    font-size: var(--text-xs);
    font-weight: 500;
    white-space: nowrap;
}

.badge::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
    flex: 0 0 6px;
}

.badge--success { background: var(--success-soft); color: var(--success); }
.badge--warning { background: var(--warning-soft); color: var(--warning); }
.badge--danger  { background: var(--danger-soft);  color: var(--danger); }
.badge--neutral { background: var(--neutral-soft); color: var(--neutral); }
.badge--primary { background: var(--primary-soft); color: var(--primary); }

.badge--plain::before {
    display: none;
}

/* --- Tables ------------------------------------------------------- */

/*
 * The table responds to the width it actually has, not the width of the
 * window. With a 232px rail pinned to the side, a 1024px viewport leaves the
 * card about 780px - the same squeeze as a 768px phone - so a viewport media
 * query stacks at the wrong moments in both directions.
 *
 * overflow-x is the safety net for the band where the table is too wide to fit
 * but too wide a card to stack: it scrolls inside its own card, and the page
 * body never scrolls sideways.
 */
.table-wrap {
    width: 100%;
    overflow-x: auto;
    container-type: inline-size;
}

.table {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--text-sm);
}

.table thead th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--surface-sunken);
    color: var(--ink-500);
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: .05em;
    text-transform: uppercase;
    text-align: left;
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--line-strong);
    white-space: nowrap;
}

.table tbody td {
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--line);
    color: var(--ink-700);
    vertical-align: middle;
}

.table tbody tr:hover {
    background: var(--surface-raised);
}

.table tbody tr:last-child td {
    border-bottom: 0;
}

/* Money and counts: right-aligned, fixed-width digits so a column of
   figures reads as a column instead of jittering. */
.table .num,
.num {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.table .strong {
    color: var(--ink-900);
    font-weight: 600;
}

.table__primary {
    color: var(--ink-900);
    font-weight: 600;
}

.table__secondary {
    font-size: var(--text-xs);
    color: var(--ink-500);
}

.table__actions {
    display: flex;
    gap: var(--space-1);
    justify-content: flex-end;
}

/* Sortable headers are real buttons and report their state. */
.th-sort {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    background: none;
    border: 0;
    padding: 0;
    font: inherit;
    color: inherit;
    text-transform: inherit;
    letter-spacing: inherit;
    cursor: pointer;
}

.th-sort__arrow {
    opacity: .35;
    font-size: 10px;
}

th[aria-sort="ascending"] .th-sort__arrow,
th[aria-sort="descending"] .th-sort__arrow {
    opacity: 1;
    color: var(--primary);
}

/* --- States ------------------------------------------------------- */

.state {
    padding: var(--space-7) var(--space-5);
    text-align: center;
}

.state__icon {
    width: 40px;
    height: 40px;
    margin: 0 auto var(--space-3);
    color: var(--ink-400);
}

.state__title {
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--ink-900);
    margin-bottom: var(--space-1);
}

.state__text {
    font-size: var(--text-sm);
    color: var(--ink-500);
    max-width: 46ch;
    margin: 0 auto var(--space-4);
}

.state--error .state__icon {
    color: var(--danger);
}

/* Skeletons reserve the real height, so nothing shifts on load. */
.skeleton {
    background: linear-gradient(90deg,
        var(--surface-sunken) 25%, #e6eaf0 37%, var(--surface-sunken) 63%);
    background-size: 400% 100%;
    animation: shimmer 1.3s ease-in-out infinite;
    border-radius: var(--radius-sm);
}

@keyframes shimmer {
    0%   { background-position: 100% 50%; }
    100% { background-position: 0 50%; }
}

.skeleton-row {
    height: 14px;
    margin: var(--space-4);
}

/* --- Toasts ------------------------------------------------------- */

.toast-stack {
    position: fixed;
    right: var(--space-4);
    bottom: var(--space-4);
    z-index: 90;
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    max-width: min(380px, calc(100vw - var(--space-6)));
}

.toast {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    background: var(--surface-card);
    border: 1px solid var(--line);
    border-left: 3px solid var(--primary);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-float);
    font-size: var(--text-sm);
    color: var(--ink-900);
    animation: toast-in var(--dur-in) var(--ease);
}

.toast--success { border-left-color: var(--success); }
.toast--error   { border-left-color: var(--danger); }
.toast--warning { border-left-color: var(--warning); }

.toast__close {
    margin-left: auto;
    background: none;
    border: 0;
    color: var(--ink-500);
    cursor: pointer;
    font-size: var(--text-md);
    line-height: 1;
    padding: 0 var(--space-1);
}

@keyframes toast-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: none; }
}

/* --- Modal -------------------------------------------------------- */

.modal[open] {
    display: flex;
}

.modal {
    position: fixed;
    inset: 0;
    z-index: 80;
    display: none;
    align-items: flex-start;
    justify-content: center;
    padding: var(--space-5) var(--space-3);
    overflow-y: auto;
    background: rgba(17, 24, 32, .45);
    border: 0;
}

.modal__panel {
    background: var(--surface-card);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-float);
    width: 100%;
    max-width: 720px;
    margin: auto;
    animation: modal-in var(--dur-in) var(--ease);
}

.modal__panel--sm {
    max-width: 440px;
}

.modal__head {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid var(--line);
}

.modal__title {
    font-size: var(--text-md);
    font-weight: 600;
}

.modal__body {
    padding: var(--space-5);
}

.modal__foot {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-2);
    padding: var(--space-4) var(--space-5);
    border-top: 1px solid var(--line);
    background: var(--surface-raised);
    border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

@keyframes modal-in {
    from { opacity: 0; transform: translateY(-8px); }
    to   { opacity: 1; transform: none; }
}

/* --- Filter bar --------------------------------------------------- */

.filters {
    display: flex;
    gap: var(--space-3);
    flex-wrap: wrap;
    align-items: flex-end;
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid var(--line);
}

.filters__field {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    min-width: 150px;
}

.filters__field--grow {
    flex: 1 1 240px;
}

.filters__label {
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--ink-500);
}

.search-input {
    position: relative;
    display: flex;
    align-items: center;
}

.search-input svg {
    position: absolute;
    left: var(--space-3);
    width: 15px;
    height: 15px;
    color: var(--ink-400);
    pointer-events: none;
}

.search-input .input {
    padding-left: 34px;
}

/* --- Pagination --------------------------------------------------- */

.pager {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-5);
    border-top: 1px solid var(--line);
    flex-wrap: wrap;
}

.pager__info {
    font-size: var(--text-xs);
    color: var(--ink-500);
    font-variant-numeric: tabular-nums;
}

.pager__controls {
    margin-left: auto;
    display: flex;
    gap: var(--space-2);
    align-items: center;
}

/* --- Chart -------------------------------------------------------- */

.chart {
    width: 100%;
    min-height: 300px;
}

.chart--short {
    min-height: 240px;
}

/* The chart's table alternative: collapsed by default, real markup, and
   reachable by keyboard. A canvas on its own tells a screen reader nothing. */
.chart-data {
    margin-top: var(--space-4);
    border-top: 1px solid var(--line);
    padding-top: var(--space-3);
}

.chart-data summary {
    font-size: var(--text-xs);
    color: var(--ink-500);
    cursor: pointer;
    font-weight: 500;
}

.chart-data__scroll {
    max-height: 260px;
    overflow: auto;
    margin-top: var(--space-3);
}

/* --- Misc --------------------------------------------------------- */

.stack {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

.row-between {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
}

.muted {
    color: var(--ink-500);
}

.text-xs {
    font-size: var(--text-xs);
}

.definition {
    display: grid;
    grid-template-columns: minmax(120px, auto) 1fr;
    gap: var(--space-2) var(--space-4);
    font-size: var(--text-sm);
}

.definition dt {
    color: var(--ink-500);
}

.definition dd {
    margin: 0;
    color: var(--ink-900);
    overflow-wrap: anywhere;
}

/* --- Touch targets ------------------------------------------------
   A 32px icon button is comfortable with a mouse and a coin toss with a
   thumb. On a narrow screen or any coarse pointer, every control grows to
   the 44px minimum. Desktop keeps the denser sizing, which is the whole
   point of a table-heavy admin screen. */

@media (max-width: 767.98px), (pointer: coarse) {
    .btn,
    .btn--sm {
        min-height: 44px;
        padding-left: var(--space-4);
        padding-right: var(--space-4);
    }

    .icon-btn,
    .rail-toggle {
        width: 44px;
        height: 44px;
    }

    /* 16px is not a readability choice here, it is a functional one: iOS Safari
       zooms the whole page in when a focused input is smaller than 16px, and
       does not zoom back out afterwards. Every field on a form would leave the
       user pinching back to where they were. */
    .input,
    .select,
    .textarea {
        font-size: 16px;
    }

    .input,
    .select {
        min-height: 44px;
    }

    .rail__link {
        min-height: 44px;
    }

    /* Row actions need room between them, not just size. */
    .table__actions {
        gap: var(--space-2);
        flex-wrap: wrap;
    }
}

/* --- Table -> stacked cards when the card itself gets narrow -------
   760px is where the widest table here - payments, seven columns ending in a
   two-button action cell - stops being readable. Below that each row becomes
   a labelled card, because a money table you have to drag sideways is worse
   than one that stacks. */

@container (max-width: 760px) {
    .table thead {
        display: none;
    }

    .table,
    .table tbody,
    .table td {
        display: block;
        width: 100%;
    }

    /* Deliberately no width on the row. A block element already fills its
       container, and width:100% plus horizontal margins would add up to more
       than the container - which shows up as a stray sideways scroll inside
       the card. */
    .table tr {
        display: block;
    }

    .table tbody tr {
        border: 1px solid var(--line);
        border-radius: var(--radius-lg);
        margin: var(--space-3);
        padding: var(--space-2) 0;
        background: var(--surface-card);
    }

    .table tbody td {
        border: 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: var(--space-4);
        padding: var(--space-2) var(--space-4);
        text-align: right;
    }

    /* The header text each cell carries, so a stacked row still reads. */
    .table tbody td::before {
        content: attr(data-label);
        font-size: var(--text-xs);
        font-weight: 600;
        color: var(--ink-500);
        text-transform: uppercase;
        letter-spacing: .05em;
        text-align: left;
        flex: 0 0 auto;
    }

    .table tbody td:empty {
        display: none;
    }

    .table__actions {
        justify-content: flex-end;
    }
}
