:root {
  --bg: #f6f8fb;
  --surface: #ffffff;
  --surface-2: #f0f4f8;
  --text: #14181f;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #1e6fd9;
  --primary-dark: #1558ad;
  --primary-soft: rgba(30, 111, 217, 0.12);
  --accent: #1e6fd9;
  --blue: #1e6fd9;
  --orange: #ff9f43;
  --green: #2a9d6e;
  --green-dark: #1f7a56;
  --green-soft: rgba(42, 157, 110, 0.14);
  --red: #f5535f;
  --sidebar: #ffffff;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(20, 24, 31, 0.04), 0 8px 24px rgba(20, 24, 31, 0.04);
}

[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1d212a;
  --text: #eef1f6;
  --muted: #8a94a6;
  --border: #262b35;
  --sidebar: #131620;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.25);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body.admin-body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  font-feature-settings: "cv02", "cv03";
  -webkit-font-smoothing: antialiased;
}

a {
  color: inherit;
  text-decoration: none;
}

button,
input,
textarea,
select {
  font: inherit;
}

.admin-shell {
  min-height: 100vh;
}

.dashboard,
.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  gap: 28px;
  width: 248px;
  height: 100vh;
  padding: 24px 16px;
  overflow-y: auto;
  border-right: 1px solid var(--border);
  background: var(--sidebar);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 8px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), var(--green));
  color: #fff;
}

.brand strong,
.brand small {
  display: block;
}

.brand-name {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand small {
  color: var(--muted);
  font-size: 12px;
}

.nav-group,
.nav-stack {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.nav-bottom {
  margin-top: auto;
}

.nav-label {
  margin: 0;
  padding: 4px 12px 6px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.nav-stack a,
.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 42px;
  padding: 10px 12px;
  border-radius: 12px;
  color: var(--muted);
  font-weight: 700;
  transition: background 0.15s, color 0.15s;
}

.nav-stack a:hover,
.nav-link:hover {
  background: var(--surface-2);
  color: var(--text);
}

.nav-stack a.active {
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 600;
}

.nav-ic {
  display: inline-flex;
  color: inherit;
}

.theme-row {
  cursor: default;
}

.theme-row span:nth-child(2) {
  flex: 1;
}

.switch {
  position: relative;
  width: 38px;
  height: 22px;
}

.switch input {
  width: 0;
  height: 0;
  opacity: 0;
}

.slider {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: var(--border);
  cursor: pointer;
  transition: 0.2s;
}

.slider::before {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
  content: "";
  transition: 0.2s;
}

.switch input:checked + .slider {
  background: var(--blue);
}

.switch input:checked + .slider::before {
  transform: translateX(16px);
}

.sidebar-footer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 0 6px rgba(52, 199, 123, 0.14);
}

.workspace,
.main {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-width: 0;
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.topbar-spacer {
  flex: 1;
}

.eyebrow {
  margin: 0;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: 24px;
  font-weight: 700;
  line-height: 1.25;
  letter-spacing: -0.02em;
}

h2 {
  margin-bottom: 4px;
  font-size: 18px;
  font-weight: 700;
  line-height: 1.25;
}

h3 {
  margin: 16px 0 10px;
  font-size: 15px;
  font-weight: 600;
  line-height: 1.25;
}

p {
  color: var(--muted);
}

.top-actions,
.action-row,
.inline-form,
.filter-grid {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.top-actions {
  flex: 1;
  justify-content: flex-end;
  margin-left: auto;
}

.search,
.search-box {
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 420px;
  min-width: min(340px, 100%);
  padding: 9px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--muted);
}

.search input,
.search-box input {
  width: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
  transition: 0.15s;
}

.icon-btn:hover {
  background: var(--surface-2);
  color: var(--text);
}

.menu-btn {
  display: none;
}

.avatar {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--primary), var(--green));
  color: #fff;
  font-size: 13px;
  font-weight: 700;
}

.page-section {
  display: none;
  padding: 28px;
}

.page-section.active {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 18px;
}

