/* =====================================================================
   e-bill Komponenten. Nutzt ausschließlich Variablen aus tokens.css.
   Mobil zuerst; Tablet ≥ 768px (Seitenleiste), Desktop ≥ 1100px.
   ===================================================================== */

/* ---------- Basis ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--color-text);
  background: var(--color-bg);
}
h1, h2, h3, h4 { line-height: var(--leading-tight); margin: 0 0 var(--space-3); font-weight: var(--weight-bold); }
h1 { font-size: var(--text-2xl); }
h2 { font-size: var(--text-xl); }
h3 { font-size: var(--text-lg); }
p { margin: 0 0 var(--space-3); }
a { color: var(--color-primary); text-underline-offset: 2px; }
a:hover { color: var(--color-primary-hover); }
:focus-visible { outline: 3px solid var(--color-focus); outline-offset: 2px; border-radius: var(--radius-sm); }
img, svg { max-width: 100%; }
hr { border: 0; border-top: 1px solid var(--color-border); margin: var(--space-5) 0; }
code, .mono { font-family: var(--font-mono); font-size: 0.92em; }
.muted { color: var(--color-text-muted); }
.small { font-size: var(--text-sm); }
.text-right { text-align: right; }
.nowrap { white-space: nowrap; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: absolute; left: var(--space-2); top: -4rem; background: var(--color-surface); padding: var(--space-2) var(--space-3); z-index: 100; border-radius: var(--radius-md); }
.skip-link:focus { top: var(--space-2); }
.icon { width: 1.25rem; height: 1.25rem; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Layout ---------- */
.shell { min-height: 100vh; }
.main { padding: var(--space-4) var(--space-4) calc(var(--tabbar-height) + var(--space-6)); max-width: var(--content-max); margin: 0 auto; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); padding: var(--space-3) var(--space-4); background: var(--color-surface); border-bottom: 1px solid var(--color-border); position: sticky; top: 0; z-index: 20; }
.brand { display: inline-flex; align-items: center; gap: var(--space-2); font-weight: var(--weight-bold); color: var(--color-text); text-decoration: none; font-size: var(--text-lg); }
.brand-mark { width: 2rem; height: 2rem; border-radius: var(--radius-md); background: var(--color-primary); color: var(--color-on-primary); display: inline-grid; place-items: center; font-size: var(--text-sm); font-weight: var(--weight-bold); }
.topbar-tenant { font-size: var(--text-sm); color: var(--color-text-muted); max-width: 50vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.sidebar { display: none; }
.tabbar {
  position: fixed; inset: auto 0 0 0; height: var(--tabbar-height); z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--color-surface); border-top: 1px solid var(--color-border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: var(--text-xs); color: var(--color-text-muted); text-decoration: none; min-height: var(--tap-min); }
.tabbar a[aria-current="page"] { color: var(--color-primary); font-weight: var(--weight-bold); }

@media (min-width: 768px) {
  .shell { display: grid; grid-template-columns: var(--sidebar-width) 1fr; }
  .topbar { display: none; }
  .tabbar { display: none; }
  .sidebar {
    display: flex; flex-direction: column; gap: var(--space-1); position: sticky; top: 0; height: 100vh;
    padding: var(--space-4) var(--space-3); background: var(--color-surface); border-right: 1px solid var(--color-border); overflow-y: auto;
  }
  .sidebar .brand { padding: var(--space-2) var(--space-3) var(--space-4); }
  .sidebar-tenant { padding: 0 var(--space-3) var(--space-3); font-size: var(--text-sm); color: var(--color-text-muted); }
  .sidebar-foot { margin-top: auto; padding-top: var(--space-4); border-top: 1px solid var(--color-border); }
  .main { padding: var(--space-6); width: 100%; }
}

.nav-link {
  display: flex; align-items: center; gap: var(--space-3); padding: var(--space-2) var(--space-3); min-height: var(--tap-min);
  border-radius: var(--radius-md); color: var(--color-text); text-decoration: none; font-weight: var(--weight-medium);
}
.nav-link:hover { background: var(--color-surface-2); color: var(--color-text); }
.nav-link[aria-current="page"] { background: var(--color-primary-soft); color: var(--color-primary); }
.nav-section { font-size: var(--text-xs); text-transform: uppercase; letter-spacing: 0.06em; color: var(--color-text-muted); padding: var(--space-4) var(--space-3) var(--space-1); }

.page-header { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-3); margin-bottom: var(--space-5); }
.page-header h1 { margin: 0; }
.page-header .subtitle { width: 100%; margin: calc(-1 * var(--space-2)) 0 0; color: var(--color-text-muted); }
.stack > * + * { margin-top: var(--space-4); }
.stack-sm > * + * { margin-top: var(--space-2); }
.cluster { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; }
.grid { display: grid; gap: var(--space-4); }
@media (min-width: 768px) {
  .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .span-2 { grid-column: span 2; }
}

