/* Kontor — Oberfläche. Farben ausschliesslich über Tokens aus tokens.css. */

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

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--ground);
  color: var(--ink);
  font-family: var(--f-ui);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
}

/* Muss vor allen Bauteilen stehen und !important tragen: .fab, .tabbar und
   .sidebar setzen selbst display, und jede Autorenregel schlaegt das
   display:none, mit dem der Browser das hidden-Attribut umsetzt. */
[hidden] {
  display: none !important;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 2px;
}

::selection {
  background: var(--accent);
  color: var(--accent-ink);
}

.num {
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

.pos {
  color: var(--pos);
}
.neg {
  color: var(--neg);
}
.zero {
  color: var(--muted);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ------------------------------------------------------------------ Gerüst */

#app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s3);
  min-height: var(--header-h);
  padding: var(--s2) var(--s4);
  padding-top: max(var(--s2), env(safe-area-inset-top));
  background: var(--ground);
  border-bottom: 1px solid var(--rule);
}

.app-header h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.015em;
}

.app-main {
  flex: 1;
  padding: var(--s4);
  padding-bottom: calc(var(--nav-h) + var(--s6) + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: var(--s4);
}

/* -------------------------------------------------------------- Navigation */

.tabbar {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 30;
  display: grid;
  /* auto-columns statt fester Spaltenzahl: eine weitere Route soll nicht
     zwingen, hier nachzuzaehlen. */
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  background: var(--surface);
  border-top: 1px solid var(--rule);
  padding-bottom: env(safe-area-inset-bottom);
}

.tabbar a {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: var(--nav-h);
  text-decoration: none;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 500;
  letter-spacing: 0.02em;
}

.tabbar a svg {
  width: 21px;
  height: 21px;
}

.tabbar a[aria-current='page'] {
  color: var(--accent);
}

.fab {
  position: fixed;
  right: var(--s4);
  bottom: calc(var(--nav-h) + var(--s3) + env(safe-area-inset-bottom));
  z-index: 31;
  width: 56px;
  height: 56px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.fab svg {
  width: 24px;
  height: 24px;
}

.sidebar {
  display: none;
}

/* ------------------------------------------------------------------ Karten */

.card {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: var(--s4);
  display: flex;
  flex-direction: column;
  gap: var(--s3);
}

.card-flush {
  padding: 0;
  overflow: hidden;
}

.card h2,
.section-title {
  margin: 0;
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.11em;
  color: var(--muted);
}

/* Kopfzahl: Gesamtvermögen */

.hero {
  display: flex;
  flex-direction: column;
  gap: var(--s1);
}

.hero .amount {
  font-size: clamp(2.1rem, 9vw, 2.9rem);
  font-weight: 600;
  letter-spacing: -0.035em;
  line-height: 1;
}

.hero .caption {
  font-size: 0.82rem;
  color: var(--muted);
}

/* Kennzahlenreihe */

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 1px;
  background: var(--rule);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  overflow: hidden;
}

.stat {
  background: var(--surface);
  padding: var(--s3) var(--s4);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat b {
  font-size: 0.62rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}

.stat span {
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

/* ------------------------------------------------------------------ Listen */

.list {
  display: flex;
  flex-direction: column;
}

.list-group-head {
  position: sticky;
  top: var(--header-h);
  z-index: 5;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s3);
  padding: var(--s2) var(--s4);
  background: var(--surface-2);
  border-bottom: 1px solid var(--rule);
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}

.row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: var(--s3);
  width: 100%;
  min-height: var(--tap);
  padding: var(--s3) var(--s4);
  background: var(--surface);
  border: 0;
  border-bottom: 1px solid var(--rule);
  text-align: left;
  cursor: pointer;
}

.row:last-child {
  border-bottom: 0;
}

.row:hover {
  background: var(--surface-2);
}

.row-main {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.row-title {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-sub {
  font-size: 0.78rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: var(--s2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-amount {
  font-weight: 600;
  white-space: nowrap;
}

/* Kontenpunkt */

.dot {
  flex: 0 0 auto;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}

.dot-kobalt { background: var(--acc-kobalt); }
.dot-petrol { background: var(--acc-petrol); }
.dot-messing { background: var(--acc-messing); }
.dot-pflaume { background: var(--acc-pflaume); }
.dot-rost { background: var(--acc-rost); }
.dot-schiefer { background: var(--acc-schiefer); }

/* Marker für Buchungstypen */

.tag {
  flex: 0 0 auto;
  font-size: 0.62rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 1px 5px;
  border-radius: 2px;
  background: var(--surface-3);
  color: var(--ink-2);
}

/* ---------------------------------------------------------------- Schalter */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  min-height: var(--tap);
  padding: 0 var(--s4);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  font-weight: 500;
  cursor: pointer;
}

.btn:hover {
  background: var(--surface-2);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-danger {
  color: var(--neg);
  border-color: var(--neg);
  background: transparent;
}

.btn-quiet {
  border-color: transparent;
  background: transparent;
  color: var(--accent);
  padding: 0 var(--s2);
  min-height: 36px;
}

.btn-block {
  width: 100%;
}

.btn-row {
  display: flex;
  gap: var(--s2);
  flex-wrap: wrap;
}

/* ---------------------------------------------------------------- Formular */

.field {
  display: flex;
  flex-direction: column;
  gap: var(--s1);
}

.field > label {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
}

.field input,
.field select,
.field textarea {
  min-height: var(--tap);
  padding: var(--s2) var(--s3);
  background: var(--surface);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  color: var(--ink);
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  border-color: var(--accent);
}

.field-hint {
  font-size: 0.75rem;
  color: var(--muted);
}

.field-error {
  font-size: 0.78rem;
  color: var(--neg);
}

.form-grid {
  display: flex;
  flex-direction: column;
  gap: var(--s3);
}

/* Betragsfeld — die wichtigste Eingabe, deshalb gross und zuerst */

.amount-field input {
  font-size: 2rem;
  font-weight: 600;
  letter-spacing: -0.03em;
  text-align: right;
  min-height: 64px;
  font-variant-numeric: tabular-nums;
}

/* Richtungsumschalter: der Nutzer tippt nie ein Minus (Bible §4) */

.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 2px;
  padding: 2px;
  background: var(--surface-2);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
}

.segmented button {
  min-height: 38px;
  border: 0;
  border-radius: 2px;
  background: transparent;
  color: var(--muted);
  font-weight: 500;
  font-size: 0.88rem;
  cursor: pointer;
}

.segmented button[aria-pressed='true'] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--rule-strong);
}

.segmented button[aria-pressed='true'][data-dir='out'] {
  color: var(--neg);
}

.segmented button[aria-pressed='true'][data-dir='in'] {
  color: var(--pos);
}

/* Kategorie-Chips statt Dropdown */

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s2);
}

