:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #16202c;
  --muted: #5d6b7a;
  --border: #d9dee5;
  --primary: #1d5fd6;
  --primary-contrast: #ffffff;
  --accent: #f07a1a;
  --ok: #1f8a4c;
  --ok-bg: #e3f4ea;
  --warn: #a15c00;
  --warn-bg: #fff1dc;
  --danger: #c62f2f;
  --danger-bg: #fde6e6;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .06);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141a;
    --surface: #18202a;
    --surface-2: #212b37;
    --text: #e8edf3;
    --muted: #97a4b3;
    --border: #2d3947;
    --primary: #5b93f5;
    --primary-contrast: #0b1320;
    --ok: #4cc27f;
    --ok-bg: #173325;
    --warn: #f0b04a;
    --warn-bg: #3a2c12;
    --danger: #f27272;
    --danger-bg: #3d1c1c;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}
a { color: var(--primary); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grow { flex: 1 1 auto; min-width: 0; }
.stack > * + * { margin-top: .75rem; }
.nowrap { white-space: nowrap; }

/* ---- Buttons & inputs ---- */
button, .btn {
  font: inherit;
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 44px; padding: .55rem 1rem;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  cursor: pointer; text-decoration: none; font-weight: 600;
  -webkit-tap-highlight-color: transparent;
}
button:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-contrast); }
.btn-accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-sm { min-height: 34px; padding: .3rem .7rem; font-size: .88rem; }
.btn-block { width: 100%; }
.btn-xl { min-height: 64px; font-size: 1.1rem; border-radius: 14px; }

label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: .3rem; }
input, select, textarea {
  font: inherit; width: 100%;
  min-height: 44px; padding: .55rem .75rem;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text);
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
input[type=checkbox], input[type=radio] { width: 20px; height: 20px; min-height: 0; padding: 0; flex: none; accent-color: var(--primary); }
.check { display: flex; align-items: center; gap: .5rem; font-weight: 500; }
.field { margin-bottom: .9rem; }
.index-input {
  font-size: 2rem; font-weight: 700; text-align: center; letter-spacing: .05em;
  min-height: 64px; font-variant-numeric: tabular-nums;
}

/* ---- Cards, badges ---- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1rem; box-shadow: var(--shadow);
}
.badge {
  display: inline-block; padding: .12rem .5rem; border-radius: 999px;
  font-size: .75rem; font-weight: 700; background: var(--surface-2); color: var(--muted);
  white-space: nowrap;
}
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.danger { background: var(--danger-bg); color: var(--danger); }
.badge.info { background: var(--surface-2); color: var(--primary); }
.alert { padding: .75rem 1rem; border-radius: 10px; font-weight: 500; }
.alert.warn { background: var(--warn-bg); color: var(--warn); }
.alert.danger { background: var(--danger-bg); color: var(--danger); }
.alert.ok { background: var(--ok-bg); color: var(--ok); }

/* ---- Toast ---- */
#toast {
  position: fixed; left: 50%; bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%); z-index: 1000;
  max-width: min(92vw, 480px); padding: .75rem 1.1rem; border-radius: 12px;
  background: #1c2733; color: #fff; font-weight: 600; box-shadow: 0 8px 24px rgba(0,0,0,.25);
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(-4px); }
#toast.error { background: var(--danger); }
#toast.ok { background: var(--ok); }

