:root { color-scheme: light; }
.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 38px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--ink);
  font: 600 13px 'DM Sans', 'Segoe UI', sans-serif;
  white-space: nowrap;
  transition: border-color .15s, background .15s;
}
.theme-toggle:hover { border-color: var(--blue); }
.theme-icon { width: 19px; font-size: 21px; line-height: 1; }
.login-page > .theme-toggle { position: fixed; right: 20px; top: 20px; z-index: 10; }
.login-page { padding-top: 80px; padding-bottom: 32px; }
.top-actions { gap: 12px; }
.add-card form > label { min-width: 0; }
button:focus-visible, input:focus-visible, select:focus-visible, a:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 3px;
}
.dialog-actions button:not(.primary) {
  padding: 9px 13px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface);
  color: var(--ink);
}
html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b1220;
  --surface: #111d30;
  --ink: #e7edf6;
  --muted: #a5b4ca;
  --line: #2a3b54;
  --blue: #9cc4ff;
  --blue2: #b8d5ff;
  --green: #6ee7b7;
  --red: #ff9aaf;
}
html[data-theme="dark"] body { background: var(--bg); }
html[data-theme="dark"] .topbar,
html[data-theme="dark"] .add-card,
html[data-theme="dark"] .users-card,
html[data-theme="dark"] .login-shell,
html[data-theme="dark"] .login-box {
  background: var(--surface);
  border-color: var(--line);
}
html[data-theme="dark"] .add-card,
html[data-theme="dark"] .users-card { box-shadow: 0 8px 28px #0000001c; }
html[data-theme="dark"] label { color: #c4d1e2; }
html[data-theme="dark"] input,
html[data-theme="dark"] select,
html[data-theme="dark"] select option {
  background: #0c1728;
  border-color: var(--line);
  color: var(--ink);
}
html[data-theme="dark"] input::placeholder { color: #93a7c0; opacity: 1; }
html[data-theme="dark"] input:focus,
html[data-theme="dark"] select:focus { border-color: #7aa2ff; box-shadow: 0 0 0 4px #498bff20; }
html[data-theme="dark"] .primary { background: #376fe6; color: #fff; }
html[data-theme="dark"] .primary:hover { background: #2c60d4; }
html[data-theme="dark"] th { background: #17253b; color: #b6c5d9; }
html[data-theme="dark"] th,
html[data-theme="dark"] td,
html[data-theme="dark"] .table-title { border-color: var(--line); }
html[data-theme="dark"] tbody tr:hover td { background: #15243a; }
html[data-theme="dark"] code { background: #0b1627; color: #b6d4ff; }
html[data-theme="dark"] .device { background: #0d192b; border-color: var(--line); }
html[data-theme="dark"] .actions button,
html[data-theme="dark"] .secondary,
html[data-theme="dark"] .logout,
html[data-theme="dark"] .dialog-actions button:not(.primary),
html[data-theme="dark"] #bot-code-dialog > button:not(.dialog-close) {
  background: #17253b;
  color: var(--ink);
  border: 1px solid var(--line);
}
html[data-theme="dark"] .actions button:hover,
html[data-theme="dark"] .secondary:hover,
html[data-theme="dark"] .logout:hover { background: #20334e; }
html[data-theme="dark"] .actions .danger { color: var(--red); }
html[data-theme="dark"] .actions .enable { color: var(--green); }
html[data-theme="dark"] .actions .accent { color: var(--blue); border-color: #355b89; }
html[data-theme="dark"] .device-toggle,
html[data-theme="dark"] .app-edit { background: #172e52; border-color: #355b89; color: var(--blue); }
html[data-theme="dark"] .app-badge,
html[data-theme="dark"] .link-code { color: var(--blue); }
html[data-theme="dark"] .sync-note,
html[data-theme="dark"] .login-foot { color: var(--muted); }
html[data-theme="dark"] .count,
html[data-theme="dark"] .plan { background: #1d355d; color: #b6d4ff; }
html[data-theme="dark"] .state.active { background: #10382d; color: #86efc5; }
html[data-theme="dark"] .state.blocked { background: #3b1b29; color: #ffacc0; }
html[data-theme="dark"] .serial-delete { background: #301b2a; border-color: #784054; color: var(--red); }
html[data-theme="dark"] .serial-delete:hover { background: #4c2435; }
html[data-theme="dark"] dialog {
  background: #132239;
  color: var(--ink);
  border: 1px solid var(--line);
  box-shadow: 0 30px 90px #00000088;
}
html[data-theme="dark"] dialog::backdrop { background: #020617bd; backdrop-filter: blur(5px); }
html[data-theme="dark"] .dialog-close { color: #b5c7dc; }
html[data-theme="dark"] .login-page {
  background: radial-gradient(ellipse at 15% 10%, #17305b 0, transparent 42%),
    radial-gradient(ellipse at 90% 95%, #162948 0, transparent 42%), #0b1220;
}
html[data-theme="dark"] .login-shell { box-shadow: 0 35px 90px #00000055; }
html[data-theme="dark"] .login-visual { background: linear-gradient(145deg, #142e50, #0d1c31); }
@media (max-width: 640px) {
  .topbar { height: auto; min-height: 68px; padding: 14px 16px; gap: 12px; flex-wrap: wrap; }
  .top-actions { margin-left: auto; gap: 8px; flex-wrap: wrap; }
  .table-title { flex-direction: column; align-items: stretch; gap: 14px; }
  .table-title input { max-width: none; }
  .login-page > .theme-toggle { top: 16px; right: 16px; }
}
@media (max-width: 480px) {
  .topbar [data-theme-label] { display: none; }
  .topbar .theme-toggle { width: 38px; padding: 8px; }
}
@media (prefers-reduced-motion: reduce) { .theme-toggle { transition: none; } }


/* Responsive admin: shared table DOM, cards below 768px. */
button, .action-menu > summary { min-height: 44px; min-width: 44px; touch-action: manipulation; }
.theme-toggle { min-height: 44px; min-width: 44px; }
button, summary { overflow-wrap: anywhere; }
.topbar { height: auto; min-height: 76px; padding: 12px max(24px, calc((100% - 1600px) / 2)); gap: 16px; }
.topbar .brand { min-width: 0; flex-shrink: 0; }
.top-actions { gap: 10px; }
.container { width: min(1600px, calc(100% - 48px)); margin: 32px auto 56px; }
.page-head { gap: 20px; }
.page-head > div:first-child { min-width: 0; }
.page-head .count { flex: 0 0 auto; }
.page-head p { line-height: 1.6; }
.add-card { grid-template-columns: 1fr; gap: 20px; align-items: stretch; }
.add-card form { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr) minmax(140px, .8fr) auto; gap: 16px; }
.add-card form > *, .table-title > *, .device > span { min-width: 0; }
.add-card input, .add-card select { min-height: 46px; width: 100%; }
.table-title { gap: 16px; }
.table-title input { max-width: 380px; }
.sync-note { line-height: 1.7; overflow-wrap: anywhere; }
.table-wrap { max-width: 100%; }
.users-card table { table-layout: fixed; }
.users-card th, .users-card td { white-space: normal; overflow-wrap: anywhere; padding: 16px 12px; vertical-align: top; }
.users-card th:nth-child(1) { width: 18%; }
.users-card th:nth-child(2) { width: 16%; }
.users-card th:nth-child(3) { width: 20%; }
.users-card th:nth-child(4) { width: 11%; }
.users-card th:nth-child(5) { width: 10%; }
.users-card th:nth-child(6) { width: 25%; }
.login-ip { margin-top: 4px; }
.login-ip code { font-size: 11px; }
.users-card td strong { line-height: 1.5; }
.users-card td small { line-height: 1.6; }
.users-card code { white-space: normal; overflow-wrap: anywhere; box-decoration-break: clone; -webkit-box-decoration-break: clone; }
.app-badge { white-space: normal; line-height: 1.6; }
.device-list, .actions { min-width: 0; }
.device { align-items: flex-start; gap: 8px; }
.device code { display: block; }
.serial-delete { flex: 0 0 44px; width: 44px; height: 44px; }
.device-toggle { max-width: 100%; line-height: 1.4; }
.actions { display: grid; gap: 8px; }
.actions > button, .action-menu, .action-menu-items > button { width: 100%; }
.actions button { font-size: 12px; line-height: 1.4; padding: 10px 8px; }
.action-menu > summary { display: flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--ink); font-size: 12px; font-weight: 600; cursor: pointer; list-style: none; text-align: center; }
.action-menu > summary::-webkit-details-marker { display: none; }
.action-menu > summary::after { content: '⌄'; flex-shrink: 0; }
.action-menu[open] > summary::after { content: '⌃'; }
.action-menu-items { display: grid; gap: 8px; padding-top: 8px; }
summary:focus-visible { outline: 2px solid var(--blue); outline-offset: 3px; }
dialog, #bot-code-dialog { max-width: calc(100% - 32px); max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); overflow-y: auto; overscroll-behavior: contain; }
dialog { padding-top: 52px; }
.dialog-close { width: 44px; height: 44px; top: 6px; right: 8px; }
dialog p, dialog h2, .link-code { overflow-wrap: anywhere; }
.dialog-actions { flex-wrap: wrap; }
.login-page { min-height: 100vh; min-height: 100dvh; }
.login-box { min-width: 0; }
.login-box h2 { overflow-wrap: anywhere; }
.login-shell { min-width: 0; }
@media (min-width: 768px) and (max-width: 1023px) {
  .add-card form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .users-card table { min-width: 1000px; }
  .login-visual { padding: 40px; }
  .login-box { padding: 40px 28px; }
}
@media (max-width: 767px) {
  .container { width: calc(100% - 24px); margin: 24px auto 40px; }
  .topbar { padding: 12px 16px; flex-wrap: wrap; gap: 12px; }
  .top-actions { margin-left: auto; flex-wrap: nowrap; gap: 8px; }
  .topbar .theme-toggle { min-width: 44px; width: 44px; padding: 8px; }
  .topbar [data-theme-label] { display: none; }
  .top-actions .online { font-size: 12px; gap: 8px; }
  .page-head { align-items: flex-start; gap: 12px; }
  .page-head h1 { font-size: 28px; }
  .page-head p:last-child { font-size: 13px; }
  .count { font-size: 12px; white-space: nowrap; }
  .add-card { padding: 20px 16px; gap: 18px; }
  .add-card form { grid-template-columns: 1fr; gap: 16px; }
  input, select { font-size: 16px; min-height: 46px; }
  .table-title { flex-direction: column; align-items: stretch; padding: 20px 16px; gap: 14px; }
  .table-title input { max-width: none; }
  .sync-note { padding: 0 16px; }
  .table-wrap { overflow: visible; padding: 4px 12px 16px; }
  .users-card table { display: block; width: 100%; min-width: 0; }
  .users-card thead { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); clip-path: inset(50%); white-space: nowrap; }
  #users { display: grid; gap: 16px; }
  #users > tr { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); border: 1px solid var(--line); border-radius: 12px; background: var(--surface); padding: 4px 14px; }
  #users > tr > td { display: block; min-width: 0; width: auto; padding: 14px 0; border-bottom: 1px solid var(--line); }
  #users > tr > td::before { content: attr(data-label); display: block; color: var(--muted); font-size: 11px; font-weight: 700; margin-bottom: 8px; }
  #users > tr > td:nth-child(2) { padding-right: 12px; }
  #users > tr > .user-identity, #users > tr > .user-actions { grid-column: 1 / -1; }
  #users > tr > .user-identity { order: 0; }
  #users > tr > td:nth-child(2) { order: 1; }
  #users > tr > td:nth-child(4) { order: 2; }
  #users > tr > .user-devices { order: 3; grid-column: 1; padding-right: 12px; }
  #users > tr > .user-status { order: 3; grid-column: 2; }
  #users > tr > .user-actions { order: 6; border-bottom: 0; }
  .user-identity > strong { font-size: 15px; }
  .actions { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
  .actions button, .action-menu > summary { font-size: 13px; }
  .device { align-items: center; }
  .device code { font-size: 12px; }
  .app-edit, .expiry button { padding: 10px 6px; font-size: 12px; }
  .login-page { padding: 80px 16px 24px; }
  .login-shell { min-height: 0; border-radius: 20px; max-width: 480px; grid-template-columns: 1fr; }
  .login-visual { display: none; }
  .login-box { padding: 32px 24px; }
  .login-box h2 { font-size: 28px; }
  .login-box form { margin-top: 28px; }
  .login-page > .theme-toggle { right: 16px; top: 16px; }
  dialog, #bot-code-dialog { padding: 52px 20px 20px; }
  .dialog-actions > button { flex: 1 1 auto; }
}
@media (max-width: 359px) {
  .top-actions { width: 100%; justify-content: flex-end; }
  #users > tr { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #users > tr > td:nth-child(2), #users > tr > td:nth-child(4) { grid-column: 1 / -1; }
  #users > tr > td:nth-child(2) { padding-right: 0; }
  .actions { grid-template-columns: minmax(0, 1fr); }
  .page-head { flex-wrap: wrap; }
  .login-box { padding: 28px 20px; }
}

/* Desktop shows every action using the same buttons and handlers as mobile. */
@media (min-width: 1024px) {
  .user-actions .actions { grid-template-columns: repeat(2, minmax(0, 1fr)); align-items: start; }
  .user-actions .action-menu { display: contents; }
  .user-actions .action-menu::details-content { display: contents; }
  .user-actions .action-menu > summary { display: none; }
  .user-actions .action-menu-items { display: contents; padding-top: 0; }
}
