:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --text: #17202b;
  --muted: #5d6b7c;
  --border: #d6dde6;
  --accent: #12357a;
  --accent-text: #ffffff;
  --ok: #1d8a4e;
  --warn: #b26a00;
  --bad: #c62828;
  --bar: #0f1d33;
  --bar-text: #e8eef7;
  --stage: #1b2433;
  --radius: 8px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161e;
    --surface: #1a212c;
    --text: #e6ebf2;
    --muted: #93a1b3;
    --border: #2c3644;
    --accent: #4d7fe0;
    --bar: #0b1018;
    --stage: #0b1018;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
button, input, select {
  font: inherit;
  color: inherit;
}
button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 6px 12px;
  cursor: pointer;
}
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.danger { color: var(--bad); }
button:disabled { opacity: .5; cursor: default; }
input, select {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 7px 10px;
  min-width: 0;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--bad); min-height: 1.4em; }

/* login */
.center { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 360px;
}
.card h1 { margin: 0 0 16px; font-size: 20px; }
.stack { display: grid; gap: 10px; }
.stack label { display: grid; gap: 4px; color: var(--muted); font-size: 13px; }

/* top bar */
.bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 8px 12px;
  background: var(--bar);
  color: var(--bar-text);
}
.bar .title { font-weight: 600; margin-right: 6px; }
.bar select, .bar button { background: transparent; border-color: #3a4a63; color: var(--bar-text); }
.bar select option { color: #000; }
.bar .spacer { flex: 1; }
.bar a { color: var(--bar-text); }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; background: #777; }
.dot.on { background: #33c27a; }
.dot.off { background: #e05252; }
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }
.stats { font-size: 12px; opacity: .75; font-variant-numeric: tabular-nums; }

/* viewer */
.viewer { display: flex; flex-direction: column; height: 100%; }
.stage {
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  background: var(--stage);
  padding: 8px;
  overflow: hidden;
  position: relative;
}
#screen {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  background: #000;
  touch-action: none;
  outline: none;
  box-shadow: 0 2px 16px rgba(0, 0, 0, .45);
}
#screen.control { cursor: pointer; }
#screen.control:focus { box-shadow: 0 0 0 2px #33c27a; }
.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #e8eef7;
  font-size: 16px;
  pointer-events: none;
  text-align: center;
  padding: 16px;
}
.viewer:fullscreen .bar { display: none; }

/* dialog */
dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  padding: 20px;
  width: min(380px, calc(100vw - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.row { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }

/* admin */
.page { max-width: 1100px; margin: 0 auto; padding: 16px; }
.tabs { display: flex; gap: 6px; margin-bottom: 14px; flex-wrap: wrap; }
.tabs button.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 14px;
  overflow-x: auto;
}
.panel h2 { margin: 0 0 12px; font-size: 16px; }
table { border-collapse: collapse; width: 100%; }
th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { color: var(--muted); font-weight: 500; font-size: 13px; }
td .row { justify-content: flex-start; }
.form { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.badge { font-size: 12px; padding: 1px 8px; border-radius: 10px; border: 1px solid var(--border); }
.badge.ok { color: var(--ok); border-color: var(--ok); }
.badge.bad { color: var(--bad); border-color: var(--bad); }
.badge.warn { color: var(--warn); border-color: var(--warn); }
pre.log { margin: 0; font-size: 12px; white-space: pre-wrap; word-break: break-all; max-height: 60vh; overflow: auto; }
.token { font-family: ui-monospace, monospace; word-break: break-all; background: var(--bg); padding: 8px; border-radius: 6px; }

/* device picker */
.device-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 260px));
  gap: 16px;
}
.device-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 20px 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  text-decoration: none;
  text-align: center;
  transition: border-color .15s, box-shadow .15s, transform .15s;
}
.device-card:hover, .device-card:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 4px 18px rgba(18, 53, 122, .15);
  transform: translateY(-2px);
  outline: none;
}
.device-card img { width: 160px; max-width: 100%; height: auto; margin-bottom: 6px; }
.device-card.offline img { filter: grayscale(1); opacity: .55; }
.device-name { font-weight: 600; font-size: 15px; word-break: break-word; }
.device-id { font-size: 12px; color: var(--muted); font-family: ui-monospace, monospace; }
.device-status { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 14px 6px 8px;
  border-radius: var(--radius);
  background: #1f3a66;
  color: #ffffff !important;
  text-decoration: none;
  font-weight: 600;
  border: 1px solid #2f5390;
  margin-right: 6px;
}
.back-btn:hover, .back-btn:focus-visible { background: #2a4d86; outline: none; }
.badge-view {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 12px;
  background: rgba(240, 160, 40, .18);
  color: #f6c26b;
  border: 1px solid rgba(240, 160, 40, .45);
}
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.row-between h1 { margin: 0 0 16px; }
.row-between .lang-select { margin-bottom: 16px; }
.lang-select { padding: 5px 8px; cursor: pointer; }
.viewers-count { font-size: 13px; opacity: .85; white-space: nowrap; }

.device-card { position: relative; }
.device-link { display: flex; flex-direction: column; align-items: center; gap: 6px; color: inherit; text-decoration: none; width: 100%; }
.device-link.disabled { cursor: not-allowed; opacity: .7; }
.device-card.closed { border-color: var(--bad); }
.closed-note { font-size: 13px; color: var(--bad); font-weight: 600; text-align: center; }
.closed-note .muted { font-weight: 400; }
.access-btn { margin-top: 10px; font-size: 13px; }
.access-btn.is-closed { background: var(--bad); border-color: var(--bad); color: #fff; }
.bar .access-btn { margin-top: 0; }
.bar .access-btn.is-closed { background: #c62828; border-color: #c62828; color: #fff; }
.badge-closed { font-size: 12px; padding: 3px 10px; border-radius: 12px; background: rgba(224, 82, 82, .2); color: #ff9a9a; border: 1px solid rgba(224, 82, 82, .55); }
.badge-guest { font-size: 12px; padding: 3px 10px; border-radius: 12px; background: rgba(77, 127, 224, .2); color: #a9c4ff; border: 1px solid rgba(77, 127, 224, .55); white-space: nowrap; }
.badge { white-space: nowrap; display: inline-block; }
.pw-field { display: inline-flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.pw-field input { min-width: 190px; }
dialog .pw-field { display: flex; }
dialog .pw-field input { flex: 1; }

.device-card { padding: 20px 16px 16px; gap: 0; }
.device-link { gap: 4px; }
.device-card img { margin-bottom: 10px; }
.device-name { line-height: 1.3; }
.device-chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; margin-top: 8px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 24px;
  padding: 0 10px;
  border-radius: 12px;
  font-size: 12.5px;
  line-height: 1;
  white-space: nowrap;
  background: var(--bg);
  color: var(--muted);
  border: 1px solid var(--border);
}
.chip .dot { width: 8px; height: 8px; flex: none; }
.chip-on { color: var(--ok); border-color: rgba(29, 138, 78, .35); background: rgba(51, 194, 122, .08); }
.chip-off { color: var(--bad); border-color: rgba(198, 40, 40, .3); background: rgba(224, 82, 82, .06); }
.chip-closed { color: var(--bad); border-color: rgba(198, 40, 40, .45); background: rgba(224, 82, 82, .08); font-weight: 600; }
.closed-by { font-size: 12px; color: var(--muted); text-align: center; margin-top: 4px; }
.device-card .access-btn { margin-top: 14px; }
.device-card .device-link { margin-bottom: 14px; }
.device-card .access-btn { margin-top: auto; }
