:root {
  --bg: #0f1220;
  --surface: #ffffff;
  --surface-alt: #f7f8fc;
  --border: #e5e7f0;
  --border-strong: #d7daea;
  --text: #0f1220;
  --text-2: #4b5168;
  --text-3: #8a90a8;
  --primary: #6366F1;
  --primary-hover: #5048e5;
  --primary-soft: #eef0ff;
  --success: #10b981;
  --success-soft: #d4f4e4;
  --danger: #ef4444;
  --danger-soft: #fde2e2;
  --warning: #f59e0b;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px rgba(15, 18, 32, 0.04), 0 1px 3px rgba(15, 18, 32, 0.06);
  --shadow-md: 0 4px 16px rgba(15, 18, 32, 0.06), 0 2px 6px rgba(15, 18, 32, 0.04);
  --shadow-lg: 0 24px 60px rgba(15, 18, 32, 0.14);
}

/* CRITICAL FIX: [hidden] must override display: flex everywhere. */
[hidden] { display: none !important; }

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--surface-alt);
  -webkit-font-smoothing: antialiased;
}

code, pre { font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace; }

h1, h2 { margin: 0; font-weight: 700; letter-spacing: -0.01em; }
h1 { font-size: 22px; }
h2 { font-size: 18px; }
p  { margin: 0; }

.muted { color: var(--text-3); font-size: 13px; }
.error { color: var(--danger); font-size: 13px; margin-top: 10px; }
.success { color: var(--success); font-size: 13px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: var(--radius-sm);
  cursor: pointer; font-size: 13px; font-weight: 500;
  font-family: inherit;
  transition: all 120ms;
}
.btn:hover { background: var(--surface-alt); border-color: var(--text-3); }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-2); }
.btn-ghost:hover { background: var(--surface); border-color: var(--border); }
.btn-danger { background: var(--surface); color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); }
.btn-full { width: 100%; justify-content: center; padding: 11px 14px; }
.btn .icon { font-size: 15px; line-height: 1; }
.btn-sm { padding: 6px 10px; font-size: 12px; }

.linklike {
  background: none; border: none; padding: 0;
  color: var(--text-3); text-decoration: underline;
  cursor: pointer; font-size: 12px; font-family: inherit;
}
.linklike:hover { color: var(--text); }

/* Forms */
form label {
  display: block; margin: 12px 0;
  font-size: 12px; color: var(--text-2); font-weight: 500;
}
form label.check { display: flex; align-items: center; gap: 8px; }
form input[type=text], form input[type=password], form input:not([type]),
form select {
  display: block; width: 100%; margin-top: 6px;
  padding: 10px 12px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  font-size: 14px; font-family: inherit;
  background: var(--surface); color: var(--text);
  transition: border-color 120ms, box-shadow 120ms;
}
form input:focus, form select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
form input[type=color] { width: 100%; height: 42px; padding: 3px; cursor: pointer; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* Login */
.login-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background:
    radial-gradient(1000px 600px at 80% -10%, rgba(99,102,241,0.15), transparent 60%),
    radial-gradient(800px 500px at -10% 80%, rgba(16,185,129,0.10), transparent 60%),
    var(--surface-alt);
  padding: 20px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 36px 32px;
  width: 400px; max-width: 100%;
  box-shadow: var(--shadow-lg);
}
.brand { display: flex; align-items: center; gap: 10px; }
.login-card .brand { margin-bottom: 6px; }
.login-card .brand h1 { font-size: 22px; }
.login-card .muted { margin: 6px 0 22px; }

/* Dashboard layout */
#dashboard { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; flex-shrink: 0;
  background: var(--bg); color: #e5e7f0;
  padding: 20px 16px;
  display: flex; flex-direction: column;
  border-right: 1px solid rgba(255,255,255,0.06);
}
.sidebar .brand { margin-bottom: 28px; color: #fff; }
.brand-title { font-size: 15px; font-weight: 600; letter-spacing: -0.01em; }
.side-nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }
.side-nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius-sm);
  color: #b3b7cc; text-decoration: none; font-size: 13px; font-weight: 500;
  transition: background 120ms, color 120ms;
}
.side-nav a:hover { background: rgba(255,255,255,0.04); color: #fff; }
.side-nav a.active { background: rgba(99,102,241,0.18); color: #fff; }
.side-nav .icon { opacity: 0.7; }

.sidebar-footer { margin-top: auto; padding-top: 16px; border-top: 1px solid rgba(255,255,255,0.08); }
.user-chip { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), #9333ea);
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 600;
}
.user-name { color: #fff; font-size: 13px; font-weight: 500; }
.sidebar-footer .linklike { color: #8a90a8; }

.main { flex: 1; padding: 28px 32px; max-width: 1280px; }
.topbar {
  display: flex; justify-content: space-between; align-items: flex-end;
  margin-bottom: 24px; gap: 16px; flex-wrap: wrap;
}
.topbar .muted { margin-top: 4px; }
.top-actions { display: flex; gap: 8px; }

/* Stats */
.stats-row {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  margin-bottom: 20px;
}
.stat {
  background: var(--surface); border: 1px solid var(--border);
  padding: 16px 18px; border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.stat-label { font-size: 12px; color: var(--text-3); font-weight: 500; }
.stat-value { font-size: 26px; font-weight: 700; margin-top: 4px; letter-spacing: -0.02em; }
.stat .accent { color: var(--primary); }

/* Panel + table */
.panel {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-sm);
  overflow: hidden;
}
.table { width: 100%; border-collapse: collapse; }
.table th, .table td {
  padding: 14px 18px; text-align: left; font-size: 14px;
  border-bottom: 1px solid var(--border);
}
.table th {
  background: var(--surface-alt); color: var(--text-2);
  font-weight: 600; font-size: 11px; letter-spacing: 0.06em;
  text-transform: uppercase;
}
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: var(--surface-alt); }
.table td.actions { white-space: nowrap; }
.table td.actions .btn { margin-right: 6px; }
.site-id {
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px; color: var(--text-3);
  background: var(--surface-alt); padding: 2px 6px; border-radius: 4px;
}
.name-cell { display: flex; align-items: center; gap: 10px; }
.name-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--primary); flex-shrink: 0;
}
.name-text { font-weight: 600; }
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  font-size: 11px; font-weight: 600;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; }
.badge.active   { background: var(--success-soft); color: var(--success); }
.badge.active::before   { background: var(--success); }
.badge.inactive { background: var(--danger-soft);  color: var(--danger); }
.badge.inactive::before { background: var(--danger); }

