:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --panel: #ffffff;
  --border: #e2e8f0;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --accent: #10b981;
  --accent-hover: #059669;
  --accent-soft: rgba(16, 185, 129, 0.12);
  --blue: #3b82f6;
  --blue-soft: rgba(59, 130, 246, 0.12);
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.12);
  --warning: #f59e0b;
  --warning-soft: rgba(245, 158, 11, 0.12);
  --shadow-sm: 0 1px 3px 0 rgba(0, 0, 0, 0.05);
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -1px rgba(0, 0, 0, 0.04);
  --shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  --radius-lg: 20px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --font-inter: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0;
  font-family: var(--font-inter);
  background: var(--bg);
  color: var(--text-primary);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; outline: none; }
button { cursor: pointer; transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1); border: 1px solid transparent; }
button:disabled { opacity: 0.6; cursor: not-allowed; transform: none !important; }

.hidden { display: none !important; }

/* Entrance Animations */
@keyframes slideUp { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes spin { to { transform: rotate(360deg); } }

.animate-fade { animation: fadeIn 0.3s ease-out; }
.animate-slide { animation: slideUp 0.4s cubic-bezier(0.16, 1, 0.3, 1); }

/* View sections — only the active view is visible */
.view { display: none; }
.view.active { display: block; }

/* Global Loader */
#global-loader {
  position: fixed; top: 20px; right: 20px; z-index: 9999;
  background: var(--surface); padding: 8px 16px; border-radius: 999px;
  box-shadow: var(--shadow); border: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600;
  pointer-events: none; opacity: 0; transition: opacity 0.2s;
}
#global-loader.active { opacity: 1; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--border); border-top-color: var(--text-primary); border-radius: 50%; animation: spin 0.8s linear infinite; }

/* Login Screen */
.login-screen {
  position: fixed; inset: 0; z-index: 1000;
  display: grid; place-items: center;
  background: rgba(248, 250, 252, 0.9);
  backdrop-filter: blur(12px);
}
.login-card {
  width: min(92vw, 420px);
  padding: 48px 40px;
  border-radius: var(--radius-lg);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  text-align: center;
}
.login-card h2 { font-size: 24px; font-weight: 800; letter-spacing: -0.02em; margin-bottom: 8px; }
.login-card p { color: var(--text-secondary); margin-bottom: 32px; font-size: 15px; }
.telegram-login-frame {
  width: 238px;
  max-width: 100%;
  height: 50px;
  border: 0;
  overflow: hidden;
  margin: 0 auto;
  display: block;
}
.login-help {
  margin-top: 14px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-muted);
}
.login-help a {
  color: var(--text-primary);
  font-weight: 700;
  text-decoration: none;
}
.login-help a:hover {
  text-decoration: underline;
}

