/* ==========================================================================
   TrackRefs Design System — "Warm Precision"
   Pures CSS, keine Toolchain. Tokens als Custom Properties, Dark Mode via
   [data-theme="dark"]. Tenant-Branding überschreibt ausschließlich --brand-*.
   ========================================================================== */

/* ---------- Tokens ---------- */
:root {
  /* Neutrale, warme Grauskala */
  --n-0: #ffffff;
  --n-25: #fcfbfa;
  --n-50: #f7f5f2;
  --n-100: #efece7;
  --n-200: #e2ddd5;
  --n-300: #cbc4b8;
  --n-400: #a89f91;
  --n-500: #857c6e;
  --n-600: #635b4f;
  --n-700: #453f37;
  --n-800: #2d2822;
  --n-900: #1c1815;

  /* System-Akzent (Backend). Tenant-Branding setzt --brand-* im Portal um. */
  --brand-500: #e8613c;
  --brand-600: #d14e2a;
  --brand-700: #b03f20;
  --brand-50: #fdf0eb;
  --brand-100: #fbdfd4;
  --brand-contrast: #ffffff;

  --ok-500: #3d9970;
  --ok-50: #e9f6f0;
  --warn-500: #d99a26;
  --warn-50: #fbf3e2;
  --danger-500: #cc4747;
  --danger-50: #fbecec;
  --info-500: #4776cc;
  --info-50: #ecf1fb;

  --bg: var(--n-50);
  --bg-elevated: var(--n-0);
  --bg-sunken: var(--n-100);
  --text: var(--n-800);
  --text-muted: var(--n-500);
  --text-faint: var(--n-400);
  --border: var(--n-200);
  --border-strong: var(--n-300);

  --radius-lg: 16px;
  --radius: 12px;
  --radius-sm: 8px;
  --radius-xs: 6px;

  --shadow-sm: 0 1px 2px rgb(28 24 21 / 0.05);
  --shadow: 0 1px 3px rgb(28 24 21 / 0.06), 0 4px 12px rgb(28 24 21 / 0.05);
  --shadow-lg: 0 4px 8px rgb(28 24 21 / 0.06), 0 12px 32px rgb(28 24 21 / 0.12);

  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --font-mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;

  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px;
  --space-5: 20px; --space-6: 24px; --space-8: 32px; --space-10: 40px; --space-12: 48px;

  --transition: 150ms cubic-bezier(0.3, 0, 0.2, 1);
  color-scheme: light;
}

[data-theme="dark"] {
  --n-0: #211d19;
  --n-25: #262220;
  --n-50: #1c1815;
  --n-100: #2d2822;
  --n-200: #3a342c;
  --n-300: #4c453b;
  --n-400: #6e6557;
  --n-500: #948a7a;
  --n-600: #b3aa9b;
  --n-700: #d4cdc2;
  --n-800: #ece8e1;
  --n-900: #f7f5f2;

  --bg: #171412;
  --bg-elevated: #211d19;
  --bg-sunken: #12100e;
  --border: #322d26;
  --border-strong: #453f37;

  --shadow-sm: 0 1px 2px rgb(0 0 0 / 0.3);
  --shadow: 0 1px 3px rgb(0 0 0 / 0.35), 0 4px 12px rgb(0 0 0 / 0.3);
  --shadow-lg: 0 4px 8px rgb(0 0 0 / 0.4), 0 12px 32px rgb(0 0 0 / 0.5);

  --brand-50: #3a231b;
  --brand-100: #4d2b1f;
  --ok-50: #1d3329;
  --warn-50: #3b3019;
  --danger-50: #3d2020;
  --info-50: #1f2a40;
  color-scheme: dark;
}

/* ---------- Reset & Basis ---------- */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font);
  font-size: 14.5px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
