*, *::before, *::after { box-sizing: border-box; }

html { font-size: 112.5%; }
/* Font size setting (spec 022): 87.5 % and 115 % of the medium 18 px. */
html[data-font-size="small"] { font-size: 98.4375%; }
html[data-font-size="large"] { font-size: 129.375%; }
/* Desktop (spec 033): one step smaller at reading distance; phones keep the sizes above. */
@media (min-width: 648px) {
  html { font-size: 100%; }                             /* medium 16 px */
  html[data-font-size="small"] { font-size: 93.75%; }   /* 15 px */
  html[data-font-size="large"] { font-size: 112.5%; }   /* 18 px */
}

body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: var(--text-body);
  line-height: 1.5;
  background-color: var(--color-bg);
  color: var(--color-fg);
}

a { color: var(--color-link); }
/* Form controls use the page font, not the browser's control font (spec 029). */
button, input, select, textarea { font: inherit; }

/* Type scale (spec 029, tokens in tokens.css). */
h1, h2, h3 { line-height: 1.2; }
h1 { font-size: var(--text-h1); font-weight: 700; }
h2 { font-size: var(--text-h2); font-weight: 700; }
h3 { font-size: var(--text-h3); font-weight: 600; }
.text-display { font-size: var(--text-display); font-weight: 800; }
.text-label { font-size: var(--text-label); font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; }
/* Numbers: .num for numeric cells (tabular, right-aligned), .amount for money only. */
.num { font-variant-numeric: tabular-nums; text-align: right; }
.amount { font-size: var(--text-amount); font-weight: 600; font-variant-numeric: tabular-nums; }
.amount strong { font-weight: 700; }  /* the grand total stays 700, not bolder than 600 */
p.num-prose { font-variant-numeric: tabular-nums; }  /* amounts inside translated sentences */

:focus-visible { outline: 3px solid var(--color-focus); outline-offset: 2px; }
/* Focus scrolling stops at the viewport edge; leave room for the ring (3 + 2 px). */
html { scroll-padding-block: 0.5rem; }

.skip-link { position: absolute; left: -9999px; }
.skip-link:focus { left: var(--space-4); top: var(--space-4); z-index: 40; }

.site-header {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-4);
  align-items: center;
  justify-content: space-between;
  padding-block: var(--space-3);
  /* Spec 040 §3.3: the bar stays full width, its content lines up with .page. */
  padding-inline: round(down, max(var(--space-6), calc((100% - 68.9rem) / 2 + var(--space-6))), 1px);
  border-bottom: 1px solid var(--color-border);
  background-color: var(--color-surface);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--color-fg);
  font-weight: 700;
  font-size: 1.25rem;
  text-decoration: none;
}

.brand { min-width: 44px; min-height: 44px; }
.brand__name { font-weight: 800; letter-spacing: -0.03em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 10rem; }

/* Header navigation and menu (spec 017). */
.site-header { flex-wrap: nowrap; }
.site-header__nav { display: flex; gap: var(--space-2); align-items: center; margin-left: auto; }
/* Spec 022: every entry has a transparent frame, the current one shows it (no layout shift). */
.site-header__nav a {
  position: relative; display: inline-flex; align-items: center; gap: var(--space-1); min-height: 44px;
  padding: 0 var(--space-1); border: 2px solid transparent; border-radius: var(--radius); text-decoration: none;
}
.site-header__nav a[aria-current="page"] {
  background-color: var(--color-nav-active-bg); color: var(--color-nav-active-fg);
}
.site-header svg { width: 20px; height: 20px; flex: 0 0 auto; }
.site-header__nav svg, .menu-button svg { width: 24px; height: 24px; }
/* Menu bar style (spec 022); the hamburger menu always shows icon and text. */
html[data-menu-style="icons"] .site-header__nav a span {
  position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap;
}
html[data-menu-style="text"] .site-header__nav svg { display: none; }
.header-actions { display: flex; align-items: center; gap: var(--space-2); }
.menu-button {
  flex: 0 0 auto;
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 44px; min-height: 44px;
  border: 1px solid var(--color-border); border-radius: var(--radius);
  background-color: var(--color-bg); color: var(--color-fg); cursor: pointer;
}
.app-menu {
  box-shadow: var(--shadow-overlay);
  /* --menu-top: below the header; app.js measures it, these are the no-JS values. */
  --menu-top: 76px;
  position: fixed; inset: auto var(--space-4) auto auto; top: var(--menu-top); margin: 0;
  width: min(20rem, 100vw - 2 * var(--space-4));
  max-height: calc(100dvh - var(--menu-top) - var(--space-2)); overflow-y: auto;
  padding: var(--space-3);
  border: 1px solid var(--color-border); border-radius: var(--radius);
  background-color: var(--color-surface); color: var(--color-fg);
}
.app-menu nav { display: grid; gap: var(--space-2); }
.app-menu form { margin: 0; }
.app-menu__entry {
  display: flex; align-items: center; gap: var(--space-2); width: 100%; min-height: 44px;
  padding: 0 var(--space-2); font: inherit; color: var(--color-link);
  background: none; border: 0; border-radius: var(--radius); text-decoration: none; cursor: pointer;
}
button.app-menu__entry { color: var(--color-fg); }
/* Spec 032: who is signed in, the divider, and the light/dark switch. */
.app-menu__user { padding: 0 var(--space-2); font-weight: 700; overflow-wrap: anywhere; }
.app-menu__divider { width: 100%; margin: 0; border: 0; border-top: 1px solid var(--color-border); }
/* The switch shows the action for the effective scheme; pure CSS so it also
   works without JavaScript and follows the device in "system" mode. */
