/* ─── Reset & Base ──────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #0f1117;
  --surface: #1a1d26;
  --card: #22252f;
  --border: #2e3240;
  --primary: #f5c518;
  --primary-hover: #e0b200;
  --text: #e8e8f0;
  --text-secondary: #8b8fa3;
  --text-hint: #5a5e70;
  --success: #22c55e;
  --error: #ef4444;
  --info: #3b82f6;
  --warning: #f59e0b;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,.35);
  --sidebar-width: 240px;
}
html { font-size: 14px; }
body {
  font-family: 'Inter', -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  min-height: 100vh;
}

.hidden { display: none !important; }

/* ─── Auth Gate ────────────────────────────────────── */
.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 400;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: min(560px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.auth-card h2 {
  font-size: 1.25rem;
}

.auth-card p {
  color: var(--text-secondary);
}

.auth-actions {
  display: flex;
  gap: 10px;
}

.auth-error {
  color: #fff;
  background: rgba(239, 68, 68, 0.2);
  border: 1px solid rgba(239, 68, 68, 0.4);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: .85rem;
}

/* ─── Sidebar ──────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
}
.sidebar-brand {
  padding: 24px 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
}
.brand-icon { font-size: 28px; }
.brand-text {
  font-size: 20px;
  font-weight: 700;
  color: var(--primary);
}
.sidebar-nav {
  list-style: none;
  padding: 12px 0;
  flex: 1;
}
.nav-item {
  padding: 12px 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 500;
  color: var(--text-secondary);
  transition: all .2s;
  border-left: 3px solid transparent;
}
.nav-item:hover {
  background: rgba(245, 197, 24, .06);
  color: var(--text);
}
.nav-item.active {
  background: rgba(245, 197, 24, .1);
  color: var(--primary);
  border-left-color: var(--primary);
}
.nav-icon { font-size: 18px; }

/* ─── Main ─────────────────────────────────────────── */
#main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  padding: 0;
  display: flex;
  flex-direction: column;
}
#top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 32px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 50;
}
#page-title {
  font-size: 22px;
  font-weight: 700;
}
.top-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

/* ─── Buttons ──────────────────────────────────────── */
.btn {
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  cursor: pointer;
  border: none;
  font-size: .875rem;
  transition: all .2s;
}
.btn-primary {
  background: var(--primary);
  color: #1a1a1a;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}
.btn-outline:hover { border-color: var(--primary); color: var(--primary); }
.btn-danger {
  background: var(--error);
  color: white;
}
.btn-danger:hover { opacity: .85; }
.btn-success {
  background: var(--success);
  color: white;
}
.btn-sm { padding: 4px 10px; font-size: .75rem; }

/* ─── Inputs ───────────────────────────────────────── */
select, .search-input, input[type="text"], textarea {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: .875rem;
  outline: none;
}
select:focus, .search-input:focus, input[type="text"]:focus, textarea:focus {
  border-color: var(--primary);
}
textarea { width: 100%; resize: vertical; }
.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 12px;
}
.form-group input,
.form-group select {
  width: 100%;
}
.form-label {
  font-size: .8rem;
  font-weight: 600;
  color: var(--text-secondary);
}
.checkbox-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: .875rem;
  color: var(--text-secondary);
  cursor: pointer;
}

/* ─── Tabs ─────────────────────────────────────────── */
.tab-content {
  display: none;
  padding: 24px 32px;
  flex: 1;
}
.tab-content.active { display: block; }
.tab-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 12px;
}
.tab-header h2 { font-size: 18px; font-weight: 700; }
.tab-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

/* ─── Stats Grid ───────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.stat-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: var(--shadow);
}
.stat-icon {
  width: 48px; height: 48px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  flex-shrink: 0;
}
.stat-info { display: flex; flex-direction: column; }
.stat-value { font-size: 24px; font-weight: 700; }
.stat-label { font-size: .8rem; color: var(--text-secondary); margin-top: 2px; }

/* ─── Charts ───────────────────────────────────────── */
.charts-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
.chart-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}
.chart-card h3 { font-size: 15px; font-weight: 600; margin-bottom: 12px; }

.device-types-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  color: var(--text-secondary);
  font-size: .9rem;
}

.device-types-platforms {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.device-types-models {
  margin: 0;
  padding-left: 18px;
  display: grid;
  gap: 6px;
}

.device-types-total {
  color: var(--text);
}

.muted {
  color: var(--text-secondary);
}

/* ─── Tables ───────────────────────────────────────── */
.table-wrapper {
  background: var(--card);
  border-radius: var(--radius);
  overflow-x: auto;
  box-shadow: var(--shadow);
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: .875rem;
}
th {
  text-align: left;
  padding: 14px 16px;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255,255,255,.02); }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 12px;
  font-size: .75rem;
  font-weight: 600;
}
.badge-success { background: rgba(34,197,94,.15); color: var(--success); }
.badge-error   { background: rgba(239,68,68,.15); color: var(--error); }
.badge-info    { background: rgba(59,130,246,.15); color: var(--info); }
.badge-warning { background: rgba(245,158,11,.15); color: var(--warning); }
.badge-neutral { background: rgba(139,143,163,.15); color: var(--text-secondary); }

