/* components.css — one visual definition for buttons, badges, tables, cards
   and form controls, shared by every page.
   ============================================================================

   HOW THIS FILE IS ADOPTED  (read this before editing)
   ----------------------------------------------------
   Every rule is scoped under `.ui-theme`. A page adopts the shared components
   by putting that class on its <body>:

       <body class="ui-theme">

   Until a page does that, NOTHING in this file matches anything. Loading the
   stylesheet is therefore a no-op, and adoption is one attribute per page —
   revert by deleting it.

   WHY SCOPED, RATHER THAN RENAMING EVERY CALL SITE
   ------------------------------------------------
   `.btn` appears ~180 times across 19 files, and five JS modules build it into
   HTML strings. Renaming all of that to `.ui-btn` would be a large, error-prone
   edit with no way to verify each site. Scoping instead means the existing
   markup is untouched: `class="btn btn-primary"` keeps working and simply
   resolves to these rules once the page opts in.

   LOAD ORDER  (this file must come AFTER the page stylesheet)
   -----------------------------------------------------------
       tokens.css → ui.css → shell.css → <page>.css → components.css → utils.css

   `.ui-theme .btn` is specificity (0,2,0) and the page's `.btn` is (0,1,0), so
   these win on specificity alone. But a page rule like `.section-header .btn`
   is ALSO (0,2,0) — those tie, and ties are broken by load order. Loading last
   makes adoption predictable: opted-in pages get the shared look consistently
   rather than in patches.

   THE `.hidden` TRAP
   ------------------
   utils.css has `.hidden { display: none }` with no !important (deliberately —
   JS toggles element.style.display, which must win). Since these rules are
   (0,2,0), a `display` declared here would BEAT `.hidden` (0,1,0) and every
   hidden button on the page would appear. The `.ui-theme .hidden` rule at the
   bottom of this file restores it at matching specificity. Do not remove it,
   and keep it last.
   ========================================================================== */


/* ============================================================================
   BUTTONS
   ========================================================================== */

.ui-theme .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: 0.5rem var(--space-4);
    min-height: 38px;
    border: 1px solid transparent;
    border-radius: var(--radius-md);
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    font-weight: var(--weight-semibold);
    line-height: 1.4;
    text-decoration: none;
    cursor: pointer;
    white-space: nowrap;
    transition: background-color var(--transition-fast),
                border-color var(--transition-fast),
                color var(--transition-fast);
    /* Default (unqualified `.btn`) reads as the neutral/secondary action. */
    background: var(--surface-default);
    border-color: var(--border-default);
    color: var(--text-default);
}

.ui-theme .btn:hover {
    background: var(--surface-hover);
    border-color: var(--border-strong);
}

/* NOTE: deliberately no `pointer-events: none` here, though vendor.css has it on
   its own .btn:disabled. pointer-events:none suppresses the cursor entirely, so
   adding it globally would silently remove `not-allowed` from every adopted
   page. vendor.css keeps its rule; this stays cursor-only. */
.ui-theme .btn:disabled,
.ui-theme .btn[disabled] {
    opacity: 0.55;
    cursor: not-allowed;
}

.ui-theme .btn:disabled:hover,
.ui-theme .btn[disabled]:hover {
    background: var(--surface-default);
    border-color: var(--border-default);
}

/* --- filled variants --- */

.ui-theme .btn-primary {
    background: var(--primary-900);
    border-color: var(--primary-900);
    color: #fff;
}
.ui-theme .btn-primary:hover {
    background: var(--primary-800);
    border-color: var(--primary-800);
}

.ui-theme .btn-secondary {
    background: var(--surface-default);
    border-color: var(--border-default);
    color: var(--text-default);
}
.ui-theme .btn-secondary:hover {
    background: var(--surface-hover);
    border-color: var(--border-strong);
}

.ui-theme .btn-success {
    background: var(--success-600);
    border-color: var(--success-600);
    color: #fff;
}
.ui-theme .btn-success:hover {
    background: var(--success-700);
    border-color: var(--success-700);
}

.ui-theme .btn-danger {
    background: var(--danger-600);
    border-color: var(--danger-600);
    color: #fff;
}
.ui-theme .btn-danger:hover {
    background: var(--danger-700);
    border-color: var(--danger-700);
}

.ui-theme .btn-warning {
    background: var(--warning-600);
    border-color: var(--warning-600);
    color: #fff;
}
.ui-theme .btn-warning:hover {
    background: var(--warning-700);
    border-color: var(--warning-700);
}

