/* ── CP Control Panel — Main Stylesheet ──────────────────────────────────── */

/* ── Tokens: Dark ────────────────────────────────────────────────────────── */
:root,
[data-theme="dark"] {
  --bg:          #0E0F11;
  --surface:     #16181C;
  --card:        #1E2127;
  --border:      #2A2D35;
  --border-soft: #232730;
  --accent:      #4ADE80;
  --accent-dim:  rgba(74,222,128,.12);
  --accent-glow: rgba(74,222,128,.25);
  --red:         #F87171;
  --amber:       #FBBF24;
  --blue:        #60A5FA;
  --text:        #F8F9FA;
  --text-2:      #9CA3AF;
  --text-3:      #6B7280;
  --mono:        'IBM Plex Mono', monospace;
  --sans:        'Inter', sans-serif;
  --radius:      8px;
  --radius-lg:   14px;
  --sidebar-w:   220px;
  --header-h:    52px;
  --shadow:      0 4px 24px rgba(0,0,0,.4);
}

/* ── Tokens: Light ───────────────────────────────────────────────────────── */
[data-theme="light"] {
  --bg:          #F4F5F7;
  --surface:     #FFFFFF;
  --card:        #FFFFFF;
  --border:      #E5E7EB;
  --border-soft: #F0F1F3;
  --accent:      #16A34A;
  --accent-dim:  rgba(22,163,74,.10);
  --accent-glow: rgba(22,163,74,.20);
  --red:         #DC2626;
  --amber:       #D97706;
  --blue:        #2563EB;
  --text:        #111827;
  --text-2:      #6B7280;
  --text-3:      #9CA3AF;
  --shadow:      0 2px 12px rgba(0,0,0,.08);
}

/* ── Reset ───────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 15px; }
body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  transition: background .2s, color .2s;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
input, textarea, select, button { font-family: inherit; }

/* ── Auth Overlay ─────────────────────────────────────────────────────────────── */
.auth-overlay {
  position: fixed; inset: 0;
  display: flex;
  z-index: 9999;
  background: var(--bg);
}
.auth-overlay.hidden { display: none; }

.auth-panel-left {
  width: 400px; flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  padding: 40px 44px;
}
.auth-brand { display: flex; align-items: center; gap: 12px; }
.auth-brand-mark {
  font-family: var(--mono); font-size: 1rem; font-weight: 700;
  color: #0E0F11; background: var(--accent);
  width: 34px; height: 34px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  letter-spacing: -.03em;
}
[data-theme="light"] .auth-brand-mark { color: #fff; }
.auth-brand-name {
  font-size: .82rem; font-weight: 600;
  color: var(--text); letter-spacing: .02em;
}
.auth-panel-body {
  flex: 1; display: flex; flex-direction: column;
  justify-content: center; gap: 30px; padding: 48px 0 32px;
}
.auth-feature { display: flex; gap: 14px; align-items: flex-start; }
.auth-feature-icon {
  width: 36px; height: 36px;
  background: var(--accent-dim);
  border: 1px solid var(--accent-glow);
  border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  color: var(--accent); flex-shrink: 0; margin-top: 1px;
}
.auth-feature-title {
  font-size: .88rem; font-weight: 600;
  color: var(--text); margin-bottom: 3px;
}
.auth-feature-desc { font-size: .78rem; color: var(--text-3); line-height: 1.55; }
.auth-panel-footer { font-size: .72rem; color: var(--text-3); font-family: var(--mono); }

.auth-panel-right {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 40px 32px; overflow-y: auto;
}
.auth-view { display: none; width: 100%; max-width: 380px; }
.auth-view.active { display: block; }
.auth-form-header { margin-bottom: 28px; }
.auth-title {
  font-size: 1.35rem; font-weight: 700;
  color: var(--text); letter-spacing: -.02em; margin-bottom: 6px;
}
.auth-sub { font-size: .84rem; color: var(--text-3); line-height: 1.5; }
.auth-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.auth-btn { margin-top: 8px; height: 42px; font-size: .9rem; }
.auth-label-link {
  float: right; font-size: .75rem; font-weight: 400; color: var(--text-3);
}
.auth-label-link:hover { color: var(--accent); }
.auth-switch {
  margin-top: 20px; font-size: .82rem;
  color: var(--text-3); text-align: center;
}
.auth-switch a { color: var(--accent); font-weight: 500; }
.auth-switch a:hover { text-decoration: underline; }
.auth-msg {
  max-width: 380px; width: 100%;
  margin-top: 14px; font-size: .82rem;
  min-height: 20px; text-align: center; transition: all .2s;
}
.auth-msg.success { color: var(--accent); }
.auth-msg.error { color: var(--red); }

@media (max-width: 640px) {
  .auth-panel-left { display: none; }
  .auth-panel-right { padding: 32px 20px; }
  .auth-row { grid-template-columns: 1fr; }
}
/* ── Fields ──────────────────────────────────────────────────────────────── */
.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: .75rem;
  font-weight: 500;
  color: var(--text-2);
  margin-bottom: 5px;
  letter-spacing: .03em;
}
.field input, .field textarea, .field select {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: .9rem;
  padding: 9px 12px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.field input:focus, .field textarea:focus, .field select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}
