:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --border: #dde2ea;
  --text: #18202b;
  --muted: #5f6b7a;
  --accent: #2563eb;
  --accent-contrast: #ffffff;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 4px 16px rgb(16 24 40 / 6%);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #182029;
    --surface-2: #212b36;
    --border: #2c3846;
    --text: #e6ebf1;
    --muted: #9aa7b5;
    --accent: #4b8bff;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5em; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hidden { display: none !important; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.stack > * + * { margin-top: .75rem; }

/* ---------- controls */
button, .btn {
  font: inherit;
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .55rem 1rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
button:hover, .btn:hover { background: var(--surface-2); }
button:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-danger { color: var(--danger); }
.btn-danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-sm { padding: .3rem .65rem; font-size: .85rem; }
button.active, .btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

label { display: block; font-weight: 500; font-size: .9rem; margin-bottom: .25rem; }
input, select, textarea {
  font: inherit;
  width: 100%;
  padding: .55rem .7rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
input:disabled, select:disabled, textarea:disabled { opacity: .6; cursor: not-allowed; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.field + .field { margin-top: .85rem; }
label.checkbox { display: flex; align-items: center; gap: .4rem; font-weight: 400; margin: .4rem 0 0; cursor: pointer; }
label.checkbox input { width: auto; margin: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
}
.error { color: var(--danger); font-size: .9rem; min-height: 1.3em; }

.badge {
  display: inline-block; padding: .1rem .55rem; border-radius: 999px;
  font-size: .78rem; font-weight: 600; text-transform: capitalize;
  background: var(--surface-2); color: var(--muted);
}
.badge.waiting { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.badge.active { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }

/* ---------- layout */
.topbar {
  display: flex; align-items: center; gap: 1rem;
  padding: .75rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.1rem; display: flex; align-items: center; gap: .5rem; text-decoration: none; color: var(--text); }
/* Signed-in user's name and icon; opens the Profile / Sign out menu. */
.user-menu { position: relative; }
.user-menu-btn { padding: .25rem .3rem .25rem .6rem; gap: .5rem; border-color: transparent; background: transparent; color: var(--muted); font-size: .85rem; }
.user-menu-btn:hover, .user-menu-btn[aria-expanded="true"] { background: var(--surface-2); color: var(--text); }
.avatar { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: var(--surface-2); color: var(--muted); }
.avatar .icon { width: 18px; height: 18px; }
.menu {
  position: absolute; right: 0; top: calc(100% + .35rem); z-index: 50; min-width: 160px;
  display: flex; flex-direction: column; padding: .3rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.menu > a, .menu > button {
  display: block; width: 100%; text-align: left; padding: .5rem .7rem; border: none; border-radius: 6px;
  background: transparent; color: var(--text); text-decoration: none; font-size: .9rem;
}
.menu > a:hover, .menu > button:hover, .menu > :focus-visible { background: var(--surface-2); }
.menu > a[aria-current="page"] { font-weight: 600; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); }
.container { max-width: 1100px; margin: 0 auto; padding: 1.5rem 1rem 3rem; }
.grid-2 { display: grid; grid-template-columns: 340px 1fr; gap: 1.25rem; align-items: start; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }
.field-pair { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }

.center-page { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.center-page .card { width: 100%; max-width: 400px; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .6rem .5rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: none; }
.table-wrap { overflow-x: auto; }

.link-box {
  display: flex; gap: .5rem; align-items: center;
  background: var(--surface-2); border-radius: 8px; padding: .5rem .5rem .5rem .75rem;
}
.link-box code { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .85rem; }

/* ---------- profile */
.container.narrow { max-width: 760px; }
.details { display: grid; grid-template-columns: max-content 1fr; gap: .4rem 1.25rem; margin: 0; }
.details dt { color: var(--muted); font-size: .9rem; }
.details dd { margin: 0; overflow-wrap: anywhere; }
.method { padding-top: 1rem; }
.method + .method { margin-top: 1rem; border-top: 1px solid var(--border); }
.method h3 { font-size: 1rem; margin: 0; display: flex; align-items: center; gap: .5rem; }
.method h3 + p { margin: .25rem 0 0; }
/* ---------- admin */
.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: .85rem 1.25rem; }
.hint { margin: .3rem 0 0; }
.notice { border-left: 4px solid var(--warn); }
.hint[data-default]::after { content: " " attr(data-default); }
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }

.totp-setup { display: grid; grid-template-columns: 180px 1fr; gap: 1.25rem; align-items: start; margin-top: 1rem; }
@media (max-width: 560px) { .totp-setup { grid-template-columns: 1fr; } }
/* QR codes need dark-on-light to scan, whatever the theme. */
.qr { background: #fff; padding: 10px; border-radius: 8px; border: 1px solid var(--border); width: 180px; }
.qr-large { width: 260px; max-width: 100%; margin-left: auto; margin-right: auto; }
.qr svg { display: block; width: 100%; height: auto; }
.secret { display: block; background: var(--surface-2); border-radius: 8px; padding: .5rem .75rem; font-size: .9rem; letter-spacing: .05em; overflow-wrap: anywhere; }
.code-input { font-size: 1.25rem; letter-spacing: .3em; font-variant-numeric: tabular-nums; max-width: 12ch; }

dialog {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  padding: 1.25rem; width: min(420px, 92vw);
}
dialog::backdrop { background: rgb(0 0 0 / 45%); }
dialog.wide { width: min(720px, 94vw); }
.subhead { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: .5rem 0 0; }
.dialog-section { border-top: 1px solid var(--border); padding-top: .9rem; }
.dialog-section h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 0 0 .3rem; }
.dropzone {
  display: flex; flex-direction: column; align-items: center; gap: .35rem;
  padding: 2rem 1rem; text-align: center; cursor: pointer; font-weight: 400; margin: 0;
  border: 2px dashed var(--border); border-radius: var(--radius); background: var(--surface-2);
}
.dropzone.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--surface-2)); }
.link { color: var(--accent); text-decoration: underline; }
.import-table { max-height: 45vh; overflow-y: auto; margin-top: .5rem; }
.import-table td { font-size: .85rem; }
.import-table .ok { color: var(--ok); }
.import-table .fail { color: var(--danger); }

