:root {
    --color-bg: #f4f6f9;
    --color-surface: #ffffff;
    --color-border: #e3e7ee;
    --color-text: #1e2433;
    --color-text-muted: #6b7385;
    --color-primary: #2f5bea;
    --color-primary-dark: #2244c0;
    --color-primary-light: #eaf0ff;
    --color-success: #1a9b6b;
    --color-success-light: #e5f7ef;
    --color-warning: #c98a12;
    --color-warning-light: #fdf3e0;
    --color-danger: #d64545;
    --color-danger-light: #fbe9e9;
    --color-muted-bg: #eef0f4;
    --radius: 10px;
    --radius-lg: 14px;
    --shadow: 0 1px 2px rgba(20, 24, 38, 0.06), 0 4px 14px rgba(20, 24, 38, 0.05);
    --sidebar-width: 270px;
    --topbar-height: 58px;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--color-bg);
    color: var(--color-text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--color-primary); text-decoration: none; }

button { font-family: inherit; }

h1, h2, h3, h4 { margin: 0 0 4px 0; font-weight: 650; letter-spacing: -0.01em; }

/* ---------- Login ---------- */
.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(160deg, #1e2433 0%, #2f5bea 120%);
    padding: 20px;
}
.login-card {
    background: var(--color-surface);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    padding: 40px 36px;
    width: 100%;
    max-width: 380px;
}
.login-card h1 { font-size: 22px; }
.login-sub { color: var(--color-text-muted); margin: 0 0 22px 0; font-size: 14px; }
.login-form { display: flex; flex-direction: column; gap: 16px; }
.login-form label { font-size: 13px; font-weight: 600; color: var(--color-text-muted); display: flex; flex-direction: column; gap: 6px; }

/* ---------- App shell ---------- */
#app-shell { min-height: 100vh; display: flex; flex-direction: column; }

.topbar {
    height: var(--topbar-height);
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 12px;
    position: sticky;
    top: 0;
    z-index: 30;
}
.topbar-title { font-weight: 700; font-size: 16px; flex: 1; }
.topbar-user { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--color-text-muted); }
.icon-btn { background: none; border: none; font-size: 20px; cursor: pointer; padding: 6px 8px; border-radius: 8px; color: var(--color-text); }
.icon-btn:hover { background: var(--color-muted-bg); }

.layout { flex: 1; display: flex; min-height: 0; }

.sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--color-surface);
    border-right: 1px solid var(--color-border);
    padding: 14px 10px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.nav-link {
    display: block;
    padding: 10px 14px;
    border-radius: 8px;
    color: var(--color-text);
    font-size: 14.5px;
    font-weight: 500;
}
.nav-link:hover { background: var(--color-muted-bg); }
.nav-link.active { background: var(--color-primary-light); color: var(--color-primary-dark); font-weight: 650; }

#sidebar-backdrop { display: none; }

.view-root { flex: 1; padding: 22px 26px 60px 26px; min-width: 0; }

@media (max-width: 880px) {
    .sidebar {
        position: fixed;
        top: var(--topbar-height);
        bottom: 0;
        left: 0;
        z-index: 40;
        transform: translateX(-100%);
        transition: transform 0.2s ease;
        box-shadow: var(--shadow);
    }
    .sidebar.open { transform: translateX(0); }
    #sidebar-backdrop.open {
        display: block;
        position: fixed;
        inset: var(--topbar-height) 0 0 0;
        background: rgba(10, 14, 25, 0.35);
        z-index: 35;
    }
    .view-root { padding: 16px 14px 50px 14px; }
    .topbar-user .user-name { display: none; }
}

/* ---------- Layout helpers ---------- */
.view-header {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 18px;
}
.view-header h2 { font-size: 20px; }
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; width: 100%; }

