:root {
  --bg:        #f4f6fb;
  --surface:   #ffffff;
  --border:    #e2e8f0;
  --text:      #0f172a;
  --text-soft: #64748b;
  --accent:    #2563eb;
  --accent-2:  #1d4ed8;
  --ok:        #16a34a;
  --warn:      #d97706;
  --danger:    #dc2626;
  --shadow:    0 1px 3px rgba(15,23,42,.08), 0 8px 24px -12px rgba(15,23,42,.18);
  --radius:    16px;
  --font:      'Inter', system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

body { min-height: 100vh; }

.hidden { display: none !important; }

/* Layout */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { display: flex; align-items: center; gap: 10px; font-weight: 700; min-width: 0; }
.topbar .brand img { width: 28px; height: 28px; flex: 0 0 auto; }
.brand-name { white-space: nowrap; }
.brand-email {
  font-weight: 400; font-size: .85rem; color: var(--text-soft);
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar nav { display: flex; gap: 8px; align-items: center; }

.gate-bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.gate-bar select { width: auto; min-width: 140px; }
.gate-bar .btn { padding: 8px 14px; }

.container { max-width: 720px; margin: 0 auto; padding: 24px 16px 64px; }
.center-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  margin-bottom: 20px;
}
.card h2 { margin: 0 0 4px; font-size: 1.15rem; }
.card .muted { color: var(--text-soft); font-size: .9rem; margin: 0 0 18px; }
.card.auth { width: 100%; max-width: 400px; }

/* Collapsible settings cards (accordion) — collapsed by default to save space. */
.card.collapsible { padding-top: 18px; padding-bottom: 18px; margin-bottom: 12px; }
.card.collapsible > h2 {
  margin: 0; cursor: pointer; user-select: none;
  display: flex; align-items: center; gap: 8px;
}
.card.collapsible > h2::after {
  content: '▾'; font-size: .85em; line-height: 1; color: var(--text-soft);
  transition: transform .15s ease; margin-left: auto;
}
.card.collapsible.open > h2::after { transform: rotate(180deg); }
.card.collapsible > .card-body { display: none; }
.card.collapsible.open > .card-body { display: block; margin-top: 18px; }
.card.collapsible.open { padding-bottom: 24px; }

/* Help "?" dots next to category titles + the help modal they open. */
.help-dot {
  display: inline-flex; align-items: center; justify-content: center;
  width: 18px; height: 18px; padding: 0; flex: 0 0 auto; vertical-align: middle;
  border-radius: 50%; border: 1px solid var(--border); background: var(--bg);
  color: var(--text-soft); font: 700 12px/1 system-ui, sans-serif; cursor: pointer;
}
.help-dot:hover { background: var(--accent); color: #fff; border-color: var(--accent); }
.modal-backdrop {
  position: fixed; inset: 0; z-index: 1000; padding: 20px;
  background: rgba(15, 23, 42, .5); display: flex; align-items: center; justify-content: center;
}
.modal-backdrop.hidden { display: none; }
.modal-card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  width: 100%; max-width: 440px; max-height: 82vh; overflow: auto; padding: 24px;
}
.modal-card h3 { margin: 0 0 12px; }
.modal-card .modal-body { color: var(--text); font-size: .95rem; line-height: 1.6; white-space: pre-line; }
.modal-card .modal-close { margin-top: 18px; width: 100%; }

/* Help (onboarding) tab */
.help-steps { margin: 0; padding-left: 22px; }
.help-steps li { margin: 0 0 14px; line-height: 1.55; }
.help-steps li b { color: var(--text); }
.help-note { background: var(--bg); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; margin: 14px 0 0; font-size: .9rem; color: var(--text-soft); }

/* Forms */
label { display: block; font-size: .85rem; color: var(--text-soft); margin: 14px 0 6px; }
input, select {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  font: inherit;
  background: #fff;
  color: var(--text);
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
.row { display: flex; gap: 12px; }
.row > * { flex: 1; }
.inline { display: flex; align-items: center; gap: 8px; }
.inline input[type="checkbox"] { width: auto; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 18px;
  border: 1px solid transparent;
  border-radius: 12px;
  font: inherit; font-weight: 600;
  cursor: pointer;
  background: var(--accent); color: #fff;
  transition: background .15s ease;
}
.btn:hover { background: var(--accent-2); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.secondary:hover { background: #f8fafc; }
.btn.danger { background: var(--danger); }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.block { width: 100%; }
.btn.big { padding: 16px 22px; font-size: 1.05rem; }

/* Monochrome icons inside buttons. By default (wide screens) the text label is
   shown and the icon is hidden; on phones (see media query) this is swapped. */
.ico { width: 20px; height: 20px; flex: 0 0 auto; }
.btn.big .ico { width: 22px; height: 22px; }
.btn.responsive .ico { display: none; }

/* Badges / status */
.badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px;
  font-size: .72rem; font-weight: 600; border: 1px solid var(--border);
}
.badge.set { color: var(--ok); border-color: #bbf7d0; background: #f0fdf4; }
.badge.unset { color: var(--text-soft); }
.badge.pending { color: var(--warn); border-color: #fed7aa; background: #fffbeb; }
.badge.expired { color: var(--danger); border-color: #fecaca; background: #fef2f2; }

/* Superadmin gets a wider content column so all the action buttons fit on one row. */
.container-wide { max-width: 1100px; }

/* Superadmin user rows: info on top, a row of actions below (left-aligned). */
.admin-user { flex-direction: column; align-items: stretch; gap: 10px; text-align: left; }
.admin-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: flex-start; }
@media (min-width: 800px) {
  .container-wide .admin-actions { flex-wrap: nowrap; }
}
.admin-actions input[type="date"] { padding: 8px 10px; }
.admin-actions input[type="number"] { padding: 8px 10px; }
.admin-actions .btn { padding: 8px 14px; }

/* Desktop: lay the Settings cards out in two columns (each card stays whole).
   The container widens; the Live view stays a comfortable single column. */
@media (min-width: 900px) {
  .container { max-width: 1040px; }
  #tab-live-panel { max-width: 760px; margin-inline: auto; }
  #tab-settings-panel { columns: 2; column-gap: 20px; }
  #tab-settings-panel .card { break-inside: avoid; -webkit-column-break-inside: avoid; }
}

/* Access-group day chips + gate checkboxes */
.day-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.day-chip { display: inline-flex; align-items: center; gap: 5px; padding: 6px 10px; border: 1px solid var(--border); border-radius: 999px; font-size: .85rem; cursor: pointer; }
.day-chip input { width: auto; }
#grp-gates label { display: flex; align-items: center; gap: 8px; }
#grp-gates input { width: auto; }

/* Impersonation banner shown in the main app while acting as another account */
.imp-banner {
  background: var(--warn); color: #fff; text-align: center;
  padding: 9px 14px; font-size: .9rem; font-weight: 600;
}
.imp-banner .toggle-link { color: #fff; text-decoration: underline; font-weight: 700; }

/* "listening for button" indicator on the Live view */
.listen { display: inline-block; margin: 0 0 6px; font-size: .8rem; font-weight: 600; color: var(--ok); }

code {
  background: #f1f5f9; padding: 1px 6px; border-radius: 6px;
  font-family: ui-monospace, monospace; font-size: .85em;
}

.result {
  border-radius: 12px; padding: 14px 16px; margin-top: 14px;
  font-weight: 600; border: 1px solid var(--border); background: #f8fafc;
}
.result.opened { color: var(--ok); border-color: #bbf7d0; background: #f0fdf4; }
.result.mismatch, .result.error { color: var(--danger); border-color: #fecaca; background: #fef2f2; }
.result.warn { color: var(--warn); border-color: #fed7aa; background: #fffbeb; }

/* Plates / events lists */
.list { list-style: none; padding: 0; margin: 14px 0 0; }
.list li {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 12px; margin-bottom: 8px;
}
.list li .plate { font-weight: 700; letter-spacing: .04em; font-family: ui-monospace, monospace; }
.list li .label { color: var(--text-soft); font-size: .85rem; margin-left: 8px; }
.event-outcome { font-size: .8rem; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--border); }
.event-outcome.opened { color: var(--ok); }
.event-outcome.error, .event-outcome.plate_mismatch { color: var(--danger); }
.event-outcome.cooldown, .event-outcome.low_confidence, .event-outcome.no_plate { color: var(--warn); }
.event-outcome.qr_detected { color: #7c3aed; }
.event-outcome.manual_open { color: #0891b2; }
.event-outcome.qr_exit_failed { color: var(--danger); }
.event-thumb { width: 60px; height: 40px; object-fit: cover; border-radius: 6px; flex: 0 0 auto; display: block; }
time { color: var(--text-soft); font-size: .8rem; }

/* Camera */
.camera-wrap {
  position: relative; border-radius: var(--radius); overflow: hidden;
  background: #0f172a; aspect-ratio: 4 / 3;
}
.camera-wrap video { width: 100%; height: 100%; object-fit: cover; display: block; }
.camera-controls { display: flex; gap: 10px; margin-top: 14px; flex-wrap: wrap; }

.error-text { color: var(--danger); font-size: .85rem; margin-top: 8px; min-height: 1em; }
.toggle-link { background: none; border: none; color: var(--accent); cursor: pointer; font: inherit; padding: 0; }
hr { border: none; border-top: 1px solid var(--border); margin: 22px 0; }

/* ---- Phone (portrait): icon-only buttons + stacked input rows ---- */
@media (max-width: 640px) {
  .topbar { padding: 12px 14px; }
  .topbar nav { gap: 6px; }

  /* Swap: hide the text label, show the monochrome icon. */
  .btn.responsive .btn-label { display: none; }
  .btn.responsive { padding: 10px; min-width: 44px; }
  .btn.responsive .ico { display: inline-flex; align-items: center; justify-content: center; }

  /* Auto button: play/stop icon swap driven by the .active class. */
  .btn.responsive .ico-stop { display: none; }
  .btn-auto.active .ico-play { display: none; }
  .btn-auto.active .ico-stop { display: inline-flex; }

  /* Primary camera actions stay large and split the width. */
  .camera-controls .btn.responsive { flex: 1; padding: 16px; }

  /* Side-by-side input pairs stack vertically so they never overflow. The
     !important overrides the inline `align-items:flex-end` (a desktop-only
     bottom-alignment) which, in a column, would shrink-wrap each field and
     push it off the right edge. */
  .row { flex-direction: column; gap: 0; align-items: stretch !important; }
  .row > * { width: 100%; }
  /* Labels carry the vertical rhythm; bare buttons in a stacked row have none,
     so give them a little breathing room from the field/each other. */
  .row > .btn { margin-top: 10px; }
}

/* --- Language picker (i18n) --- */
.lang-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 4px; }
.lang-option {
  text-align: left; padding: 12px 14px; border: 1px solid var(--border);
  border-radius: 12px; background: #fff; color: var(--text); font: inherit; cursor: pointer;
}
.lang-option:hover { border-color: var(--accent); }
.lang-option.active { border-color: var(--accent); background: var(--accent); color: #fff; font-weight: 600; }
.lang-modal .modal-card { max-width: 420px; }
/* Icon-only globe trigger (login card / qr page) */
.lang-trigger { padding: 8px; min-width: 40px; line-height: 0; }
.lang-trigger .ico { width: 22px; height: 22px; }
.card.auth { position: relative; }
.auth-lang { position: absolute; top: 14px; right: 14px; }
.qr-lang { position: fixed; top: 12px; right: 12px; z-index: 20; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.15); }
@media (max-width: 600px) { .lang-grid { grid-template-columns: 1fr; } }

/* Notifications matrix (channels × triggers) */
.notify-matrix { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
.notify-table { width: 100%; max-width: 100%; border-collapse: collapse; font-size: .85rem; table-layout: fixed; }
.notify-table th, .notify-table td { padding: 6px 2px; text-align: center; border-bottom: 1px solid var(--border); }
.notify-table th:first-child, .notify-table td:first-child { text-align: left; color: var(--text-soft); width: 40%; overflow-wrap: anywhere; }
.notify-table thead th { color: var(--text-soft); font-weight: 600; font-size: .72rem; white-space: nowrap; }
.notify-table input[type="checkbox"] { width: 18px; height: 18px; }

/* Live occupancy badge turns amber when full (reuses .badge.pending colour) */
#occupancy-indicator { font-variant-numeric: tabular-nums; }

/* Transient alert toasts (the in-app notification channel) */
.toast-host { position: fixed; top: 16px; right: 16px; z-index: 100; display: flex; flex-direction: column; gap: 8px; max-width: 360px; }
.toast {
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-left: 4px solid var(--accent); border-radius: 8px; padding: 10px 14px;
  box-shadow: 0 4px 16px rgba(0,0,0,.12); font-size: .92rem;
  animation: toast-in .25s ease; }
.toast.out { opacity: 0; transform: translateX(20px); transition: opacity .4s, transform .4s; }
@keyframes toast-in { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: none; } }

/* Compact icon-only action button (e.g. ⚙️ edit in lists) */
.btn.icon-btn { padding: 6px 10px; line-height: 1; }

/* In-app printable report overlay (Logs → Export PDF). Stays within the app so
   the user is never stranded; print CSS hides everything else. */
.print-overlay { position: fixed; inset: 0; z-index: 2000; background: #fff; overflow: auto; padding: 20px; }
.print-toolbar { position: sticky; top: 0; display: flex; gap: 8px; justify-content: flex-end;
  background: #fff; padding-bottom: 12px; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.print-doc h1 { font-size: 18px; margin: 0 0 2px; }
.print-doc .sub { color: var(--text-soft); font-size: 12px; margin: 0 0 16px; }
.print-doc table { width: 100%; border-collapse: collapse; }
.print-doc td { border-bottom: 1px solid var(--border); padding: 8px; vertical-align: top; }
.print-doc td.img { width: 150px; }
.print-doc img { width: 140px; height: auto; border-radius: 6px; display: block; }
.print-doc .noimg { color: #94a3b8; }
.print-doc .plate { font-weight: 700; font-size: 15px; letter-spacing: .5px; }
.print-doc .meta { color: var(--text-soft); font-size: 12px; margin-top: 2px; }
.print-doc .outcome { font-size: 12px; margin-top: 4px; }
@media print {
  body > *:not(.print-overlay) { display: none !important; }
  .print-overlay { position: static; padding: 0; overflow: visible; }
  .print-toolbar { display: none !important; }
  .print-doc tr { break-inside: avoid; }
}

/* Reports ("Справки") table */
.rep-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.rep-table th, .rep-table td { padding: 7px 8px; border-bottom: 1px solid var(--border); text-align: left; }
.rep-table th { color: var(--text-soft); font-weight: 600; font-size: .8rem; }
.rep-table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rep-table tfoot td { font-weight: 700; border-top: 2px solid var(--border); border-bottom: none; }
.rep-table .muted { font-weight: 400; }

/* Superadmin account cards — collapsible, with grouped controls (scales as more
   per-account options are added). */
.list li.sa-acc { display: block; padding: 0; overflow: hidden; }
.list li.sa-acc.inactive { opacity: .85; }
.sa-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px; cursor: pointer; }
.sa-head:hover { background: #f8fafc; }
.sa-head-main { flex: 1; min-width: 0; }
.sa-summary { display: block; margin: 4px 0 0; color: var(--text-soft); font-size: .82rem; line-height: 1.5; }
.sa-chevron { flex: 0 0 auto; color: var(--text-soft); transition: transform .15s ease; }
.sa-acc.open .sa-chevron { transform: rotate(180deg); }
.sa-body { display: none; padding: 4px 14px 14px; border-top: 1px solid var(--border); }
.sa-acc.open .sa-body { display: block; }
.sa-groups { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 12px; }
.sa-group { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; background: var(--bg); }
.sa-glabel { font-size: .68rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-soft); margin-bottom: 8px; font-weight: 600; }
.sa-controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.sa-controls .btn { padding: 8px 12px; }
.sa-controls input { width: auto; padding: 8px 10px; }
.sa-controls input[type="number"] { width: 64px; }

/* API keys card */
.api-scopes { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 6px 14px; margin-top: 4px; }
.api-scopes label { font-size: .85rem; color: var(--text); }
.api-scopes input[type="checkbox"] { width: auto; }
.api-newkey code { font-family: ui-monospace, monospace; font-size: .85rem; }