.mode-to-light { display: none; }
[data-theme="dark"] .mode-to-dark { display: none; }
[data-theme="dark"] .mode-to-light { display: inline-flex; }
@media (prefers-color-scheme: dark) {
  [data-theme="system"] .mode-to-dark { display: none; }
  [data-theme="system"] .mode-to-light { display: inline-flex; }
}
.table-wrap { overflow-x: auto; position: relative; }  /* contains .visually-hidden headers */
/* Fields never push the page wider than the screen (spec 017). */
fieldset { min-width: 0; }
input, select, textarea { max-width: 100%; min-width: 0; }
.field, .field__label label { min-width: 0; }
/* Spec 044: the ? keeps its place after the label text; a long label wraps
   inside itself instead of pushing the ? onto a line of its own. */
.field__label { flex-wrap: nowrap; max-width: 100%; }
h1, h2 { overflow-wrap: break-word; hyphens: auto; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(16rem, 100%), 1fr)); gap: 0 var(--space-4); }
.form-grid > * { min-width: 0; }
.form-grid > h2, .form-grid__item--full { grid-column: 1 / -1; }
.form-grid__item .field input:not([type="checkbox"], [type="radio"]),
.form-grid__item .field select { width: 100%; }


.inline-form { display: inline; margin: 0; }

/* Empty states (spec 040 §4.6): icon with marker dot, heading, one sentence, action. */
.empty-state { display: flex; flex-direction: column; align-items: center; gap: var(--space-2); text-align: center; }
.empty-state__media { position: relative; width: 64px; height: 64px; }
.empty-state__icon { display: block; width: 64px; height: 64px; color: var(--color-fg-muted); }
.empty-state__marker {
  position: absolute; top: 0; right: 0; width: 10px; height: 10px; border-radius: 50%;
  background-color: var(--color-marker-border);
}
.empty-state__title { margin: 0; font-size: var(--text-h3); font-weight: 600; }

/* Payment method as a segmented control; the radios stay native (spec 030).
   The track is the notice surface; the selected segment takes the primary
   colour (spec 033), so the choice stands out in every theme. */
[data-testid="payment-method"] > div {
  display: flex; gap: 4px; width: 100%; padding: 4px;
  border: 1px solid var(--color-border); border-radius: var(--radius-md);
  background-color: var(--color-notice-bg);
}
[data-testid="payment-method"] > div > div { flex: 1 1 0; display: flex; min-width: 0; }
[data-testid="payment-method"] label {
  flex: 1 1 auto; display: flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 0 var(--space-3);
  border-radius: var(--radius-sm); color: var(--color-fg-muted);
  cursor: pointer;
}
[data-testid="payment-method"] label:not(:has(input:checked)):hover { color: var(--color-fg); }
[data-testid="payment-method"] input { position: absolute; opacity: 0; width: 1px; height: 1px; }
[data-testid="payment-method"] label:has(input:checked) {
  background-color: var(--color-primary); color: var(--color-primary-fg); font-weight: 700;
  box-shadow: 0 1px 2px rgb(15 23 42 / 0.18);
}
[data-testid="payment-method"] label:has(input:focus-visible) {
  outline: 3px solid var(--color-focus); outline-offset: 2px;
}

/* Dates row of the invoice editor: a fixed four-column grid, so hiding the
   payment term for a cash invoice does not resize the other fields. */
.invoice-form__dates {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, auto);
  align-items: start; column-gap: var(--space-3); row-gap: var(--space-1);
  margin-bottom: var(--space-4);
}
/* Spec 019: the tallest label in the row sets every label's height, so the
   inputs stay on one line even when one label carries the help button. */
.invoice-form__dates .field { grid-row: span 4; display: grid; grid-template-rows: subgrid; margin-bottom: 0; }
.invoice-form__dates [data-payment-term] { display: contents; }

/* Appearance in "Meine Einstellungen" (spec 032): labels above full-width
   selects, then the button (no-JS only). */
.theme-switcher { display: grid; gap: var(--space-2); justify-items: stretch; margin: var(--space-6) 0 0; }
.theme-switcher__head { display: flex; align-items: center; gap: var(--space-2); }
.theme-switcher__head h2 { margin: 0; }
.theme-switcher select { width: 100%; min-height: 44px; }
.theme-switcher .button { justify-self: start; min-height: 44px; }

.page { max-width: 68.9rem; margin: 0 auto; padding: var(--space-8) var(--space-6); }
.page--narrow { max-width: 32rem; }
/* Customer and service capture (spec 044): room for two columns of fields. */
.page--form { max-width: 48rem; }

.card {
  padding: var(--space-6);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  background-color: var(--color-surface);
  box-shadow: none;
}

.lead { font-size: 1.15rem; }

.field { display: flex; flex-direction: column; gap: var(--space-1); margin-bottom: var(--space-4); }
.field__error { margin: 0; color: var(--color-danger); }
.field__errors { color: var(--color-danger); font-size: 16px; font-weight: 500; }
.field--invalid :is(input:not([type="hidden"]), select, textarea) { border: 2px solid var(--color-danger); }
.field__required { color: var(--color-danger); font-weight: 700; }
.field__hint { margin: 0; color: var(--color-fg-muted); font-size: var(--text-hint); }

input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not(.date-field__picker),
select {
  min-height: 48px;
}
input, select {
  font: inherit;
  color: var(--color-fg);
  background-color: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: var(--space-2) var(--space-3);
}

.button {
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--color-primary);
  border-radius: var(--radius);
  padding: var(--space-2) var(--space-4);
  background-color: var(--color-primary);
  color: var(--color-primary-fg);
}

.button {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 48px;
}
.button:hover { background-color: var(--color-primary-hover); }
.button--secondary {
  background-color: transparent;
  color: var(--color-fg);
  border-color: var(--color-border);
}
.button--secondary:hover { background-color: var(--color-notice-bg); }
/* Pale destructive action (Farben board); the solid red stays for the final
   confirmation of an irreversible step. */