.chip {
  min-height: 36px;
  padding: 0 var(--s3);
  border: 1px solid var(--rule-strong);
  border-radius: 18px;
  background: var(--surface);
  color: var(--ink-2);
  font-size: 0.85rem;
  cursor: pointer;
}

.chip[aria-pressed='true'] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

/* -------------------------------------------------------------------- Blatt */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: color-mix(in srgb, var(--ink) 45%, transparent);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.sheet {
  width: 100%;
  max-width: 560px;
  max-height: 92dvh;
  overflow-y: auto;
  background: var(--ground);
  border: 1px solid var(--rule);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: var(--s4);
  padding-bottom: max(var(--s4), env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: var(--s4);
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s3);
}

.sheet-head h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.015em;
  text-transform: none;
  color: var(--ink);
}

@media (prefers-reduced-motion: no-preference) {
  .sheet {
    animation: sheet-in 200ms cubic-bezier(0.2, 0.7, 0.3, 1);
  }
  @keyframes sheet-in {
    from {
      transform: translateY(14px);
      opacity: 0;
    }
  }
}

/* --------------------------------------------------------------- Leerlauf */

.empty {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--s3);
  padding: var(--s5) var(--s4);
  color: var(--muted);
}

.empty p {
  margin: 0;
  max-width: 38ch;
}

