/*
   Mobile first. Die Erfassung passiert fast immer am Telefon, oft einhändig und im Stehen:
   grosse Flächen, Zahlenfelder mit numerischer Tastatur, Navigation am unteren Rand.
   Ab 900px wird daraus die Desktop-Ansicht für die Auswertung und die Abrechnung.
*/

:root {
    color-scheme: light dark;

    --bg: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #eceef1;
    --border: #d8dbe0;
    --text: #16181d;
    --muted: #61656e;
    --accent: #1f6feb;
    --accent-text: #ffffff;
    --danger: #c0392b;
    --warn: #a86a00;
    --success: #1a7f4b;
    --radius: 14px;
    --topbar-h: 56px;
    --tabbar-h: 60px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14161a;
        --surface: #1d2026;
        --surface-2: #262a31;
        --border: #31353d;
        --text: #eceef2;
        --muted: #9aa0ab;
        --accent: #4c8dff;
        --danger: #ff6b5c;
        --warn: #e0a137;
        --success: #4ec98a;
    }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    overscroll-behavior-y: none;
}

h1 { font-size: 1.5rem; margin: 0 0 16px; letter-spacing: -0.02em; }
h2 { font-size: 1.05rem; margin: 0 0 12px; letter-spacing: -0.01em; }
p { margin: 0 0 14px; }
a { color: var(--accent); }

.visually-hidden {
    position: absolute; width: 1px; height: 1px; overflow: hidden;
    clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}

/* ---------------------------------------------------------------- Gerüst */

.app { min-height: 100dvh; display: flex; flex-direction: column; }

.topbar {
    position: sticky; top: 0; z-index: 10;
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    height: calc(var(--topbar-h) + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) 16px 0;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 9px; text-decoration: none; color: inherit; }

.brand-mark {
    display: grid; place-items: center;
    width: 30px; height: 30px; border-radius: 8px;
    background: var(--accent); color: var(--accent-text);
    font-weight: 700; font-size: 12px; letter-spacing: 0.04em;
}

.brand-name { font-weight: 600; }

.topbar-status { display: flex; align-items: center; gap: 8px; }

.content {
    flex: 1;
    width: 100%;
    max-width: 62rem;
    margin: 0 auto;
    /* Platz für die Tableiste am unteren Rand, inklusive Home-Indicator. */
    padding: 20px 16px calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
}

/* ---------------------------------------------------------------- Navigation */

.tabbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
    display: flex;
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--surface);
    border-top: 1px solid var(--border);
}

.tab {
    flex: 1;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    min-height: var(--tabbar-h);
    text-decoration: none;
    color: var(--muted);
    font-size: 0.7rem;
}

.tab-icon { font-size: 1.05rem; line-height: 1; }
.tab.active { color: var(--accent); }

/* ---------------------------------------------------------------- Startseite */

