:root {
  --bg: #f2f4f7; --panel: #ffffff; --ink: #19212c; --mute: #5f6b7c; --faint: #8a94a3; --line: #dadfe6; --soft: #eaedf2;
  --act: #2f55d4; --act-ink: #ffffff; --on: #139a57; --busy: #c98500; --off: #cf3b2e; --idle: #9aa3b1;
  --on-bg: #e3f4ea; --busy-bg: #fbf0d6; --off-bg: #fbe5e2;
  --side: #19212c; --side-ink: #c9d1dc; --side-act: #ffffff;
  --radius: 10px; --shadow: 0 1px 2px rgba(25, 33, 44, .06), 0 4px 16px rgba(25, 33, 44, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161c; --panel: #1a1f27; --ink: #e5e9ef; --mute: #9aa4b2; --faint: #6f7987; --line: #2b323d; --soft: #222833;
    --act: #6b8cf5; --act-ink: #0d1220; --on: #3fc07f; --busy: #e2a53a; --off: #f06b5f; --idle: #5d6674;
    --on-bg: #17301f; --busy-bg: #33280f; --off-bg: #3a1c19;
    --side: #0e1116; --side-ink: #9aa4b2; --side-act: #ffffff;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--ink); font-variant-numeric: tabular-nums; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--act); outline-offset: 2px; }
a { color: var(--act); text-decoration: none; }
a:hover { text-decoration: underline; }
a.btn { display: inline-block; color: inherit; }
a.btn.primary { color: var(--act-ink); }
a.btn:hover { text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 16px; font-weight: 650; }
h3 { font-size: 14px; font-weight: 650; }
.mute { color: var(--mute); }
.faint { color: var(--faint); }
.small { font-size: 12.5px; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12.5px; }
.err { color: var(--off); }
.hidden { display: none !important; }

/* buttons and fields */
.btn, button { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 6px 12px; cursor: pointer; line-height: 1.3; }
button:hover { border-color: var(--act); }
button:disabled { opacity: .5; cursor: default; }
.primary { background: var(--act); border-color: var(--act); color: var(--act-ink); font-weight: 600; }
.primary:hover { filter: brightness(1.08); }
.danger { color: var(--off); }
.danger.solid { background: var(--off); border-color: var(--off); color: #fff; }
.ghost { border-color: transparent; background: transparent; }
.ghost:hover { background: var(--soft); border-color: transparent; }
input, select, textarea { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; }
textarea { width: 100%; min-height: 72px; resize: vertical; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
label.field { display: grid; gap: 4px; font-size: 13px; color: var(--mute); }
label.field > input, label.field > select, label.field > textarea { color: var(--ink); width: 100%; min-width: 0; }
.fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }

/* app frame */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; background: linear-gradient(to right, var(--side) 220px, transparent 220px); }
.side { background: var(--side); color: var(--side-ink); padding: 18px 12px; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 4px; }
.brand { display: flex; align-items: center; gap: 10px; color: var(--side-act); font-weight: 650; padding: 4px 10px 18px; }
.brand svg { flex: none; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; color: var(--side-ink); text-decoration: none; }
.nav a svg { flex: none; opacity: .8; }
.nav a:hover { background: rgba(255, 255, 255, .06); color: var(--side-act); }
.nav a.on { background: rgba(255, 255, 255, .1); color: var(--side-act); }
.nav a .count { margin-left: auto; font-size: 12px; background: var(--off); color: #fff; border-radius: 999px; padding: 0 7px; }
.side .foot { margin-top: auto; font-size: 12px; padding: 0 10px; display: grid; gap: 8px; }
.side .foot button { background: transparent; color: var(--side-ink); border-color: rgba(255, 255, 255, .15); }
.main { padding: 26px 32px 60px; min-width: 0; }
.page { max-width: 1200px; }
.page-head { display: flex; align-items: flex-end; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--mute); }

/* panels */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 18px; }
.panel > h2 { margin-bottom: 12px; display: flex; align-items: center; gap: 10px; }
.panel > h2 .grow { flex: 1; }
.panel > h2 button, .panel > h2 .btn { font-size: 14px; font-weight: 600; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 18px; }

/* status */
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--idle); display: inline-block; flex: none; }
.dot.online { background: var(--on); box-shadow: 0 0 0 3px var(--on-bg); }
.dot.busy { background: var(--busy); box-shadow: 0 0 0 3px var(--busy-bg); }
.dot.offline { background: var(--off); box-shadow: 0 0 0 3px var(--off-bg); }
.pill { display: inline-flex; align-items: center; gap: 6px; border-radius: 999px; padding: 1px 9px; font-size: 12.5px; background: var(--soft); color: var(--mute); white-space: nowrap; }
.pill.online { background: var(--on-bg); color: var(--on); }
.pill.busy { background: var(--busy-bg); color: var(--busy); }
.pill.offline { background: var(--off-bg); color: var(--off); }
.tag { display: inline-block; border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; font-size: 12px; color: var(--mute); margin: 0 4px 4px 0; }

