/* Shared components (see docs/frontend.md, "Components") */

/* ------------------------------------------------------------------------- */
/* Layout                                                                    */
/* ------------------------------------------------------------------------- */

.app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.app__main {
    flex: 1 0 auto;
}

.container {
    width: 100%;
    max-width: var(--width-default);
    margin-inline: auto;
    padding-inline: var(--space-5);
}

.container--narrow { max-width: var(--width-narrow); }
.container--wide { max-width: var(--width-wide); }

.page {
    padding-block: var(--space-6) var(--space-8);
}

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

.stack--tight {
    gap: var(--space-3);
}

.cluster {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-2);
}

.page-header {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: space-between;
    gap: var(--space-4);
    margin-bottom: var(--space-5);
}

/* The page's stack already spaces it */
.stack > .page-header {
    margin-bottom: 0;
}

.page-header__eyebrow {
    margin-bottom: var(--space-1);
    color: var(--color-text-muted);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
}

.page-header__subtitle {
    margin-top: var(--space-1);
    color: var(--color-text-muted);
}

/* ------------------------------------------------------------------------- */
/* Top bar and footer                                                        */
/* ------------------------------------------------------------------------- */

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    background: color-mix(in srgb, var(--color-surface) 92%, transparent);
    backdrop-filter: blur(8px);
    border-bottom: 1px solid var(--color-border);
}

.topbar__inner {
    display: flex;
    align-items: center;
    gap: var(--space-5);
    height: var(--topbar-height);
}

.topbar__actions {
    display: flex;
    align-items: center;
    gap: var(--space-1);
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    color: var(--color-text);
    font-weight: var(--weight-semibold);
    font-size: var(--text-lg);
    letter-spacing: -0.01em;
    text-decoration: none;
}

.brand:hover {
    color: var(--color-text);
}

.brand__logo {
    width: 1.75rem;
    height: 1.75rem;
}

.topbar__nav {
    display: flex;
    gap: var(--space-1);
    margin-right: auto;
}

.topbar__link {
    padding: var(--space-2) var(--space-3);
    white-space: nowrap;
    border-radius: var(--radius-sm);
    color: var(--color-text-muted);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    text-decoration: none;
}

.topbar__link:hover,
.topbar__link[aria-current="page"] {
    background: var(--color-surface-muted);
    color: var(--color-text);
}

.footer {
    padding-block: var(--space-5);
    border-top: 1px solid var(--color-border);
    color: var(--color-text-muted);
    font-size: var(--text-sm);
}

.footer__inner {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: var(--space-2);
}

/* Theme button: sun in the dark theme (to switch to light), moon in the light theme; hidden without JS */
:root:not([data-theme]) .theme-toggle,
:root[data-theme="dark"] .theme-toggle__to-dark,
:root[data-theme="light"] .theme-toggle__to-light {
    display: none;
}

/* Link to skip the top bar, shown when focused with the keyboard */
.skip-link {
    position: absolute;
    top: var(--space-2);
    left: var(--space-2);
    z-index: 50;
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-sm);
    background: var(--color-accent);
    color: var(--color-on-accent);
    transform: translateY(-200%);
}

.skip-link:focus {
    transform: none;
}

/* Menu: a <details> showing a small panel (user menu, confirmations) */
.menu {
    position: relative;
}

.menu > summary {
    list-style: none;
}

.menu > summary::-webkit-details-marker {
    display: none;
}

.menu__panel {
    position: absolute;
    right: 0;
    top: calc(100% + var(--space-2));
    z-index: 10;
    min-width: 13rem;
    padding: var(--space-2);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
}

.menu__header {
    padding: var(--space-2) var(--space-3);
    color: var(--color-text-muted);
    font-size: var(--text-sm);
}

.menu__item {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    width: 100%;
    padding: var(--space-2) var(--space-3);
    border: 0;
    border-radius: var(--radius-sm);
    background: none;
    color: var(--color-text);
    font: inherit;
    font-size: var(--text-sm);
    text-align: left;
    cursor: pointer;
}

.menu__item:hover {
    background: var(--color-surface-muted);
}

.menu__item--danger {
    color: var(--color-danger);
}

