:root{
  /* -----------------------------------------------------------------------
   * Farb-Designsystem (Dark Mode)
   * --------------------------------------------------------------------- */

  /* Grundflächen */
  --color-bg:           #0f172a;   /* App Hintergrund */
  --color-bg-card:      #0f172a;   /* Karten / Panels */
  --color-bg-deep:      #0b1220;   /* Inputs, Canvas, tiefe Flächen */
  --color-bg-sticky:    #0c1629;   /* Sticky-Header/-Footer in Tabellen */
  --color-bg-footer:    #0b1220;   /* Fusszeile / starke Absetzung */
  --color-bg-badge:     #06161a;   /* Standard Badge Hintergrund */

  /* Linien & Rahmen */
  --color-border:       #1f2937;   /* Hauptgrenzen */
  --color-border-sub:   #2a3445;   /* Subtile Grenzen, Hover */

  /* Textfarben */
  --color-text:         #e5e7eb;   /* Primärtext */
  --color-text-muted:   #94a3b8;   /* Sekundärtext */
  --color-text-subtle:  #cbd5e1;   /* Sehr dezente Labels/Icons */

  /* Aktionen & Akzente */
  --color-primary:      #22c55e;   /* Grün: Primäraktion, positiv */
  --color-secondary:    #38bdf8;   /* Blau: Sekundäraktion, Akzent */

  /* Statusfarben */
  --color-danger:       #ef4444;   /* Rot: Fehler, negativ */
  --color-warning:      #ea9f08;   /* Orange: Warnung, neutral */
  --color-info:         var(--color-warning);   /* Blau #3b82f6: Info (EV-Segment) */

  /* Hilfsfarben */
  --color-kbd:          #0f172a;   /* KBD Hintergrund */
  --color-white:        #ffffff;   /* Weiss für Text auf dunklem Grund */

  /* Radius / Spacing / Fonts */
  --radius:             12px;
  --grid-gap:           12px;
  --font-mono:          ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;

  /* -----------------------------------------------------------------------
   * Aliase für Bestandscode (kompatibel lassen)
   * --------------------------------------------------------------------- */
  --bg:     var(--color-bg);
  --card:   var(--color-bg-card);
  --muted:  var(--color-text-muted);
  --fg:     var(--color-text);
  --pri:    var(--color-primary);
  --sec:    var(--color-secondary);
  --red:    var(--color-danger);
  --orange: var(--color-warning);
}

/* Reset & Grundlayout */
* { box-sizing: border-box; }
html,body {
  margin:0; padding:0;
  background:var(--bg);
  color:var(--fg);
  font: 16px/1.5 Inter, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Noto Sans", "Apple Color Emoji", "Segoe UI Emoji";
}

header, footer { max-width: 1200px; margin: 24px auto; padding: 0 16px; }
header h1 { margin: 0 0 8px; font-size: 28px; }
header p, footer p { color: var(--muted); margin: 0; }

footer { padding-bottom: 80px; background: var(--color-bg-footer); }

main { max-width: 1200px; margin: 0 auto; padding: 0 16px; }

/* Karten */
.card {
  background: var(--card);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 18px;
  margin: 16px 0;
}

h2 { margin: 0 0 12px; font-size: 20px; }
h3 { margin: 8px 0; font-size: 16px; color: var(--muted); }

.grid { display: grid; gap: var(--grid-gap); }
.grid-2 { grid-template-columns: repeat(2, minmax(0,1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0,1fr)); }
@media (max-width: 900px){
  .grid-4, .grid-2 { grid-template-columns: 1fr; }
}

label { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
input[type="number"]{
  background: var(--color-bg-deep);
  border: 1px solid var(--color-border);
  color: var(--fg);
  padding: 8px 10px;
  border-radius: 8px;
  font-family: var(--font-mono);
}

/* Buttons / Aktionen */
.actions { display:flex; gap:5px; margin-top: 5px; }
button{
  border:1px solid var(--color-border);
  background: var(--pri);
  color:#051b0d;
  font-weight:600;
  padding:13px;
  border-radius: 8px;
  cursor:pointer;
}
button.secondary{ background: var(--bg); color: var(--fg); border-color: var(--color-border-sub); }
button:hover{ filter: brightness(1.05); }

.actions {
  position: fixed;
  bottom: 0.5rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  gap: 1rem;
  padding: 0.5rem;
  border-radius: 0.25rem;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.actions button { flex: 1; min-width: 150px; }

/* Monats-Grid */
.month-grid{
  display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 8px;
}
.month-grid label{ background:var(--color-bg-deep); border:1px solid var(--color-border); padding:8px; border-radius:8px; }
.month-grid span{ font-size:12px; color:var(--muted); }
.month-grid input{ margin-top:6px; width:100%; }

/* Tabellen */
.table-wrap{ overflow:auto; position: relative; }
table{ width:100%; border-collapse: separate; border-spacing: 0; border:1px solid var(--color-border); }
thead th{
  position: sticky; top:0;
  background:var(--color-bg-sticky);
  z-index:1;
}
th, td{
  padding:10px 12px;
  border-bottom:1px solid var(--color-border);
  text-align:right;
  font-variant-numeric: tabular-nums;
}
td:first-child, th:first-child, td:nth-child(2), th:nth-child(2){ text-align:left; }
tfoot td{ font-weight:700; background:var(--color-bg-footer); }

/* Badges */
.badge{
  padding:2px 6px; border-radius:6px; font-size:12px;
  color: var(--color-white);
  background: var(--color-bg-badge);
  border: 1px solid var(--color-white);
}
.badge.winter { background: rgba(56,189,248,.12); border-color: var(--sec); color: #e5f6ff; }
.badge.sommer { background: rgba(34,197,94,.12);  border-color: var(--pri); color: #eafff2; }

/* Textklassen */
.num { font-family: var(--font-mono); }
.positive { color: var(--pri); background: linear-gradient(to bottom, rgba(34,197,94,.06), transparent); }
.negative { color: var(--red); background: linear-gradient(to bottom, rgba(239,68,68,.06), transparent); }
.neutral  { color: var(--orange); background: linear-gradient(to bottom, rgba(234,159,8,.08), transparent); }
small{ color:var(--muted); }

/* Canvas */
canvas{
  background:var(--color-bg-deep);
  border:1px solid var(--color-border);
  border-radius: 8px;
  padding: 8px;
}

/* Tooltip */
.tooltip-portal {
  position: fixed;
  z-index: 99999;
  pointer-events: none;
  opacity: 0;
  transition: opacity .12s ease;
  background: var(--color-bg-deep);
  color: var(--fg);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  padding: 10px 12px;
  box-shadow: 0 6px 18px rgba(0,0,0,.35);
  max-width: 360px;
  font-size: 12px;
  line-height: 1.4;
}
.tooltip-portal.show { opacity: 1; }
.tooltip-portal kbd{
  background:var(--color-kbd);
  border:1px solid var(--color-border);
  border-radius:4px;
  padding:0 4px;
  font-family: var(--font-mono);
  font-size:11px;
}

/* Info-Icon im Header */
th .th-tip{ display:inline-flex; gap:6px; align-items:center; }
th .i {
  display:inline-block; width:16px; height:16px; line-height:16px; text-align:center;
  border-radius:50%; border:1px solid var(--color-border-sub);
  color:var(--color-text-subtle); font-size:11px;
  cursor: help; user-select:none;
}

/* Sticky Header/Footer/Spalten */
thead tr:first-child th {
  position: sticky; top: 0; z-index: 3;
  background:var(--color-bg-sticky); border-bottom:1px solid var(--color-border);
  text-transform: uppercase; letter-spacing:.02em; font-size:12px;
}
thead tr:nth-child(2) th {
  position: sticky; top: 34px; z-index: 2;
  background:var(--color-bg-sticky); border-bottom:1px solid var(--color-border);
}
td:first-child, th.group-0, th.col-0 {
  position: sticky; left: 0; z-index: 4; background:var(--color-bg-sticky);
  box-shadow: 1px 0 0 var(--color-border);
}

/* Hover-Effekte */
td.hover-col, th.hover-col { background: rgba(255, 255, 255, 0.03) !important; }
tbody tr:hover td { background:rgba(255, 255, 255, 0.03); }

/* Sub-Werte */
td.sub, th.sub { opacity: .82; font-weight: 500; font-size: 0.95em; }
td.nt, th.nt { opacity: .4; }
td.ht, th.ht { opacity: .4; }

/* Meter */
td.meter { position: relative; overflow: visible; }
td.meter::after {
  content: ""; position: absolute; left: 8px; right: 8px; bottom: 6px;
  height: 4px; border-radius: 999px;
  background: linear-gradient(
    to right,
    var(--meter-color, var(--sec)) calc(var(--p, 0) * 1%),
    rgba(148,163,184,.25) calc(var(--p, 0) * 1%)
  );
  opacity: .85;
}
td.ht { --meter-color: var(--red); }
td.nt { --meter-color: var(--orange); }
tbody td.meter:hover::after { opacity: 1; }
th.ht.sub, th.nt.sub { font-weight: 600; opacity: .85; }

/* Switch */
.switch { display: inline-flex; align-items: center; gap: 10px; user-select: none; cursor: pointer; margin: 6px 0 12px; }
.switch input[type="checkbox"] {
  appearance: none; width: 44px; height: 24px; border-radius: 999px;
  background: var(--color-bg-deep);
  border: 1px solid var(--color-border);
  position: relative;
  transition: background .2s ease, border-color .2s ease;
  outline: none;
}
.switch input[type="checkbox"]::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 20px; height: 20px;
  border-radius: 50%; background: var(--muted);
  transition: transform .2s ease, background .2s ease;
}
.switch input[type="checkbox"]:checked {
  background: rgba(34,197,94,.25);
  border-color: #195a34;
}
.switch input[type="checkbox"]:checked::after { transform: translateX(20px); background: var(--pri); }
.switch span { color: var(--fg); font-weight: 600; }

/* EV-Spalten */
td.sub.ht { opacity: .7; background: linear-gradient(to bottom, rgba(56,189,248,.06), transparent); }
td.sub.nt { opacity: .7; background: linear-gradient(to bottom, rgba(148,163,184,.05), transparent); }

/* EV 3-Segment Meter */
td.ev.meter {
  position: relative;
  --pv: 0; --ht: 0; --nt: 0;
}
/* Load (Gesamtverbrauch) Balken: PV (sec), HT (red), NT (info) */
td.load.meter { position: relative; overflow: visible; }
td.load.meter::before {
  content: ""; position: absolute; left: 0; bottom: 4px; height: 4px; width: 100%;
  background: rgba(148,163,184,.2); border-radius: 2px;
}
td.load.meter::after {
  content: ""; position: absolute; left: 0; bottom: 4px; height: 4px; width: 100%;
  border-radius: 2px;
  background: linear-gradient(
    to right,
    var(--sec) 0 calc(var(--pv) * 1%),
    var(--red) calc(var(--pv) * 1%) calc((var(--pv) + var(--ht)) * 1%),
    var(--color-info) calc((var(--pv) + var(--ht)) * 1%) 100%
  );
}

td.ev.meter::before {
  content: ""; position: absolute; left: 0; bottom: 4px; height: 4px; width: 100%;
  background: rgba(148,163,184,.2); border-radius: 2px;
}
td.ev.meter::after {
  content: ""; position: absolute; left: 0; bottom: 4px; height: 4px; width: 100%;
  border-radius: 2px;
  background: linear-gradient(
    to right,
    var(--sec) 0 calc(var(--pv) * 1%),
    var(--red) calc(var(--pv) * 1%) calc((var(--pv) + var(--ht)) * 1%),
    var(--color-info) calc((var(--pv) + var(--ht)) * 1%) 100%
  );
}

/* Mobile */
@media (max-width: 700px){
  canvas { width: 100% !important; height: auto !important; }
}
.hide-sm { opacity: .4; }
