/**
 * Admin dashboard (owner only) – "Daylight Paper" design.
 * Colours come ONLY from tokens.css (var(--…)); no colour literals here.
 * Layout: works at 390px (tables scroll inside their own box) and on desktop.
 */

/* ── Layout ─────────────────────────────────────────────────── */

.admin-dashboard {
  padding: 16px;
  max-width: 1200px;
  margin: 0 auto;
  min-width: 0;
  font-family: var(--font-ui);
  color: var(--ink);
  background: var(--paper);
}

.admin-dashboard *,
.admin-dashboard *::before,
.admin-dashboard *::after {
  box-sizing: border-box;
}

.admin-dashboard button,
.admin-dashboard select,
.admin-dashboard input {
  font-family: var(--font-ui);
}

.admin-dashboard button:focus-visible,
.admin-dashboard select:focus-visible,
.admin-dashboard input:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--gold-line);
}

.dashboard-header h1 {
  font-family: var(--font-head);
  font-size: 26px;
  font-weight: 600;
  color: var(--navy);
  margin: 0;
}

.dashboard-subtitle {
  margin-top: 4px;
  font-size: 14px;
  color: var(--ink-2);
}

.dashboard-actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

/* ── Buttons ────────────────────────────────────────────────── */

.dashboard-actions button {
  min-height: 44px;
  padding: 10px 18px;
  border-radius: 8px;
  border: 1px solid var(--field-border);
  background: var(--card);
  color: var(--ink);
  cursor: pointer;
  font-size: 15px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: background-color 0.15s, border-color 0.15s;
}

.dashboard-actions button:hover {
  background: var(--paper-2);
}

.dashboard-actions button:active {
  background: var(--press);
}

.dashboard-actions .btn-refresh {
  background: var(--navy);
  border-color: var(--navy);
  color: var(--on-navy);
}

.dashboard-actions .btn-refresh:hover {
  background: var(--navy-2);
  border-color: var(--navy-2);
}

/* ── Stat cards ─────────────────────────────────────────────── */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 3px solid var(--gold-line);
  border-radius: 12px;
  padding: 20px;
  box-shadow: var(--shadow-1);
  min-width: 0;
}

.stat-card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 8px;
}

.stat-card-badge {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 12px;
  font-weight: 600;
  margin-left: auto;
  white-space: nowrap;
}

.badge-positive {
  background: var(--ok-bg);
  color: var(--ok-ink);
  border: 1px solid var(--line);
}

.badge-negative {
  background: var(--error-bg);
  color: var(--error-ink);
  border: 1px solid var(--line);
}

.badge-neutral {
  background: var(--chip-bg);
  color: var(--chip-ink);
  border: 1px solid var(--line);
}

.stat-card-value {
  font-size: 32px;
  font-weight: 700;
  color: var(--navy);
  margin-bottom: 4px;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

.stat-card-label {
  font-size: 14px;
  color: var(--label);
  font-weight: 600;
}

.stat-card-sublabel {
  font-size: 13px;
  color: var(--ink-3);
  margin-top: 4px;
}

/* Card variants – a thin accent line only */
.stat-card.members  { border-left-color: var(--gold-line); }
.stat-card.active   { border-left-color: var(--ok-ink); }
.stat-card.messages { border-left-color: var(--navy); }
.stat-card.voice    { border-left-color: var(--navy); }
.stat-card.costs    { border-left-color: var(--error-ink); }
.stat-card.revenue  { border-left-color: var(--ok-ink); }
.stat-card.profit   { border-left-color: var(--navy); }
.stat-card.alerts   { border-left-color: var(--error-ink); }

/* ── Sections (credits, alerts, members) ────────────────────── */

.cost-section,
.alerts-section,
.members-section {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 24px;
  box-shadow: var(--shadow-1);
  min-width: 0;
}

.cost-section h2,
.alerts-section h2,
.members-section-header h2 {
  font-family: var(--font-head);
  font-size: 19px;
  font-weight: 600;
  color: var(--navy);
  margin: 0 0 16px;
}

.members-section-header h2 {
  margin: 0;
}

/* Credits usage tiles */
.credits-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
}

.credit-stat-card {
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 16px;
  min-width: 0;
}

.credit-stat-card.total {
  background: var(--card);
  border-color: var(--gold-line);
}

.credit-stat-value {
  font-size: 24px;
  font-weight: 700;
  color: var(--navy);
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
}

.credit-stat-label {
  margin-top: 4px;
  font-size: 14px;
  font-weight: 600;
  color: var(--label);
}

.credit-stat-cost {
  margin-top: 2px;
  font-size: 13px;
  color: var(--ink-2);
}

/* ── Profit summary ─────────────────────────────────────────── */

