* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #0a0b0f;
  --panel: #12141d;
  --panel2: #1a1d29;
  --line: rgba(99, 102, 241, 0.2);
  --line2: rgba(75, 85, 99, 0.35);
  --text: #e5e7eb;
  --muted: #9ca3af;
  --dim: #6b7280;
  --accent: #6366f1;
  --accent2: #8b5cf6;
  --ok: #10b981;
  --warn: #fbbf24;
  --bad: #ef4444;
  --ai: #a855f7;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Inter', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

[hidden] { display: none !important; }
a { color: #a5b4fc; }
.muted { color: var(--muted); font-size: 13px; }
.hint { color: var(--muted); font-size: 12px; margin-top: 6px; }
.hint-inline { color: var(--dim); font-size: 11px; font-weight: 400; }

/* ---------- auth ---------- */
.auth-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background:
    radial-gradient(ellipse at 20% 0%, rgba(99,102,241,.12), transparent 55%),
    radial-gradient(ellipse at 90% 100%, rgba(168,85,247,.1), transparent 55%),
    var(--bg);
}
.auth-card {
  width: 100%; max-width: 400px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 34px 30px;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.auth-logo { font-size: 26px; font-weight: 800; }
.auth-logo span { background: linear-gradient(135deg, var(--accent), var(--ai)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.auth-sub { color: var(--muted); font-size: 13px; margin: 4px 0 24px; }
.auth-form h2 { font-size: 17px; margin-bottom: 14px; }
.auth-form input {
  width: 100%; padding: 11px 13px; margin-bottom: 10px;
  background: rgba(31,41,55,.8); border: 1px solid var(--line2);
  border-radius: 8px; color: var(--text); font-size: 14px;
}
.auth-form input:focus { outline: none; border-color: var(--accent); }
.auth-links { display: flex; justify-content: space-between; margin-top: 12px; font-size: 13px; }
.auth-msg { margin-top: 14px; padding: 10px 12px; border-radius: 8px; font-size: 13px; background: rgba(99,102,241,.12); border: 1px solid var(--line); }
.auth-msg.error { background: rgba(239,68,68,.12); border-color: rgba(239,68,68,.35); color: #fca5a5; }
.auth-msg.ok { background: rgba(16,185,129,.12); border-color: rgba(16,185,129,.35); color: #34d399; }

/* ---------- layout ---------- */
.app-container { display: flex; flex-direction: column; height: 100vh; max-width: 1920px; margin: 0 auto; }

.header {
  background: linear-gradient(135deg, var(--panel2) 0%, var(--panel) 100%);
  border-bottom: 1px solid var(--line);
  padding: 12px 20px;
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  flex-wrap: wrap;
}
.header-brand { display: flex; align-items: center; gap: 10px; }
.brand-icon {
  width: 34px; height: 34px; border-radius: 8px;
  background: linear-gradient(135deg, var(--accent), var(--ai));
  display: flex; align-items: center; justify-content: center; font-size: 18px;
}
.brand-title { font-size: 19px; font-weight: 700; }
.brand-title span { background: linear-gradient(135deg, var(--accent), var(--ai)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
.header-status { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.status-item {
  display: flex; align-items: center; gap: 7px;
  padding: 5px 11px; font-size: 12.5px;
  background: rgba(31,41,55,.6); border: 1px solid var(--line2); border-radius: 8px;
}
.account-chip { cursor: pointer; }
.account-chip:hover { border-color: var(--accent); }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--dim); }
.status-dot.on { background: var(--ok); box-shadow: 0 0 8px rgba(16,185,129,.6); }
.status-dot.rec { background: var(--bad); animation: pulse 1.5s ease-in-out infinite; }
.status-dot.warn { background: var(--warn); }

.banner {
  background: rgba(251,191,36,.1); border-bottom: 1px solid rgba(251,191,36,.3);
  color: #fde68a; padding: 8px 20px; font-size: 13px;
}
.btn-link { background: none; border: none; color: #a5b4fc; cursor: pointer; font-size: 13px; text-decoration: underline; }

.main-layout {
  display: grid; grid-template-columns: 1fr 400px;
  flex: 1; min-height: 0;
}

/* ---------- video ---------- */
.video-section { background: #000; position: relative; display: flex; flex-direction: column; min-width: 0; }
.video-header {
  position: absolute; top: 0; left: 0; right: 0; z-index: 10;
  padding: 14px 16px;
  background: linear-gradient(180deg, rgba(0,0,0,.75), transparent);
  display: flex; justify-content: space-between; align-items: center;
}
.video-title { font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.live-badge { background: var(--bad); color: #fff; padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: 700; animation: pulse 2s ease-in-out infinite; }
.rec-badge { background: var(--ai); color: #fff; padding: 2px 8px; border-radius: 4px; font-size: 11px; font-weight: 700; animation: pulse 1s ease-in-out infinite; }
.video-actions { display: flex; gap: 8px; }
.icon-btn {
  background: rgba(17,24,39,.9); border: 1px solid rgba(99,102,241,.3);
  color: var(--text); padding: 7px 11px; border-radius: 6px;
  cursor: pointer; font-size: 12px; font-weight: 500;
}
.icon-btn:hover { background: rgba(99,102,241,.2); border-color: var(--accent); }
.icon-btn.active { background: rgba(99,102,241,.35); border-color: var(--accent); }

.video-container { flex: 1; position: relative; display: flex; align-items: center; justify-content: center; min-height: 0; }
#videoCanvas { max-width: 100%; max-height: 100%; }
#videoCanvas.draw-mode { cursor: crosshair; }

.ai-processing-badge {
  position: absolute; top: 56px; left: 16px; z-index: 10;
  background: rgba(168,85,247,.9); color: #fff;
  padding: 7px 14px; border-radius: 6px; font-size: 12px; font-weight: 600;
  display: flex; align-items: center; gap: 8px;
  animation: pulse 1.5s ease-in-out infinite;
}
.spinner { width: 12px; height: 12px; border: 2px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin 1s linear infinite; }

.video-overlay {
  position: absolute; bottom: 0; left: 0; right: 0;
  padding: 16px;
  background: linear-gradient(0deg, rgba(0,0,0,.8), transparent);
  display: flex; justify-content: space-between; align-items: flex-end; gap: 16px;
}
.motion-meter { flex: 1; max-width: 420px; }
.meter-label { font-size: 11px; color: var(--muted); margin-bottom: 5px; display: flex; justify-content: space-between; }
.meter-value { font-size: 20px; font-weight: 700; color: var(--warn); }
.meter-bar { height: 8px; background: rgba(31,41,55,.8); border: 1px solid rgba(75,85,99,.5); border-radius: 4px; overflow: visible; position: relative; }
.meter-fill { height: 100%; border-radius: 3px; background: linear-gradient(90deg, var(--ok), var(--warn), var(--bad)); width: 0%; transition: width .2s ease; }
.ai-trigger-marker { position: absolute; top: -2px; height: calc(100% + 4px); width: 2px; background: var(--ai); z-index: 1; }
.ai-trigger-marker::before { content: 'AI'; position: absolute; top: -15px; left: -6px; font-size: 9px; font-weight: 700; color: var(--ai); }
.quick-stats { display: flex; gap: 10px; }
.stat-card { background: rgba(17,24,39,.8); border: 1px solid var(--line2); padding: 6px 12px; border-radius: 6px; text-align: center; }
.stat-value { font-size: 18px; font-weight: 700; }
.stat-label { font-size: 10px; color: var(--muted); }

/* ---------- panel ---------- */
.control-panel { background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.panel-tabs { display: flex; background: var(--panel2); border-bottom: 1px solid var(--line); overflow-x: auto; }
.tab-btn {
  flex: 1; padding: 11px 8px; background: transparent; border: none;
  color: var(--muted); font-size: 12.5px; font-weight: 600; cursor: pointer;
  border-bottom: 2px solid transparent; white-space: nowrap;
}
.tab-btn:hover { color: var(--text); background: rgba(99,102,241,.05); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); background: rgba(99,102,241,.1); }
.panel-content { flex: 1; overflow-y: auto; padding: 18px; }
.tab-pane { display: none; }
.tab-pane.active { display: block; }
.control-group { margin-bottom: 24px; }
.control-label {
  font-size: 11.5px; font-weight: 600; color: var(--muted);
  margin-bottom: 8px; display: block; text-transform: uppercase; letter-spacing: .5px;
}

.btn {
  width: 100%; padding: 11px 14px; border: none; border-radius: 7px;
  font-size: 13.5px; font-weight: 600; cursor: pointer; margin-bottom: 8px;
  transition: transform .15s, box-shadow .15s;
}
.btn-primary { background: linear-gradient(135deg, var(--accent), var(--accent2)); color: #fff; }
.btn-primary:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 6px 18px rgba(99,102,241,.4); }
.btn-danger { background: linear-gradient(135deg, #ef4444, #dc2626); color: #fff; }
.btn-success { background: linear-gradient(135deg, #10b981, #059669); color: #fff; }
.btn-secondary { background: rgba(75,85,99,.5); color: var(--text); border: 1px solid rgba(99,102,241,.3); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-group { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.slider-control { margin-bottom: 14px; }
.slider-header { display: flex; justify-content: space-between; margin-bottom: 6px; }
.slider-label { font-size: 13px; color: #d1d5db; }
.slider-value { font-size: 12.5px; font-weight: 600; color: var(--accent); background: rgba(99,102,241,.1); padding: 2px 8px; border-radius: 4px; }
input[type="range"] { width: 100%; height: 6px; -webkit-appearance: none; appearance: none; background: rgba(75,85,99,.5); border-radius: 3px; outline: none; }
input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; background: linear-gradient(135deg, var(--accent), var(--accent2)); border-radius: 50%; cursor: pointer; }
input[type="range"]::-moz-range-thumb { width: 16px; height: 16px; background: var(--accent); border-radius: 50%; cursor: pointer; border: none; }

select, input[type="text"], input[type="url"], input[type="email"], input[type="password"], input[type="time"] {
  width: 100%; padding: 9px 11px; margin-bottom: 8px;
  background: rgba(31,41,55,.8); border: 1px solid var(--line2);
  border-radius: 6px; color: var(--text); font-size: 13px;
}
select:focus, input:focus { outline: none; border-color: var(--accent); }
.inline-select, .time-input { width: auto; margin-bottom: 0; padding: 5px 8px; font-size: 12.5px; }
.model-list { height: auto; font-family: inherit; }
.model-list option { padding: 4px 6px; }

.toggle-switch { display: flex; align-items: center; gap: 10px; padding: 7px 0; font-size: 13.5px; flex-wrap: wrap; }
.switch { position: relative; width: 44px; height: 24px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch-slider { position: absolute; inset: 0; background: rgba(75,85,99,.5); border-radius: 24px; cursor: pointer; transition: .3s; }
.switch-slider:before { content: ""; position: absolute; height: 16px; width: 16px; left: 4px; bottom: 4px; background: #fff; border-radius: 50%; transition: .3s; }
.switch input:checked + .switch-slider { background: linear-gradient(135deg, var(--accent), var(--accent2)); }
.switch input:checked + .switch-slider:before { transform: translateX(20px); }

.info-box { background: rgba(99,102,241,.08); border: 1px solid var(--line); border-radius: 6px; padding: 11px; font-size: 12px; color: var(--muted); margin-bottom: 10px; }
.region-list { background: rgba(31,41,55,.5); border: 1px solid var(--line2); border-radius: 6px; padding: 10px; margin-bottom: 8px; max-height: 180px; overflow-y: auto; }
.region-item { display: flex; justify-content: space-between; align-items: center; padding: 7px 8px; background: rgba(17,24,39,.6); border-radius: 4px; margin-bottom: 6px; font-size: 12px; }
.region-item:last-child { margin-bottom: 0; }
.region-delete { background: rgba(239,68,68,.2); border: 1px solid rgba(239,68,68,.3); color: var(--bad); padding: 3px 8px; border-radius: 4px; font-size: 11px; cursor: pointer; }

.usage-bar-wrap { margin: 10px 0; }
.usage-bar { height: 7px; background: rgba(31,41,55,.8); border-radius: 4px; overflow: hidden; border: 1px solid var(--line2); }
.usage-fill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--accent), var(--accent2)); transition: width .3s; }
.usage-fill.hot { background: linear-gradient(90deg, var(--warn), var(--bad)); }

.kv { display: flex; justify-content: space-between; font-size: 13px; padding: 6px 0; border-bottom: 1px dashed rgba(75,85,99,.3); }
.kv span { color: var(--muted); }

.log-container {
  background: rgba(0,0,0,.5); border: 1px solid var(--line2); border-radius: 6px;
  padding: 10px; height: 220px; overflow-y: auto;
  font-family: 'Courier New', monospace; font-size: 11px;
}
.log-entry { padding: 5px 8px; margin: 3px 0; border-radius: 3px; background: rgba(31,41,55,.5); border-left: 2px solid var(--accent); word-break: break-word; }
.log-entry.ai { border-left-color: var(--ai); background: rgba(168,85,247,.1); color: #e879f9; }
.log-entry.alert { border-left-color: var(--ok); background: rgba(16,185,129,.1); color: #34d399; }
.log-entry.error { border-left-color: var(--bad); background: rgba(239,68,68,.1); color: #fca5a5; }

/* ---------- history ---------- */
.history-filters { display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.history-filters input { flex: 1; min-width: 120px; margin-bottom: 0; }
.history-grid { display: grid; grid-template-columns: 1fr; gap: 12px; margin: 10px 0; }
.history-card { background: rgba(17,24,39,.8); border: 1px solid var(--line2); border-radius: 8px; overflow: hidden; }
.history-card:hover { border-color: rgba(99,102,241,.5); }
.history-image { width: 100%; max-height: 200px; object-fit: cover; display: block; cursor: pointer; }
.history-info { padding: 10px 12px; }
.history-meta { font-size: 11px; color: var(--muted); display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.history-meta .lvl { font-weight: 700; }
.lvl-HIGH { color: var(--bad); } .lvl-MEDIUM { color: var(--warn); } .lvl-LOW { color: var(--ok); }
.history-description { font-size: 12px; color: #d1d5db; margin-top: 8px; background: rgba(168,85,247,.1); padding: 8px; border-radius: 4px; border-left: 2px solid var(--ai); }
.history-actions { display: flex; gap: 8px; margin-top: 8px; }
.history-actions button { flex: 1; font-size: 11px; padding: 5px; border-radius: 4px; cursor: pointer; background: rgba(75,85,99,.4); color: var(--text); border: 1px solid var(--line2); }
.history-actions button.del { color: var(--bad); border-color: rgba(239,68,68,.3); }
.empty-state { text-align: center; padding: 40px 16px; color: var(--dim); font-size: 13px; }

.admin-users { font-size: 12px; }
.admin-user { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 7px 8px; background: rgba(17,24,39,.6); border-radius: 4px; margin-bottom: 6px; flex-wrap: wrap; }
.admin-user .em { overflow: hidden; text-overflow: ellipsis; }
.admin-user select { width: auto; margin: 0; padding: 3px 6px; font-size: 12px; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--panel2); border: 1px solid var(--line); color: var(--text);
  padding: 11px 18px; border-radius: 10px; font-size: 13.5px; z-index: 2000;
  box-shadow: 0 10px 30px rgba(0,0,0,.5); max-width: 90vw;
}
.toast.error { border-color: rgba(239,68,68,.5); color: #fca5a5; }
.toast.ok { border-color: rgba(16,185,129,.5); color: #34d399; }

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .55; } }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .main-layout { grid-template-columns: 1fr; grid-template-rows: minmax(45vh, 1fr) auto; overflow-y: auto; }
  .control-panel { border-left: none; border-top: 1px solid var(--line); max-height: none; }
  .panel-content { max-height: 60vh; }
}
@media (max-width: 640px) {
  .header { padding: 10px 12px; }
  .header-status { gap: 6px; }
  .status-item { padding: 4px 8px; font-size: 11px; }
  .btn-group { grid-template-columns: 1fr; }
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: rgba(31,41,55,.5); }
::-webkit-scrollbar-thumb { background: rgba(99,102,241,.5); border-radius: 4px; }