.field textarea { resize: vertical; min-height: 80px; }
.input-wrap { position: relative; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn-primary {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  width: 100%; padding: 10px 16px;
  background: var(--accent);
  color: #0E0F11;
  font-size: .88rem;
  font-weight: 600;
  border: none; border-radius: var(--radius);
  cursor: pointer;
  transition: opacity .15s, transform .1s;
}
.btn-primary:hover { opacity: .9; }
.btn-primary:active { transform: scale(.98); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }

.btn-secondary {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px;
  background: var(--card);
  color: var(--text);
  font-size: .85rem;
  font-weight: 500;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.btn-secondary:hover { background: var(--border-soft); border-color: var(--text-3); }

.btn-danger {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px;
  background: transparent;
  color: var(--red);
  font-size: .85rem;
  font-weight: 500;
  border: 1px solid var(--red);
  border-radius: var(--radius);
  cursor: pointer;
  opacity: .8;
  transition: opacity .15s, background .15s;
}
.btn-danger:hover { opacity: 1; background: rgba(248,113,113,.08); }

.btn-icon {
  background: none; border: none;
  color: var(--text-2);
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  display: inline-flex; align-items: center; justify-content: center;
  transition: color .15s, background .15s;
}
.btn-icon:hover { color: var(--text); background: var(--card); }

.btn-sm {
  padding: 5px 10px;
  font-size: .78rem;
}

/* ── App Shell ───────────────────────────────────────────────────────────── */
.app { display: flex; flex-direction: column; min-height: 100vh; }
.app.hidden { display: none; }

/* ── Header ──────────────────────────────────────────────────────────────── */
.app-header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  position: sticky; top: 0; z-index: 100;
  flex-shrink: 0;
}
.header-left { display: flex; align-items: center; gap: 10px; }
.header-brand {
  font-family: var(--mono);
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: -.03em;
}
.header-right { display: flex; align-items: center; gap: 8px; }

.user-pill {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 10px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 20px;
  cursor: pointer;
  font-size: .82rem;
  color: var(--text-2);
  transition: border-color .15s;
}
.user-pill:hover { border-color: var(--text-3); }
#user-initial {
  width: 22px; height: 22px;
  background: var(--accent-dim);
  border: 1px solid var(--accent);
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: .72rem;
  font-weight: 600;
  color: var(--accent);
  font-family: var(--mono);
}

/* ── App Body ─────────────────────────────────────────────────────────────── */
.app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
  transition: transform .25s ease;
}

.sidebar-nav { padding: 10px 8px 24px; }

.nav-section {
  font-family: var(--mono);
  font-size: .65rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--text-3);
  padding: 14px 8px 6px;
}

.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px;
  border-radius: 7px;
  font-size: .85rem;
  font-weight: 450;
  color: var(--text-2);
  cursor: pointer;
  transition: background .12s, color .12s;
  user-select: none;
  border: none; background: none; width: 100%; text-align: left;
}
.nav-item:hover { background: var(--card); color: var(--text); }
.nav-item.active {
  background: var(--accent-dim);
  color: var(--accent);
  font-weight: 600;
}
.nav-item svg { flex-shrink: 0; opacity: .7; }
.nav-item.active svg { opacity: 1; }
.nav-item.logout { color: var(--red); margin-top: 8px; }
.nav-item.logout:hover { background: rgba(248,113,113,.08); }

.sidebar-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 50;
}

/* ── Main Content ────────────────────────────────────────────────────────── */
.main-content {
  flex: 1;
  overflow-y: auto;
  background: var(--bg);
  position: relative;
}

.content-loader {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  z-index: 10;
  opacity: 0; pointer-events: none;
  transition: opacity .15s;
}
.content-loader.visible { opacity: 1; pointer-events: all; }