.button--danger-soft {
  background-color: var(--color-danger-bg);
  color: var(--color-danger);
  border-color: var(--color-danger-bg);
}
/* The pale surface keeps the text's contrast in every theme; the border marks the hover. */
.button--danger-soft:hover { background-color: var(--color-danger-bg); border-color: var(--color-danger); }
.button--link {
  background: none; border-color: transparent; color: var(--color-link);
  padding-inline: var(--space-1);
}
.button--link:hover { background: none; text-decoration: underline; }
.button:disabled, .button[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; }
.button:disabled:hover { background-color: var(--color-primary); }

.form-error {
  padding: var(--space-3);
  border-radius: var(--radius);
  color: var(--color-danger);
  background-color: var(--color-danger-bg);
  border: 1px solid var(--color-danger);
}

.qr { display: inline-block; padding: var(--space-2); background: #ffffff; border-radius: var(--radius); }
.qr svg { display: block; }
/* Spec 045: the logo on its paper colour in every theme; forms stay inside 320 px. */
.logo-section input[type="file"] { max-width: 100%; }
.logo-section form { margin-top: var(--space-3); }
.logo-tile { display: inline-block; max-width: 100%; padding: var(--space-2); background: #ffffff; border-radius: var(--radius); }
.logo-tile img { display: block; max-width: 100%; max-height: 6rem; }
.secret { font-family: var(--font-mono); word-break: break-all; }

/* Help: ? button, tooltip (hover/focus) and popover. Spec 044: a thin ring in
   the primary colour instead of a filled circle; the 44 px touch target is the
   invisible ::before. */
.help { --help-size: 24px; position: relative; display: inline-block; font-size: 1rem; font-weight: 400; }
.help--lg { --help-size: 28px; }

.help__button {
  position: relative;
  width: var(--help-size);
  height: var(--help-size);
  margin-inline-start: var(--space-1);
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--color-fg);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 700;
  line-height: 1;
  vertical-align: middle;
  cursor: pointer;
}
/* The drawn circle is smaller than the box, so it never reaches above or below
   the label's line box (spec 044 AC-3). */
.help__button::after {
  content: "";
  position: absolute;
  inset: 3px;
  border: 1.5px solid var(--color-primary);  /* the ring is a graphic, the ? keeps the text contrast */
  border-radius: 50%;
}
.help__button::before { content: ""; position: absolute; inset: calc((var(--help-size) - 44px) / 2); }
/* Interactive neighbours paint above that invisible target, so a tap on a label,
   field, link or button next to the ? keeps its own meaning (spec 033 §3.1). */
:where(main, dialog) :where(a, button:not(.help__button), input, select, textarea, label, summary) {
  position: relative; z-index: 1;
}

.help__tooltip {
  display: none;
  position: absolute;
  z-index: 20;
  top: 100%;
  left: 0;
  transform: translateX(var(--help-shift-x, 0px));  /* set by help-tooltip.js */
  width: max-content;
  max-width: min(18rem, calc(100vw - 2 * var(--space-4)));
  overflow-wrap: anywhere;
  pointer-events: none;  /* never covers the target or the field below */
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius);
  background-color: var(--color-tooltip-bg);
  color: var(--color-tooltip-fg);
  font-size: 0.9rem;
  box-shadow: var(--shadow-overlay);
}

.help__button:hover + .help__tooltip,
.help__button:focus + .help__tooltip { display: block; }
.help__tooltip[data-placement="top"] { top: auto; bottom: 100%; }
/* Inside clipping containers a field's hint spans that field, so it wraps to the
   dialog width instead of being cut off (spec 033 §3.2). */
:is(.app-menu, .editor-dialog, .table-wrap) .field { position: relative; }
:is(.app-menu, .editor-dialog, .table-wrap) .field .help { position: static; }
:is(.app-menu, .editor-dialog, .table-wrap) .field .help__tooltip {
  right: 0; width: auto; max-width: none; transform: none;
}

.help__popover {
  box-shadow: var(--shadow-overlay);
  border-radius: var(--radius-lg);
  max-width: min(32rem, calc(100vw - 2 * var(--space-4)));
  max-height: calc(100dvh - 2 * var(--space-4));
  overflow-y: auto;
  overflow-wrap: anywhere;
  padding: var(--space-6);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background-color: var(--color-surface);
  color: var(--color-fg);
}

/* Spec 040 §4.8: no loading text, only room for the text that is on its way. */
.help__popover[aria-busy="true"], .help__popover.htmx-request { min-height: 6rem; }
.help__title { margin-top: 0; font-size: 1.2rem; }

.form-footer { margin-top: var(--space-4); }

/* Forms: labels with help, radio groups, actions; setup wizard (spec 004). */
fieldset.field { border: 0; padding: 0; margin: 0 0 var(--space-4); }
fieldset.field legend { padding: 0; margin-bottom: var(--space-1); }
fieldset.field ul { list-style: none; padding: 0; margin: 0; display: grid; gap: var(--space-1); }
.field__label { display: inline-flex; gap: var(--space-2); align-items: center; }
.field--checkbox { flex-direction: row-reverse; justify-content: flex-end; align-items: center; gap: var(--space-2); }
textarea {
  min-height: 48px;
  font: inherit;
  color: var(--color-fg);
  background-color: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: var(--space-2) var(--space-3);
}
/* "Next" comes first in the DOM, so Enter in a field moves on; Back shows left. */
.form-actions { display: flex; flex-direction: row-reverse; justify-content: space-between; gap: var(--space-4); margin-top: var(--space-6); }
.setup-progress { margin: 0 0 var(--space-2); color: var(--color-fg-muted); font-size: 0.95rem; }
.number-preview { padding: var(--space-3); border: 1px dashed var(--color-border); border-radius: var(--radius); }
.number-preview__error { color: var(--color-danger); }
.edit-link { font-size: 0.9rem; font-weight: 400; margin-left: var(--space-2); }
.summary-list { display: grid; grid-template-columns: max-content 1fr; gap: var(--space-1) var(--space-4); }
.summary-list dt { font-weight: 600; }
.summary-list dd { margin: 0; }
.notice {
  padding: var(--space-3); border: 1px solid var(--color-notice-border);
  border-radius: var(--radius-md); background-color: var(--color-notice-bg);
  color: var(--color-notice-fg);
}
.notice--hint {
  border-color: var(--color-marker-border); background-color: var(--color-marker-bg);
  color: var(--color-marker-fg);
}

