/*
 * erp-core design system: hand-written CSS on custom properties, light and dark by prefers-color-scheme.
 * Served as /_content/ErpCore.Cloud/css/erp-core.css. A connector overrides only the accent through /_ui/theme.css.
 * Vendored scripts: js/htmx.min.js is htmx 2.0.4 (https://unpkg.com/htmx.org@2.0.4/dist/htmx.min.js). Bump it deliberately.
 * Contrast: text colors are chosen for WCAG AA (4.5:1) on --bg and --surface in both modes.
 */

:root {
  color-scheme: light dark;

  /* Palette (light) */
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --fg: #1a1d21;
  --muted: #5b6470;
  --line: #e3e6ea;
  --line-strong: #c9cfd7;
  --accent: #2563eb;
  --on-accent: #ffffff;
  --accent-text: var(--accent);
  --ok: #166534;
  --warn: #92400e;
  --bad: #b91c1c;
  --shadow-color: 20 30 50;

  /* Type scale */
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --text-xs: 0.75rem;
  --text-sm: 0.8125rem;
  --text-md: 0.875rem;
  --text-lg: 1rem;
  --text-xl: 1.25rem;
  --text-2xl: 1.5rem;

  /* Spacing, radius, elevation */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --radius-sm: 6px;
  --radius: 10px;
  --shadow-sm: 0 1px 2px rgb(var(--shadow-color) / 0.06);
  --shadow: 0 1px 3px rgb(var(--shadow-color) / 0.08), 0 4px 12px rgb(var(--shadow-color) / 0.06);
  --shadow-lg: 0 12px 40px rgb(var(--shadow-color) / 0.2);
  --sidebar-width: 240px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1e222a;
    --fg: #e8eaed;
    --muted: #9aa3b0;
    --line: #2a2f38;
    --line-strong: #3a414d;
    /* Links and focus rings on a dark surface need a lighter tint of the accent. */
    --accent-text: color-mix(in srgb, var(--accent) 45%, white);
    --ok: #4ade80;
    --warn: #fbbf24;
    --bad: #f87171;
    --shadow-color: 0 0 0;
  }
}

/* Derived from --accent, so an override only needs to set --accent (and --on-accent). */
:root {
  --accent-hover: color-mix(in srgb, var(--accent) 85%, black);
  --accent-soft: color-mix(in srgb, var(--accent) 12%, var(--surface));
}

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

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font: var(--text-md) / 1.5 var(--font);
  color: var(--fg);
  background: var(--bg);
}

h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: var(--text-xl); font-weight: 650; }
h2 { font-size: var(--text-lg); font-weight: 600; }
h3 { font-size: var(--text-md); font-weight: 600; }
p { margin: 0 0 var(--space-3); }

a { color: var(--accent-text); text-decoration-thickness: 1px; text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

code, pre { font-family: var(--mono); }
code { font-size: 0.92em; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

:focus-visible {
  outline: 2px solid var(--accent-text);
  outline-offset: 2px;
  border-radius: 4px;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- Text utilities (state colors) ---------- */
.ok { color: var(--ok); }
.warn { color: var(--warn); }
.bad { color: var(--bad); }
.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }

/* ---------- App shell ---------- */
.skip-link {
  position: absolute; left: var(--space-3); top: -100px; z-index: 100;
  background: var(--surface); color: var(--fg); padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm); box-shadow: var(--shadow);
}
.skip-link:focus { top: var(--space-3); }

.app { display: grid; grid-template-columns: var(--sidebar-width) minmax(0, 1fr); min-height: 100vh; }

.sidebar {
  position: sticky; top: 0; align-self: start; height: 100vh;
  display: flex; flex-direction: column; gap: var(--space-4);
  padding: var(--space-4) var(--space-3);
  background: var(--surface); border-right: 1px solid var(--line);
}

.brand {
  display: flex; align-items: center; gap: var(--space-2);
  padding: var(--space-1) var(--space-2);
  color: var(--fg); font-weight: 650; font-size: var(--text-lg); text-decoration: none;
}
.brand:hover { text-decoration: none; }
.brand-logo { height: 28px; width: auto; max-width: 140px; object-fit: contain; }

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: block; padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm); color: var(--muted); text-decoration: none; font-weight: 500;
}
.nav a:hover { background: var(--surface-2); color: var(--fg); }
.nav a[aria-current="page"], .nav a.is-active { background: var(--accent-soft); color: var(--accent-text); }