/* --- outline / quiet variants --- */

.ui-theme .btn-outline-primary {
    background: transparent;
    border-color: var(--primary-900);
    color: var(--primary-900);
}
.ui-theme .btn-outline-primary:hover {
    background: var(--primary-50);
    border-color: var(--primary-900);
}

.ui-theme .btn-outline-danger {
    background: transparent;
    border-color: var(--danger-600);
    color: var(--danger-600);
}
.ui-theme .btn-outline-danger:hover {
    background: var(--danger-50);
}

.ui-theme .btn-ghost,
.ui-theme .btn-text {
    background: transparent;
    border-color: transparent;
    color: var(--text-default);
}
.ui-theme .btn-ghost:hover,
.ui-theme .btn-text:hover {
    background: var(--surface-hover);
    border-color: transparent;
}

/* --- sizes --- */

.ui-theme .btn-sm {
    padding: 0.3rem var(--space-3);
    min-height: 30px;
    font-size: var(--text-xs);
    gap: var(--space-1);
}

.ui-theme .btn-lg {
    padding: 0.7rem var(--space-6);
    min-height: 46px;
    font-size: var(--text-base);
}

/* `height` is set as well as `width`/`min-height` so the button is square even
   when a page stylesheet still declares its own height. entity.css sized these
   34px; without an explicit height here it kept that value and the button
   rendered 38 wide by 34 tall. */
.ui-theme .btn-icon {
    padding: 0;
    width: 38px;
    height: 38px;
    min-height: 38px;
    justify-content: center;
}

/* No `.btn-group` rule here on purpose. Only profile.html uses that class, and
   it needs a block-level `display: flex` so its two buttons split the row via
   `flex: 1`. A shared `inline-flex` shrank the container to content width and
   broke that layout — a speculative rule for a class one page owns. */

/* Touch target sizes live at the bottom of this file — see the TOUCH TARGETS
   section. They must come after the form-control rules, not here. */


/* ============================================================================
   BADGES / STATUS PILLS

   Flat tinted chips. The dashboard previously used gradient pills with a drop
   shadow while vendor used flat chips for the same states; this settles on flat,
   which stays legible at the small sizes these appear in inside table cells.
   ========================================================================== */

.ui-theme .status-badge,
.ui-theme .badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-1);
    padding: 0.2rem 0.6rem;
    border-radius: var(--radius-full);
    border: 1px solid transparent;
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    line-height: 1.5;
    letter-spacing: 0.02em;
    white-space: nowrap;
    /* neutral default */
    background: var(--gray-100);
    border-color: var(--gray-200);
    color: var(--gray-700);
}

.ui-theme .status-approved,
.ui-theme .status-active,
.ui-theme .status-paid,
.ui-theme .badge-success {
    background: var(--success-50);
    border-color: var(--success-100);
    color: var(--success-700);
}

.ui-theme .status-pending,
.ui-theme .status-scheduled,
.ui-theme .badge-warning {
    background: var(--warning-50);
    border-color: var(--warning-100);
    color: var(--warning-700);
}

.ui-theme .status-rejected,
.ui-theme .status-inactive,
.ui-theme .badge-danger {
    background: var(--danger-50);
    border-color: var(--danger-100);
    color: var(--danger-700);
}

.ui-theme .status-unknown,
.ui-theme .badge-info {
    background: var(--info-50);
    border-color: var(--info-100);
    color: var(--info-700);
}


/* ============================================================================
   CARDS / SURFACES
   ========================================================================== */

.ui-theme .card,
.ui-theme .profile-card,
.ui-theme .section-card {
    background: var(--surface-default);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
}


/* ============================================================================
   TABLES

   Amount and date columns get tabular figures so digits line up column-wise —
   previously only dashboard.css and invoiceUpload.css did this.
   ========================================================================== */

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

.ui-theme thead th {
    text-align: left;
    padding: var(--space-3) var(--space-4);
    background: var(--surface-sunken);
    border-bottom: 1px solid var(--border-default);
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    white-space: nowrap;
}

.ui-theme tbody td {
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--border-subtle);
    color: var(--text-default);
    vertical-align: middle;
}

.ui-theme tbody tr:hover { background: var(--surface-hover); }
.ui-theme tbody tr:last-child td { border-bottom: 0; }