/* Documents (spec 005). */
.actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.filters { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: end; margin-bottom: var(--space-4); }
/* A filter label must not size itself from the input's default width (spec 029
   AC-17a: the search field is 332 px wide inside a 279 px card at 320 px). */
.filters label { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; max-width: 100%; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: var(--space-2); border-bottom: 1px solid var(--color-border); }
.table thead th {
  font-size: 0.778rem; font-weight: 600; letter-spacing: 0.06em;
  text-transform: uppercase; color: var(--color-fg-muted);
}
.table tbody tr:hover { background-color: var(--color-notice-bg); }
.table .num { text-align: right; white-space: nowrap; }
.table .select-cell { width: 1%; }  /* bulk download checkboxes (spec 025) */
.upload-results { padding-left: var(--space-4); }
.upload-result--rejected { color: var(--color-danger); }
.suggestion { font-size: 0.85rem; color: var(--color-fg-muted); }
.meta { color: var(--color-fg-muted); }
.button--danger { background-color: var(--color-danger); border-color: var(--color-danger); }
.button--danger:hover { background-color: color-mix(in srgb, var(--color-danger) 85%, black); }
.einvoice__parties { display: flex; flex-wrap: wrap; gap: var(--space-8); }
a.button { text-decoration: none; }

/* Invoices (spec 006). */
.badge { display: inline-block; font-size: 0.8rem; font-weight: 600; padding: 0 var(--space-2); border: 1px solid var(--color-border); border-radius: var(--radius); vertical-align: middle; }
/* State chips (spec 030): lifecycle, payment standing and the overdue marker. */
.chip {
  display: inline-flex; align-items: center; gap: var(--space-1);
  min-height: 24px; padding: 0 var(--space-2);
  border-radius: var(--radius-pill);
  font-size: 0.778rem; font-weight: 600; vertical-align: middle;
}
.chip__dot { width: 0.5rem; height: 0.5rem; border-radius: 50%; background-color: currentColor; flex: 0 0 auto; }
.chip--draft { color: var(--color-status-draft-fg); background-color: var(--color-status-draft-bg); }
.chip--issued { color: var(--color-status-issued-fg); background-color: var(--color-status-issued-bg); }
.chip--sent { color: var(--color-status-sent-fg); background-color: var(--color-status-sent-bg); }
.chip--paid { color: var(--color-status-paid-fg); background-color: var(--color-status-paid-bg); }
.chip--overdue { color: var(--color-status-overdue-fg); background-color: var(--color-status-overdue-bg); }
.chip--cancelled { color: var(--color-status-cancelled-fg); background-color: var(--color-status-cancelled-bg); }
/* Payment standing (spec 040 §4.7): neutral outline, so it never shares a pair with the lifecycle chip. */
.chip--payment { color: var(--color-fg); background-color: transparent; border: 1px solid var(--color-border); }
.badge--danger { color: var(--color-danger); border-color: var(--color-danger); }
.invoice-line { border: 1px solid var(--color-border); border-radius: var(--radius); padding: var(--space-3); margin: 0 0 var(--space-3); }
/* A row of fields shares one line for its inputs: the tallest label sets the
   height of every label in the row, hints and errors stay below the inputs (spec 019). */
.invoice-line__numbers {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 8rem), 1fr));
  grid-template-rows: repeat(4, auto);
  align-items: start;
  column-gap: var(--space-3);
  row-gap: var(--space-1);
  margin-bottom: var(--space-4);
}
.invoice-line__numbers .field {
  grid-row: span 4;
  display: grid;
  grid-template-rows: subgrid;
  margin-bottom: 0;
}
/* The cash-invoice wrapper must be the row's cell itself (spec 016, 019). */
.invoice-line__numbers [data-payment-term] { display: contents; }
/* A collapsed item's summary line (spec 019). */
.invoice-line__summary {
  display: block; width: 100%; margin: 0 0 var(--space-3);
  padding: var(--space-2) var(--space-3);
  font: inherit; text-align: left; white-space: normal;
  color: var(--color-fg); background-color: var(--color-bg);
  border: 1px solid var(--color-border); border-radius: var(--radius);
  cursor: pointer;
}
/* Spec 029 AC-17a: 24 rem is 496 px at the large font size, and a max-content
   label column pushes the value out of the card at 320 px; the label wraps
   and the value keeps its width. */
.invoice-totals { max-width: min(24rem, 100%); margin-left: auto; grid-template-columns: minmax(0, 1fr) auto; }
/* Long labels ("Gesamtbetrag", "Umsatzsteuer") must break instead of running into
   the value at 320 px with the large font size (spec 029 AC-17a). */
.invoice-totals dt { overflow-wrap: anywhere; }
.invoice-totals dd { text-align: right; }
/* Summen (Komponenten board): a rule above the gross row and a stronger value. */
.invoice-totals dt:last-of-type, .invoice-totals dd:last-of-type {
  border-top: 1px solid var(--color-border); padding-top: var(--space-1);
}
.invoice-totals dd:last-of-type { font-size: var(--text-h3); font-weight: 700; }