.card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 18px;
}
.card h3 { font-size: 15px; margin-bottom: 14px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 20px; }
.stat-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: 16px 18px;
    box-shadow: var(--shadow);
}
.stat-card .stat-label { font-size: 12.5px; color: var(--color-text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.stat-card .stat-value { font-size: 24px; font-weight: 700; margin-top: 6px; }
.stat-card.warn .stat-value { color: var(--color-warning); }
.stat-card.danger .stat-value { color: var(--color-danger); }
.stat-card.success .stat-value { color: var(--color-success); }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 9px 16px;
    border-radius: 8px;
    border: 1px solid transparent;
    font-size: 13.5px;
    font-weight: 600;
    cursor: pointer;
    background: var(--color-muted-bg);
    color: var(--color-text);
    white-space: nowrap;
}
.btn:hover { filter: brightness(0.97); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover { background: var(--color-primary-dark); }
.btn-danger { background: var(--color-danger-light); color: var(--color-danger); }
.btn-success { background: var(--color-success-light); color: var(--color-success); }
.btn-ghost { background: transparent; border: 1px solid var(--color-border); }
.btn-sm { padding: 6px 11px; font-size: 12.5px; }
.btn-block { width: 100%; justify-content: center; }
.btn-icon { padding: 7px 9px; }

/* ---------- Forms ---------- */
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px 16px; }
.form-grid.cols-1 { grid-template-columns: 1fr; }
.form-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 640px) {
    .form-grid, .form-grid.cols-3 { grid-template-columns: 1fr; }
}
.field { display: flex; flex-direction: column; gap: 6px; }
.field.span-2 { grid-column: span 2; }
@media (max-width: 640px) { .field.span-2 { grid-column: span 1; } }
.field label { font-size: 12.5px; font-weight: 650; color: var(--color-text-muted); }
.field .hint { font-size: 11.5px; color: var(--color-text-muted); }

input[type=text], input[type=email], input[type=password], input[type=number], input[type=date],
input[type=month], input[type=week], input[type=time], input[type=search], input[type=url],
input[type=tel], input[type=file], select, textarea {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    padding: 9px 11px;
    font-size: 14px;
    line-height: 1.3;
    background: #fff;
    color: var(--color-text);
    font-family: inherit;
    width: 100%;
    box-sizing: border-box;
}
/* Safari zeichnet Datum/Monat/Zeit-Felder sonst mit eigenem Innenabstand & Höhe. */
input[type=date], input[type=month], input[type=week], input[type=time] {
    min-height: 37px;
}
input[type=file] { padding: 6px 8px; }
select {
    padding-right: 30px;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='none' stroke='%236b7385' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'><polyline points='5 8 10 13 15 8'/></svg>");
    background-repeat: no-repeat;
    background-position: right 10px center;
    background-size: 14px;
    cursor: pointer;
}
textarea { resize: vertical; min-height: 70px; }
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--color-primary-light);
}
/* Safari/Chrome fügen sonst einen inneren Schatten oder farbigen Rand bei Autofill hinzu. */
input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0 1000px #fff inset;
    -webkit-text-fill-color: var(--color-text);
}
label.checkbox { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; color: var(--color-text); }
label.checkbox input { width: auto; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border-radius: var(--radius-lg); }
table.data-table { width: 100%; border-collapse: collapse; background: var(--color-surface); font-size: 13.5px; }
table.data-table th {
    text-align: left; padding: 11px 14px; font-size: 11.5px; text-transform: uppercase;
    letter-spacing: 0.03em; color: var(--color-text-muted); border-bottom: 1.5px solid var(--color-border); white-space: nowrap;
}
table.data-table td { padding: 11px 14px; border-bottom: 1px solid var(--color-border); vertical-align: middle; }
table.data-table tr:last-child td { border-bottom: none; }
table.data-table tr:hover td { background: #fafbfd; }
.text-right { text-align: right; }
.text-muted { color: var(--color-text-muted); }
.empty-state { padding: 40px 20px; text-align: center; color: var(--color-text-muted); }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 11.5px; font-weight: 700; letter-spacing: 0.01em; }
.badge-draft { background: var(--color-muted-bg); color: var(--color-text-muted); }
.badge-sent { background: var(--color-primary-light); color: var(--color-primary-dark); }
.badge-paid { background: var(--color-success-light); color: var(--color-success); }
.badge-overdue { background: var(--color-danger-light); color: var(--color-danger); }
.badge-cancelled { background: #eee; color: #999; text-decoration: line-through; }

/* ---------- Modal ---------- */
.modal-backdrop {
    position: fixed; inset: 0; background: rgba(10, 14, 25, 0.45);
    display: flex; align-items: flex-start; justify-content: center;
    padding: 30px 16px; overflow-y: auto; z-index: 100;
}
.modal {
    background: var(--color-surface); border-radius: var(--radius-lg); box-shadow: var(--shadow);
    width: 100%; max-width: 640px; padding: 24px 26px; margin: auto;
}
.modal.modal-lg { max-width: 920px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.modal-header h3 { font-size: 17px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; flex-wrap: wrap; }
.modal-close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--color-text-muted); }