/* Sortable header control. dashboard.js wraps each header's content in this
   button so sorting is keyboard reachable; it must read as the header text, not
   as a button. The sort arrow stays on th::after, where the page CSS puts it. */
.ui-theme th .sort-btn {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    width: 100%;
    padding: 0;
    border: 0;
    background: none;
    font: inherit;
    color: inherit;
    letter-spacing: inherit;
    text-transform: inherit;
    text-align: inherit;
    cursor: pointer;
}

.ui-theme th .sort-btn:hover { color: var(--text-strong); }

/* Digits align across rows in money/date/count columns. */
.ui-theme td[data-sort-val],
.ui-theme .amount,
.ui-theme .num {
    font-variant-numeric: tabular-nums;
}

/* Wide tables scroll inside their own container rather than the page. */
.ui-theme .table-wrapper,
.ui-theme .invoice-table-container {
    overflow-x: auto;
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-lg);
    background: var(--surface-default);
}

.ui-theme .table-wrapper > table,
.ui-theme .invoice-table-container > table {
    margin: 0;
}


/* ============================================================================
   FORM CONTROLS
   ========================================================================== */

.ui-theme input[type="text"],
.ui-theme input[type="email"],
.ui-theme input[type="password"],
.ui-theme input[type="number"],
.ui-theme input[type="date"],
.ui-theme input[type="month"],
.ui-theme input[type="search"],
.ui-theme input[type="tel"],
.ui-theme select,
.ui-theme textarea {
    padding: 0.5rem var(--space-3);
    min-height: 38px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    background: var(--surface-default);
    color: var(--text-strong);
    font-family: var(--font-sans);
    font-size: var(--text-sm);
    line-height: 1.4;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.ui-theme input:focus,
.ui-theme select:focus,
.ui-theme textarea:focus {
    outline: none;
    border-color: var(--accent-600);
    box-shadow: 0 0 0 3px rgba(21, 101, 192, 0.15);
}

.ui-theme input:disabled,
.ui-theme select:disabled,
.ui-theme textarea:disabled {
    background: var(--surface-sunken);
    color: var(--text-muted);
    cursor: not-allowed;
}

/* Typography only — `display` is deliberately NOT set here. Pages stack labels
   above fields with `display: block`, and forcing `inline-block` globally would
   silently collapse a label to its text width and let a field ride up beside it.
   The block behaviour is restored below, scoped to field wrappers. */
.ui-theme label {
    margin-bottom: var(--space-1);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: var(--text-default);
}

.ui-theme .form-group label,
.ui-theme .filter-field label,
.ui-theme .filters label {
    display: block;
}

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

/* Full width only inside a labelled field wrapper. Applying width:100% to every
   input would stretch inline inputs sitting in toolbars and filter bars, which
   several pages have. .form-group is the wrapper every form page already uses. */
.ui-theme .form-group input,
.ui-theme .form-group select,
.ui-theme .form-group textarea,
.ui-theme .filter-field input,
.ui-theme .filter-field select {
    width: 100%;
}

.ui-theme .form-group input[type="checkbox"],
.ui-theme .form-group input[type="radio"] {
    width: auto;
}

/* Checkboxes and radios must not inherit the full-width sizing above. */
.ui-theme input[type="checkbox"],
.ui-theme input[type="radio"] {
    width: auto;
    min-height: 0;
    padding: 0;
    accent-color: var(--primary-900);
}


/* ============================================================================
   EMPTY STATES

   Matches the markup dashboard.js already builds in createEmptyState():
   .empty-state > .empty-icon + .empty-message + p, with an optional action
   button appended. Only four stylesheets defined this, each differently.
   ========================================================================== */

.ui-theme .empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    text-align: center;
    padding: var(--space-10) var(--space-6);
    background: var(--surface-sunken);
    border: 1px dashed var(--border-default);
    border-radius: var(--radius-lg);
    color: var(--text-muted);
}

.ui-theme .empty-state .empty-icon {
    font-size: 2rem;
    line-height: 1;
    color: var(--text-subtle);
    margin: 0 0 var(--space-1);
}

/* dashboard.js nests a Font Awesome <i> here and the old rule sized it at 4rem,
   which dwarfed the text. Keep it in proportion. */
.ui-theme .empty-state .empty-icon i { font-size: 2rem; }

.ui-theme .empty-state .empty-message,
.ui-theme .empty-state h3 {
    margin: 0;
    font-size: var(--text-base);
    font-weight: var(--weight-semibold);
    color: var(--text-strong);
}

