:root {
  --bg: #f9fafb;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --primary: #3b82f6;
  --primary-soft: #eff6ff;
  --line: #e5e7eb;
  --radius: 10px;
  --sidebar-width: 240px;
  --shadow-soft: 0 1px 3px rgba(15, 23, 42, 0.06);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
}

.auth-check .auth-panel,
.auth-check .app-shell {
  opacity: 0;
  pointer-events: none;
}

h1,
h2,
h3,
h4 {
  margin: 0;
  font-weight: 600;
}

p {
  margin: 0;
}

/* Auth */
.auth-panel {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 32px;
  background: var(--bg);
  z-index: 4;
  transition: opacity 180ms ease;
}

.auth-card {
  width: min(420px, 92vw);
  background: var(--surface);
  border-radius: 12px;
  padding: 32px;
  box-shadow: var(--shadow-soft);
  display: grid;
  gap: 20px;
}

.auth-brand {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 14px;
  align-items: center;
}

.auth-form {
  display: grid;
  gap: 14px;
}

.field {
  display: grid;
  gap: 6px;
  font-size: 0.9rem;
  color: var(--muted);
}

.field input {
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  font-family: inherit;
}

.field select {
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  font-family: inherit;
  background: #fff;
}

.primary-button {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 12px 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 180ms ease;
}

.primary-button:hover {
  background: #2563eb;
}

.auth-status {
  min-height: 18px;
  font-size: 0.9rem;
  color: var(--muted);
}

.auth-status[data-variant="error"] {
  color: #dc2626;
}

.auth-status[data-variant="loading"] {
  color: var(--primary);
}

/* Layout */
.app-shell {
  display: flex;
  min-height: 100vh;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
}

body.is-authenticated .auth-panel {
  opacity: 0;
  pointer-events: none;
}

body.is-authenticated .app-shell {
  opacity: 1;
  pointer-events: auto;
}

.sidebar {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: var(--sidebar-width);
  background: var(--bg);
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  border-right: 1px solid rgba(15, 23, 42, 0.06);
  transform: translateX(0);
  transition: transform 180ms ease;
  z-index: 3;
}

.sidebar-header {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 12px;
  align-items: center;
}

.logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 600;
}

.logo-text {
  display: grid;
  gap: 2px;
  font-size: 0.95rem;
}

.eyebrow {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
}

.nav {
  display: grid;
  gap: 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--text);
  text-decoration: none;
  transition: background 180ms ease, color 180ms ease;
}

.nav-item:hover,
.nav-item.is-active {
  background: var(--primary-soft);
  color: #1d4ed8;
}

.nav-icon svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.sidebar-footer {
  margin-top: auto;
}

.text-button {
  border: none;
  background: transparent;
  font: inherit;
  color: var(--muted);
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 8px;
  transition: color 180ms ease, background 180ms ease;
  text-decoration: none;
}

.text-button:hover {
  background: rgba(59, 130, 246, 0.08);
  color: var(--primary);
}

.content {
  flex: 1;
  margin-left: var(--sidebar-width);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 28px 40px 12px;
  max-width: 1120px;
  margin: 0 auto;
  width: 100%;
}

.topbar-title {
  display: grid;
  gap: 4px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.section-actions {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.export-controls {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  padding: 4px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
}

.export-controls select {
  border: none;
  background: transparent;
  font: inherit;
  color: var(--text);
  outline: none;
}

.icon-button {
  display: none;
  border: none;
  background: transparent;
  padding: 6px;
  cursor: pointer;
}

.icon-button span {
  display: block;
  width: 20px;
  height: 2px;
  background: var(--text);
  margin: 4px 0;
}

.status-pill {
  font-size: 0.8rem;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.06);
  color: var(--muted);
}

.status-pill[data-state="up"] {
  background: rgba(59, 130, 246, 0.12);
  color: #1d4ed8;
}

.user-pill {
  font-size: 0.85rem;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.06);
}

.main {
  padding: 0 40px 64px;
  max-width: 1120px;
  margin: 0 auto;
  width: 100%;
}

.section {
  margin-top: 32px;
  display: grid;
  gap: 16px;
}

