:root {
  --bg: #f5f5f5;
  --surface: #ffffff;
  --on-surface: #1a1a1a;
  --muted: #5f6368;
  --primary: #1565c0;
  --on-primary: #ffffff;
  --border: #e0e0e0;
  --error: #c62828;
  --debt: #c62828;
  --credit: #2e7d32;
  --font-scale: 1;
  font-size: calc(16px * var(--font-scale));
}
[data-theme="dark"] {
  --bg: #121212;
  --surface: #1e1e1e;
  --on-surface: #eaeaea;
  --muted: #9aa0a6;
  --primary: #64b5f6;
  --on-primary: #06243d;
  --border: #333;
  --error: #ef5350;
  --debt: #ef5350;
  --credit: #66bb6a;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* overscroll-behavior: none — вимикає «потягни, щоб оновити» (pull-to-refresh),
   щоб випадковий свайп униз не перезавантажив сторінку й не скинув стан
   (відкритий абонент, налаштовані фільтри, введені показники). */
html { overscroll-behavior: none; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--on-surface);
  overscroll-behavior: none; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
#app { max-width: 720px; margin: 0 auto; min-height: 100vh; }

.topbar { position: sticky; top: 0; z-index: 10; background: var(--primary); color: var(--on-primary);
  display: flex; align-items: center; gap: .5rem; padding: max(env(safe-area-inset-top), .5rem) 1rem .75rem;
  font-size: 1.1rem; font-weight: 600; }
.topbar .grow { flex: 1; }
.topbar button { background: rgba(255,255,255,.18); color: var(--on-primary); border: 0;
  border-radius: 8px; padding: .45rem .8rem; font-size: .9rem; }
.topbar .back { background: transparent; padding: .3rem .5rem; font-size: 1.1rem; }

.content { padding: 1rem; padding-bottom: 2rem; }

.tabs { display: flex; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky;
  top: 0; }
.tabs button { flex: 1; background: none; border: 0; padding: .9rem .3rem; color: var(--muted);
  font-size: .95rem; border-bottom: 2px solid transparent; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

label.field { display: block; margin-bottom: .7rem; }
label.field span { display: block; font-size: .8rem; color: var(--muted); margin-bottom: .2rem; }
input, select { width: 100%; padding: .7rem .8rem; font-size: 1rem; border: 1px solid var(--border);
  border-radius: 10px; background: var(--surface); color: var(--on-surface); }
input:focus, select:focus { outline: 2px solid var(--primary); border-color: var(--primary); }
.row2 { display: flex; gap: .6rem; }
.row2 > * { flex: 1; }

/* Рядок чіпів з одним активним варіантом (напр. парність будинків). */
.chips { display: flex; gap: .6rem; margin-bottom: .7rem; }
.chips .chip { flex: 1; width: auto; padding: .55rem .3rem; font-size: .9rem; text-align: center;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface); color: var(--on-surface); }
.chips .chip.active { border-color: var(--primary); background: var(--primary); color: var(--on-primary); font-weight: 600; }

button.btn { width: 100%; padding: .8rem; font-size: 1rem; border: 0; border-radius: 10px;
  background: var(--primary); color: var(--on-primary); font-weight: 600; }
button.btn.secondary { background: transparent; color: var(--primary); border: 1px solid var(--primary); }
button.btn.small { width: auto; padding: .5rem .9rem; font-size: .9rem; }

.checkbox { display: flex; align-items: center; gap: .5rem; margin: .3rem 0; }
.checkbox input { width: auto; }

.count { font-size: .8rem; color: var(--muted); margin: .4rem 0; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: .9rem; margin-bottom: .6rem; }
/* Рядки списку за межами екрана браузер не розкладає й не малює (економія
   CPU/батареї при прокручуванні сотень результатів). contain-intrinsic-size
   тримає правильну висоту смуги прокрутки для нерозкладених рядків. */
#results .card { content-visibility: auto; contain-intrinsic-size: auto 96px; }
.card.tap:active { background: var(--bg); }
.card .title { font-weight: 600; margin-bottom: .2rem; }
.card .sub { color: var(--muted); font-size: .9rem; }
.balance.debt { color: var(--debt); font-weight: 600; }
.balance.credit { color: var(--credit); }
.phone { color: var(--primary); text-decoration: underline; }
.overdue { color: var(--error); font-weight: 600; }
.comment { color: var(--muted); font-size: .85rem; white-space: pre-wrap; }
.comment.collapsed { display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden; }

.section-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 1rem; margin-bottom: 1rem; }
.section-card h3 { margin: 0 0 .7rem; font-size: 1rem; }
.note { font-size: .8rem; color: var(--muted); }
.status { font-size: .9rem; padding: .6rem; background: var(--surface); border-radius: 10px; border: 1px solid var(--border); }