/* ---- Login ---- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card { width: 100%; max-width: 380px; }
.brand { display: flex; align-items: center; gap: .6rem; font-weight: 800; font-size: 1.15rem; }
.brand img { width: 36px; height: 36px; }

/* ---- App shell (reader) ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: .6rem;
  padding: calc(.6rem + env(safe-area-inset-top)) 1rem .6rem;
  background: var(--primary); color: var(--primary-contrast);
}
.topbar .title { font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar button { background: transparent; border-color: transparent; color: inherit; min-height: 40px; padding: .3rem .5rem; }
.sync-pill {
  font-size: .75rem; font-weight: 700; padding: .2rem .55rem; border-radius: 999px;
  background: rgba(255,255,255,.2); white-space: nowrap;
}
.sync-pill.offline { background: #f07a1a; color: #fff; }
.screen { padding: 1rem; padding-bottom: calc(1.5rem + env(safe-area-inset-bottom)); max-width: 640px; margin: 0 auto; }

.progress { height: 10px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; background: var(--ok); transition: width .3s; }

.action-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.action-grid .btn-xl { flex-direction: column; gap: .2rem; min-height: 96px; }
.action-grid svg { width: 30px; height: 30px; }

.meter-list { list-style: none; margin: 0; padding: 0; }
.meter-item {
  display: flex; align-items: center; gap: .75rem;
  padding: .8rem .9rem; border-bottom: 1px solid var(--border); cursor: pointer;
  background: var(--surface);
}
.meter-item:first-child { border-top-left-radius: var(--radius); border-top-right-radius: var(--radius); }
.meter-item:last-child { border-bottom: 0; border-bottom-left-radius: var(--radius); border-bottom-right-radius: var(--radius); }
.meter-item:active { background: var(--surface-2); }
.meter-item .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--border); flex: none; }
.meter-item.done .dot { background: var(--ok); }
.meter-item .serial { font-weight: 700; font-family: ui-monospace, Menlo, Consolas, monospace; }

.segmented { display: flex; background: var(--surface-2); padding: 3px; border-radius: 10px; }
.segmented button { flex: 1; min-height: 36px; border: 0; background: transparent; font-size: .9rem; color: var(--muted); }
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: .25rem .9rem; font-size: .95rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-weight: 600; word-break: break-word; }

.consumption { text-align: center; font-size: 1.05rem; padding: .5rem; border-radius: 10px; background: var(--surface-2); }
.consumption strong { font-size: 1.3rem; }

.photo-preview { width: 100%; max-height: 260px; object-fit: contain; border-radius: 10px; background: #000; }

/* ---- Scanner ---- */
.scanner {
  position: fixed; inset: 0; z-index: 100; background: #000; color: #fff;
  display: flex; flex-direction: column;
}
.scanner video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scanner .frame {
  position: absolute; left: 8%; right: 8%; top: 50%; height: 26%;
  transform: translateY(-50%);
  border: 3px solid rgba(255,255,255,.9); border-radius: 14px;
  box-shadow: 0 0 0 100vmax rgba(0,0,0,.45);
}
.scanner .frame::after {
  content: ""; position: absolute; left: 4%; right: 4%; top: 50%; height: 2px;
  background: #ff4d4d; box-shadow: 0 0 8px #ff4d4d; animation: scanline 1.6s ease-in-out infinite;
}
@keyframes scanline { 0%, 100% { transform: translateY(-40px); } 50% { transform: translateY(40px); } }
.scanner .bar {
  position: relative; z-index: 2; display: flex; gap: .5rem; align-items: center;
  padding: calc(.75rem + env(safe-area-inset-top)) 1rem .75rem;
  background: linear-gradient(rgba(0,0,0,.6), transparent);
}
.scanner .bar.bottom {
  margin-top: auto; flex-direction: column; align-items: stretch;
  padding-bottom: calc(1rem + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, rgba(0,0,0,.7));
}
.scanner button { background: rgba(255,255,255,.15); border-color: transparent; color: #fff; }
.scanner .hint { text-align: center; font-weight: 600; text-shadow: 0 1px 3px #000; }

/* ---- Modal ---- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 200; background: rgba(10, 15, 20, .55);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  background: var(--surface); width: 100%; max-width: 640px; max-height: 92vh; overflow: auto;
  border-radius: 16px 16px 0 0; padding: 1.1rem 1rem calc(1rem + env(safe-area-inset-bottom));
}
@media (min-width: 720px) {
  .modal-backdrop { align-items: center; }
  .modal { border-radius: 16px; }
}

/* ---- Backoffice ---- */
.admin-layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 230px; flex: none; background: #13233d; color: #d8e2f0;
  display: flex; flex-direction: column; padding: 1rem .75rem;
  position: sticky; top: 0; height: 100vh;
}
.sidebar .brand { color: #fff; margin: 0 .4rem 1.25rem; font-size: 1rem; }
.sidebar nav a {
  display: flex; align-items: center; gap: .6rem; padding: .6rem .75rem; border-radius: 8px;
  color: inherit; text-decoration: none; font-weight: 600; font-size: .95rem;
}
.sidebar nav a:hover { background: rgba(255,255,255,.07); }
.sidebar nav a.active { background: var(--primary); color: #fff; }
.sidebar .me { margin-top: auto; font-size: .85rem; padding: .5rem; }
.sidebar .me button { margin-top: .5rem; width: 100%; background: rgba(255,255,255,.08); color: #fff; border-color: transparent; }
.admin-main { flex: 1; min-width: 0; padding: 1.25rem; }
.admin-mobilebar { display: none; }
@media (max-width: 860px) {
  .admin-layout { flex-direction: column; }
  .sidebar { display: none; }
  .sidebar.open { display: flex; position: fixed; inset: 0 auto 0 0; z-index: 150; width: 260px; }
  .admin-mobilebar {
    display: flex; align-items: center; gap: .5rem; position: sticky; top: 0; z-index: 50;
    background: #13233d; color: #fff; padding: .5rem .75rem;
  }
  .admin-mobilebar button { background: transparent; color: #fff; border-color: transparent; }
  .admin-main { padding: .9rem; }
}

.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.stat .value { font-size: 1.8rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat .label { color: var(--muted); font-size: .85rem; font-weight: 600; }

.toolbar { display: flex; flex-wrap: wrap; gap: .5rem; align-items: flex-end; margin-bottom: .9rem; }
.toolbar > * { flex: 0 1 auto; }
.toolbar input, .toolbar select { min-height: 38px; padding: .35rem .6rem; width: auto; min-width: 140px; }
.toolbar .search { flex: 1 1 220px; }
.toolbar .search input { width: 100%; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { padding: .55rem .7rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { background: var(--surface-2); font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); position: sticky; top: 0; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.clickable { cursor: pointer; }
.pager { display: flex; gap: .5rem; align-items: center; justify-content: flex-end; margin-top: .75rem; }
.bulkbar { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; padding: .6rem .8rem; margin-bottom: .75rem; border-radius: 10px; background: var(--surface-2); }

.chart { display: flex; align-items: flex-end; gap: 4px; height: 120px; padding-top: .5rem; }
.chart .bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; }
.chart .bar-col div { width: 100%; background: var(--primary); border-radius: 4px 4px 0 0; min-height: 2px; }
.chart .bar-col span { font-size: .68rem; color: var(--muted); }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 0 1rem; }
@media (max-width: 720px) { .grid-2 { grid-template-columns: 1fr; } }
.thumb { width: 64px; height: 48px; object-fit: cover; border-radius: 6px; cursor: zoom-in; }

/* ---- Rute de citire (hartă) ---- */
.map-layout { display: grid; grid-template-columns: minmax(0, 1fr) 360px; gap: 1rem; align-items: start; }
.map { height: calc(100vh - 170px); min-height: 420px; border-radius: var(--radius); border: 1px solid var(--border); z-index: 0; }
#rt-panel { max-height: calc(100vh - 170px); overflow-y: auto; }
@media (max-width: 1000px) {
  .map-layout { grid-template-columns: 1fr; }
  .map { height: 60vh; }
  #rt-panel { max-height: none; }
}
.route-row { border: 1px solid var(--border); border-radius: 10px; padding: .55rem .65rem; display: flex; flex-direction: column; gap: .35rem; }
.swatch { width: 14px; height: 14px; border-radius: 4px; flex: none; }
.leaflet-tooltip.stop-label {
  background: transparent; border: 0; box-shadow: none; padding: 0;
  color: #fff; font-weight: 800; font-size: 9px;
}
.leaflet-tooltip.stop-label::before { display: none; }
.nav-btn { min-height: 34px; padding: .3rem .6rem; font-size: .85rem; }
.route-pos {
  flex: none; min-width: 28px; height: 28px; border-radius: 999px; display: grid; place-items: center;
  background: var(--surface-2); color: var(--primary); font-weight: 800; font-size: .8rem;
}
.meter-item.done .route-pos { background: var(--ok-bg); color: var(--ok); }

/* ---- Listă contoare (cititor): adresă, CLC, serie, ultimul index ---- */
.meter-item { align-items: flex-start; }
.meter-item .dot, .meter-item .route-pos { margin-top: .2rem; }
.meter-address { font-weight: 700; line-height: 1.3; }
.meter-meta { display: flex; flex-wrap: wrap; gap: .15rem .9rem; margin-top: .25rem; font-size: .88rem; }
.meter-meta .mono { font-weight: 700; }
.lbl { color: var(--muted); font-size: .72rem; text-transform: uppercase; letter-spacing: .03em; font-weight: 600; }
.meter-index { flex: none; text-align: right; display: flex; flex-direction: column; align-items: flex-end; gap: .1rem; min-width: 84px; }
.meter-index .val { font-size: 1.15rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.meter-item.done .meter-index .val { color: var(--ok); }
.meter-index .nav-btn { margin-top: .3rem; }

/* ---- Listă de verificare a configurării ---- */
.checklist { list-style: none; margin: 0; padding: 0; display: grid; gap: .5rem; }
.checklist li { display: flex; gap: .6rem; align-items: flex-start; }
.checklist li > span { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: var(--surface-2); color: var(--muted); font-weight: 800; font-size: .8rem; }
.checklist li.ok > span { background: var(--ok-bg); color: var(--ok); }
.checklist li.ok strong { font-weight: 500; color: var(--muted); }

/* ---- Zone de apăsare mai mari pe ecranele cititorului (lucru pe teren, cu mănuși) ---- */
.meter-index .nav-btn { min-height: 44px; min-width: 44px; font-size: 1.1rem; }
#reading-form .check { min-height: 44px; font-size: 1.05rem; }
#reading-form .check input { width: 26px; height: 26px; }
#history .btn-sm { min-height: 40px; }

/* Căutare în toată baza de contoare (aplicația cititorului) */
.meter-item.list-head { cursor: default; font-weight: 700; color: var(--muted); background: var(--surface-2); }
.meter-list.suggest:empty { display: none; }
.meter-list.suggest { max-height: 45vh; overflow-y: auto; }

/* Tipuri contoare: poza-model cu chenarele indexului și seriei */
.mt-thumb { width: 150px; flex: none; }
.mt-canvas { position: relative; width: 100%; aspect-ratio: 3 / 4; background: #1b232d; border-radius: 8px; overflow: hidden; touch-action: none; user-select: none; }
.mt-canvas img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: fill; pointer-events: none; }
.mt-empty { position: absolute; inset: 0; display: grid; place-items: center; color: #8a96a3; font-size: .85rem; }
.mt-box { position: absolute; border: 3px solid; border-radius: 4px; box-shadow: 0 0 0 1px rgba(255,255,255,.6); pointer-events: none; }
.mt-cells { position: absolute; inset: 2px; display: flex; gap: 2px; }
.mt-cells span { flex: 1; border: 1px dashed rgba(255,255,255,.8); border-radius: 2px; }
.mt-cells span.dec { border-color: #ff6b5b; background: rgba(224, 69, 43, .18); }
.mt-editor { max-width: 420px; cursor: crosshair; }
.mt-values { display: flex; flex-wrap: wrap; gap: .3rem 1rem; max-height: 180px; overflow: auto; padding: .3rem 0; }
.mt-swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; vertical-align: middle; }

/* Aplicația cititorului: poza contorului cu chenarele tipului de contor */
.photo-main { display: flex; align-items: center; justify-content: center; gap: .8rem; text-align: left; }
.photo-main small { font-weight: 500; opacity: .9; font-size: .85rem; }
.meter-photo video { object-fit: contain; background: #000; }
.meter-photo .mp-frame { position: fixed; inset: 0; pointer-events: none; }
.mp-box { position: absolute; border: 3px solid; border-radius: 8px; box-shadow: 0 0 0 2px rgba(0,0,0,.35); }
.mp-box > span { position: absolute; top: -1.6rem; left: 0; font-size: .8rem; font-weight: 700; padding: .1rem .45rem; border-radius: 6px; color: #fff; white-space: nowrap; }
.mp-box.idx { border-color: #ff5a44; }
.mp-box.idx > span { background: #e0452b; }
.mp-box.ser { border-color: #4d8dff; }
.mp-box.ser > span { background: #1d5fd6; top: auto; bottom: -1.6rem; }
.mp-cells { position: absolute; inset: 3px; display: flex; gap: 3px; }
.mp-cells i { flex: 1; border: 1px dashed rgba(255,255,255,.75); border-radius: 3px; }
.mp-cells i.dec { border-color: #ff8a7a; background: rgba(224, 69, 43, .15); }
.meter-photo select { width: auto; min-height: 38px; background: rgba(0,0,0,.55); color: #fff; border-color: rgba(255,255,255,.35); }
.btn-shutter { min-height: 60px; font-size: 1.15rem; font-weight: 800; background: #fff !important; color: #111 !important; }

/* Citire automată (backoffice): decupajele cu cifrele */
.ai-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: .75rem; }
.ai-card { display: flex; flex-direction: column; gap: .4rem; padding: .75rem; }
.ai-card img { width: 100%; max-height: 110px; object-fit: contain; background: #111; border-radius: 6px; }

/* Poza contorului: analiza automată */
.photo-busy { position: fixed; inset: 0; z-index: 60; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1rem; background: rgba(0,0,0,.72); color: #fff; font-size: 1.1rem; font-weight: 600; text-align: center; padding: 1rem; }
.photo-busy .spinner { width: 48px; height: 48px; border: 5px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.ai-crop { display: block; width: 100%; max-height: 90px; object-fit: contain; background: #111; border-radius: 6px; margin-bottom: .4rem; }
