:root {
  color-scheme: dark;
  --bg: #0b0d10;
  --bg-2: #12151a;
  --bg-3: #1a1e25;
  --line: #262b34;
  --text: #e9edf2;
  --muted: #8b95a3;
  --accent: #6ea8ff;
  --red: #ff4d4f;
  --green: #3ddc84;
  --amber: #ffb547;
  --radius: 14px;
  --glass: rgba(12, 14, 18, 0.62);
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 22px; }
h2 { font-size: 18px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.spacer { flex: 1; }
.error { color: var(--red); min-height: 1.4em; margin: 0; font-size: 14px; }

body:not(.is-owner) .owner-only { display: none !important; }
body.is-owner .viewer-only { display: none !important; }

/* ---------- Controls ---------- */
button, .button {
  font: inherit;
  color: var(--text);
  background: var(--bg-3);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
button:hover:not(:disabled), .button:hover { background: #222833; text-decoration: none; }
button:disabled { opacity: 0.45; cursor: default; }
button:focus-visible, .button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); border-color: var(--accent); color: #06101f; font-weight: 600; }
button.primary:hover:not(:disabled) { background: #8cbaff; }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); }
button.ghost:hover:not(:disabled) { color: var(--text); background: var(--bg-3); }
button.small { padding: 6px 10px; font-size: 14px; }
.glass { background: var(--glass); border-color: rgba(255, 255, 255, 0.12); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); }
.glass:hover:not(:disabled) { background: rgba(30, 34, 42, 0.8); }
.danger { color: #ff8a8b; }

select, input[type="password"], input[type="text"] {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 11px;
  width: 100%;
}
select:disabled { opacity: 0.5; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 10px max(16px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: rgba(11, 13, 16, 0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 9px; color: var(--text); font-weight: 700; letter-spacing: -0.02em; font-size: 17px; }
.brand:hover { text-decoration: none; }
.brand-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--red); box-shadow: 0 0 0 3px rgba(255, 77, 79, 0.22); }
.brand-dot.big { width: 22px; height: 22px; box-shadow: 0 0 0 7px rgba(255, 77, 79, 0.18); }
.topbar nav { display: flex; gap: 4px; flex: 1; }
#logout { white-space: nowrap; }
.topbar nav a { color: var(--muted); padding: 6px 12px; border-radius: 8px; font-weight: 550; }
.topbar nav a:hover { color: var(--text); background: var(--bg-3); text-decoration: none; }
.topbar nav a.active { color: var(--text); background: var(--bg-3); }

main { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.view { flex: 1; padding: 16px; width: 100%; max-width: 1400px; margin: 0 auto; }

/* ---------- Login ---------- */
.login { display: grid; place-items: center; }
.login-card { width: min(360px, 100%); display: flex; flex-direction: column; gap: 12px; text-align: center; padding: 28px; background: var(--bg-2); border: 1px solid var(--line); border-radius: 18px; }
.login-card h1 { font-size: 26px; }
.login-card p { margin: 0; }
.login-card button { justify-content: center; padding: 10px; }
.login-mark { display: grid; place-items: center; height: 44px; }

/* ---------- Stage (video area) ---------- */
.stage {
  position: relative;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  aspect-ratio: 16 / 9;
  width: 100%;
  border: 1px solid var(--line);
}
.stage video, .stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; display: block; }
#preview.mirror { transform: scaleX(-1); }
.stage-empty {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  text-align: center; padding: 24px;
  background: radial-gradient(ellipse at center, #151a22 0%, #07080a 75%);
}
.stage-empty p { margin: 0; max-width: 420px; }
.pulse-ring { width: 18px; height: 18px; border-radius: 50%; background: var(--muted); opacity: 0.6; margin-bottom: 6px; animation: pulse 2.2s ease-out infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(139, 149, 163, 0.5); } 100% { box-shadow: 0 0 0 22px rgba(139, 149, 163, 0); } }

.hud { position: absolute; left: 0; right: 0; display: flex; align-items: center; gap: 8px; padding: 12px; pointer-events: none; flex-wrap: wrap; }
.hud > * { pointer-events: auto; }
.hud-top { top: 0; }
.hud-bottom { bottom: 0; }
.badge, .chip {
  font-size: 12px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase;
  padding: 4px 9px; border-radius: 7px;
  background: var(--glass); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  display: inline-flex; align-items: center; gap: 6px;
}
.chip { text-transform: none; letter-spacing: 0; font-weight: 550; }
.badge.live { background: var(--red); border-color: var(--red); color: #fff; }
.badge.rec { color: #fff; }
.badge.rec::before, .badge.live::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.rec::before { background: var(--red); animation: blink 1.2s steps(2, start) infinite; }
@keyframes blink { to { visibility: hidden; } }
.remote { display: flex; gap: 8px; }
.remote button.recording { background: var(--red); border-color: var(--red); color: #fff; }

.flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.flash.go { animation: flash 0.35s ease-out; }
@keyframes flash { 0% { opacity: 0.85; } 100% { opacity: 0; } }

/* Live view fills the window */
#view-live { display: flex; flex-direction: column; }
#view-live .stage { flex: 1; aspect-ratio: auto; min-height: 260px; max-height: calc(100dvh - 100px); }
#live-stage:fullscreen { max-height: none; border-radius: 0; border: 0; }
#live-stage.pseudo-full { position: fixed; inset: 0; z-index: 50; max-height: none; border-radius: 0; }

/* ---------- Studio ---------- */
.studio { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; }
.studio-main { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.action { padding: 12px 20px; font-weight: 600; border-radius: 12px; min-width: 132px; justify-content: center; }
.ico { width: 14px; height: 14px; border-radius: 50%; display: inline-block; flex: none; }
.ico-snap { border: 2px solid currentColor; }
.ico-rec { background: var(--red); }
.ico-live { border: 2px solid var(--red); box-shadow: inset 0 0 0 2px var(--bg-3), inset 0 0 0 7px var(--red); }
.action.on { background: var(--red); border-color: var(--red); color: #fff; }
.action.on .ico-rec { background: #fff; border-radius: 3px; }
.action.on .ico-live { border-color: #fff; box-shadow: inset 0 0 0 2px var(--red), inset 0 0 0 7px #fff; }
.status-line { text-align: center; margin: 0; min-height: 1.45em; font-size: 14px; }
.panel { background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.panel h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin-top: 6px; }
.panel h3:first-child { margin-top: 0; }
.panel label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
.panel label.check { flex-direction: row; align-items: flex-start; gap: 8px; }
.panel label.check input { margin-top: 3px; }
.hint { font-size: 13px; color: var(--muted); margin: 0; }

.recent { background: var(--bg-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.recent-head { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 8px; }
.recent-strip { display: flex; gap: 8px; overflow-x: auto; }
.recent-strip img { height: 64px; aspect-ratio: 16 / 9; object-fit: cover; border-radius: 8px; flex: none; background: #000; }

/* ---------- Library ---------- */
.lib-head { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.segmented { display: inline-flex; background: var(--bg-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; }
.segmented button { border: 0; background: transparent; color: var(--muted); padding: 6px 12px; border-radius: 7px; font-size: 14px; }
.segmented button.on { background: var(--bg-3); color: var(--text); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; }
.day { grid-column: 1 / -1; font-size: 13px; font-weight: 650; color: var(--muted); padding: 14px 2px 2px; text-transform: uppercase; letter-spacing: 0.06em; }
.day:first-child { padding-top: 0; }
.card { position: relative; padding: 0; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; aspect-ratio: 16 / 9; background: #000; display: block; width: 100%; }
.card img { width: 100%; height: 100%; object-fit: cover; display: block; transition: transform 0.25s; }
.card:hover img { transform: scale(1.03); }
.card .meta { position: absolute; left: 0; right: 0; bottom: 0; display: flex; justify-content: space-between; align-items: end; gap: 6px; padding: 18px 8px 6px; font-size: 12px; background: linear-gradient(transparent, rgba(0, 0, 0, 0.75)); color: #fff; }
.card .tag { font-weight: 650; background: rgba(0, 0, 0, 0.55); border-radius: 5px; padding: 1px 6px; }
.card .tag.video::before { content: "▶ "; font-size: 10px; }
.card .noimg { width: 100%; height: 100%; display: grid; place-items: center; color: var(--muted); font-size: 13px; }
.lib-empty { text-align: center; padding: 60px 16px; display: flex; flex-direction: column; gap: 6px; align-items: center; }
.lib-empty p { margin: 0; max-width: 420px; }
.lib-more { display: flex; justify-content: center; padding: 16px; }

/* ---------- Lightbox ---------- */
dialog#lightbox { padding: 0; border: 0; background: #000; color: var(--text); width: 100vw; height: 100dvh; max-width: 100vw; max-height: 100dvh; margin: 0; }
dialog#lightbox::backdrop { background: rgba(0, 0, 0, 0.9); }
dialog#lightbox[open] { display: flex; flex-direction: column; }
.lb-media { flex: 1; min-height: 0; display: grid; place-items: center; }
.lb-media img, .lb-media video { max-width: 100%; max-height: 100%; object-fit: contain; display: block; outline: none; }
dialog#lightbox:focus, dialog#lightbox:focus-visible { outline: none; }
.lb-bar { display: flex; align-items: center; gap: 8px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); background: var(--bg-2); border-top: 1px solid var(--line); flex-wrap: wrap; }
.lb-info { flex: 1; display: flex; flex-direction: column; min-width: 140px; }

/* ---------- Toasts ---------- */
#toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 100; width: max-content; max-width: calc(100vw - 32px); }
.toast { background: var(--bg-3); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; font-size: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4); animation: toast-in 0.2s ease-out; }
.toast.error { border-color: rgba(255, 77, 79, 0.5); }
.toast.ok { border-color: rgba(61, 220, 132, 0.45); }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } }

/* ---------- Small screens ---------- */
@media (max-width: 860px) {
  .studio { grid-template-columns: 1fr; }
  .topbar { gap: 10px; }
  .topbar nav a { padding: 6px 8px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
}
@media (max-width: 520px) {
  .brand-word { display: none; }
  .topbar { gap: 6px; }
  .action { min-width: 0; flex: 1; padding: 12px 10px; }
  .hud .chip.mono { display: none; }
  .remote button, .hud-bottom button { padding: 7px 10px; font-size: 13px; }
}