img, svg, canvas { display: block; max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand-600); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { font-weight: 650; letter-spacing: -0.015em; line-height: 1.25; }
h1 { font-size: 24px; }
h2 { font-size: 19px; }
h3 { font-size: 16px; }
small, .text-sm { font-size: 13px; }
.text-xs { font-size: 12px; }
.muted { color: var(--text-muted); }
.faint { color: var(--text-faint); }
.mono { font-family: var(--font-mono); font-size: 0.92em; }
strong { font-weight: 600; }
::selection { background: var(--brand-100); }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  padding: 9px 16px; border-radius: var(--radius-sm);
  border: 1px solid transparent; background: transparent;
  font-weight: 560; font-size: 14px; line-height: 1.2; cursor: pointer;
  transition: background var(--transition), border-color var(--transition),
              color var(--transition), transform var(--transition), box-shadow var(--transition);
  white-space: nowrap; user-select: none; text-decoration: none !important;
}
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--brand-500); outline-offset: 2px; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--brand-600); color: var(--brand-contrast); box-shadow: var(--shadow-sm); }
.btn-primary:hover { background: var(--brand-700); }
.btn-secondary { background: var(--bg-elevated); border-color: var(--border-strong); color: var(--text); box-shadow: var(--shadow-sm); }
.btn-secondary:hover { border-color: var(--n-400); background: var(--n-25); }
.btn-ghost { color: var(--text-muted); }
.btn-ghost:hover { background: var(--bg-sunken); color: var(--text); }
.btn-danger { background: var(--danger-500); color: #fff; }
.btn-danger:hover { filter: brightness(0.92); }
.btn-sm { padding: 5px 11px; font-size: 13px; border-radius: var(--radius-xs); }
.btn-lg { padding: 12px 22px; font-size: 15px; border-radius: var(--radius); }
.btn .spinner { width: 14px; height: 14px; }

/* ---------- Cards ---------- */
.card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}
.card-pad { padding: var(--space-6); }
.card-header {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-4);
  padding: var(--space-5) var(--space-6);
  border-bottom: 1px solid var(--border);
}
.card-header h2, .card-header h3 { margin: 0; }
.card-body { padding: var(--space-6); }
.card-footer {
  padding: var(--space-4) var(--space-6);
  border-top: 1px solid var(--border);
  background: var(--n-25);
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
  display: flex; justify-content: flex-end; gap: var(--space-3);
}

