/*
 * Mobile first: the phone is the main screen. Tokens are named by role; light and dark are both
 * full modes. Everything tappable is at least 44 px, inputs are 16 px so iOS does not zoom.
 */
:root {
    color-scheme: light dark;
    --background: #f5f6f8;
    --surface: #ffffff;
    --surface-pressed: #eef0f3;
    --border: #dcdfe5;
    --border-strong: #8a919c;
    --text: #1a1d22;
    --text-muted: #5c6470;
    --accent: #1b5e8c;
    --accent-pressed: #154b70;
    --on-accent: #ffffff;
    --danger: #b3261e;
    --danger-surface: #fbeaea;
    --ok: #1b6e3c;
    --ok-surface: #e6f4ea;
    --warn: #8a5300;
    --warn-surface: #fdf1dd;
    --info: #1b5e8c;
    --info-surface: #e6f0f7;
    --muted-surface: #eceef1;
    --tabbar-height: 60px;
    --radius: 12px;
    --shadow: 0 1px 2px rgb(0 0 0 / 6%);
}

@media (prefers-color-scheme: dark) {
    :root {
        --background: #14171c;
        --surface: #1e2229;
        --surface-pressed: #292e37;
        --border: #333a45;
        --border-strong: #7c8591;
        --text: #eceef1;
        --text-muted: #a4acb8;
        --accent: #6db3e4;
        --accent-pressed: #8cc4eb;
        --on-accent: #0c1a24;
        --danger: #ff8a80;
        --danger-surface: #3b1f1e;
        --ok: #7fd49c;
        --ok-surface: #1c3325;
        --warn: #f0b95e;
        --warn-surface: #38290f;
        --info: #8cc4eb;
        --info-surface: #1b2e3c;
        --muted-surface: #2a2f38;
        --shadow: none;
    }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--background);
    color: var(--text);
    font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
    overflow-wrap: anywhere;
}

a { color: var(--accent); }

h1 { font-size: 1.4rem; line-height: 1.25; margin: 0; }
h2 { font-size: 1.05rem; margin: 0 0 .5rem; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.faint { color: var(--text-muted); font-size: .875rem; }

/* ---------------------------------------------------------------- layout */

.main {
    max-width: 720px;
    margin: 0 auto;
    padding: 16px 16px calc(var(--tabbar-height) + 96px + env(safe-area-inset-bottom));
}

.body--bare .main { padding-bottom: 32px; }

.page-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 16px;
}
.page-head > :first-child { min-width: 0; }
.page-head .button { flex: none; }

.back {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    margin-top: -8px;
    color: var(--text-muted);
    font-size: .875rem;
    text-decoration: none;
}
.back::before { content: "‹"; font-size: 1.3em; margin-right: 6px; line-height: 1; }

.section { margin-top: 24px; }
.section__head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.section__head h2 { margin: 0; }
.section--quiet { display: flex; flex-wrap: wrap; gap: 8px; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
}
.form > .card + .card, .card + .card, .card + .form { margin-top: 12px; }

.empty { color: var(--text-muted); margin: 8px 0; }
.more-link { margin: 16px 0 0; font-size: .9rem; text-align: center; }

/* ---------------------------------------------------------------- navigation */

.tabs {
    position: fixed;
    inset: auto 0 0 0;
    z-index: 20;
    display: flex;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
}
.tabs__brand, .tabs__env { display: none; }
.tabs__item {
    flex: 1 1 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    min-height: var(--tabbar-height);
    color: var(--text-muted);
    font-size: .72rem;
    text-decoration: none;
}
.tabs__item svg {
    width: 24px;
    height: 24px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}
.tabs__item.is-active { color: var(--accent); font-weight: 600; }

/* ---------------------------------------------------------------- lists */

.list {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}
.row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    min-height: 60px;
    padding: 10px 14px;
    color: inherit;
    text-decoration: none;
}
.row + .row, dialog + .row { border-top: 1px solid var(--border); }
a.row:active { background: var(--surface-pressed); }
.row__main { display: flex; flex-direction: column; min-width: 0; }
.row__side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex: none; text-align: right; }
.amount { white-space: nowrap; font-variant-numeric: tabular-nums; }
.card-link { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }

.chip {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: .75rem;
    font-weight: 600;
    white-space: nowrap;
}
.chip--ok { background: var(--ok-surface); color: var(--ok); }
.chip--warn { background: var(--warn-surface); color: var(--warn); }
.chip--danger { background: var(--danger-surface); color: var(--danger); }
.chip--info { background: var(--info-surface); color: var(--info); }
.chip--muted { background: var(--muted-surface); color: var(--text-muted); }

