:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #16191d;
  --muted: #5d6673;
  --accent: #2357d9;
  --accent-text: #ffffff;
  --on: #12a150;
  --off: #c2410c;
  --warn-bg: #fff4d6;
  --warn-text: #7a5300;
  --danger: #c42b2b;
  --shadow: 0 10px 30px rgb(15 23 42 / 0.18);
  --radius: 10px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1f252d;
    --border: #2c333d;
    --text: #e7eaee;
    --muted: #97a1ad;
    --accent: #6d95ff;
    --accent-text: #0b0e12;
    --on: #34d17a;
    --off: #f08a4b;
    --warn-bg: #3a2f12;
    --warn-text: #f4d27a;
    --danger: #ff6b6b;
    --shadow: 0 10px 30px rgb(0 0 0 / 0.5);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
code, pre, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* Buttons and inputs */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 7px 12px;
  font: inherit;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn.icon { padding: 4px 9px; }
input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 9px;
  min-width: 0;
  width: 100%;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, .btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); }

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 17px; margin: 0; }
.brand-mark { width: 14px; height: 22px; border-radius: 3px; border: 2px solid var(--accent); display: inline-block; }
.counts { display: flex; gap: 14px; flex: 1; flex-wrap: wrap; color: var(--muted); }
.count b { color: var(--text); }
.topbar-actions { display: flex; gap: 8px; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 5px; vertical-align: 0; }
.dot.on { background: var(--on); box-shadow: 0 0 0 3px color-mix(in srgb, var(--on) 20%, transparent); }
.dot.off { background: var(--off); }

.tabs { display: flex; gap: 4px; padding: 0 20px; background: var(--surface); border-bottom: 1px solid var(--border); overflow-x: auto; }
.tab {
  appearance: none; border: 0; background: none; color: var(--muted); font: inherit;
  padding: 10px 12px; border-bottom: 2px solid transparent; cursor: pointer; white-space: nowrap;
}
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

main { padding: 16px 20px 60px; max-width: 1400px; margin: 0 auto; }

.filters { display: grid; grid-template-columns: 1fr 180px 150px; gap: 8px; margin-bottom: 12px; }
.bulkbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  padding: 8px 10px; margin-bottom: 12px;
  background: color-mix(in srgb, var(--accent) 10%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border));
  border-radius: var(--radius);
}
.bulkbar > span { margin-right: 6px; }