.meter { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: .9rem; margin-bottom: .7rem; }
.meter .num { font-weight: 600; }
.meter .last { color: var(--muted); font-size: .9rem; margin: .2rem 0; }
.meter .entry { display: flex; gap: .5rem; align-items: flex-end; margin-top: .5rem; }
.meter .entry label.field { flex: 1; margin: 0; }
.meter .saved { color: var(--credit); font-size: .9rem; margin-top: .4rem; }
.err { color: var(--error); font-size: .85rem; margin-top: .25rem; }

.fontbtns { display: flex; gap: .5rem; flex-wrap: wrap; }
.fontbtns button { flex: 1; }

.dialog-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex;
  align-items: center; justify-content: center; padding: 1.5rem; z-index: 50; }
.dialog { background: var(--surface); border-radius: 14px; padding: 1.2rem; max-width: 420px; width: 100%; }
.dialog h3 { margin: 0 0 .6rem; }
.dialog p { margin: 0 0 1rem; color: var(--on-surface); }
.dialog .actions { display: flex; gap: .5rem; justify-content: flex-end; flex-wrap: wrap; }
.dialog .actions button { background: none; border: 0; color: var(--primary); font-weight: 600;
  padding: .5rem .8rem; font-size: 1rem; }

.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; }
.expired { padding: 2rem 1.5rem; }
.expired h2 { margin-top: 0; }
.switch-row { display: flex; align-items: center; gap: .6rem; margin: .5rem 0; }
.switch-row input { width: auto; transform: scale(1.3); }

.fab-top { position: fixed; right: 16px; bottom: max(16px, env(safe-area-inset-bottom));
  width: 52px; height: 52px; border-radius: 50%; background: var(--primary); color: var(--on-primary);
  border: 0; font-size: 1.6rem; line-height: 1; display: none; box-shadow: 0 3px 10px rgba(0,0,0,.35); z-index: 30; }
.fab-top.show { display: block; }

/* ---- список виділених у результатах пошуку ---- */
.row-sel { display: flex; align-items: stretch; gap: .6rem; }
.row-sel .rowcheck { width: auto; align-self: center; transform: scale(1.3); flex: 0 0 auto; }
.row-sel .row-body { flex: 1; min-width: 0; }
.row-sel .row-body.tap:active { background: var(--bg); }

.selrow { display: flex; align-items: center; gap: .6rem; margin-top: .4rem; flex-wrap: wrap; }
.iconbtn { background: transparent; border: 1px solid var(--border); border-radius: 8px;
  padding: .35rem .55rem; font-size: 1.05rem; color: var(--on-surface); }
.hint-credit { color: var(--credit); font-size: .75rem; font-weight: 600; }

/* ---- картка: інфо-рядки, пароль, прописано ---- */
.kv { font-size: .9rem; margin: .1rem 0; }
.regpw { display: flex; flex-wrap: wrap; gap: .3rem 1.2rem; margin: .2rem 0; align-items: center; }
.pw { color: var(--primary); font-size: .9rem; cursor: pointer; user-select: none; }

/* ---- результат контакту ---- */
.contact-head { color: var(--primary); font-weight: 600; padding: .3rem 0; cursor: pointer; }
.contact-list { display: flex; flex-direction: column; gap: .1rem; margin-top: .3rem; }
.radio-row { display: flex; align-items: center; gap: .5rem; padding: .35rem 0; }
.radio-row input { width: auto; transform: scale(1.2); }

/* ---- нижній рядок лічильника (стан + фото) ---- */
.meter-bottom { display: flex; align-items: center; gap: .6rem; margin-top: .5rem; }
.meter-bottom .saved { flex: 1; margin: 0; }
.meter-bottom > span { flex: 1; }

