:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-alt: #f0f2f5;
  --border: #dcdfe5;
  --text: #1b1f24;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --danger: #b91c1c;
  --danger-bg: #fee2e2;
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

/*
 * Any `display` rule below outranks the user agent's [hidden] { display: none },
 * so state it once here and let every panel use the attribute freely.
 */
[hidden] { display: none !important; }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --panel: #1c2027;
    --panel-alt: #232833;
    --border: #333a45;
    --text: #e7eaef;
    --muted: #9aa3b2;
    --accent: #3b82f6;
    --ok: #4ade80;
    --ok-bg: #14321f;
    --warn: #fbbf24;
    --warn-bg: #3a2c08;
    --danger: #f87171;
    --danger-bg: #3a1414;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ─────────── Top bar ─────────── */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}

.logo { color: var(--accent); font-size: 18px; }

.pills { display: flex; gap: 6px; flex-wrap: wrap; }

.pill {
  font-family: var(--mono);
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  white-space: nowrap;
}

.pill-ok { color: var(--ok); background: var(--ok-bg); border-color: transparent; }
.pill-warn { color: var(--warn); background: var(--warn-bg); border-color: transparent; }
.pill-danger { color: var(--danger); background: var(--danger-bg); border-color: transparent; }
.pill-off { color: var(--muted); background: var(--panel-alt); }

.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.topbar-sep { width: 1px; align-self: stretch; background: var(--border); }

/* ─────────── Sign-in ─────────── */

.login {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 16px;
  background: var(--bg);
}

.login-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(340px, 100%);
  padding: 24px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.login-card h2 { margin: 0 0 4px; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.login-card input { width: 100%; }
.login-card .status { margin: 0; }

/* ─────────── Access dialog ─────────── */

.access {
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 16px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.access::backdrop { background: rgb(0 0 0 / 0.45); }

.access-section { padding-top: 12px; margin-top: 12px; border-top: 1px solid var(--border); }
.access-section h3 { margin: 0 0 4px; font-size: 13px; }
.access-section .hint { margin: 0 0 8px; }
.access-section .profile-form { margin: 0 0 8px; }

.access-list { display: flex; flex-direction: column; gap: 6px; }

.access-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 8px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-alt);
}

.access-row.is-disabled { opacity: 0.6; }
.access-name { font-size: 12px; font-weight: 600; }
.access-code { font-family: var(--mono); font-size: 11px; }

.key-reveal {
  padding: 8px;
  margin-bottom: 8px;
  border-radius: 6px;
  background: var(--ok-bg);
}

.key-reveal-row { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
.key-reveal-row code { flex: 1; font-family: var(--mono); font-size: 12px; word-break: break-all; }

/* ─────────── Layout ─────────── */

.layout {
  columns: 2;
  column-gap: 12px;
  padding: 12px;
}

.layout > .panel {
  break-inside: avoid;
  margin-bottom: 12px;
}

@media (max-width: 1080px) {
  .layout { columns: 1; }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  min-width: 0;
}

.panel h2 { margin: 0 0 12px; font-size: 15px; }
.panel h3 { margin: 0; font-size: 13px; color: var(--muted); font-weight: 600; }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.panel-head h2 { margin: 0; }

/* ─────────── Forms ─────────── */

.form {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 10px;
  align-items: flex-end;
}

.field { display: flex; flex-direction: column; gap: 3px; }
.field span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.field-wide { flex: 1 1 100%; }
.field-sm input { width: 72px; }

input[type="text"],
input[type="number"],
input[type="password"],
select,
textarea {
  font: inherit;
  color: var(--text);
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  min-width: 0;
}

input:focus, select:focus, canvas:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.check { display: flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); padding-bottom: 6px; }

.btn {
  font: inherit;
  color: var(--text);
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}

.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: default; border-color: var(--border); }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn-ghost { background: transparent; }
.btn-danger { color: var(--danger); }
.btn-danger:hover:not(:disabled) { border-color: var(--danger); }
.btn-tiny { padding: 3px 8px; font-size: 12px; }

/* ─────────── Search result ─────────── */

.request-line {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 0 8px;
  padding: 6px 8px;
  background: var(--panel-alt);
  border-radius: 6px;
  overflow: hidden;
}

.request-line code {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow-x: auto;
  flex: 1;
}

.status { padding: 8px 10px; border-radius: 6px; font-size: 13px; margin-bottom: 8px; word-break: break-all; }
.status-info { background: var(--panel-alt); color: var(--muted); }
.status-ok { background: var(--ok-bg); color: var(--ok); }
.status-warn { background: var(--warn-bg); color: var(--warn); }
.status-error { background: var(--danger-bg); color: var(--danger); }

.result-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.result-actions { display: flex; align-items: center; gap: 8px; }