/* ---------- Karten & Kennzahlen ---------- */
.card { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius-lg); padding: var(--space-4); box-shadow: var(--shadow-1); }
@media (min-width: 768px) { .card { padding: var(--space-5); } }
.card-header { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); margin-bottom: var(--space-3); }
.card-header h2, .card-header h3 { margin: 0; }
.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-3); margin-bottom: var(--space-5); }
@media (min-width: 1100px) { .stats { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.stat { background: var(--color-surface); border: 1px solid var(--color-border); border-radius: var(--radius-lg); padding: var(--space-4); }
.stat-label { font-size: var(--text-sm); color: var(--color-text-muted); }
.stat-value { font-size: var(--text-xl); font-weight: var(--weight-bold); margin-top: var(--space-1); font-variant-numeric: tabular-nums; }
.stat-meta { font-size: var(--text-xs); color: var(--color-text-muted); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: var(--tap-min); padding: var(--space-2) var(--space-4);
  font: inherit; font-weight: var(--weight-medium); line-height: 1.2; text-decoration: none; cursor: pointer;
  border-radius: var(--radius-md); border: 1px solid var(--color-border-strong);
  background: var(--color-surface); color: var(--color-text);
  transition: background var(--duration-fast), border-color var(--duration-fast);
}
.btn:hover { background: var(--color-surface-2); color: var(--color-text); }
.btn-primary { background: var(--color-primary); border-color: var(--color-primary); color: var(--color-on-primary); }
.btn-primary:hover { background: var(--color-primary-hover); border-color: var(--color-primary-hover); color: var(--color-on-primary); }
.btn-danger { background: var(--color-danger); border-color: var(--color-danger); color: #fff; }
.btn-danger:hover { background: var(--color-danger-hover); border-color: var(--color-danger-hover); color: #fff; }
@media (prefers-color-scheme: dark) { .btn-danger, .btn-danger:hover { color: #1a0a08; } }
.btn-ghost { background: transparent; border-color: transparent; color: var(--color-primary); }
.btn-ghost:hover { background: var(--color-primary-soft); color: var(--color-primary); }
.btn-sm { min-height: 2.25rem; padding: var(--space-1) var(--space-3); font-size: var(--text-sm); }
.btn-block { width: 100%; }
.btn[disabled], .btn[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; }
.btn-icon { padding: var(--space-2); min-width: var(--tap-min); }
form.inline { display: inline; }

/* ---------- Formulare ---------- */
.field { display: flex; flex-direction: column; gap: var(--space-1); margin-bottom: var(--space-4); }
.field label, .label { font-weight: var(--weight-medium); font-size: var(--text-sm); }
.field .hint { font-size: var(--text-sm); color: var(--color-text-muted); }
.field-error { font-size: var(--text-sm); color: var(--color-danger); font-weight: var(--weight-medium); }
input[type="text"], input[type="email"], input[type="password"], input[type="number"], input[type="date"],
input[type="search"], input[type="tel"], input[type="url"], select, textarea {
  width: 100%; min-height: var(--tap-min); padding: var(--space-2) var(--space-3);
  font: inherit; color: var(--color-text); background: var(--color-surface);
  border: 1px solid var(--color-border-strong); border-radius: var(--radius-md);
}
textarea { min-height: 6rem; resize: vertical; }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 3px solid var(--color-focus); outline-offset: 0; border-color: var(--color-focus); }
.is-invalid, .is-invalid:focus-visible { border-color: var(--color-danger); }
input[type="file"] { font: inherit; }
.checkbox { display: flex; align-items: flex-start; gap: var(--space-2); margin-bottom: var(--space-3); min-height: var(--tap-min); }
.checkbox input { width: 1.25rem; height: 1.25rem; margin-top: 0.15rem; accent-color: var(--color-primary); }
fieldset { border: 1px solid var(--color-border); border-radius: var(--radius-lg); padding: var(--space-4); margin: 0 0 var(--space-5); }
legend { font-weight: var(--weight-bold); padding: 0 var(--space-2); }
.form-row { display: grid; gap: 0 var(--space-4); }
@media (min-width: 640px) { .form-row { grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)); } }
.form-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); justify-content: flex-end; margin-top: var(--space-4); }
.search { display: flex; gap: var(--space-2); margin-bottom: var(--space-4); }
.search input { flex: 1; }

/* ---------- Belegpositionen (Editor) ---------- */
.lines { display: flex; flex-direction: column; gap: var(--space-3); }
.line-row { display: grid; gap: var(--space-2); grid-template-columns: 1fr 1fr; padding: var(--space-3); border: 1px solid var(--color-border); border-radius: var(--radius-md); background: var(--color-surface-2); }
.line-row .field { margin: 0; }
.line-row .line-name, .line-row .line-desc, .line-row .line-article { grid-column: 1 / -1; }
.line-row .line-remove { grid-column: 1 / -1; justify-self: end; }
@media (min-width: 1100px) {
  .line-row { grid-template-columns: 3fr 1fr 1.2fr 1.3fr 1fr auto; align-items: end; }
  .line-row .line-name { grid-column: 1 / 2; }
  .line-row .line-article { grid-column: 1 / -1; }
  .line-row .line-desc { grid-column: 1 / -2; }
  .line-row .line-remove { grid-column: auto; align-self: end; }
}
.totals { margin-left: auto; max-width: 22rem; font-variant-numeric: tabular-nums; }
.totals div { display: flex; justify-content: space-between; gap: var(--space-4); padding: var(--space-1) 0; }
.totals .grand { font-weight: var(--weight-bold); font-size: var(--text-lg); border-top: 1px solid var(--color-border-strong); margin-top: var(--space-1); padding-top: var(--space-2); }

/* ---------- Tabellen & Listen ---------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th { text-align: left; font-size: var(--text-sm); color: var(--color-text-muted); font-weight: var(--weight-medium); padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--color-border-strong); }
.table td { padding: var(--space-3); border-bottom: 1px solid var(--color-border); vertical-align: top; }
.table tr:hover td { background: var(--color-surface-2); }
.table .num { text-align: right; white-space: nowrap; }
/* Mobil: Tabellenzeilen werden zu Karten (td[data-label]) */
@media (max-width: 767px) {
  .table.responsive thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .table.responsive tr { display: block; border: 1px solid var(--color-border); border-radius: var(--radius-md); margin-bottom: var(--space-3); background: var(--color-surface); }
  .table.responsive td { display: flex; justify-content: space-between; gap: var(--space-3); border: 0; padding: var(--space-2) var(--space-3); }
  .table.responsive td::before { content: attr(data-label); color: var(--color-text-muted); font-size: var(--text-sm); }
  .table.responsive td.num { text-align: right; }
}
.list { list-style: none; margin: 0; padding: 0; }
.list-item { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); padding: var(--space-3) 0; border-bottom: 1px solid var(--color-border); text-decoration: none; color: inherit; min-height: var(--tap-min); }
.list-item:last-child { border-bottom: 0; }
a.list-item:hover { background: var(--color-surface-2); }
.list-title { font-weight: var(--weight-medium); }
.list-meta { font-size: var(--text-sm); color: var(--color-text-muted); }
.list-amount { font-weight: var(--weight-bold); white-space: nowrap; font-variant-numeric: tabular-nums; text-align: right; }
dl.details { display: grid; grid-template-columns: max-content 1fr; gap: var(--space-1) var(--space-4); margin: 0; }
dl.details dt { color: var(--color-text-muted); font-size: var(--text-sm); }
dl.details dd { margin: 0; }

