/* XStream Engine panel. Original design: a left menu with a teal-to-navy
   gradient, a status strip on top and light content cards. No third-party
   CSS, fonts or icons. */
:root {
  --bg: #f1f4f9;
  --card: #ffffff;
  --line: #e2e8f0;
  --text: #1f2937;
  --text-dim: #64748b;
  --accent: #0d9488;
  --accent-dark: #0f766e;
  --blue: #2563eb;
  --red: #dc2626;
  --amber: #d97706;
  --green: #16a34a;
  --gray: #64748b;
  --side-top: #0f766e;
  --side-bottom: #172554;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 1px 3px rgba(15, 23, 42, 0.04);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}
.hidden { display: none !important; }
code { background: #eef2f7; padding: 1px 5px; border-radius: 4px; font-size: 12.5px; word-break: break-all; }
a { color: var(--accent-dark); }

/* ---------- forms & buttons ---------- */
input, select, textarea {
  font: inherit;
  color: var(--text);
  background: #fff;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  padding: 7px 9px;
  width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(13, 148, 136, 0.35); border-color: var(--accent); }
textarea { resize: vertical; font-family: inherit; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; color: var(--text-dim); font-weight: 600; }
label input, label select, label textarea { font-weight: 400; }
label.checkbox { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-weight: 500; }
label.checkbox input { width: auto; }
.optional { font-weight: 400; opacity: 0.8; }

.btn {
  font: inherit; font-weight: 600; cursor: pointer;
  border: 1px solid #cbd5e1; background: #fff; color: var(--text);
  padding: 8px 14px; border-radius: 6px; line-height: 1.2;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-blue { background: var(--blue); border-color: var(--blue); color: #fff; }
.btn-red { background: var(--red); border-color: var(--red); color: #fff; }
.btn-amber { background: var(--amber); border-color: var(--amber); color: #fff; }
.btn-small { padding: 3px 9px; font-size: 12px; }
.btn-block { width: 100%; }
.btn-link { border: none; background: none; color: var(--text-dim); text-decoration: underline; padding: 6px; }
.error-text { color: var(--red); min-height: 1.2em; margin: 8px 0 0; font-size: 13px; }
.hint { color: var(--text-dim); font-size: 12.5px; margin: 6px 0; }
.empty { color: var(--text-dim); padding: 18px; text-align: center; margin: 0; }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; align-items: end; }
.form-grid .wide { grid-column: 1 / -1; }
.form-foot { grid-column: 1 / -1; display: flex; gap: 12px; align-items: center; }
.inline-form { display: flex; gap: 8px; margin: 10px 0; max-width: 520px; }
.inline-form .btn { white-space: nowrap; }

/* ---------- login / forced password change ---------- */
.auth-screen {
  min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 24px;
  background: linear-gradient(160deg, var(--side-top), var(--side-bottom));
}
.auth-card {
  width: 100%; max-width: 380px; background: var(--card); border-radius: 12px; padding: 28px;
  display: flex; flex-direction: column; gap: 14px; box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}
.auth-card h1 { margin: 0; font-size: 20px; }
.auth-brand { display: flex; align-items: center; gap: 10px; font-weight: 700; color: var(--accent-dark); }
.logo-mark {
  display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px;
  border-radius: 8px; background: var(--accent); color: #fff; font-weight: 800;
}

/* ---------- shell ---------- */
.app { display: flex; min-height: 100%; }
.sidebar {
  width: 232px; flex: none; color: #e6fffb; display: flex; flex-direction: column;
  background: linear-gradient(180deg, var(--side-top) 0%, var(--side-bottom) 100%);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 16px 18px; font-weight: 700; font-size: 16px; color: #fff; }
.brand .logo-mark { background: rgba(255, 255, 255, 0.18); }
.nav { flex: 1; padding: 6px 0; overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; gap: 12px; width: 100%; padding: 12px 18px;
  color: inherit; text-decoration: none; font: inherit; font-weight: 600; font-size: 13px;
  text-transform: uppercase; letter-spacing: 0.04em; background: none; border: none; cursor: pointer; text-align: left;
}
.nav-item:hover { background: rgba(255, 255, 255, 0.08); }
.nav-item.active { background: rgba(255, 255, 255, 0.16); box-shadow: inset 3px 0 0 #5eead4; }
.nav-item .ico { width: 18px; height: 18px; flex: none; }
.chev { margin-left: auto; width: 16px; height: 16px; transition: transform 0.15s; }
.nav-group.open .chev { transform: rotate(180deg); }
.nav-sub { display: none; background: rgba(0, 0, 0, 0.16); }
.nav-group.open .nav-sub { display: block; }
.nav-subitem {
  display: block; padding: 9px 18px 9px 48px; color: #ccfbf1; text-decoration: none; font-size: 13px; font-weight: 500;
}
.nav-subitem:hover { color: #fff; background: rgba(255, 255, 255, 0.07); }
.nav-subitem.active { color: #fff; font-weight: 700; }
.sidebar-foot { padding: 12px 18px; font-size: 11.5px; color: #99f6e4; opacity: 0.85; word-break: break-all; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 10px 22px; background: var(--card);
  border-bottom: 1px solid var(--line); box-shadow: var(--shadow); position: sticky; top: 0; z-index: 5;
}
.stats { display: flex; flex-wrap: wrap; gap: 6px 26px; flex: 1; font-size: 12.5px; color: var(--accent-dark); font-weight: 600; }
.stat b { color: var(--text); }
.stat b.ok { color: var(--green); }
.userbox { display: flex; align-items: center; gap: 10px; color: var(--text-dim); }
.userbox .ico { width: 30px; height: 30px; padding: 5px; border-radius: 50%; background: #e6f4f1; color: var(--accent-dark); }
.user-lines { display: flex; flex-direction: column; line-height: 1.15; }
.user-lines b { color: var(--text); }
.user-lines small { font-size: 11px; }
.icon-btn { background: none; border: none; cursor: pointer; color: var(--text-dim); padding: 6px; border-radius: 6px; }
.icon-btn:hover { background: #eef2f7; color: var(--text); }
.icon-btn .ico { width: 20px; height: 20px; }
.only-narrow { display: none; }
.content { padding: 22px; }

.ico svg, .ico-inline svg { width: 100%; height: 100%; display: block; }
.ico { display: inline-block; }

/* ---------- pages & cards ---------- */
.view { display: none; }
.view.active { display: block; }
.page-title { margin: 0 0 16px; font-size: 20px; }
.section-title { margin: 26px 0 12px; font-size: 15px; }
.section-title small { color: var(--text-dim); font-weight: 400; margin-left: 8px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 18px; }
.card h3 { margin: 0 0 12px; font-size: 15px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 14px; margin-bottom: 8px; }
.cards .card { margin: 0; padding: 14px 16px; }
.metric-label { font-size: 11.5px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.05em; font-weight: 600; }
.metric-value { font-size: 24px; font-weight: 700; margin-top: 4px; }
.metric-value.ok { color: var(--green); }
.metric-value.bad { color: var(--red); }
.settings-card { display: none; }
.view[data-section="account"] #card-account,
.view[data-section="server"] #card-server,
.view[data-section="tokens"] #card-tokens,
.view[data-section="users"] #card-users { display: block; }

/* DEC-031: a viewer session hides every admin-only nav entry and action
   button -- the server refuses these anyway (RequireAdmin, 403), this
   just keeps a viewer from seeing controls that would only ever fail.
   [data-viewer-only] (the Watch nav entry) is the mirror image: visible
   only for a viewer, since an admin already has Overview/Streams. */
body.role-viewer [data-admin-only] { display: none !important; }
body:not(.role-viewer) [data-viewer-only] { display: none !important; }

.watch-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.watch-card { display: flex; flex-direction: column; gap: 8px; }
.watch-card .name { font-weight: 600; }

.kv-grid { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; margin: 0; }
.kv-grid dt { color: var(--text-dim); font-weight: 600; font-size: 12.5px; }
.kv-grid dd { margin: 0; word-break: break-word; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.toolbar .search { flex: 1; min-width: 220px; width: auto; }
.toolbar .spacer { flex: 1; }

/* ---------- tables ---------- */
.table-wrap { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
.card .data-table { margin-top: 10px; }
.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
.data-table th { color: var(--text-dim); font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.04em; font-weight: 700; background: #f8fafc; }
.data-table tbody tr:hover { background: #f8fafc; }
.data-table td.wrap { white-space: normal; word-break: break-all; max-width: 280px; }
.col-actions { text-align: right !important; }
td.actions { text-align: right; }
.actions-inner { display: inline-flex; gap: 2px; }

.row-btn { background: none; border: none; cursor: pointer; padding: 5px; border-radius: 5px; color: var(--text-dim); }
.row-btn .ico { width: 17px; height: 17px; display: block; }
.row-btn:hover:not(:disabled) { background: #eef2f7; }
.row-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.row-btn.start { color: var(--green); }
.row-btn.stop { color: var(--red); }
.row-btn.restart { color: var(--amber); }
.row-btn.watch { color: var(--blue); }
.row-btn.flow { color: #7c3aed; }
.row-btn.danger { color: var(--red); }

.badge { display: inline-block; padding: 2px 9px; border-radius: 4px; font-size: 11.5px; font-weight: 700; color: #fff; background: var(--gray); }
.badge.running { background: var(--green); }
.badge.error { background: var(--red); }
.badge.starting, .badge.stopping { background: var(--blue); }
.badge.reconnecting, .badge.degraded { background: var(--amber); }
.badge.stopped { background: var(--gray); }
.stream-link { color: var(--blue); font-weight: 600; cursor: pointer; text-decoration: none; }
.stream-link:hover { text-decoration: underline; }
.tag { display: inline-block; background: #e6f4f1; color: var(--accent-dark); border-radius: 4px; padding: 1px 7px; font-size: 11.5px; font-weight: 600; margin-right: 4px; }
.tag.warn { background: #fef3c7; color: #92400e; }
.tag.dim { background: #eef2f7; color: var(--text-dim); }
.status-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; background: var(--gray); }
.status-dot.running, .status-dot.healthy { background: var(--green); }
.status-dot.error, .status-dot.unhealthy { background: var(--red); }

/* ---------- flow overview ---------- */
.flows { display: flex; flex-direction: column; gap: 16px; }
.flow-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.flow-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); background: #f8fafc; }
.flow-head .name { font-weight: 700; font-size: 15px; }
.flow-head .grow { flex: 1; }
.flow-body { display: grid; grid-template-columns: 1fr 34px 1fr 34px 1fr; align-items: stretch; padding: 14px 16px; gap: 0; }
.flow-col { border: 1px solid var(--line); border-radius: 8px; padding: 12px; min-width: 0; }
.flow-col h4 { margin: 0 0 8px; font-size: 11.5px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); }
.flow-arrow { display: flex; align-items: center; justify-content: center; color: var(--accent); font-size: 22px; font-weight: 700; }
.flow-item { padding: 8px 0; border-bottom: 1px dashed var(--line); }
.flow-item:last-child { border-bottom: none; padding-bottom: 0; }
.flow-item:first-of-type { padding-top: 0; }
.flow-url { font-size: 12.5px; word-break: break-all; }
.flow-item.active-src { box-shadow: inset 3px 0 0 var(--green); padding-left: 9px; }
.flow-meta { color: var(--text-dim); font-size: 12px; margin-top: 3px; display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: center; }
.flow-warn { color: var(--amber); font-weight: 600; font-size: 13px; }
.flow-actions { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.metrics-grid { display: grid; grid-template-columns: max-content 1fr; gap: 3px 14px; font-size: 13px; margin-top: 8px; }
.metrics-grid dt { color: var(--text-dim); }
.metrics-grid dd { margin: 0; }

/* ---------- dialogs ---------- */
dialog { border: none; border-radius: 12px; padding: 22px; width: min(640px, 94vw); max-height: 92vh; box-shadow: 0 25px 60px rgba(15, 23, 42, 0.35); color: var(--text); }
dialog.wide-modal { width: min(1040px, 96vw); }
dialog::backdrop { background: rgba(15, 23, 42, 0.55); }
dialog h2 { margin: 0 0 14px; font-size: 18px; }
dialog h3 { margin: 14px 0 8px; font-size: 14px; }
dialog fieldset { border: 1px solid var(--line); border-radius: 8px; margin: 14px 0 0; padding: 12px; }
dialog legend { padding: 0 6px; font-weight: 700; font-size: 12.5px; color: var(--text-dim); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 6px 24px; }
.log-box { background: #0f172a; color: #cbd5e1; padding: 12px; border-radius: 8px; max-height: 260px; overflow: auto; font-size: 12px; white-space: pre-wrap; word-break: break-all; margin: 0; }

.player-stage { background: #000; border-radius: 8px; overflow: hidden; position: relative; aspect-ratio: 16 / 9; max-height: 62vh; margin: 0 auto; }
.player-stage video { width: 100%; height: 100%; display: block; background: #000; }
.player-help { position: absolute; inset: 0; padding: 20px; overflow: auto; background: #0f172a; color: #e2e8f0; font-size: 13px; line-height: 1.5; }
.player-help code { background: #1e293b; color: #99f6e4; display: block; margin: 8px 0; padding: 8px; white-space: pre-wrap; }
.player-info { display: flex; flex-wrap: wrap; gap: 6px 22px; color: var(--text-dim); font-size: 12.5px; }
.player-info b { color: var(--text); }
.tabs-inline { display: flex; gap: 6px; margin-bottom: 10px; flex-wrap: wrap; }
.tabs-inline .btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.reveal { background: #ecfdf5; border: 1px solid #6ee7b7; border-radius: 8px; padding: 10px 12px; margin: 10px 0; display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }

.toast { position: fixed; right: 18px; bottom: 18px; background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 8px; opacity: 0; pointer-events: none; transition: opacity 0.2s; z-index: 50; max-width: 420px; }
.toast.show { opacity: 1; }
.toast.err { background: var(--red); }

/* ---------- narrow screens ---------- */
@media (max-width: 960px) {
  .flow-body { grid-template-columns: 1fr; gap: 6px; }
  .flow-arrow { transform: rotate(90deg); height: 26px; }
}
@media (max-width: 860px) {
  .only-narrow { display: inline-block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; transform: translateX(-100%); transition: transform 0.2s; }
  .sidebar.open { transform: none; }
  .content { padding: 14px; }
  .stats { gap: 4px 14px; }
  .user-lines { display: none; }
}