/* ---------- Stat-Karten ---------- */
.stat {
  display: flex; flex-direction: column; gap: var(--space-1);
  padding: var(--space-5) var(--space-6);
}
.stat-label { font-size: 12.5px; font-weight: 560; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-muted); }
.stat-value { font-size: 26px; font-weight: 680; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.stat-delta { font-size: 12.5px; font-weight: 560; }
.stat-delta.up { color: var(--ok-500); }
.stat-delta.down { color: var(--danger-500); }

/* ---------- Formulare ---------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.field + .field { margin-top: var(--space-4); }
/* Nebeneinander im Grid: kein Stapel-Abstand, sonst verschobene Höhen */
.grid > .field { margin-top: 0; }
.label { font-size: 13px; font-weight: 580; color: var(--text); }
.hint { font-size: 12.5px; color: var(--text-muted); }
.error { font-size: 12.5px; color: var(--danger-500); }
.input, .select, .textarea {
  width: 100%; padding: 9px 12px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  transition: border-color var(--transition), box-shadow var(--transition);
}
.input:focus, .select:focus, .textarea:focus {
  outline: none; border-color: var(--brand-500);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand-500) 18%, transparent);
}
.input.is-invalid, .select.is-invalid, .textarea.is-invalid { border-color: var(--danger-500); }
.input::placeholder, .textarea::placeholder { color: var(--text-faint); }
.textarea { min-height: 110px; resize: vertical; }
.select { appearance: none; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23857c6e' stroke-width='2.5'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; padding-right: 34px; }
.checkbox { display: inline-flex; gap: var(--space-3); align-items: flex-start; cursor: pointer; }
.checkbox input { width: 16px; height: 16px; margin-top: 2px; accent-color: var(--brand-600); }

/* Switch */
.switch { position: relative; display: inline-block; width: 38px; height: 22px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .track {
  position: absolute; inset: 0; border-radius: 999px;
  background: var(--n-300); transition: background var(--transition); cursor: pointer;
}
.switch .track::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px;
  border-radius: 50%; background: #fff; box-shadow: var(--shadow-sm);
  transition: transform var(--transition);
}
.switch input:checked + .track { background: var(--brand-600); }
.switch input:checked + .track::after { transform: translateX(16px); }
.switch input:focus-visible + .track { outline: 2px solid var(--brand-500); outline-offset: 2px; }

/* ---------- Badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 580; line-height: 1.5;
  background: var(--bg-sunken); color: var(--text-muted);
}
.badge-ok { background: var(--ok-50); color: var(--ok-500); }
.badge-warn { background: var(--warn-50); color: var(--warn-500); }
.badge-danger { background: var(--danger-50); color: var(--danger-500); }
.badge-info { background: var(--info-50); color: var(--info-500); }
.badge-brand { background: var(--brand-50); color: var(--brand-600); }
.badge .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

/* ---------- Tabellen ---------- */
.table-wrap { overflow-x: auto; border-radius: var(--radius-lg); }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
  text-align: left; padding: 11px var(--space-4);
  font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em;
  color: var(--text-muted); border-bottom: 1px solid var(--border);
  white-space: nowrap; background: var(--n-25);
}
.table td { padding: 13px var(--space-4); border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr { transition: background var(--transition); }
.table-hover tbody tr:hover { background: var(--n-25); cursor: pointer; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Navigation / App-Shell ---------- */
.shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 232px; flex-shrink: 0;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
  padding: var(--space-5) var(--space-3);
}
.sidebar-logo { display: flex; align-items: center; gap: 10px; padding: 0 var(--space-3) var(--space-5); }
.sidebar-logo img { height: 26px; }
.sidebar-logo .name { font-weight: 700; font-size: 15.5px; letter-spacing: -0.01em; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-section { margin-top: var(--space-5); padding: 0 var(--space-3); font-size: 11px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.08em; color: var(--text-faint); }
.nav-link {
  display: flex; align-items: center; gap: 10px;
  padding: 8px var(--space-3); border-radius: var(--radius-sm);
  color: var(--text-muted); font-weight: 530; font-size: 14px;
  transition: background var(--transition), color var(--transition);
  text-decoration: none !important;
}
.nav-link:hover { background: var(--bg-sunken); color: var(--text); }
.nav-link.active { background: var(--brand-50); color: var(--brand-600); font-weight: 600; }
.nav-link svg { width: 17px; height: 17px; flex-shrink: 0; }
.sidebar-footer { margin-top: auto; padding: var(--space-3); border-top: 1px solid var(--border); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-4);
  padding: var(--space-4) var(--space-8);
  background: color-mix(in srgb, var(--bg) 80%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 30;
}
.page { padding: var(--space-8); max-width: 1200px; width: 100%; margin: 0 auto; }
.page-header { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--space-4); margin-bottom: var(--space-6); flex-wrap: wrap; }
.page-header .sub { color: var(--text-muted); margin-top: 4px; }

/* ---------- Auth-Layout ---------- */
.auth-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  padding: var(--space-6);
  background:
    radial-gradient(1200px 500px at 20% -10%, var(--brand-50), transparent 60%),
    var(--bg);
}
.auth-card { width: 100%; max-width: 400px; }
.auth-logo { display: flex; justify-content: center; margin-bottom: var(--space-6); }
.auth-logo img { height: 34px; }

/* ---------- Dropdown ---------- */
.dropdown { position: relative; }
.dropdown-menu {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 190px; z-index: 50;
  background: var(--bg-elevated); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-lg);
  padding: 6px; display: flex; flex-direction: column;
}
.dropdown-item {
  display: flex; align-items: center; gap: 9px; width: 100%;
  padding: 8px 10px; border: 0; background: none; text-align: left;
  border-radius: var(--radius-xs); font-size: 13.5px; color: var(--text);
  cursor: pointer; text-decoration: none !important;
}
.dropdown-item:hover { background: var(--bg-sunken); }
.dropdown-sep { height: 1px; background: var(--border); margin: 5px 4px; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 90;
  background: rgb(28 24 21 / 0.45);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 8vh var(--space-4) var(--space-4);
  animation: fade-in 140ms ease;
}
.modal {
  width: 100%; max-width: 520px;
  background: var(--bg-elevated); border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg); border: 1px solid var(--border);
  animation: pop-in 160ms cubic-bezier(0.2, 0.9, 0.3, 1.2);
  max-height: 84vh; display: flex; flex-direction: column;
}
.modal .card-body { overflow-y: auto; }
@keyframes fade-in { from { opacity: 0; } }
@keyframes pop-in { from { opacity: 0; transform: translateY(10px) scale(0.98); } }