/* ---------- Statuschips ---------- */
.chip { display: inline-flex; align-items: center; gap: var(--space-1); padding: 0.125rem var(--space-2); border-radius: var(--radius-pill); font-size: var(--text-xs); font-weight: var(--weight-bold); line-height: 1.5; white-space: nowrap; background: var(--color-neutral-bg); color: var(--color-neutral); }
.chip::before { content: ""; width: 0.5rem; height: 0.5rem; border-radius: 50%; background: currentColor; }
.chip-draft { background: var(--color-neutral-bg); color: var(--color-neutral); }
.chip-sent, .chip-issued, .chip-info { background: var(--color-info-bg); color: var(--color-info); }
.chip-accepted, .chip-paid, .chip-success, .chip-active { background: var(--color-success-bg); color: var(--color-success); }
.chip-rejected, .chip-cancelled, .chip-danger, .chip-suspended { background: var(--color-danger-bg); color: var(--color-danger); }
.chip-invoiced, .chip-warning, .chip-overdue, .chip-pending_approval, .chip-pending_email { background: var(--color-warning-bg); color: var(--color-warning); }

/* ---------- Hinweise & Toasts ---------- */
.alert { padding: var(--space-3) var(--space-4); border-radius: var(--radius-md); border-left: 4px solid currentColor; margin-bottom: var(--space-4); background: var(--color-info-bg); color: var(--color-text); }
.alert strong { color: inherit; }
.alert-info { border-color: var(--color-info); background: var(--color-info-bg); }
.alert-success { border-color: var(--color-success); background: var(--color-success-bg); }
.alert-warning { border-color: var(--color-warning); background: var(--color-warning-bg); }
.alert-danger { border-color: var(--color-danger); background: var(--color-danger-bg); }
.alert ul { margin: var(--space-2) 0 0; padding-left: var(--space-5); }
.toast-region { position: fixed; z-index: 50; left: var(--space-3); right: var(--space-3); bottom: calc(var(--tabbar-height) + var(--space-3)); display: flex; flex-direction: column; gap: var(--space-2); pointer-events: none; }
@media (min-width: 768px) { .toast-region { left: auto; right: var(--space-5); bottom: var(--space-5); width: 24rem; } }
.toast { pointer-events: auto; display: flex; gap: var(--space-3); align-items: flex-start; padding: var(--space-3) var(--space-4); border-radius: var(--radius-md); background: var(--color-surface); color: var(--color-text); box-shadow: var(--shadow-2); border-left: 4px solid var(--color-info); animation: toast-in var(--duration-normal) ease-out; }
.toast-success { border-left-color: var(--color-success); }
.toast-error { border-left-color: var(--color-danger); }
.toast-warning { border-left-color: var(--color-warning); }
.toast button { margin-left: auto; background: none; border: 0; color: var(--color-text-muted); font: inherit; cursor: pointer; min-width: 2rem; min-height: 2rem; }
@keyframes toast-in { from { transform: translateY(0.5rem); opacity: 0; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .toast { animation: none; } }

/* ---------- Leere Zustände ---------- */
.empty { text-align: center; padding: var(--space-7) var(--space-4); color: var(--color-text-muted); }
.empty .icon { width: 2.5rem; height: 2.5rem; color: var(--color-border-strong); margin-bottom: var(--space-3); }
.empty h2 { color: var(--color-text); font-size: var(--text-lg); }

/* ---------- Öffentliche Seiten ---------- */
.public { min-height: 100vh; display: flex; flex-direction: column; }
.public-header { padding: var(--space-4); display: flex; justify-content: space-between; align-items: center; max-width: var(--content-max); width: 100%; margin: 0 auto; }
.public-main { flex: 1; width: 100%; max-width: var(--content-max); margin: 0 auto; padding: var(--space-4); }
.auth-card { max-width: 28rem; margin: var(--space-6) auto; }
.hero { padding: var(--space-7) 0 var(--space-6); max-width: 44rem; }
.hero h1 { font-size: var(--text-3xl); }
.hero p { font-size: var(--text-lg); color: var(--color-text-muted); }
.features { display: grid; gap: var(--space-4); }
@media (min-width: 768px) { .features { grid-template-columns: repeat(3, 1fr); } }
.public-footer { padding: var(--space-5) var(--space-4); text-align: center; font-size: var(--text-sm); color: var(--color-text-muted); }
.secret-box { font-family: var(--font-mono); word-break: break-all; padding: var(--space-3); background: var(--color-surface-2); border: 1px dashed var(--color-border-strong); border-radius: var(--radius-md); user-select: all; }
.htmx-indicator { opacity: 0; transition: opacity var(--duration-fast); }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }
.swatch { height: 3rem; border-radius: var(--radius-md); border: 1px solid var(--color-border); }
.nav-button { width: 100%; border: 0; background: none; cursor: pointer; font: inherit; text-align: left; }
.mt-0 { margin-top: 0; }
.mb-0 { margin-bottom: 0; }
.qr-preview { width: 8rem; height: 8rem; }
.section-gap { margin-top: var(--space-5); }