#toast {
  position: fixed; left: 50%; bottom: 5.5rem; transform: translateX(-50%);
  background: #18202b; color: #fff; padding: .6rem 1rem; border-radius: 8px;
  font-size: .9rem; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 100;
}
#toast.show { opacity: 1; }

/* ---------- video stage (viewer + onsite) */
.stage {
  position: relative;
  background: #000;
  overflow: hidden;
  display: grid; place-items: center;
}
/* Pinned to the stage so object-fit: contain letterboxes the whole picture. Percentage
   heights don't resolve inside the grid, which let portrait video overflow and get cropped. */
.stage video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; display: block; }
.stage-overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: #cfd8e3; text-align: center; padding: 1rem;
  pointer-events: none;
}
.pointer {
  position: absolute; width: 44px; height: 44px; margin: -22px 0 0 -22px;
  border-radius: 50%; border: 3px solid #ff3b30; box-shadow: 0 0 0 3px rgb(255 255 255 / 70%);
  pointer-events: none; animation: pulse 1s ease-out 3; opacity: 0; transition: opacity .3s;
}
.pointer.show { opacity: 1; }
@keyframes pulse {
  0% { transform: scale(.6); }
  50% { transform: scale(1.15); }
  100% { transform: scale(1); }
}
.stats {
  position: absolute; left: .75rem; top: .75rem;
  background: rgb(0 0 0 / 55%); color: #fff; font-size: .78rem;
  padding: .25rem .55rem; border-radius: 6px; font-variant-numeric: tabular-nums;
}
.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid rgb(255 255 255 / 25%); border-top-color: #fff;
  animation: spin 1s linear infinite; margin: 0 auto .75rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- agent viewer */