/* Device table */
table { width: 100%; border-collapse: collapse; }
.devices, .simple { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); background: var(--surface-2); font-weight: 600; }
tbody tr:last-child td { border-bottom: 0; }
.devices tbody tr { cursor: pointer; }
.devices tbody tr:hover { background: var(--surface-2); }
.col-check { width: 36px; }
.col-thumb { width: 52px; }
.thumb {
  width: 36px; height: 64px; border-radius: 4px; object-fit: cover; display: block;
  background: var(--surface-2); border: 1px solid var(--border);
}
.url { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.name-cell b { display: block; }
.badge {
  display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 99px;
  background: var(--warn-bg); color: var(--warn-text); margin-left: 6px; white-space: nowrap;
}
.empty { text-align: center; color: var(--muted); padding: 40px 0; }

/* Phone layout: rows become cards */
@media (max-width: 760px) {
  .topbar { padding: 10px 16px; gap: 10px; }
  .tabs { padding: 0 8px; }
  main { padding: 12px 16px 60px; }
  .filters { grid-template-columns: 1fr 1fr; }
  .filters input { grid-column: 1 / -1; }
  .devices thead { display: none; }
  .devices, .devices tbody { display: block; border: 0; background: none; }
  .devices tr {
    display: grid; grid-template-columns: 28px 44px 1fr auto; gap: 2px 10px; align-items: center;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 10px; margin-bottom: 8px;
  }
  .devices td { border: 0; padding: 0; }
  .devices td.c-check { grid-row: 1 / span 3; }
  .devices td.c-thumb { grid-row: 1 / span 3; }
  .devices td.c-name { grid-column: 3; }
  .devices td.c-status { grid-column: 4; grid-row: 1; justify-self: end; }
  .devices td.c-group, .devices td.c-seen, .devices td.c-app { display: none; }
  .devices td.c-url { grid-column: 3 / span 2; }
  .devices td.c-meta { display: block !important; grid-column: 3 / span 2; color: var(--muted); font-size: 12px; }
  .url { max-width: none; }
  .simple { display: block; overflow-x: auto; }
}
.devices td.c-meta { display: none; }

/* Forms */
.inline-form { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.inline-form input { width: auto; flex: 1 1 180px; }
.grid-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.grid-form label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.grid-form label > input, .grid-form label > select, .grid-form label > textarea { color: var(--text); font-size: 14px; }
.grid-form label.check { flex-direction: row; align-items: center; gap: 8px; font-size: 14px; color: var(--text); }
.grid-form .wide { grid-column: 1 / -1; }
.grid-form h2 { margin: 0; font-size: 17px; }
.form-actions { display: flex; gap: 8px; justify-content: space-between; flex-wrap: wrap; }
.form-error { color: var(--danger); margin: 0; }
.code-input { font-size: 22px !important; letter-spacing: 0.3em; text-transform: uppercase; font-family: ui-monospace, Menlo, monospace; }
.notice { background: var(--warn-bg); color: var(--warn-text); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 12px; }
.notice pre { background: var(--surface); color: var(--text); padding: 10px; border-radius: 8px; overflow-x: auto; white-space: pre-wrap; word-break: break-all; }

/* Dialogs */
dialog { color: var(--text); background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); padding: 0; }
dialog::backdrop { background: rgb(8 10 14 / 0.45); }
.modal { width: min(480px, calc(100vw - 32px)); border-radius: 14px; }
.modal form { padding: 18px; }
.drawer {
  margin: 0 0 0 auto; height: 100dvh; max-height: 100dvh; width: min(560px, 100vw); max-width: 100vw;
  border-radius: 0; border-width: 0 0 0 1px;
}
.drawer[open] { display: flex; flex-direction: column; }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.drawer-head h2 { margin: 0; font-size: 17px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drawer-body { padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 16px; }
.shot { margin: 0; display: flex; flex-direction: column; gap: 6px; align-items: center; }
.shot img { max-width: 100%; max-height: 46vh; border-radius: 6px; border: 1px solid var(--border); background: var(--surface-2); }
.shot figcaption { display: flex; gap: 10px; align-items: center; color: var(--muted); font-size: 12px; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.tip-wrap { position: relative; display: inline-flex; }
.tip-wrap[data-tip]:hover::after, .tip-wrap[data-tip]:focus-within::after {
  content: attr(data-tip);
  position: absolute; bottom: calc(100% + 6px); left: 0; z-index: 5;
  width: max-content; max-width: 240px; padding: 6px 8px; border-radius: 6px;
  background: var(--text); color: var(--bg); font-size: 12px; line-height: 1.35;
}
details summary { cursor: pointer; font-weight: 600; padding: 4px 0; }
.commands { list-style: none; margin: 6px 0 0; padding: 0; font-size: 13px; }
.commands li { display: flex; gap: 8px; justify-content: space-between; padding: 5px 0; border-bottom: 1px solid var(--border); }
.st-ok { color: var(--on); }
.st-error, .st-timeout, .st-offline { color: var(--off); }
.st-pending { color: var(--muted); }
.info { display: grid; grid-template-columns: max-content 1fr; gap: 4px 12px; margin: 6px 0 0; font-size: 13px; }
.info dt { color: var(--muted); }
.info dd { margin: 0; overflow-wrap: anywhere; }

@media (max-width: 760px) {
  .grid-form { grid-template-columns: 1fr; }
}

/* Toasts */
.toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; z-index: 100; width: min(420px, calc(100vw - 32px)); }
.toast { background: var(--text); color: var(--bg); padding: 9px 12px; border-radius: 8px; box-shadow: var(--shadow); font-size: 13px; }
.toast.error { background: var(--danger); color: #fff; }

/* Login */
.login-page { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 24px; box-shadow: var(--shadow); }
.login-card form { display: flex; flex-direction: column; gap: 12px; margin-top: 18px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