/* Styleguide-Farbfelder */
.sw-bg { background: var(--color-bg); }
.sw-surface { background: var(--color-surface); }
.sw-surface-2 { background: var(--color-surface-2); }
.sw-border { background: var(--color-border); }
.sw-border-strong { background: var(--color-border-strong); }
.sw-text { background: var(--color-text); }
.sw-text-muted { background: var(--color-text-muted); }
.sw-primary { background: var(--color-primary); }
.sw-primary-hover { background: var(--color-primary-hover); }
.sw-on-primary { background: var(--color-on-primary); }
.sw-primary-soft { background: var(--color-primary-soft); }
.sw-success { background: var(--color-success); }
.sw-success-bg { background: var(--color-success-bg); }
.sw-warning { background: var(--color-warning); }
.sw-warning-bg { background: var(--color-warning-bg); }
.sw-danger { background: var(--color-danger); }
.sw-danger-hover { background: var(--color-danger-hover); }
.sw-danger-bg { background: var(--color-danger-bg); }
.sw-info { background: var(--color-info); }
.sw-info-bg { background: var(--color-info-bg); }
.sw-neutral { background: var(--color-neutral); }
.sw-neutral-bg { background: var(--color-neutral-bg); }
.sw-focus { background: var(--color-focus); }
.sw-overlay { background: var(--color-overlay); }
.sg-swatches { display: grid; gap: var(--space-3); grid-template-columns: repeat(auto-fill, minmax(9rem, 1fr)); }
.sg-swatches figure { margin: 0; }
.sg-swatches figcaption { font-size: var(--text-xs); margin-top: var(--space-1); word-break: break-all; }
.sg-text-xs { font-size: var(--text-xs); }
.sg-text-sm { font-size: var(--text-sm); }
.sg-text-base { font-size: var(--text-base); }
.sg-text-lg { font-size: var(--text-lg); }
.sg-text-xl { font-size: var(--text-xl); }
.sg-text-2xl { font-size: var(--text-2xl); }
.sg-text-3xl { font-size: var(--text-3xl); }
.sg-weight-normal { font-weight: var(--weight-normal); }
.sg-weight-medium { font-weight: var(--weight-medium); }
.sg-weight-bold { font-weight: var(--weight-bold); }
.sg-space { display: block; height: 1rem; background: var(--color-primary); border-radius: var(--radius-sm); }
.sg-space-1 { width: var(--space-1); }
.sg-space-2 { width: var(--space-2); }
.sg-space-3 { width: var(--space-3); }
.sg-space-4 { width: var(--space-4); }
.sg-space-5 { width: var(--space-5); }
.sg-space-6 { width: var(--space-6); }
.sg-space-7 { width: var(--space-7); }
.sg-space-8 { width: var(--space-8); }
.sg-radius { width: 4rem; height: 3rem; background: var(--color-primary-soft); border: 1px solid var(--color-primary); }
.sg-radius-sm { border-radius: var(--radius-sm); }
.sg-radius-md { border-radius: var(--radius-md); }
.sg-radius-lg { border-radius: var(--radius-lg); }
.sg-radius-pill { border-radius: var(--radius-pill); }
.sg-shadow-1 { box-shadow: var(--shadow-1); }
.sg-shadow-2 { box-shadow: var(--shadow-2); }
.sg-toast-demo { position: static; width: auto; max-width: 24rem; }
.sg-icons { display: grid; gap: var(--space-3); grid-template-columns: repeat(auto-fill, minmax(7rem, 1fr)); }
.sg-icons figure { margin: 0; display: flex; flex-direction: column; align-items: center; gap: var(--space-1); padding: var(--space-3); border: 1px solid var(--color-border); border-radius: var(--radius-md); }
