:root {
  --bg-primary: #090d16;
  --bg-secondary: #0f172a;
  --bg-tertiary: #1e293b;
  --card-bg: rgba(15, 23, 42, 0.85);
  --border-color: rgba(148, 163, 184, 0.14);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --accent: #06b6d4;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background-color: var(--bg-primary);
  color: var(--text-main);
  min-height: 100vh;
  min-height: 100dvh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

/* Accessibility: Focus visible outline on interactive elements */
:focus-visible {
  outline: 2px solid #3b82f6 !important;
  outline-offset: 2px !important;
}

button, a, input, select, textarea {
  touch-action: manipulation;
}

button, a {
  cursor: pointer;
}

/* Mobile input zoom prevention & minimum touch targets */
input, select, textarea {
  font-size: 16px !important;
}

/* Minimum 44x44px target utility */
.min-touch,
button.min-touch,
a.min-touch {
  min-height: 44px;
  min-width: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Glassmorphism Card */
.glass-card {
  background: var(--card-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  box-shadow: 0 10px 30px -10px rgba(0, 0, 0, 0.35);
}

/* Code Box & Pre Blocks */
.code-box,
pre.code-block {
  background: #060911;
  border: 1px solid #1e293b;
  border-radius: 10px;
  padding: 10px 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  color: #38bdf8;
  overflow-x: auto;
  word-break: break-all;
}

pre.code-block {
  word-break: normal;
  white-space: pre;
}

/* Hide scrollbars utility */
.no-scrollbar::-webkit-scrollbar {
  display: none;
}
.no-scrollbar {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

/* Custom Scrollbars */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: rgba(15, 23, 42, 0.4);
}
::-webkit-scrollbar-thumb {
  background: #334155;
  border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
  background: #475569;
}

/* Pulsing Status Dots */
.pulse-green {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #10b981;
  box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7);
  animation: pulse-green-anim 2s infinite;
  flex-shrink: 0;
}

.pulse-red {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ef4444;
  box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7);
  animation: pulse-red-anim 2s infinite;
  flex-shrink: 0;
}

@keyframes pulse-green-anim {
  0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7); }
  70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(16, 185, 129, 0); }
  100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(16, 185, 129, 0); }
}

@keyframes pulse-red-anim {
  0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7); }
  70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(239, 68, 68, 0); }
  100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
}

/* Floating Toast Notification (#toast) */
#toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  min-width: 260px;
  max-width: calc(100vw - 32px);
  padding: 12px 16px;
  border-radius: 12px;
  background: rgba(15, 23, 42, 0.96);
  border: 1px solid rgba(59, 130, 246, 0.35);
  color: #f8fafc;
  font-size: 13px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 15px 30px -5px rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  opacity: 0;
  pointer-events: none;
  transform: translateY(16px);
  transition: opacity 0.22s ease, transform 0.22s ease;
}

#toast.show {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

/* Modal Backdrop & Dialog (100dvh + Safe Area Support) */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.78);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease-in-out;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}

.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.modal-content {
  background: #0f172a;
  border: 1px solid rgba(148, 163, 184, 0.18);
  border-radius: 18px;
  width: 100%;
  max-width: 580px;
  max-height: 88vh;
  max-height: 88dvh;
  overflow-y: auto;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.7);
  transform: translateY(8px) scale(0.98);
  transition: transform 0.2s ease;
}

.modal-overlay.active .modal-content {
  transform: translateY(0) scale(1);
}

/* QR Code Containers */
#qr-container,
#sub-qr-container,
#wa-qr-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 14px;
  background: #ffffff;
  border-radius: 14px;
  width: fit-content;
  margin: 0 auto;
}

/* More Actions Dropdown Menu */
.user-menu-dropdown {
  position: absolute;
  right: 0;
  top: 100%;
  margin-top: 6px;
  min-width: 170px;
  background: #0f172a;
  border: 1px solid rgba(148, 163, 184, 0.2);
  border-radius: 12px;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.65);
  z-index: 50;
  display: none;
  overflow: hidden;
}

.user-menu-dropdown.show {
  display: block;
}

/* ========================================================
   Light Theme System
   ======================================================== */
html.light, body.light {
  --bg-primary: #f1f5f9;
  --bg-secondary: #ffffff;
  --bg-tertiary: #e2e8f0;
  --card-bg: #ffffff;
  --border-color: #cbd5e1;
  --text-main: #0f172a;
  --text-muted: #475569;
}

html.light body {
  background-color: #f1f5f9 !important;
  color: #0f172a !important;
}

html.light header {
  background-color: rgba(255, 255, 255, 0.95) !important;
  border-bottom-color: #cbd5e1 !important;
}