.spinner {
  width: 32px; height: 32px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Page Layout ─────────────────────────────────────────────────────────── */
.page { padding: 24px 28px; max-width: 1100px; }
.page-header {
  display: flex; align-items: flex-start; justify-content: space-between;
  margin-bottom: 24px; gap: 16px;
}
.page-title {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.2;
}
.page-sub {
  font-size: .82rem;
  color: var(--text-3);
  margin-top: 3px;
  font-family: var(--mono);
}
.page-actions { display: flex; gap: 8px; align-items: center; flex-shrink: 0; }

/* ── Cards ───────────────────────────────────────────────────────────────── */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
}
.card-title {
  font-size: .7rem;
  font-family: var(--mono);
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-3);
  margin-bottom: 10px;
}
.card-value {
  font-size: 1.6rem;
  font-weight: 600;
  font-family: var(--mono);
  color: var(--text);
  line-height: 1;
}
.card-sub {
  font-size: .78rem;
  color: var(--text-3);
  margin-top: 6px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

/* ── Tables ──────────────────────────────────────────────────────────────── */
.table-wrap {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
table {
  width: 100%; min-width: 520px; border-collapse: collapse;
  font-size: .84rem;
}
thead th {
  font-family: var(--mono);
  font-size: .68rem;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-3);
  padding: 11px 16px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-weight: 500;
  background: var(--surface);
}
tbody tr {
  border-bottom: 1px solid var(--border-soft);
  transition: background .1s;
}
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: var(--border-soft); }
td { padding: 12px 16px; color: var(--text-2); vertical-align: middle; }
td:first-child { color: var(--text); font-weight: 500; }

/* ── Badges ──────────────────────────────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px;
  border-radius: 20px;
  font-size: .72rem;
  font-family: var(--mono);
  font-weight: 500;
}
.badge-green { background: var(--accent-dim); color: var(--accent); border: 1px solid var(--accent-glow); }
.badge-red { background: rgba(248,113,113,.12); color: var(--red); border: 1px solid rgba(248,113,113,.25); }
.badge-amber { background: rgba(251,191,36,.10); color: var(--amber); border: 1px solid rgba(251,191,36,.25); }
.badge-blue { background: rgba(96,165,250,.10); color: var(--blue); border: 1px solid rgba(96,165,250,.25); }
.badge-gray { background: var(--border); color: var(--text-3); }

.dot {
  width: 6px; height: 6px; border-radius: 50%; background: currentColor;
}
.dot-pulse {
  animation: pulse 1.8s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: .5; transform: scale(.8); }
}

/* ── Status Ring ─────────────────────────────────────────────────────────── */
.status-ring-wrap {
  display: flex; flex-direction: column; align-items: center;
  gap: 16px; padding: 32px 0;
}
.status-ring {
  width: 80px; height: 80px;
  border-radius: 50%;
  border: 2px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  position: relative;
  transition: border-color .3s;
}
.status-ring.connected {
  border-color: var(--accent);
  box-shadow: 0 0 0 8px var(--accent-dim);
  animation: ring-pulse 2.4s ease-in-out infinite;
}
.status-ring.disconnected { border-color: var(--border); }
.status-ring.pairing { border-color: var(--amber); animation: ring-pulse 1.2s ease-in-out infinite; }
@keyframes ring-pulse {
  0%,100% { box-shadow: 0 0 0 8px var(--accent-dim); }
  50% { box-shadow: 0 0 0 16px transparent; }
}
.status-ring svg { width: 32px; height: 32px; }
.status-ring.connected svg { color: var(--accent); }
.status-ring.disconnected svg { color: var(--text-3); }
.status-ring.pairing svg { color: var(--amber); }

/* ── Pairing Code ────────────────────────────────────────────────────────── */
.pairing-code-block {
  display: flex; align-items: center; justify-content: center;
  font-family: var(--mono);
  font-size: 2.2rem;
  font-weight: 600;
  letter-spacing: .18em;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px 32px;
  margin: 20px 0;
  user-select: all;
}

/* ── Toast ───────────────────────────────────────────────────────────────── */
#toast-container {
  position: fixed; bottom: 20px; right: 20px;
  display: flex; flex-direction: column; gap: 8px;
  z-index: 9999;
  pointer-events: none;
}
.toast {
  min-width: 260px; max-width: 360px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: .84rem;
  color: var(--text);
  box-shadow: var(--shadow);
  display: flex; align-items: flex-start; gap: 10px;
  pointer-events: all;
  animation: toast-in .25s ease;
}
.toast.fading { animation: toast-out .25s ease forwards; }
.toast-icon { flex-shrink: 0; margin-top: 1px; }
.toast.success { border-left: 3px solid var(--accent); }
.toast.error { border-left: 3px solid var(--red); }
.toast.info { border-left: 3px solid var(--blue); }
.toast.warn { border-left: 3px solid var(--amber); }
@keyframes toast-in { from { opacity:0; transform: translateY(8px); } to { opacity:1; transform: none; } }
@keyframes toast-out { to { opacity:0; transform: translateY(8px); } }