.result {
  font-family: var(--mono);
  font-size: 12px;
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  margin: 0;
  max-height: 46vh;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.muted { color: var(--muted); font-size: 12px; }
.hint { color: var(--muted); font-size: 12px; margin: 10px 0 0; }
.empty { color: var(--muted); font-size: 12px; margin: 0; padding: 8px; }

/* ─────────── Browser panel ─────────── */

.viewport-controls { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.viewport-controls input { width: 68px; }

.nav-form { display: flex; gap: 6px; margin-bottom: 10px; }
.nav-form input { flex: 1; }

.tabs { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; max-height: 176px; overflow-y: auto; }

.tab {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-alt);
  cursor: pointer;
}

.tab:hover { border-color: var(--accent); }
.tab.is-active { border-color: var(--accent); box-shadow: inset 2px 0 0 var(--accent); }
.tab.is-blocked { background: var(--warn-bg); }

.tab-main { flex: 1; min-width: 0; }
.tab-title { font-size: 12px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tab-url { font-family: var(--mono); font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tab-badge { font-size: 10px; padding: 2px 6px; border-radius: 999px; background: var(--warn); color: #1b1f24; white-space: nowrap; }
.tab-close { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 14px; padding: 0 4px; }
.tab-close:hover { color: var(--danger); }

.stage { display: flex; gap: 4px; }

.canvas-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
  background: #000;
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  aspect-ratio: 16 / 10;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

canvas.is-hidden { visibility: hidden; }

.canvas-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
  color: #9aa3b2;
  font-size: 13px;
  pointer-events: none;
}

.scroll-track {
  width: 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-alt);
  position: relative;
  visibility: hidden;
}

.scroll-track.is-active { visibility: visible; }

.scroll-thumb {
  position: absolute;
  left: 1px;
  right: 1px;
  border-radius: 6px;
  background: var(--muted);
  opacity: 0.6;
  cursor: grab;
}

/* ─────────── Profiles ─────────── */

.profiles { margin-bottom: 10px; }

.profiles-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 6px; }
.profiles-head #profiles-summary { margin-left: auto; }

.profile-list { display: flex; flex-direction: column; gap: 6px; }
.profile-entry { display: flex; flex-direction: column; gap: 4px; }

/* Settings drawer: the add form, and the per-profile one under its row. */
.profile-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  padding: 8px;
  margin-bottom: 6px;
  border: 1px dashed var(--border);
  border-radius: 6px;
}

/* `display: flex` above would otherwise beat the UA rule for [hidden]. */
.profile-form[hidden] { display: none; }

.profile-edit { margin: 0 0 0 16px; }

.field-inline { display: flex; align-items: center; gap: 5px; }
.field-inline span {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.field-inline input { padding: 4px 6px; font-size: 12px; }
.field-grow { flex: 1 1 220px; }
.field-grow input { width: 100%; }

.profile {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--panel-alt);
}

.profile.is-warming { border-color: var(--accent); }
.profile.is-challenged { background: var(--warn-bg); }
.profile.is-disabled { opacity: 0.55; }

.profile-name { font-size: 12px; font-weight: 600; }
.profile-meta { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.profile-actions { margin-left: auto; display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px; }

.profile-badge {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
}

.profile-badge.is-idle { color: var(--ok); background: var(--ok-bg); border-color: transparent; }
.profile-badge.is-busy,
.profile-badge.is-warming { color: var(--accent); border-color: var(--accent); }
.profile-badge.is-challenged { color: var(--warn); background: var(--warn-bg); border-color: transparent; }
.profile-badge.is-resetting { color: var(--danger); background: var(--danger-bg); border-color: transparent; }

/* ─────────── Overview grid ─────────── */

.overview {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
  margin-bottom: 10px;
}

.overview .empty { grid-column: 1 / -1; }

.thumb {
  display: grid;
  grid-template-rows: auto auto auto;
  gap: 4px;
  position: relative;
  padding: 6px;
  text-align: left;
  font: inherit;
  color: var(--text);
  background: var(--panel-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}

.thumb:hover { border-color: var(--accent); }
.thumb.is-blocked { border-color: var(--warn); background: var(--warn-bg); }

.thumb-frame {
  position: relative;
  aspect-ratio: 16 / 10;
  overflow: hidden;
  border-radius: 4px;
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: center;
}

.thumb-frame img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: top center;
  display: block;
}

.thumb-placeholder { font-size: 11px; color: var(--muted); }

.thumb-badge {
  position: absolute;
  top: 12px;
  left: 12px;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--warn);
  color: #1b1f24;
}

.thumb-meta { display: flex; align-items: center; gap: 6px; min-width: 0; }
.thumb-title {
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.thumb-url {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thumb-profile {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--muted);
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-radius: 999px;
  white-space: nowrap;
  flex: none;
}

.thumb-close {
  position: absolute;
  top: 10px;
  right: 10px;
  border: none;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.45);
  color: #fff;
  font-size: 12px;
  line-height: 1;
  padding: 3px 5px;
  cursor: pointer;
  opacity: 0;
}

.thumb:hover .thumb-close { opacity: 1; }
.thumb-close:hover { background: var(--danger); }

/* ─────────── Stage header ─────────── */

.stage-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.stream-info { font-family: var(--mono); font-size: 11px; white-space: nowrap; }
.stream-controls { display: flex; align-items: center; gap: 6px; margin-left: auto; font-size: 12px; color: var(--muted); }
.stream-controls input { width: 54px; }
.stage-title {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
  flex: 1;
}

.tab-profile {
  font-family: var(--mono);
  font-size: 10px;
  color: var(--muted);
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  white-space: nowrap;
}

.hint-tight { margin-top: 8px; }