.content { min-width: 0; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: var(--space-3);
  min-height: 56px; padding: var(--space-2) var(--space-5);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { margin-right: auto; overflow-wrap: anywhere; }
.topbar form { margin: 0; }

.page { width: 100%; max-width: 1240px; margin: 0 auto; padding: var(--space-5); display: flex; flex-direction: column; gap: var(--space-5); }

.page-actions { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; }
.page-actions form { margin: 0; }

/* ---------- Authentication layout ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: var(--space-4); }
.auth-card { width: min(100%, 380px); display: flex; flex-direction: column; gap: var(--space-4); }
.auth-brand { display: flex; align-items: center; justify-content: center; gap: var(--space-2); font-weight: 650; font-size: var(--text-lg); }

/* ---------- Cards ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow-sm);
  padding: var(--space-4) var(--space-5);
}
.card > :last-child { margin-bottom: 0; }
.card-header { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); margin-bottom: var(--space-3); }
.card-header h2, .card-header h3 { margin-right: auto; }
.card-flush { padding: 0; overflow: hidden; }
.card-flush > .card-header { padding: var(--space-4) var(--space-5) 0; }
.card-flush > p { padding: 0 var(--space-5); }
.card-flush > .table-wrap { margin-top: var(--space-2); }
.card-flush > .table-wrap:last-child table tr:last-child td { border-bottom: 0; }
.stack { display: flex; flex-direction: column; gap: var(--space-3); }
.row { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; }

/* ---------- Empty state ---------- */
.empty {
  display: flex; flex-direction: column; align-items: center; gap: var(--space-2);
  padding: var(--space-6) var(--space-4); text-align: center;
}
.empty-title { font-weight: 600; font-size: var(--text-lg); }
.empty-hint { color: var(--muted); max-width: 46ch; margin: 0; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: var(--space-2) var(--space-4); border-bottom: 1px solid var(--line); vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: var(--text-sm); white-space: nowrap; background: var(--surface-2); }
tbody tr:hover td { background: color-mix(in srgb, var(--fg) 3%, transparent); }
table.kv th { width: 1%; background: transparent; }
.table-filter { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--line); }
.table-filter form { display: contents; }
.sub { display: block; color: var(--muted); font-size: var(--text-sm); }

pre {
  margin: 0; padding: var(--space-3) var(--space-4); overflow: auto;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius-sm);
  font-size: var(--text-xs); line-height: 1.6; white-space: pre-wrap; word-break: break-all;
}
pre.bad { color: var(--bad); }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 1px 9px; border-radius: 999px;
  font-size: var(--text-sm); font-weight: 550; line-height: 1.5; white-space: nowrap;
  --tone: var(--muted);
  color: var(--tone);
  background: color-mix(in srgb, var(--tone) 12%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--tone) 28%, var(--surface));
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge-ok { --tone: var(--ok); }
.badge-warn { --tone: var(--warn); }
.badge-bad { --tone: var(--bad); }
.badge-muted { --tone: var(--muted); }

/* ---------- Forms ---------- */
label { display: block; margin: 0 0 var(--space-1); color: var(--muted); font-weight: 500; font-size: var(--text-sm); }
.field { display: flex; flex-direction: column; gap: var(--space-1); margin-bottom: var(--space-3); }
.field label { margin: 0; }
.field-hint { color: var(--muted); font-size: var(--text-sm); margin: 0; }

input, select, textarea, button { font: inherit; }
input:not([type="hidden"]), select, textarea {
  width: 100%; padding: 7px 10px; color: var(--fg);
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
}
select { width: auto; max-width: 100%; }
input:hover, select:hover, textarea:hover { border-color: var(--muted); }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent-text); outline-offset: 0; border-color: var(--accent-text); }
.input-wide { max-width: 520px; }
textarea.code { font: var(--text-xs) / 1.6 var(--mono); }

.field-validation-error, .validation-summary-errors, .form-error { color: var(--bad); font-size: var(--text-sm); }
.validation-summary-errors ul { margin: 0; padding-left: 1.2em; }
.input-validation-error { border-color: var(--bad) !important; }
.form-error ul { margin: 0; padding-left: 1.2em; }

.btn, button {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  padding: 7px 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--accent); background: var(--accent); color: var(--on-accent);
  font-weight: 550; line-height: 1.4; cursor: pointer; text-decoration: none;
  transition: background-color 0.12s, border-color 0.12s;
}
.btn:hover, button:hover { background: var(--accent-hover); border-color: var(--accent-hover); text-decoration: none; }
.btn-secondary, button.secondary { background: var(--surface); color: var(--fg); border-color: var(--line-strong); }
.btn-secondary:hover, button.secondary:hover { background: var(--surface-2); border-color: var(--muted); }
.btn-danger, button.danger { background: var(--surface); color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, var(--surface)); }
.btn-danger:hover, button.danger:hover { background: color-mix(in srgb, var(--bad) 10%, var(--surface)); border-color: var(--bad); }
.btn-sm, button.small { padding: 3px 10px; font-size: var(--text-sm); }
button:disabled { opacity: 0.55; cursor: not-allowed; }
form.inline { display: inline; margin: 0; }