.ui-theme .empty-state p {
    margin: 0;
    font-size: var(--text-sm);
    max-width: 46ch;
}

/* An empty state is more useful when it offers the next step. */
.ui-theme .empty-state .btn { margin-top: var(--space-3); }


/* ============================================================================
   SKELETONS

   Placeholder blocks shown while data loads, so a panel reserves its space
   instead of collapsing and then jumping when content arrives. Before this the
   only skeleton in the codebase was two lines for the vendor dropdown.
   ========================================================================== */

.ui-theme .skeleton {
    position: relative;
    overflow: hidden;
    background: var(--surface-sunken);
    border-radius: var(--radius-sm);
    /* Never let a skeleton read as real content. */
    color: transparent;
    user-select: none;
    pointer-events: none;
}

.ui-theme .skeleton::after {
    content: '';
    position: absolute;
    inset: 0;
    transform: translateX(-100%);
    background: linear-gradient(90deg,
        transparent 0%,
        rgba(255, 255, 255, 0.45) 50%,
        transparent 100%);
    animation: ui-skeleton-sweep 1.4s ease-in-out infinite;
}

:root[data-theme="dark"] .ui-theme .skeleton::after {
    background: linear-gradient(90deg,
        transparent 0%,
        rgba(255, 255, 255, 0.07) 50%,
        transparent 100%);
}

@keyframes ui-skeleton-sweep {
    100% { transform: translateX(100%); }
}

/* Shapes */
.ui-theme .skeleton-text {
    height: 0.85em;
    margin: 0.3em 0;
    border-radius: var(--radius-full);
}
.ui-theme .skeleton-text--sm { width: 40%; }
.ui-theme .skeleton-text--md { width: 65%; }
.ui-theme .skeleton-text--lg { width: 90%; }

.ui-theme .skeleton-block { height: 96px; border-radius: var(--radius-md); }
.ui-theme .skeleton-circle { border-radius: var(--radius-full); width: 40px; height: 40px; }

/* A skeleton row inside a real table keeps the column widths stable. */
.ui-theme tbody tr.skeleton-row td { padding: var(--space-3) var(--space-4); }
.ui-theme tbody tr.skeleton-row td > span {
    display: block;
    height: 0.85em;
    border-radius: var(--radius-full);
    background: var(--surface-sunken);
}

/* The sweep is decoration; without it the block still reserves space. */
@media (prefers-reduced-motion: reduce) {
    .ui-theme .skeleton::after { animation: none; }
}


/* ============================================================================
   RESPONSIVE DATA TABLES

   Below 768px a wide table stops being a table and becomes one card per row,
   with each value labelled by its column. The scheduled-payments table is twelve
   columns wide; horizontally scrolling that on a phone means the invoice id is
   off-screen by the time you reach the amount.

   Depends on `data-label` being present on each cell — ui.js stamps it from the
   header row (see labelTableCells). A cell without one still renders, just
   without its label, so a missed table degrades rather than breaks.

   Scoped to the two known data-table wrappers on purpose. Applying this to every
   table would also catch the small layout tables some pages use for detail
   panels, which are fine as they are.
   ========================================================================== */

