:root {
  --bg: #0e1013;
  --panel: #171a1f;
  --panel-2: #1f232a;
  --line: #2a2f37;
  --text: #e7e9ec;
  --muted: #8b929c;
  --accent: #19b26b;
  --accent-ink: #04140c;
  --danger: #e5484d;
  --live: #ff4d4f;
  --radius: 10px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-size: 14px; }
a { color: inherit; }
button, select, input {
  font: inherit; color: var(--text); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 11px;
}
button { cursor: pointer; }
button:hover:not(:disabled) { border-color: #3c434d; }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---- top bar ---- */
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 10px 16px;
  border-bottom: 1px solid var(--line); background: var(--panel); flex-wrap: wrap;
}
.brand { font-weight: 700; letter-spacing: .3px; display: flex; align-items: center; gap: 8px; }
.brand::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 10px var(--accent); }
.nav { display: flex; gap: 4px; }
.nav a { text-decoration: none; padding: 6px 12px; border-radius: 8px; color: var(--muted); }
.nav a.active, .nav a:hover { color: var(--text); background: var(--panel-2); }
.spacer { flex: 1; }
.controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.controls label { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 6px; }

/* ---- live ---- */
.live-wrap { height: calc(100% - 58px); display: flex; flex-direction: column; }
.stage {
  flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center;
  background: #000; position: relative;
}
.stage img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; }
.stage .placeholder { color: var(--muted); text-align: center; padding: 24px; }
.statusbar {
  display: flex; gap: 18px; padding: 7px 16px; font-size: 12px; color: var(--muted);
  border-top: 1px solid var(--line); background: var(--panel); flex-wrap: wrap;
}
.pill { display: inline-flex; align-items: center; gap: 6px; }
.pill.locked { margin-left: 8px; padding: 1px 8px; border-radius: 999px; background: rgba(229,72,77,.15); color: #e5484d; font-size: 12px; font-weight: 600; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.on { background: var(--accent); }
.dot.live { background: var(--live); animation: pulse 1.4s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* ---- history ---- */
.history { padding: 16px; max-width: 1600px; margin: 0 auto; }
.hour-group { margin-bottom: 22px; }
.hour-group h3 { font-size: 13px; color: var(--muted); font-weight: 600; margin: 0 0 8px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden; cursor: pointer; padding: 0; text-align: left;
}
.card:hover { border-color: var(--accent); }
.card img { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; display: block; background: #000; }
.card span { display: block; padding: 6px 9px; font-size: 12px; color: var(--muted); }
.empty { color: var(--muted); padding: 40px; text-align: center; }

.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,.92); display: none;
  flex-direction: column; z-index: 10;
}
.lightbox.open { display: flex; }
.lightbox .lb-bar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; color: var(--muted); }
.lightbox .lb-img { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 0 16px 16px; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lightbox .lb-scrub { display: flex; align-items: center; gap: 12px; padding: 0 16px 16px; }
.lightbox .lb-scrub input[type=range] { flex: 1; accent-color: var(--accent); cursor: pointer; height: 26px; min-width: 0; }
.lb-scrub-end { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; min-width: 52px; }
.lb-scrub-end:last-child { text-align: right; }

/* ---- login ---- */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100%; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 28px; display: flex; flex-direction: column; gap: 12px;
}
.login-card h1 { font-size: 18px; margin: 0 0 6px; }
.login-card input { width: 100%; padding: 10px 12px; }
.login-card .err { color: var(--danger); min-height: 18px; font-size: 13px; }