/* ---------- Line items editor ---------- */
.items-editor { width: 100%; border-collapse: collapse; margin-top: 8px; }
.items-editor th { text-align: left; font-size: 11px; text-transform: uppercase; color: var(--color-text-muted); padding: 6px 6px; }
.items-editor td { padding: 5px 6px; vertical-align: top; }
.items-editor input, .items-editor select { padding: 7px 8px; font-size: 13px; }
.items-editor select { padding-right: 26px; background-position: right 6px center; background-size: 12px; }
.items-editor .col-desc { width: 34%; }
.items-editor .col-qty { width: 10%; }
.items-editor .col-unit { width: 10%; }
.items-editor .col-price { width: 16%; }
.items-editor .col-vat { width: 10%; }
.items-editor .col-total { width: 14%; text-align: right; padding-top: 12px; font-weight: 600; white-space: nowrap; }
.items-editor .col-remove { width: 5%; text-align: center; }
.items-editor tr.deduction-row td { color: var(--color-danger); }
.items-totals { margin-top: 10px; margin-left: auto; width: 280px; font-size: 13.5px; }
.items-totals div { display: flex; justify-content: space-between; padding: 4px 2px; }
.items-totals .grand { font-weight: 700; font-size: 15px; border-top: 1.5px solid var(--color-border); margin-top: 6px; padding-top: 8px; }

/* ---------- Toasts ---------- */
.toast-root { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 10px; z-index: 200; max-width: calc(100vw - 40px); }
.toast {
    background: #1e2433; color: #fff; padding: 12px 18px; border-radius: 9px; font-size: 13.5px;
    box-shadow: var(--shadow); animation: toast-in 0.18s ease;
}
.toast.toast-error { background: var(--color-danger); }
.toast.toast-success { background: var(--color-success); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* ---------- Misc ---------- */
.alert { padding: 11px 14px; border-radius: 8px; font-size: 13.5px; margin-bottom: 14px; }
.alert-error { background: var(--color-danger-light); color: var(--color-danger); }
.alert-info { background: var(--color-primary-light); color: var(--color-primary-dark); }
.alert-success { background: var(--color-success-light); color: var(--color-success); }
.loading-placeholder { padding: 60px; text-align: center; color: var(--color-text-muted); }
.section-divider { border: none; border-top: 1px solid var(--color-border); margin: 22px 0; }
.pill-tabs { display: flex; gap: 6px; background: var(--color-muted-bg); padding: 4px; border-radius: 10px; width: fit-content; margin-bottom: 18px; flex-wrap: wrap; }
.pill-tab { padding: 7px 16px; border-radius: 8px; font-size: 13px; font-weight: 600; cursor: pointer; color: var(--color-text-muted); }
.pill-tab.active { background: var(--color-surface); color: var(--color-text); box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.logo-preview { max-height: 60px; max-width: 200px; border: 1px dashed var(--color-border); padding: 8px; border-radius: 8px; background: #fafbfd; }
.inline-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.help-box { background: var(--color-primary-light); color: var(--color-primary-dark); padding: 12px 14px; border-radius: 10px; font-size: 13px; margin-bottom: 16px; }
