/* Components shared by every page. Reads tokens.css only. The area files (area-*.css) add page-specific rules and
   must not need to change this file. Class vocabulary:
   - Sections: <section> inside <main> (whitespace + hairline, no card)
   - Text: .hint (help), .info, .error, .success, .warning, .error-banner, .field-error, .mono
   - Status: .badge with .ok / .pending / .off (inactive, not an error) / .error (failure); dot + text
   - Data: .details (definition list "ficha"), table in .table-wrap, .session-list, .pager
   - Actions: .toolbar, .back-link, .inline-form, .secondary / .danger / .link-button (see base.css), .form-grid
   - Values: .secret, .one-time (read-only input with a one-time value), .codes, .recovery-codes, .qr, .code-input
   - Closing link outside a form: p.actions > a.button
   - Overlays: .confirm-dialog, .app-notices / .app-notice (api.js)
   - Utility: .sr-only */

/* ---- Sections: separated by whitespace and a hairline ---- */
section {
    margin-top: var(--space-6);
    padding-top: var(--space-5);
    border-top: var(--hairline) solid var(--rule);
}

section > h2:first-child {
    margin-top: 0;
}

/* ---- Text ---- */
.hint {
    margin: var(--space-1) 0 0;
    color: var(--ink-soft);
    font-size: var(--text-sm);
}

.info {
    color: var(--ink-soft);
    font-size: var(--text-sm);
}

.error {
    color: var(--error);
    font-weight: var(--weight-medium);
}

.field-error {
    margin: var(--space-1) 0 0;
    font-size: var(--text-sm);
}

/* Banners: tinted ground and a coloured edge; the text stays in ink */
.success,
.warning,
.error-banner {
    margin: var(--space-3) 0;
    padding: var(--space-3) var(--space-4);
    border-left: var(--marker) solid var(--ink-soft);
    border-radius: var(--radius);
    color: var(--ink);
}

.success {
    border-left-color: var(--ok);
    background: var(--ok-bg);
}

.warning {
    border-left-color: var(--pending);
    background: var(--pending-bg);
}

.error-banner {
    border-left-color: var(--error);
    background: var(--error-bg);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

/* ---- Status: a dot and text (the shape differs per state, so colour is never the only cue) ---- */
.badge {
    display: inline-flex;
    align-items: baseline;
    gap: var(--space-2);
    font-weight: var(--weight-medium);
    white-space: nowrap;
}

.badge::before {
    content: "";
    flex: none;
    width: var(--dot-size);
    height: var(--dot-size);
    border-radius: 50%;
    background: var(--ink-soft);
    transform: translateY(-0.05em);
}

.badge.ok::before {
    background: var(--ok);
}

.badge.pending::before {
    background: transparent;
    box-shadow: inset 0 0 0 var(--focus-width) var(--pending);
}

/* Inactive but not an error (disabled, not enabled): a hollow square in the soft ink */
.badge.off::before {
    border-radius: 0;
    background: transparent;
    box-shadow: inset 0 0 0 var(--focus-width) var(--ink-soft);
}

/* A failure (e.g. a failed login in the audit): a solid red square; the text takes the error colour from .error */
.badge.error::before {
    border-radius: 0;
    background: var(--error);
}

/* ---- Definition list ("ficha"): label | value rows with hairlines ---- */
.details {
    display: grid;
    grid-template-columns: minmax(0, var(--label-width)) minmax(0, 1fr);
    margin: 0;
}

.details dt,
.details dd {
    margin: 0;
    padding: var(--space-2) 0;
    border-bottom: var(--hairline) solid var(--rule);
    overflow-wrap: anywhere;
}

.details dt {
    padding-right: var(--space-4);
    color: var(--ink-soft);
    font-size: var(--text-sm);
}

@media (max-width: 40rem) {
    .details {
        grid-template-columns: minmax(0, 1fr);
    }

    .details dt {
        padding-bottom: 0;
        border-bottom: 0;
    }
}

/* ---- Tables: dense, hairlines, sticky header, no zebra ---- */
.table-wrap {
    margin-top: var(--space-4);
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--text-sm);
}

caption {
    padding-bottom: var(--space-2);
    color: var(--ink-soft);
    text-align: left;
}

th,
td {
    padding: var(--space-2) var(--space-3) var(--space-2) 0;
    border-bottom: var(--hairline) solid var(--rule);
    text-align: left;
    vertical-align: top;
    overflow-wrap: break-word;
}

th {
    position: sticky;
    top: 0;
    background: var(--surface);
    color: var(--ink-soft);
    font-weight: var(--weight-medium);
    box-shadow: 0 var(--hairline) 0 var(--rule);
}

td:first-child {
    font-weight: var(--weight-medium);
}