/* ── Modal ───────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 500;
  padding: 16px;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 100%; max-width: 480px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  font-size: .9rem;
  font-weight: 600;
  color: var(--text);
}
#modal-body { padding: 20px; }

/* ── Skeleton Loaders ────────────────────────────────────────────────────── */
.skeleton {
  background: linear-gradient(90deg, var(--card) 25%, var(--border-soft) 50%, var(--card) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: 6px;
}
@keyframes shimmer { to { background-position: -200% 0; } }
.sk-line { height: 14px; margin-bottom: 10px; }
.sk-line:last-child { width: 60%; }

/* ── Misc ─────────────────────────────────────────────────────────────────── */
.divider { border: none; border-top: 1px solid var(--border); margin: 20px 0; }

.empty-state {
  padding: 48px 24px;
  text-align: center;
  color: var(--text-3);
}
.empty-state svg { width: 40px; height: 40px; margin: 0 auto 12px; display: block; opacity: .4; }
.empty-state p { font-size: .88rem; line-height: 1.5; }

.mono { font-family: var(--mono); }
.text-accent { color: var(--accent); }
.text-red { color: var(--red); }
.text-2 { color: var(--text-2); }

.row { display: flex; gap: 12px; align-items: center; }
.row-between { display: flex; justify-content: space-between; align-items: center; gap: 12px; }

.search-bar {
  display: flex; gap: 10px; align-items: center;
  margin-bottom: 16px;
}
.search-bar input {
  flex: 1;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: .88rem;
  padding: 8px 12px;
  outline: none;
}
.search-bar input:focus { border-color: var(--accent); }

.section-title {
  font-size: .72rem;
  font-family: var(--mono);
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--text-3);
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.tag {
  display: inline-block;
  padding: 2px 8px;
  background: var(--border);
  border-radius: 4px;
  font-size: .72rem;
  font-family: var(--mono);
  color: var(--text-2);
}

/* ── Pagination ──────────────────────────────────────────────────────────── */
.pagination {
  display: flex; gap: 6px; align-items: center; justify-content: center;
  padding: 16px 0;
}
.page-btn {
  padding: 5px 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: .8rem;
  font-family: var(--mono);
  color: var(--text-2);
  cursor: pointer;
  transition: all .12s;
}
.page-btn:hover, .page-btn.active { border-color: var(--accent); color: var(--accent); }

/* ── Inbox Messages ──────────────────────────────────────────────────────── */
.msg-bubble-wrap { display: flex; flex-direction: column; gap: 12px; padding: 16px 0; }
.msg-bubble {
  max-width: 72%;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: .88rem;
  line-height: 1.5;
}
.msg-bubble.out {
  background: var(--accent-dim);
  border: 1px solid var(--accent-glow);
  align-self: flex-end;
  color: var(--text);
}
.msg-bubble.in {
  background: var(--card);
  border: 1px solid var(--border);
  align-self: flex-start;
  color: var(--text);
}
.msg-meta { font-size: .7rem; color: var(--text-3); margin-top: 4px; font-family: var(--mono); }

/* ── Toggle ──────────────────────────────────────────────────────────────── */
.toggle-wrap { display: flex; align-items: center; gap: 10px; }
.toggle {
  width: 38px; height: 22px;
  background: var(--border);
  border-radius: 11px;
  position: relative;
  cursor: pointer;
  transition: background .2s;
  flex-shrink: 0;
  border: none;
}
.toggle.on { background: var(--accent); }
.toggle::after {
  content: '';
  position: absolute;
  top: 3px; left: 3px;
  width: 16px; height: 16px;
  background: #fff;
  border-radius: 50%;
  transition: transform .2s;
}
.toggle.on::after { transform: translateX(16px); }

/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .page { padding: 16px; }
  .sidebar {
    position: fixed;
    top: var(--header-h); left: 0;
    height: calc(100vh - var(--header-h));
    z-index: 60;
    transform: translateX(-100%);
  }
  .sidebar.open { transform: none; }
  .sidebar-overlay.visible { display: block; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .page-header { flex-direction: column; }
  .pairing-code-block { font-size: 1.4rem; padding: 14px 16px; letter-spacing: .1em; }
}
@media (max-width: 400px) {
  .stats-grid { grid-template-columns: 1fr; }
}