.menu__item--danger:hover {
    background: var(--color-danger-soft);
}

.login__header {
    display: grid;
    justify-items: center;
    gap: var(--space-2);
    text-align: center;
}

.login__logo {
    width: 3rem;
    height: 3rem;
}

/* ------------------------------------------------------------------------- */
/* Icons                                                                     */
/* ------------------------------------------------------------------------- */

.icon {
    width: 1.25em;
    height: 1.25em;
    flex-shrink: 0;
    vertical-align: -0.25em;
}

.icon--sm { width: 1rem; height: 1rem; }
.icon--lg { width: 2rem; height: 2rem; }

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

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: 2.5rem;
    padding: var(--space-2) var(--space-4);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    background: none;
    color: var(--color-text);
    font: inherit;
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    line-height: 1.2;
    text-decoration: none;
    white-space: nowrap;
    cursor: pointer;
    transition: background-color var(--transition), border-color var(--transition), color var(--transition);
}

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

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

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

.btn--secondary:hover {
    background: var(--color-surface-muted);
    color: var(--color-text);
}

.btn--ghost {
    color: var(--color-text-muted);
}

.btn--ghost:hover {
    background: var(--color-surface-muted);
    color: var(--color-text);
}

.btn--danger {
    background: var(--color-surface);
    border-color: color-mix(in srgb, var(--color-danger) 40%, transparent);
    color: var(--color-danger);
}

.btn--danger:hover {
    background: var(--color-danger-soft);
    color: var(--color-danger);
}

.btn--sm {
    min-height: 2rem;
    padding: var(--space-1) var(--space-3);
}

.btn--lg {
    min-height: 3rem;
    padding: var(--space-3) var(--space-5);
    font-size: var(--text-base);
}

.btn--block {
    width: 100%;
}

/* Square button containing only an icon (with a tooltip or a visually hidden label) */
.btn--icon {
    width: 2.5rem;
    padding: 0;
}

.btn--icon.btn--sm {
    width: 2rem;
}

.btn:disabled,
.btn.is-disabled {
    opacity: 0.4;
    pointer-events: none;
}

/* Button of a POST form displayed as a link (logout, delete, ...) */
.link-button {
    padding: 0;
    border: 0;
    background: none;
    color: var(--color-accent);
    font: inherit;
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
    cursor: pointer;
}

.link-button:hover {
    color: var(--color-accent-hover);
}

/* ------------------------------------------------------------------------- */
/* Cards                                                                     */
/* ------------------------------------------------------------------------- */

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

.card__header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid var(--color-border);
}

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

.card__subtitle {
    color: var(--color-text-muted);
    font-size: var(--text-sm);
}

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

/* Card linking to a tool (homepage) */
.tool-card {
    display: flex;
    align-items: center;
    gap: var(--space-4);
    padding: var(--space-5);
    color: var(--color-text);
    text-decoration: none;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.tool-card:hover {
    border-color: var(--color-accent);
    box-shadow: var(--shadow-md);
    color: var(--color-text);
}

.tool-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(20rem, 1fr));
    gap: var(--space-4);
}

.tool-card__icon {
    display: grid;
    place-items: center;
    width: 3rem;
    height: 3rem;
    border-radius: var(--radius);
    background: var(--color-accent-soft);
    color: var(--color-accent);
}

.tool-card__text {
    flex: 1;
}

.tool-card__description {
    color: var(--color-text-muted);
    font-size: var(--text-sm);
}

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

.field {
    display: grid;
    gap: var(--space-2);
}

.field__label {
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
}

.input {
    width: 100%;
    min-height: 2.5rem;
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-sm);
    background: var(--color-surface);
    color: var(--color-text);
    font: inherit;
    transition: border-color var(--transition), box-shadow var(--transition);
}

.input:focus {
    outline: none;
    border-color: var(--color-accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-accent) 25%, transparent);
}


.field__error {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    color: var(--color-danger);
    font-size: var(--text-sm);
}

.field--invalid .input,
.field--invalid .dropzone {
    border-color: var(--color-danger);
}

input[type="checkbox"] {
    width: 1rem;
    height: 1rem;
    margin: 0;
    accent-color: var(--color-accent);
    cursor: pointer;
}

