:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #1c2330;
  --muted: #5d6778;
  --border: #dde1e8;
  --accent: #2458c6;
  --accent-text: #ffffff;
  --warn-bg: #fff4d6;
  --warn-text: #6b4e00;
  --c-bonds: #2f7fd1;
  --c-stocks: #1f9d6b;
  --c-cash: #d49a1a;
  --c-need: #c2410c;
  --c-pension: #7c3aed;
  --grid: rgba(0, 0, 0, 0.08);
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #11151c;
  --surface: #1a2029;
  --surface-2: #232a35;
  --text: #e6e9ef;
  --muted: #9aa4b5;
  --border: #2e3642;
  --accent: #5b8def;
  --accent-text: #0b1020;
  --warn-bg: #3a3014;
  --warn-text: #f3d98a;
  --c-bonds: #5aa2ea;
  --c-stocks: #3cc28c;
  --c-cash: #e8b43e;
  --c-need: #f07a45;
  --c-pension: #a78bfa;
  --grid: rgba(255, 255, 255, 0.08);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11151c;
    --surface: #1a2029;
    --surface-2: #232a35;
    --text: #e6e9ef;
    --muted: #9aa4b5;
    --border: #2e3642;
    --accent: #5b8def;
    --accent-text: #0b1020;
    --warn-bg: #3a3014;
    --warn-text: #f3d98a;
    --c-bonds: #5aa2ea;
    --c-stocks: #3cc28c;
    --c-cash: #e8b43e;
    --c-need: #f07a45;
    --c-pension: #a78bfa;
    --grid: rgba(255, 255, 255, 0.08);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
/* hidden-Attribut muss immer gewinnen (sonst überschreiben z. B. .field/.scenario-bar mit display:flex) */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ---------- Kopfzeile ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 18px; }
.logo {
  display: grid; place-items: center; width: 32px; height: 32px; border-radius: 8px;
  background: var(--accent); color: var(--accent-text); font-weight: 700;
}
.badge {
  font-size: 12px; font-weight: 500; color: var(--muted);
  border: 1px solid var(--border); border-radius: 999px; padding: 2px 8px;
}
.icon-btn {
  width: 38px; height: 38px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font-size: 18px;
}
.icon-moon { display: none; }
:root[data-theme="dark"] .icon-sun { display: none; }
:root[data-theme="dark"] .icon-moon { display: inline; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .icon-sun { display: none; }
  :root:not([data-theme="light"]) .icon-moon { display: inline; }
}

.notice {
  background: var(--warn-bg); color: var(--warn-text);
  padding: 8px 20px; font-size: 14px; text-align: center;
}

/* ---------- Layout ---------- */
.layout {
  display: grid; grid-template-columns: 360px 1fr; gap: 20px;
  padding: 20px; max-width: 1500px; margin: 0 auto;
}
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; padding: 16px; }
}
.inputs, .results { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 16px;
}
.card h2 { margin: 0 0 12px; font-size: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head h2 { margin: 0; }

/* ---------- Formularfelder ---------- */
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > span { font-size: 13px; color: var(--muted); }
.field small { font-size: 12px; }
input, select {
  font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }
.money { position: relative; }
.money input { text-align: right; padding-right: 30px; font-variant-numeric: tabular-nums; }
.money em { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); font-style: normal; color: var(--muted); }
.money.override input { border-color: var(--accent); }

.sum-row { display: flex; justify-content: space-between; padding-top: 8px; border-top: 1px dashed var(--border); }
.hint { font-size: 13px; color: var(--muted); margin: 8px 0 0; }
.calc-line { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-top: -4px; }
.link-btn { background: none; border: 0; color: var(--accent); cursor: pointer; font: inherit; padding: 0; text-decoration: underline; }

