* { box-sizing: border-box; }

:root {
  color-scheme: light;
  --page: #f2f2f7;
  --surface: #ffffff;
  --text: #000000;
  --text-secondary: #3c3c43;
  --text-muted: #8a8a8e;
  --separator: #c6c6c8;
  --hairline: #e5e5ea;
  --fill: #e3e3e8;
  --bar-bg: rgba(249, 249, 249, 0.86);
  --link: #007aff;
  --error: #d70015;
  --success: #248a3d;
  --shadow: #0000001a;

  /* Akzentfarbe je Zählerart (Navigation, Buttons) */
  --tint-gas: #e0561c;
  --tint-strom: #2a72d0;
  --tint-wasser: #11906a;
  --tint-pv: #b07200;

  /* Diagrammfarben je Zählerart */
  --meter-gas: #eb6834;
  --meter-strom: #2a78d6;
  --meter-wasser: #1baf7a;
  --meter-pv: #eda100;

  --tint: var(--link);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #000000;
    --surface: #1c1c1e;
    --text: #ffffff;
    --text-secondary: #ebebf5;
    --text-muted: #8d8d93;
    --separator: #38383a;
    --hairline: #2c2c2e;
    --fill: #2c2c2e;
    --bar-bg: rgba(22, 22, 24, 0.86);
    --link: #0a84ff;
    --error: #ff6961;
    --success: #30d158;
    --shadow: #00000080;
    --tint-gas: #f27a43;
    --tint-strom: #4c93ee;
    --tint-wasser: #2cc48c;
    --tint-pv: #f0b429;
    --meter-gas: #d95926;
    --meter-strom: #3987e5;
    --meter-wasser: #199e70;
    --meter-pv: #c98500;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #000000;
  --surface: #1c1c1e;
  --text: #ffffff;
  --text-secondary: #ebebf5;
  --text-muted: #8d8d93;
  --separator: #38383a;
  --hairline: #2c2c2e;
  --fill: #2c2c2e;
  --bar-bg: rgba(22, 22, 24, 0.86);
  --link: #0a84ff;
  --error: #ff6961;
  --success: #30d158;
  --shadow: #00000080;
  --tint-gas: #f27a43;
  --tint-strom: #4c93ee;
  --tint-wasser: #2cc48c;
  --tint-pv: #f0b429;
  --meter-gas: #d95926;
  --meter-strom: #3987e5;
  --meter-wasser: #199e70;
  --meter-pv: #c98500;
}

/* Akzentfarbe des geöffneten Bereichs */
[data-type="gas"] { --tint: var(--tint-gas); --series: var(--meter-gas); }
[data-type="strom"] { --tint: var(--tint-strom); --series: var(--meter-strom); }
[data-type="wasser"] { --tint: var(--tint-wasser); --series: var(--meter-wasser); }
[data-type="pv"] { --tint: var(--tint-pv); --series: var(--meter-pv); }

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  background: var(--page);
  color: var(--text);
  margin: 0;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

p { color: var(--text-muted); }

button {
  font: inherit;
  border: 0;
  cursor: pointer;
  background: none;
  color: inherit;
  padding: 0;
}

button:disabled { opacity: 0.5; cursor: wait; }

input {
  font: inherit;
  color: var(--text);
}

.message { min-height: 20px; font-size: 14px; margin: 8px 4px; }
.error { color: var(--error); }
.success { color: var(--success); }

.footnote {
  font-size: 13px;
  margin: 8px 16px;
  color: var(--text-muted);
}

/* LADEN UND LOGIN */
.center-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px 16px;
  margin: 0;
}

.login-box {
  width: 100%;
  max-width: 380px;
}

.login-box h1 {
  font-size: 34px;
  margin: 0 0 4px;
  text-align: center;
}

.login-box > p { text-align: center; margin: 0 0 24px; }

/* BUTTONS */
.primary-button {
  display: block;
  width: 100%;
  margin-top: 20px;
  padding: 14px;
  border-radius: 12px;
  background: var(--tint);
  color: #ffffff;
  font-size: 17px;
  font-weight: 600;
}

.link-button {
  color: var(--link);
  font-size: 17px;
  padding: 4px 0;
}

.link-button.strong { font-weight: 600; }

.icon-button {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  color: var(--link);
}

.icon-button svg,
.chevron,
.tab-icon svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-button svg { width: 24px; height: 24px; }