.profit-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.profit-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-left: 3px solid var(--navy);
  border-radius: 12px;
  padding: clamp(16px, 3vw, 24px);
  text-align: center;
  box-shadow: var(--shadow-1);
  min-width: 0;
}

.profit-card.revenue { border-left-color: var(--ok-ink); }
.profit-card.costs   { border-left-color: var(--error-ink); }
.profit-card.profit  { border-left-color: var(--navy); }
.profit-card.profit.negative { border-left-color: var(--error-ink); }

.profit-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--label);
  margin-bottom: 8px;
}

.profit-value {
  font-size: clamp(24px, 5vw, 34px);
  font-weight: 700;
  line-height: 1.2;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.profit-card.revenue .profit-value { color: var(--ok-ink); }
.profit-card.costs .profit-value   { color: var(--error-ink); }
.profit-card.profit .profit-value  { color: var(--navy); }
.profit-card.profit.negative .profit-value { color: var(--error-ink); }

.profit-detail {
  font-size: 13px;
  color: var(--ink-2);
  margin-top: 4px;
}

.profit-period {
  font-size: 13px;
  color: var(--ink-3);
  margin-top: 4px;
}

/* ── Alerts ─────────────────────────────────────────────────── */

.alert-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--error-bg);
  color: var(--error-ink);
  border-radius: 8px;
  margin-bottom: 8px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--error-ink);
}

.alert-item:last-child {
  margin-bottom: 0;
}

.alert-item.warning {
  background: var(--notice-bg);
  color: var(--notice-ink);
  border-left-color: var(--gold-line);
}

.alert-item.info {
  background: var(--ok-bg);
  color: var(--ok-ink);
  border-left-color: var(--ok-ink);
}

.alert-content {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.alert-text {
  font-size: 15px;
  color: inherit;
}

.alert-count {
  font-weight: 700;
}

.alert-action {
  min-height: 36px;
  padding: 6px 14px;
  background: var(--card);
  border: 1px solid var(--field-border);
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  color: var(--ink);
  flex-shrink: 0;
}

.alert-action:hover {
  background: var(--paper-2);
}

/* ── Members section ────────────────────────────────────────── */

.members-section {
  margin-bottom: 0;
}

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

.members-sort select {
  min-height: 40px;
  padding: 8px 12px;
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--field-border);
  border-radius: 8px;
  font-size: 15px;
}

.members-table-container {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  width: 100%;
}

/* Bulk action bar */
.bulk-action-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--notice-bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.bulk-action-bar[hidden] {
  display: none;
}

.bulk-count {
  font-size: 14px;
  color: var(--notice-ink);
  font-weight: 600;
  flex: 1;
}

.btn-bulk-delete {
  min-height: 36px;
  background: var(--error-bg);
  color: var(--error-ink);
  border: 1px solid var(--error-ink);
  padding: 6px 14px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
}

.btn-bulk-delete:hover {
  background: var(--card);
}

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

.btn-bulk-cancel {
  min-height: 36px;
  background: transparent;
  color: var(--ink-2);
  border: 1px solid var(--field-border);
  padding: 6px 12px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 14px;
}

.btn-bulk-cancel:hover {
  background: var(--press);
  color: var(--ink);
}

/* Checkbox column */
.select-cell {
  width: 36px;
  text-align: center;
  padding: 0 8px !important;
}

.member-select-cb,
#selectAllCb {
  width: 18px;
  height: 18px;
  accent-color: var(--navy);
  cursor: pointer;
}