/* Sidebar & Navigation */
.app-shell {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  min-height: 100vh;
}
.sidebar {
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column;
  padding: 32px 20px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.brand { display: flex; gap: 14px; align-items: center; margin-bottom: 40px; padding: 0 8px; }
.brand-badge {
  width: 44px; height: 44px; border-radius: 12px;
  background: var(--text-primary);
  color: white; display: grid; place-items: center; font-weight: 800; font-size: 20px;
  box-shadow: 0 4px 12px rgba(15, 23, 42, 0.2);
}
.brand-title { font-size: 20px; font-weight: 800; letter-spacing: -0.03em; line-height: 1; }
.brand-subtitle { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.12em; font-weight: 700; margin-top: 2px; }

.sidebar-nav { display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; border-radius: var(--radius-md);
  color: var(--text-secondary); font-weight: 600; font-size: 14px;
  background: transparent; text-align: left;
}
.nav-item svg { width: 20px; height: 20px; opacity: 0.6; transition: opacity 0.2s; }
.nav-item:hover { background: var(--bg); color: var(--text-primary); }
.nav-item:hover svg { opacity: 1; }
.nav-item.active { background: var(--text-primary); color: white; box-shadow: 0 4px 12px rgba(15, 23, 42, 0.1); }
.nav-item.active svg { opacity: 1; color: white; }

.sidebar-footer { margin-top: auto; padding: 0 4px; }
.viewer-card {
  padding: 18px; border-radius: var(--radius-md);
  background: var(--bg); border: 1px solid var(--border);
  transition: all 0.2s;
}
.viewer-card:hover { border-color: #cbd5e1; }
.viewer-role { font-size: 10px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-muted); }
.viewer-id { margin-top: 6px; font-weight: 700; font-size: 14px; color: var(--text-primary); }

/* Buttons */
.primary-btn, .ghost-btn, .danger-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px 20px; border-radius: var(--radius-md);
  font-weight: 600; font-size: 14px;
  white-space: nowrap;
}
.primary-btn { background: var(--text-primary); color: white; }
.primary-btn:hover { background: #1e293b; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(15, 23, 42, 0.15); }
.primary-btn:active { transform: translateY(0); }

.ghost-btn { background: white; border-color: var(--border); color: var(--text-secondary); }
.ghost-btn:hover { background: var(--bg); border-color: #cbd5e1; color: var(--text-primary); }
.ghost-btn.active { background: var(--text-primary); color: white; border-color: var(--text-primary); }

.danger-btn { background: var(--danger-soft); color: var(--danger); }
.danger-btn:hover { background: var(--danger); color: white; box-shadow: 0 4px 12px rgba(239, 68, 68, 0.15); }

.small { padding: 6px 12px; font-size: 12px; border-radius: var(--radius-sm); }

/* Content & Layout */
.content { padding: 48px 40px 100px; max-width: 1440px; margin: 0 auto; width: 100%; }
.topbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 40px; }
.topbar h1 { margin: 0; font-size: 32px; font-weight: 900; letter-spacing: -0.04em; }
.topbar p { margin: 4px 0 0; color: var(--text-secondary); font-size: 16px; font-weight: 500; }

.kpi-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 24px; margin-bottom: 32px;
}
.kpi-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 28px; display: flex; flex-direction: column; gap: 14px;
  box-shadow: var(--shadow-sm); transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative; overflow: hidden;
}
.kpi-card::after {
  content: ""; position: absolute; top: 0; right: 0; width: 80px; height: 80px;
  background: radial-gradient(circle at top right, var(--blue-soft), transparent 70%);
  opacity: 0; transition: opacity 0.3s;
}
.kpi-card:hover { transform: translateY(-4px); box-shadow: var(--shadow); border-color: #cbd5e1; }
.kpi-card:hover::after { opacity: 1; }
.kpi-card span { color: var(--text-secondary); font-size: 14px; font-weight: 600; }
.kpi-card strong { font-size: 32px; font-weight: 900; letter-spacing: -0.03em; line-height: 1; color: var(--text-primary); }
.kpi-card em { font-style: normal; font-size: 12px; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.02em; }

.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 32px; box-shadow: var(--shadow-sm);
}
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 28px; gap: 16px; }
.panel-head h2 { margin: 0; font-size: 22px; font-weight: 800; letter-spacing: -0.03em; }

.two-col-grid { display: grid; grid-template-columns: 1fr 420px; gap: 24px; margin-top: 24px; }
@media (max-width: 1280px) { .two-col-grid { grid-template-columns: 1fr; } }