.metric-card,
.panel {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.metric-card {
  position: relative;
  min-height: 146px;
  padding: 22px;
  overflow: hidden;
}

.metric-card::after {
  position: absolute;
  right: -28px;
  bottom: -32px;
  width: 96px;
  height: 96px;
  border-radius: 999px;
  background: rgba(30, 111, 217, 0.12);
  content: "";
}

.metric-card span,
.metric-card small {
  display: block;
  color: var(--muted);
  font-weight: 700;
}

.metric-card strong {
  display: block;
  margin: 14px 0 6px;
  color: var(--text);
  font-size: 30px;
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.02em;
}

.metric-card.accent {
  background: linear-gradient(135deg, var(--primary), var(--green));
}

.metric-card.accent strong,
.metric-card.accent span,
.metric-card.accent small {
  color: var(--surface);
}

.content-grid,
.section-grid {
  display: grid;
  gap: 24px;
}

.content-grid.two,
.section-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.api-console-grid {
  grid-template-columns: minmax(260px, 360px) minmax(0, 1fr);
}

.panel {
  min-width: 0;
  padding: 22px;
}

.panel-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.panel-heading p {
  margin-bottom: 0;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.form-grid label,
.console-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.form-grid label:has(textarea),
.console-form label:has(textarea) {
  grid-column: 1 / -1;
}

input,
textarea,
select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  outline: 0;
}

input,
select {
  min-height: 42px;
  padding: 9px 14px;
}

textarea {
  min-height: 84px;
  padding: 10px 14px;
  resize: vertical;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 10px 18px;
  border: 0;
  border-radius: 10px;
  font-weight: 800;
  cursor: pointer;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-dark);
  color: #fff;
}

.btn-soft,
.btn-linkish {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
}

.btn-soft:hover,
.btn-linkish:hover {
  background: var(--surface-2);
  color: var(--text);
}

.btn-linkish {
  min-height: 34px;
  padding: 6px 10px;
}

.btn-danger {
  background: var(--red);
  color: #ffffff;
}

.danger-zone {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

form .btn {
  margin-top: 12px;
}

.inline-form .btn,
.filter-grid .btn {
  margin-top: 0;
}

.inline-form.wide {
  margin-top: 12px;
}

.filter-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr)) auto;
  margin-top: 10px;
}

.table-host,
.result-box {
  min-height: 74px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: auto;
}

.result-box {
  padding: 14px;
  color: var(--text);
  white-space: pre-wrap;
}

.result-box:empty::before,
.table-host:empty::before {
  display: block;
  padding: 18px;
  color: var(--muted);
  content: "No data loaded yet.";
}

.empty-state {
  padding: 18px;
  color: var(--muted);
}

.empty-state.error,
.result-box.error {
  color: var(--red);
}

.muted {
  color: var(--muted);
}

.table-host.compact {
  max-height: 330px;
}

.data-table {
  width: 100%;
  min-width: 760px;
  border-collapse: collapse;
}

.data-table th,
.data-table td {
  padding: 14px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.data-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.data-table td {
  color: var(--text);
  font-size: 14px;
}

.data-table tr:hover td {
  background: var(--surface-2);
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--green-soft);
  color: var(--green);
  font-size: 12px;
  font-weight: 600;
}

.pill.danger {
  background: rgba(245, 83, 95, 0.14);
  color: var(--red);
}

.toast-region {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 60;
  display: grid;
  gap: 8px;
}

.toast-message {
  max-width: 360px;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--green);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  font-weight: 700;
}

.toast-message.error {
  border-left-color: var(--red);
}

.endpoint-list {
  display: grid;
  gap: 8px;
  max-height: calc(100vh - 220px);
  overflow: auto;
}