/* ---------- Alerts (inline notices / toast content) ---------- */
.notice {
  --tone: var(--muted);
  display: flex; gap: var(--space-3); align-items: flex-start;
  padding: var(--space-3) var(--space-4); border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--tone) 10%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--tone) 35%, var(--surface));
  color: var(--fg); border-left: 4px solid var(--tone);
}
.notice-ok { --tone: var(--ok); }
.notice-warn { --tone: var(--warn); }
.notice-bad { --tone: var(--bad); }
.notice-text { flex: 1; overflow-wrap: anywhere; }
.notice-close { padding: 0 6px; background: transparent; color: var(--muted); border-color: transparent; font-size: var(--text-lg); line-height: 1.2; }
.notice-close:hover { background: var(--surface-2); color: var(--fg); border-color: transparent; }

/* ---------- Toasts ---------- */
.toasts {
  position: fixed; right: var(--space-4); bottom: var(--space-4); z-index: 60;
  display: flex; flex-direction: column; gap: var(--space-2);
  width: min(420px, calc(100vw - 2 * var(--space-4)));
  pointer-events: none;
}
.toasts > * { pointer-events: auto; box-shadow: var(--shadow); background: var(--surface); animation: toast-in 0.18s ease-out; }
.toasts > .is-leaving { opacity: 0; transition: opacity 0.2s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- Confirm dialog ---------- */
dialog.confirm {
  width: min(92vw, 420px); padding: var(--space-5);
  color: var(--fg); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-lg);
}
dialog.confirm::backdrop { background: rgb(0 0 0 / 0.45); }
dialog.confirm p { font-size: var(--text-lg); margin-bottom: var(--space-4); }
dialog.confirm .dialog-actions { display: flex; justify-content: flex-end; gap: var(--space-2); }

/* ---------- Charts (inline SVG) ---------- */
.chart { display: block; overflow: visible; }
.chart-line { fill: none; stroke: var(--accent-text); stroke-width: 1.75; stroke-linejoin: round; stroke-linecap: round; }
.chart-dot { fill: var(--accent-text); }
.chart-bar { fill: var(--accent-text); opacity: 0.85; }

/* ---------- htmx ---------- */
.htmx-request { opacity: 0.6; transition: opacity 0.15s; }
.htmx-indicator { opacity: 0; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }

/* ---------- Tablet and narrow screens: sidebar becomes a top bar ---------- */
@media (max-width: 900px) {
  .app { display: block; }
  .sidebar {
    position: static; height: auto; flex-direction: row; align-items: center; flex-wrap: wrap;
    gap: var(--space-2) var(--space-4); padding: var(--space-2) var(--space-4);
    border-right: 0; border-bottom: 1px solid var(--line);
  }
  .nav { flex-direction: row; flex-wrap: wrap; }
  .topbar { position: static; padding: var(--space-2) var(--space-4); backdrop-filter: none; }
  .page { padding: var(--space-4); gap: var(--space-4); }
  .card { padding: var(--space-4); }
  .card-flush { padding: 0; }
  .card-flush > .card-header { padding: var(--space-4) var(--space-4) 0; }
  .card-flush > p { padding: 0 var(--space-4); }
  th, td { padding: var(--space-2) var(--space-3); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Usage metering (tenant detail card, /admin/usage) */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: var(--space-3); margin-bottom: var(--space-3); }
.stat { border: 1px solid var(--line); border-radius: var(--radius-sm); padding: var(--space-3); background: var(--surface-2); }
.stat-value { font-size: var(--text-lg); font-weight: 600; font-variant-numeric: tabular-nums; }
.stat-label { color: var(--muted); font-size: var(--text-sm); }
.usage-trend, .usage-limit { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); margin-bottom: var(--space-3); }
.usage-progress { width: 100%; max-width: 320px; height: 8px; accent-color: var(--accent, currentColor); }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Step indicator ---------- */
.steps { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); margin: 0 0 var(--space-4); padding: 0; list-style: none; counter-reset: none; }
.step { display: inline-flex; align-items: center; gap: var(--space-2); color: var(--muted); font-size: var(--text-sm); }
.step-number {
  display: inline-flex; align-items: center; justify-content: center; width: 24px; height: 24px; border-radius: 50%;
  border: 1px solid var(--line-strong); background: var(--surface); font-weight: 600; font-variant-numeric: tabular-nums;
}
.step.is-done { color: var(--fg); }
.step.is-done .step-number { background: var(--ok); border-color: var(--ok); color: var(--on-accent); }
.step.is-current { color: var(--fg); font-weight: 600; }
.step.is-current .step-number { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
