:root {
  --brand: #F56400;
  --brand-light: #fb923c;
  --brand-deep: #9a3412;
  --brand-glow: rgba(245, 100, 0, 0.15);
  --ink: #0f172a;
  --ink-soft: #334155;
  --muted: #64748b;
  --muted-light: #94a3b8;
  --border: #e2e8f0;
  --border-soft: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --sidebar: #0f172a;
  --sidebar-hover: rgba(255, 255, 255, 0.06);
  --sidebar-active: rgba(245, 100, 0, 0.15);
  --danger: #ef4444;
  --danger-bg: #fef2f2;
  --ok: #16a34a;
  --ok-bg: #f0fdf4;
  --warn: #d97706;
  --warn-bg: #fffbeb;
  --info-bg: #eff6ff;
  --info: #2563eb;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 4px 20px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 20px 50px rgba(15, 23, 42, 0.12);
  --font: 'DM Sans', ui-sans-serif, system-ui, -apple-system, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

[hidden] { display: none !important; }

html { -webkit-font-smoothing: antialiased; }

body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--surface-2);
  min-height: 100vh;
  line-height: 1.5;
}

.screen { min-height: 100vh; }

/* ── Login ── */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  position: relative;
  overflow: hidden;
}

.login-bg {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse 80% 60% at 50% -10%, rgba(245, 100, 0, 0.18), transparent 55%),
    radial-gradient(ellipse 50% 40% at 100% 100%, rgba(234, 88, 12, 0.1), transparent 50%),
    linear-gradient(160deg, #fafaf9 0%, #f1f5f9 50%, #fff7ed 100%);
}

.login-card {
  position: relative;
  width: 100%;
  max-width: 420px;
  padding: 40px 36px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: 24px;
  box-shadow: var(--shadow-lg), 0 0 0 1px rgba(15, 23, 42, 0.04);
}

.login-brand {
  display: flex;
  gap: 16px;
  align-items: center;
  margin-bottom: 32px;
}

.brand-mark {
  width: 52px;
  height: 52px;
  border-radius: 16px;
  background: linear-gradient(135deg, #ea580c 0%, var(--brand) 50%, #fb923c 100%);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 22px;
  box-shadow: 0 8px 24px rgba(245, 100, 0, 0.35);
  flex-shrink: 0;
}

.brand-mark.sm {
  width: 36px;
  height: 36px;
  font-size: 16px;
  border-radius: 10px;
}

.login-brand h1 {
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  color: var(--ink);
}

.login-brand p {
  font-size: 14px;
  color: var(--muted);
  margin-top: 2px;
}

.login-form label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
  margin-bottom: 8px;
  letter-spacing: 0.02em;
}

.input-wrap {
  position: relative;
  margin-bottom: 20px;
}

.input-wrap input {
  margin-bottom: 0;
}

input {
  width: 100%;
  padding: 13px 16px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
  font-size: 15px;
  background: var(--surface);
  color: var(--ink);
  transition: border-color 0.15s, box-shadow 0.15s;
}

input::placeholder { color: var(--muted-light); }

input:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 4px var(--brand-glow);
}

.input-toggle {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  border: none;
  background: var(--surface-2);
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
}

.input-toggle:hover { color: var(--brand); }

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: var(--radius-sm);
  padding: 11px 18px;
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s, background 0.15s;
  white-space: nowrap;
}

.btn:active { transform: scale(0.98); }