/* Inline forms in a cell (sessions): the field keeps a fixed width instead of the input's intrinsic one */
td input {
    width: var(--field-narrow);
    max-width: 100%;
    margin-top: 0;
    padding: var(--space-1) var(--space-2);
}

td .inline-form {
    flex-wrap: wrap;
}

/* Narrow screens: each row becomes a block of its cells (the header stays for screen readers only) */
@media (max-width: 40rem) {
    thead {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip-path: inset(50%);
        white-space: nowrap;
    }

    table,
    tbody,
    tr,
    td {
        display: block;
        width: 100%;
    }

    tr {
        padding: var(--space-2) 0;
        border-bottom: var(--hairline) solid var(--rule);
    }

    td {
        padding: var(--space-1) 0;
        border-bottom: 0;
    }

    td input {
        width: 100%;
    }

    td:empty {
        display: none;
    }

    caption {
        display: block;
    }

    /* Each cell names its column (data-label of the template), because the header is hidden */
    td[data-label]::before {
        content: attr(data-label);
        display: block;
        color: var(--ink-soft);
        font-size: var(--text-xs);
        font-weight: var(--weight-regular);
    }
}

/* Wide screens: dates, addresses and identifiers stay on one line, actions do not break; a table wider than its column scrolls */
@media (min-width: 40.01rem) {
    td.mono,
    td button {
        white-space: nowrap;
    }
}

@media (min-width: 40.01rem) and (max-width: 80rem) {
    .table-wrap {
        overflow-x: auto;
    }
}

.pager {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: var(--space-4);
    margin-top: var(--space-4);
    font-size: var(--text-sm);
}

/* Sessions of "Mi cuenta": one block per session, separated by a hairline */
.session-list {
    margin: 0;
    padding: 0;
    list-style: none;
}

.session-list > li {
    padding: var(--space-3) 0;
    border-top: var(--hairline) solid var(--rule);
}

.session-list > li:first-child {
    border-top: 0;
}

/* ---- Actions ---- */
.toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    margin-top: 0;
    margin-bottom: var(--space-4);
}

.toolbar h1 {
    margin: 0;
}

.back-link {
    margin-top: var(--space-5);
    font-size: var(--text-sm);
}


.inline-form {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin: 0;
}

.inline-form button {
    margin: 0;
}

.inline-form button,
.toolbar a.button {
    padding: var(--space-1) var(--space-3);
}

/* Secondary actions under a form (second factor pages) */
p.secondary,
form.secondary {
    margin: var(--space-4) 0 0;
    font-size: var(--text-sm);
    text-align: center;
}

/* Optional two-column settings form (label left, control right) from 48rem; one column below */
@media (min-width: 48rem) {
    .form-grid {
        display: grid;
        grid-template-columns: minmax(0, var(--label-width)) minmax(0, 1fr);
        column-gap: var(--space-5);
        align-items: start;
    }

    .form-grid > label {
        grid-column: 1;
        margin-top: var(--space-4);
        padding-top: var(--space-2);
    }

    .form-grid > input,
    .form-grid > select,
    .form-grid > textarea {
        grid-column: 2;
        margin-top: var(--space-3);
    }

    .form-grid > .hint,
    .form-grid > .field-error,
    .form-grid > .error,
    .form-grid > button,
    .form-grid > a {
        grid-column: 2;
        justify-self: start;
    }

    /* Buttons and plain links at the end of the form stack in the control column, with a small gap between them */
    .form-grid > button + button,
    .form-grid > button + a,
    .form-grid > a + button,
    .form-grid > a + a {
        margin-top: var(--space-2);
    }
}

/* A plain link (e.g. "Quitar filtros") closing a form-grid; on narrow screens the grid is a plain column */
.form-grid > a:not(.button) {
    margin-top: var(--space-3);
    font-size: var(--text-sm);
}

/* Single closing action outside a form (one-time value pages, finished invitation, new recovery codes) */
p.actions {
    margin: var(--space-4) 0 0;
}

/* ---- Machine values (monospace) ---- */
.secret,
.one-time {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
}

.secret {
    display: block;
    padding: var(--space-3);
    border: var(--hairline) solid var(--rule);
    border-radius: var(--radius);
    background: var(--bg);
    letter-spacing: 0.05em;
    text-align: center;
    word-break: break-all;
}

/* A one-time value shown in a read-only input (client secret, invitation link): bordered in ink, selected in one click */
input.one-time {
    padding: var(--space-3) var(--space-4);
    border-color: var(--ink);
    user-select: all;
}

input.one-time + .hint {
    margin-bottom: var(--space-4);
}

/* One-time codes: a grid of selectable cells (recovery codes) */
.codes,
.recovery-codes {
    margin: var(--space-4) 0;
    padding: 0;
    list-style: none;
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
}

