:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --text: #0f1a14;
  --muted: #5f6f66;
  --border: #e3e8e5;
  --primary: #16a34a;
  --primary-strong: #12823b;
  --primary-soft: #e8f6ed;
  --danger: #d92d20;
  --danger-soft: #fdecea;
  --warn: #b45309;
  --warn-soft: #fdf3e3;
  --info: #2563eb;
  --info-soft: #e8effd;
  --neutral-soft: #eef1ef;
  --sidebar: #0d1b13;
  --sidebar-text: #b9c7bf;
  --sidebar-active: #1c3326;
  --chat-bg: #efeae2;
  --bubble: #d9fdd3;
  --shadow: 0 1px 2px rgba(16, 24, 20, .06), 0 1px 3px rgba(16, 24, 20, .08);
  --radius: 12px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b110e;
    --surface: #121a16;
    --surface-2: #17211c;
    --text: #e6efe9;
    --muted: #93a39a;
    --border: #24312a;
    --primary: #22c55e;
    --primary-strong: #16a34a;
    --primary-soft: #133222;
    --danger: #f87171;
    --danger-soft: #3a1717;
    --warn: #fbbf24;
    --warn-soft: #3a2c10;
    --info: #60a5fa;
    --info-soft: #14243f;
    --neutral-soft: #1d2722;
    --sidebar: #080e0b;
    --sidebar-active: #15261d;
    --chat-bg: #0f1914;
    --bubble: #1f4d33;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 20px; }