@media (max-width: 768px) {
    .ui-theme .table-wrapper,
    .ui-theme .invoice-table-container {
        border: 0;
        background: none;
        overflow-x: visible;   /* nothing left to scroll sideways */
    }

    .ui-theme .table-wrapper > table,
    .ui-theme .invoice-table-container > table {
        display: block;
    }

    /* The header row is redundant once every cell carries its own label, but it
       must stay in the accessibility tree — clip it instead of display:none. */
    .ui-theme .table-wrapper > table > thead,
    .ui-theme .invoice-table-container > table > thead {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    .ui-theme .table-wrapper > table > tbody,
    .ui-theme .invoice-table-container > table > tbody {
        display: block;
    }

    .ui-theme .table-wrapper > table > tbody > tr,
    .ui-theme .invoice-table-container > table > tbody > tr {
        display: block;
        margin-bottom: var(--space-3);
        padding: var(--space-2) var(--space-3);
        background: var(--surface-default);
        border: 1px solid var(--border-subtle);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow-sm);
    }

    .ui-theme .table-wrapper > table > tbody > tr:hover,
    .ui-theme .invoice-table-container > table > tbody > tr:hover {
        background: var(--surface-default);   /* row hover means nothing as a card */
    }

    .ui-theme .table-wrapper > table > tbody > td,
    .ui-theme .table-wrapper > table > tbody > tr > td,
    .ui-theme .invoice-table-container > table > tbody > tr > td {
        display: flex;
        align-items: baseline;
        justify-content: space-between;
        gap: var(--space-4);
        padding: var(--space-2) 0;
        border: 0;
        border-bottom: 1px solid var(--border-subtle);
        text-align: right;
    }

    .ui-theme .table-wrapper > table > tbody > tr > td:last-child,
    .ui-theme .invoice-table-container > table > tbody > tr > td:last-child {
        border-bottom: 0;
    }

    .ui-theme .table-wrapper > table > tbody > tr > td::before,
    .ui-theme .invoice-table-container > table > tbody > tr > td::before {
        content: attr(data-label);
        flex: 0 0 40%;
        text-align: left;
        font-size: var(--text-xs);
        font-weight: var(--weight-semibold);
        text-transform: uppercase;
        letter-spacing: 0.04em;
        color: var(--text-muted);
    }

    /* A cell with no label (unlabelled table, or an actions column) spans full
       width rather than leaving a dead 40% gutter. */
    .ui-theme .table-wrapper > table > tbody > tr > td:not([data-label])::before,
    .ui-theme .invoice-table-container > table > tbody > tr > td:not([data-label])::before {
        content: none;
    }

    .ui-theme .table-wrapper > table > tbody > tr > td:not([data-label]),
    .ui-theme .invoice-table-container > table > tbody > tr > td:not([data-label]) {
        justify-content: flex-start;
        text-align: left;
    }
}


/* ============================================================================
   TOUCH TARGETS

   38px is comfortable with a mouse, but user-management.css previously used 44px
   and dropping every page to 38px would be a real regression on phones.

   ORDER MATTERS: this block must come AFTER the button and form-control rules.
   Its selectors have the same specificity as theirs — (0,2,1) for the
   attribute-qualified inputs — so ties are broken by document order alone. When
   this block sat up in the BUTTONS section, `.btn` picked up 44px (its base rule
   was earlier) but the inputs silently did not (their base rule was later).
   ========================================================================== */

@media (hover: none) and (pointer: coarse) {
    /* min-width matters as much as min-height for a tap target — a 44px-tall
       button that is 20px wide is still a hard target. */
    .ui-theme .btn { min-height: 44px; min-width: 44px; }
    .ui-theme .btn-sm { min-height: 36px; }
    .ui-theme .btn-icon { width: 44px; min-height: 44px; }

    /* The attribute-qualified selectors are repeated verbatim on purpose: a
       plain `.ui-theme input` is only (0,1,1) and would lose to the (0,2,1)
       base rules regardless of order. */
    .ui-theme input[type="text"],
    .ui-theme input[type="email"],
    .ui-theme input[type="password"],
    .ui-theme input[type="number"],
    .ui-theme input[type="date"],
    .ui-theme input[type="month"],
    .ui-theme input[type="search"],
    .ui-theme input[type="tel"],
    .ui-theme select,
    .ui-theme textarea { min-height: 44px; }

    /* …but checkboxes and radios keep their natural size. */
    .ui-theme input[type="checkbox"],
    .ui-theme input[type="radio"] { min-height: 0; }
}

/* iOS Safari zooms the whole page when focusing a field whose font-size is under
   16px, and does not zoom back out. The shared field size is 14px, which is right
   on a desktop but triggers that on a phone. invoiceUpload.css already carried
   this workaround for its own inputs; it belongs with the shared field styles so
   every adopted page gets it.

   Keyed to viewport width rather than pointer type: iPad reports a fine pointer
   with a trackpad attached yet still zooms. */
@media (max-width: 768px) {
    .ui-theme input[type="text"],
    .ui-theme input[type="email"],
    .ui-theme input[type="password"],
    .ui-theme input[type="number"],
    .ui-theme input[type="date"],
    .ui-theme input[type="month"],
    .ui-theme input[type="search"],
    .ui-theme input[type="tel"],
    .ui-theme select,
    .ui-theme textarea { font-size: 16px; }
}


/* ============================================================================
   MUST STAY LAST — see "THE `.hidden` TRAP" in the file header.
   Matches the (0,2,0) specificity of the rules above so `.hidden` keeps winning,
   while an inline element.style.display set by JS still beats it.
   ========================================================================== */

.ui-theme .hidden { display: none; }