html.light .glass-card,
html.light .modal-content,
html.light .user-menu-dropdown {
  background: #ffffff !important;
  border: 1px solid #cbd5e1 !important;
  box-shadow: 0 6px 18px -4px rgba(15, 23, 42, 0.08) !important;
}

html.light .from-slate-900 {
  --tw-gradient-from: #ffffff !important;
  --tw-gradient-to: #f8fafc !important;
  background: #ffffff !important;
}

html.light .bg-slate-950,
html.light .bg-slate-950\/70,
html.light .bg-slate-900,
html.light .bg-slate-900\/40,
html.light .bg-slate-900\/50,
html.light .bg-slate-900\/60,
html.light .bg-slate-900\/80,
html.light .bg-slate-800,
html.light .bg-slate-800\/40,
html.light .bg-slate-800\/60,
html.light .bg-slate-800\/80 {
  background-color: #f8fafc !important;
}

html.light .bg-emerald-950\/40 {
  background-color: rgba(16, 185, 129, 0.1) !important;
}

html.light .border-slate-900,
html.light .border-slate-800,
html.light .border-slate-800\/80,
html.light .border-slate-800\/60,
html.light .border-slate-700,
html.light .border-slate-700\/80,
html.light .border-slate-700\/60 {
  border-color: #cbd5e1 !important;
}

html.light .text-white,
html.light .text-slate-100,
html.light .text-slate-200 {
  color: #0f172a !important;
}

html.light .text-slate-300 {
  color: #334155 !important;
}

html.light .text-slate-400,
html.light .text-slate-500 {
  color: #475569 !important;
}

html.light .text-emerald-400,
html.light .text-emerald-300 {
  color: #059669 !important;
}

html.light .text-amber-400 {
  color: #d97706 !important;
}

html.light .text-blue-400 {
  color: #2563eb !important;
}

html.light .text-cyan-400,
html.light .text-cyan-300 {
  color: #0891b2 !important;
}

html.light pre.code-block,
html.light .code-box {
  background-color: #f8fafc !important;
  border: 1px solid #cbd5e1 !important;
  color: #0369a1 !important;
}

html.light input,
html.light textarea,
html.light select {
  background-color: #ffffff !important;
  color: #0f172a !important;
  border-color: #94a3b8 !important;
}

html.light #toast {
  background: #ffffff !important;
  color: #0f172a !important;
  border-color: #94a3b8 !important;
}

/* ========================================================
   Mobile UI & Touch Ergonomics (< 640px)
   ======================================================== */
@media (max-width: 640px) {
  .modal-overlay {
    padding: 0 !important;
    align-items: flex-end !important;
  }

  .modal-content {
    border-bottom-left-radius: 0 !important;
    border-bottom-right-radius: 0 !important;
    border-top-left-radius: 20px !important;
    border-top-right-radius: 20px !important;
    max-height: 90vh !important;
    max-height: 90dvh !important;
    padding: 1.15rem 1rem 1.75rem 1rem !important;
    width: 100% !important;
    max-width: 100% !important;
  }

  #toast {
    left: 12px;
    right: 12px;
    bottom: 14px;
    min-width: 0;
  }
}

/* ========================================================
   Desktop Grid Breakpoint (>= 1024px)
   ======================================================== */
@media (min-width: 1024px) {
  #users-cards-grid {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(360px, 1fr)) !important;
    gap: 1.25rem !important;
  }
}

/* ========================================================
   Accessibility: Prefers-Reduced-Motion
   ======================================================== */
@media (prefers-reduced-motion: reduce) {
  *, ::before, ::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .pulse-green, .pulse-red {
    animation: none !important;
  }
}
/* The panel shell and its controls share the same tokens in both themes. */
html {
  color-scheme: dark;
  --panel-bg: #11171c;
  --panel-surface: #192126;
  --panel-line: #34424a;
  --panel-text: #edf3f4;
  --panel-muted: #abbcc1;
  --panel-accent: #83dbc4;
  --panel-alert: #ffc18d;
  scrollbar-gutter: stable;
  scrollbar-color: var(--panel-line) var(--panel-bg);
  scrollbar-width: thin;
}

[hidden] { display: none !important; }

html.light {
  color-scheme: light;
  --panel-bg: #f0f4f3;
  --panel-surface: #ffffff;
  --panel-line: #bdcdca;
  --panel-text: #172b2a;
  --panel-muted: #4f6463;
  --panel-accent: #106857;
  --panel-alert: #8a3c16;
}

html body, html.light body {
  background: var(--panel-bg) !important;
  color: var(--panel-text) !important;
}