/* KOPFZEILE */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  padding: env(safe-area-inset-top) 8px 0;
  min-height: calc(44px + env(safe-area-inset-top));
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  border-bottom: 0.5px solid var(--separator);
}

.topbar-title {
  grid-column: 2;
  text-align: center;
  font-size: 17px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar .icon-button { grid-column: 3; }

/* INHALT */
.content {
  max-width: 680px;
  margin: 0 auto;
  padding: 8px 16px calc(96px + env(safe-area-inset-bottom));
}

.large-title {
  font-size: 34px;
  font-weight: 700;
  letter-spacing: 0.01em;
  margin: 12px 0 0;
}

.subtitle { margin: 2px 0 16px; font-size: 15px; }

.card {
  background: var(--surface);
  border-radius: 12px;
  padding: 16px;
}

.group-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin: 28px 4px 8px;
}

.group-header h2,
.group-title {
  margin: 0;
  font-size: 13px;
  font-weight: 400;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--text-muted);
}

.group-header .link-button { font-size: 15px; padding: 0; }

.group-title { margin: 24px 16px 8px; }
.sheet-body .group-title:first-child { margin-top: 8px; }

/* LISTEN */
.list {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--surface);
  border-radius: 12px;
  overflow: hidden;
}

.list:empty { display: none; }

.row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 56px;
  padding: 8px 16px;
  position: relative;
}

.row + .row::before {
  content: "";
  position: absolute;
  top: 0;
  left: 16px;
  right: 0;
  border-top: 0.5px solid var(--separator);
}

.row-main { flex: 1; min-width: 0; }

.row-title {
  font-size: 17px;
  font-variant-numeric: tabular-nums;
}

.row-sub {
  font-size: 14px;
  color: var(--text-muted);
  margin-top: 2px;
}

.row-trail {
  font-size: 15px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.delete-button {
  color: var(--error);
  font-size: 15px;
  padding: 6px 0 6px 8px;
}

/* EINSTELLUNGS-ZELLE */
.list-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 48px;
  padding: 10px 12px 10px 16px;
  background: var(--surface);
  border-radius: 12px;
  font-size: 17px;
  text-align: left;
}

.list-cell:active { background: var(--fill); }

.list-cell > span:first-child { flex: 1; }

.cell-value {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.chevron {
  width: 16px;
  height: 16px;
  color: var(--separator);
  stroke-width: 2.5;
  flex: none;
}

/* EINGABEFELDER IM IOS-STIL */
.field-group {
  background: var(--surface);
  border-radius: 12px;
  overflow: hidden;
}

.field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 48px;
  padding: 0 16px;
  position: relative;
  font-size: 17px;
}

.field + .field::before {
  content: "";
  position: absolute;
  top: 0;
  left: 16px;
  right: 0;
  border-top: 0.5px solid var(--separator);
}

.field span { flex: none; }

.field input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  text-align: right;
  padding: 12px 0;
  font-size: 17px;
  outline: none;
}

.field input::placeholder { color: var(--text-muted); }

.field input[type="date"] {
  display: flex;
  justify-content: flex-end;
}

/* DIAGRAMM-STEUERUNG */
.chart-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.segmented {
  display: inline-flex;
  padding: 2px;
  background: var(--fill);
  border-radius: 9px;
}

.segmented button {
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 500;
  border-radius: 7px;
  color: var(--text);
}

.segmented button[aria-pressed="true"] {
  background: var(--surface);
  box-shadow: 0 1px 4px var(--shadow);
  font-weight: 600;
}

.year-nav {
  display: inline-flex;
  align-items: center;
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 15px;
}

.year-nav button {
  color: var(--tint);
  font-size: 24px;
  line-height: 1;
  padding: 2px 10px;
}

.year-nav button:disabled { opacity: 0.25; cursor: default; }

.summary {
  margin: 14px 0 6px;
  font-size: 15px;
  color: var(--text-muted);
}

.summary strong {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* DIAGRAMM */
.chart { position: relative; }

#chart-svg {
  display: block;
  width: 100%;
  height: auto;
  overflow: visible;
}

#chart-svg .grid { stroke: var(--hairline); stroke-width: 1; }
#chart-svg .baseline { stroke: var(--separator); stroke-width: 1; }

#chart-svg .tick {
  fill: var(--text-muted);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}

#chart-svg .bar { fill: var(--series); }
#chart-svg .bar.partial { fill-opacity: 0.5; }