/* ---------- Toasts ---------- */
.toast-host { position: fixed; bottom: var(--space-6); right: var(--space-6); z-index: 100; display: flex; flex-direction: column; gap: var(--space-2); }
.toast {
  display: flex; align-items: center; gap: 10px;
  background: var(--n-800); color: var(--n-50);
  padding: 11px 16px; border-radius: var(--radius);
  box-shadow: var(--shadow-lg); font-size: 13.5px; font-weight: 530;
  animation: toast-in 220ms cubic-bezier(0.2, 0.9, 0.3, 1.15);
}
[data-theme="dark"] .toast { background: var(--n-100); color: var(--n-800); }
.toast.ok::before, .toast.error::before { content: ""; width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.toast.ok::before { background: var(--ok-500); }
.toast.error::before { background: var(--danger-500); }
@keyframes toast-in { from { opacity: 0; transform: translateY(12px); } }

/* ---------- Empty State ---------- */
.empty {
  display: flex; flex-direction: column; align-items: center; text-align: center;
  gap: var(--space-2); padding: var(--space-12) var(--space-6); color: var(--text-muted);
}
.empty .icon { font-size: 30px; margin-bottom: var(--space-2); }
.empty h3 { color: var(--text); }
.empty p { max-width: 380px; }
.empty .btn { margin-top: var(--space-3); }

/* ---------- Tabs ---------- */
.tabs { display: flex; gap: var(--space-1); border-bottom: 1px solid var(--border); margin-bottom: var(--space-6); }
.tab {
  padding: 9px 14px; margin-bottom: -1px;
  font-weight: 550; font-size: 14px; color: var(--text-muted);
  border: 0; background: none; cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color var(--transition), border-color var(--transition);
  text-decoration: none !important;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--brand-600); border-bottom-color: var(--brand-600); }

/* ---------- Progress / Level ---------- */
.progress { height: 8px; border-radius: 999px; background: var(--bg-sunken); overflow: hidden; }
.progress > div { height: 100%; border-radius: 999px; background: var(--brand-600); transition: width 600ms cubic-bezier(0.3, 0, 0.2, 1); }

/* ---------- Skeleton ---------- */
.skeleton {
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, var(--bg-sunken) 25%, var(--n-200) 50%, var(--bg-sunken) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- Copy-Feld ---------- */
.copy-field {
  display: flex; align-items: stretch;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  overflow: hidden; background: var(--bg-elevated);
}
.copy-field input { flex: 1; border: 0; padding: 9px 12px; background: transparent; font-family: var(--font-mono); font-size: 13px; min-width: 0; }
.copy-field input:focus { outline: none; }
.copy-field button {
  border: 0; border-left: 1px solid var(--border);
  padding: 0 14px; background: var(--n-25); cursor: pointer;
  font-weight: 570; font-size: 13px; color: var(--text-muted);
  transition: background var(--transition), color var(--transition);
}
.copy-field button:hover { background: var(--bg-sunken); color: var(--text); }
.copy-field button.copied { color: var(--ok-500); }

/* ---------- Utilities (bewusst klein gehalten) ---------- */
.flex { display: flex; } .items-center { align-items: center; } .justify-between { justify-content: space-between; }
.gap-2 { gap: var(--space-2); } .gap-3 { gap: var(--space-3); } .gap-4 { gap: var(--space-4); } .gap-6 { gap: var(--space-6); }
.grid { display: grid; gap: var(--space-5); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.mt-2 { margin-top: var(--space-2); } .mt-4 { margin-top: var(--space-4); } .mt-6 { margin-top: var(--space-6); } .mt-8 { margin-top: var(--space-8); }
.mb-4 { margin-bottom: var(--space-4); } .mb-6 { margin-bottom: var(--space-6); }
.w-full { width: 100%; }
.text-right { text-align: right; }
.hidden { display: none; }

.mobile-only { display: none; }
[x-cloak] { display: none !important; }

@media (max-width: 900px) {
  .mobile-only { display: inline-flex; }
  .grid-3, .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .sidebar { position: fixed; z-index: 60; transform: translateX(-100%); transition: transform 200ms ease; }
  .sidebar.open { transform: none; box-shadow: var(--shadow-lg); }
  .page { padding: var(--space-5); }
  .topbar { padding: var(--space-3) var(--space-5); }
}
@media (max-width: 620px) {
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}

/* ---------- Spinner ---------- */
.spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid color-mix(in srgb, currentColor 25%, transparent);
  border-top-color: currentColor;
  animation: spin 700ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Reduzierte Bewegung respektieren */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
