/* Event Power — mobile-first field app styling.
 * --brand is set per-company at runtime from GET /api/org/branding. */
:root {
  /* Follow the OS light/dark preference. `color-scheme` makes native controls
     (date/time pickers, scrollbars, autofill) match the ground so nothing looks
     missing. Default tokens below are DARK; the light block overrides them. */
  color-scheme: light dark;
  --brand: #0f7b6c;
  --bg: #0f1720;
  --panel: #16212e;
  --panel-2: #1d2a3a;
  --line: #263447;
  --text: #e7eef6;
  --muted: #93a4b8;
  --ok: #2fae7d;
  --warn: #d99a34;
  --error: #e05a5a;
  --pending: #e0a13a;
  --shadow: 0 6px 24px rgba(0,0,0,.45);
  --hit: rgba(245,197,24,.18);
  --radius: 12px;
  --tap: 44px;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6f9;
    --panel: #ffffff;
    --panel-2: #eef2f7;
    --line: #d7dee8;
    --text: #10202e;
    --muted: #5b6b7c;
    --shadow: 0 6px 20px rgba(15,32,46,.12);
    --hit: rgba(245,197,24,.22);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border-bottom: 1px solid var(--line);
  padding: max(8px, env(safe-area-inset-top)) 12px 6px;
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; min-width: 0; }
.brand img { height: 26px; width: auto; border-radius: 6px; }
#brandName { color: var(--brand); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Right cluster: colour-coded sync dot + menu button. */
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.sync-dot { width: 26px; height: 26px; min-height: 0; padding: 0; border-radius: 50%; border: 2px solid var(--line);
  background: var(--muted); cursor: pointer; flex: 0 0 auto; position: relative; }