/* Table Styling */
.table-wrap { overflow-x: auto; margin: 0 -32px; padding: 0 32px; }
table { width: 100%; border-collapse: separate; border-spacing: 0; min-width: 800px; }
th {
  text-align: left; padding: 14px 16px;
  font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.08em;
  color: var(--text-muted); border-bottom: 1px solid var(--border);
}
td { padding: 18px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; font-size: 14px; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #fcfdfe; }

.row-actions { display: flex; gap: 8px; }

.status {
  padding: 4px 12px; border-radius: 999px; font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: 0.04em;
}
.status.active { background: var(--accent-soft); color: var(--accent); }
.status.archived { background: var(--bg); color: var(--text-muted); border: 1px solid var(--border); }
.status.disabled { background: var(--warning-soft); color: var(--warning); }
.status.pending, .status.requested { background: var(--blue-soft); color: var(--blue); }
.status.rejected { background: var(--danger-soft); color: var(--danger); }
.status.paid { background: var(--accent-soft); color: var(--accent); }

/* Role Badges */
.role-badge {
  padding: 2px 8px; border-radius: 6px; font-size: 10px; font-weight: 800; text-transform: uppercase;
  background: var(--bg); color: var(--text-secondary); border: 1px solid var(--border);
  letter-spacing: 0.05em;
}
.role-badge.super_admin { background: var(--blue-soft); color: var(--blue); border-color: transparent; }

/* Forms & Modals */
.modal-overlay {
  position: fixed; inset: 0; z-index: 2000;
  background: rgba(15, 23, 42, 0.4); backdrop-filter: blur(8px);
  display: grid; place-items: center; padding: 20px;
  opacity: 0; pointer-events: none; transition: opacity 0.2s;
}
.modal-overlay:not(.hidden) { opacity: 1; pointer-events: auto; }

.modal-card {
  width: 100%; max-width: 520px;
  background: var(--surface); border-radius: var(--radius-lg);
  padding: 40px; box-shadow: var(--shadow-lg); border: 1px solid var(--border);
  transform: translateY(20px); transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}
.modal-overlay:not(.hidden) .modal-card { transform: translateY(0); }

.modal-card h3 { margin: 0 0 10px; font-size: 24px; font-weight: 900; letter-spacing: -0.03em; }
.modal-card p { color: var(--text-secondary); margin: 0 0 32px; font-size: 16px; font-weight: 500; }

.form-group { display: flex; flex-direction: column; gap: 8px; margin-bottom: 24px; }
.form-group label { font-size: 14px; font-weight: 700; color: var(--text-primary); padding-left: 2px; }
.form-group input {
  padding: 14px 18px; border-radius: var(--radius-md); border: 1px solid var(--border);
  background: var(--bg); transition: all 0.2s; font-size: 15px; font-weight: 500;
}
.form-group input:hover { border-color: #cbd5e1; }
.form-group input:focus { background: white; border-color: var(--blue); box-shadow: 0 0 0 4px var(--blue-soft); }

.form-actions { display: flex; gap: 12px; justify-content: flex-end; margin-top: 40px; }

/* Toasts */
#toast-container {
  position: fixed; bottom: 32px; right: 32px; z-index: 3000;
  display: flex; flex-direction: column; gap: 10px;
}
.toast {
  background: var(--text-primary); color: white;
  padding: 14px 24px; border-radius: var(--radius-md);
  font-size: 14px; font-weight: 700; box-shadow: var(--shadow-lg);
  display: flex; align-items: center; gap: 14px;
  animation: slideUp 0.4s cubic-bezier(0.16, 1, 0.3, 1);
  border-left: 4px solid var(--blue);
}
.toast.success { border-color: var(--accent); }
.toast.error { border-color: var(--danger); }

/* Chart & Others */
.chart-container { height: 380px; position: relative; width: 100%; margin-top: 8px; }
.legend-row { display: flex; gap: 24px; margin-top: 24px; justify-content: center; }
.legend-row span { font-size: 13px; font-weight: 600; color: var(--text-secondary); display: flex; align-items: center; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-right: 8px; }

.funnel-card { height: 100%; }
.funnel-list { list-style: none; padding: 0; margin: 24px 0 0; display: flex; flex-direction: column; gap: 12px; }
.funnel-list li {
  display: flex; justify-content: space-between; align-items: center;
  padding: 18px 24px; border-radius: var(--radius-md); background: var(--bg);
  transition: transform 0.2s; border: 1px solid transparent;
}
.funnel-list li:hover { transform: scale(1.02); border-color: var(--border); }
.funnel-list span { font-size: 14px; font-weight: 600; color: var(--text-secondary); }
.funnel-list b { font-size: 20px; font-weight: 900; letter-spacing: -0.02em; }

/* Empty States */
.empty-state {
  text-align: center; padding: 60px 20px; color: var(--text-muted);
  display: flex; flex-direction: column; align-items: center; gap: 16px;
}
.empty-state svg { width: 48px; height: 48px; opacity: 0.3; }
.empty-state p { font-size: 16px; font-weight: 600; margin: 0; }

/* Mobile Navigation */
.mobile-nav {
  display: none; position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 12px 12px 24px; z-index: 100;
  grid-template-columns: repeat(5, 1fr);
  box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.04);
}
@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .content { padding: 32px 20px 120px; }
  .mobile-nav { display: grid; }
  .nav-item { flex-direction: column; gap: 6px; font-size: 10px; padding: 8px 4px; text-align: center; font-weight: 700; }
  .nav-item svg { width: 24px; height: 24px; opacity: 0.8; }
  .nav-item.active { background: transparent; color: var(--text-primary); box-shadow: none; }
  .nav-item.active svg { color: var(--text-primary); }
  .topbar { flex-direction: column; align-items: flex-start; gap: 20px; }
  .topbar-actions { width: 100%; display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .topbar-actions button { width: 100%; }
  .table-wrap { margin: 0 -20px; padding: 0 20px; }
  .kpi-grid { gap: 16px; }
}

@media (max-width: 480px) {
  .topbar h1 { font-size: 28px; }
  .kpi-card strong { font-size: 28px; }
  .panel { padding: 24px; }
}

