/* EverKey admin dashboard — styles
 * Plan v1 §13 design tokens (Anthropic-style).
 * CSP-safe: no inline styles ever written from JS except
 * element.style.setProperty('--w', '<n>%') on .plan-bar__fill
 * and .cost-monitor__segment — which pairs with `width: var(--w, 0%)`
 * below. See csp-preflight results + canonical pattern in Phase 2
 * Implementation Brief item #1.
 */

/* ========================================================================
   Design tokens
   ==================================================================== */
:root {
  /* Surfaces */
  --color-bg: #ffffff;
  --color-bg-secondary: #fafafa;
  --color-bg-tertiary: #f1f1f0;
  --color-surface-hover: #f5f5f4;

  /* Text */
  --color-text: #1a1a1a;
  --color-text-secondary: #525252;
  --color-text-tertiary: #8a8a8a;

  /* Borders */
  --color-border: rgba(0, 0, 0, 0.08);
  --color-border-strong: rgba(0, 0, 0, 0.16);

  /* Status / accent */
  --color-plan: #378ADD;   /* plan-cycle bar */
  --color-bank: #BA7517;   /* bank bar */
  --color-hot:  #E24B4A;   /* ≥90% usage, critical alert */
  --color-ok:   #3D9970;   /* health dot green */
  --color-warn: #BA7517;   /* alert level info / amber */
  --color-bad:  #E24B4A;   /* alert level warning / red dot */
  --color-unknown: #8a8a8a;/* unknown state / grey dot */

  /* Pill backgrounds (subtle, large rounded rects) */
  --pill-bg-active: rgba(61, 153, 112, 0.10);
  --pill-fg-active: #186b43;
  --pill-bg-depleted: rgba(226, 75, 74, 0.10);
  --pill-fg-depleted: #a52725;
  --pill-bg-expired: rgba(138, 138, 138, 0.12);
  --pill-fg-expired: #5f5f5f;
  --pill-bg-unverified: rgba(186, 117, 23, 0.10);
  --pill-fg-unverified: #7a4a0c;
  --pill-bg-limited: rgba(138, 138, 138, 0.10);
  --pill-fg-limited: #6a6a6a;
  --pill-bg-unknown: rgba(138, 138, 138, 0.12);
  --pill-fg-unknown: #5f5f5f;
  --pill-bg-stuck: rgba(186, 117, 23, 0.14);
  --pill-fg-stuck: #7a4a0c;

  /* Typography */
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui,
               "Helvetica Neue", Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco,
               "Courier New", monospace;

  /* Spacing (4px grid) */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 40px;
  --space-8: 48px;

  /* Radii */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-pill: 999px;

  /* Borders */
  --border-hairline: 0.5px solid var(--color-border);
  --border-strong:   0.5px solid var(--color-border-strong);
}

/* ========================================================================
   Reset
   ==================================================================== */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
h1, h2, h3, p, ul, ol { margin: 0; }
button { font: inherit; color: inherit; cursor: pointer; }
input { font: inherit; }
table { border-collapse: collapse; border-spacing: 0; }

html, body {
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

[hidden] { display: none !important; }

.num {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum";
}
.num-unit {
  color: var(--color-text-secondary);
  font-size: 0.9em;
}

/* ========================================================================
   Buttons
   ==================================================================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: 6px 12px;
  border-radius: var(--radius-md);
  border: var(--border-hairline);
  background: var(--color-bg);
  color: var(--color-text);
  font-weight: 500;
  font-size: 13px;
  line-height: 1;
  height: 30px;
  transition: background 120ms ease, border-color 120ms ease;
}
.btn:hover { background: var(--color-surface-hover); }
.btn:focus-visible {
  outline: 2px solid var(--color-plan);
  outline-offset: 2px;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn--primary {
  background: var(--color-text);
  color: #ffffff;
  border-color: var(--color-text);
  padding: 8px 16px;
  height: 36px;
  font-weight: 500;
}
.btn--primary:hover { background: #000000; }

.btn--secondary {
  background: var(--color-bg);
  border-color: var(--color-border-strong);
}

.btn--ghost {
  background: transparent;
  border-color: transparent;
  color: var(--color-text-secondary);
}
.btn--ghost:hover {
  background: var(--color-surface-hover);
  color: var(--color-text);
}

/* ========================================================================
   LOGIN VIEW
   ==================================================================== */
.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: var(--space-5);
  background: var(--color-bg-secondary);
}
.login__form {
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-6);
  background: var(--color-bg);
  border: var(--border-hairline);
  border-radius: var(--radius-lg);
}
.login__title { font-size: 20px; font-weight: 600; margin-bottom: var(--space-1); }
.login__subtitle { color: var(--color-text-secondary); margin-bottom: var(--space-3); }
.login__label {
  font-size: 13px;
  font-weight: 500;
  color: var(--color-text-secondary);
}
.login__input {
  height: 38px;
  padding: 0 12px;
  border: var(--border-strong);
  border-radius: var(--radius-md);
  background: var(--color-bg);
  color: var(--color-text);
}
.login__input:focus {
  outline: 2px solid var(--color-plan);
  outline-offset: 0;
  border-color: var(--color-plan);
}
.login__error {
  color: var(--color-bad);
  font-size: 13px;
}

