/* ──────────────────────────────────────────────────────────────────────
   Tilly Platform Admin — component system + app shell.
   Built on the design tokens (tokens.css) + brand fonts (fonts.css) +
   Phosphor duotone icons (phosphor.css). Dark theme, indigo/violet accents.
   ────────────────────────────────────────────────────────────────────── */

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

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans, 'Plus Jakarta Sans', system-ui, sans-serif);
  font-size: var(--text-body, 0.875rem);
  line-height: 1.5;
  color: var(--fg-2, #E2E8F0);
  background:
    radial-gradient(900px 480px at 78% -8%, var(--glow-1, rgba(99,102,241,0.10)), transparent 60%),
    radial-gradient(720px 420px at 12% 4%, var(--glow-2, rgba(139,92,246,0.07)), transparent 55%),
    var(--bg-1, #030712);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  transition: background-color .25s ease, color .25s ease;
}
a { color: var(--tilly-light-main, #6366F1); text-decoration: none; }
a:hover { color: var(--tilly-violet, #8B5CF6); }
h1, h2, h3, h4 { font-weight: 700; color: var(--fg-1, #fff); line-height: 1.25; margin: 0 0 .5rem; }
.muted, .text-muted { color: var(--fg-3, #94A3B8); }
code, pre, .mono { font-family: var(--font-mono, ui-monospace, monospace); }

/* scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb, rgba(148,163,184,.22)); border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: var(--scrollbar-thumb-hover, rgba(148,163,184,.38)); }

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

.admin-sidebar {
  flex: 0 0 264px; width: 264px;
  position: sticky; top: 0; align-self: flex-start;
  height: 100vh; overflow-y: auto;
  background: linear-gradient(180deg, var(--sidebar-grad-top, #0c1322) 0%, var(--bg-2, #0F172A) 100%);
  border-right: 1px solid var(--border-1, #1F2937);
  padding: 18px 12px 28px;
  display: flex; flex-direction: column; gap: 6px;
}
.admin-brand {
  display: flex; align-items: center; gap: 11px;
  padding: 6px 10px 16px; margin-bottom: 6px;
  border-bottom: 1px solid var(--border-2, rgba(255,255,255,.08));
}
.admin-brand .logo {
  width: 34px; height: 34px; border-radius: 10px;
  background: var(--tilly-gradient, linear-gradient(135deg,#6366F1,#8B5CF6));
  display: grid; place-items: center; color: var(--fg-on-accent, #fff); font-size: 19px;
  box-shadow: var(--shadow-tilly, 0 8px 24px rgba(99,102,241,.35));
}
.admin-brand .name { font-weight: 800; color: var(--fg-1, #fff); letter-spacing: .01em; font-size: .98rem; }
.admin-brand .sub { display: block; font-size: .68rem; color: var(--fg-4, #64748B); font-weight: 600; letter-spacing: .12em; text-transform: uppercase; }

.nav-group-label {
  font-size: .66rem; font-weight: 700; letter-spacing: .14em; text-transform: uppercase;
  color: var(--fg-4, #64748B); padding: 14px 12px 6px;
}
.admin-sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 11px;
  padding: 9px 11px; border-radius: var(--radius-md, 12px);
  color: var(--fg-3, #94A3B8); font-weight: 600; font-size: .85rem;
  border: 1px solid transparent; position: relative;
  transition: background .15s, color .15s;
}
.nav-item .ph-duotone { font-size: 1.2rem; color: var(--fg-4, #64748B); transition: color .15s; }
.nav-item:hover { background: rgba(99,102,241,.08); color: var(--fg-1, #fff); }
.nav-item:hover .ph-duotone { color: var(--tilly-light-main, #6366F1); }
.nav-item.active {
  background: linear-gradient(90deg, rgba(99,102,241,.20), rgba(139,92,246,.08));
  color: var(--fg-1, #fff);
  border-color: rgba(99,102,241,.30);
}
.nav-item.active::before {
  content: ""; position: absolute; left: -12px; top: 9px; bottom: 9px; width: 3px;
  border-radius: 0 3px 3px 0; background: var(--tilly-light-main, #6366F1);
}
.nav-item.active .ph-duotone { color: var(--tilly-light-main, #6366F1); }

/* ── Main column + topbar ───────────────────────────────────────────── */
.admin-main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
.admin-topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 14px 28px;
  background: var(--topbar-bg, rgba(10,14,26,.72)); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-1, #1F2937);
}
.admin-topbar .page-title { font-size: var(--text-h3, 1.125rem); font-weight: 800; color: var(--fg-1, #fff); margin: 0; }
.admin-topbar .spacer { flex: 1 1 auto; }
.topbar-search {
  display: flex; align-items: center; gap: 8px; min-width: 200px;
  background: var(--bg-3, rgba(15,23,42,.5)); border: 1px solid var(--border-1, #1F2937);
  border-radius: var(--radius-pill, 9999px); padding: 7px 14px; color: var(--fg-3);
}
.topbar-search input { background: none; border: none; outline: none; color: var(--fg-2); font: inherit; width: 100%; }
.topbar-icon {
  display: grid; place-items: center; width: 38px; height: 38px; border-radius: 11px;
  color: var(--fg-3); border: 1px solid var(--border-1); background: var(--bg-3);
}
.topbar-user { display: flex; align-items: center; gap: 10px; }
.topbar-user .avatar {
  width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center;
  background: var(--tilly-gradient); color: var(--fg-on-accent, #fff); font-weight: 700; font-size: .8rem;
}
.topbar-user .who { line-height: 1.15; }
.topbar-user .who b { color: var(--fg-1, #fff); font-size: .82rem; display: block; }
.topbar-user .who span { color: var(--fg-4); font-size: .7rem; }

.admin-content { padding: 26px 28px 60px; flex: 1 1 auto; }
.page-head { margin-bottom: 20px; }
.page-head h1 { font-size: var(--text-h1, 1.875rem); }
.page-head p { color: var(--fg-3); margin: 0; }

/* ── Cards & KPIs ───────────────────────────────────────────────────── */
.card {
  background: linear-gradient(180deg, var(--card-sheen, rgba(255,255,255,.02)), transparent), var(--bg-2, #0F172A);
  border: 1px solid var(--border-1, #1F2937);
  border-radius: var(--radius-lg, 16px);
  padding: 20px; box-shadow: var(--shadow-sm);
}
.card + .card { margin-top: 16px; }
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.card-header h2, .card-header h3 { margin: 0; font-size: var(--text-h4, 1rem); }

.kpi-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 16px; }
.kpi-card {
  background: var(--bg-2, #0F172A); border: 1px solid var(--border-1, #1F2937);
  border-radius: var(--radius-lg, 16px); padding: 18px; position: relative; overflow: hidden;
}
.kpi-card::after {
  content: ""; position: absolute; inset: 0 0 auto auto; width: 120px; height: 120px;
  background: radial-gradient(circle at top right, rgba(99,102,241,.16), transparent 70%);
}
.kpi-card .kpi-label { color: var(--fg-3); font-size: .72rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; }
.kpi-card .kpi-value { font-size: 1.7rem; font-weight: 800; color: var(--fg-1, #fff); margin-top: 6px; }
.kpi-card .kpi-delta { font-size: .78rem; font-weight: 600; margin-top: 4px; }
.kpi-delta.up { color: var(--tilly-success, #10B981); }
.kpi-delta.down { color: var(--tilly-error, #EF3C39); }

/* ── Buttons ────────────────────────────────────────────────────────── */
.btn, button.btn, a.btn {
  display: inline-flex; align-items: center; gap: 8px; cursor: pointer;
  font: inherit; font-weight: 600; font-size: .85rem;
  padding: 9px 16px; border-radius: var(--radius-md, 12px);
  border: 1px solid var(--border-1, #1F2937); background: var(--bg-3, rgba(15,23,42,.5));
  color: var(--fg-2, #E2E8F0); transition: transform .08s, filter .15s, background .15s;
}
.btn:hover { filter: brightness(1.12); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--tilly-button-gradient, linear-gradient(to right,#4F46E5,#6366F1)); border-color: transparent; color: var(--fg-on-accent, #fff); box-shadow: var(--shadow-tilly); }
.btn-danger { background: linear-gradient(to right, var(--tilly-error-2,#DC2626), var(--tilly-error,#EF3C39)); border-color: transparent; color: var(--fg-on-accent, #fff); }
.btn-ghost, .btn.secondary { background: transparent; }
.btn-sm { padding: 6px 11px; font-size: .78rem; border-radius: 9px; }
/* fieldset-as-card: tidy legend into a card-header-like heading */
fieldset.card { border: 1px solid var(--border-1); }
fieldset.card > legend { padding: 0 8px; font-weight: 700; color: var(--fg-1, #fff); font-size: var(--text-h4); }

/* ── Badges / chips ─────────────────────────────────────────────────── */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 10px; border-radius: var(--radius-pill, 9999px);
  font-size: .72rem; font-weight: 700; letter-spacing: .02em;
  background: rgba(148,163,184,.14); color: var(--fg-2);
}
.badge.success, .badge.active, .badge.paid { background: rgba(16,185,129,.16); color: #34d399; }
.badge.warning, .badge.trial, .badge.pending { background: rgba(245,158,11,.16); color: #fbbf24; }
.badge.error, .badge.danger, .badge.suspended, .badge.failed { background: rgba(239,60,57,.16); color: #f87171; }
.badge.info { background: rgba(59,130,246,.16); color: #60a5fa; }
.badge.violet { background: rgba(139,92,246,.16); color: #a78bfa; }

/* ── Tables ─────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; border: 1px solid var(--border-1, #1F2937); border-radius: var(--radius-lg, 16px); }
table.table { width: 100%; border-collapse: collapse; font-size: .85rem; }
table.table th, table.table td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--border-1, #1F2937); white-space: nowrap; }
table.table thead th {
  position: sticky; top: 0; background: var(--thead-bg, #0c1322); color: var(--fg-3);
  font-size: .7rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase;
}
table.table tbody tr:hover { background: var(--row-hover, rgba(99,102,241,.06)); }
table.table tbody tr:last-child td { border-bottom: none; }
table.table a { color: var(--fg-1); font-weight: 600; }

/* ── Forms ──────────────────────────────────────────────────────────── */
label { display: inline-block; font-weight: 600; font-size: .8rem; color: var(--fg-3); margin-bottom: 6px; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--fg-1);
  background: var(--bg-1, #030712); border: 1px solid var(--border-1, #1F2937);
  border-radius: var(--radius-md, 12px); padding: 9px 12px; outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus { border-color: var(--tilly-light-main, #6366F1); box-shadow: 0 0 0 3px rgba(99,102,241,.18); }
input[type="checkbox"], input[type="radio"] { width: auto; accent-color: var(--tilly-main, #4F46E5); }
.form-row { margin-bottom: 14px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px,1fr)); gap: 14px; }

/* ── Flash messages ─────────────────────────────────────────────────── */
.flash {
  padding: 12px 16px; border-radius: var(--radius-md, 12px); margin-bottom: 12px;
  border: 1px solid var(--border-1); font-weight: 600; font-size: .85rem;
  display: flex; align-items: center; gap: 9px;
}
.flash.error { background: rgba(239,60,57,.12); border-color: rgba(239,60,57,.35); color: #fca5a5; }
.flash.warning { background: rgba(245,158,11,.12); border-color: rgba(245,158,11,.35); color: #fcd34d; }
.flash.success { background: rgba(16,185,129,.12); border-color: rgba(16,185,129,.35); color: #6ee7b7; }
.flash.info { background: rgba(59,130,246,.12); border-color: rgba(59,130,246,.35); color: #93c5fd; }

/* ── Inline form validation (replaces native browser bubbles) ───────── */
input.is-invalid, select.is-invalid, textarea.is-invalid {
  border-color: var(--tilly-error, #EF3C39);
  box-shadow: 0 0 0 3px rgba(239, 60, 57, .18);
}
.field-error {
  display: none; align-items: center; gap: 6px;
  color: #fca5a5; font-size: .78rem; font-weight: 600; margin-top: 6px;
}
.field-error.show { display: flex; }
.field-error .ph-duotone { font-size: 1rem; }
@keyframes admin-shake { 0%,100%{transform:translateX(0)} 25%{transform:translateX(-5px)} 75%{transform:translateX(5px)} }
.shake { animation: admin-shake .28s ease; }

/* ── Tabs (underline nav across related pages) ──────────────────────── */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border-1); margin-bottom: 20px; flex-wrap: wrap; }
.tabs a {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 10px 16px; color: var(--fg-3); font-weight: 600; font-size: .88rem;
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs a .ph-duotone { font-size: 1.1rem; }
.tabs a:hover { color: var(--fg-1, #fff); }
.tabs a.active { color: var(--fg-1, #fff); border-bottom-color: var(--tilly-light-main); }

/* ── Filter bar (inline, wrapping row of filter controls) ───────────── */
.filter-bar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.filter-bar.labeled { align-items: flex-end; gap: 12px; }
.filter-field { display: flex; flex-direction: column; gap: 4px; }
.filter-field > label {
  margin: 0; font-size: .68rem; font-weight: 700; letter-spacing: .06em;
  text-transform: uppercase; color: var(--fg-4);
}
.filter-field input, .filter-field select { width: auto; min-width: 150px; }
.filter-field.org input { min-width: 200px; }

/* ── Bulk-action bar (inline; action-aware fields) ──────────────────── */
.bulk-bar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.bulk-bar select, .bulk-bar input { width: auto; }
.bulk-bar .when { display: none; align-items: center; gap: 6px; }
.bulk-bar .when.show { display: inline-flex; }
.bulk-bar .when label { margin: 0; font-size: .72rem; color: var(--fg-4); text-transform: uppercase; letter-spacing: .05em; }
.sel-count { font-size: .8rem; color: var(--fg-3); font-weight: 600; }
table.table td.check, table.table th.check { width: 36px; text-align: center; }

/* ── Table row actions (compact icon buttons) ───────────────────────── */
.row-actions { display: inline-flex; gap: 6px; white-space: nowrap; }
.row-actions form { display: inline; margin: 0; }
.row-actions .btn { padding: 6px 9px; }
.row-actions .btn .ph-duotone { font-size: 1.05rem; }

/* ── Helpers ────────────────────────────────────────────────────────── */
.row { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px,1fr)); gap: 16px; }
.stack > * + * { margin-top: 12px; }
.linklike { background: none; border: none; color: var(--tilly-light-main); cursor: pointer; padding: 0; font: inherit; }

/* ── Auth (login / mfa / enroll / sudo) — centered, no shell ────────── */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.auth-card {
  width: 100%; max-width: 420px;
  background: linear-gradient(180deg, var(--card-sheen, rgba(255,255,255,.03)), transparent), var(--bg-2, #0F172A);
  border: 1px solid var(--border-1, #1F2937); border-radius: var(--radius-2xl, 24px);
  padding: 34px 30px; box-shadow: var(--shadow-lg, 0 25px 50px -12px rgba(0,0,0,.5));
}
.auth-brand { display: flex; flex-direction: column; align-items: center; gap: 12px; margin-bottom: 22px; text-align: center; }
.auth-brand .logo {
  width: 52px; height: 52px; border-radius: 15px; display: grid; place-items: center;
  background: var(--tilly-gradient, linear-gradient(135deg,#6366F1,#8B5CF6)); color: var(--fg-on-accent, #fff); font-size: 28px;
  box-shadow: var(--shadow-tilly, 0 8px 24px rgba(99,102,241,.35));
}
.auth-brand h1 { font-size: 1.25rem; margin: 0; }
.auth-brand p { margin: 0; color: var(--fg-3); font-size: .82rem; }
.auth-card .btn-primary { width: 100%; justify-content: center; margin-top: 6px; }

/* ── Mobile ─────────────────────────────────────────────────────────── */
@media (max-width: 880px) {
  .admin-sidebar { position: fixed; left: 0; top: 0; z-index: 50; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow-lg); }
  .admin-sidebar.open { transform: translateX(0); }
  .admin-content { padding: 18px 16px 48px; }
  .topbar-search { display: none; }
  .nav-toggle { display: grid !important; }
}
.nav-toggle { display: none; }

/* ── Theme toggle (topbar) ──────────────────────────────────────────── */
.theme-toggle { cursor: pointer; }
.theme-toggle .ph-sun { display: none; }
.theme-toggle .ph-moon { display: block; }
[data-theme="light"] .theme-toggle .ph-sun { display: block; }
[data-theme="light"] .theme-toggle .ph-moon { display: none; }

/* ── Light-theme component touch-ups ────────────────────────────────────
   Badges/flashes use bright text tuned for dark surfaces; on light surfaces
   they need darker, higher-contrast text. Brand/semantic accents elsewhere
   read fine on both, so only these tint-on-tint chips are overridden. */
[data-theme="light"] .badge { background: rgba(100,116,139,.14); color: #475569; }
[data-theme="light"] .badge.success, [data-theme="light"] .badge.active, [data-theme="light"] .badge.paid { background: rgba(16,185,129,.14); color: #047857; }
[data-theme="light"] .badge.warning, [data-theme="light"] .badge.trial, [data-theme="light"] .badge.pending { background: rgba(245,158,11,.16); color: #b45309; }
[data-theme="light"] .badge.error, [data-theme="light"] .badge.danger, [data-theme="light"] .badge.suspended, [data-theme="light"] .badge.failed { background: rgba(239,60,57,.12); color: #b91c1c; }
[data-theme="light"] .badge.info { background: rgba(59,130,246,.13); color: #1d4ed8; }
[data-theme="light"] .badge.violet { background: rgba(139,92,246,.13); color: #6d28d9; }

[data-theme="light"] .flash.error { color: #b91c1c; }
[data-theme="light"] .flash.warning { color: #92400e; }
[data-theme="light"] .flash.success { color: #047857; }
[data-theme="light"] .flash.info { color: #1d4ed8; }
[data-theme="light"] .field-error { color: #dc2626; }
[data-theme="light"] .kpi-delta.down { color: var(--tilly-error-2, #DC2626); }