.breakdown { margin-top: 12px; font-size: 13px; }
.breakdown summary { cursor: pointer; color: var(--accent); }
.breakdown table { width: 100%; border-collapse: collapse; margin-top: 8px; font-variant-numeric: tabular-nums; }
.breakdown td { padding: 3px 0; border-bottom: 1px solid var(--border); }
.breakdown td:last-child { text-align: right; }
.breakdown tr.strong td { font-weight: 600; }
.breakdown tr.total td { font-weight: 700; color: var(--accent); border-bottom: 0; }

.primary {
  font: inherit; font-weight: 600; padding: 12px; border-radius: 10px; border: 0; cursor: pointer;
  background: var(--accent); color: var(--accent-text);
}

/* ---------- Ergebnis ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
@media (max-width: 700px) { .kpis { grid-template-columns: repeat(2, 1fr); } }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px; display: flex; flex-direction: column; gap: 4px;
}
.kpi span { font-size: 13px; color: var(--muted); }
.kpi strong { font-size: 20px; font-variant-numeric: tabular-nums; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button {
  font: inherit; font-size: 13px; padding: 6px 12px; border: 0; cursor: pointer;
  background: var(--surface-2); color: var(--muted);
}
.seg button.active { background: var(--accent); color: var(--accent-text); }

.chart-box { position: relative; height: 360px; }

.inline { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.inline select { width: auto; }

.table-wrap { overflow: auto; max-height: 480px; border: 1px solid var(--border); border-radius: 8px; }
table.data { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.data th {
  position: sticky; top: 0; background: var(--surface-2); text-align: right;
  padding: 8px 10px; border-bottom: 1px solid var(--border); font-weight: 600;
}
table.data td { padding: 6px 10px; text-align: right; border-bottom: 1px solid var(--border); }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data tr.pension-start td { background: color-mix(in srgb, var(--c-pension) 14%, transparent); }
table.data tr.jan td:first-child { font-weight: 600; }
table.data td.zero { color: var(--muted); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s;
}
.toast.show { opacity: 1; }

/* ---------- Stufe 1b: Modi, Monte-Carlo ---------- */
.seg-full { display: flex; width: 100%; margin-bottom: 12px; }
.seg-full button { flex: 1; }
.field .seg-full { margin-bottom: 0; }

/* Sichtbarkeit je Modus (body[data-mode], body[data-infl]) */
.mode-block { display: none; }
body[data-mode="fixed"] .mode-block[data-for="fixed"],
body[data-mode="param"] .mode-block[data-for="param"],
body[data-mode="hist"]  .mode-block[data-for="hist"] { display: block; }
body[data-mode="fixed"] .mc-only { display: none !important; }
body:not([data-mode="hist"]) .only-hist { display: none; }
body[data-infl="hist"] #inflFixed { display: none; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: 80px 1fr 1fr; gap: 8px; align-items: center; margin-bottom: 8px; }
.grid3.head { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.rowlabel { font-size: 13px; color: var(--muted); }

.rules { margin: 10px 0 0; padding-left: 18px; font-size: 13px; color: var(--muted); }
.rules li { margin-bottom: 4px; }

.datasrc {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px; margin-bottom: 12px; font-size: 13px;
}
.datasrc table { width: 100%; border-collapse: collapse; margin: 8px 0; }
.datasrc td { padding: 3px 4px 3px 0; color: var(--muted); }
.datasrc td:first-child { color: var(--text); }
.pill { font-size: 11px; padding: 1px 8px; border-radius: 999px; margin-left: 6px; }
.pill.warn { background: var(--warn-bg); color: var(--warn-text); }
.secondary {
  font: inherit; font-size: 13px; padding: 6px 12px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--accent); background: transparent; color: var(--accent);
}

.run-bar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px;
}
.run-bar .hint { margin: 0; }
.muted { color: var(--muted); font-weight: 400; font-size: 14px; }
.table-wrap.small { max-height: none; }
table.data tr.sel td { background: color-mix(in srgb, var(--accent) 12%, transparent); font-weight: 600; }
table.data tr.dec td { border-bottom: 2px solid var(--border); }
body:not([data-rmode="param"]):not([data-rmode="hist"]) .kpis { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 700px) { body:not([data-rmode="param"]):not([data-rmode="hist"]) .kpis { grid-template-columns: 1fr 1fr; } }