/* ─── Modals ───────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  width: 90%;
  max-width: 600px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}
.modal-header h3 { font-size: 18px; font-weight: 700; }
.modal-close {
  background: none;
  border: none;
  color: var(--text-hint);
  font-size: 20px;
  cursor: pointer;
}
.modal-close:hover { color: var(--text); }

/* ─── Chat ─────────────────────────────────────────── */
.chat-container {
  display: flex;
  height: min(72vh, 720px);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--card);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}
.chat-sidebar {
  width: 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 14px 0;
}
.chat-sidebar h3 {
  padding: 0 16px 10px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-secondary);
}
.chat-channel {
  margin: 2px 10px;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-secondary);
  transition: .2s;
  list-style: none;
}
.chat-channel:hover { background: rgba(255,255,255,.03); color: var(--text); }
.chat-channel.active {
  background: rgba(245,197,24,.12);
  color: var(--primary);
  box-shadow: inset 0 0 0 1px rgba(245,197,24,.22);
}
.channel-icon { font-size: 16px; }
.chat-main { flex: 1; display: flex; flex-direction: column; }
.chat-messages {
  flex: 1;
  padding: 16px 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.chat-bubble {
  max-width: min(78%, 620px);
  padding: 11px 14px;
  border-radius: 12px;
  font-size: .875rem;
  line-height: 1.4;
  word-break: break-word;
}
.chat-bubble.owner {
  align-self: flex-end;
  background: var(--primary);
  color: #1a1a1a;
}
.chat-bubble.other {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
}
.chat-bubble .bubble-author {
  font-size: .72rem;
  font-weight: 600;
  margin-bottom: 4px;
  opacity: .8;
}
.chat-bubble .bubble-time {
  font-size: .68rem;
  opacity: .68;
  margin-top: 4px;
  text-align: right;
}
.chat-input-bar {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  background: linear-gradient(180deg, rgba(26,29,38,.86), rgba(26,29,38,1));
}
.chat-input-bar input {
  flex: 1;
  min-width: 0;
}

/* ─── Hamburger (hidden on desktop) ───────────────── */
.sidebar-toggle-btn { display: none; }
.top-bar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* ─── Sidebar backdrop ─────────────────────────────── */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 99;
}
.sidebar-backdrop.hidden { display: none; }

/* ─── Responsive — Tablet (≤900px) ────────────────── */
@media (max-width: 900px) {
  #sidebar { width: 60px; }
  .brand-text, .nav-item span:not(.nav-icon) { display: none; }
  .nav-item { justify-content: center; padding: 12px; }
  #main-content { margin-left: 60px; }
  .charts-row { grid-template-columns: 1fr; }
  .chat-container { height: min(68vh, 640px); }
  .chat-sidebar { width: 58px; }
  .chat-sidebar h3 { display: none; }
  .chat-channel span:not(.channel-icon) { display: none; }
  .chat-channel { margin: 4px 8px; justify-content: center; }
}

/* ─── Responsive — Mobile (≤640px) ────────────────── */
@media (max-width: 640px) {
  /* Sidebar becomes a full overlay panel */
  #sidebar {
    width: var(--sidebar-width);
    transform: translateX(calc(-1 * var(--sidebar-width)));
    transition: transform .25s ease;
    z-index: 100;
  }
  #sidebar.open {
    transform: translateX(0);
  }
  /* Restore text labels inside open sidebar */
  #sidebar.open .brand-text,
  #sidebar.open .nav-item span:not(.nav-icon) { display: inline; }
  #sidebar.open .nav-item { justify-content: flex-start; padding: 12px 20px; }

  /* Main content fills full width */
  #main-content { margin-left: 0; }

  /* Show hamburger button */
  .sidebar-toggle-btn { display: inline-flex; }

  /* Top bar: stack title + actions on narrow screens */
  #top-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
    padding: 14px 16px;
  }
  .top-actions {
    width: 100%;
    flex-wrap: wrap;
  }
  #period-filter { flex: 1; min-width: 130px; }

  /* Tab content: less padding */
  .tab-content { padding: 16px; }

  /* Stats: 2-column grid on mobile */
  .stats-grid { grid-template-columns: 1fr 1fr; }

  /* Tab header: stack vertically */
  .tab-header { flex-direction: column; align-items: flex-start; }
  .tab-actions { width: 100%; }
  .search-input { width: 100%; }

  /* Chat: sidebar becomes a thin icon strip */
  .chat-container { height: min(66vh, 560px); }
  .chat-sidebar { width: 56px; }
  .chat-sidebar h3 { display: none; }
  .chat-channel span:not(.channel-icon) { display: none; }
  .chat-channel { justify-content: center; padding: 9px 8px; margin: 4px 8px; }
  .chat-bubble { max-width: 92%; }
  .chat-input-bar { padding: 10px; }

  /* Modals: full-width on mobile */
  .modal-content { width: 96%; padding: 16px; max-height: 92vh; }
}

/* ─── Scrollbar ────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-hint); }