.endpoint-button {
  display: grid;
  gap: 4px;
  width: 100%;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.endpoint-button.active,
.endpoint-button:hover {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.method {
  width: max-content;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--text);
  color: var(--surface);
  font-size: 11px;
  font-weight: 900;
}

.endpoint-path {
  overflow-wrap: anywhere;
  color: var(--muted);
  font-size: 12px;
}

.console-form {
  display: grid;
  gap: 12px;
}

.console-form .console-actions {
  display: flex;
  gap: 10px;
}

.loading {
  opacity: 0.55;
  pointer-events: none;
}

code {
  color: var(--text);
  white-space: normal;
}

@media (max-width: 1100px) {
  .metric-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .content-grid.two,
  .section-grid,
  .api-console-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .menu-btn {
    display: grid;
  }

  .topbar {
    align-items: flex-start;
    flex-wrap: wrap;
    padding: 16px 18px;
  }

  .top-actions {
    width: 100%;
    justify-content: flex-start;
    margin-left: 0;
  }

  .page-section {
    padding: 18px;
  }
}

@media (max-width: 620px) {
  .metric-grid,
  .form-grid,
  .filter-grid {
    grid-template-columns: 1fr;
  }

  .search,
  .search-box {
    min-width: 100%;
  }
}

/* Page content & breadcrumbs */
.page-content {
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 28px;
  flex: 1;
}

.breadcrumbs {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px 28px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.breadcrumbs a:hover {
  color: var(--primary);
}

.breadcrumbs [aria-current="page"] {
  color: var(--text);
  font-weight: 700;
}

.breadcrumb-sep {
  display: inline-flex;
  align-items: center;
  color: var(--muted);
  opacity: 0.5;
}

.breadcrumb-sep svg {
  width: 14px;
  height: 14px;
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.page-header h2 {
  margin: 0 0 4px;
  font-size: 24px;
  font-weight: 700;
  line-height: 1.25;
}

.page-header p {
  margin: 0;
}

.page-header-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-end;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.filter-bar label {
  display: grid;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  min-width: 140px;
}

.tab-bar {
  display: flex;
  gap: 0;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  border-bottom: 2px solid var(--border);
  background: transparent;
  padding: 0;
  border-radius: 0;
  scrollbar-width: thin;
}

.tab-btn {
  flex-shrink: 0;
  padding: 12px 20px;
  border: 0;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.tab-btn:hover {
  color: var(--text);
}

.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  background: var(--primary-soft);
  box-shadow: none;
}

[data-theme="dark"] .tab-btn.active {
  color: #eef1f6;
  border-bottom-color: var(--primary);
  background: rgba(30, 111, 217, 0.18);
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}

.detail-card {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}

.detail-card span {
  display: block;
  margin-bottom: 6px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.detail-card strong {
  font-size: 15px;
  word-break: break-word;
}

.status-badge {
  display: inline-flex;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
}

.status-badge.active { background: var(--green-soft); color: var(--green); }
.status-badge.deleted { background: rgba(245, 83, 95, 0.14); color: var(--red); }
.status-badge.suspended { background: rgba(255, 159, 67, 0.14); color: var(--orange); }

.table-wrap { overflow: auto; }
.table-actions { display: flex; gap: 6px; }
.actions-col { width: 1%; white-space: nowrap; }
.clickable-row { cursor: pointer; }
.btn-sm { min-height: 32px; padding: 6px 12px; font-size: 12px; }

.table-pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}

.pagination-controls { display: flex; align-items: center; gap: 10px; }
.pagination-info, .pagination-page { font-size: 13px; color: var(--muted); font-weight: 600; }
.page-size-select { min-height: 32px; padding: 4px 10px; border-radius: 8px; }

.table-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 40px;
  color: var(--muted);
  font-weight: 600;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Modals */
#modal-host { position: relative; z-index: 100; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(20, 24, 31, 0.45);
  backdrop-filter: blur(2px);
  opacity: 0;
  transition: opacity 0.2s;
}

.modal-backdrop.show { opacity: 1; }

[data-theme="dark"] .modal-backdrop {
  background: rgba(0, 0, 0, 0.72);
  backdrop-filter: blur(4px);
}

.app-modal {
  width: 100%;
  max-width: 520px;
  max-height: 90vh;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 24px 48px rgba(20, 24, 31, 0.18);
  transform: translateY(12px);
  transition: transform 0.2s;
}

[data-theme="dark"] .app-modal {
  border: 1px solid var(--border);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.55), 0 0 0 1px rgba(255, 255, 255, 0.06);
}

.modal-backdrop.show .app-modal { transform: translateY(0); }
.app-modal.modal-lg { max-width: 720px; }

.app-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 22px 16px;
  border-bottom: 1px solid var(--border);
}

.app-modal-header h3 { margin: 0; font-size: 18px; font-weight: 700; }
.app-modal-body { padding: 20px 22px; }
.app-modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 0 22px 20px;
}

.modal-form-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.form-field { display: grid; gap: 6px; }
.form-field span { font-size: 12px; font-weight: 600; color: var(--muted); }
.form-field:has(textarea) { grid-column: 1 / -1; }
.modal-message { margin: 0; color: var(--text); }

.user-menu {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-meta { text-align: right; }
.user-meta strong { display: block; font-size: 13px; }
.user-meta small { color: var(--muted); font-size: 11px; font-weight: 600; }

.nav-admin-only[data-hidden="true"] { display: none; }

/* Enhanced toasts */
.toast-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  min-width: 300px;
  max-width: 400px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: 0 12px 32px rgba(20, 24, 31, 0.12);
  opacity: 0;
  transform: translateX(24px);
  transition: opacity 0.3s, transform 0.3s;
  position: relative;
  overflow: hidden;
}

.toast-item.show { opacity: 1; transform: translateX(0); }
.toast-icon { flex-shrink: 0; margin-top: 1px; }
.toast-success .toast-icon { color: var(--green); }
.toast-error .toast-icon { color: var(--red); }
.toast-info .toast-icon { color: var(--blue); }
.toast-text { flex: 1; font-weight: 600; font-size: 13px; line-height: 1.4; }
.toast-close {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  padding: 0;
  line-height: 1;
}

.toast-progress {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 3px;
  width: 100%;
  background: var(--primary);
  transform-origin: left;
  animation: toast-progress linear forwards;
}

.toast-success .toast-progress { background: var(--green); }
.toast-error .toast-progress { background: var(--red); }

@keyframes toast-progress { from { transform: scaleX(1); } to { transform: scaleX(0); } }

@media (max-width: 860px) {
  .page-content { padding: 18px; }
  .breadcrumbs { padding: 0 18px; }
  .modal-form-grid { grid-template-columns: 1fr; }
}
