:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e6e9ef;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #111827;
  --accent-soft: #f1f2f4;
  --success: #15803d;
  --success-soft: #ecfdf3;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --warning: #b45309;
  --warning-soft: #fffbeb;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 2px 6px rgba(16, 24, 40, 0.06);
  --sidebar-w: 248px;
  --topbar-h: 64px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }

/* ---------- Shell layout ---------- */

.admin-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 40;
  display: flex;
  flex-direction: column;
  transition: transform 0.2s ease;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  min-height: var(--topbar-h);
}

.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  flex-shrink: 0;
  overflow: hidden;
}

.brand-mark img { width: 100%; height: 100%; object-fit: cover; }

.brand-mark-lg {
  width: 44px;
  height: 44px;
  border-radius: 11px;
  font-size: 18px;
}

.brand-logo-img {
  display: block;
  max-width: 180px;
  max-height: 44px;
  width: auto;
  height: auto;
  object-fit: contain;
}

.sidebar-nav {
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 500;
}

.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }

.nav-item:hover {
  background: var(--bg);
  color: var(--text);
}

.nav-item.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.nav-divider {
  height: 1px;
  background: var(--border);
  margin: 10px 12px;
}

.sidebar-footer {
  margin-top: auto;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  z-index: 30;
}

.main {
  flex: 1;
  margin-left: var(--sidebar-w);
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: var(--topbar-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-toggle {
  display: none;
  border: none;
  background: transparent;
  padding: 6px;
  cursor: pointer;
  border-radius: 8px;
  color: var(--text);
}

.topbar-toggle:hover { background: var(--bg); }

.topbar-title {
  font-size: 17px;
  font-weight: 650;
  flex: 1;
  min-width: 0;
}

/* ---------- User menu (topbar avatar + dropdown) ---------- */

.user-menu { position: relative; flex-shrink: 0; }

.user-menu-trigger {
  display: flex;
  align-items: center;
  gap: 9px;
  border: 1px solid transparent;
  background: transparent;
  padding: 6px 8px 6px 6px;
  border-radius: 999px;
  cursor: pointer;
}

.user-menu-trigger:hover { background: var(--bg); }

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  flex-shrink: 0;
}

.user-name {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text);
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-menu-trigger .chev { color: var(--muted); flex-shrink: 0; }

.user-dropdown {
  display: none;
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.14);
  min-width: 210px;
  overflow: hidden;
  z-index: 50;
}

.user-dropdown.open { display: block; }

.user-dropdown-email {
  padding: 12px 14px;
  font-size: 12.5px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-dropdown-item {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  border: none;
  background: transparent;
  padding: 11px 14px;
  font-size: 13.5px;
  color: var(--danger);
  cursor: pointer;
  text-align: left;
}

.user-dropdown-item:hover { background: var(--danger-soft); }

.content {
  padding: 24px;
  max-width: 1200px;
  width: 100%;
}

@media (max-width: 900px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); box-shadow: 0 0 0 9999px rgba(15, 23, 42, 0.35); }
  .sidebar.open ~ .sidebar-backdrop { display: block; }
  .main { margin-left: 0; }
  .topbar-toggle { display: inline-flex; }
  .content { padding: 16px; }
  .user-name { display: none; }
}

/* ---------- Page header ---------- */

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.page-header h1 {
  font-size: 20px;
  margin: 0 0 4px;
}

.page-header p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid transparent;
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 550;
  cursor: pointer;
}

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