/* overview */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: grid; gap: 2px; }
.stat b { font-size: 26px; font-weight: 650; line-height: 1.1; }
.stat .bar { height: 6px; background: var(--soft); border-radius: 3px; overflow: hidden; margin-top: 8px; display: flex; }
.stat .bar i { display: block; height: 100%; }
.alerts { display: grid; gap: 8px; }
.alert { display: flex; gap: 12px; align-items: center; padding: 10px 12px; border-radius: 8px; background: var(--soft); }
.alert.offline { background: var(--off-bg); }
.alert.busy { background: var(--busy-bg); }
.alert .grow { min-width: 0; }

/* the shelf: each device drawn as the phone it is */
.filters { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 16px; align-items: center; }
.chip { border-radius: 999px; padding: 4px 12px; font-size: 13px; }
.chip.on { background: var(--ink); color: var(--panel); border-color: var(--ink); }
.shelf-group + .shelf-group { margin-top: 30px; }
.shelf-head { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.live-group { font-size: 12.5px; color: var(--mute); font-weight: 600; margin: 8px 2px 2px; }
.live-group:first-child { margin-top: 0; }
.shelf { display: grid; grid-template-columns: repeat(auto-fill, minmax(176px, 1fr)); gap: 18px 16px; }
.phone { position: relative; text-align: left; background: var(--panel); border: 2px solid var(--line); border-radius: 26px; padding: 26px 14px 14px; min-height: 236px;
  display: flex; flex-direction: column; gap: 6px; cursor: pointer; box-shadow: var(--shadow); transition: border-color .15s, transform .15s; color: var(--ink); }
.phone .tags { display: flex; flex-wrap: wrap; }
.phone:hover { transform: translateY(-2px); border-color: var(--act); }
.phone.sel { border-color: var(--act); }
.phone.emulator { border-style: dashed; }
.phone .notch { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); width: 46px; height: 8px; border-radius: 4px; background: var(--soft); display: flex; justify-content: center; align-items: center; }
.phone .notch .dot { width: 7px; height: 7px; box-shadow: none; }
.phone .name { font-weight: 650; font-size: 15px; line-height: 1.25; }
.phone .spec { color: var(--mute); font-size: 12.5px; }
.phone .screen { margin-top: auto; border-radius: 12px; background: var(--soft); padding: 10px; display: grid; gap: 4px; font-size: 12.5px; }
.phone .screen.online { background: var(--on-bg); }
.phone .screen.busy { background: var(--busy-bg); }
.phone .screen.offline { background: var(--off-bg); }
.phone .who { font-weight: 600; overflow-wrap: anywhere; }
.via { display: inline-flex; align-items: center; gap: 4px; border-radius: 999px; padding: 0 8px 0 6px; font-size: 12px; font-weight: 600; line-height: 20px; }
.via.wifi { background: #e6edfd; color: #2f55d4; }
.via.usb { background: #efe8fb; color: #6d3fc0; }
@media (prefers-color-scheme: dark) { .via.wifi { background: #1d2a4d; color: #8fa9f5; } .via.usb { background: #2c2142; color: #b796f0; } }
.batt { display: inline-flex; align-items: center; gap: 4px; }
.batt i { display: inline-block; width: 22px; height: 10px; border: 1.5px solid currentColor; border-radius: 3px; position: relative; }
.batt i::after { content: ""; position: absolute; left: 1px; top: 1px; bottom: 1px; width: var(--lvl); background: currentColor; border-radius: 1px; }
.batt.low { color: var(--off); }

/* tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-weight: 600; font-size: 12.5px; color: var(--mute); }
tr:last-child td { border-bottom: none; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--soft); }
.empty { padding: 26px; text-align: center; color: var(--mute); }
.empty p { margin: 0 0 12px; }
.scroll { overflow-x: auto; }
.quota { display: flex; align-items: center; gap: 10px; min-width: 150px; }
.quota .bar { flex: 1; height: 6px; border-radius: 3px; background: var(--soft); overflow: hidden; }
.quota .bar i { display: block; height: 100%; background: var(--act); }
.quota .bar i.full { background: var(--busy); }

/* device rows */
.devrows { display: grid; gap: 6px; }
.devrow { display: grid; grid-template-columns: minmax(120px, 0.9fr) 150px 36px 80px 130px minmax(200px, 2.2fr) 70px; gap: 14px; align-items: center;
  padding: 10px 14px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; cursor: pointer; }
.devrow:hover { border-color: var(--act); }
.devrow.sel { border-color: var(--act); box-shadow: inset 3px 0 0 var(--act); }
.devrow.head { background: transparent; border: none; padding: 0 14px; cursor: default; font-size: 12px; color: var(--mute); font-weight: 600; }
.devrow > span { min-width: 0; }
.dr-name { display: flex; align-items: center; gap: 10px; }
.dr-name > span:last-child { display: grid; line-height: 1.3; min-width: 0; }
.dr-name b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dr-who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dr-act { text-align: right; }
.via.emu { background: var(--soft); color: var(--mute); }
.via-ico { display: inline-grid; place-items: center; width: 30px; height: 26px; border-radius: 8px; }
.via-ico.wifi { background: #e6edfd; color: #2f55d4; }
.via-ico.usb { background: #efe8fb; color: #6d3fc0; }
.via-ico.emu, .via-ico.off { background: var(--soft); color: var(--faint); }
@media (prefers-color-scheme: dark) { .via-ico.wifi { background: #1d2a4d; color: #8fa9f5; } .via-ico.usb { background: #2c2142; color: #b796f0; } }
.dr-name b { display: block; }

/* drawer */
.scrim { position: fixed; inset: 0; background: rgba(15, 20, 28, .35); z-index: 20; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(440px, 100vw); background: var(--panel); border-left: 1px solid var(--line); z-index: 21;
  overflow-y: auto; padding: 22px; box-shadow: -8px 0 30px rgba(0, 0, 0, .15); animation: slide .18s ease-out; }
.drawer.wide { width: auto; max-width: 100vw; }
.drawer-body { display: flex; gap: 20px; align-items: flex-start; }
.drawer-info { width: 380px; flex: none; }
.drawer-live { flex: none; background: #0d1015; border-radius: 16px; display: flex; min-width: 300px; overflow: hidden; }
.drawer-live .empty { color: #9aa4b2; margin: auto; padding: 120px 40px; }
.drawer-live .dv { --screen-h: calc(100vh - 72px); }
body.drawer-open .main { padding-right: 470px; }
/* beside an open drawer a row keeps what tells devices apart: name, link, state, holder, View */
body.drawer-open .devrow { grid-template-columns: minmax(110px, 1fr) 36px 116px minmax(130px, 1.6fr) 62px; }
body.drawer-open .dr-os, body.drawer-open .dr-batt, body.drawer-open .devrow.head > span:nth-child(2), body.drawer-open .devrow.head > span:nth-child(4) { display: none; }
@keyframes slide { from { transform: translateX(24px); opacity: 0; } to { transform: none; opacity: 1; } }
.drawer h1 { font-size: 20px; }
.drawer section { border-top: 1px solid var(--line); padding: 16px 0; }
.drawer section:first-of-type { border-top: none; }
.drawer section > h3 { margin-bottom: 10px; }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 6px 12px; font-size: 13.5px; }
.kv dt { color: var(--mute); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.access-row { display: flex; align-items: center; gap: 10px; padding: 6px 0; }
.access-row .grow { min-width: 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: none; border-radius: 0; padding: 4px 10px; font-size: 12.5px; background: var(--panel); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--ink); color: var(--panel); }
.tags-edit { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.tags-edit .tag { display: inline-flex; align-items: center; gap: 4px; margin: 0; padding: 2px 4px 2px 8px; }
.tags-edit .tag button { border: none; padding: 0 4px; background: transparent; color: var(--faint); }
.tags-edit input { width: 130px; padding: 3px 8px; }

/* modal and toast */
.modal { position: fixed; inset: 0; display: grid; place-items: center; z-index: 30; background: rgba(15, 20, 28, .45); padding: 16px; }
.modal .box { background: var(--panel); border-radius: 14px; width: min(560px, 100%); max-height: 90vh; overflow-y: auto; padding: 22px; display: grid; gap: 14px; box-shadow: 0 20px 60px rgba(0, 0, 0, .3); }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; }
.picklist { border: 1px solid var(--line); border-radius: 8px; max-height: 300px; overflow-y: auto; }
.picklist label { display: flex; gap: 10px; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--line); cursor: pointer; }
.picklist label:last-child { border-bottom: none; }
.picklist label:hover { background: var(--soft); }
.token { font-family: ui-monospace, Menlo, monospace; background: var(--soft); border-radius: 8px; padding: 10px 12px; word-break: break-all; font-size: 13px; }
.toasts { position: fixed; bottom: 18px; right: 18px; display: grid; gap: 8px; z-index: 40; }
.toast { background: var(--ink); color: var(--panel); border-radius: 8px; padding: 10px 14px; box-shadow: var(--shadow); max-width: 380px; animation: slide .18s ease-out; }
.toast.bad { background: var(--off); color: #fff; }

/* steps on Add device */
.how { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 18px; }
.steps { margin: 0 0 12px; padding-left: 20px; color: var(--mute); }
.steps li { margin-bottom: 4px; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; width: min(380px, 100%); display: grid; gap: 14px; box-shadow: var(--shadow); }

/* live view: the panel is as wide as the device, not the page */
.live { display: grid; grid-template-columns: 220px auto; gap: 18px; align-items: start; justify-content: start; }
.live-list { display: grid; gap: 6px; }
.live-list button { display: flex; gap: 10px; align-items: center; text-align: left; padding: 10px 12px; }
.live-list button.on { border-color: var(--act); background: var(--soft); }
.live-panel { background: #0d1015; border-radius: 16px; overflow: hidden; display: flex; min-width: 300px; }
.live-panel .empty { color: #9aa4b2; margin: auto; padding: 120px 40px; }

/* the viewer component, in the Live panel and on view.html: an emulator window. The screen box has a
   fixed height and the device's aspect ratio, so it has its size before the first frame arrives. */
.dv { display: flex; color: #e5e9ef; background: #0d1015; --screen-h: calc(100vh - 150px); }
.dv button { background: #1e242d; color: #e5e9ef; border-color: #333b47; }
.dv [hidden] { display: none !important; }
.dv-body { display: flex; align-items: stretch; }
.dv-stage { position: relative; flex: none; padding: 14px 4px 14px 14px; }
.dv-stage:focus { outline: none; }
.dv-screenbox { --ar: 9 / 19.5; position: relative; height: var(--screen-h); min-height: 420px; aspect-ratio: var(--ar); max-width: 70vw;
  background: #000; border-radius: 16px; overflow: hidden; box-shadow: 0 0 0 1px #2a313b; }
.dv-screenbox > canvas, .dv-screenbox > video, .dv-screenbox > img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; touch-action: none; }
.dv-poster { filter: saturate(.6) brightness(.75); }
.dv-stage .ctl { box-shadow: inset 0 0 0 2px var(--busy); cursor: crosshair; }
.dv-side { flex: none; width: 132px; display: flex; flex-direction: column; gap: 12px; padding: 14px 12px 14px 10px; }
.dv-info { display: grid; gap: 2px; }
.dv-title { font-weight: 650; line-height: 1.25; overflow-wrap: anywhere; }
.dv-status { color: #9aa4b2; font-size: 12px; line-height: 1.35; }
.dv-acts { display: flex; gap: 6px; }
.dv-acts .dv-take, .dv-acts .dv-release { flex: 1; padding: 7px 6px; font-size: 13px; }
.dv-acts .dv-take { background: var(--act); border-color: var(--act); color: var(--act-ink); font-weight: 600; }
.dv-acts .dv-icon { width: 34px; height: auto; }
.dv-tools { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px; margin-top: auto; margin-bottom: auto; }
.dv-icon { height: 40px; padding: 0; display: grid; place-items: center; border-radius: 10px; }
.dv-icon:not(:disabled):hover { border-color: var(--act); background: #262d38; }
.dv-icon:disabled { opacity: .35; }
.dv-sep { grid-column: 1 / -1; height: 1px; background: #2a313b; margin: 2px 4px; }
.dv-type { position: absolute; left: 10px; right: 10px; bottom: 12px; display: flex; }
.dv-type input { flex: 1; background: rgba(13, 16, 21, .92); border-color: #333b47; color: #e5e9ef; padding: 9px 12px; }
.dv-note { position: absolute; left: 10px; right: 10px; bottom: 16px; text-align: center; color: #c9d1dc; font-size: 13px; pointer-events: none; text-shadow: 0 1px 3px #000; }
.dv-overlay { position: absolute; inset: 0; background: rgba(0, 0, 0, .7); display: flex; align-items: center; justify-content: center; flex-direction: column; gap: 12px; text-align: center; padding: 16px; }
body.viewer-page { background: #0d1015; }
body.viewer-page #viewer { min-height: 100vh; display: flex; justify-content: center; align-items: flex-start; }
body.viewer-page .dv { --screen-h: calc(100vh - 28px); }

/* narrow screens: the sidebar becomes a bottom bar */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .side { position: fixed; bottom: 0; top: auto; left: 0; right: 0; height: auto; flex-direction: row; padding: 6px; z-index: 15; overflow-x: auto; }
  .brand, .side .foot { display: none; }
  .nav { display: flex; gap: 2px; width: 100%; }
  .nav a { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 8px; flex: 1; justify-content: center; }
  .nav a .count { position: absolute; margin: 0; transform: translate(14px, -4px); }
  .main { padding: 18px 16px 90px; }
  .live { grid-template-columns: 1fr; }
  body.drawer-open .main { padding-right: 16px; }
  .drawer.wide { width: 100vw; }
  .drawer-body { flex-direction: column; align-items: stretch; }
  .drawer-info { width: auto; }
  .drawer-live .dv { --screen-h: 60vh; }
  .devrow { grid-template-columns: 1fr auto; gap: 6px 10px; }
  .devrow.head, .dr-os, .dr-batt, .dr-who { display: none; }
  .dr-link { grid-column: 1; } .dr-state { grid-column: 2; grid-row: 1; } .dr-act { grid-column: 2; }
  .live-panel { justify-content: center; }
  .dv { --screen-h: 62vh; }
  .dv-body { flex-direction: column; align-items: center; }
  .dv-stage { padding: 12px 12px 0; }
  .dv-screenbox { max-width: calc(100vw - 56px); }
  .dv-side { width: auto; align-self: stretch; }
  .dv-tools { grid-template-columns: repeat(5, 1fr); }
  .dv-sep { display: none; }
  .app { background: none; }
  .shelf { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .phone { min-height: 200px; padding: 24px 10px 10px; border-radius: 22px; }
  .phone .tags { display: none; }
  .kv { grid-template-columns: 100px 1fr; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
