/* ===== Reset & Variables ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1117;
  --bg-card: #1a1c2e;
  --bg-sidebar: #161822;
  --bg-input: #232538;
  --bg-hover: #252840;
  --border: #2a2d42;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --text-heading: #f1f5f9;
  --radius: 8px;
  --sidebar-width: 240px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  min-height: 100vh;
  line-height: 1.5;
  overflow-x: hidden;
}

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 10;
}

.sidebar-brand {
  padding: 20px 20px 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--border);
}

.sidebar-brand h1 {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-heading);
}

.sidebar nav {
  flex: 1;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  color: var(--text-muted);
  text-decoration: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
}

.nav-link:hover { background: var(--bg-hover); color: var(--text); }
.nav-link.active { background: var(--accent); color: #fff; }
.nav-link.active svg { stroke: #fff; }

.sidebar-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}

/* ===== Main ===== */
.main {
  margin-left: var(--sidebar-width);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-sidebar);
  position: sticky;
  top: 0;
  z-index: 5;
}

.topbar h2 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-heading);
}

/* ===== Global Search ===== */
.global-search-wrapper {
  position: relative;
  flex: 0 1 360px;
}
.global-search-input {
  width: 100%;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-main);
  color: var(--text-heading);
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}
.global-search-input:focus {
  border-color: var(--accent);
}
.global-search-input::placeholder {
  color: var(--text-muted);
}
.search-results-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  max-height: 400px;
  overflow-y: auto;
  z-index: 100;
}
.search-result-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}
.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: rgba(99,102,241,0.1); }
.search-result-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.search-result-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-heading);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.search-result-detail {
  font-size: 11px;
  color: var(--text-muted);
}
.search-result-empty {
  padding: 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}
.search-result-type {
  font-size: 10px;
  text-transform: uppercase;
  flex-shrink: 0;
}

.content {
  padding: 24px 28px;
  flex: 1;
}

/* ===== Cards ===== */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.card-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.card-value {
  font-size: 28px;
  font-weight: 700;
  color: var(--text-heading);
}

.card-value.accent { color: var(--accent); }
.card-value.success { color: var(--success); }
.card-value.warning { color: var(--warning); }

/* ===== Tables ===== */
.table-wrapper {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
}

.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}

td {
  padding: 12px 16px;
  font-size: 14px;
  border-bottom: 1px solid var(--border);
}

tr:hover td { background: var(--bg-hover); }
tr:last-child td { border-bottom: none; }

.table-empty {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-muted);
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