.btn-primary {
  background: linear-gradient(135deg, #ea580c, var(--brand));
  color: #fff;
  box-shadow: 0 4px 16px rgba(245, 100, 0, 0.32);
}

.btn-primary:hover {
  box-shadow: 0 8px 24px rgba(245, 100, 0, 0.4);
  transform: translateY(-1px);
}

.btn-block { width: 100%; }

.btn-secondary {
  background: var(--surface);
  border: 1.5px solid var(--border);
  color: var(--ink-soft);
}

.btn-secondary:hover {
  border-color: rgba(245, 100, 0, 0.3);
  color: var(--brand-deep);
}

/* ── Alerts ── */
.alert {
  font-size: 13px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  margin-bottom: 16px;
  line-height: 1.55;
}

.alert-error {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid rgba(239, 68, 68, 0.2);
}

.alert-warn {
  background: var(--warn-bg);
  color: #92400e;
  border: 1px solid rgba(217, 119, 6, 0.2);
}

.alert-info {
  background: var(--info-bg);
  color: #1e40af;
  border: 1px solid rgba(37, 99, 235, 0.15);
}

.alert.ok {
  background: var(--ok-bg);
  color: var(--ok);
  border: 1px solid rgba(22, 163, 74, 0.2);
}

.alert.err {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid rgba(239, 68, 68, 0.2);
}

/* ── App shell ── */
.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--sidebar);
  color: #e2e8f0;
  display: flex;
  flex-direction: column;
  padding: 24px 16px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 12px 28px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  margin-bottom: 20px;
}

.sidebar-brand strong {
  display: block;
  font-size: 15px;
  color: #fff;
  letter-spacing: -0.02em;
}

.sidebar-brand span {
  font-size: 12px;
  color: var(--muted-light);
  font-weight: 500;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 14px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: #94a3b8;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s, color 0.15s;
}

.nav-item svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  opacity: 0.85;
}

.nav-item:hover {
  background: var(--sidebar-hover);
  color: #e2e8f0;
}

.nav-item.active {
  background: var(--sidebar-active);
  color: #fff;
}

.nav-item.active svg { color: var(--brand-light); }

.nav-item--muted { color: #64748b; margin-top: auto; }

.sidebar-foot {
  padding-top: 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.main-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 28px 32px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border-soft);
}

.topbar-eyebrow {
  font-size: 12px;
  font-weight: 600;
  color: var(--brand);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 4px;
}

.topbar h1 {
  font-size: 1.65rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  color: var(--ink);
}

.topbar-actions .btn { display: none; }
.topbar-actions .btn.visible { display: inline-flex; }

.content {
  padding: 28px 32px 48px;
  flex: 1;
}

.panel { display: none; animation: fadeIn 0.25s ease; }
.panel.active { display: block; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── Stats ── */
.stats-row {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

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

.stat-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  color: var(--ink);
  line-height: 1;
}

.stat-value--sm {
  font-size: 1rem;
  font-weight: 700;
  color: var(--ink-soft);
}

.stat-meta {
  display: block;
  margin-top: 10px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
  line-height: 1.4;
}

.grant-card {
  padding: 22px 24px;
  margin-bottom: 16px;
}

.grant-card h2 {
  font-size: 1.05rem;
  margin-bottom: 6px;
}

.grant-card p {
  color: var(--muted);
  font-size: 14px;
  margin-bottom: 16px;
}

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

.grant-grid label,
.filter-bar label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.grant-span-2 {
  grid-column: 1 / -1;
}

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

.inline-fields input {
  flex: 1;
  min-width: 0;
}

.inline-fields select {
  flex: 1.2;
  min-width: 0;
}

.field-hint {
  font-weight: 400;
  font-size: 11px;
  color: var(--muted-light);
  margin-top: 4px;
}

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

.modal[hidden] {
  display: none !important;
}

.modal-card {
  width: 100%;
  max-width: 440px;
  padding: 24px;
}

.modal-card h2 {
  margin-bottom: 16px;
  font-size: 1.1rem;
}

.modal-card label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 14px;
}

.modal-card input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
}

#refresh-customers:not(.visible),
#refresh-discounts:not(.visible),
#refresh-free-licenses:not(.visible) {
  display: none;
}

.grant-grid input,
.grant-grid select,
.filter-bar input,
.filter-bar select {
  font: inherit;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
}

.grant-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: end;
  padding: 16px 18px;
  margin-bottom: 16px;
}

.filter-bar label { min-width: 140px; flex: 1; }

.filter-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 180px;
}

.filter-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

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

.btn-danger {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid rgba(239, 68, 68, 0.25);
}