h2 { font-size: 16px; }
h3 { font-size: 14px; }
code, pre, .mono { font-family: var(--mono); font-size: 12.5px; }
svg.i { width: 18px; height: 18px; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; flex: none; background: var(--sidebar); color: var(--sidebar-text);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 20px 20px 16px; color: #fff; font-weight: 700; font-size: 16px; }
.brand .logo { width: 32px; height: 32px; border-radius: 9px; background: var(--primary); color: #fff; display: grid; place-items: center; }
.brand small { display: block; font-weight: 500; font-size: 11px; color: var(--sidebar-text); }
.nav { padding: 8px 12px; display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 8px;
  color: var(--sidebar-text); font-weight: 500; text-decoration: none;
}
.nav a:hover { background: var(--sidebar-active); color: #fff; }
.nav a.active { background: var(--sidebar-active); color: #fff; box-shadow: inset 3px 0 0 var(--primary); }
.sidebar-foot { padding: 16px 20px; font-size: 12px; border-top: 1px solid rgba(255,255,255,.06); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 60px; display: flex; align-items: center; gap: 12px; padding: 0 24px;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5;
}
.topbar h1 { flex: 1; font-size: 17px; }
.btn.menu-btn { display: none; }
.user { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.content { padding: 24px; max-width: 1240px; width: 100%; margin: 0 auto; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--muted); }

/* ---------- surfaces ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 20px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-devices { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.split { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.between { justify-content: space-between; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mt { margin-top: 16px; }
.hidden { display: none !important; }

.stat { padding: 18px 20px; }
.stat .label { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 8px; }
.stat .value { font-size: 28px; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; }
.stat .value small { font-size: 15px; color: var(--muted); font-weight: 500; }
.stat .ico { width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); }
.stat .ico.red { background: var(--danger-soft); color: var(--danger); }
.stat .ico.blue { background: var(--info-soft); color: var(--info); }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 38px; padding: 0 14px;
  border-radius: 8px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; text-decoration: none;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-strong); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { height: 32px; padding: 0 10px; font-size: 13px; }
.btn.icon { width: 38px; padding: 0; }
.btn.sm.icon { width: 32px; }

label.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 13px; }
label.field .hint, .hint { font-weight: 400; color: var(--muted); font-size: 12px; }
input[type=text], input[type=password], input[type=url], input[type=search], select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 12px; outline: none;
}
textarea { resize: vertical; min-height: 120px; }
textarea.mono { font-family: var(--mono); font-size: 13px; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.check { display: flex; gap: 10px; align-items: flex-start; font-weight: 500; cursor: pointer; }
.check input { margin-top: 3px; accent-color: var(--primary); width: 16px; height: 16px; }
.check small { display: block; color: var(--muted); font-weight: 400; }
.form { display: flex; flex-direction: column; gap: 16px; }

/* ---------- badges ---------- */
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.green { background: var(--primary-soft); color: var(--primary); }
.badge.amber { background: var(--warn-soft); color: var(--warn); }
.badge.blue { background: var(--info-soft); color: var(--info); }
.badge.red { background: var(--danger-soft); color: var(--danger); }
.badge.gray { background: var(--neutral-soft); color: var(--muted); }
.badge.pulse::before { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .25; } }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 16px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
tr:last-child td { border-bottom: none; }
td.msg { max-width: 340px; color: var(--muted); }
td.msg div { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-line; }
.nowrap { white-space: nowrap; }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.empty svg.i { width: 36px; height: 36px; opacity: .5; margin-bottom: 8px; }

.progress { height: 8px; border-radius: 999px; background: var(--neutral-soft); overflow: hidden; display: flex; min-width: 120px; }
.progress span { display: block; height: 100%; }
.progress .ok { background: var(--primary); }
.progress .bad { background: var(--danger); }
.progress .skip { background: var(--muted); opacity: .4; }

/* ---------- device cards ---------- */
.device { padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.device-top { display: flex; gap: 12px; align-items: flex-start; }
.avatar { width: 44px; height: 44px; border-radius: 12px; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; flex: none; }
.avatar.off { background: var(--neutral-soft); color: var(--muted); }
.device-name { font-weight: 700; font-size: 15px; word-break: break-word; }
.device-meta { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 13px; }
.device-meta dt { color: var(--muted); }
.device-meta dd { margin: 0; word-break: break-all; }
.device-actions { display: flex; gap: 8px; flex-wrap: wrap; border-top: 1px solid var(--border); padding-top: 14px; }

/* ---------- modal ---------- */
.overlay { position: fixed; inset: 0; background: rgba(5, 12, 8, .55); display: grid; place-items: center; padding: 16px; z-index: 50; animation: fade .15s ease; }
.modal { background: var(--surface); border-radius: 14px; width: 100%; max-width: 520px; max-height: calc(100vh - 32px); overflow: auto; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal.wide { max-width: 860px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.modal-body { padding: 20px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
@keyframes fade { from { opacity: 0; } }

.qr-box { display: grid; grid-template-columns: 240px 1fr; gap: 20px; align-items: center; }
.qr-frame { width: 240px; height: 240px; border-radius: 12px; border: 1px solid var(--border); background: #fff; display: grid; place-items: center; overflow: hidden; }
.qr-frame img { width: 100%; height: 100%; image-rendering: pixelated; }
.steps { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 8px; }
.center-state { text-align: center; padding: 24px 8px; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.big-ico { width: 64px; height: 64px; border-radius: 50%; display: grid; place-items: center; background: var(--primary-soft); color: var(--primary); }
.big-ico svg.i { width: 32px; height: 32px; }
.big-ico.red { background: var(--danger-soft); color: var(--danger); }
.spinner { width: 36px; height: 36px; border-radius: 50%; border: 3px solid var(--neutral-soft); border-top-color: var(--primary); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- chat preview ---------- */
.phone { background: var(--chat-bg); border-radius: 12px; padding: 16px; min-height: 220px; display: flex; flex-direction: column; gap: 6px; }
.bubble { align-self: flex-end; max-width: 88%; background: var(--bubble); color: var(--text); padding: 7px 10px 6px; border-radius: 8px 0 8px 8px; box-shadow: 0 1px 1px rgba(0,0,0,.08); white-space: pre-wrap; word-break: break-word; }
.bubble time { display: block; text-align: right; font-size: 11px; color: var(--muted); margin-top: 2px; }
.chat-to { font-size: 12px; color: var(--muted); margin-bottom: 6px; }

/* ---------- code / docs ---------- */
pre.code { margin: 0; background: #0d1512; color: #d6e5dc; padding: 14px 16px; border-radius: 10px; overflow-x: auto; line-height: 1.55; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 12px; overflow-x: auto; }
.tabs button { border: none; background: none; font: inherit; font-weight: 600; color: var(--muted); padding: 8px 12px; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); }
.endpoint { display: flex; align-items: center; gap: 10px; font-family: var(--mono); font-size: 13px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; overflow-x: auto; }
.method { font-weight: 700; font-size: 11px; padding: 2px 8px; border-radius: 4px; background: var(--primary-soft); color: var(--primary); }
.method.get { background: var(--info-soft); color: var(--info); }
.token-box { display: flex; gap: 8px; }
.token-box input { font-family: var(--mono); }
.alert { padding: 12px 14px; border-radius: 10px; background: var(--warn-soft); color: var(--warn); display: flex; gap: 10px; align-items: flex-start; }
.alert.info { background: var(--info-soft); color: var(--info); }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; background:
  radial-gradient(1200px 600px at 10% -10%, var(--primary-soft), transparent), var(--bg); }
.login { width: 100%; max-width: 380px; padding: 28px; }
.login .brand { padding: 0 0 20px; color: var(--text); }
.login .brand small { color: var(--muted); }
.error-text { color: var(--danger); font-size: 13px; min-height: 18px; }

/* ---------- toasts ---------- */
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: var(--text); color: var(--surface); padding: 10px 14px; border-radius: 10px; box-shadow: var(--shadow); max-width: 360px; animation: fade .15s ease; }
.toast.err { background: var(--danger); color: #fff; }

.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 16px; border-top: 1px solid var(--border); }
.filters { display: flex; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.filters > * { width: auto; flex: 1 1 160px; }

/* ---------- responsive ---------- */
@media (max-width: 1024px) {
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .split, .grid-2 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 760px) {
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 40; transform: translateX(-100%); transition: transform .2s ease; }
  .sidebar.open { transform: none; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 39; }
  .btn.menu-btn { display: inline-flex; }
  .topbar { padding: 0 16px; }
  .content { padding: 16px; }
  .user .name { display: none; }
  .qr-box { grid-template-columns: minmax(0, 1fr); justify-items: center; }
  .grid-4 { grid-template-columns: minmax(0, 1fr); }
  th, td { padding: 10px 12px; }
}

/* ---------- branding / admin ---------- */
.brand .logo.img { background: transparent; overflow: hidden; }
.brand .logo img { width: 100%; height: 100%; object-fit: contain; }
.brand-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.nav-sep { margin: 14px 12px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--sidebar-text); opacity: .6; }
.user .name { display: inline-flex; align-items: center; gap: 8px; }
.logo-preview { width: 72px; height: 72px; flex: none; border-radius: 14px; border: 1px dashed var(--border); background: var(--surface-2);
  display: grid; place-items: center; overflow: hidden; }
.logo-preview img { width: 100%; height: 100%; object-fit: contain; }
.preview-side { background: var(--sidebar); border-radius: 10px; }
.preview-side .brand { padding: 16px 18px; }
.preview-login { border: 1px solid var(--border); border-radius: 10px; }
.preview-login .brand { padding: 16px 18px; color: var(--text); }
.preview-login .brand small { color: var(--muted); }
.empty .btn svg.i { width: 18px; height: 18px; margin: 0; opacity: 1; }