.hero {
    display: flex; flex-direction: column; gap: 4px; align-items: flex-start;
    padding: 24px 20px;
    margin-bottom: 18px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.hero-label { font-size: 0.8rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.hero-value { font-size: 1.9rem; font-weight: 700; letter-spacing: -0.03em; }
.hero-sub { color: var(--muted); font-size: 0.92rem; }
.hero-idle .hero-value { color: var(--muted); }

.panel {
    padding: 18px 16px;
    margin-bottom: 18px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.summary {
    display: flex; align-items: baseline; justify-content: space-between;
    padding: 14px 0; margin-bottom: 6px;
    border-top: 1px solid var(--border); border-bottom: 1px solid var(--border);
    font-size: 1.3rem; font-weight: 600;
}

.summary-amount { color: var(--accent); }

/* ---------------------------------------------------------------- Formulare */

.field { margin-bottom: 16px; }
.field-check { display: flex; align-items: center; }

.field label {
    display: block; margin-bottom: 6px;
    font-size: 0.85rem; font-weight: 600; color: var(--muted);
}

.control {
    width: 100%;
    /* Unter 16px zoomt iOS beim Fokus in das Feld hinein. */
    font: inherit; font-size: 16px;
    padding: 12px 14px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--bg);
    color: var(--text);
}

.control:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.control-number { font-variant-numeric: tabular-nums; font-size: 1.3rem; font-weight: 600; }

.form-row { display: grid; gap: 0 16px; }

.btn {
    display: inline-flex; align-items: center; justify-content: center;
    /* 44px ist die Untergrenze, unter der Tippen am Telefon unzuverlässig wird. */
    min-height: 44px;
    padding: 11px 18px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface-2);
    color: var(--text);
    font: inherit; font-weight: 600;
    text-decoration: none;
    cursor: pointer;
}

.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn-danger { background: transparent; color: var(--danger); }
.btn-quiet { background: transparent; color: var(--muted); }
.btn-block { display: flex; width: 100%; margin-bottom: 10px; }

.btn-hero {
    display: flex; width: 100%;
    min-height: 68px;
    margin-bottom: 12px;
    font-size: 1.15rem;
}

.link { background: none; border: none; padding: 0; color: var(--accent); font: inherit; cursor: pointer; }

/* ---------------------------------------------------------------- Listen und Tabellen */

.list { list-style: none; margin: 0; padding: 0; }

.list-row {
    display: flex; align-items: flex-start; justify-content: space-between; gap: 14px;
    padding: 14px 16px; margin-bottom: 10px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

.row-main { min-width: 0; }
.row-title { font-weight: 600; }
.row-sub { font-size: 0.88rem; color: var(--text); }
.row-sub.muted { color: var(--muted); }
.row-side { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; flex-shrink: 0; }
.row-amount { font-weight: 600; font-variant-numeric: tabular-nums; }
.row-money { color: var(--accent); font-weight: 600; font-variant-numeric: tabular-nums; }
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.table th, .table td { padding: 10px 8px; text-align: left; border-bottom: 1px solid var(--border); }
.table th { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.col-num { text-align: right; font-variant-numeric: tabular-nums; }
.col-check { width: 2.4rem; }
.row-muted { opacity: 0.45; }

/* Breite Tabellen scrollen in sich selbst - die Seite darf nie horizontal wandern. */
.panel-wide { overflow-x: auto; }

.totals { font-size: 1.02rem; }
.total-row { display: flex; justify-content: space-between; padding: 9px 0; border-bottom: 1px solid var(--border); }
.total-net { font-size: 1.25rem; font-weight: 700; border-bottom: none; padding-top: 14px; }

.meta-grid { display: grid; gap: 10px; margin-bottom: 16px; }
.meta-label { display: block; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }

/* ---------------------------------------------------------------- Statusdarstellung */

.chip {
    display: inline-block;
    padding: 3px 9px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface-2);
    color: var(--muted);
    font-size: 0.74rem; font-weight: 600;
    cursor: default;
}

.chip-offline { color: var(--warn); border-color: var(--warn); }
.chip-pending { color: var(--accent); border-color: var(--accent); cursor: pointer; }

.badge {
    padding: 3px 9px;
    border-radius: 999px;
    background: var(--surface-2);
    font-size: 0.74rem; font-weight: 600;
    color: var(--muted);
}

.badge-active { color: var(--accent); }
.badge-completed, .badge-submitted { color: var(--warn); }
.badge-settled { color: var(--success); }
.badge-approved { color: var(--success); }
.badge-rejected, .badge-cancelled { color: var(--danger); }

.pill {
    padding: 7px 14px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface);
    color: var(--muted);
    font: inherit; font-size: 0.86rem; font-weight: 600;
    cursor: pointer;
}

.pill-active { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }

.banner { padding: 12px 16px; border-bottom: 1px solid var(--border); }
.banner-error { background: color-mix(in srgb, var(--danger) 12%, var(--surface)); }
.banner-success { background: color-mix(in srgb, var(--success) 12%, var(--surface)); }
.banner-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.error { color: var(--danger); }
.warn { color: var(--warn); }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.empty { color: var(--muted); font-style: italic; }

.notice { padding: 32px 0; text-align: center; }

/* ---------------------------------------------------------------- Kacheln */

.tiles { display: grid; gap: 12px; margin-bottom: 20px; }

.tile {
    display: flex; flex-direction: column; gap: 3px;
    padding: 18px 20px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    text-decoration: none; color: inherit;
}

.tile-value { font-size: 2rem; font-weight: 700; letter-spacing: -0.03em; }
.tile-label { color: var(--muted); font-size: 0.9rem; }
.tile-action { margin-top: 6px; color: var(--accent); font-weight: 600; font-size: 0.9rem; }
.tile-primary { border-color: var(--accent); }

.admin-links { display: flex; flex-wrap: wrap; gap: 16px; }

/* ---------------------------------------------------------------- Ziehen zum Aktualisieren */

.ptr {
    position: fixed;
    top: calc(var(--topbar-h) + env(safe-area-inset-top));
    left: 50%;
    z-index: 9;
    display: grid;
    place-items: center;
    width: 34px;
    height: 34px;
    margin-left: -17px;
    border: 1px solid var(--border);
    border-radius: 50%;
    background: var(--surface);
    opacity: 0;
    /* Startet oberhalb der Kante und wird beim Ziehen heruntergeschoben. */
    transform: translateY(0);
    margin-top: -42px;
    pointer-events: none;
}

.ptr-spinner {
    width: 16px;
    height: 16px;
    border: 2px solid var(--border);
    border-top-color: var(--muted);
    border-radius: 50%;
}

/* Ab der Auslöseschwelle: die Farbe signalisiert "jetzt loslassen". */
.ptr-armed .ptr-spinner { border-top-color: var(--accent); }
.ptr-spinning .ptr-spinner { animation: spin 0.8s linear infinite; border-top-color: var(--accent); }

@media (prefers-reduced-motion: reduce) {
    .ptr-spinning .ptr-spinner { animation-duration: 3s; }
}

/* ---------------------------------------------------------------- Audit-Log */

.table-audit { font-size: 0.86rem; }
.table-audit td { vertical-align: top; }
.nowrap { white-space: nowrap; }
.tiny { font-size: 0.72rem; }

/* Details sind die eigentliche Information - sie bekommen die Breite und dürfen umbrechen. */
.audit-details { min-width: 18rem; word-break: break-word; }
.audit-agent { color: var(--muted); font-size: 0.8rem; white-space: nowrap; }

/* Sicherheitsrelevantes soll beim Überfliegen auffallen. */
.audit-warn td:first-child { box-shadow: inset 3px 0 0 var(--danger); }
.audit-warn { background: color-mix(in srgb, var(--danger) 7%, transparent); }
.audit-notable td:first-child { box-shadow: inset 3px 0 0 var(--warn); }

/* ---------------------------------------------------------------- Ladezustand */

.splash { display: grid; place-items: center; padding: 48px 0; }

.spinner {
    width: 26px; height: 26px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
    .spinner { animation-duration: 3s; }
    .btn:active { transform: none; }
}

/* ---------------------------------------------------------------- Desktop */

@media (min-width: 900px) {
    /* Am Rechner sitzt niemand mit dem Daumen an der Unterkante - die Navigation
       wandert nach oben und wird zur normalen Zeile. */
    .tabbar {
        position: sticky; top: calc(var(--topbar-h) + env(safe-area-inset-top)); bottom: auto;
        justify-content: center; gap: 8px;
        border-top: none; border-bottom: 1px solid var(--border);
    }

    .tab { flex: 0 0 auto; flex-direction: row; gap: 8px; min-height: 46px; padding: 0 18px; font-size: 0.92rem; }
    .tab-icon { font-size: 0.95rem; }

    /* Reihenfolge im Flex-Container: Kopfzeile, dann Navigation, dann Inhalt. */
    .topbar { order: -2; }
    .tabbar { order: -1; }
    .content { padding: 28px 24px 60px; }

    .form-row { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); align-items: end; }
    .tiles { grid-template-columns: repeat(3, 1fr); }
    .meta-grid { grid-template-columns: repeat(2, 1fr); }
    .btn-hero { max-width: 22rem; }
    .btn-block { width: auto; min-width: 12rem; }
}