/* ---- тост ---- */
.toast { position: fixed; left: 50%; bottom: 2rem; transform: translateX(-50%) translateY(1rem);
  background: rgba(0,0,0,.85); color: #fff; padding: .6rem 1rem; border-radius: 10px; font-size: .9rem;
  opacity: 0; transition: opacity .2s, transform .2s; z-index: 100; pointer-events: none; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }




/* ---- керування обліковими записами ---- */
.acct-row { border: 1px solid var(--border); border-radius: 10px; padding: .6rem; margin-bottom: .6rem; background: var(--surface); }
.acct-row .a-name { margin-bottom: .4rem; }
.acct-sub { display: flex; gap: .5rem; align-items: center; }
.acct-sub .a-pin { flex: 0 0 5.5rem; }
.acct-sub .a-role { flex: 1; }
.acct-sub .a-del { flex: 0 0 auto; }

/* ---- запасний ручний імпорт (розкривний) ---- */
.import-manual > summary { cursor: pointer; color: var(--muted); font-size: .9rem; padding: .2rem 0; user-select: none; }
.import-manual[open] > summary { color: var(--on-surface); font-weight: 600; }



/* ---- банер оновлення застосунку ---- */
.update-bar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 300;
  display: flex; align-items: center; gap: .8rem; justify-content: center;
  padding: .7rem 1rem max(env(safe-area-inset-bottom), .7rem);
  background: var(--primary); color: var(--on-primary); font-size: .95rem;
  box-shadow: 0 -2px 10px rgba(0,0,0,.25); }
.update-bar button { background: #fff; color: var(--primary); border: 0; border-radius: 8px;
  padding: .5rem 1rem; font-size: .95rem; font-weight: 600; }

/* ---- індикатор завантаження ---- */
.spinner { display: inline-block; width: 1em; height: 1em; vertical-align: -.15em;
  border: 2px solid var(--border); border-top-color: var(--primary); border-radius: 50%;
  animation: spin .7s linear infinite; margin-right: .4rem; }