/* Drop zone: the file input covers the whole zone, invisible */
.dropzone {
    position: relative;
    display: grid;
    justify-items: center;
    gap: var(--space-2);
    padding: var(--space-7) var(--space-5);
    border: 2px dashed var(--color-border-strong);
    border-radius: var(--radius);
    background: var(--color-surface-muted);
    text-align: center;
    transition: border-color var(--transition), background-color var(--transition);
}

.dropzone:hover,
.dropzone.is-dragover,
.dropzone:focus-within {
    border-color: var(--color-accent);
    background: var(--color-accent-soft);
}

.dropzone input[type="file"] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
}

.dropzone__icon {
    color: var(--color-accent);
}

.dropzone__title {
    font-weight: var(--weight-semibold);
}

.dropzone__hint {
    color: var(--color-text-muted);
    font-size: var(--text-sm);
}

.file-list {
    display: grid;
    gap: var(--space-2);
    margin: 0;
    padding: 0;
    list-style: none;
}

.file-list__item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-surface);
    font-size: var(--text-sm);
}

.file-list__name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-list__size {
    color: var(--color-text-muted);
}

.file-list:empty {
    display: none;
}

/* ------------------------------------------------------------------------- */
/* Alerts and badges                                                         */
/* ------------------------------------------------------------------------- */

.alert {
    display: flex;
    gap: var(--space-3);
    padding: var(--space-4);
    border: 1px solid transparent;
    border-radius: var(--radius);
    font-size: var(--text-sm);
}

.alert__title {
    font-weight: var(--weight-semibold);
}

.alert__body {
    display: grid;
    gap: var(--space-1);
    min-width: 0;
}

.alert__details {
    max-height: 20rem;
    margin: var(--space-2) 0 0;
    padding: var(--space-3);
    overflow: auto;
    border-radius: var(--radius-sm);
    background: var(--color-surface);
    font-size: var(--text-xs);
    white-space: pre;
}

.alert--info {
    background: var(--color-accent-soft);
    border-color: color-mix(in srgb, var(--color-accent) 30%, transparent);
}

.alert--info > .icon { color: var(--color-accent); }

.alert--warning {
    background: var(--color-warning-soft);
    border-color: color-mix(in srgb, var(--color-warning) 30%, transparent);
}

.alert--warning > .icon { color: var(--color-warning); }

.alert--success {
    background: var(--color-credit-soft);
    border-color: color-mix(in srgb, var(--color-credit) 30%, transparent);
}

.alert--success > .icon { color: var(--color-credit); }

.alert--error {
    background: var(--color-danger-soft);
    border-color: color-mix(in srgb, var(--color-danger) 30%, transparent);
}

.alert--error > .icon { color: var(--color-danger); }

.badge {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    padding: 0.125rem var(--space-2);
    border-radius: 999px;
    background: var(--color-surface-muted);
    color: var(--color-text-muted);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    white-space: nowrap;
}

.badge--success { background: var(--color-credit-soft); color: var(--color-credit); }
.badge--danger { background: var(--color-danger-soft); color: var(--color-danger); }
.badge--accent { background: var(--color-accent-soft); color: var(--color-accent); }

/* ------------------------------------------------------------------------- */
/* Summary figures                                                           */
/* ------------------------------------------------------------------------- */

.stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(13rem, 1fr));
    gap: var(--space-4);
}

.stat {
    display: grid;
    align-content: start;
    gap: var(--space-1);
    padding: var(--space-4) var(--space-5);
}

.stat__label {
    color: var(--color-text-muted);
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
}

.stat__value {
    font-size: var(--text-2xl);
    font-weight: var(--weight-semibold);
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.02em;
}

.stat__total {
    color: var(--color-text-muted);
    font-size: var(--text-lg);
}

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

/*
 * Highlighted figure, with an accent gradient: from the top in the dark theme (accent-soft is lighter than the card),
 * from the bottom in the light theme (accent-soft is darker, and a tint at the top looks like a shadow). Each stop is a
 * light-dark() pair, as a gradient's direction can't depend on the theme; --stat-glow is the colour 30% into the fade.
 */