/* Invoice editor usability (spec 015). */
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
/* Date fields (spec 023): text field, calendar button (44 px tap target) and the
   picker it opens, kept rendered but invisible: browsers anchor the picker to it. */
.date-field { position: relative; display: inline-flex; align-items: flex-start; gap: var(--space-1); max-width: 100%; min-width: 0; }
.date-field > input[type="text"] { flex: 1 1 auto; min-width: 0; }
.date-field__button {
  flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center;
  min-width: 44px; min-height: 44px; padding: 0;
  border: 1px solid var(--color-border); border-radius: var(--radius);
  background-color: var(--color-bg); color: var(--color-fg); cursor: pointer;
}
.date-field__button svg { width: 20px; height: 20px; }
.date-field__picker {
  position: absolute; top: 0; right: 0; width: 1px; height: 1px; margin: 0; padding: 0;
  border: 0; opacity: 0; clip-path: inset(50%); pointer-events: none;
}
/* Touch (spec 033 §3.4): the invisible native input lies exactly over the button,
   so a tap opens the picker itself (iOS ignores showPicker from a script). */
@media (pointer: coarse) {
  .date-field__picker {
    width: 44px; height: 44px; z-index: 1;
    pointer-events: auto; clip-path: none;
    font-size: 16px;  /* >= 16 px: iOS does not zoom on focus */
  }
}
.date-field__button[hidden] ~ .date-field__picker { display: none; }
/* Spec 039: the item's service combobox; the list lies over the fields below it. */
.service-combobox { position: relative; display: flex; gap: var(--space-1); }
.service-combobox > input { flex: 1 1 auto; min-width: 0; }
.service-combobox > [role="listbox"] {
  position: absolute; top: 100%; left: 0; right: 0; z-index: 10;
  margin: var(--space-1) 0 0; padding: 0; list-style: none;
  max-height: 16rem; overflow-y: auto;
  color: var(--color-fg); background-color: var(--color-bg);
  border: 1px solid var(--color-border); border-radius: var(--radius);
  box-shadow: var(--shadow-overlay);
}
.service-combobox [role="option"] {
  min-height: 44px; padding: var(--space-2) var(--space-3);
  cursor: pointer; overflow-wrap: anywhere;
}
.service-combobox [role="option"]:hover { background-color: var(--color-notice-bg); }
.service-combobox [role="option"][data-current="true"] { font-weight: 600; }
.service-combobox [role="option"][aria-selected="true"] {
  color: var(--color-primary-fg); background-color: var(--color-primary);
}
/* The toggle is out of the tab order and hands focus to the input: it marks the focused pair. */
.service-combobox:focus-within > [data-service-toggle] { box-shadow: 0 0 0 2px var(--color-focus); }
.invoice-line textarea {
  width: 100%; line-height: 1.5; resize: vertical; overflow-wrap: anywhere;
}
/* Spec 039: a button that shows only an icon; its name is the aria-label. */
.button--icon { flex: 0 0 auto; min-width: 44px; min-height: 44px; padding: 0; }
.button--icon svg { width: 20px; height: 20px; }
@media (pointer: coarse) {
  .button--icon { min-width: 52px; min-height: 52px; }
}
.invoice-line__actions { margin-top: var(--space-2); }
.editor-dialog {
  box-shadow: var(--shadow-overlay);
  width: min(36rem, calc(100vw - 2 * var(--space-4)));
  padding: var(--space-6);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background-color: var(--color-surface);
  color: var(--color-fg);
}
.editor-dialog::backdrop { background-color: rgb(0 0 0 / 0.5); }
.editor-dialog h2 { margin-top: 0; }
.dialog-actions { display: flex; flex-wrap: wrap; gap: var(--space-3); margin-top: var(--space-4); }
/* Zoom (spec 024): wider pages scroll inside the page area, never the window. */
.zoom-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); margin-top: var(--space-4); }
.zoom-toolbar .button, .zoom-toolbar select { min-height: 44px; }
.zoom-toolbar .button:disabled { opacity: 0.5; cursor: not-allowed; }
.preview__pages { display: grid; gap: var(--space-4); margin: var(--space-4) 0; overflow-x: auto; }
.preview__pages img { display: block; max-width: 100%; height: auto; background: #ffffff; border: 1px solid var(--color-border); }
/* An installed app opens a downloaded file full screen with no way back (specs 015, 018). */
/* !important: .download-link alone loses to .button and to the phone rules for its buttons. */
@media (display-mode: standalone) { .download-link { display: none !important; } }
.share-status { margin: 0; align-self: center; }
/* Select all shown (spec 033): a visible label, 44 px high to tap. */
.list-toolbar__all { display: inline-flex; align-items: center; gap: var(--space-2); min-height: 44px; cursor: pointer; }

/* Spec 040. Page and section heads: the heading holds only its text (§4.8). */
.page-head__title, .section-head { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
.page-head__meta, .section-head__meta { display: inline-flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
/* Text links with a 44 px target (§3.5). */
.text-target, .table thead a { display: inline-flex; align-items: center; min-height: 44px; }
/* Row checkbox (§4.1): the label is the 44 px target, the box keeps its size. */
.row-select { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; min-height: 44px; cursor: pointer; }
.row-select input { flex: none; width: 1.25rem; height: 1.25rem; min-width: auto; max-width: none; margin: 0; padding: 0; }
/* Tables scroll inside .table-wrap, so their buttons stay on one line (§3.7). */
.table .button { white-space: nowrap; }
/* Start page (§4.4). */
.tile-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: var(--space-3); margin-bottom: var(--space-6); }
a.tile { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--space-2); min-height: 120px; padding: var(--space-2); text-align: center; }
.tile svg { width: 32px; height: 32px; }
.open-list { list-style: none; margin: 0; padding: 0; }
.open-list li {
  display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; align-items: center;
  gap: var(--space-1) var(--space-3); padding: var(--space-1) 0; border-bottom: 1px solid var(--color-border);
}
.open-list__name { overflow-wrap: anywhere; }
.open-list__chips { display: inline-flex; flex-wrap: wrap; gap: var(--space-1); }
/* Form pages (§4.3): from 648 px these are plain parts of the page. */
.form-header { display: contents; }
.form-header__back, .form-bar__total { display: none; }
/* Filters (§4.2): the icon button only exists on phones. */
.filter-button { display: none; }
.selection-toolbar .actions { margin-block: 1em; }
@media (min-width: 648px) {
  #filter-sheet:not(:popover-open) { display: contents; }
  /* Menu split (§4.12): the header bar has the navigation. */
  .app-menu__entry--nav, .app-menu__divider { display: none; }
}

/* Narrow screens (spec 017). Last in the file so they override the rules above;
   px, because rem in media queries ignores the root font size (64/36/48rem at 18 px). */
/* Tablets: logo and menu button on top, the navigation below (spec 017). */
@media (max-width: 1151.98px) {
  .site-header {
    display: grid; grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "brand button" "nav nav";
    padding: var(--space-2) var(--space-3);
    gap: var(--space-2);
  }
  .brand { grid-area: brand; }
  .brand__name { max-width: none; }
.header-actions { grid-area: button; justify-self: end; }
  .site-header__nav { grid-area: nav; flex-wrap: wrap; gap: 0; margin-left: 0; }
  .site-header__nav a {
    flex: 1 1 0; min-width: 0; min-height: 44px;
    flex-direction: column; justify-content: center; gap: 0;
    padding: 0 var(--space-1);
  }
  .site-header__nav a span {
    max-width: 100%; font-size: 0.75rem;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .app-menu { --menu-top: 125px; }
}

/* Phones: the six entries in two rows of three. */
@media (max-width: 647.98px) {
  .button, input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not(.date-field__picker), select { min-height: 52px; }
  .button--icon { min-width: 52px; }
  .site-header__nav a { flex-basis: 33.333%; padding-inline: 0; }
  /* Spec 029 AC-17a: three cells at 320 px leave ~96 px for a label, and the
     fallback stack (DejaVu Sans) needs 92 px for "Rechnungen" at the large
     font size; 0.7 rem keeps it unclipped with both the web font and the
     fallback. */
  .site-header__nav a span { font-size: 0.7rem; }
  html[data-menu-style="icons"] .site-header__nav a { flex-basis: auto; }  /* one row of six (spec 022) */
  .app-menu { --menu-top: 174px; }
  /* Labels like "Leistungsdatum" next to "Überweisung" do not fit in 320 px. */
  .summary-list:not(.invoice-totals) { grid-template-columns: minmax(0, 1fr); row-gap: 0; }
  .summary-list:not(.invoice-totals) dd + dt { margin-top: var(--space-2); }
}

/* Content on narrow screens. */
@media (max-width: 863.98px) {
  .page { padding: var(--space-4); }
  .invoice-line__numbers { grid-template-columns: minmax(0, 1fr); grid-template-rows: none; row-gap: 0; }
  .invoice-form__dates { grid-template-columns: minmax(0, 1fr); grid-template-rows: none; row-gap: 0; }
  .invoice-form__dates .field {
    grid-row: auto; display: flex; flex-direction: column; gap: var(--space-1);
    margin-bottom: var(--space-4);
  }
  .invoice-line__numbers .field {
    grid-row: auto; display: flex; flex-direction: column; gap: var(--space-1);
    margin-bottom: var(--space-4);
  }
  .card { padding: var(--space-4); }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .form-actions { flex-wrap: wrap; }
}

/* Statistics charts (spec 026): colours from the theme tokens, no inline styles (CSP). */
/* Money fields (spec 030): digits line up and the decimal keypad opens. */
input[inputmode="decimal"] { text-align: right; font-variant-numeric: tabular-nums; }

.chart { display: block; width: 100%; height: auto; margin: var(--space-4) 0; }
.chart--pie { max-width: 22rem; margin-inline: auto; }
.chart text { fill: var(--color-fg); }
.chart__grid { stroke: var(--color-border); stroke-width: 1; }
.chart__zero { stroke: var(--color-fg-muted); stroke-width: 1.5; }
.chart__income { fill: var(--color-chart-income); }
.chart__expense { fill: var(--color-chart-expense); }
/* Open invoices (spec 036): a dashed outline, so the series does not rely on colour alone. */
.chart__open { fill: var(--color-chart-open); stroke: var(--color-fg); stroke-width: 1.5; stroke-dasharray: 4 3; }
.chart__line { fill: none; stroke-width: 3; }
.chart__line--income { stroke: var(--color-chart-income); }
.chart__line--expense { stroke: var(--color-chart-expense); }
.chart__line--open { stroke: var(--color-chart-open); stroke-dasharray: 10 6; }
.chart__slice-1 { fill: var(--color-chart-1); }
.chart__slice-2 { fill: var(--color-chart-2); }
.chart__slice-3 { fill: var(--color-chart-3); }
.chart__slice-4 { fill: var(--color-chart-4); }
.chart__slice-5 { fill: var(--color-chart-5); }
.chart__slice-6 { fill: var(--color-chart-6); }
.chart__slice-7 { fill: var(--color-chart-7); }
.chart__slice-8 { fill: var(--color-chart-8); }
.chart__slice-9 { fill: var(--color-chart-9); }
.chart--pie path, .chart--pie circle { stroke: var(--color-bg); stroke-width: 2; }
.chart-legend { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); list-style: none; padding: 0; margin: 0 0 var(--space-4); }
.chart-legend li { display: inline-flex; align-items: center; gap: var(--space-1); }
.chart-legend__swatch { display: inline-block; width: 0.9em; height: 0.9em; border-radius: 2px; }
.chart-legend__swatch.chart__income { background: var(--color-chart-income); }
.chart-legend__swatch.chart__expense { background: var(--color-chart-expense); }
.chart-legend__swatch.chart__open { background: var(--color-chart-open); border: 1px dashed var(--color-fg); }
.chart-legend__swatch.chart__slice-1 { background: var(--color-chart-1); }
.chart-legend__swatch.chart__slice-2 { background: var(--color-chart-2); }
.chart-legend__swatch.chart__slice-3 { background: var(--color-chart-3); }
.chart-legend__swatch.chart__slice-4 { background: var(--color-chart-4); }
.chart-legend__swatch.chart__slice-5 { background: var(--color-chart-5); }
.chart-legend__swatch.chart__slice-6 { background: var(--color-chart-6); }
.chart-legend__swatch.chart__slice-7 { background: var(--color-chart-7); }
.chart-legend__swatch.chart__slice-8 { background: var(--color-chart-8); }
.chart-legend__swatch.chart__slice-9 { background: var(--color-chart-9); }
@media (max-width: 479px) { .chart__month--even { display: none; } }

/* Spec 021: invoice attachments; rows wrap on narrow screens. */
.attachment-list { list-style: none; padding: 0; margin: var(--space-2) 0; }
.attachment-list__item { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); padding: var(--space-2) 0; border-bottom: 1px solid var(--color-border); }
.attachment-list__name { overflow-wrap: anywhere; min-width: 0; }
.attachment-upload input[type="file"] { max-width: 100%; }