/* Table */
.members-table-v2 {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.members-table-v2 th {
  background: var(--paper-2);
  padding: 12px 14px;
  text-align: left;
  font-weight: 600;
  color: var(--label);
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
  font-size: 14px;
}

.members-table-v2 th.sortable {
  user-select: none;
}

.members-table-v2 .sort-btn {
  background: none;
  border: none;
  padding: 4px 0;
  margin: 0;
  font: inherit;
  color: inherit;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.members-table-v2 .sort-btn:hover {
  color: var(--navy);
}

.members-table-v2 th .sort-icon {
  opacity: 0.6;
}

.members-table-v2 th.sorted .sort-icon {
  opacity: 1;
}

.members-table-v2 td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
  color: var(--ink);
  font-size: 14px;
}

.members-table-v2 tr:last-child td {
  border-bottom: none;
}

.members-table-v2 tbody tr:hover {
  background: var(--press);
}

/* Member info cell */
.member-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.member-name {
  font-weight: 600;
  color: var(--ink);
}

.member-email {
  font-size: 13px;
  color: var(--ink-3);
  overflow-wrap: anywhere;
}

/* Cost cells */
.cost-cell {
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.cost-cell .amount {
  font-weight: 600;
}

.cost-cell .amount.negative {
  color: var(--error-ink);
}

.cost-cell .amount.positive {
  color: var(--ok-ink);
}

.cost-cell .breakdown {
  font-size: 12px;
  color: var(--ink-3);
}

/* Usage cells */
.usage-cell {
  text-align: center;
}

.usage-breakdown {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.usage-badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 6px;
  font-weight: 600;
  font-size: 12px;
  border: 1px solid var(--line);
  white-space: nowrap;
}

.usage-badge.text,
.usage-badge.chat {
  background: var(--chip-bg);
  color: var(--chip-ink);
}

.usage-badge.voice {
  background: var(--paper-2);
  color: var(--ink-2);
}

.usage-badge.daily {
  background: var(--notice-bg);
  color: var(--notice-ink);
}

.usage-total {
  font-size: 12px;
  color: var(--ink-3);
  white-space: nowrap;
}

/* Profit cell */
.profit-cell {
  text-align: right;
  white-space: nowrap;
}

.profit-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 6px;
  font-weight: 600;
  font-size: 13px;
  border: 1px solid var(--line);
  font-variant-numeric: tabular-nums;
}

.profit-badge.positive {
  background: var(--ok-bg);
  color: var(--ok-ink);
}

.profit-badge.negative {
  background: var(--error-bg);
  color: var(--error-ink);
}

.profit-badge.neutral {
  background: var(--chip-bg);
  color: var(--chip-ink);
}

/* Credits available */
.credits-value {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.credits-value.credits-low {
  color: var(--error-ink);
}

/* Actions cell */
.actions-cell {
  white-space: nowrap;
}

.actions-cell button {
  min-height: 36px;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  margin-right: 4px;
  border: 1px solid var(--field-border);
  background: var(--card);
  color: var(--ink);
}

.actions-cell button:hover {
  background: var(--paper-2);
}

.actions-cell .btn-unlock {
  background: var(--ok-bg);
  color: var(--ok-ink);
  border-color: var(--ok-ink);
}

.actions-cell .btn-unlock:hover {
  background: var(--card);
}

/* ── Loading, empty, error ──────────────────────────────────── */

.dashboard-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: var(--ink-2);
}

.dashboard-loading .spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--line);
  border-top-color: var(--navy);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

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

@media (prefers-reduced-motion: reduce) {
  .dashboard-loading .spinner {
    animation-duration: 3s;
  }
}

.dashboard-empty,
.dashboard-error {
  text-align: center;
  padding: 40px 20px;
  color: var(--ink-2);
}

.dashboard-error {
  background: var(--error-bg);
  color: var(--error-ink);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.dashboard-error button {
  margin-top: 12px;
  min-height: 44px;
  padding: 10px 18px;
  border-radius: 8px;
  border: 1px solid var(--navy);
  background: var(--navy);
  color: var(--on-navy);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}

.dashboard-error button:hover {
  background: var(--navy-2);
}

/* ── Responsive ─────────────────────────────────────────────── */

@media (max-width: 1024px) {
  .profit-summary {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 12px;
  }
}

@media (max-width: 768px) {
  .admin-dashboard {
    padding: 12px;
  }

  .dashboard-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .dashboard-header h1 {
    font-size: 22px;
  }

  .dashboard-actions {
    width: 100%;
    flex-direction: column;
  }

  .dashboard-actions button {
    width: 100%;
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }

  .profit-summary {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .stat-card {
    padding: 16px;
  }

  .stat-card-value {
    font-size: 26px;
  }

  .cost-section,
  .alerts-section,
  .members-section {
    padding: 16px;
  }

  /* the table scrolls inside .members-table-container, the page does not */
  .members-table-v2 {
    min-width: 720px;
  }

  .members-table-v2 th,
  .members-table-v2 td {
    padding: 10px 8px;
  }
}

@media (max-width: 480px) {
  .admin-dashboard {
    padding: 16px;
  }

  .dashboard-header h1 {
    font-size: 21px;
  }

  .stats-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .credits-grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .profit-summary {
    gap: 10px;
  }

  .profit-card {
    padding: 14px;
  }

  .profit-value {
    font-size: 28px;
  }

  .stat-card {
    padding: 14px;
  }

  .stat-card-value {
    font-size: 24px;
  }

  .alert-item {
    flex-direction: column;
    align-items: flex-start;
  }

  .members-table-v2 td {
    font-size: 13px;
  }

  .member-name {
    font-size: 14px;
  }

  .actions-cell button,
  .alert-action,
  .btn-bulk-delete,
  .btn-bulk-cancel {
    min-height: 44px;
    min-width: 44px;
    font-size: 14px;
  }
}
