:root {
  --bg: #0e1116;
  --panel: #171b22;
  --panel2: #1e242d;
  --text: #e6e9ee;
  --muted: #9aa4b2;
  --accent: #4f8cff;
  --ok: #38c172;
  --err: #ff5c5c;
  --border: #2a313b;
}
* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
}
[hidden] { display: none !important; }
header {
  display: flex; align-items: center; gap: 16px;
  padding: 14px 20px; border-bottom: 1px solid var(--border);
}
h1 { font-size: 18px; margin: 0; letter-spacing: 1px; }
h2 { font-size: 17px; margin: 0; }
.tag {
  font-size: 12px; color: var(--accent);
  border: 1px solid var(--accent); border-radius: 10px;
  padding: 1px 8px; margin-left: 6px;
}
.header-toolbar { display: flex; gap: 8px; margin-left: auto; }
.header-toolbar button { padding: 6px 12px; font-size: 13px; }
.header-toolbar[hidden] + .status { margin-left: auto; }
.status { font-size: 13px; color: var(--muted); }
.status.ok { color: var(--ok); }
.status.err { color: var(--err); }
main { padding: 16px 20px; max-width: 1100px; margin: 0 auto; }
.panel {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px; margin-bottom: 14px;
}
.row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.grow { flex: 1; }
label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 4px; }
select, input {
  background: var(--panel2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 7px 9px; font-size: 14px;
}
input[readonly] { width: 100%; }
button {
  background: var(--panel2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 16px; font-size: 14px; cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { border-color: #814348; color: #ffb9b9; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
p { line-height: 1.6; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.stage {
  position: relative; background: #000;
  border: 1px solid var(--border); border-radius: 12px;
  overflow: hidden; aspect-ratio: 16 / 9;
}
.stage video { width: 100%; height: 100%; object-fit: contain; background: #000; display: block; }
.stats {
  display: none;
  color: #cfe; font-size: 12px; padding: 5px 9px;
  font-variant-numeric: tabular-nums; pointer-events: none;
}
.overlay {
  position: absolute; inset: 0; display: flex;
  align-items: center; justify-content: center;
  background: rgba(0, 0, 0, 0.78); cursor: pointer;
}
.overlay[hidden] { display: none; }
.overlay.passive { pointer-events: none; }

.watch { height: 100%; overflow: hidden; }
.watch-main { padding: 12px 16px; max-width: none; height: calc(100vh - 63px); height: calc(100dvh - 63px); }
.narrow { max-width: 440px; margin: 36px auto; }
.error { color: var(--err); }
.offline-card { text-align: center; color: var(--text); }
.offline-card p { color: var(--muted); }
.viewer-shell { display: grid; grid-template-columns: minmax(0, 1fr) minmax(280px, 340px); gap: 12px; height: 100%; }
.viewer-shell .stage { aspect-ratio: auto; height: 100%; min-height: 0; }
.chat-panel { min-height: 0; display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.chat-heading { display: flex; align-items: center; gap: 10px; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.chat-history { flex: 1; min-height: 0; overflow-y: auto; padding: 8px 12px; overscroll-behavior: contain; }
.chat-message { padding: 9px 2px; border-bottom: 1px solid rgba(255, 255, 255, 0.06); }
.chat-message-meta { display: flex; align-items: center; justify-content: space-between; gap: 12px; color: var(--accent); font-size: 12px; }
.chat-message-meta time { color: var(--muted); font-variant-numeric: tabular-nums; }
.chat-message p { margin: 4px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.5; }
.chat-composer { display: flex; gap: 8px; padding: 10px 12px max(10px, env(safe-area-inset-bottom)); border-top: 1px solid var(--border); }
.chat-composer input { min-width: 0; flex: 1; font-size: 16px; }
.chat-error { min-height: 18px; margin: 0; padding: 0 12px 8px; }
.stage:fullscreen, .stage:-webkit-full-screen { width: 100%; height: 100%; border: 0; border-radius: 0; }
.stage:fullscreen + .stats, .stage:-webkit-full-screen + .stats { display: none; }

@media (max-width: 760px) and (orientation: portrait) {
  header { padding: 10px 12px; gap: 8px; }
  h1 { font-size: 15px; }
  .tag { display: block; width: fit-content; margin: 3px 0 0; }
  .status { max-width: 42%; text-align: right; }
  .watch-main { padding: 8px; height: calc(100vh - 58px); height: calc(100dvh - 58px); }
  .viewer-shell { display: flex; flex-direction: column; gap: 8px; }
  .viewer-shell .stage { flex: 0 0 auto; width: 100%; height: auto; aspect-ratio: 8 / 5; }
  .stats { display: block; flex: 0 0 auto; width: 100%; margin-top: -4px; text-align: center; }
  .chat-panel { flex: 1; }
}

@media (orientation: landscape) and (max-height: 500px) and (max-width: 950px) {
  header { display: none; }
  .watch-main { height: 100vh; height: 100dvh; padding: 0; position: relative; }
  .viewer-shell { display: block; position: relative; }
  .viewer-shell .stage { width: 100%; height: 100%; border: 0; border-radius: 0; }
  .chat-panel { position: fixed; z-index: 10; left: max(8px, env(safe-area-inset-left)); right: max(8px, env(safe-area-inset-right)); bottom: max(8px, env(safe-area-inset-bottom)); min-height: auto; border: 0; background: transparent; overflow: visible; pointer-events: none; }
  .chat-heading, .chat-history, .chat-error { display: none; }
  .chat-composer { pointer-events: auto; max-width: 620px; margin: 0 auto; padding: 7px; border: 1px solid var(--border); border-radius: 10px; background: rgba(14, 17, 22, 0.9); }
}