.notice {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--s3);
  padding: var(--s3) var(--s4);
  background: var(--accent-soft);
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-size: 0.88rem;
}

.notice-warn {
  background: color-mix(in srgb, var(--warn) 14%, var(--surface));
  border-left-color: var(--warn);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--nav-h) + var(--s5));
  z-index: 60;
  transform: translateX(-50%);
  max-width: min(90vw, 420px);
  padding: var(--s3) var(--s4);
  background: var(--ink);
  color: var(--ground);
  border-radius: var(--radius);
  font-size: 0.88rem;
}

/* ------------------------------------------------------------ Einrichtung */

.setup {
  max-width: 560px;
  margin: 0 auto;
  padding: var(--s5) var(--s4) var(--s6);
  display: flex;
  flex-direction: column;
  gap: var(--s4);
}

.setup-lede {
  font-size: 1.02rem;
  color: var(--ink-2);
  margin: 0;
}

.setup h1 {
  margin: 0;
  font-size: clamp(1.8rem, 7vw, 2.4rem);
  font-weight: 600;
  letter-spacing: -0.03em;
  line-height: 1.05;
}

.setup-account {
  display: grid;
  grid-template-columns: 1fr 140px;
  align-items: end;
  gap: var(--s3);
}

@media (max-width: 460px) {
  .setup-account {
    grid-template-columns: 1fr;
  }
}

/* ------------------------------------------------------- Desktop ab 1024px */

@media (min-width: 1024px) {
  #app {
    display: grid;
    grid-template-columns: 232px minmax(0, 1fr);
    min-height: 100dvh;
  }

  .sidebar {
    display: flex;
    flex-direction: column;
    gap: var(--s2);
    padding: var(--s4);
    border-right: 1px solid var(--rule);
    background: var(--surface);
    position: sticky;
    top: 0;
    height: 100dvh;
  }

  .sidebar-brand {
    font-size: 1.15rem;
    font-weight: 600;
    letter-spacing: -0.02em;
    padding: var(--s2) var(--s3) var(--s4);
  }

  .sidebar a {
    display: flex;
    align-items: center;
    gap: var(--s3);
    min-height: 42px;
    padding: 0 var(--s3);
    border-radius: var(--radius);
    text-decoration: none;
    color: var(--ink-2);
    font-weight: 500;
    font-size: 0.92rem;
  }

  .sidebar a svg {
    width: 18px;
    height: 18px;
  }

  .sidebar a:hover {
    background: var(--surface-2);
  }

  .sidebar a[aria-current='page'] {
    background: var(--accent-soft);
    color: var(--accent);
  }

  .sidebar-spacer {
    flex: 1;
  }

  .tabbar {
    display: none;
  }

  .fab {
    display: none;
  }

  .app-header {
    background: var(--ground);
    padding: var(--s3) var(--s5);
  }

  .app-main {
    padding: var(--s5);
    padding-bottom: var(--s6);
    max-width: 1180px;
    width: 100%;
  }

  .sheet-backdrop {
    align-items: center;
  }

  .sheet {
    border-radius: var(--radius);
    max-height: 86dvh;
  }

  .toast {
    bottom: var(--s5);
  }

  /* Zweispaltiges Raster auf dem Heute-Schirm */
  .cols {
    display: grid;
    grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr);
    gap: var(--s4);
    align-items: start;
  }

  .list-group-head {
    top: 0;
  }
}

/* =============================================================== Phase 2 */

/* -------------------------------------------------------------- Diagramme */

.chart-card {
  gap: var(--s3);
}

.chart-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--s3);
  flex-wrap: wrap;
}

.chart-hint {
  margin: 2px 0 0;
  font-size: 0.78rem;
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0;
}

.chart-meta {
  text-align: right;
  flex: 0 0 auto;
}

.chart-value {
  font-size: 1.3rem;
  font-weight: 600;
  letter-spacing: -0.025em;
  line-height: 1.1;
}