/* ========================================================================
   DASHBOARD LAYOUT
   ==================================================================== */
.dashboard {
  max-width: 1280px;
  margin: 0 auto;
  padding: var(--space-5) var(--space-5) var(--space-7);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

/* §6.1 Top bar ----------------------------------------------------------- */
.top-bar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  padding-bottom: var(--space-4);
  border-bottom: var(--border-hairline);
  flex-wrap: wrap;
}
.top-bar__brand { min-width: 0; }
.top-bar__title {
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin-bottom: 2px;
}
.top-bar__meta {
  color: var(--color-text-secondary);
  font-size: 13px;
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  align-items: center;
}
.top-bar__dot { color: var(--color-text-tertiary); }
.top-bar__paused { color: var(--color-warn); font-weight: 500; }
.top-bar__actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.stuck-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  background: var(--pill-bg-stuck);
  color: var(--pill-fg-stuck);
  font-size: 12px;
  font-weight: 500;
  border-radius: var(--radius-pill);
}
.stuck-badge[hidden] { display: none; }
.stuck-badge__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--pill-fg-stuck);
  color: #ffffff;
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
}

/* §6.2 Server strip ------------------------------------------------------ */
.server-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: var(--space-3);
}
.server-card {
  padding: var(--space-4);
  background: var(--color-bg);
  border: var(--border-hairline);
  border-radius: var(--radius-md);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.server-card__header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.server-card__title {
  font-size: 13px;
  font-weight: 500;
  color: var(--color-text);
  letter-spacing: 0;
}
.server-card__detail {
  font-size: 14px;
  font-weight: 500;
  color: var(--color-text);
  font-variant-numeric: tabular-nums;
}
.server-card__sub {
  font-size: 12px;
  color: var(--color-text-tertiary);
}

.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot--ok      { background: var(--color-ok); }
.dot--warn    { background: var(--color-warn); }
.dot--bad     { background: var(--color-bad); }
.dot--unknown { background: var(--color-unknown); }

/* §6.3 Cost monitor ------------------------------------------------------ */
.cost-monitor {
  padding: var(--space-5);
  background: var(--color-bg);
  border: var(--border-hairline);
  border-radius: var(--radius-lg);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.cost-monitor__header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
}
.cost-monitor__title {
  font-size: 14px;
  font-weight: 500;
  color: var(--color-text);
}
.cost-monitor__summary {
  font-size: 14px;
  color: var(--color-text);
}
.cost-monitor__pct {
  color: var(--color-text-secondary);
  margin-left: var(--space-1);
}

.cost-monitor__bar {
  height: 10px;
  background: var(--color-bg-tertiary);
  border-radius: var(--radius-pill);
  overflow: hidden;
  display: flex;
}
.cost-monitor__segment {
  height: 100%;
  width: var(--w, 0%);
  transition: width 240ms ease-out;
}
.cost-monitor__segment--plan { background: var(--color-plan); }
.cost-monitor__segment--bank { background: var(--color-bank); }

.cost-monitor__legend {
  font-size: 13px;
  color: var(--color-text-secondary);
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: var(--space-2);
}
.cost-monitor__sep { color: var(--color-text-tertiary); }
.legend-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  margin-right: 4px;
  vertical-align: -1px;
}
.legend-swatch--plan { background: var(--color-plan); }
.legend-swatch--bank { background: var(--color-bank); }

.cost-monitor__caveat {
  font-size: 12px;
  color: var(--color-text-tertiary);
  line-height: 1.5;
}

/* §6.4 Users table ------------------------------------------------------- */
.users {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}
.users__header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  flex-wrap: wrap;
}
.users__title { font-size: 14px; font-weight: 500; }
.users__summary { font-size: 13px; color: var(--color-text-secondary); }
.users__sep { color: var(--color-text-tertiary); }
.users__footnote { font-size: 12px; color: var(--color-text-tertiary); }