/* The page head is only a flex box on phones; on wider screens the wrapper is
   transparent so the desktop flow stays exactly as before. */
.page-head { display: contents; }

/* Mobile shell (spec 031): bottom tab bar and Plus sheet.
   Everything here only exists below the phone breakpoint. */
.tabbar, .plus-sheet { display: none; }

@media (max-width: 647.98px) {
  .site-header__nav { display: none; }

  .tabbar {
    position: fixed; inset-inline: 0; bottom: 0; z-index: 30;
    display: flex; align-items: stretch; justify-content: space-around;
    padding-bottom: env(safe-area-inset-bottom);
    border-top: 1px solid var(--color-border); background-color: var(--color-surface);
  }
  .tabbar a, .tabbar button {
    flex: 1 1 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px; min-height: 56px; padding: 0 var(--space-1);
    border: 0; background: none; color: var(--color-fg-muted);
    font: inherit; font-size: 0.7rem; text-decoration: none; cursor: pointer;
  }
  .tabbar svg { width: 26px; height: 26px; }
  .tabbar a[aria-current="page"] { background-color: var(--color-nav-active-bg); color: var(--color-nav-active-fg); }
  .tabbar .tabbar__plus {
    flex: 0 0 auto; align-self: center; width: 52px; height: 52px; min-height: 52px;
    margin-top: calc(-1 * var(--space-4));
    border-radius: 50%; background-color: var(--color-primary); color: var(--color-primary-fg);
  }
  .tabbar__plus svg { width: 24px; height: 24px; }

  .plus-sheet {
    position: fixed; inset: auto var(--space-3) calc(56px + env(safe-area-inset-bottom) + var(--space-2)) var(--space-3);
    display: block; margin: 0; padding: var(--space-2);
    border: 1px solid var(--color-border); border-radius: var(--radius-lg);
    background-color: var(--color-surface); box-shadow: var(--shadow-overlay);
  }
  .plus-sheet:not(:popover-open) { display: none; }
  .plus-sheet nav { display: grid; gap: var(--space-1); }

  /* Content clears the bar, including the iOS home indicator. */
  .page { padding-bottom: calc(56px + env(safe-area-inset-bottom) + var(--space-6)); }

  /* Rows become cards (spec 031); the select-all control moved to a toolbar. */
  .list-toolbar { display: flex; align-items: center; gap: var(--space-2); }
  .table--cards { display: block; }
  .table--cards thead {
    position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden;
    clip-path: inset(50%); white-space: nowrap;
  }
  /* Spec 035: the sort links stay in the tab order; reveal the header when focused. */
  .table--cards thead:focus-within {
    position: static; width: auto; height: auto; padding: var(--space-2) 0;
    overflow: visible; clip-path: none; white-space: normal;
  }
  .table--cards thead:focus-within tr { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-3); }
  .table--cards thead:focus-within th { display: block; }
  .table--cards tbody, .table--cards tr, .table--cards td { display: block; width: auto; }
  .table--cards tr {
    display: grid; grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "check check" "title amount" "status due" "number date";
    gap: var(--space-1) var(--space-3);
    padding: var(--space-3); margin-bottom: var(--space-3);
    border: 1px solid var(--color-border); border-radius: var(--radius-lg);
    background-color: var(--color-surface);
  }
  .table--cards td { border: 0; padding: 0; min-width: 0; }
  .table--cards tr:hover { background-color: var(--color-surface); }
  .cell-check { grid-area: check; }
  .cell-title { grid-area: title; font-weight: 600; overflow-wrap: anywhere; }
  .cell-amount { grid-area: amount; text-align: right; }
  .cell-status { grid-area: status; }
  .cell-due { grid-area: due; text-align: right; }
  .cell-number { grid-area: number; }
  .cell-date { grid-area: date; text-align: right; }
  .table--cards td[data-label]::before {
    content: attr(data-label) " "; color: var(--color-fg-muted); font-size: 0.778rem;
  }

  /* Phone page head: heading above its actions. */
  .page-head { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2) var(--space-4); margin-bottom: var(--space-3); }
  .page-head__title { flex: 1 1 0; min-width: 0; }
  .page-head h1 { margin: 0; }
  .page-head .actions { flex: 1 1 100%; margin-block: 0 var(--space-2); }
  .page-head .actions .button { flex: 1 1 100%; justify-content: center; text-align: center; }
  /* Spec 040 §3.7: tab bar and menu already offer these. */
  .nav-duplicate { display: none; }

  /* Spec 040 §3.4, §3.6, §4.12. */
  .help { --help-size: 22px; }
  .help--lg { --help-size: 26px; }
  textarea { min-height: 52px; }
  body.has-tabbar .menu-button { display: none; }
  /* §3.7: "Als neuen Entwurf kopieren" stays on one line at 320 px. */
  .button { padding-inline: var(--space-3); font-size: min(1rem, 5vw); }

  /* Spec 040 §4.2: the invoice filters as a native bottom sheet.
     shortcut: no popover or color-mix polyfill; revisit when an older browser is supported. */
  .filter-button { display: inline-flex; }
  .filters > label:first-child { flex: 1 1 100%; }
  .filters input[type="search"] { width: 100%; }
  #filter-sheet {
    inset: auto 0 0 0; width: 100%; max-width: 100%; max-height: 85dvh; overflow-y: auto; margin: 0;
    padding: var(--space-4) var(--space-4) calc(var(--space-4) + env(safe-area-inset-bottom));
    border: 1px solid var(--color-border); border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    background-color: var(--color-surface); color: var(--color-fg); box-shadow: var(--shadow-overlay);
  }
  #filter-sheet:popover-open { display: grid; gap: var(--space-3); }
  #filter-sheet:focus { outline: none; }
  #filter-sheet :is(input, select) { width: 100%; }
  #filter-sheet::backdrop { background-color: rgb(0 0 0 / 0.4); }

  /* Spec 040 §4.2: the download toolbar follows the cards and clears the tab bar. */
  form[data-bulk-download] { display: flex; flex-direction: column; }
  .selection-toolbar {
    order: 1; position: sticky; bottom: calc(56px + env(safe-area-inset-bottom) + var(--space-2)); z-index: 20;
    padding: var(--space-3); border: 1px solid var(--color-border); border-radius: var(--radius-lg);
    background-color: var(--color-surface); box-shadow: var(--shadow-overlay);
  }
  .selection-toolbar[data-none-selected] { display: none; }
  .selection-toolbar .actions { margin-block: 0; }
  .selection-toolbar ~ .list-toolbar { order: 2; }
  /* "Alle angezeigten herunterladen (125)" stays on one line at 320 px. */
  .download-link.actions .button {
    flex: 1 1 100%; justify-content: center; text-align: center; padding-inline: var(--space-2);
    font-size: min(0.778rem, 4.2vw);
  }

  /* Spec 040 §4.4. */
  .tile-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-2); }
  a.tile { padding-inline: var(--space-1); font-size: min(0.889rem, 3.8vw); }
  .open-list li { grid-template-columns: minmax(0, 1fr) auto; }
  .open-list .amount { grid-row: 1; grid-column: 2; }
  .open-list__name, .open-list__chips { grid-column: 1 / -1; }

  /* Spec 040 §4.3: form pages get their own header and fixed footer. */
  .form-page .site-header, .form-page .tabbar { display: none; }
  .form-page body { padding-bottom: calc(9rem + env(safe-area-inset-bottom)); }
  .form-page .page { padding-top: 0; padding-bottom: var(--space-4); }
  .form-page main > .card { padding: 0; border: 0; background: none; box-shadow: none; }
  /* Focused fields never end up behind the header (up to 6.8rem when its title wraps at 320 px) or the footer. */
  .form-page { scroll-padding: 7.5rem 0 10rem; }
  .form-header {
    position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: var(--space-2);
    min-height: 3.5rem; margin: 0 calc(-1 * var(--space-4)) var(--space-4);
    padding: calc(var(--space-1) + env(safe-area-inset-top)) var(--space-4) var(--space-1) var(--space-1);
    border-bottom: 1px solid var(--color-border); background-color: var(--color-bg);
  }
  [data-testid="invoice-editor"] .form-header { padding-right: 6rem; }
  .form-header h1 { margin: 0; font-size: 22px; }
  .form-header__back {
    flex: none; display: inline-flex; align-items: center; justify-content: center;
    width: 44px; height: 44px; color: var(--color-fg);
  }
  .form-header__back svg { width: 24px; height: 24px; }
  .form-page .form-bar {
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
    display: flex; flex-flow: row wrap; align-items: center; gap: var(--space-2); margin: 0;
    padding: var(--space-2) var(--space-3) calc(var(--space-2) + env(safe-area-inset-bottom));
    border-top: 1px solid var(--color-border); background-color: var(--color-surface);
  }
  .form-page .form-bar .button { flex: 1 1 0; min-width: 0; }
  .form-page .form-bar [data-testid="issue"] { flex-grow: 2; }
  .form-bar__total { flex: 1 1 100%; display: flex; align-items: center; justify-content: space-between; margin: 0; }
  .form-bar__total strong { font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums; }
  /* The full text stays the button's name (aria-label). */
  .form-bar [data-short] > span { display: none; }
  .form-bar [data-short]::before { content: attr(data-short); }
  /* One control at every width: on phones it sits in the sticky header as a text button. */
  .form-page .form-bar [data-testid="preview"] {
    position: fixed; top: calc(var(--space-1) + env(safe-area-inset-top)); right: var(--space-2);
    flex: none; min-height: 44px; padding-inline: var(--space-2);
    border-color: transparent; background: none; color: var(--color-link);
  }
}
/* Spec 036: the open pie slice keeps the dashed outline above the slice colour. */
.chart__slice-3.chart__open { fill: var(--color-chart-open); stroke: var(--color-fg); stroke-width: 1.5; stroke-dasharray: 4 3; }