.section.is-hidden {
  display: none;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  padding: 12px 14px;
  background: var(--surface);
  border-radius: var(--radius);
}

.filter-bar .field {
  gap: 4px;
  min-width: 160px;
}

.filter-bar .field input {
  padding: 8px 10px;
}

.filter-actions {
  display: inline-flex;
  gap: 8px;
  align-items: center;
}

.section-meta {
  font-size: 0.85rem;
  color: var(--muted);
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}

.kpi {
  background: var(--surface);
  padding: 16px 18px;
  border-radius: var(--radius);
  display: grid;
  gap: 8px;
}

.kpi-label {
  font-size: 0.85rem;
  color: var(--muted);
}

.kpi-value {
  font-size: 1.5rem;
  font-weight: 600;
}

.grid-two {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 16px;
}

.panel {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px;
  display: grid;
  gap: 12px;
}

.panel-header {
  display: grid;
  gap: 4px;
}

.chart {
  display: grid;
  gap: 10px;
}

.chart canvas {
  width: 100%;
  max-height: 260px;
}

.bar {
  display: grid;
  gap: 6px;
}

.bar-track {
  height: 6px;
  background: rgba(15, 23, 42, 0.08);
  border-radius: 999px;
  overflow: hidden;
}

.bar-fill {
  display: block;
  height: 100%;
  background: var(--primary);
}

.bar-label {
  font-size: 0.85rem;
  color: var(--muted);
}

.bar-value {
  font-size: 0.85rem;
  color: var(--text);
  margin-left: 6px;
}

.list {
  display: grid;
  gap: 10px;
}

.list-item {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.04);
}

.list-title {
  font-weight: 600;
}

.list-subtle {
  font-size: 0.8rem;
  color: var(--muted);
}

.list-meta {
  text-align: right;
  font-size: 0.85rem;
}

.sparkline {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  min-height: 120px;
}

.spark-bar {
  flex: 1;
  min-height: 16px;
  background: rgba(59, 130, 246, 0.2);
  border-radius: 999px 999px 6px 6px;
  position: relative;
}

.spark-bar span {
  position: absolute;
  bottom: -18px;
  font-size: 0.7rem;
  color: var(--muted);
  transform: rotate(-45deg);
  transform-origin: bottom left;
}

.table {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 6px;
  display: grid;
  gap: 4px;
  overflow-x: auto;
}

.table-row {
  display: grid;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  align-items: center;
  border-bottom: 1px solid rgba(15, 23, 42, 0.06);
}

.table-row:last-child {
  border-bottom: none;
}

.table-row:hover:not(.table-head) {
  background: rgba(59, 130, 246, 0.06);
}

.table-head {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.table-row[data-layout="orders"] {
  grid-template-columns: repeat(6, minmax(120px, 1fr));
  min-width: 720px;
}

.table-row[data-layout="users"] {
  grid-template-columns: 2fr 1.4fr 1fr 1fr 1fr;
  min-width: 860px;
}

.table-row[data-layout="items"] {
  grid-template-columns: 2fr 1fr 1fr 1fr;
  min-width: 520px;
}

.table-row[data-layout="top-clients"] {
  grid-template-columns: 60px 2fr 1fr 1fr 1fr;
  min-width: 720px;
}

.table-row[data-layout="paid-orders"] {
  grid-template-columns: 140px 140px 2fr 1fr 2fr 1fr 120px;
  min-width: 980px;
}

.rank-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 999px;
  font-size: 0.8rem;
  background: rgba(15, 23, 42, 0.08);
  color: var(--text);
}

.role-badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  text-transform: capitalize;
  background: rgba(15, 23, 42, 0.08);
  color: var(--text);
}

.role-superadmin {
  background: rgba(99, 102, 241, 0.2);
  color: #4338ca;
}

.role-admin {
  background: rgba(59, 130, 246, 0.18);
  color: #1d4ed8;
}

.role-agent {
  background: rgba(16, 185, 129, 0.18);
  color: #047857;
}

.role-client {
  background: rgba(15, 23, 42, 0.08);
  color: var(--muted);
}

.badge-protected {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  background: rgba(245, 158, 11, 0.2);
  color: #b45309;
}

