/* Meterlog: mobile-first, light/dark by system preference. Series colors are the validated
   data-viz palette: water = blue slot, electric = orange slot (stepped separately for dark). */
:root {
  color-scheme: light;
  --bg: #f6f5f2;
  --surface: #fcfcfb;
  --surface-2: #efeeea;
  --border: #dedcd5;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #8a8884;
  --accent: #1f6f78;
  --accent-ink: #ffffff;
  --electric: #eb6834;
  --electric-soft: #fbe3d8;
  --water: #2a78d6;
  --water-soft: #d9e7f9;
  --good: #008300;
  --bad: #e34948;
  --grid: #e6e4de;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.05);
  --radius: 14px;
  --tab-h: 62px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121413;
    --surface: #1a1c1b;
    --surface-2: #242726;
    --border: #33373a;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #8f918d;
    --accent: #3aa1ab;
    --accent-ink: #061516;
    --electric: #d95926;
    --electric-soft: #3a261d;
    --water: #3987e5;
    --water-soft: #1d2b3e;
    --good: #45b552;
    --bad: #e66767;
    --grid: #2b2f2e;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121413; --surface: #1a1c1b; --surface-2: #242726; --border: #33373a; --text: #ffffff; --text-2: #c3c2b7;
  --muted: #8f918d; --accent: #3aa1ab; --accent-ink: #061516; --electric: #d95926; --electric-soft: #3a261d;
  --water: #3987e5; --water-soft: #1d2b3e; --good: #45b552; --bad: #e66767; --grid: #2b2f2e; --shadow: none;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tab-h) + env(safe-area-inset-bottom));
  min-height: 100vh;
}
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.05rem; font-weight: 600; }
p { margin: 0; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.15rem; color: var(--text); text-decoration: none; }
.brand img { border-radius: 7px; }
.topbar-link { font-size: .85rem; text-decoration: none; padding: 6px 10px; border: 1px solid var(--border); border-radius: 999px; color: var(--text-2); }

.view { max-width: 720px; margin: 0 auto; padding: 16px 16px 24px; display: grid; gap: 16px; }
.view-title { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: grid; grid-template-columns: 1fr 1fr 1fr;
  height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: .72rem; font-weight: 600;
}
.tabbar a svg { width: 26px; height: 26px; }
.tabbar a.active { color: var(--accent); }
.tabbar a.tab-add svg { width: 30px; height: 30px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.card h2 { margin-bottom: 8px; }
.card .sub { color: var(--muted); font-size: .8rem; margin-bottom: 8px; }

/* Segmented control */
.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.segmented button {
  border: 0; background: transparent; padding: 9px 8px; border-radius: 9px; font-weight: 600; color: var(--text-2); cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.dot.electric { background: var(--electric); }
.dot.water { background: var(--water); }

/* Stat tiles */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; box-shadow: var(--shadow); min-width: 0; }
.tile .label { color: var(--muted); font-size: .75rem; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.tile .value { font-size: 1.45rem; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; overflow-wrap: anywhere; }
.tile .value small { font-size: .8rem; font-weight: 500; color: var(--text-2); margin-left: 2px; }
.tile .delta { font-size: .78rem; color: var(--text-2); margin-top: 2px; }
.tile .delta.up { color: var(--bad); }
.tile .delta.down { color: var(--good); }

.chart-wrap { position: relative; height: 230px; }
.chart-wrap.tall { height: 260px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: .78rem; color: var(--text-2); margin-top: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 12px; height: 4px; border-radius: 2px; display: inline-block; }

.empty { text-align: center; color: var(--muted); padding: 32px 12px; }
.empty .big { font-size: 2.2rem; }

/* Table view */
table.data { width: 100%; border-collapse: collapse; font-size: .85rem; font-variant-numeric: tabular-nums; }
table.data th, table.data td { text-align: right; padding: 6px 6px; border-bottom: 1px solid var(--border); white-space: nowrap; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data th { color: var(--muted); font-weight: 600; font-size: .75rem; text-transform: uppercase; }
.table-scroll { overflow-x: auto; }
.linkbtn { background: none; border: 0; color: var(--accent); font-weight: 600; padding: 4px 0; cursor: pointer; }

/* Bill list */
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 7px 14px; font-weight: 600; font-size: .85rem; cursor: pointer; color: var(--text-2); display: inline-flex; align-items: center; gap: 6px; }
.chip.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.list { display: grid; gap: 8px; }
.row {
  display: grid; grid-template-columns: 56px 1fr auto; gap: 12px; align-items: center;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; text-decoration: none; color: inherit; box-shadow: var(--shadow);
}
.row .thumb { width: 56px; height: 56px; border-radius: 10px; object-fit: cover; background: var(--surface-2); display: grid; place-items: center; color: var(--muted); font-size: 1.4rem; }
.row .main { min-width: 0; }
.row .title { font-weight: 600; display: flex; align-items: center; gap: 6px; overflow: hidden; }
.row .title span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .meta { color: var(--muted); font-size: .8rem; }
.row .amt { text-align: right; font-variant-numeric: tabular-nums; }
.row .amt b { font-size: 1.05rem; }
.row .amt small { display: block; color: var(--muted); font-size: .78rem; }
.badge { font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: 2px 7px; border-radius: 999px; flex: none; }
.badge.electric { background: var(--electric-soft); color: var(--electric); }
.badge.water { background: var(--water-soft); color: var(--water); }

/* Forms */
.form { display: grid; gap: 12px; }
.field { display: grid; gap: 4px; }
.field label { font-size: .8rem; font-weight: 600; color: var(--text-2); }
.field input, .field select, .field textarea {
  width: 100%; padding: 11px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text);
}
.field input:focus, .field textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field .unit { position: relative; }
.field .unit span { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: .85rem; pointer-events: none; }
.field .unit input { padding-right: 52px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.check { display: flex; align-items: center; gap: 10px; font-weight: 500; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.hint { color: var(--muted); font-size: .78rem; }
.filled { background: color-mix(in srgb, var(--accent) 12%, var(--surface)) !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 16px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-weight: 600; cursor: pointer; text-decoration: none; min-height: 46px;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, var(--border)); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.btn svg { width: 22px; height: 22px; }
.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* Photo picker */
.photo-box { display: grid; gap: 10px; }
.photo-box input[type=file] { display: none; }
.photo-preview { position: relative; border-radius: 12px; overflow: hidden; background: var(--surface-2); }
.photo-preview img { display: block; width: 100%; max-height: 340px; object-fit: contain; }
.scan-status { font-size: .85rem; color: var(--text-2); display: flex; align-items: center; gap: 8px; min-height: 22px; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Detail */
.detail-photo { display: block; border-radius: 12px; overflow: hidden; background: var(--surface-2); }
.detail-photo img { display: block; width: 100%; max-height: 60vh; object-fit: contain; }
.kv { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.kv div { min-width: 0; }
.kv .k { color: var(--muted); font-size: .75rem; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.kv .v { font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kv .wide { grid-column: 1 / -1; }
.hero { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.hero .amount { font-size: 2rem; font-weight: 800; font-variant-numeric: tabular-nums; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 14px); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: .9rem; font-weight: 600; z-index: 9;
  max-width: calc(100vw - 32px); box-shadow: 0 6px 20px rgba(0,0,0,.25);
}
.toast.error { background: var(--bad); color: #fff; }

@media (min-width: 640px) {
  .tiles { grid-template-columns: repeat(4, 1fr); }
  .chart-wrap { height: 280px; }
}