/* ---------- Läufe ein-/ausblenden ---------- */
.run-toggles { display: none; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
body:is([data-rmode="param"],[data-rmode="hist"])[data-view="runs"] .run-toggles { display: flex; }
.run-toggles .hint { margin: 0; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; font: inherit; font-size: 13px;
  padding: 4px 10px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--muted);
}
.chip.on { color: var(--text); border-color: var(--text); }
.chip:not(.on) .dot { opacity: .3; }
.chip:not(.on) { text-decoration: line-through; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.c-worst { background: var(--c-need); }
.c-median { background: var(--accent); }
.c-best { background: var(--c-stocks); }
.run-toggles .spacer { flex: 1; }

/* ---------- Linke Spalte sticky (nur breite Bildschirme) ---------- */
@media (min-width: 961px) {
  .inputs {
    position: sticky; top: 82px; align-self: start;
    max-height: calc(100vh - 102px); overflow-y: auto;
    padding-right: 6px; scrollbar-width: thin; scrollbar-color: var(--border) transparent;
  }
}

/* ---------- Ringdiagramm Vermögensaufteilung ---------- */
.alloc { display: grid; grid-template-columns: 120px 1fr; gap: 14px; align-items: center; margin-top: 12px; }
.alloc-chart { position: relative; height: 120px; }
.alloc-legend { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.alloc-legend li { display: grid; grid-template-columns: 14px 1fr auto; gap: 6px; align-items: center; padding: 3px 0; }
.alloc-legend strong { font-variant-numeric: tabular-nums; }
.c-bonds { background: var(--c-bonds); }
.c-stocks { background: var(--c-stocks); }
.c-cash { background: var(--c-cash); }

/* ---------- Monatsbilanz ---------- */
.balance-bar { display: flex; height: 22px; border-radius: 6px; overflow: hidden; background: var(--surface-2); }
.balance-bar > div { height: 100%; }
.seg-pension { background: var(--c-pension); }
.seg-gap { background: var(--c-need); }
.c-pension { background: var(--c-pension); }
.c-gap { background: var(--c-need); }
.balance-legend { list-style: none; margin: 10px 0 0; padding: 0; font-size: 13px; }
.balance-legend li { display: grid; grid-template-columns: 14px 1fr auto; gap: 6px; align-items: center; padding: 2px 0; }
.balance-legend li.total { grid-template-columns: 1fr auto; border-top: 1px dashed var(--border); margin-top: 4px; padding-top: 6px; }
.balance-legend strong { font-variant-numeric: tabular-nums; }
.coverage { margin: 10px 0 0; font-size: 14px; }
.coverage strong { color: var(--accent); }

/* ---------- Histogramm ---------- */
.hist-chart { position: relative; height: 200px; }
.mode-block .hint { margin-bottom: 12px; }

/* ---------- Stufe 2 ---------- */
/* Ergebnis-Elemente folgen dem Modus der letzten Berechnung (data-rmode), nicht der Eingabe */
body:not([data-rmode="param"]):not([data-rmode="hist"]) .mc-res { display: none !important; }

.status {
  border-radius: 12px; padding: 10px 14px; font-size: 14px;
  background: var(--warn-bg); color: var(--warn-text); border: 1px solid transparent;
}
.status.error { background: color-mix(in srgb, var(--c-need) 18%, var(--surface)); color: var(--text); border-color: var(--c-need); }
.status.busy { background: var(--surface-2); color: var(--muted); }

.btn-row { display: flex; gap: 10px; }
.btn-row .primary { flex: 1; }
.btn-row .secondary { padding: 0 14px; }
.primary:disabled { opacity: .6; cursor: progress; }

.import-row { display: flex; gap: 8px; align-items: center; margin-top: 4px; }
.import-row select { width: auto; padding: 5px 8px; font-size: 13px; }
.datasrc td.years { text-align: right; white-space: nowrap; }
.datasrc .reset { background: none; border: 0; color: var(--accent); cursor: pointer; font-size: 12px; padding: 0 0 0 6px; text-decoration: underline; }
.pill.ok { background: color-mix(in srgb, var(--c-stocks) 18%, transparent); color: var(--text); }

input.invalid { border-color: var(--c-need); outline-color: var(--c-need); }

/* ---------- Stufe 3: Szenarien, Anmeldung, Dialoge, Vergleich ---------- */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.topbar { gap: 12px; flex-wrap: wrap; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.scenario-bar { display: flex; align-items: center; gap: 6px; flex: 1; flex-wrap: wrap; justify-content: center; }
.scenario-bar select { width: auto; min-width: 200px; max-width: 320px; padding: 6px 10px; font-size: 14px; }
.dirty { color: var(--c-cash); font-size: 18px; line-height: 1; }
.tb-btn {
  font: inherit; font-size: 13px; padding: 6px 10px; border-radius: 8px; cursor: pointer; list-style: none;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text); white-space: nowrap;
}
.tb-btn::-webkit-details-marker { display: none; }
.tb-btn:disabled { opacity: .45; cursor: default; }
.tb-btn.accent { border-color: var(--accent); color: var(--accent); }

.menu { position: relative; }
.menu > summary { list-style: none; }
.menu-list {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 30; min-width: 180px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 6px;
  box-shadow: 0 8px 24px rgba(0,0,0,.18); display: flex; flex-direction: column;
}
.menu-list.right { left: auto; right: 0; }
.menu-list button {
  font: inherit; font-size: 14px; text-align: left; padding: 8px 10px; border: 0; border-radius: 6px;
  background: none; color: var(--text); cursor: pointer;
}
.menu-list button:hover { background: var(--surface-2); }
.menu-list button.danger, .danger { color: var(--c-need); }

.overlay {
  position: fixed; inset: 0; z-index: 50; background: var(--bg);
  display: grid; place-items: center; padding: 16px;
}
.overlay[hidden] { display: none; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 24px; display: flex; flex-direction: column;
}
.auth-card .brand { margin-bottom: 16px; }
.auth-card h2 { margin: 0 0 4px; font-size: 18px; }
.auth-card .hint { margin: 0 0 14px; }
.form-error { color: var(--c-need); font-size: 13px; min-height: 1em; margin: 0 0 10px; }

dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 20px; width: min(420px, calc(100vw - 32px));
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h3 { margin: 0 0 8px; font-size: 17px; }
dialog p { margin: 0 0 12px; font-size: 14px; color: var(--muted); }
.dlg-btns { display: flex; justify-content: flex-end; gap: 8px; }
.dlg-btns .primary { padding: 8px 16px; }
.dlg-btns .secondary { padding: 8px 14px; font-size: 14px; }
.primary.danger { background: var(--c-need); }

.compare {
  position: fixed; inset: 0; z-index: 40; background: var(--bg); overflow-y: auto; padding: 20px 16px;
}
.compare[hidden] { display: none; }
.compare-inner { max-width: 1200px; margin: 0 auto; display: flex; flex-direction: column; gap: 16px; }
.compare-pick { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0 14px; }
.compare-pick label {
  display: inline-flex; align-items: center; gap: 6px; font-size: 14px; cursor: pointer;
  border: 1px solid var(--border); border-radius: 999px; padding: 5px 12px; background: var(--surface-2);
}
.compare-pick input { width: auto; }
.compare-pick label.on { border-color: var(--accent); color: var(--text); }
.compare-table th, .compare-table td { text-align: right; }
.compare-table th:first-child, .compare-table td:first-child { text-align: left; color: var(--muted); font-weight: 400; }
.compare-table thead th { font-weight: 600; color: var(--text); }
.compare-table .sw { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; }
#compareResult .chart-box { margin-top: 16px; }

@media (max-width: 960px) {
  .scenario-bar { order: 3; flex-basis: 100%; justify-content: flex-start; }
  .scenario-bar select { min-width: 0; flex: 1; }
}