.users-table-wrap {
  background: var(--color-bg);
  border: var(--border-hairline);
  border-radius: var(--radius-md);
  overflow: hidden;
}
.users-table {
  width: 100%;
  font-size: 13px;
}
.users-table thead th {
  text-align: left;
  padding: 10px 12px;
  font-size: 12px;
  font-weight: 500;
  color: var(--color-text-secondary);
  background: var(--color-bg-secondary);
  border-bottom: var(--border-hairline);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.users-table thead th:hover { color: var(--color-text); }
.users-table thead th.is-sorted-asc::after  { content: " ↑"; color: var(--color-text-tertiary); }
.users-table thead th.is-sorted-desc::after { content: " ↓"; color: var(--color-text-tertiary); }

.users-table tbody tr {
  border-bottom: var(--border-hairline);
}
.users-table tbody tr:last-child { border-bottom: none; }
.users-table tbody tr.is-dim { opacity: 0.55; }
.users-table tbody tr:hover { background: var(--color-bg-secondary); }

.users-table td {
  padding: 10px 12px;
  vertical-align: middle;
}

.email {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--color-text);
  overflow-wrap: anywhere;
}
.cell-sync {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--color-text-secondary);
  font-variant-numeric: tabular-nums;
}
.cell-sync--stale { color: var(--color-bad); font-weight: 500; }

/* Pills (status + stuck) */
.pill {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  font-size: 11px;
  font-weight: 500;
  line-height: 1.4;
  letter-spacing: 0;
  white-space: nowrap;
}
.pill--active     { background: var(--pill-bg-active);     color: var(--pill-fg-active); }
.pill--depleted   { background: var(--pill-bg-depleted);   color: var(--pill-fg-depleted); }
.pill--expired    { background: var(--pill-bg-expired);    color: var(--pill-fg-expired); }
.pill--suspended  { background: var(--pill-bg-expired);    color: var(--pill-fg-expired); }
.pill--unverified { background: var(--pill-bg-unverified); color: var(--pill-fg-unverified); }
.pill--limited    { background: var(--pill-bg-limited);    color: var(--pill-fg-limited); }
.pill--unknown    { background: var(--pill-bg-unknown);    color: var(--pill-fg-unknown); }
.pill--stuck {
  background: var(--pill-bg-stuck);
  color: var(--pill-fg-stuck);
  margin-left: var(--space-2);
}

/* Plan-usage cell with inline bar */
.plan-cell {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 140px;
}
.plan-cell__text {
  font-size: 12px;
  color: var(--color-text);
  font-variant-numeric: tabular-nums;
}
.plan-bar {
  height: 4px;
  width: 100%;
  background: var(--color-bg-tertiary);
  border-radius: var(--radius-pill);
  overflow: hidden;
}
.plan-bar__fill {
  height: 100%;
  width: var(--w, 0%);
  background: var(--color-plan);
  transition: width 240ms ease-out;
}
.plan-bar__fill--hot { background: var(--color-hot); }

/* ========================================================================
   Toast
   ==================================================================== */
.toast {
  position: fixed;
  bottom: var(--space-5);
  left: 50%;
  transform: translateX(-50%);
  max-width: 90%;
  padding: 10px 16px;
  background: var(--color-text);
  color: #ffffff;
  border-radius: var(--radius-md);
  font-size: 13px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 100;
}
.toast--error { background: var(--color-bad); }

/* ========================================================================
   Mobile (≤640px)  — refined further in Phase 2F
   ==================================================================== */
@media (max-width: 640px) {
  .dashboard {
    padding: var(--space-4);
    gap: var(--space-4);
  }

  .top-bar {
    flex-direction: column;
    align-items: stretch;
  }
  .top-bar__actions {
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .server-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .cost-monitor { padding: var(--space-4); }

  /* Users table → stacked card layout per Plan v1 §6.5. */
  .users-table-wrap { border: none; background: transparent; }
  .users-table, .users-table thead, .users-table tbody,
  .users-table tr, .users-table td {
    display: block;
    width: 100%;
  }
  .users-table thead { display: none; }
  .users-table tbody tr {
    padding: var(--space-3);
    margin-bottom: var(--space-3);
    background: var(--color-bg);
    border: var(--border-hairline);
    border-radius: var(--radius-md);
  }
  .users-table td {
    padding: 4px 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .users-table td::before {
    content: attr(data-label);
    color: var(--color-text-tertiary);
    font-size: 12px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.03em;
  }
  .plan-cell { min-width: 0; flex: 1 1 auto; align-items: flex-end; }

  /* Cell-email: keep email + stuck pill grouped on the right edge. With
     justify-content: space-between and 3 flex items (::before label, email
     span, stuck pill span), the browser spreads them evenly, leaving a
     visible gap between email and pill. Switching to flex-start plus
     margin-right: auto on ::before absorbs the free space into one gap
     between the label and the email+pill group. */
  .users-table td.cell-email {
    justify-content: flex-start;
    gap: var(--space-2);
  }
  .users-table td.cell-email::before {
    margin-right: auto;
  }

  /* Mobile touch-target minimum. Desktop buttons are 30px tall, which is a
     bit small for finger taps on a phone. 36px is a common floor that
     doesn't feel oversized on desktop when a user resizes the window. */
  .btn { min-height: 36px; }
}

/* ≤400px: server strip collapses further */
@media (max-width: 400px) {
  .server-strip { grid-template-columns: 1fr; }
}