/* ===== Badges ===== */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.badge-free { background: #374151; color: #9ca3af; }
.badge-pro { background: #1e3a5f; color: #60a5fa; }
.badge-premium { background: #4c1d95; color: #a78bfa; }
.badge-muted { background: #2a2d42; color: var(--text-muted); }
.badge-success { background: #14532d; color: #4ade80; }
.badge-danger { background: #7f1d1d; color: #fca5a5; }
.badge-warning { background: #78350f; color: #fbbf24; }

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
}

.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { background: #dc2626; }

.btn-outline {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.btn-outline:hover:not(:disabled) { background: var(--bg-hover); color: var(--text); }

.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-icon { padding: 6px; }

/* ===== Inputs ===== */
input[type="text"], input[type="search"], input[type="number"],
input[type="date"], input[type="datetime-local"],
select, textarea {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 8px 12px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
}

.search-input {
  width: 280px;
  padding-left: 12px;
}

/* ===== Forms ===== */
.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-overlay.hidden { display: none; }

.modal {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  width: 440px;
  max-width: 90vw;
}

.modal h3 {
  font-size: 16px;
  margin-bottom: 8px;
  color: var(--text-heading);
}

.modal p {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 20px;
}

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

/* ===== Toast ===== */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  font-size: 14px;
  min-width: 280px;
  animation: slideIn 0.2s ease;
}

.toast.toast-success { border-left: 3px solid var(--success); }
.toast.toast-error { border-left: 3px solid var(--danger); }

@keyframes slideIn {
  from { transform: translateX(40px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ===== Charts ===== */
.chart-container {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 24px;
}

.chart-container h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-heading);
  margin-bottom: 16px;
}

canvas {
  width: 100% !important;
  height: 200px !important;
}

/* ===== Detail Page ===== */
.detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}

.detail-header h3 {
  font-size: 16px;
  color: var(--text-heading);
}

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 24px;
}

.detail-field label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.detail-field p {
  font-size: 14px;
  color: var(--text);
  margin-top: 2px;
}

/* ===== Promo Form ===== */
.promo-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.promo-card .promo-info h4 {
  font-size: 14px;
  color: var(--text-heading);
}

.promo-card .promo-info p {
  font-size: 13px;
  color: var(--text-muted);
}

/* ===== Tabs ===== */
.tab-bar {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 24px;
  overflow-x: auto;
}

.tab-btn {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-muted);
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.15s, border-color 0.15s;
}

.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ===== Utilities ===== */
.text-muted { color: var(--text-muted); }
.text-sm { font-size: 13px; }
.text-xs { font-size: 12px; }
.text-right { text-align: right; }
.text-center { text-align: center; }
.text-mono { font-family: 'SF Mono', 'Fira Code', monospace; font-size: 13px; }
.mb-4 { margin-bottom: 4px; }
.mb-16 { margin-bottom: 16px; }
.mb-24 { margin-bottom: 24px; }
.mt-8 { margin-top: 8px; }
.flex { display: flex; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.gap-8 { gap: 8px; }
.gap-16 { gap: 16px; }
.hidden { display: none !important; }
.truncate { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 200px; }
.clickable { cursor: pointer; }
.clickable:hover { color: var(--accent); }
.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.loading-state {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 60px 0;
  color: var(--text-muted);
}

/* ===== Section Titles ===== */
.section-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-heading);
  margin: 24px 0 16px;
}

/* ===== Funnel Chart ===== */
.funnel-step {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}
.funnel-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--accent);
  border-radius: var(--radius);
  padding: 10px 16px;
  min-height: 40px;
  transition: width 0.3s ease;
}
.funnel-label {
  font-size: 13px;
  font-weight: 500;
  color: #fff;
  white-space: nowrap;
}
.funnel-value {
  font-size: 14px;
  font-weight: 700;
  color: #fff;
}
.funnel-conversion {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ===== Card value variants ===== */
.card-value.danger { color: #ef4444; }

/* ===== Misc ===== */
.mt-24 { margin-top: 24px; }

/* ===== Sidebar Toggle (mobile) ===== */
.sidebar-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius);
  flex-shrink: 0;
}
.sidebar-toggle:hover { background: var(--bg-hover); }

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

/* ===== Responsive: Tablet (≤ 1024px) ===== */
@media (max-width: 1024px) {
  .sidebar { width: 60px; }
  .sidebar-brand h1, .nav-link span, .sidebar-footer { display: none; }
  .nav-link { justify-content: center; padding: 12px; }
  .main { margin-left: 60px; }
  .content { padding: 20px; }
}

/* ===== Responsive: Small tablet / large phone (≤ 768px) ===== */
@media (max-width: 768px) {
  .sidebar-toggle { display: flex; }

  .sidebar {
    left: -240px;
    width: 240px;
    transition: left 0.2s ease;
    z-index: 20;
  }
  .sidebar.open { left: 0; }
  .sidebar.open ~ .sidebar-overlay { display: block; }

  /* Restore sidebar text when open */
  .sidebar.open .sidebar-brand h1,
  .sidebar.open .nav-link span,
  .sidebar.open .sidebar-footer { display: revert; }
  .sidebar.open .nav-link { justify-content: flex-start; padding: 10px 12px; }

  .main { margin-left: 0; }
  .content { padding: 16px; }

  .topbar { padding: 12px 16px; gap: 12px; }
  .topbar h2 { font-size: 16px; }
  .global-search-wrapper { flex: 1 1 auto; min-width: 0; }

  .card-grid { grid-template-columns: 1fr 1fr !important; }
  .detail-grid { grid-template-columns: 1fr; }
  .form-row { grid-template-columns: 1fr; }

  .flex-between { flex-wrap: wrap; }

  .table-wrapper { overflow-x: auto; }
  .table-wrapper th { white-space: nowrap; }
  .table-toolbar { flex-wrap: wrap; }
  .search-input { width: 100%; }
}

/* ===== Responsive: Phone (≤ 480px) ===== */
@media (max-width: 480px) {
  .content { padding: 12px; }
  .topbar { padding: 10px 12px; }

  .card-grid { grid-template-columns: 1fr !important; }
  .card-value { font-size: 22px; }
  .card { padding: 16px; }

  .global-search-wrapper { display: none; }
  .topbar-actions .badge { font-size: 10px; padding: 2px 6px; }

  .flex { flex-wrap: wrap; }
  .detail-header { flex-wrap: wrap; gap: 8px; }

  canvas { height: 160px !important; }

  .toast-container {
    left: 12px;
    right: 12px;
    bottom: 12px;
  }
  .toast { min-width: auto; }

  .modal { width: calc(100vw - 32px); padding: 16px; }

  .promo-card { flex-direction: column; align-items: flex-start; gap: 12px; }

  .funnel-bar { padding: 8px 12px; }
  .funnel-label { font-size: 12px; }
}