.btn-danger:hover { background: #fee2e2; }

.license-cell code.copyable {
  cursor: pointer;
  user-select: all;
}

.license-cell code.copyable:hover {
  color: var(--brand-deep);
}

/* ── Cards & table ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.table-wrap { overflow: auto; }

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.data-table th {
  text-align: left;
  padding: 14px 18px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: 16px 18px;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
  color: var(--ink-soft);
}

.data-table tbody tr {
  transition: background 0.12s;
}

.data-table tbody tr:hover {
  background: #fffbf7;
}

.data-table tbody tr:last-child td { border-bottom: none; }

.cell-customer strong {
  display: block;
  color: var(--ink);
  font-weight: 600;
  margin-bottom: 2px;
}

.cell-customer span {
  font-size: 13px;
  color: var(--muted);
}

.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  text-transform: capitalize;
  letter-spacing: 0.02em;
}

.badge--active, .badge--paid, .badge--granted {
  background: var(--ok-bg);
  color: var(--ok);
}

.badge--trialing, .badge--pending {
  background: var(--warn-bg);
  color: var(--warn);
}

.badge--canceled, .badge--revoked, .badge--expired {
  background: var(--danger-bg);
  color: var(--danger);
}

.badge--default {
  background: var(--surface-2);
  color: var(--muted);
}

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 48px 24px !important;
  font-size: 14px;
}

/* ── Plans ── */
.plans-grid {
  display: grid;
  gap: 18px;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  margin-bottom: 24px;
}

.plan-card {
  background: var(--surface);
  border: 1.5px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.plan-card:hover {
  border-color: rgba(245, 100, 0, 0.25);
  box-shadow: var(--shadow-md);
}

.plan-card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 18px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border-soft);
}

.plan-card-head h3 {
  font-size: 15px;
  font-weight: 800;
  color: var(--brand-deep);
  letter-spacing: -0.02em;
}

.plan-card-head .plan-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--brand), var(--brand-light));
}

.plan-card label {
  display: block;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin: 14px 0 6px;
}

.plan-card label:first-of-type { margin-top: 0; }

.plan-card input,
.plan-card select {
  font: inherit;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  width: 100%;
}

.plan-meta {
  font-size: 12px;
  color: var(--muted);
  margin: -4px 0 8px;
}

.plan-meta--warn {
  color: var(--warn);
}

.alert.warn {
  background: var(--warn-bg);
  color: var(--warn);
  border: 1px solid rgba(217, 119, 6, 0.25);
}

.panel-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 16px;
  justify-content: flex-end;
}

/* ── API key card ── */
.key-card {
  max-width: 560px;
  padding: 28px;
}

.key-card-head {
  display: flex;
  gap: 16px;
  margin-bottom: 24px;
}

.key-icon {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  background: #fff7ed;
  border-radius: 14px;
  flex-shrink: 0;
}

.key-card-head h2 {
  font-size: 1.1rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  margin-bottom: 4px;
}

.key-card-head p {
  font-size: 13px;
  color: var(--muted);
}

.key-current {
  background: var(--surface-2);
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  margin-bottom: 24px;
}

.key-current-label {
  display: block;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 6px;
}

.key-current code {
  font-size: 14px;
  font-family: ui-monospace, 'Cascadia Code', monospace;
  color: var(--ink);
  word-break: break-all;
}

.stack-form label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
  margin-bottom: 8px;
}

.stack-form .input-wrap { margin-bottom: 18px; }

code {
  background: transparent;
}

/* ── Responsive ── */
@media (max-width: 900px) {
  .app-shell { flex-direction: column; }

  .sidebar {
    width: 100%;
    height: auto;
    position: relative;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    padding: 16px;
    gap: 8px;
  }

  .sidebar-brand {
    border: none;
    margin: 0;
    padding: 0 8px 0 0;
  }

  .sidebar-nav {
    flex-direction: row;
    flex: 1;
    overflow-x: auto;
  }

  .sidebar-foot {
    border: none;
    padding: 0;
  }

  .nav-item { padding: 10px 12px; white-space: nowrap; }
  .nav-item span { display: none; }

  .topbar, .content { padding-left: 20px; padding-right: 20px; }
}

@media (max-width: 640px) {
  .topbar { flex-direction: column; align-items: flex-start; }
  .stats-row { grid-template-columns: 1fr; }
}