@media (max-width: 640px) {
  .topbar { gap: 10px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
}

/* ---- chung ---- */
.btn {
  display: inline-flex; align-items: center; text-decoration: none; padding: 6px 11px; border-radius: 8px;
  background: var(--accent); color: var(--accent-ink); font-weight: 600; font-size: 13px; border: 1px solid var(--accent);
}
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line); font-weight: 400; }
button.danger { color: var(--danger); border-color: #5a2629; background: transparent; }
button.danger:hover { background: #2a1416; }
.check { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; cursor: pointer; }
.summary { color: var(--muted); font-size: 12px; }
.warn { color: #f0aa28; }
.hint { color: var(--muted); margin: 0 0 12px; line-height: 1.5; }
.err { color: var(--danger); font-size: 13px; }
[hidden] { display: none !important; }

/* ---- dashboard ---- */
.dash { padding: 16px; display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); }
.dash[data-size="s"] { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
.dash[data-size="l"] { grid-template-columns: repeat(auto-fill, minmax(520px, 1fr)); }
.dash > .empty { grid-column: 1 / -1; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; display: flex; flex-direction: column; }
.tile.offline .tile-screen img { filter: grayscale(.85) brightness(.7); }
.tile-screen { position: relative; display: block; aspect-ratio: 16 / 9; background: #000; }
.tile-screen img { width: 100%; height: 100%; object-fit: contain; display: block; }
.tile-ph { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 12px; padding: 12px; text-align: center; }
.live-badge {
  position: absolute; top: 8px; left: 8px; background: var(--live); color: #fff; font-size: 10px; font-weight: 700;
  letter-spacing: .8px; padding: 2px 7px; border-radius: 4px;
}
.tile-body { padding: 10px 12px 12px; display: flex; flex-direction: column; gap: 6px; }
.tile-head { display: flex; align-items: center; gap: 8px; min-width: 0; }
.tile-head strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile-sub { color: var(--muted); font-size: 12px; font-family: ui-monospace, Consolas, monospace; }
.tile-line { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.tile-actions { display: flex; gap: 6px; margin-top: 2px; }

/* ---- quản lý máy ---- */
.page { padding: 16px; max-width: 1100px; margin: 0 auto; display: flex; flex-direction: column; gap: 16px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px; }
.panel h2 { font-size: 15px; margin: 0 0 8px; }
.add-form { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.add-form label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; }
.table-wrap { overflow-x: auto; }
table.devices { width: 100%; border-collapse: collapse; }
table.devices th { text-align: left; color: var(--muted); font-weight: 600; font-size: 12px; padding: 8px; border-bottom: 1px solid var(--line); }
table.devices td { padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
table.devices tr.offline td { color: var(--muted); }
.dev-name { display: flex; flex-direction: column; gap: 3px; }
.dev-name input { padding: 5px 8px; min-width: 180px; }
.dev-name input.saved { border-color: var(--accent); }
.dev-name code { font-size: 11px; color: var(--muted); }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }
.row-actions button, .row-actions .btn { font-size: 12px; padding: 5px 9px; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.7); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 20; }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 20px; width: 100%; max-width: 560px; display: flex; flex-direction: column; gap: 10px; }
.modal-card h2 { font-size: 16px; margin: 0; }
.copy-row { display: flex; gap: 8px; align-items: center; }
.copy-row code { flex: 1; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px; overflow-x: auto; white-space: nowrap; font-size: 13px; }
.copy-row code.token { color: var(--accent); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

@media (max-width: 640px) {
  .dash, .dash[data-size="l"], .dash[data-size="s"] { grid-template-columns: 1fr; padding: 12px; }
  .add-form > * { flex: 1 1 100%; }
  table.devices th:nth-child(3), table.devices td:nth-child(3), table.devices th:nth-child(4), table.devices td:nth-child(4) { display: none; }
}

/* ---- form / đăng nhập / khởi tạo ---- */
.login-logo { width: 56px; height: 56px; align-self: center; }
.login-card h1 { text-align: center; }
.setup-card { max-width: 420px; }
.field { display: flex; flex-direction: column; gap: 5px; color: var(--muted); font-size: 12px; }
.field input { width: 100%; padding: 10px 12px; font-size: 15px; }
.field small { color: #6b727c; font-size: 11px; }
.stack-form { display: flex; flex-direction: column; gap: 12px; max-width: 420px; }
.form-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.panel > button { margin-top: 4px; }

/* ---- zoom ảnh live / lightbox ---- */
.stage, .lightbox .lb-img { touch-action: none; overflow: hidden; position: relative; user-select: none; -webkit-user-select: none; }
.stage img, .lightbox .lb-img img { -webkit-user-drag: none; will-change: transform; }
.stage.zoomed, .lb-img.zoomed { cursor: grab; }

/* ---- điện thoại: thanh tab dưới đáy như app ---- */
@media (max-width: 760px) {
  body { padding-top: env(safe-area-inset-top); }
  .topbar { padding: 8px 12px; gap: 8px; }
  .brand { font-size: 15px; }
  .nav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
    background: var(--panel); border-top: 1px solid var(--line);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    justify-content: space-around; gap: 0;
  }
  .nav a { flex: 1; text-align: center; font-size: 12px; padding: 8px 2px; border-radius: 8px; }
  .nav a.active { color: var(--accent); background: transparent; font-weight: 600; }
  body:has(.nav) { padding-bottom: calc(58px + env(safe-area-inset-bottom)); }
  .spacer { display: none; }
  .controls { width: 100%; flex-wrap: nowrap; overflow-x: auto; padding-bottom: 2px; -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  .controls::-webkit-scrollbar { display: none; }
  .controls > * { flex: 0 0 auto; }
  .controls label { font-size: 11px; }
  .topbar #logout { display: none; }
  .live-wrap { height: calc(100dvh - 104px - 58px - env(safe-area-inset-bottom) - env(safe-area-inset-top)); }
  .statusbar { gap: 10px; padding: 6px 12px; }
  #sizeText, #lastText { display: none; }
  .history { padding: 12px; }
  .lightbox { padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
  .lightbox .lb-bar { flex-wrap: wrap; padding: 8px 12px; }
  .page { padding: 12px; }
  .login-page { min-height: 100dvh; }
}

/* Bảng máy → dạng thẻ trên điện thoại */
@media (max-width: 760px) {
  table.devices thead { display: none; }
  table.devices, table.devices tbody, table.devices tr, table.devices td { display: block; width: 100%; }
  table.devices tr { border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-bottom: 10px; }
  table.devices td { border: 0; padding: 4px 0; }
  table.devices td:nth-child(3), table.devices td:nth-child(4) { display: none; }
  .dev-name input { min-width: 0; width: 100%; }
  .row-actions { justify-content: flex-start; margin-top: 4px; }
}

.hint.center { text-align: center; margin-top: 2px; }
.hint.center a { color: var(--accent); }

/* ---- gói & thanh toán ---- */
.meter { height: 8px; background: var(--panel-2); border-radius: 5px; overflow: hidden; margin: 8px 0 6px; }
.meter-bar { height: 100%; width: 0; background: var(--accent); transition: width .3s; }
.meter-bar.full { background: var(--danger); }
.plans { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.plan-card { border: 1px solid var(--line); border-radius: 12px; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.plan-card.current { border-color: var(--accent); }
.plan-card h3 { margin: 0; font-size: 16px; }
.plan-price { color: var(--accent); font-weight: 600; }
.plan-feat { color: var(--muted); font-size: 13px; }
.plan-actions { margin-top: auto; padding-top: 10px; }
.plan-current { color: var(--accent); font-weight: 600; font-size: 13px; }

/* Trang Kiểm soát (rules) */
.page textarea { width: 100%; box-sizing: border-box; font: inherit; color: var(--text); background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; resize: vertical; }
.panel h3 { font-size: 13px; margin: 14px 0 4px; color: var(--text); }
.win-row { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); }
.win-row .days { display: flex; flex-wrap: wrap; gap: 8px; }
.day { display: inline-flex; align-items: center; gap: 3px; font-size: 12px; color: var(--muted); cursor: pointer; }
.win-row > label { font-size: 12px; color: var(--muted); display: inline-flex; gap: 4px; align-items: center; }
.win-del { padding: 4px 9px; }
.page .actions { display: flex; align-items: center; gap: 12px; margin-top: 4px; }

/* Banner thông báo (gating tính năng) */
.notice { margin: 0 0 12px; padding: 10px 14px; border-radius: 10px; background: rgba(240,170,40,.15); color: #f0aa28; font-size: 13px; }
.notice a { color: var(--accent); font-weight: 600; }