.sync-dot.ok { background: #2fae7d; border-color: #1f6a4f; }
.sync-dot.pending { background: var(--pending); border-color: #a97b1e; }
.sync-dot.busy { background: #3b82f6; border-color: #2b62c0; animation: pulse 1s ease-in-out infinite; }
.sync-dot.offline { background: #6b7787; border-color: #4a5666; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .45; } }
@media (prefers-reduced-motion: reduce) { .sync-dot.busy { animation: none; } }

.menu-wrap { position: relative; }
.menu-btn { width: 40px; height: 40px; min-height: 0; padding: 0; border-radius: 10px; font-size: 1.2rem;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text); cursor: pointer; }
.top-menu { position: absolute; right: 0; top: calc(100% + 8px); min-width: 210px; z-index: 60;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); overflow: hidden; padding: 6px; }
.top-menu[hidden] { display: none; }
.tm-head { padding: 8px 10px 6px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.tm-who { font-weight: 700; }
.tm-item { display: block; width: 100%; text-align: left; background: transparent; border: 0; color: var(--text);
  padding: 10px 10px; border-radius: 8px; cursor: pointer; font: inherit; min-height: 40px; }
.tm-item:hover { background: var(--panel-2); }
.tm-foot { padding: 6px 10px 4px; border-top: 1px solid var(--line); margin-top: 4px; }

/* Main nav: a single horizontally-scrollable row (never falls off the edge). */
.nav { display: flex; align-items: center; gap: 4px; width: 100%; margin-top: 6px; padding-top: 6px;
  border-top: 1px solid var(--line); overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch;
  scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }
.navlink { color: var(--text); text-decoration: none; padding: 7px 12px; border-radius: 8px; font-weight: 600; white-space: nowrap; flex: 0 0 auto; }
.navlink:hover { background: var(--panel-2); }
.navlink.active { background: var(--brand); color: #fff; }

/* ---------- Chips (status indicators) ---------- */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 10px; border-radius: 999px; font-size: .78rem; font-weight: 600;
  border: 1px solid var(--line); white-space: nowrap;
}
.chip.conn.offline { background: #223040; color: #cdd9e6; border-color: #34506b; }
.chip.conn.online { background: #14352b; color: #8fe0be; }
.chip.conn.unknown { background: #202b39; color: var(--muted); }
.chip.sync { font-size: .82rem; }
.chip.sync.ok { background: #14352b; color: #8fe0be; border-color: #1f5a44; }
.chip.sync.pending { background: #3a2f14; color: #f0cf88; border-color: #6b571f; }
.chip.sync.busy { background: #1c3350; color: #9cc4f0; border-color: #2f5580; }

/* ---------- Buttons ---------- */
.btn {
  appearance: none; border: 1px solid var(--line); background: var(--panel-2); color: var(--text);
  padding: 10px 14px; border-radius: 10px; font-size: .95rem; font-weight: 600;
  cursor: pointer; min-height: var(--tap); display: inline-flex; align-items: center; justify-content: center;
  gap: 6px; text-decoration: none;
}
.btn:hover { border-color: var(--brand); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { padding: 6px 10px; min-height: 34px; font-size: .82rem; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .55; cursor: default; }
.btn.danger { border-color: var(--error); color: #ffd5d5; }
.btn.danger:hover { border-color: var(--error); background: var(--error); color: #fff; }

/* ---------- Layout ---------- */
.view { max-width: 900px; margin: 0 auto; padding: 14px; }
h1 { font-size: 1.5rem; margin: .2em 0; }
h2 { font-size: 1.25rem; margin: .2em 0 .4em; }
h3 { font-size: 1.05rem; margin: .2em 0 .4em; }
h4 { font-size: .95rem; margin: .2em 0; }
.muted { color: var(--muted); }
.mt { margin-top: 12px; }
.center { text-align: center; }
.grow { flex: 1 1 auto; min-width: 0; }

.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.spread { justify-content: space-between; }

.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; margin: 12px 0;
}
.card.error { border-color: var(--error); color: #ffd5d5; }
.card.scroll, .scroll { overflow-x: auto; }

/* ---------- Lists ---------- */
.list { display: flex; flex-direction: column; gap: 8px; }
.list-item, .li {
  display: flex; align-items: center; gap: 10px; padding: 12px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  text-decoration: none; color: var(--text);
}
.list-item:hover { border-color: var(--brand); }
.li-title { font-weight: 700; }
.li-sub { color: var(--muted); font-size: .82rem; }
.chev { color: var(--muted); font-size: 1.4rem; }

/* Per-record sync markers */
.dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; display: inline-block; }
.dot.pending { background: var(--pending); box-shadow: 0 0 0 3px rgba(224,161,58,.18); }
.dot.synced { background: #35506b; }

/* ---------- Tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; margin: 12px 0; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px; text-align: center; }
.tile-val { font-size: 1.5rem; font-weight: 800; color: var(--brand); }
.tile-label { color: var(--muted); font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }

.task ul.tasklist { list-style: none; margin: 6px 0 0; padding: 0; }
.tasklist li { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-top: 1px solid var(--line); }

/* ---------- Forms ---------- */
.field { display: flex; flex-direction: column; gap: 4px; margin: 8px 0; flex: 1 1 180px; }
.field-label { font-size: .82rem; color: var(--muted); font-weight: 600; }
.field.check { flex-direction: row; align-items: center; gap: 8px; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px; min-height: var(--tap);
  width: 100%;
}
input[type=checkbox] { width: 22px; height: 22px; min-height: 0; }
input[type=color] { padding: 4px; height: 44px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }

.inline-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); }
.inline-form input, .inline-form select { width: auto; flex: 1 1 130px; min-height: 40px; }

/* Inventory: distro meters + ways with per-phase channel links. */
.small { font-size: .82rem; }
.meters .meter-row:first-child { border-top: none; margin-top: 0; padding-top: 0; }
.way-block { margin-top: 10px; padding: 8px; border: 1px dashed var(--line); border-radius: 8px; }
.way-block .way-row { border-top: none; margin-top: 0; padding-top: 0; }
.links { margin-left: 14px; padding-left: 10px; border-left: 2px solid var(--line); }
.link-row { border-top: none; padding-top: 6px; margin-top: 6px; }
.link-phase { flex: 0 0 auto; min-width: 24px; }

.auth { max-width: 380px; margin: 8vh auto; }
.oauth { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; }
.divider { text-align: center; color: var(--muted); font-size: .8rem; margin: 12px 0 4px; }

/* ---------- Tables ---------- */
.table { border-collapse: collapse; width: 100%; font-size: .85rem; }
.table th, .table td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; white-space: nowrap; }
.table th { background: var(--panel-2); position: sticky; top: 0; }

/* ---------- Modal ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; padding: 14px; z-index: 40; }
.modal { max-width: 460px; width: 100%; max-height: 90vh; overflow: auto; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(30px);
  background: #0b1219; color: #fff; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 16px; opacity: 0; pointer-events: none; transition: .2s; z-index: 60; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.warn { border-color: var(--warn); }
.toast.error { border-color: var(--error); }

/* ---------- Wider screens (iPad/desktop) ---------- */
@media (min-width: 720px) {
  .nav { width: auto; border-top: none; margin-top: 0; padding-top: 0; }
  .status { margin-left: 0; }
}

/* Change-company picker overlay */
.ep-overlay{position:fixed;inset:0;background:rgba(0,0,0,.45);display:flex;align-items:center;justify-content:center;z-index:1000;padding:16px}
.ep-picker{max-width:360px;width:100%;max-height:80vh;overflow:auto}
.ep-picker .btn.block{display:block;width:100%;margin:6px 0;text-align:left}

/* Collapsible spanning-tree (event site model) */
.tree{padding:8px 6px}
.tree-node{margin:1px 0}
.tree-row{display:flex;align-items:center;gap:8px;padding:5px 6px;border-radius:8px}
.tree-row:hover{background:rgba(127,127,127,.08)}
.tree-kids{margin-left:14px;padding-left:12px;border-left:2px solid rgba(127,127,127,.22)}
.tree-tog{width:22px;height:22px;flex:0 0 22px;border:none;background:transparent;color:inherit;cursor:pointer;font-size:12px;border-radius:5px}
.tree-tog:hover{background:rgba(127,127,127,.18)}
.tree-tog.leaf{cursor:default;opacity:.4}
.tree-row .grow{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
.tree-site{font-weight:600}
.leafrow{padding-left:30px}
.tree-add{margin:4px 0 8px 30px;flex-wrap:wrap;gap:6px}
.tree-add input,.tree-add select{max-width:180px}
.btn.xs{padding:4px 9px;font-size:11px;line-height:1.4;min-height:32px}

/* In-app links (distro/source/circuit names, list rows) shouldn't look like raw
   blue hyperlinks — inherit the text colour, underline only on hover. Buttons
   and nav links keep their own styling. */
.view a:not(.btn):not(.navlink){color:inherit;text-decoration:none}
.view a:not(.btn):not(.navlink):hover{text-decoration:underline}
.view a.grow{cursor:pointer}
.view .chev{opacity:.4}

/* Wide pop-up (distro / source detail opened from anywhere) */
.modal-wide { max-width: 760px; }
.ep-modal .modal .row.spread[style*="sticky"] { background: var(--panel); padding-bottom: 6px; z-index: 1; }

/* Inventory Group → Type → Serials browser */
.inv-group { cursor: pointer; font-weight: 600; padding: 6px 2px; list-style: none; }
.inv-group::-webkit-details-marker { display: none; }
.inv-type { cursor: pointer; padding: 4px 2px; opacity: .92; list-style: none; }
.inv-type::-webkit-details-marker { display: none; }
details > summary::before { content: '▸ '; opacity: .6; }
details[open] > summary::before { content: '▾ '; opacity: .6; }

/* In-app print preview (power plan) */
.print-sheet { background: #fff; color: #0f172a; padding: 18px; border-radius: 8px; margin-top: 8px; }
.print-sheet .ps-head { border-bottom: 2px solid #0f7b6c; padding-bottom: 8px; margin-bottom: 14px; }
.print-sheet .ps-head h1 { margin: 0 0 4px; font-size: 20px; }
.print-sheet .ps-meta { color: #475569; font-size: 12px; }
.print-sheet .ps-diagram { width: 100%; overflow: auto; }
.print-sheet .ps-diagram svg { max-width: 100%; height: auto; }
@media print {
  body > *:not(.print-overlay) { display: none !important; }
  .print-overlay { position: static !important; inset: auto !important; background: #fff !important; padding: 0 !important; display: block !important; }
  .print-overlay .print-card { max-width: none !important; max-height: none !important; width: auto !important; box-shadow: none !important; border: 0 !important; overflow: visible !important; background: #fff !important; padding: 0 !important; }
  .print-overlay .no-print { display: none !important; }
  .print-sheet { padding: 0 !important; margin: 0 !important; }
  @page { margin: 12mm; }
}

/* Map pins (Leaflet DivIcons) — a circular badge with an emoji/glyph */
.ep-pin { background: none; border: 0; }
.ep-pin-dot { display: flex; align-items: center; justify-content: center; width: 26px; height: 26px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.45); font-size: 13px; line-height: 1; }
.ep-pin-dot.ep-you { background: #e11d48; color: #fff; font-size: 15px; }
.leaflet-host .leaflet-popup-content { margin: 10px 12px; }
.leaflet-host .leaflet-popup-content .btn { margin: 0; }

/* Circuit picker tree (searchable, collapsible) */
.cpick-sum { cursor: pointer; padding: 5px 2px; font-weight: 600; list-style: none; }
.cpick-sum::-webkit-details-marker { display: none; }
.cpick-row { display: block; width: 100%; text-align: left; background: transparent; border: 0; color: var(--text);
  padding: 6px 8px 6px 22px; border-radius: 8px; cursor: pointer; font: inherit; }
.cpick-row:hover { background: var(--panel-2); }
.cpick-row.hit { background: var(--hit); box-shadow: inset 2px 0 0 #f59e0b; }

/* ============================================================
   Responsive polish — iPhone / iPad / desktop
   ============================================================ */
/* Never let a stray wide child scroll the whole page sideways. */
html, body { overflow-x: hidden; max-width: 100%; }
.view { width: 100%; }
img, svg, canvas, video { max-width: 100%; height: auto; }
/* The power-plan schematic keeps its natural width and pans inside its own
   scroll container instead of shrinking to illegibility on a phone. */
.card.scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.card.scroll svg { max-width: none; height: auto; }

/* Cards/rows: comfortable on phones, roomier on larger screens. */
@media (max-width: 560px) {
  .view { padding: 12px 12px calc(16px + env(safe-area-inset-bottom)); }
  .card { padding: 12px; margin: 10px 0; }
  h2 { font-size: 1.15rem; }
  .row { gap: 8px; }
  /* Rows explicitly set to nowrap can overflow on tiny screens — let them scroll
     rather than push the layout off the edge. */
  .li > .row[style*="nowrap"], .row[style*="nowrap"] { overflow-x: auto; }
  /* Give buttons a touch more height for thumbs. */
  .btn.small { min-height: 36px; }
}
/* Tablet & up: a wider, calmer canvas. */
@media (min-width: 700px) {
  .view { max-width: 940px; padding: 18px; }
}
@media (min-width: 1100px) {
  .view { max-width: 1040px; }
}

/* Modals fit the screen on phones, centre on larger screens. */
/* Use dynamic viewport height so iOS toolbars don't push the modal's action
   buttons (Save/Cancel) off-screen. vh first as a fallback, dvh wins where supported. */
.modal { max-height: min(90vh, 720px); max-height: min(90dvh, 720px); overflow-y: auto; -webkit-overflow-scrolling: touch; }
@media (max-width: 560px) {
  .modal-backdrop { padding: 10px; align-items: flex-start; }
  .modal { max-height: calc(100vh - 20px); max-height: calc(100dvh - 20px); border-radius: 14px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
}

/* Tap targets: links inside lists shouldn't be cramped. */
.li a, .tasklist a { min-height: 28px; }

/* Tables always scroll inside their own container. */
.table-wrap, .card.scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { min-width: 100%; }

/* Light-mode nudges for elements with baked-in dark colours. */
@media (prefers-color-scheme: light) {
  .dot.synced { background: #9db4cc; }
  .chip.conn.unknown { background: var(--panel-2); color: var(--muted); }
  .modal-backdrop { background: rgba(15,32,46,.45); }
  .leaflet-host { border: 1px solid var(--line); }
}
.tm-sep { height: 1px; background: var(--line); margin: 4px 0; }

/* Deep tree eats horizontal space on phones — shrink the per-level indent so
   the actual content gets the width. Also trims the event distro pop-up's
   inline 34px indents (see .ind34 helper applied in JS). */
@media (max-width: 620px) {
  .tree { padding: 6px 2px; }
  .tree-row { padding: 4px 2px; gap: 5px; }
  /* Near-zero per-level indent: just a thin guide line so deep chains keep
     the width for their content instead of marching off the right edge. */
  .tree-kids { margin-left: 0; padding-left: 6px; border-left-width: 1px; }
  .tree-tog { width: 18px; height: 18px; flex-basis: 18px; font-size: 11px; }
  .leafrow { padding-left: 6px; }
  .tree-add { margin-left: 4px; }
  .tree-add input, .tree-add select { max-width: 150px; }
  /* Row labels can use the reclaimed width. */
  .tree-row .grow { min-width: 0; }
}
.ind34 { margin-left: 34px; }
@media (max-width: 620px) { .ind34 { margin-left: 12px; } }
/* On phones, hide the inline circuit-label editor on the site model (the label
   still shows in the row; edit it from the distro instead). */
@media (max-width: 620px) { .label-edit { display: none; } }
/* Brief highlight when a distro is opened focused on one circuit (from the plan). */
.focus-flash { animation: focusFlash 2.2s ease-out 1; border-radius: 8px; }
@keyframes focusFlash { 0%,60% { background: rgba(15,123,108,.22); } 100% { background: transparent; } }

/* Per-event circuit LABEL shown inline in the distro circuit title (after the
   circuit id). Amber, to match the power plan. */
.circ-load { color: #b45309; font-weight: 600; }
@media (prefers-color-scheme: dark) { .circ-load { color: #fbbf24; } }

/* Traders page: compact tappable list rows (whole row is a button). */
.trader-row { width: 100%; text-align: left; cursor: pointer; }
.trader-row:hover { border-color: var(--brand); }
.trader-row .li-title { font-weight: 600; }

/* Trader detail pop-up: the richer management controls live in .trader-adv,
   revealed by a "More details" toggle (collapsed by default on phones). */
.trader-adv { margin-top: 4px; }

/* Searchable combo box (typeahead): text input + filtered dropdown. Used for
   picking a customer (with add-new) and an equipment type (existing only).
   The menu is IN-FLOW (not absolutely positioned) so it can never be clipped
   by a scrolling modal — it pushes the rest of the form down and the modal
   scrolls to it. Reliable on phones. */
.combo { position: relative; }
.combo-menu {
  margin-top: 4px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  max-height: 244px; overflow-y: auto; box-shadow: 0 10px 26px rgba(0,0,0,.20);
}
.combo-item {
  display: block; width: 100%; text-align: left; padding: 10px 12px;
  background: transparent; border: none; border-bottom: 1px solid var(--line);
  cursor: pointer; color: var(--text); min-height: 42px; font: inherit;
}
.combo-item:last-child { border-bottom: none; }
.combo-item:hover, .combo-item:focus { background: var(--panel-2); }
.combo-new { color: var(--brand); font-weight: 600; }
.combo-empty { padding: 10px 12px; }
