:root {
  color-scheme: light;
  --bg: #f5f5f2;
  --surface: #ffffff;
  --surface-2: #f0f0ec;
  --surface-3: #e7e7e2;
  --line: #e4e4df;
  --text: #121211;
  --text-2: #5a5955;
  --text-3: #8c8b86;
  --accent: #121211;
  --accent-ink: #ffffff;
  --op: #e9e9e4;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --warn: #b25b00;
  --shadow: 0 1px 2px rgba(18,18,17,.04), 0 6px 24px rgba(18,18,17,.04);
  --radius: 20px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #222220;
    --surface-3: #2c2c2a;
    --line: #2a2a28;
    --text: #f0efec;
    --text-2: #b4b3ac;
    --text-3: #7d7c76;
    --accent: #f0efec;
    --accent-ink: #121211;
    --op: #262624;
    --series-1: #3987e5;
    --series-2: #d95926;
    --warn: #e7a250;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface: #1a1a19;
  --surface-2: #222220;
  --surface-3: #2c2c2a;
  --line: #2a2a28;
  --text: #f0efec;
  --text-2: #b4b3ac;
  --text-3: #7d7c76;
  --accent: #f0efec;
  --accent-ink: #121211;
  --op: #262624;
  --series-1: #3987e5;
  --series-2: #d95926;
  --warn: #e7a250;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  font-feature-settings: "cv11", "ss01";
}
button { touch-action: manipulation; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
svg { width: 20px; height: 20px; flex: none; }
:focus-visible { outline: 2px solid var(--series-1); outline-offset: 2px; border-radius: 8px; }

/* ---------- Layout ---------- */
.app {
  display: grid;
  grid-template-columns: 88px minmax(0, 1fr) 340px;
  min-height: 100vh;
}
.rail {
  position: sticky; top: 0; height: 100vh; height: 100dvh;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: calc(20px + env(safe-area-inset-top)) 0 calc(20px + env(safe-area-inset-bottom));
  border-right: 1px solid var(--line);
}
.brand { color: var(--text); margin-bottom: 18px; }
.brand svg { width: 34px; height: 34px; }
.tab {
  width: 68px; padding: 10px 0 8px;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  border-radius: 14px;
  color: var(--text-3);
  font-size: 11px; font-weight: 500; letter-spacing: .01em;
  transition: background .15s, color .15s;
}
.tab svg { width: 24px; height: 24px; }
.tab:hover { color: var(--text); background: var(--surface-2); }
.tab[aria-current="page"] { color: var(--text); background: var(--surface); box-shadow: var(--shadow); }
:root[data-theme="dark"] .tab[aria-current="page"] { background: var(--surface-3); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .tab[aria-current="page"] { background: var(--surface-3); } }
.tab-history { display: none; }
.rail-spacer { flex: 1; }
.main > .theme-toggle-m { display: none; }
.main { position: relative; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center; color: var(--text-2);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.when-dark { display: none; }
.is-dark .when-dark { display: block; }
.is-dark .when-light { display: none; }

.main { padding: calc(40px + env(safe-area-inset-top)) clamp(20px, 4vw, 56px) calc(64px + env(safe-area-inset-bottom)); min-width: 0; }
.side {
  position: sticky; top: 0; height: 100vh; height: 100dvh;
  border-left: 1px solid var(--line);
  padding: calc(32px + env(safe-area-inset-top)) calc(20px + env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) 20px;
  display: flex; flex-direction: column; min-height: 0;
}
.side .history-host { flex: 1; min-height: 0; display: flex; }

.view { display: none; max-width: 980px; margin: 0 auto; animation: in .25s ease; }
.view[data-active] { display: block; }
@keyframes in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.view-head { margin-bottom: 24px; }
.view-head h1 { font-size: 28px; font-weight: 600; letter-spacing: -.02em; margin: 0 0 4px; }
.view-head p { margin: 0; color: var(--text-2); }
#view-calc .view-head { text-align: center; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- Calculator ---------- */
.calc { max-width: 380px; margin: 0 auto; padding: 18px; }
.calc-display {
  padding: 20px 10px 18px; text-align: right;
  min-height: 116px; display: flex; flex-direction: column; justify-content: flex-end;
}
.calc-expr {
  font-family: var(--mono); font-size: 14px; color: var(--text-3);
  white-space: nowrap; overflow-x: auto; scrollbar-width: none;
}
.calc-value {
  font-size: 48px; font-weight: 500; letter-spacing: -.03em; line-height: 1.15;
  white-space: nowrap; overflow-x: auto; scrollbar-width: none;
  font-variant-numeric: tabular-nums;
  transition: font-size .12s;
}
.calc-value.sm { font-size: 34px; }
.calc-value.xs { font-size: 24px; }
.calc-value.err { color: var(--warn); font-size: 24px; }
.keys { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.key {
  height: 64px; border-radius: 18px;
  background: var(--surface-2);
  font-size: 22px; font-weight: 500;
  transition: transform .06s, background .12s;
  user-select: none; -webkit-tap-highlight-color: transparent;
}
.key:hover { background: var(--surface-3); }
.key:active, .key.pressed { transform: scale(.96); }
.key.fn { color: var(--text-2); font-size: 18px; }
.key.op { background: var(--op); font-size: 24px; color: var(--series-1); }
.key.op.armed { background: var(--series-1); color: #fff; }
.key.eq { background: var(--accent); color: var(--accent-ink); font-size: 26px; }
.key.eq:hover { opacity: .9; }
.calc-extra { display: flex; gap: 8px; margin-top: 12px; }
.chip {
  flex: 1; height: 40px; border-radius: 12px;
  display: grid; place-items: center;
  color: var(--text-2); font-size: 16px;
  border: 1px solid var(--line);
}
.chip:hover { background: var(--surface-2); color: var(--text); }
.hint { text-align: center; color: var(--text-3); font-size: 13px; margin-top: 18px; }
kbd {
  font-family: var(--mono); font-size: 12px; padding: 1px 6px;
  border: 1px solid var(--line); border-radius: 6px; background: var(--surface);
}

/* ---------- Forms ---------- */
.split { display: grid; grid-template-columns: minmax(260px, 320px) minmax(0, 1fr); gap: 20px; align-items: start; }
.form { padding: 22px; display: flex; flex-direction: column; gap: 18px; position: sticky; top: 24px; }
.field { display: flex; flex-direction: column; gap: 7px; }
.field[hidden] { display: none; }
.field > span { font-size: 13px; color: var(--text-2); font-weight: 500; }
.input {
  display: flex; align-items: center;
  background: var(--surface-2); border: 1px solid transparent;
  border-radius: 12px; padding: 0 14px;
  transition: border-color .15s, background .15s;
}
.input:focus-within { border-color: var(--text); background: var(--surface); }
.input input {
  flex: 1; min-width: 0; border: 0; background: none; outline: none;
  font: 500 16px var(--font); color: var(--text); padding: 12px 0;
  font-variant-numeric: tabular-nums;
}
.input em { font-style: normal; color: var(--text-3); font-size: 14px; margin-left: 8px; }
.input.invalid { border-color: var(--warn); }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; }
.seg label { flex: 1; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: block; text-align: center; padding: 8px 4px; border-radius: 9px;
  font-size: 13px; font-weight: 500; color: var(--text-2); cursor: pointer;
  transition: background .15s, color .15s;
}
.seg input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
:root[data-theme="dark"] .seg input:checked + span { background: var(--surface-3); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .seg input:checked + span { background: var(--surface-3); } }
.seg input:focus-visible + span { outline: 2px solid var(--series-1); }

/* ---------- Results ---------- */
.results { display: flex; flex-direction: column; gap: 20px; min-width: 0; }
.hero { padding: 24px; position: relative; }
.eyebrow { font-size: 13px; color: var(--text-2); font-weight: 500; }
.hero-num {
  font-size: clamp(34px, 5vw, 46px); font-weight: 600; letter-spacing: -.03em;
  line-height: 1.1; margin: 4px 0 20px; font-variant-numeric: tabular-nums;
}
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(112px, 1fr)); gap: 14px; padding-top: 18px; border-top: 1px solid var(--line); }
.stats > div { display: grid; grid-template-columns: auto 1fr; column-gap: 7px; align-items: center; }
.stats .lbl { font-size: 12.5px; color: var(--text-2); grid-column: 2; }
.stats .dot + .lbl { grid-column: 2; }
.stats .dot { grid-column: 1; }
.stats b { grid-column: 1 / -1; font-weight: 600; font-size: 17px; font-variant-numeric: tabular-nums; margin-top: 2px; }
.stats > div:not(:has(.dot)) { grid-template-columns: 1fr; }
.stats > div:not(:has(.dot)) .lbl { grid-column: 1; }
.dot { width: 8px; height: 8px; border-radius: 3px; display: inline-block; }
.dot.s1 { background: var(--series-1); }
.dot.s2 { background: var(--series-2); }
.warn { color: var(--warn); font-size: 13px; margin: 14px 0 0; }
.btn-save {
  position: absolute; top: 18px; right: 18px;
  display: flex; align-items: center; gap: 6px;
  height: 36px; padding: 0 14px 0 11px; border-radius: 11px;
  background: var(--surface-2); color: var(--text);
  font-size: 13px; font-weight: 500;
  transition: background .15s;
}
.btn-save svg { width: 18px; height: 18px; }
.btn-save:hover { background: var(--surface-3); }
.btn-save.done { background: var(--accent); color: var(--accent-ink); }

.chart-card { padding: 20px 20px 12px; }
.chart-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.chart-head h2, .history-head h2 { font-size: 15px; font-weight: 600; margin: 0; }
.legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--text-2); }
.legend span { display: flex; align-items: center; gap: 6px; }
.chart { position: relative; height: 240px; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .grid line { stroke: var(--line); stroke-width: 1; }
.chart .axis text { fill: var(--text-3); font-size: 11px; font-family: var(--font); font-variant-numeric: tabular-nums; }
.chart .bar { transition: opacity .12s; }
.chart .col.dim .bar { opacity: .35; }
.chart .hit { fill: transparent; cursor: crosshair; }

.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--surface); border: 1px solid var(--line);
  box-shadow: 0 8px 28px rgba(0,0,0,.12);
  border-radius: 12px; padding: 10px 12px; font-size: 12.5px; min-width: 170px;
}
.tooltip h4 { margin: 0 0 6px; font-size: 12.5px; font-weight: 600; }
.tooltip div { display: flex; align-items: center; gap: 6px; color: var(--text-2); }
.tooltip div b { margin-left: auto; color: var(--text); font-weight: 500; font-variant-numeric: tabular-nums; padding-left: 12px; }
.tooltip .tot { border-top: 1px solid var(--line); margin-top: 6px; padding-top: 6px; }