.codes {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(var(--code-min), 1fr));
    gap: var(--space-2);
    font-size: var(--text-lg);
}

.codes li {
    padding: var(--space-2);
    border: var(--hairline) solid var(--rule);
    border-radius: var(--radius);
    background: var(--bg);
    text-align: center;
    user-select: all;
}

.recovery-codes {
    columns: 2;
}

.recovery-codes li {
    padding: var(--space-1) 0;
    user-select: all;
}

/* QR code: drawn black on white in both schemes, on a white quiet zone */
.qr {
    display: block;
    width: var(--qr-size);
    height: var(--qr-size);
    margin: var(--space-4) auto;
    padding: var(--space-2);
    border-radius: var(--radius);
    background: var(--qr-bg);
}

.qr svg {
    display: block;
    width: 100%;
    height: 100%;
}

/* Code entry: ONE <input> whose cells are lines drawn with a gradient (CSS only, no images, no script).
   Numeric entry gets cells; the text variant (recovery codes) is a plain monospace field. */
.code-input {
    font-family: var(--font-mono);
    font-size: var(--text-xl);
    font-variant-numeric: tabular-nums;
    letter-spacing: 0.1em;
    text-align: center;
}

.code-input[inputmode="numeric"] {
    --cell: calc(var(--glyph-mono) + var(--code-gap));
    width: calc(var(--code-cells) * var(--cell) + 2 * var(--code-pad));
    max-width: 100%;
    margin-inline: auto;
    padding-inline: var(--code-pad);
    border-width: 0;
    background-color: transparent;
    background-image: repeating-linear-gradient(90deg, var(--control-border) 0 var(--glyph-mono), transparent var(--glyph-mono) var(--cell));
    background-size: calc(var(--code-cells) * var(--cell)) var(--cell-line);
    background-position: var(--code-pad) calc(100% - var(--cell-lift));
    background-repeat: no-repeat;
    font-size: var(--text-2xl);
    letter-spacing: var(--code-gap);
    text-align: left;
}

/* ---- Password confirmation dialog (fragments/password-confirm.html) ---- */
.confirm-dialog {
    width: min(var(--dialog-width), calc(100vw - 2 * var(--space-4)));
    padding: var(--space-5);
    border: var(--hairline) solid var(--rule);
    border-radius: var(--radius-lg);
    background: var(--surface);
    color: var(--ink);
    box-shadow: var(--shadow-dialog);
    overscroll-behavior: contain;
}

.confirm-dialog::backdrop {
    background: var(--backdrop);
}

.confirm-dialog h2 {
    margin-top: 0;
    padding-right: var(--space-6);
}

.confirm-dialog .confirm-actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-3);
    margin-top: var(--space-4);
}

.confirm-dialog .confirm-actions button {
    flex: 1 1 var(--action-min);
    margin: 0;
}

.confirm-dialog .error[hidden] {
    display: none;
}

/* Close button (x) at the top right */
.confirm-dialog .confirm-close {
    position: absolute;
    top: var(--space-2);
    right: var(--space-2);
    width: var(--control-height);
    height: var(--control-height);
    min-height: 0;
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--ink);
    font-size: var(--text-xl);
    line-height: 1;
}

.confirm-dialog .confirm-close:hover {
    background: var(--hover);
}

/* ---- Floating notices (api.js): fixed at the top right, they take no space in the page flow ---- */
.app-notices {
    position: fixed;
    top: var(--space-4);
    right: var(--space-4);
    z-index: 100;
    max-width: var(--notice-width);
    pointer-events: none;
}

.app-notice {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    margin-bottom: var(--space-2);
    padding: var(--space-3) var(--space-2) var(--space-3) var(--space-4);
    border-left: var(--marker) solid var(--ink-soft);
    border-radius: var(--radius);
    background: var(--surface);
    color: var(--ink);
    box-shadow: var(--shadow-float);
    overflow-wrap: anywhere;
    pointer-events: auto;
    animation: app-notice-in var(--duration) var(--easing);
}

.app-notice > span {
    flex: 1 1 auto;
}

#app-status .app-notice {
    border-left-color: var(--ok);
    background: var(--ok-bg);
}

#app-alert .app-notice {
    border-left-color: var(--error);
    background: var(--error-bg);
}

/* The close button is a plain cross: it does not take the button styles */
.app-notice .app-notice-close {
    flex: none;
    min-height: 0;
    margin: 0;
    padding: 0 var(--space-2);
    border: 0;
    background: transparent;
    color: inherit;
    font-size: var(--text-lg);
    line-height: var(--leading);
}

.app-notice .app-notice-close:hover {
    background: var(--hover);
}

@keyframes app-notice-in {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@media (max-width: 48rem) {
    .app-notices {
        left: var(--space-4);
        max-width: none;
    }
}