.chart-delta {
  font-size: 0.76rem;
  color: var(--muted);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: var(--s2) var(--s3);
  font-size: 0.76rem;
  color: var(--muted);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.range-filter {
  position: sticky;
  top: var(--header-h);
  z-index: 6;
}

.chart-pair {
  display: flex;
  flex-direction: column;
  gap: var(--s4);
}

/* ------------------------------------------------------- Monatsaufteilung */

.breakdown {
  display: flex;
  flex-direction: column;
  gap: var(--s3);
}

.breakdown-row {
  display: flex;
  flex-direction: column;
  gap: 5px;
  width: 100%;
  padding: var(--s2) 0;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--rule);
  text-align: left;
  cursor: pointer;
}

.breakdown-row:last-child {
  border-bottom: 0;
}

.breakdown-row:disabled {
  cursor: default;
  opacity: 0.75;
}

.breakdown-row:not(:disabled):hover .breakdown-bar {
  filter: brightness(1.12);
}

.breakdown-label,
.breakdown-foot {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--s3);
}

.breakdown-label {
  font-size: 0.92rem;
  font-weight: 500;
}

.breakdown-foot {
  font-size: 0.72rem;
  color: var(--muted);
}

/* Geisterbalken und Balken teilen sich die Spur, damit der Vormonat
   direkt hinter dem aktuellen Wert liegt statt daneben. */
.breakdown-track {
  position: relative;
  display: block;
  height: 10px;
  background: var(--surface-2);
  border-radius: 2px;
  overflow: hidden;
}

.breakdown-ghost,
.breakdown-bar {
  position: absolute;
  inset: 0 auto 0 0;
  display: block;
  border-radius: 2px;
}

.breakdown-ghost {
  background: var(--rule-strong);
  opacity: 0.55;
}

.breakdown-bar {
  background: var(--accent);
}

/* ------------------------------------------------------------------ Chips */

.chips-scroll {
  flex-wrap: nowrap;
  overflow-x: auto;
  padding-bottom: var(--s1);
  scrollbar-width: thin;
}

.chips-scroll .chip {
  flex: 0 0 auto;
}

.chips-sub {
  margin-top: var(--s2);
}

.chip-small {
  min-height: 32px;
  font-size: 0.8rem;
  border-style: dashed;
}

/* Ein Vorschlag der Regel-Engine ist sichtbar automatisch, aber nie
   endgueltig — gestrichelter Rahmen statt gefuellter Flaeche. */
.chip-suggested[aria-pressed='true'] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  border-style: dashed;
}

/* --------------------------------------------------------- Lernhinweis */

.learn-note {
  display: flex;
  align-items: flex-start;
  gap: var(--s2);
  margin-top: var(--s3);
  padding: var(--s2) var(--s3);
  background: var(--accent-soft);
  border-left: 3px solid var(--accent);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-size: 0.84rem;
  cursor: pointer;
}

.learn-note input {
  width: 18px;
  height: 18px;
  min-height: 0;
  margin-top: 2px;
  flex: 0 0 auto;
}

.learn-note .field-hint {
  display: block;
  margin-top: 2px;
}

/* ------------------------------------------------------- Kategorien, Import */

.row-child {
  padding-left: var(--s6);
  background: var(--surface);
}

.row-child .row-title {
  font-weight: 400;
  color: var(--ink-2);
}

.category-list .row-child .row-title::before {
  content: '↳';
  margin-right: 6px;
  color: var(--muted);
}

.import-preview {
  border-top: 1px solid var(--rule);
  padding-top: var(--s3);
}

.list-foot {
  padding: var(--s2) var(--s4);
  border-top: 1px solid var(--rule);
  font-size: 0.82rem;
}

.list-foot a {
  color: var(--accent);
}

/* --------------------------------------------------- Desktop ab 1024px */

@media (min-width: 1024px) {
  .chart-pair {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .range-filter {
    top: 0;
    max-width: 420px;
  }

  .chart-value {
    font-size: 1.5rem;
  }
}
