/* Cashflow — Basis-Styles + Theme-System.
   Theme-aware: hell (Default), dunkel via prefers-color-scheme ODER
   data-theme-Attribut (index.php setzt es aus localStorage 'cashflowTheme').
   Mobile-first — primärer Client ist die PWA am Handy. */

:root {
    --bg: #f3f4f6;
    --surface: #ffffff;
    --surface-2: #f9fafb;
    --text: #111827;
    --text-muted: #6b7280;
    --border: #e5e7eb;
    --accent: #4f46e5;
    --accent-text: #ffffff;
    --income: #16a34a;
    --expense: #dc2626;
    --radius: 14px;
    --shadow: 0 1px 3px rgba(0, 0, 0, .08), 0 8px 24px rgba(0, 0, 0, .05);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0b1120;
        --surface: #111827;
        --surface-2: #1f2937;
        --text: #f9fafb;
        --text-muted: #9ca3af;
        --border: #263041;
        --accent: #6366f1;
        --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .3);
    }
}

/* Explizite Overrides gewinnen in BEIDE Richtungen gegen prefers-color-scheme. */
:root[data-theme="light"] {
    --bg: #f3f4f6; --surface: #fff; --surface-2: #f9fafb; --text: #111827;
    --text-muted: #6b7280; --border: #e5e7eb; --accent: #4f46e5;
}
:root[data-theme="dark"] {
    --bg: #0b1120; --surface: #111827; --surface-2: #1f2937; --text: #f9fafb;
    --text-muted: #9ca3af; --border: #263041; --accent: #6366f1;
}

* { box-sizing: border-box; }
/* [hidden] muss auch dann greifen, wenn eine Regel display setzt (z.B. #update-banner:display:flex). */
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
}

/* ── Login ── */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.auth-card {
    background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 32px 24px; width: 100%; max-width: 360px; text-align: center;
}
.auth-card h1 { margin: 0 0 4px; font-size: 28px; }
.auth-sub { margin: 0 0 20px; color: var(--text-muted); font-size: 14px; }
.auth-error { background: color-mix(in srgb, var(--expense) 15%, transparent); color: var(--expense);
    padding: 8px 12px; border-radius: 10px; margin-bottom: 14px; font-size: 14px; }
.auth form { display: flex; flex-direction: column; gap: 10px; }
button { font: inherit; cursor: pointer; }
.auth input, .auth button { font: inherit; padding: 12px 14px; border-radius: 10px; }
.auth input { border: 1px solid var(--border); background: var(--surface-2); color: var(--text); }
.auth button { border: none; background: var(--accent); color: var(--accent-text); font-weight: 600; }
.auth button:active { transform: translateY(1px); }

/* Primär-Button (Dashboard, Formulare) */
.btn-primary {
    background: var(--accent); color: var(--accent-text); border: none;
    border-radius: 10px; padding: 10px 14px; font-weight: 600; cursor: pointer; font: inherit;
}
.btn-primary:active { transform: translateY(1px); }

/* ── App-Shell ── */
#app { max-width: 640px; margin: 0 auto; min-height: 100vh; }
#app-header {
    position: sticky; top: 0; z-index: 10;
    display: flex; align-items: center; gap: 12px;
    background: var(--surface); border-bottom: 1px solid var(--border);
    padding: 12px 16px; padding-top: calc(12px + env(safe-area-inset-top));
}
#app-header .brand { font-weight: 700; display: inline-flex; align-items: center; gap: 8px; }
.brand-logo { width: 26px; height: 26px; border-radius: 7px; display: inline-block; }
#app-header .spacer { flex: 1; }
#sync-state { font-size: 12px; color: #9ca3af; }
.logout { text-decoration: none; color: var(--text-muted); font-size: 20px; }

#app-main { padding: 16px; display: flex; flex-direction: column; gap: 20px; }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.tile {
    background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 16px 12px; text-align: center; display: flex; flex-direction: column; gap: 4px;
}
.tile-num { font-size: 26px; font-weight: 700; }
.tile-lbl { font-size: 12px; color: var(--text-muted); }

.scaffold-note {
    background: var(--surface-2); border: 1px dashed var(--border);
    border-radius: var(--radius); padding: 16px; font-size: 14px; line-height: 1.5;
}
.scaffold-note p { margin: 0 0 12px; }
.scaffold-note button { width: 100%; }

h2 { font-size: 16px; margin: 0 0 8px; }
.muted { color: var(--text-muted); font-size: 14px; }