.panel-frame { width: min(100% - 2rem, 76rem); margin-inline: auto; }
.panel-header, html.light .panel-header {
  background: var(--panel-surface) !important;
  border-bottom: 1px solid var(--panel-line);
}
.panel-header-inner, .panel-brand, .panel-actions, .login-heading, .dialog-heading {
  display: flex;
  align-items: center;
}
.panel-header-inner {
  justify-content: space-between;
  flex-wrap: wrap;
  gap: .75rem 1.25rem;
  padding-block: .75rem;
}
.panel-brand { gap: .65rem; min-width: 0; flex-wrap: wrap; }
.panel-actions { flex-wrap: wrap; gap: .35rem; }
.wordmark-icon {
  display: grid;
  width: 2rem;
  height: 2rem;
  place-items: center;
  border: 1px solid var(--panel-line);
  border-radius: .4rem;
  color: var(--panel-accent);
  font: 700 .95rem ui-monospace, monospace;
}
.panel-host {
  font-family: ui-monospace, monospace;
  overflow-wrap: anywhere;
  color: var(--panel-accent);
}
.panel-brand .panel-host { font-size: .8rem; }
.panel-action {
  min-height: 2.75rem;
  padding: .35rem .65rem;
  border: 1px solid transparent;
  border-radius: .4rem;
  font-size: .85rem;
  color: var(--panel-text);
  cursor: pointer;
}
.panel-action:hover, .service-card:hover { border-color: var(--panel-accent); }
button:disabled { cursor: not-allowed; opacity: .6; }
:where(button, a, input, textarea, select):focus-visible {
  outline: 2px solid var(--panel-accent) !important;
  outline-offset: 3px;
}
.panel-main { padding-block: 1.9rem; display: grid; gap: 2rem; }
.overview h1 { font-size: clamp(1.5rem, 3vw, 2.35rem); font-weight: 650; letter-spacing: -.045em; }
.overview > p { color: var(--panel-muted); margin-block: .25rem 1rem; font-size: .9rem; }
.overview-metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  border-block: 1px solid var(--panel-line);
}
.overview-metrics > div { padding: .9rem .85rem .9rem 0; min-width: 0; }
.overview-metrics dt, .overview-metrics span { font-size: .78rem; color: var(--panel-muted); }
.overview-metrics dd { font-size: 1.1rem; font-weight: 650; margin-block: .2rem; }
.overview-metrics span { overflow-wrap: anywhere; display: block; }
.panel-services h2 { font-size: 1.05rem; font-weight: 650; margin-bottom: .65rem; }
.service-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(9.6rem, 1fr)); gap: .55rem; }
.service-card {
  display: grid;
  align-content: start;
  gap: .2rem;
  min-height: 5rem;
  padding: .65rem .75rem;
  border: 1px solid var(--panel-line);
  border-radius: .45rem;
  background: var(--panel-surface);
  text-align: start;
  cursor: pointer;
}
.service-state { font-size: .8rem; color: var(--panel-muted); text-transform: capitalize; }
.service-state::before {
  content: "";
  display: inline-block;
  width: .45rem;
  height: .45rem;
  margin-inline-end: .4rem;
  border-radius: 50%;
  background: currentColor;
  vertical-align: .05rem;
}
[data-state="running"] .service-state { color: var(--panel-accent); }
[data-state="offline"] .service-state { color: var(--panel-alert); }
.service-card small { color: var(--panel-muted); font: .68rem ui-monospace, monospace; overflow-wrap: anywhere; }
.service-card small:empty { display: none; }
.login-heading, .dialog-heading { justify-content: space-between; gap: 1rem; margin-bottom: .7rem; }
.login-logo {
  flex: 1 1 13rem;
  min-width: 0;
  max-width: 13rem;
  aspect-ratio: 2.6;
  object-fit: contain;
}
.login-heading [data-theme-toggle] { flex: none; }
.login-title { font-size: 1.5rem; font-weight: 650; margin-bottom: 1.2rem; }
.dialog-heading h3 { font-size: 1.1rem; font-weight: 650; }
html.light button.bg-blue-600,
html.light button.bg-emerald-600,
html.light button.bg-red-600,
html.light a.bg-emerald-600 { color: white !important; }
@media (max-width: 640px) {
  .overview-metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .panel-main { padding-block: 1.25rem; gap: 1.35rem; }
  .user-tools {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
  }
  .user-search-field { grid-column: 1 / -1; }
  #user-sort { width: 100%; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
.inbound-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr));
  gap: .4rem;
}
.inbound-options label {
  display: flex;
  align-items: center;
  gap: .55rem;
  min-height: 2.75rem;
  padding: .35rem .6rem;
  border: 1px solid var(--panel-line);
  border-radius: .4rem;
  font-size: .85rem;
  cursor: pointer;
}
.inbound-options label:has(input:disabled) {
  opacity: .65;
  cursor: default;
}
.inbound-options input {
  flex: 0 0 1rem;
  min-height: 1rem;
  width: 1rem;
  height: 1rem;
  accent-color: var(--panel-accent);
}