#chart-svg .hit { fill: transparent; outline: none; }
#chart-svg .hit:hover,
#chart-svg .hit:focus-visible { fill: var(--fill); fill-opacity: 0.5; }

.tooltip {
  position: absolute;
  z-index: 2;
  pointer-events: none;
  transform: translate(-50%, -100%);
  background: var(--surface);
  color: var(--text);
  border: 0.5px solid var(--separator);
  border-radius: 10px;
  box-shadow: 0 4px 16px var(--shadow);
  padding: 6px 10px;
  font-size: 13px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.tooltip .muted { color: var(--text-muted); }

.card .message { margin: 6px 0 0; font-size: 13px; color: var(--text-muted); }
.card .message.error { color: var(--error); }

details { margin-top: 8px; font-size: 14px; }
summary { cursor: pointer; color: var(--link); }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
  margin-top: 8px;
}

th, td {
  padding: 8px 4px;
  border-bottom: 0.5px solid var(--separator);
  text-align: left;
}

th { color: var(--text-muted); font-weight: 500; }

.number {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* NAVIGATION UNTEN */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  backdrop-filter: blur(20px) saturate(180%);
  border-top: 0.5px solid var(--separator);
}

.tabbar button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 2px 0;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
}

.tab-icon {
  display: grid;
  place-items: center;
  width: 56px;
  height: 30px;
  border-radius: 15px;
  transition: background-color 0.2s;
}

.tab-icon svg { width: 24px; height: 24px; }

.tabbar button[aria-selected="true"] {
  color: var(--tint);
  font-weight: 600;
}

.tabbar button[aria-selected="true"] .tab-icon {
  background: color-mix(in srgb, var(--tint) 16%, transparent);
}

.tabbar button[aria-selected="true"] .tab-icon svg {
  fill: color-mix(in srgb, var(--tint) 30%, transparent);
}

/* OVERLAYS (SHEETS) */
dialog.sheet {
  border: 0;
  padding: 0;
  width: 100%;
  max-width: 100%;
  max-height: 92dvh;
  margin: auto auto 0;
  border-radius: 14px 14px 0 0;
  background: var(--page);
  color: var(--text);
  overflow: hidden;
}

dialog.sheet[open] {
  display: flex;
  flex-direction: column;
  animation: sheet-up 0.28s cubic-bezier(0.2, 0.9, 0.3, 1);
}

dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.4); }

@keyframes sheet-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

@media (min-width: 640px) {
  dialog.sheet {
    max-width: 520px;
    margin: auto;
    border-radius: 14px;
  }

  @keyframes sheet-up {
    from { transform: translateY(24px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
  }
}

/* flex-basis auto: Höhe ergibt sich aus dem Inhalt (bei 0 klappt das
   Overlay in manchen Browsern auf 0 px zusammen). */
dialog.sheet > form,
dialog.sheet > .sheet-form {
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1 1 auto;
}

.sheet-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--bar-bg);
  border-bottom: 0.5px solid var(--separator);
}

.sheet-header h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  text-align: center;
}

.sheet-header > :first-child { justify-self: start; }
.sheet-header > :last-child { justify-self: end; }

.sheet-body {
  overflow-y: auto;
  padding: 20px 16px calc(24px + env(safe-area-inset-bottom));
}

/* ACTION SHEET (MENÜ) */
dialog.action-sheet {
  background: transparent;
  max-width: 520px;
  padding: 0 8px calc(8px + env(safe-area-inset-bottom));
  overflow: visible;
}

@media (min-width: 640px) {
  dialog.action-sheet { margin: auto auto 0; border-radius: 0; }
}

.action-group,
.action-cancel {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-radius: 14px;
  overflow: hidden;
}

.action-caption {
  margin: 0;
  padding: 14px 16px;
  font-size: 13px;
  text-align: center;
  border-bottom: 0.5px solid var(--separator);
}

.action-group button,
.action-cancel {
  padding: 16px;
  font-size: 20px;
  color: var(--link);
  text-align: center;
}

.action-group button + button { border-top: 0.5px solid var(--separator); }
.action-group .destructive { color: var(--error); }

.action-cancel {
  margin-top: 8px;
  font-weight: 600;
}

/* Produktname und Version, bewusst unauffällig */
.action-footer {
  margin: 0;
  padding: 8px 16px 10px;
  font-size: 11px;
  text-align: center;
  color: var(--text-muted);
  border-top: 0.5px solid var(--separator);
}