.empty-state { padding: 60px 20px; text-align: center; color: var(--text-3); }
.empty-state .icon { font-size: 48px; margin-bottom: 12px; display: block; }

/* Modals */
.modal {
  position: fixed; inset: 0;
  background: rgba(15, 18, 32, 0.55);
  backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal-card {
  background: var(--surface); border-radius: 16px;
  padding: 28px; width: 460px; max-width: 100%;
  max-height: 92vh; overflow-y: auto;
  position: relative;
  box-shadow: var(--shadow-lg);
  animation: modalIn 160ms ease-out;
}
.modal-card.wide  { width: 700px; }
.modal-card.xwide { width: 960px; }
@keyframes modalIn {
  from { opacity: 0; transform: translateY(10px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.modal-card h2 { margin-bottom: 4px; }
.modal-card > .muted { margin-bottom: 16px; }
.modal-close {
  position: absolute; top: 14px; right: 14px;
  width: 32px; height: 32px; border-radius: 8px;
  background: transparent; border: none;
  font-size: 20px; color: var(--text-3); cursor: pointer;
  transition: background 120ms, color 120ms;
}
.modal-close:hover { background: var(--surface-alt); color: var(--text); }
.modal-actions {
  display: flex; justify-content: flex-end; gap: 8px;
  margin-top: 20px; align-items: center;
}
.modal-actions .success { margin-right: auto; }
pre#embed-snippet {
  background: #0f172a; color: #e2e8f0;
  padding: 16px; border-radius: 10px;
  font-size: 12px; line-height: 1.6;
  overflow-x: auto; white-space: pre-wrap; word-break: break-all;
  margin: 0;
}

/* Usage modal */
.usage-header {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 12px; flex-wrap: wrap; margin-bottom: 20px;
}
.usage-sub {
  font-size: 11px; color: var(--text-3); font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.1em; margin-bottom: 4px;
}
.range-pills {
  display: inline-flex; background: var(--surface-alt);
  border-radius: 10px; padding: 3px;
  border: 1px solid var(--border);
}
.range-pills button {
  background: transparent; border: none;
  padding: 6px 14px; border-radius: 7px;
  font-size: 12px; font-weight: 600; color: var(--text-2);
  cursor: pointer; font-family: inherit;
  transition: all 120ms;
}
.range-pills button.active {
  background: var(--surface); color: var(--text);
  box-shadow: var(--shadow-sm);
}
.kpi-row {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px;
  margin-bottom: 20px;
}
.kpi {
  background: var(--surface-alt); padding: 14px 16px;
  border-radius: var(--radius); border: 1px solid var(--border);
}
.kpi-label { font-size: 11px; color: var(--text-3); font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
.kpi-value { font-size: 24px; font-weight: 700; margin-top: 4px; letter-spacing: -0.02em; }
.kpi-sub { font-size: 11px; color: var(--text-3); margin-top: 2px; }

.chart-wrap {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px;
  overflow: hidden;
}
#usage-chart { width: 100%; height: 320px; display: block; }
.chart-grid-line { stroke: var(--border); stroke-width: 1; }
.chart-axis-label { fill: var(--text-3); font-size: 11px; font-family: 'Inter'; }
.chart-area { fill: url(#areaGrad); }
.chart-line { fill: none; stroke: var(--primary); stroke-width: 2; stroke-linejoin: round; }
.chart-dot  { fill: var(--primary); opacity: 0; transition: opacity 120ms; }
.chart-hover-col { fill: transparent; }
.chart-dot.hover { opacity: 1; }

.chart-tooltip {
  position: absolute; pointer-events: none;
  background: var(--bg); color: #fff;
  padding: 8px 12px; border-radius: 8px;
  font-size: 12px; line-height: 1.4;
  box-shadow: var(--shadow-md);
  transform: translate(-50%, -110%);
  opacity: 0; transition: opacity 120ms;
  white-space: nowrap; z-index: 200;
}
.chart-tooltip.show { opacity: 1; }
.chart-tooltip-date { font-weight: 600; }
.chart-tooltip-count { color: #c7d2fe; }

/* Responsive */
@media (max-width: 860px) {
  .sidebar { width: 72px; padding: 16px 10px; }
  .brand-title, .side-nav a span:last-child,
  .user-name, .sidebar-footer .linklike { display: none; }
  .main { padding: 20px; }
  .kpi-row { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 520px) {
  .table th:nth-child(3), .table td:nth-child(3) { display: none; }
  .grid-2 { grid-template-columns: 1fr; }
}
