:root {
  --bg: #f4f1ec;
  --surface: #ffffff;
  --surface-2: #faf8f5;
  --text: #1f2421;
  --muted: #6b716d;
  --line: #e5e0d8;
  --line-strong: #d3ccc1;
  --primary: #2e3532;
  --primary-hover: #1d2220;
  --accent: #a6814f;
  --accent-soft: #efe6d8;
  --gut: #2c7a66;
  --gut-soft: #e3f1ec;
  --ab: #b4532e;
  --ab-soft: #f8e7df;
  --danger: #b3261e;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgba(31, 36, 33, .05), 0 4px 16px rgba(31, 36, 33, .05);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
h1, h2 { margin: 0; letter-spacing: -.01em; }
button, input, select { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 42px; padding: 0 18px;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: var(--surface); font-weight: 600; text-decoration: none; cursor: pointer;
  transition: background .15s, border-color .15s, box-shadow .15s, transform .05s;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn-sm { height: 36px; padding: 0 14px; font-size: 14px; }
.btn-block { width: 100%; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface); border-color: var(--line-strong); color: var(--text); }
.btn-secondary:hover { border-color: var(--primary); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: rgba(0,0,0,.05); color: var(--text); }
.btn-danger-ghost { background: transparent; color: var(--danger); padding: 0 12px; }
.btn-danger-ghost:hover { background: #fbeceb; }
.btn:focus-visible, .icon-btn:focus-visible, .stat:focus-visible, .segmented button:focus-visible, tr:focus-visible {
  outline: 3px solid rgba(166, 129, 79, .45); outline-offset: 2px;
}
.icon-btn {
  display: inline-grid; place-items: center; width: 34px; height: 34px;
  border: 1px solid var(--line-strong); border-radius: 50%; background: var(--surface);
  font-size: 20px; line-height: 1; cursor: pointer; color: var(--text);
}
.icon-btn:hover { border-color: var(--primary); }

/* Login */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 24px;
  background: radial-gradient(1200px 600px at 20% -10%, #e9e1d4 0%, transparent 60%), var(--bg); }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border-radius: 20px; padding: 36px 32px 32px; box-shadow: var(--shadow); text-align: center; }
.login-logo { border-radius: 14px; margin-bottom: 16px; }
.login-card h1 { font-size: 21px; }
.login-card p.muted { margin: 4px 0 24px; }
.login-card form { text-align: left; display: grid; gap: 16px; }
.login-hint { margin: 14px 0 0; font-size: 13px; color: var(--muted); }
.login-install { margin-top: 18px; border-top: 1px solid var(--line); border-radius: 0 0 var(--radius-sm) var(--radius-sm); height: auto; padding: 14px 8px 4px; white-space: normal; color: var(--text); }
.login-install:hover { background: transparent; color: var(--accent); }
.login-error { background: var(--ab-soft); color: var(--ab); border-radius: var(--radius-sm); padding: 10px 12px; font-size: 14px; margin: -8px 0 16px; }

/* Layout */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--primary); color: #fff;
  padding-left: env(titlebar-area-x, 0px);
  padding-right: max(0px, calc(100vw - env(titlebar-area-x, 0px) - env(titlebar-area-width, 100vw)));
  min-height: env(titlebar-area-height, 0px);
  -webkit-app-region: drag; app-region: drag;
  -webkit-user-select: none; user-select: none;
}
.topbar a, .topbar button, .topbar form, .topbar input { -webkit-app-region: no-drag; app-region: no-drag; }
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.btn-install { background: rgba(255,255,255,.12); color: #fff; border-color: rgba(255,255,255,.18); }
.btn-install:hover { background: rgba(255,255,255,.2); }
.topbar-inner { max-width: 1180px; margin: 0 auto; padding: 12px 24px; display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.brand { display: flex; align-items: center; gap: 12px; color: inherit; text-decoration: none; }
.brand img { border-radius: 9px; box-shadow: 0 0 0 1px rgba(255,255,255,.12); }
.brand strong { display: block; font-size: 16px; line-height: 1.2; }
.brand small { display: block; color: #c9c0b2; font-size: 12.5px; }
.topbar .btn-ghost { color: #d9d3c9; }
.topbar .btn-ghost:hover { background: rgba(255,255,255,.1); color: #fff; }

.container { max-width: 1180px; margin: 0 auto; padding: 24px; display: grid; gap: 28px; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }

/* Entry form */
.entry-card { padding: 22px 24px 24px; }
.entry-card h2 { font-size: 18px; margin-bottom: 16px; }

.type-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 18px; }
.type-option { position: relative; cursor: pointer; }
.type-option input { position: absolute; opacity: 0; pointer-events: none; }
.type-box {
  display: flex; align-items: center; gap: 12px; padding: 12px 16px;
  border: 1.5px solid var(--line-strong); border-radius: 12px; background: var(--surface-2);
  transition: border-color .15s, background .15s, box-shadow .15s;
}
.type-box svg { width: 22px; height: 22px; color: var(--muted); }
.type-box strong { display: block; font-size: 15.5px; }
.type-box small { display: block; color: var(--muted); font-size: 13px; line-height: 1.3; }
.type-option:hover .type-box { border-color: var(--primary); }
.type-gutschrift input:checked + .type-box { border-color: var(--gut); background: var(--gut-soft); box-shadow: 0 0 0 3px rgba(44,122,102,.12); }
.type-gutschrift input:checked + .type-box svg, .type-gutschrift input:checked + .type-box strong { color: var(--gut); }
.type-abschreibung input:checked + .type-box { border-color: var(--ab); background: var(--ab-soft); box-shadow: 0 0 0 3px rgba(180,83,46,.12); }
.type-abschreibung input:checked + .type-box svg, .type-abschreibung input:checked + .type-box strong { color: var(--ab); }
.type-option input:focus-visible + .type-box { outline: 3px solid rgba(166,129,79,.45); outline-offset: 2px; }

.grid { display: grid; gap: 14px 14px; grid-template-columns: 150px minmax(0, 1.6fr) minmax(0, 1.5fr) 90px minmax(0, 1.2fr) 130px; }
.f-bemerkung { grid-column: 1 / -1; }

.field { display: grid; gap: 6px; align-content: start; }
.field > span:first-child { font-size: 13px; font-weight: 600; color: var(--muted); }
.field small { font-weight: 400; }
.field input {
  width: 100%; height: 44px; padding: 0 12px;
  border: 1px solid var(--line-strong); border-radius: var(--radius-sm); background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
}
.field input::placeholder { color: #a8aaa6; }
.field input:hover { border-color: #b9b1a4; }
.field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(166,129,79,.18); }
.input-suffix { position: relative; display: block; }
.input-suffix input { padding-right: 30px; text-align: right; }
.input-suffix > span { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.err { display: none; font-style: normal; font-size: 12.5px; color: var(--danger); }
.has-error input { border-color: var(--danger); }
.has-error .err { display: block; }

.form-actions { display: flex; align-items: center; gap: 10px; margin-top: 18px; }
.form-actions .spacer { flex: 1; }
.entry-card .form-actions .btn-primary { min-width: 160px; }

/* List header */
.list-section { display: grid; gap: 16px; }
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.period { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.segmented { display: inline-flex; background: #e9e4dc; padding: 3px; border-radius: 10px; }
.segmented button { border: 0; background: transparent; padding: 6px 14px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--muted); cursor: pointer; }
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.period-nav { display: flex; align-items: center; gap: 10px; }
.period-nav span { font-size: 18px; font-weight: 700; min-width: 150px; text-align: center; }
.period-nav.disabled .icon-btn { visibility: hidden; }
.export { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.export-label { color: var(--muted); font-size: 13.5px; margin-right: 2px; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
.stat {
  text-align: left; border: 1.5px solid transparent; border-radius: var(--radius); background: var(--surface);
  padding: 14px 18px; box-shadow: var(--shadow); cursor: pointer; display: grid; gap: 2px;
  transition: border-color .15s, transform .1s;
}
.stat:hover { border-color: var(--line-strong); }
.stat.active { border-color: var(--primary); }
.stat-gutschrift.active { border-color: var(--gut); }
.stat-abschreibung.active { border-color: var(--ab); }
.stat-label { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13.5px; font-weight: 600; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.stat-gutschrift .dot { background: var(--gut); }
.stat-abschreibung .dot { background: var(--ab); }
.stat-sum { font-size: 24px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.stat-count { color: var(--muted); font-size: 13px; }

/* Table */
.table-card { overflow: hidden; }
.table-tools { padding: 14px 16px; border-bottom: 1px solid var(--line); }
.table-tools input {
  width: 100%; max-width: 420px; height: 38px; padding: 0 12px 0 36px; border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  background: var(--surface-2) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%236b716d' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cpath d='m20 20-3.5-3.5'/%3E%3C/svg%3E") no-repeat 11px center / 16px;
}
.table-tools input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(166,129,79,.18); background-color: var(--surface); }

.entries { width: 100%; border-collapse: collapse; font-size: 14.5px; }
.entries th { text-align: left; font-size: 12.5px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; padding: 10px 14px; background: var(--surface-2); border-bottom: 1px solid var(--line); }
.entries td { padding: 12px 14px; border-bottom: 1px solid var(--line); vertical-align: top; }
.entries tbody tr { cursor: pointer; transition: background .1s; }
.entries tbody tr:hover { background: #faf6f0; }
.entries tbody tr:last-child td { border-bottom: 0; }
.entries.is-empty thead { display: none; }
.num { text-align: right !important; font-variant-numeric: tabular-nums; }
.nowrap { white-space: nowrap; }
.strong { font-weight: 600; }
.mono { font-family: var(--mono); font-size: 13.5px; }
.note { color: var(--muted); font-size: 13px; margin-top: 2px; }
.actions-col { width: 40px; text-align: right; }
.edit-hint { color: #b9b3a9; opacity: 0; transition: opacity .15s; display: inline-flex; }
tr:hover .edit-hint, tr:focus-visible .edit-hint { opacity: 1; }
.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.badge-gutschrift { background: var(--gut-soft); color: var(--gut); }
.badge-abschreibung { background: var(--ab-soft); color: var(--ab); }

.empty { padding: 48px 24px; text-align: center; color: var(--muted); }
.empty svg { width: 34px; height: 34px; color: #c6bfb3; }
.empty p { margin: 8px 0 0; }

/* Dialog */
.dialog { border: 0; border-radius: 18px; padding: 0; width: min(760px, calc(100vw - 24px)); box-shadow: 0 20px 60px rgba(0,0,0,.25); color: var(--text); }
.dialog::backdrop { background: rgba(31, 36, 33, .45); backdrop-filter: blur(2px); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px 0; }
.dialog-head h2 { font-size: 18px; }
#edit-form-slot { padding: 16px 22px 22px; }
.dialog .grid { grid-template-columns: 1fr 1fr 1fr; }
.dialog .f-name, .dialog .f-rechnungsnr { grid-column: span 1; }

/* Install dialog */
.install-dialog { width: min(560px, calc(100vw - 24px)); }
.install-body { padding: 8px 22px 22px; font-size: 15px; }
.install-body ol, .install-body ul { margin: 8px 0 0; padding-left: 22px; display: grid; gap: 10px; }
.install-body li::marker { color: var(--accent); font-weight: 700; }
.install-body p { margin: 8px 0 0; }
.install-note { color: var(--muted); font-size: 13.5px; margin-top: 14px !important; padding-top: 12px; border-top: 1px solid var(--line); }
.install-url { display: flex; align-items: center; gap: 10px; margin-top: 12px; padding: 8px 8px 8px 12px; background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius-sm); }
.install-url code { flex: 1; font-family: var(--mono); font-size: 14px; overflow-wrap: anywhere; }

/* Installed app */
html.is-app, html.is-app body { overscroll-behavior: none; }
html.is-app .stats, html.is-app .segmented, html.is-app .period-nav, html.is-app .entries th, html.is-app .field > span:first-child,
html.is-app .type-box, html.is-app .btn, html.is-app .entry-card h2, html.is-app .export-label { -webkit-user-select: none; user-select: none; }

/* Toast */
.toasts { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); display: grid; gap: 8px; z-index: 100; width: max-content; max-width: calc(100vw - 32px); }
.toast { display: flex; align-items: center; gap: 14px; background: var(--primary); color: #fff; padding: 12px 16px; border-radius: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.2); font-size: 14.5px; opacity: 0; transform: translateY(8px); transition: opacity .25s, transform .25s; }
.toast.show { opacity: 1; transform: none; }
.toast-error { background: var(--danger); }
.toast button { border: 0; background: rgba(255,255,255,.15); color: #fff; font-weight: 600; padding: 5px 10px; border-radius: 7px; cursor: pointer; }
.toast button:hover { background: rgba(255,255,255,.25); }

/* Responsive */
@media (max-width: 1080px) {
  .grid { grid-template-columns: 150px 1fr 1fr; }
}
@media (max-width: 760px) {
  .container { padding: 16px; gap: 20px; }
  .topbar-inner { padding: 10px 16px; }
  .brand strong { font-size: 15px; }
  .brand small { display: none; }
  .btn-install span { display: none; }
  .btn-install { padding: 0 10px; }
  .entry-card { padding: 18px 16px; }
  .type-toggle { grid-template-columns: 1fr; gap: 8px; }
  .grid, .dialog .grid { grid-template-columns: 1fr 1fr; }
  .f-name, .f-rechnungsnr, .f-produkt, .f-bemerkung { grid-column: 1 / -1; }
  .entry-card .form-actions .btn-primary { flex: 1; }
  .list-head, .period { flex-direction: column; align-items: stretch; }
  .segmented { display: flex; }
  .segmented button { flex: 1; }
  .period-nav { justify-content: space-between; }
  .export-label { width: 100%; }
  .export .btn { flex: 1; }
  .stats { grid-template-columns: 1fr; gap: 8px; }
  .stat { grid-template-columns: 1fr auto; align-items: center; padding: 12px 16px; }
  .stat-sum { font-size: 19px; grid-row: span 2; grid-column: 2; }
  .entries thead { display: none; }
  .entries, .entries tbody, .entries tr, .entries td { display: block; }
  .entries tbody tr { padding: 12px 16px; border-bottom: 1px solid var(--line); display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; }
  .entries td { padding: 0; border: 0; }
  .entries td[data-label="Art"] { grid-column: 1; }
  .entries td[data-label="Summe"] { grid-column: 2; grid-row: 1 / span 2; align-self: center; font-size: 16px; }
  .entries td[data-label="Datum"] { grid-column: 1; color: var(--muted); font-size: 13px; }
  .entries td[data-label="Name"], .entries td[data-label="Rechnungsnr."], .entries td[data-label="Produkt"] { grid-column: 1 / -1; }
  .entries td[data-label="Anzahl"] { grid-column: 1 / -1; text-align: left !important; color: var(--muted); font-size: 13px; }
  .entries td[data-label="Anzahl"]::before { content: "Anzahl: "; }
  .actions-col { display: none !important; }
}