@keyframes spin { to { transform: rotate(360deg); } }
.btn .spinner { border-color: rgba(255,255,255,.45); border-top-color: #fff; }
.btn.secondary .spinner { border-color: var(--border); border-top-color: var(--primary); }
.status.busy { display: flex; align-items: center; }

/* ---- переглядач фото (лайтбокс) ---- */
.photo-viewer { position: fixed; inset: 0; z-index: 200; background: rgba(0,0,0,.97);
  display: flex; flex-direction: column; }
.pv-bar { display: flex; align-items: center; gap: .5rem; padding: max(env(safe-area-inset-top), .6rem) .8rem .6rem;
  color: #fff; }
.pv-bar .grow { flex: 1; }
.pv-count { font-size: .95rem; opacity: .9; }
.pv-btn { background: rgba(255,255,255,.15); color: #fff; border: 0; border-radius: 8px;
  padding: .45rem .7rem; font-size: .9rem; }
.pv-stage { flex: 1; position: relative; display: flex; align-items: center; justify-content: center;
  padding: .5rem; min-height: 0; }
.pv-stage img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 6px; }
.pv-nav { position: absolute; top: 50%; transform: translateY(-50%); background: rgba(255,255,255,.15);
  color: #fff; border: 0; width: 44px; height: 64px; font-size: 2rem; line-height: 1;
  display: flex; align-items: center; justify-content: center; }
.pv-nav.prev { left: 6px; border-radius: 0 10px 10px 0; }
.pv-nav.next { right: 6px; border-radius: 10px 0 0 10px; }

/* Друк відкритого фото: на аркуш іде лише сам знімок, без темного тла,
   панелі й стрілок. Якщо переглядач закритий — друкувати нема чого. */
@media print {
  @page { margin: 10mm; }
  body.printing-photo { background: #fff; }
  body.printing-photo > *:not(.photo-viewer) { display: none !important; }
  body.printing-photo .photo-viewer { position: static; background: #fff; display: block; }
  body.printing-photo .photo-viewer .pv-bar,
  body.printing-photo .photo-viewer .pv-nav { display: none !important; }
  body.printing-photo .photo-viewer .pv-stage { display: block; padding: 0; text-align: center; }
  body.printing-photo .photo-viewer .pv-stage img { max-width: 100%; max-height: 90vh;
    width: auto; height: auto; border-radius: 0; }
  body.printing-photo .pv-caption { display: block !important; margin: 0 0 6mm;
    color: #000; font-size: 12pt; text-align: left; }
}
.pv-caption { display: none; }

/* ==================== ПК-версія (широкі екрани) ==================== */
@media (min-width: 1000px) {
  #app { max-width: 1100px; }

  /* Мишею зручніше з видимими станами наведення */
  button, .tap, .chip, .radio-row, .contact-head, select, input[type="checkbox"], .rowcheck { cursor: pointer; }
  .row-sel .row-body.tap:hover { background: var(--bg); }
  button.btn:hover { filter: brightness(1.08); }
  .tabs button:hover { color: var(--primary); }

  /* Екран пошуку: форма ліворуч (закріплена), результати праворуч */
  .content:has(> #fields) {
    display: grid;
    grid-template-columns: 360px minmax(0, 1fr);
    gap: 0 2rem;
    padding: 1.5rem 2rem 3rem;
    align-items: start;
  }
  .content:has(> #fields) > #fields {
    grid-column: 1;
    grid-row: 1 / span 2;
    position: sticky;
    top: 8rem;             /* під закріпленими шапкою і вкладками */
    align-self: start;
  }
  .content:has(> #fields) > .count { grid-column: 2; grid-row: 1; margin-top: 0; }
  .content:has(> #fields) > #results { grid-column: 2; grid-row: 2; }

  /* Екрани картки й «Дані»: звужена центрована колонка для читабельності */
  .content:not(:has(> #fields)) { max-width: 680px; margin: 0 auto; padding: 1.5rem 1rem 3rem; }
}

.btn.secondary.danger { color: var(--error); border-color: var(--error); }

/* ---- візит у статистиці обходів ---- */
.visit { border-top: 1px solid var(--border); padding: .5rem 0; }
.visit:first-of-type { border-top: 0; }
.visit .selrow { margin-top: .4rem; }

/* ---- коментар, що згортається ---- */
.cmt-wrap { margin: .35rem 0; }
.cmt-toggle { background: transparent; border: 1px solid var(--border); border-radius: 8px; padding: .3rem .6rem; font-size: .85rem; color: var(--primary); }
.cmt-body { margin-top: .4rem; color: var(--on-surface); font-size: .9rem; white-space: pre-wrap; background: var(--bg); border-radius: 8px; padding: .5rem .6rem; }

/* Статистика обходу (старший інспектор / адмін) */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: .5rem; margin-top: .6rem; }
.stat-box { background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: .5rem .4rem; text-align: center; }
.stat-box b { display: block; font-size: 1.25rem; line-height: 1.2; color: var(--primary); }
.stat-box span { font-size: .78rem; color: var(--muted); }
.tbl-wrap { overflow-x: auto; }
.stat-tbl { width: 100%; border-collapse: collapse; font-size: .88rem; }
.stat-tbl th, .stat-tbl td { padding: .35rem .5rem; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
.stat-tbl th { color: var(--muted); font-weight: 600; font-size: .8rem; }
.stat-tbl th.num, .stat-tbl td.num { text-align: right; }
.stat-tbl tbody tr:last-child td { border-bottom: none; }

/* ---- вбудована камера ---- */
.cam { position: fixed; inset: 0; z-index: 300; background: #000; display: flex; flex-direction: column; }
.cam-video { flex: 1; min-height: 0; width: 100%; object-fit: contain; background: #000; }
.cam-msg { position: absolute; top: 45%; left: 0; right: 0; text-align: center; color: #fff; font-size: 1rem; }
.cam-bar { display: flex; align-items: center; justify-content: space-around;
  padding: .8rem 1rem max(.8rem, env(safe-area-inset-bottom)); background: #000; }
.cam-btn { width: 52px; height: 52px; border-radius: 50%; border: 0; background: rgba(255,255,255,.18);
  color: #fff; font-size: 1.4rem; line-height: 1; }
.cam-btn.on { background: #ffd54f; color: #000; }
.cam-shot { width: 74px; height: 74px; border-radius: 50%; border: 5px solid #fff; background: rgba(255,255,255,.25); }
.cam-shot:active { background: #fff; }
.cam-shot:disabled { opacity: .4; }
.cam-alt { position: absolute; top: max(env(safe-area-inset-top), .6rem); right: .6rem; border: 0;
  border-radius: 8px; padding: .45rem .7rem; font-size: .85rem; background: rgba(255,255,255,.18); color: #fff; }