.table-card { padding: 0; }
.table-card summary {
  list-style: none; cursor: pointer; padding: 16px 20px; font-weight: 500; font-size: 14px;
  display: flex; justify-content: space-between; align-items: center;
}
.table-card summary::-webkit-details-marker { display: none; }
.table-card summary::after { content: ""; width: 8px; height: 8px; border-right: 1.6px solid var(--text-3); border-bottom: 1.6px solid var(--text-3); transform: rotate(45deg); transition: transform .15s; margin-top: -4px; }
.table-card[open] summary::after { transform: rotate(-135deg); margin-top: 4px; }
.table-wrap { overflow-x: auto; padding: 0 8px 12px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
th, td { padding: 9px 12px; text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
th { color: var(--text-3); font-weight: 500; font-size: 12px; border-bottom: 1px solid var(--line); }
tbody tr:hover td { background: var(--surface-2); }

/* ---------- History ---------- */
.history { display: flex; flex-direction: column; min-height: 0; width: 100%; }
.history-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.text-btn { font-size: 13px; color: var(--text-3); padding: 4px 8px; border-radius: 8px; }
.text-btn:hover { color: var(--warn); background: var(--surface-2); }
.filters { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.filter {
  font-size: 12.5px; font-weight: 500; padding: 5px 11px; border-radius: 99px;
  color: var(--text-2); border: 1px solid var(--line);
}
.filter[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.history-list { list-style: none; margin: 0 -6px; padding: 0; overflow-y: auto; flex: 1; min-height: 0; }
.h-item {
  display: grid; grid-template-columns: 36px minmax(0, 1fr) auto; gap: 10px; align-items: center;
  padding: 10px 8px; border-radius: 14px; cursor: pointer;
  transition: background .12s;
}
.h-item:hover { background: var(--surface-2); }
.h-icon {
  width: 36px; height: 36px; border-radius: 11px; display: grid; place-items: center;
  background: var(--surface-2); color: var(--text-2);
}
.h-item:hover .h-icon { background: var(--surface); }
.h-icon svg { width: 19px; height: 19px; }
.h-body { min-width: 0; }
.h-title { font-size: 12.5px; color: var(--text-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: var(--mono); }
.h-item[data-type="compound"] .h-title, .h-item[data-type="loan"] .h-title { font-family: var(--font); }
.h-result { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.h-meta { font-size: 11px; color: var(--text-3); }
.h-actions { display: flex; gap: 2px; opacity: 0; transition: opacity .12s; }
.h-item:hover .h-actions, .h-item:focus-within .h-actions, .h-item.pinned .h-actions { opacity: 1; }
.h-actions button { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; color: var(--text-3); }
.h-actions button svg { width: 17px; height: 17px; }
.h-actions button:hover { background: var(--surface-3); color: var(--text); }
.h-actions .del:hover { color: var(--warn); }
.h-item.pinned .pin { color: var(--series-1); }
.h-item:not(.pinned) .h-actions .pin { opacity: .9; }
.h-sep { font-size: 11px; color: var(--text-3); text-transform: uppercase; letter-spacing: .06em; padding: 14px 8px 6px; font-weight: 500; }
.empty { text-align: center; color: var(--text-3); padding: 40px 0; }
.empty svg { width: 48px; height: 48px; opacity: .6; }
.empty p { margin: 8px 0 0; font-size: 13px; }

.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translate(-50%, 20px);
  background: var(--accent); color: var(--accent-ink);
  padding: 10px 16px; border-radius: 12px; font-size: 13.5px; font-weight: 500;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 60;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Responsive ---------- */
@media (max-width: 1180px) {
  .app { grid-template-columns: 88px minmax(0, 1fr); }
  .side { display: none; }
  .tab-history { display: flex; }
}
@media (max-width: 860px) {
  .split { grid-template-columns: 1fr; }
  .form { position: static; }
}
@media (max-width: 640px) {
  .app { grid-template-columns: 1fr; }
  .rail {
    position: fixed; top: auto; bottom: 0; left: 0; right: 0; height: auto; z-index: 40;
    flex-direction: row; justify-content: space-around; gap: 0;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    border-right: 0; border-top: 1px solid var(--line);
    background: color-mix(in srgb, var(--bg) 88%, transparent);
    backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
  }
  .brand, .rail-spacer { display: none; }
  .tab { width: auto; flex: 1; }
  .rail .theme-toggle { display: none; }
  .main > .theme-toggle-m { display: grid; position: absolute; top: calc(14px + env(safe-area-inset-top)); right: 12px; }
  .main { padding: calc(28px + env(safe-area-inset-top)) 16px 110px; }
  .view-head h1 { font-size: 24px; }
  .calc { padding: 12px; border: 0; background: none; box-shadow: none; }
  .key { height: 68px; }
  .chart { height: 200px; }
  .toast { bottom: 96px; }
  .h-actions { opacity: 1; }
}
#view-history .history-head h2 { visibility: hidden; }
#view-history .history-head { margin-top: -8px; margin-bottom: 4px; }

/* Tablets (iPad): größere Tasten */
@media (min-width: 641px) and (pointer: coarse) {
  .calc { max-width: 460px; padding: 22px; }
  .key { height: 78px; font-size: 26px; border-radius: 22px; }
  .chip { height: 48px; }
  .calc-value { font-size: 56px; }
  #view-calc .view-head { margin-top: 4vh; }
}
