/* ─── Human-friendly color system ──────────────────────────────────────────
   Soft neutrals (not pure #fff/#000) reduce glare and eye strain.
   Warm paper light + cool charcoal dark; Red Dot accent kept for actions/debt.
   WCAG-minded contrast: body text on surfaces ≥ ~7:1.
──────────────────────────────────────────────────────────────────────────── */

:root {
  /* Accent / status (shared) */
  --rd-red: #d91828;          /* slightly softer than neon #e30613 */
  --rd-red-soft: color-mix(in srgb, var(--rd-red) 12%, transparent);
  --rd-ok: #0f8f6c;           /* calm teal-green — credit / paid */
  --rd-zero: #7a7a82;         /* muted mid-gray — zero balances */
  --rd-warn: #c48a00;

  /* Light defaults (overridden by [data-theme] / media) */
  --bg: #f5f4f1;              /* warm paper */
  --fg: #1b1b1f;              /* soft charcoal, not pure black */
  --hint: #6a6a72;
  --sec: #ebe9e4;             /* sand / chip track */
  --elev: #ffffff;            /* cards lift off paper */
  --btn: var(--rd-red);
  --btn-fg: #ffffff;
  --ok: var(--rd-ok);
  --bad: var(--rd-red);
  --zero: var(--rd-zero);
  --line: color-mix(in srgb, var(--fg) 9%, transparent);
  --shadow: 0 1px 2px color-mix(in srgb, #1b1b1f 5%, transparent),
            0 8px 24px color-mix(in srgb, #1b1b1f 4%, transparent);
  --radius: 18px;
  --radius-sm: 12px;
  --pad: 16px;

  color-scheme: light dark;
  font-family: "Helvetica Neue", Helvetica, Inter, system-ui, -apple-system,
    "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* Explicit themes beat Telegram vars so brand stays coherent */
html[data-theme="light"] {
  color-scheme: light;
  --bg: #f5f4f1;
  --fg: #1b1b1f;
  --hint: #6a6a72;
  --sec: #ebe9e4;
  --elev: #ffffff;
  --line: color-mix(in srgb, #1b1b1f 9%, transparent);
  --zero: #7a7a82;
  --shadow: 0 1px 2px rgb(27 27 31 / 5%), 0 10px 28px rgb(27 27 31 / 5%);
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121316;              /* soft cool charcoal — not pure black */
  --fg: #f1f1f3;
  --hint: #9b9ba3;
  --sec: #1c1d22;
  --elev: #23242b;
  --line: color-mix(in srgb, #fff 9%, transparent);
  --zero: #8e8e96;
  --rd-ok: #2bb892;           /* slightly brighter for dark surfaces */
  --rd-red: #f04552;          /* readable debt red on dark */
  --rd-red-soft: color-mix(in srgb, var(--rd-red) 16%, transparent);
  --ok: var(--rd-ok);
  --bad: var(--rd-red);
  --shadow: 0 1px 2px rgb(0 0 0 / 35%), 0 12px 32px rgb(0 0 0 / 30%);
}

/* Fallback when JS has not set data-theme yet */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121316;
    --fg: #f1f1f3;
    --hint: #9b9ba3;
    --sec: #1c1d22;
    --elev: #23242b;
    --line: color-mix(in srgb, #fff 9%, transparent);
    --zero: #8e8e96;
    --rd-ok: #2bb892;
    --rd-red: #f04552;
    --rd-red-soft: color-mix(in srgb, var(--rd-red) 16%, transparent);
    --ok: var(--rd-ok);
    --bad: var(--rd-red);
    --shadow: 0 1px 2px rgb(0 0 0 / 35%), 0 12px 32px rgb(0 0 0 / 30%);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  min-height: 100%;
}

body {
  padding: 14px 14px calc(28px + env(safe-area-inset-bottom) + var(--main-btn-space, 0px));
}

#app {
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}

/* iPad / large phones: use more width + slightly larger type */
@media (min-width: 600px) {
  :root {
    font-size: 17px;
  }
  #app {
    max-width: 720px;
  }
  body {
    padding-left: 20px;
    padding-right: 20px;
  }
  .bill-item > summary {
    padding: 18px 18px;
  }
  .bill-item > summary .amt,
  .bill-grand {
    font-size: 1.2rem;
  }
}

@media (min-width: 900px) {
  :root {
    font-size: 18px;
  }
  #app {
    max-width: 880px;
  }
  body {
    padding-left: 28px;
    padding-right: 28px;
  }
}

/* ─── Tabs (segmented, Red Dot editorial) ────────────────────────────────── */

.tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  margin-bottom: 16px;
  padding: 4px;
  background: var(--sec);
  border-radius: 999px;
}
.tabs.tabs-two {
  grid-template-columns: 1fr 1fr;
}
.tabs.tabs-two .tab {
  width: 100%;
  text-align: center;
  padding: 12px 10px;
  font-weight: 700;
}

.tab, .chip, .primary, .ghost, .pay {
  border: 0;
  border-radius: 999px;
  padding: 10px 12px;
  font: inherit;
  cursor: pointer;
}

.tab {
  background: transparent;
  color: var(--hint);
  font-size: 0.86rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}
.tab.active {
  background: var(--elev);
  color: var(--fg);
  box-shadow: var(--shadow);
}

/* ─── Chips ──────────────────────────────────────────────────────────────── */

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 14px;
}
.chips[hidden] {
  display: none !important;
}
.chip {
  background: var(--sec);
  color: var(--fg);
  font-size: 0.82rem;
  font-weight: 600;
  padding: 8px 14px;
  border: 1px solid transparent;
}
.chip.active {
  background: var(--rd-red-soft);
  color: var(--rd-red);
  border-color: color-mix(in srgb, var(--rd-red) 35%, transparent);
  outline: none;
}