.action-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.users-stats {
  margin-bottom: 8px;
}

.rank-1 {
  background: rgba(59, 130, 246, 0.18);
  color: #1d4ed8;
}

.rank-2 {
  background: rgba(16, 185, 129, 0.18);
  color: #047857;
}

.rank-3 {
  background: rgba(245, 158, 11, 0.2);
  color: #b45309;
}

.row-highlight {
  background: rgba(59, 130, 246, 0.06);
}


.status {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  text-transform: capitalize;
  background: rgba(15, 23, 42, 0.06);
  color: var(--muted);
}

.status-success {
  background: rgba(16, 185, 129, 0.12);
  color: #059669;
}

.status-warning {
  background: rgba(245, 158, 11, 0.16);
  color: #b45309;
}

.status-muted {
  background: rgba(15, 23, 42, 0.08);
  color: var(--muted);
}

.muted {
  color: var(--muted);
  font-size: 0.9rem;
}

.hint {
  font-size: 0.85rem;
  color: var(--muted);
}

.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--text);
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.85rem;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 180ms ease, transform 180ms ease;
  z-index: 5;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

.modal {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
  z-index: 6;
}

.modal.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
}

.modal-panel {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  width: min(760px, 92vw);
  max-height: 90vh;
  overflow-y: auto;
  display: grid;
  gap: 16px;
  box-shadow: var(--shadow-soft);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.modal-close {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid var(--line);
  position: relative;
  background: transparent;
  cursor: pointer;
}

.modal-close span {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 16px;
  height: 2px;
  background: var(--text);
}

.modal-close span:first-child {
  transform: translate(-50%, -50%) rotate(45deg);
}

.modal-close span:last-child {
  transform: translate(-50%, -50%) rotate(-45deg);
}

.modal-form {
  display: grid;
  gap: 16px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}

.field-group {
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 12px;
}

.field-wide {
  grid-column: span 2;
}

.permissions-panel {
  background: rgba(15, 23, 42, 0.04);
  border-radius: var(--radius);
  padding: 12px;
  display: grid;
  gap: 10px;
}

.permissions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 8px;
  font-size: 0.9rem;
  color: var(--muted);
}

.permissions-grid label {
  display: flex;
  align-items: center;
  gap: 8px;
}

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

.modal-status {
  font-size: 0.9rem;
  color: var(--muted);
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease;
  z-index: 2;
}

.detail-page {
  background: var(--bg);
  color: var(--text);
}

.detail-header {
  max-width: 1120px;
  margin: 0 auto;
  padding: 32px 40px 12px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.detail-main {
  max-width: 1120px;
  margin: 0 auto;
  padding: 0 40px 64px;
  display: grid;
  gap: 16px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
}

.detail-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  display: grid;
  gap: 12px;
}

.detail-list {
  display: grid;
  gap: 8px;
}

.detail-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.9rem;
  color: var(--muted);
}

.detail-row strong {
  color: var(--text);
  font-weight: 600;
}

.detail-error {
  background: rgba(239, 68, 68, 0.1);
  color: #b91c1c;
  padding: 12px 16px;
  border-radius: 8px;
}

/* Mobile */
@media (max-width: 900px) {
  .icon-button {
    display: inline-flex;
    flex-direction: column;
    gap: 4px;
  }

  .sidebar {
    transform: translateX(-100%);
  }

  .content {
    margin-left: 0;
  }

  body.sidebar-open .sidebar {
    transform: translateX(0);
  }

  body.sidebar-open .sidebar-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .topbar {
    padding: 20px 20px 8px;
    flex-wrap: wrap;
  }

  .main {
    padding: 0 20px 48px;
  }

  .detail-header,
  .detail-main {
    padding-left: 20px;
    padding-right: 20px;
  }

  .topbar-actions {
    width: 100%;
    justify-content: space-between;
  }
}

@media (max-width: 600px) {
  .topbar-actions {
    flex-direction: column;
    align-items: flex-start;
  }

  .export-controls {
    width: 100%;
    justify-content: space-between;
  }

  .field-group {
    grid-template-columns: 1fr;
  }

  .field-wide {
    grid-column: span 1;
  }
}