.tx-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.tx-list li {
    display: grid; grid-template-columns: auto 1fr auto auto; gap: 10px; align-items: center;
    background: var(--surface); border-radius: 10px; padding: 10px 12px; box-shadow: var(--shadow); font-size: 14px;
}
.tx-list li.pending { opacity: .6; }
.tx-list .badge { font-size: 12px; }

/* ── Update-Banner ── */
#update-banner {
    position: fixed; left: 50%; transform: translateX(-50%);
    bottom: calc(16px + env(safe-area-inset-bottom));
    background: var(--accent); color: var(--accent-text);
    padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow);
    display: flex; align-items: center; gap: 12px; z-index: 100; font-size: 14px;
}
#update-banner button { background: rgba(255, 255, 255, .2); color: #fff; border: none; border-radius: 8px; padding: 6px 12px; }

/* ── Icon-Buttons (Header, Karten) ── */
.icon-btn {
    background: transparent; border: none; color: var(--text); padding: 6px;
    border-radius: 8px; cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn svg { fill: currentColor; display: block; }
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.small { padding: 4px 6px; font-size: 15px; }
.icon-btn.danger { color: var(--expense); }

/* ── Menü-Drawer + Overlay ── */
.menu-overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 40; }
.menu-drawer {
    position: fixed; top: 0; left: 0; bottom: 0; width: 300px; max-width: 85vw;
    background: var(--surface); z-index: 50; transform: translateX(-100%); transition: transform .25s ease;
    display: flex; flex-direction: column; box-shadow: var(--shadow); overflow-y: auto;
    padding-top: env(safe-area-inset-top);
}
.menu-drawer.open { transform: translateX(0); }
.menu-header { display: flex; align-items: center; justify-content: space-between; padding: 16px; border-bottom: 1px solid var(--border); }
.menu-title-group { display: flex; align-items: center; gap: 12px; }
.menu-avatar {
    width: 42px; height: 42px; border-radius: 50%; background: var(--accent); color: #fff;
    display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 18px; flex: 0 0 auto;
}
.menu-header h2 { margin: 0; font-size: 16px; }
.menu-role { font-size: 12px; color: var(--text-muted); }
.menu-nav { flex: 1; padding: 8px 0; }
.menu-nav ul { list-style: none; margin: 0; padding: 0; }
.menu-nav a { display: flex; align-items: center; gap: 12px; padding: 12px 16px; color: var(--text); text-decoration: none; font-size: 15px; }
.menu-nav a:hover { background: var(--surface-2); }
.menu-nav .mi { width: 22px; text-align: center; }
.menu-nav .chevron { margin-left: auto; transition: transform .2s; font-size: 12px; color: var(--text-muted); }
.menu-nav .chevron.open { transform: rotate(180deg); }
.menu-sep { height: 1px; background: var(--border); margin: 8px 0; }
.menu-logout { color: var(--expense) !important; }
.menu-version { text-align: center; font-size: 11px; color: var(--text-muted); padding: 10px; }

.submenu { background: var(--surface-2); }
.submenu.collapsed { display: none; }
.submenu-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 16px 12px 40px; font-size: 14px; }
.submenu-label { color: var(--text-muted); }
.submenu ul li a { padding-left: 40px; }

/* Toggle-Switch */
.menu-switch { width: 42px; height: 24px; border-radius: 999px; background: var(--border); position: relative; cursor: pointer; transition: background .2s; flex: 0 0 auto; }
.menu-switch::after { content: ''; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .2s; }
.menu-switch.active { background: var(--accent); }
.menu-switch.active::after { transform: translateX(18px); }