.view { display: none; }
.view.active { display: block; }

/* ─── Bills ──────────────────────────────────────────────────────────────── */

.bill-list {
  background: var(--elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.bill-item {
  border-bottom: 1px solid var(--line);
}
.bill-item:last-child { border-bottom: 0; }
.bill-item > summary {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px var(--pad);
  cursor: pointer;
  user-select: none;
}
.bill-item > summary::-webkit-details-marker { display: none; }
.bill-item > summary .left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.bill-item > summary .title {
  font-weight: 650;
  letter-spacing: -0.015em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bill-logo {
  width: 26px;
  height: 26px;
  object-fit: contain;
  flex-shrink: 0;
  border-radius: 0;
  display: block;
  background: transparent;
  /* keep dark marks (Київводоканал) crisp; no white plate */
  -webkit-user-drag: none;
}
html[data-theme="dark"] .bill-logo {
  /* slight lift so dark navy marks stay readable on charcoal */
  filter: drop-shadow(0 0 0.6px rgb(255 255 255 / 40%));
}
.bill-emoji {
  flex-shrink: 0;
  font-size: 1.05rem;
  line-height: 1;
}
.bill-flat .bill-flat-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px var(--pad);
}
.bill-flat .left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.bill-flat .title {
  font-weight: 650;
  letter-spacing: -0.015em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bill-flat .amt {
  font-variant-numeric: tabular-nums;
  font-weight: 750;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
  white-space: nowrap;
}
.bill-item > summary .amt {
  font-variant-numeric: tabular-nums;
  font-weight: 750;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
  white-space: nowrap;
}
.amt.bad { color: var(--bad) !important; }
.amt.ok { color: var(--ok) !important; }
.amt.zero { color: var(--zero) !important; }

.bill-item > summary .chev {
  color: var(--hint);
  transition: transform 0.15s ease;
  font-size: 0.75rem;
  width: 1em;
}
.bill-item[open] > summary .chev { transform: rotate(90deg); }
.bill-item[open] > summary {
  background: color-mix(in srgb, var(--sec) 55%, transparent);
}

.bill-details {
  padding: 0 var(--pad) 14px;
}
.bill-details .meta-line {
  color: var(--hint);
  font-size: 0.8rem;
  margin-bottom: 8px;
}

.bill-grand {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px var(--pad);
  margin-top: 4px;
  background: var(--elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-weight: 800;
  font-size: 1.15rem;
  letter-spacing: -0.025em;
  box-shadow: var(--shadow);
  position: relative;
}
.bill-grand::before {
  content: "";
  position: absolute;
  left: 0;
  top: 18%;
  bottom: 18%;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--rd-red);
}

/* ─── Cards / rows ───────────────────────────────────────────────────────── */

.card {
  background: var(--elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--pad);
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.card h2, .card h3 {
  margin: 0 0 10px;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
}
.row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
  font-size: 0.93rem;
}
.row:last-child { border-bottom: 0; }
.row .name { color: var(--hint); }
.row .amt { font-variant-numeric: tabular-nums; font-weight: 700; }
.total {
  margin-top: 10px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  font-weight: 800;
  font-size: 1.05rem;
  letter-spacing: -0.02em;
}
.hint {
  color: var(--hint);
  font-size: 0.88rem;
  margin-bottom: 12px;
  line-height: 1.45;
}
.empty { color: var(--hint); padding: 8px 0; }

/* ─── Readings ───────────────────────────────────────────────────────────── */

.reading-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.reading-line:last-child { border-bottom: 0; }
.reading-line .name {
  color: var(--hint);
  flex: 1;
  min-width: 0;
}
.reading-line input {
  flex: 0 1 48%;
  max-width: 180px;
  border: 1px solid var(--line);
  background: var(--sec);
  color: var(--fg);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font: inherit;
  text-align: right;
  opacity: 0.95;
}
.reading-line input:disabled {
  cursor: default;
  -webkit-text-fill-color: var(--fg);
}
.reading-line.is-done input:disabled {
  opacity: 0.85;
  color: var(--ok);
  -webkit-text-fill-color: var(--ok);
  font-weight: 700;
}
.field {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}
.field label { font-size: 0.82rem; color: var(--hint); font-weight: 600; }
.field input, .field textarea {
  border: 1px solid var(--line);
  background: var(--sec);
  color: var(--fg);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font: inherit;
}
.field textarea {
  width: 100%;
  resize: vertical;
  min-height: 110px;
  line-height: 1.4;
}
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }

.primary {
  background: var(--rd-red);
  color: #fff;
  width: 100%;
  font-weight: 700;
  letter-spacing: -0.01em;
  padding: 14px 16px;
  box-shadow: 0 8px 20px color-mix(in srgb, var(--rd-red) 28%, transparent);
}
.primary:active { filter: brightness(0.95); }

.ghost {
  background: transparent;
  color: var(--fg);
  border: 1px solid var(--line);
}

.pay {
  display: block;
  text-align: center;
  text-decoration: none;
  background: var(--rd-red);
  color: #fff;
  font-weight: 700;
  margin-top: 12px;
  padding: 14px 16px;
  letter-spacing: -0.01em;
  box-shadow: 0 8px 20px color-mix(in srgb, var(--rd-red) 28%, transparent);
}

.badge {
  display: inline-block;
  font-size: 0.72rem;
  padding: 3px 9px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--ok) 18%, transparent);
  color: var(--ok);
  margin-left: 6px;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.badge.warn {
  background: color-mix(in srgb, #f5c542 22%, transparent);
  color: var(--rd-warn);
}
html[data-theme="dark"] .badge.warn { color: #f0c14d; }

#toast {
  position: fixed;
  left: 14px;
  right: 14px;
  bottom: calc(18px + env(safe-area-inset-bottom));
  background: var(--fg);
  color: var(--bg);
  padding: 13px 16px;
  border-radius: var(--radius-sm);
  z-index: 20;
  text-align: center;
  font-weight: 600;
  box-shadow: var(--shadow);
  max-width: 880px;
  margin: 0 auto;
}

.gate {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  display: grid;
  place-items: center;
  padding: 24px;
  z-index: 30;
}
.gate[hidden] {
  display: none !important;
}
.gate .card h2 {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 8px;
}
.gate .card h2::before {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--rd-red);
}
.gate .card p {
  margin: 0;
  color: var(--hint);
  line-height: 1.45;
}
.gate .muted {
  display: block;
  margin-top: 8px;
  font-size: 0.8rem;
  opacity: 0.85;
}

.role-row {
  display: grid;
  gap: 8px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
}
.role-row:last-child { border-bottom: 0; }
.role-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.role-actions button {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 12px;
  background: var(--sec);
  color: var(--fg);
  font: inherit;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.82rem;
}
.role-actions button.active {
  background: var(--rd-red);
  color: #fff;
  border-color: var(--rd-red);
}
.danger {
  background: var(--rd-red-soft) !important;
  color: var(--rd-red) !important;
  border-color: color-mix(in srgb, var(--rd-red) 35%, transparent) !important;
}
.secure-note {
  font-size: 0.8rem;
  color: var(--hint);
  margin: 0 0 10px;
}