.stat--accent {
    --stat-glow: color-mix(in srgb, var(--color-accent-soft) 57%, var(--color-surface));
    border-color: color-mix(in srgb, var(--color-accent) 35%, var(--color-border));
    background: linear-gradient(
        light-dark(var(--color-surface), var(--color-accent-soft)),
        light-dark(var(--color-surface), var(--stat-glow)) 30%,
        light-dark(var(--stat-glow), var(--color-surface)) 70%,
        light-dark(var(--color-accent-soft), var(--color-surface))
    );
}

.stat--accent .stat__value {
    color: var(--color-accent);
}

/* ------------------------------------------------------------------------- */
/* Data tables                                                               */
/* ------------------------------------------------------------------------- */

.table-wrap {
    container: table / inline-size;
    overflow-x: auto;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    background: var(--color-surface);
}

/* Table whose header sticks under the top bar while the page scrolls: it can't scroll sideways, so its table must fit */
.table-wrap--sticky {
    overflow: clip;
}

.table-wrap--sticky .data-table th {
    top: var(--topbar-height);
    z-index: 1;
}

/* Table filling the bottom of a card */
.table-wrap--flush {
    border: 0;
    border-radius: 0 0 var(--radius) var(--radius);
}

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

.data-table th {
    position: sticky;
    top: 0;
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid var(--color-border);
    background: var(--color-surface-muted);
    color: var(--color-text-muted);
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    letter-spacing: 0.02em;
    text-align: left;
    text-transform: uppercase;
    white-space: nowrap;
}

.data-table td {
    padding: var(--space-3);
    border-bottom: 1px solid var(--color-border);
    vertical-align: top;
}

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

.data-table tbody tr {
    transition: background-color var(--transition);
}

.data-table tbody tr:hover {
    background: var(--color-surface-muted);
}

.data-table tbody tr.is-selected {
    background: var(--color-accent-soft);
}

/* Row just restored by an undo, or added by hand */
.data-table tbody tr.is-restored {
    box-shadow: inset 3px 0 0 var(--color-warning);
    background: var(--color-warning-soft);
}

.data-table .col-check {
    width: 2.75rem;
    text-align: center;
}

.data-table label {
    display: inline-flex;
    align-items: center;
    padding: var(--space-1);
    cursor: pointer;
}

.data-table .col-actions {
    width: 1%;
    text-align: right;
}

.data-table .num {
    text-align: right;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.data-table .debit { color: var(--color-debit); }
.data-table .credit { color: var(--color-credit); }

.data-table .nowrap {
    white-space: nowrap;
}

/* ------------------------------------------------------------------------- */
/* Selection bar: as wide as its parent, stuck to the bottom of the screen   */
/* while its parent is scrolled                                              */
/* ------------------------------------------------------------------------- */

.selection-bar {
    position: sticky;
    bottom: var(--space-5);
    z-index: 30;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3) var(--space-5);
    padding: var(--space-3) var(--space-3) var(--space-3) var(--space-5);
    border-radius: var(--radius-lg);
    background: var(--color-text);
    color: var(--color-bg);
    box-shadow: var(--shadow-lg);
}

.selection-bar__text {
    flex: 1;
    min-width: 10rem;
    font-size: var(--text-sm);
}

.selection-bar__sum {
    font-weight: var(--weight-semibold);
    font-variant-numeric: tabular-nums;
}

.selection-bar .btn {
    color: inherit;
}

.selection-bar .btn:hover {
    background: color-mix(in srgb, var(--color-bg) 15%, transparent);
}

.selection-bar :focus-visible {
    outline-color: var(--color-bg);
}

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

/* ------------------------------------------------------------------------- */
/* Disclosures                                                               */
/* ------------------------------------------------------------------------- */

.disclosure > summary {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    cursor: pointer;
    list-style: none;
}

.disclosure > summary::-webkit-details-marker {
    display: none;
}

.disclosure > summary .disclosure__chevron {
    color: var(--color-text-muted);
    transition: transform var(--transition);
}

.disclosure[open] > summary .disclosure__chevron {
    transform: rotate(180deg);
}

/* A closed card disclosure is only its header */
.disclosure:not([open]) > .card__header {
    border-bottom: 0;
}


