:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f232c;
  --line: #2a2f3a;
  --text: #e6e8ec;
  --dim: #8b93a3;
  --accent: #4c8dff;
  --accent-2: #2f6fe0;
  --danger: #e5484d;
  --ok: #46a758;
  --radius: 12px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  overscroll-behavior: none;
}

body { -webkit-tap-highlight-color: transparent; }

.screen { position: fixed; inset: 0; }
.hidden { display: none !important; }

/* ------------------------------------------------------------------- login */

#login {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow-y: auto;
}

.card {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.card h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 650;
  letter-spacing: -0.3px;
}

.card .sub {
  margin: -10px 0 4px;
  color: var(--dim);
  font-size: 13px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--dim);
}

label > span { font-weight: 500; }

input[type=text], input[type=password], select {
  width: 100%;
  padding: 11px 12px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font-size: 16px; /* >=16px de iOS khong tu zoom khi focus */
  font-family: inherit;
  appearance: none;
}

select {
  background-image: linear-gradient(45deg, transparent 50%, var(--dim) 50%),
                    linear-gradient(135deg, var(--dim) 50%, transparent 50%);
  background-position: right 16px center, right 11px center;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
}

.row { display: flex; gap: 10px; }
.grow { flex: 1 1 0; min-width: 0; }

.check {
  flex-direction: row;
  align-items: center;
  gap: 9px;
  color: var(--text);
  font-size: 14px;
}

.check input { width: 18px; height: 18px; accent-color: var(--accent); }

#f-connect {
  margin-top: 4px;
  padding: 13px;
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: var(--radius);
  font: inherit;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}

#f-connect:active { background: var(--accent-2); }
#f-connect:disabled { opacity: 0.55; cursor: default; }

.msg { margin: 0; font-size: 13px; min-height: 18px; }
.msg.err { color: var(--danger); }
.msg.ok { color: var(--ok); }
.hint { margin: 0; font-size: 12px; color: var(--dim); }

/* ------------------------------------------------------------------ viewer */

#stage-wrap {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: #000;
  touch-action: none;
}

/* Vi tri va kich thuoc do JS quan ly hoan toan qua transform (goc 0,0) */
#stage {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  transform-origin: 0 0;
  will-change: transform;
}

#video, #canvas {
  display: block;
  width: 100%;
  height: auto;
  background: #000;
}

#canvas { display: none; }
#stage.mjpeg #video { display: none; }
#stage.mjpeg #canvas { display: block; }

#hud {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0px) + 8px);
  left: 8px;
  right: 8px;
  display: flex;
  gap: 6px;
  pointer-events: none;
  flex-wrap: wrap;
}

.pill {
  background: rgba(15, 17, 21, 0.78);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  backdrop-filter: blur(6px);
}

.pill.dim { color: var(--dim); }
.pill.err { color: var(--danger); border-color: var(--danger); }
.pill.ok { color: var(--ok); }

#toolbar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  gap: 6px;
  padding: 8px 8px calc(8px + var(--safe-b));
  background: linear-gradient(to top, rgba(15, 17, 21, 0.95), rgba(15, 17, 21, 0));
}

.tool {
  flex: 1 1 0;
  min-width: 0;
  padding: 11px 6px;
  background: rgba(31, 35, 44, 0.92);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tool:active { background: var(--accent); border-color: var(--accent); }
.tool.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.tool.danger:active { background: var(--danger); border-color: var(--danger); }

#keys {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(56px + var(--safe-b));
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  padding: 8px;
  background: rgba(15, 17, 21, 0.95);
  border-top: 1px solid var(--line);
}

#keys button {
  flex: 0 0 auto;
  min-width: 44px;
  padding: 9px 10px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

#keys button:active { background: var(--accent); border-color: var(--accent); }
#keys button.mod.on { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Input de goi ban phim he thong len, khong can nhin thay */
#typer {
  position: absolute;
  bottom: calc(56px + var(--safe-b));
  left: 8px;
  right: 8px;
  opacity: 0;
  pointer-events: none;
  height: 1px;
  padding: 0;
  border: 0;
}

#typer.show {
  opacity: 1;
  pointer-events: auto;
  height: auto;
  padding: 11px 12px;
  border: 1px solid var(--accent);
}