.chips {
    display: flex;
    gap: 8px;
    margin: 0 -16px 12px;
    padding: 0 16px 4px;
    overflow-x: auto;
    scrollbar-width: none;
}
.filter {
    display: inline-flex;
    align-items: center;
    min-height: 44px;
    padding: 0 14px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text);
    font-size: .875rem;
    text-decoration: none;
    white-space: nowrap;
}
.filter.is-active { background: var(--accent); border-color: var(--accent); color: var(--on-accent); font-weight: 600; }

.search { display: flex; gap: 8px; margin-bottom: 12px; }
.search input { flex: 1; min-width: 0; }

/* ---------------------------------------------------------------- overview */

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.stat {
    display: flex;
    flex-direction: column;
    padding: 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    color: inherit;
    text-decoration: none;
}
.stat__label { font-size: .85rem; color: var(--text-muted); }
.stat__value { font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.stat--danger { border-color: var(--danger); background: var(--danger-surface); }
.stat--danger .stat__label { color: var(--danger); }

/* ---------------------------------------------------------------- facts */

.facts { margin: 0; }
.facts > div {
    display: grid;
    grid-template-columns: 38% 1fr;
    gap: 8px;
    padding: 8px 0;
    border-top: 1px solid var(--border);
}
.facts > div:first-child { border-top: 0; padding-top: 0; }
.facts > div:last-child { padding-bottom: 0; }
.facts dt { color: var(--text-muted); font-size: .875rem; }
.facts dd { margin: 0; min-width: 0; }

.summary__main { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; }
.summary__amount { font-size: 1.75rem; line-height: 1.2; font-variant-numeric: tabular-nums; }
.summary p { margin: 10px 0 0; }

.item { padding: 8px 0; border-bottom: 1px solid var(--border); }
.item:first-child { padding-top: 0; }
.item__numbers { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 2px 12px; margin-top: 2px; }
.item__numbers strong { color: var(--text); margin-left: auto; }

.totals { margin: 8px 0 0; }
.totals > div { display: flex; justify-content: space-between; padding: 2px 0; }
.totals dt { color: var(--text-muted); }
.totals dd { margin: 0; }
.totals__sum { font-weight: 700; font-size: 1.05rem; }
.totals__sum dt { color: var(--text); }

/* ---------------------------------------------------------------- forms */

.form .card > h2 { margin-bottom: 12px; }

.field { margin: 0 0 14px; padding: 0; border: 0; min-width: 0; }
.field:last-child { margin-bottom: 0; }
.field label, .field legend { display: block; margin-bottom: 4px; padding: 0; font-size: .9rem; font-weight: 600; }
.field__required { color: var(--text-muted); font-weight: 400; font-size: .8rem; }
.field__hint { margin: 4px 0 0; color: var(--text-muted); font-size: .85rem; }
.field__error { margin: 4px 0 0; color: var(--danger); font-size: .875rem; font-weight: 600; }
.field-row { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 3fr); gap: 12px; }
.field-row > .field { margin-bottom: 14px; }
[data-show-when]:not(:last-child) { margin-bottom: 14px; }
[hidden] { display: none !important; }

input[type="text"], input[type="email"], input[type="tel"], input[type="date"], input[type="number"], input[type="search"], select, textarea {
    width: 100%;
    min-height: 46px;
    padding: 10px 12px;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border-strong);
    border-radius: 10px;
    font: inherit;
}
input[aria-invalid="true"] { border-color: var(--danger); border-width: 2px; }
textarea { resize: vertical; }

.segmented {
    display: flex;
    gap: 2px;
    padding: 2px;
    background: var(--muted-surface);
    border-radius: 11px;
}
.segmented__option { flex: 1 1 0; margin: 0 !important; font-weight: 500 !important; cursor: pointer; }
.segmented__option input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.segmented__option span {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 4px 8px;
    border-radius: 9px;
    text-align: center;
    font-size: .95rem;
}
.segmented__option input:checked + span { background: var(--surface); color: var(--accent); font-weight: 700; box-shadow: 0 1px 3px rgb(0 0 0 / 20%); }
.segmented__option input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: 1px; }

.check { display: flex; align-items: center; gap: 12px; min-height: 44px; }
.check input { width: 22px; height: 22px; flex: none; accent-color: var(--accent); }