.viewer { display: grid; grid-template-columns: 1fr 600px; height: calc(100vh - 57px); }
.viewer .stage { height: 100%; cursor: crosshair; }
.viewer aside { border-left: 1px solid var(--border); background: var(--surface); overflow-y: auto; padding: 1rem; }
.viewer aside > :not(.hidden) ~ :not(.hidden) { margin-top: 1.25rem; padding-top: 1.25rem; border-top: 1px solid var(--border); }
.viewer aside h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.panel > summary { display: flex; align-items: center; gap: .5rem; cursor: pointer; list-style: none; }
.panel > summary::-webkit-details-marker { display: none; }
.panel > summary h3 { margin: 0; }
.panel > summary::after {
  content: ""; width: .45rem; height: .45rem; margin: 0 .2rem 0 auto;
  border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted);
  transform: rotate(45deg); transition: transform .15s;
}
.panel:not([open]) > summary::after { transform: rotate(-45deg); }
.panel[open] > summary { margin-bottom: .75rem; }
.subpanel { background: var(--surface-2); border-radius: 8px; padding: .75rem; }
.subpanel h4 { margin: 0 0 .6rem; font-size: .85rem; }
.subpanel button { width: 100%; }
.subpanel .row button { width: auto; }
.badge.unread { background: var(--accent); color: #fff; text-transform: none; }

/* ---------- chat (agent panel + onsite drawer) */
.chat-log {
  display: flex; flex-direction: column; gap: .5rem;
  height: 240px; overflow-y: auto; padding: .6rem;
  background: var(--surface-2); border-radius: 8px;
}
.chat-log:empty::before { content: "No messages yet."; color: var(--muted); font-size: .85rem; margin: auto; }
.chat-msg { max-width: 85%; align-self: flex-start; }
.chat-msg.mine { align-self: flex-end; text-align: right; }
.chat-text {
  display: inline-block; text-align: left; padding: .4rem .65rem; border-radius: 10px;
  background: var(--surface); border: 1px solid var(--border);
  white-space: pre-wrap; overflow-wrap: anywhere; user-select: text;
}
.chat-msg.mine .chat-text { background: var(--accent); border-color: var(--accent); color: #fff; }
.chat-meta { font-size: .72rem; color: var(--muted); margin-top: .15rem; }
.chat-form { display: flex; gap: .5rem; margin-top: .5rem; }
.chat-form input { flex: 1; min-width: 0; }
.toolbar {
  position: absolute; bottom: 1rem; left: 50%; transform: translateX(-50%);
  display: flex; gap: .5rem; background: rgb(15 20 25 / 75%); padding: .5rem; border-radius: 12px;
  backdrop-filter: blur(6px);
}
.toolbar button { background: transparent; border-color: rgb(255 255 255 / 20%); color: #fff; }
.toolbar button:hover { background: rgb(255 255 255 / 12%); }
.toolbar button.active { background: var(--accent); border-color: var(--accent); }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: .5rem; }
.snap { position: relative; margin: 0; min-width: 0; }
.gallery a { display: block; aspect-ratio: 4 / 3; border-radius: 6px; overflow: hidden; background: var(--surface-2); }
.snap-delete {
  position: absolute; top: 4px; right: 4px;
  padding: 0 .4rem; min-height: 0; font-size: 1.1rem; line-height: 1.4;
  background: rgb(0 0 0 / 55%); color: #fff; border: none;
}
.snap-delete:hover { background: var(--danger); }
.snap-name { margin-top: .3rem; padding: .25rem .45rem; font-size: .8rem; }
.gallery img { width: 100%; height: 100%; object-fit: cover; display: block; }
@media (max-width: 1100px) {
  .viewer { grid-template-columns: 1fr; height: auto; }
  .viewer .stage { height: 60vh; }
  .viewer aside { border-left: none; }
}

/* ---------- onsite */
.onsite { height: 100dvh; display: flex; flex-direction: column; background: #000; color: #fff; }
.onsite .stage { flex: 1; }
.onsite-bar {
  display: flex; align-items: center; gap: .5rem; padding: .6rem .75rem;
  padding-bottom: max(.6rem, env(safe-area-inset-bottom));
  background: #111820;
}
.onsite-bar button { background: #1f2a36; border-color: #2c3846; color: #fff; padding: .7rem; min-width: 48px; }
.onsite-bar button.off { background: #4a1d1d; border-color: #6b2525; }
.onsite-bar button.active { background: #b45309; border-color: #b45309; }
.onsite-request {
  display: flex; align-items: center; gap: .5rem; flex-wrap: wrap;
  padding: .6rem .9rem; background: #1f2a36; border-top: 1px solid #2c3846; font-size: .9rem;
}
.onsite-request span { flex: 1; min-width: 12rem; }
.onsite-request button:not(.btn-primary) { background: transparent; border-color: #2c3846; color: #fff; }
.onsite-status { flex: 1; font-size: .85rem; color: #cfd8e3; display: flex; align-items: center; gap: .45rem; min-width: 0; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--warn); flex: none; }
.dot.live { background: #ef4444; box-shadow: 0 0 0 3px rgb(239 68 68 / 30%); }
.icon { width: 20px; height: 20px; flex: none; }
.btn-icon { padding: .4rem .55rem; }
.btn-icon .icon { width: 18px; height: 18px; }

/* ---------- onsite chat drawer */
.chat-tab {
  position: absolute; right: 0; top: 50%; transform: translateY(-50%); z-index: 5;
  padding: 1rem .35rem; min-width: 0; border-radius: 10px 0 0 10px; border-right: none;
  background: rgb(15 20 25 / 75%); border-color: rgb(255 255 255 / 25%); color: #fff;
  backdrop-filter: blur(6px);
}
.chat-tab:hover { background: rgb(15 20 25 / 90%); }
.chat-tab-count {
  position: absolute; top: -.5rem; left: -.5rem; min-width: 1.3rem; height: 1.3rem; padding: 0 .3rem;
  border-radius: 999px; background: #ef4444; color: #fff; font-size: .75rem; font-weight: 700;
  display: grid; place-items: center;
}
.chat-drawer {
  position: absolute; top: 0; right: 0; bottom: 0; z-index: 6;
  width: min(360px, 88vw); display: flex; flex-direction: column; gap: .5rem; padding: .75rem;
  background: #111820; border-left: 1px solid #2c3846;
  transform: translateX(100%); transition: transform .2s ease, visibility .2s; visibility: hidden;
  --surface: #1f2a36; --surface-2: #0b1015; --border: #2c3846; --text: #e6ebf1; --muted: #9aa7b5;
  color: var(--text);
}
.chat-drawer.open { transform: none; visibility: visible; }
.chat-drawer-head { display: flex; align-items: center; justify-content: space-between; }
.chat-drawer .chat-log { flex: 1; height: auto; }
.chat-drawer .chat-form { margin-top: 0; }
.chat-drawer button:not(.btn-primary) { background: #1f2a36; border-color: #2c3846; color: #fff; }
.chat-drawer-head button { padding: .45rem; }