/* --------------------------------------------------------------- panel AI */

#ai {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(52px + var(--safe-b));
  max-height: min(70vh, 560px);
  display: flex;
  flex-direction: column;
  background: rgba(15, 17, 21, 0.97);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(10px);
}

#ai-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

#ai-title {
  font-size: 13px;
  font-weight: 600;
  margin-right: auto;
}

#ai-model {
  width: auto;
  max-width: 46%;
  padding: 5px 24px 5px 8px;
  font-size: 12px;
  border-radius: 8px;
  background-position: right 9px center, right 5px center;
}

#ai-head button {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  padding: 0;
  background: var(--panel-2);
  color: var(--dim);
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

#ai-head button:active { background: var(--accent); color: #fff; }

#ai-power {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  background: rgba(229, 72, 77, 0.16);
  border-bottom: 1px solid var(--danger);
  font-size: 13px;
}

#ai-power-text { flex: 1 1 auto; color: var(--danger); font-weight: 600; }

#ai-power-cancel {
  flex: 0 0 auto;
  padding: 7px 14px;
  background: var(--danger);
  color: #fff;
  border: 0;
  border-radius: 8px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

#ai-log {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 14px;
  line-height: 1.5;
}

.ai-msg {
  max-width: 88%;
  padding: 8px 11px;
  border-radius: 12px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.ai-msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.ai-msg.bot {
  align-self: flex-start;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}

.ai-msg.err {
  align-self: flex-start;
  background: rgba(229, 72, 77, 0.14);
  border: 1px solid var(--danger);
  color: var(--danger);
  font-size: 13px;
}

.ai-msg code {
  background: rgba(255, 255, 255, 0.09);
  padding: 1px 5px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}

.ai-msg pre {
  margin: 6px 0 0;
  padding: 8px 10px;
  background: #0b0d11;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow-x: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.45;
}

.ai-msg pre code { background: none; padding: 0; }

/* Chip cho moi lan goi tool */
.ai-tool {
  align-self: flex-start;
  max-width: 94%;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 6px 10px;
  background: rgba(76, 141, 255, 0.08);
  border: 1px solid rgba(76, 141, 255, 0.3);
  border-radius: 10px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.ai-tool.risk { background: rgba(229, 72, 77, 0.1); border-color: rgba(229, 72, 77, 0.4); }
.ai-tool.err { background: rgba(229, 72, 77, 0.14); border-color: var(--danger); }
.ai-tool .n { font-weight: 600; }
.ai-tool .d { color: var(--dim); white-space: pre-wrap; overflow-wrap: anywhere; }

.ai-think {
  align-self: flex-start;
  color: var(--dim);
  font-size: 12px;
  padding: 2px 4px;
}

.ai-think::after {
  content: '';
  animation: dots 1.4s steps(4, end) infinite;
}

@keyframes dots {
  0% { content: ''; }
  25% { content: '.'; }
  50% { content: '..'; }
  75% { content: '...'; }
}

#ai-form {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 7px;
  padding: 8px 10px;
  border-top: 1px solid var(--line);
}

#ai-input {
  flex: 1 1 auto;
  min-width: 0;
  max-height: 120px;
  padding: 10px 12px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  font: inherit;
  font-size: 16px; /* >=16px de iOS khong tu zoom */
  font-family: inherit;
  resize: none;
  overflow-y: auto;
}

#ai-input:focus { outline: none; border-color: var(--accent); }

#ai-form button {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: 12px;
  font: inherit;
  font-size: 17px;
  cursor: pointer;
}

#ai-send { background: var(--accent); color: #fff; }
#ai-send:disabled { opacity: 0.45; }
#ai-stop { background: var(--danger); color: #fff; }

#toast {
  position: absolute;
  left: 50%;
  bottom: calc(70px + var(--safe-b));
  transform: translateX(-50%);
  background: rgba(15, 17, 21, 0.95);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 13px;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
  white-space: nowrap;
  max-width: 90vw;
  overflow: hidden;
  text-overflow: ellipsis;
}

#toast.show { opacity: 1; }

@media (min-width: 720px) {
  .tool { flex: 0 0 auto; padding: 10px 16px; }
  #toolbar { justify-content: center; }
}