.switch-row { padding: 10px 0; border-top: 1px solid var(--border); }
.card > h2 + .switch-row { border-top: 0; padding-top: 0; }
.switch-row:last-child { padding-bottom: 0; }
.switch { display: flex; justify-content: space-between; align-items: center; gap: 16px; min-height: 44px; cursor: pointer; }
.switch__text { display: flex; flex-direction: column; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch__track {
    position: relative;
    flex: none;
    width: 52px;
    height: 32px;
    border-radius: 16px;
    background: var(--border-strong);
    transition: background .2s;
}
.switch__track::after {
    content: "";
    position: absolute;
    top: 3px;
    left: 3px;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 3px rgb(0 0 0 / 35%);
    transition: transform .2s;
}
.switch input:checked + .switch__track { background: var(--accent); }
.switch input:checked + .switch__track::after { transform: translateX(20px); }
.switch input:focus-visible + .switch__track { outline: 3px solid var(--accent); outline-offset: 2px; }

.inline-form .button { margin-top: 2px; }

.disclosure { margin-top: 10px; }
.disclosure > summary { display: flex; align-items: center; min-height: 44px; color: var(--accent); cursor: pointer; }
.disclosure form .button { margin-top: 8px; }

.price-preview { margin: -6px 0 14px; color: var(--text-muted); font-size: .9rem; }

/* ---------------------------------------------------------------- buttons */

.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 44px;
    padding: 8px 16px;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border-strong);
    border-radius: 10px;
    font: inherit;
    font-weight: 600;
    text-decoration: none;
    cursor: pointer;
}
.button:active { background: var(--surface-pressed); }
.button--primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.button--primary:active { background: var(--accent-pressed); }
.button--danger { background: var(--danger); border-color: var(--danger); color: var(--surface); }
.button--block { display: flex; width: 100%; }
.button--small { min-height: 44px; padding: 4px 12px; font-size: .875rem; }
.button[aria-busy="true"] { opacity: .75; cursor: progress; }
.button[aria-busy="true"]::before {
    content: "";
    width: 16px;
    height: 16px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.link-danger {
    min-height: 44px;
    margin: -10px 0;
    padding: 0;
    background: none;
    border: 0;
    color: var(--danger);
    font: inherit;
    font-size: .85rem;
    text-decoration: underline;
    cursor: pointer;
}

/* The main action of a screen, within reach of the thumb, just above the tab bar. */
.actionbar {
    position: fixed;
    inset: auto 0 calc(var(--tabbar-height) + env(safe-area-inset-bottom)) 0;
    z-index: 10;
    padding: 10px 16px;
    background: var(--surface);
    border-top: 1px solid var(--border);
}

/* ---------------------------------------------------------------- messages */

.alert {
    margin: 0 0 12px;
    padding: 10px 14px;
    background: var(--warn-surface);
    color: var(--text);
    border-radius: 10px;
}
.alert--error { background: var(--danger-surface); color: var(--danger); font-weight: 600; }
.alert--ok { background: var(--ok-surface); color: var(--ok); font-weight: 600; }

.toasts {
    position: fixed;
    inset: 12px 12px auto 12px;
    z-index: 50;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    pointer-events: none;
}
.toast {
    padding: 10px 16px;
    background: var(--ok);
    color: var(--surface);
    border-radius: 10px;
    font-weight: 600;
    box-shadow: 0 4px 16px rgb(0 0 0 / 25%);
}
.toast::before { content: "✓ "; }

.dialog {
    width: min(480px, calc(100vw - 32px));
    max-height: calc(100dvh - 32px);
    padding: 20px;
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 16px;
}
.dialog::backdrop { background: rgb(0 0 0 / 55%); }
.dialog h2 { font-size: 1.15rem; margin: 0 0 8px; }
.dialog p { margin: 0 0 14px; }
.dialog__actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.dialog__actions .button { flex: 1 1 auto; }

.login { max-width: 360px; margin: 18vh auto 0; text-align: center; }
.login h1 { font-size: 1.6rem; }
.login form, .login .button { margin-top: 12px; }

@media (prefers-reduced-motion: reduce) {
    .switch__track, .switch__track::after { transition: none; }
    .button[aria-busy="true"]::before { animation: none; }
}

/* ---------------------------------------------------------------- larger screens */

@media (min-width: 900px) {
    .tabs {
        position: sticky;
        inset: 0 auto auto 0;
        justify-content: center;
        align-items: center;
        gap: 4px;
        border-top: 0;
        border-bottom: 1px solid var(--border);
        padding: 0 16px;
    }
    .tabs__brand { display: block; margin-right: 16px; font-weight: 700; color: var(--text); text-decoration: none; }
    .tabs__env { display: block; margin-left: 12px; padding: 2px 8px; border-radius: 999px; background: var(--warn-surface); color: var(--warn); font-size: .75rem; font-weight: 600; }
    .tabs__item { flex: none; flex-direction: row; gap: 6px; padding: 0 14px; min-height: 52px; font-size: .95rem; }
    .tabs__item svg { width: 20px; height: 20px; }
    .main { padding-bottom: 48px; }
    .actionbar { position: static; margin-top: 16px; padding: 0; background: none; border: 0; }
    .actionbar .button--block { display: inline-flex; width: auto; }
    .chips { margin-inline: 0; padding-inline: 0; flex-wrap: wrap; overflow: visible; }
}