.btn-outline { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-outline:hover { background: var(--bg); }

.btn-danger-soft { background: var(--danger-soft); color: var(--danger); }
.btn-danger-soft:hover { background: #fde4e4; }

.btn-sm { padding: 5px 10px; font-size: 12px; border-radius: 6px; }

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

/* ---------- Card / table ---------- */

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

table.dt {
  width: 100% !important;
  border-collapse: collapse;
  font-size: 13.5px;
}

table.dt thead th {
  text-align: left;
  padding: 12px;
  color: #45516b;
  font-weight: 700;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  background: #eaeff8;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

table.dt thead th:first-child { border-top-left-radius: 8px; }
table.dt thead th:last-child { border-top-right-radius: 8px; }

table.dt tbody td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

table.dt tbody tr:hover { background: #fafbfc; }

.table-scroll {
  overflow-x: auto;
  border-radius: 8px;
  -webkit-overflow-scrolling: touch;
}

/* ---------- Icon action buttons ---------- */

.icon-btn-group {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 7px;
  color: var(--muted);
  cursor: pointer;
  flex-shrink: 0;
}

.icon-btn svg { width: 16px; height: 16px; }

.icon-btn:hover { background: var(--bg); color: var(--text); border-color: #cbd3e0; }

.icon-btn.icon-btn-danger:hover { background: var(--danger-soft); color: var(--danger); border-color: #f3c6c6; }
.icon-btn.icon-btn-accent:hover { background: var(--accent-soft); color: var(--accent); border-color: #c7cad1; }

/* ---------- Search bar ---------- */

.search-bar-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.search-bar {
  position: relative;
  flex: 1;
  min-width: 220px;
}

.search-bar svg {
  position: absolute;
  left: 13px;
  top: 50%;
  transform: translateY(-50%);
  width: 17px;
  height: 17px;
  color: var(--muted);
  pointer-events: none;
}

.search-bar input {
  width: 100%;
  padding: 10px 14px 10px 40px;
  border: 1px solid var(--border);
  border-radius: 9px;
  font-size: 13.5px;
  background: var(--surface);
  color: var(--text);
}

.search-bar input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.badge-success { background: var(--success-soft); color: var(--success); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-muted { background: var(--bg); color: var(--muted); }

.mono { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12.5px; }
.truncate { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* DataTables control overrides */

.dataTables_wrapper { width: 100%; }

.dt-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 14px;
}

.dataTables_filter, .dataTables_length { margin-bottom: 14px; }
.dt-footer .dataTables_length { margin-bottom: 0; }
.dataTables_filter label, .dataTables_length label {
  font-size: 13px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 8px;
}
.dataTables_filter input, .dataTables_length select {
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 7px 10px;
  font-size: 13px;
  background: var(--surface);
  color: var(--text);
}

.dataTables_info { color: var(--muted); font-size: 12.5px; }

.dataTables_paginate {
  display: flex;
  gap: 4px;
}

.dataTables_paginate .paginate_button {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 11px;
  font-size: 12.5px;
  cursor: pointer;
  color: var(--text);
}

.dataTables_paginate .paginate_button.current {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.dataTables_paginate .paginate_button.disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 4px;
  flex-wrap: wrap;
}

/* ---------- Dashboard: stat cards + chart ---------- */

.stat-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  margin-bottom: 20px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
}

.stat-card .stat-label {
  font-size: 12.5px;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 8px;
}

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

.stat-card.stat-success .stat-value { color: var(--success); }
.stat-card.stat-danger .stat-value { color: var(--danger); }

.chart-card { margin-bottom: 0; }

.date-input {
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 7px 10px;
  font-size: 13px;
  background: var(--surface);
  color: var(--text);
}

.select-input {
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 7px 10px;
  font-size: 13px;
  background: var(--surface);
  color: var(--text);
  min-width: 160px;
}

@media (max-width: 720px) {
  .stat-grid { grid-template-columns: 1fr; }
}

/* ---------- Modal ---------- */

.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.modal-backdrop.open { display: flex; }

.modal {
  background: var(--surface);
  border-radius: 14px;
  width: 100%;
  max-width: 440px;
  box-shadow: 0 20px 45px rgba(15, 23, 42, 0.18);
  max-height: 90vh;
  overflow-y: auto;
}

.modal.modal-wide { max-width: 640px; }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 { margin: 0; font-size: 16px; }

.modal-close {
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 18px;
  color: var(--muted);
  line-height: 1;
}

.modal-body { padding: 20px; }
.modal-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.form-group { margin-bottom: 14px; }
.form-group label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 6px;
}
.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="password"],
.form-group input[type="file"] {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13.5px;
  background: var(--bg);
  color: var(--text);
}
.form-group input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.form-hint { font-size: 12px; color: var(--muted); margin-top: 6px; }

.raw-key-box {
  background: var(--accent-soft);
  border: 1px dashed var(--accent);
  border-radius: 8px;
  padding: 12px;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 13px;
  word-break: break-all;
  margin-top: 10px;
}

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 20px;
  margin-bottom: 14px;
}
.detail-grid .full { grid-column: 1 / -1; }
.detail-label {
  font-size: 11px;
  text-transform: uppercase;
  color: var(--muted);
  letter-spacing: 0.03em;
  margin-bottom: 3px;
}
.detail-value { font-size: 13.5px; word-break: break-word; }
.error-box {
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: 8px;
  padding: 12px;
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
}

.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: var(--text);
  color: #fff;
  padding: 11px 18px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 200;
  opacity: 0;
  transform: translateY(8px);
  transition: all 0.2s ease;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { background: var(--danger); }

@media (max-width: 640px) {
  .detail-grid { grid-template-columns: 1fr; }
  .search-bar-row { flex-direction: column; align-items: stretch; }
  .search-bar-row .btn { width: 100%; justify-content: center; }
  .modal { max-width: 100%; margin: 0 4px; }
  .dataTables_paginate { justify-content: center; }
  table.dt { font-size: 12.5px; }
}