/* Theme-Buttons */
.theme-buttons { display: flex; gap: 6px; }
.theme-btn { padding: 6px 10px; font-size: 13px; border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 8px; cursor: pointer; }
.theme-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ── Sync-Indikator ── */
.sync-indicator { display: flex; align-items: center; gap: 6px; cursor: pointer; padding: 4px 8px; border-radius: 999px; }
.sync-indicator svg { width: 20px; height: 20px; display: block; fill: currentColor; }
.sync-label { font-size: 12px; max-width: 0; overflow: hidden; white-space: nowrap; opacity: 0; transition: max-width .3s, opacity .3s; }
.sync-label.visible { max-width: 130px; opacity: 1; }
.sync-online { color: #22c55e; } .sync-offline { color: #9ca3af; }
.sync-pending { color: #f97316; } .sync-syncing { color: #eab308; } .sync-forced { color: #a855f7; }
.sync-syncing svg { animation: cf-spin 1.2s linear infinite; }
@keyframes cf-spin { to { transform: rotate(360deg); } }

/* ── Queue-Panel ── */
.queue-panel {
    position: fixed; top: calc(54px + env(safe-area-inset-top)); right: 8px;
    width: 320px; max-width: calc(100vw - 16px); background: var(--surface);
    border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow);
    z-index: 60; padding: 12px; max-height: 75vh; overflow-y: auto; font-size: 14px;
}
.queue-offline-toggle { display: flex; align-items: center; justify-content: space-between; padding: 8px 10px; background: var(--surface-2); border-radius: 10px; margin-bottom: 10px; cursor: pointer; }
.queue-title { display: flex; align-items: center; gap: 8px; font-weight: 600; margin-bottom: 10px; }
.queue-dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; }
.queue-dot.green { background: #22c55e; } .queue-dot.orange { background: #f97316; }
.queue-dot.red { background: #ef4444; } .queue-dot.yellow { background: #eab308; } .queue-dot.blue { background: #3b82f6; }
.queue-storage { margin-left: auto; font-size: 12px; color: var(--text-muted); font-weight: 400; }
.queue-section { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.queue-item { display: flex; align-items: center; gap: 10px; background: var(--surface-2); border-radius: 10px; padding: 8px 10px; }
.queue-item.error { background: color-mix(in srgb, var(--expense) 12%, transparent); }
.queue-item-icon { font-size: 16px; }
.queue-item-info { flex: 1; min-width: 0; }
.queue-item-action { font-weight: 500; }
.queue-item-err { font-size: 12px; color: var(--expense); word-break: break-word; }
.queue-item-retry { background: transparent; border: none; color: var(--accent); cursor: pointer; font-size: 18px; }
.queue-empty { color: var(--text-muted); text-align: center; padding: 16px 0; }
.queue-actions { display: flex; flex-direction: column; gap: 6px; }
.queue-actions button { width: 100%; padding: 9px; border-radius: 9px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 13px; }
.queue-actions button.danger { color: var(--expense); border-color: color-mix(in srgb, var(--expense) 40%, transparent); }

/* ── Modal ── */
#modal-overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, .5); z-index: 70; }
#modal {
    position: fixed; z-index: 80; top: 50%; left: 50%; transform: translate(-50%, -50%);
    width: 480px; max-width: calc(100vw - 24px); max-height: 85vh; overflow-y: auto;
    background: var(--surface); border-radius: 16px; box-shadow: var(--shadow); padding: 20px;
}
#modal h2 { margin-top: 0; }
.modal-close { position: absolute; top: 12px; right: 12px; background: transparent; border: none; font-size: 18px; color: var(--text-muted); cursor: pointer; }

/* ── Admin-Karten (Benutzer) ── */
.admin-list { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.admin-card { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; }
.admin-card-body { display: flex; gap: 12px; align-items: center; }
.admin-avatar { width: 44px; height: 44px; border-radius: 50%; background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 18px; flex: 0 0 auto; }
.admin-card-right { flex: 1; min-width: 0; }
.admin-card-head { display: flex; align-items: center; gap: 8px; }
.admin-name { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.admin-badge { font-size: 12px; color: var(--text-muted); }
.admin-edit-form { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; align-items: center; }
.admin-edit-form input, .admin-role-select { flex: 1; min-width: 130px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); font: inherit; }

/* ── App-Einstellungen ── */
.settings-list { display: flex; flex-direction: column; gap: 14px; }
.settings-toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

/* ── Mobile: Modal als Bottom-Sheet ── */
@media (max-width: 600px) {
    #modal {
        top: auto; bottom: 0; left: 0; transform: none;
        width: 100%; max-width: 100%; max-height: 90vh;
        border-radius: 18px 18px 0 0; padding-bottom: calc(20px + env(safe-area-inset-bottom));
    }
    .queue-panel { right: 8px; left: 8px; width: auto; }
}

/* ════════ Fachscreens (Konten, Buchungen, Budgets, Verrechnung, Dashboard) ════════ */
#app-main { max-width: 960px; margin-inline: auto; }

.screen-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.screen-header h2 { margin: 0; font-size: 18px; }

.card-block { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.card-block > h3 { margin: 0 0 10px; font-size: 14px; }

.amount { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.amount.income { color: var(--income); }
.amount.expense { color: var(--expense); }
.amount.muted { color: var(--text-muted); }

/* Zusammenfassungs-Zeile (Home) */
.dash-line { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.dash-line:last-child { border-bottom: none; }

/* Buchungszeile innerhalb eines Panels flach darstellen */
.tx-main { display: flex; flex-direction: column; min-width: 0; }
.tx-title { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-sub { font-size: 12px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-date { font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.card-block .tx-list li { background: transparent; box-shadow: none; border-radius: 0; border-bottom: 1px solid var(--border); padding: 10px 0; }
.card-block .tx-list li:last-child { border-bottom: none; }
.tx-item { cursor: pointer; }

/* Entitäts-Karten (Konten, Daueraufträge) als flache Zeilen im Panel */
.entity-card { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 6px; border-bottom: 1px solid var(--border); }
.entity-card:last-child { border-bottom: none; }
.entity-main { display: flex; align-items: center; gap: 12px; min-width: 0; }
.entity-icon { font-size: 22px; flex: 0 0 auto; }
.entity-title { font-weight: 600; }
.entity-sub { font-size: 12px; color: var(--text-muted); }
.entity-right { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }

/* Formulare (Modal) */
.form { display: flex; flex-direction: column; gap: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-muted); }
.field > span { font-weight: 500; }
.field input, .field select { padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); font-family: inherit; font-size: 15px; }
.field input[type=color] { padding: 4px; height: 44px; }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; flex-wrap: wrap; }
.btn-danger { background: transparent; border: 1px solid color-mix(in srgb, var(--expense) 40%, transparent); color: var(--expense); border-radius: 10px; padding: 10px 14px; font-family: inherit; cursor: pointer; }
.btn-sm { background: var(--surface-2); border: 1px solid var(--border); color: var(--text); border-radius: 8px; padding: 6px 10px; font-family: inherit; font-size: 13px; cursor: pointer; }

/* Typ-Umschalter */
.segmented { display: flex; gap: 4px; background: var(--surface-2); border-radius: 10px; padding: 4px; }
.segmented button { flex: 1; padding: 8px; border: none; background: transparent; color: var(--text); border-radius: 8px; font-family: inherit; font-size: 14px; cursor: pointer; }
.segmented button.active { background: var(--accent); color: var(--accent-text); font-weight: 600; }

/* Positionen-Editor */
.items-editor { border: 1px dashed var(--border); border-radius: 10px; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.items-head, .items-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; font-size: 13px; color: var(--text-muted); }
.item-row { display: grid; grid-template-columns: 1fr 58px 66px 76px auto; gap: 6px; align-items: center; }
.item-row input { padding: 7px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); font-family: inherit; font-size: 13px; min-width: 0; }
.item-row .it-del { background: transparent; border: none; color: var(--expense); cursor: pointer; font-size: 16px; padding: 0 2px; }

/* Filterzeile */
.filters { display: flex; flex-wrap: wrap; gap: 8px; }
.filters select { flex: 1; min-width: 130px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); font-family: inherit; }
.month-input { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); font-family: inherit; }

/* Budgets */
.budget-row { padding: 10px 0; border-bottom: 1px solid var(--border); }
.budget-row:last-child { border-bottom: none; }
.budget-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 6px; font-size: 14px; }
.budget-bar { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.budget-bar-fill { height: 100%; background: var(--accent); border-radius: 999px; transition: width .3s; }
.budget-bar.over .budget-bar-fill { background: var(--expense); }

/* Verrechnung / Ausgleichsstand */
.settle-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.settle-row:last-child { border-bottom: none; }
.settle-net { font-variant-numeric: tabular-nums; font-weight: 600; }
.settle-net.pos { color: var(--income); }
.settle-net.neg { color: var(--expense); }
.settle-suggestion { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; margin-top: 6px; font-size: 14px; }

/* Modulares Dashboard */
.dash-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; align-items: start; }
.dash-tile { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; }
.dash-tile > h3 { margin: 0 0 10px; font-size: 14px; }
.dash-config-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.dash-config-row:last-child { border-bottom: none; }
.dash-config-arrows { display: flex; gap: 4px; }
.dash-config-arrows button[disabled] { opacity: .4; }
.dash-tile .quick-actions { display: flex; flex-direction: column; gap: 8px; }
.dash-tile .quick-actions button { width: 100%; }
.dash-tile .tx-list li { background: transparent; box-shadow: none; border-radius: 0; border-bottom: 1px solid var(--border); padding: 8px 0; }
.dash-tile .tx-list li:last-child { border-bottom: none; }

@media (max-width: 600px) {
    .item-row { grid-template-columns: 1fr 46px 54px 64px auto; gap: 4px; }
    .form-actions > button { flex: 1; }
}

/* Login-Logo + AI-Statistik-Tabelle */
.auth-logo { margin-bottom: 10px; }
.auth-logo img { width: 64px; height: 64px; border-radius: 16px; box-shadow: var(--shadow); }
.ai-table { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 4px; }
.ai-table th, .ai-table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.ai-table th { color: var(--text-muted); font-weight: 600; }
.ai-table td:nth-child(n+2), .ai-table th:nth-child(n+2) { text-align: right; font-variant-numeric: tabular-nums; }