/* ------------------------------------------------------------------------- */
/* Tooltips: data-tooltip on any element                                     */
/* ------------------------------------------------------------------------- */

[data-tooltip] {
    position: relative;
}

/* Only rendered on hover/focus, so that hidden tooltips never widen the page */
[data-tooltip]::after {
    display: none;
    content: attr(data-tooltip);
    position: absolute;
    left: 50%;
    bottom: calc(100% + var(--space-2));
    z-index: 40;
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
    background: var(--color-text);
    color: var(--color-bg);
    font-size: var(--text-xs);
    font-weight: var(--weight-medium);
    white-space: nowrap;
    pointer-events: none;
    transform: translateX(-50%);
}

[data-tooltip]:hover::after,
[data-tooltip]:focus-visible::after {
    display: block;
}

/* Tooltip aligned on the right edge of the element (elements at the right of the screen) */
[data-tooltip-align="end"]::after {
    left: auto;
    right: 0;
    transform: none;
}

/* Tooltip under the element (e.g. in the selection bar at the bottom of the screen, above is fine) */
[data-tooltip-position="bottom"]::after {
    top: calc(100% + var(--space-2));
    bottom: auto;
}

@container table (max-width: 40rem) {
    /*
     * Responsive data table, when its frame is narrower than 40rem (small screens, side panels): each row becomes a two-line card,
     *     reference          amount
     *     operation          date
     * with a sign before the amount, as the debit and credit columns are gone.
     */
    /* Only the "select all" checkbox of the header is kept, with its label */
    .data-table--responsive thead:not(:has(input)),
    .data-table--responsive thead th:not(.col-check) {
        display: none;
    }

    .data-table--responsive thead th.col-check {
        display: block;
        width: auto;
        padding: var(--space-1) var(--space-4);
        text-align: left;
        text-transform: none;
    }

    .data-table--responsive thead .visually-hidden {
        position: static;
        width: auto;
        height: auto;
        margin-left: var(--space-2);
        clip-path: none;
    }

    .data-table--responsive tbody tr {
        display: grid;
        grid-template-columns: auto 1fr auto;
        column-gap: var(--space-3);
        row-gap: var(--space-1);
        padding: var(--space-3) var(--space-4);
        border-bottom: 1px solid var(--color-border);
    }

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

    .data-table--responsive td {
        padding: 0;
        border: 0;
    }

    .data-table--responsive .col-check { grid-row: 1 / span 2; grid-column: 1; width: auto; }
    .data-table--responsive .col-ref { grid-row: 1; grid-column: 2; font-weight: var(--weight-medium); }
    .data-table--responsive .num { grid-row: 1; grid-column: 3; font-weight: var(--weight-semibold); }
    .data-table--responsive .num:empty { display: none; }
    .data-table--responsive .col-op { grid-row: 2; grid-column: 2; color: var(--color-text-muted); }
    .data-table--responsive .col-date { grid-row: 2; grid-column: 3; text-align: right; color: var(--color-text-muted); }

    .data-table--responsive .debit::before { content: "−\2009"; }
    .data-table--responsive .credit::before { content: "+\2009"; }
}

/* ------------------------------------------------------------------------- */
/* Print: light colours, no navigation or controls, flat cards               */
/* ------------------------------------------------------------------------- */

@media print {
    :root {
        color-scheme: light !important;
    }

    .topbar,
    .footer,
    .btn {
        display: none !important;
    }

    body {
        background: none;
    }

    .page {
        padding-block: 0;
    }

    .card {
        box-shadow: none;
    }

    .stat {
        break-inside: avoid;
    }

    .data-table th {
        position: static;
    }

    .data-table tr {
        break-inside: avoid;
    }
}

/* ------------------------------------------------------------------------- */
/* Small screens                                                             */
/* ------------------------------------------------------------------------- */

@media (max-width: 40rem) {
    .hide-small {
        display: none !important;
    }

    .container {
        padding-inline: var(--space-4);
    }

    .page {
        padding-block: var(--space-5) var(--space-7);
    }

    .topbar__inner {
        gap: var(--space-3);
    }

    .topbar__link {
        padding-inline: var(--space-2);
    }

    .card__header,
    .card__body {
        padding: var(--space-4);
    }

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