/* ==========================================================================
   Design system — modelled on shadcn/ui + Radix primitives.

   Colours are stored as raw HSL channels ("240 10% 3.9%") rather than finished
   colours, so any token can be used at partial opacity with
   hsl(var(--token) / 0.5). This is the same trick shadcn uses.
   ========================================================================== */

:root {
  --background: 0 0% 100%;
  --foreground: 240 10% 3.9%;

  --card: 0 0% 100%;
  --card-foreground: 240 10% 3.9%;

  --primary: 240 5.9% 10%;
  --primary-foreground: 0 0% 98%;

  --secondary: 240 4.8% 95.9%;
  --secondary-foreground: 240 5.9% 10%;

  --muted: 240 4.8% 95.9%;
  --muted-foreground: 240 3.8% 46.1%;

  --accent: 240 4.8% 95.9%;
  --accent-foreground: 240 5.9% 10%;

  --destructive: 0 72% 51%;
  --destructive-foreground: 0 0% 98%;

  --success: 142 71% 45%;
  --success-foreground: 0 0% 100%;

  --warning: 38 92% 50%;
  --warning-foreground: 48 96% 9%;

  --brand: 141 70% 36%;
  --brand-foreground: 0 0% 100%;

  --border: 240 5.9% 90%;
  --input: 240 5.9% 90%;
  --ring: 240 10% 3.9%;

  --chart-1: 141 70% 42%;
  --chart-2: 262 83% 68%;
  --chart-3: 0 72% 61%;

  --radius: 0.5rem;
}

.dark {
  --background: 240 10% 3.9%;
  --foreground: 0 0% 98%;

  --card: 240 10% 5.5%;
  --card-foreground: 0 0% 98%;

  --primary: 0 0% 98%;
  --primary-foreground: 240 5.9% 10%;

  --secondary: 240 3.7% 15.9%;
  --secondary-foreground: 0 0% 98%;

  --muted: 240 3.7% 15.9%;
  --muted-foreground: 240 5% 64.9%;

  --accent: 240 3.7% 15.9%;
  --accent-foreground: 0 0% 98%;

  --destructive: 0 62.8% 50%;
  --destructive-foreground: 0 0% 98%;

  --success: 142 71% 45%;
  --warning: 38 92% 50%;

  --border: 240 3.7% 15.9%;
  --input: 240 3.7% 18%;
  --ring: 240 4.9% 83.9%;
}

/* ---------- Base ---------- */

*, *::before, *::after { box-sizing: border-box; border-color: hsl(var(--border)); }

* { margin: 0; padding: 0; }

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

body {
  background: hsl(var(--background));
  color: hsl(var(--foreground));
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
               "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4 { font-weight: 600; letter-spacing: -0.02em; line-height: 1.25; }

a { color: inherit; text-decoration: none; }

::selection { background: hsl(var(--primary) / 0.15); }

/* A single, consistent focus ring across every interactive element. */
:where(a, button, input, select, textarea, [tabindex]):focus-visible {
  outline: 2px solid hsl(var(--ring));
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------- Button ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  white-space: nowrap;
  border-radius: calc(var(--radius) - 2px);
  border: 1px solid transparent;
  font-size: .875rem;
  font-weight: 500;
  height: 2.25rem;
  padding: 0 1rem;
  cursor: pointer;
  font-family: inherit;
  transition: background-color .15s, color .15s, border-color .15s, opacity .15s;
  user-select: none;
}

.btn:disabled { opacity: .5; pointer-events: none; }

.btn svg { width: 1rem; height: 1rem; flex-shrink: 0; }

.btn-primary { background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); }
.btn-primary:hover { background: hsl(var(--primary) / .9); }

.btn-brand { background: hsl(var(--brand)); color: hsl(var(--brand-foreground)); }
.btn-brand:hover { background: hsl(var(--brand) / .9); }

.btn-secondary { background: hsl(var(--secondary)); color: hsl(var(--secondary-foreground)); }
.btn-secondary:hover { background: hsl(var(--secondary) / .8); }

.btn-outline {
  border-color: hsl(var(--border));
  background: hsl(var(--background));
  color: hsl(var(--foreground));
}
.btn-outline:hover { background: hsl(var(--accent)); color: hsl(var(--accent-foreground)); }

.btn-ghost { background: transparent; color: hsl(var(--foreground)); }
.btn-ghost:hover { background: hsl(var(--accent)); color: hsl(var(--accent-foreground)); }

.btn-destructive { background: hsl(var(--destructive)); color: hsl(var(--destructive-foreground)); }
.btn-destructive:hover { background: hsl(var(--destructive) / .9); }

.btn-sm { height: 2rem; padding: 0 .75rem; font-size: .8125rem; border-radius: calc(var(--radius) - 3px); }
.btn-lg { height: 2.75rem; padding: 0 1.5rem; font-size: .9375rem; }
.btn-icon { width: 2.25rem; height: 2.25rem; padding: 0; }
.btn-icon.btn-sm { width: 2rem; height: 2rem; }

/* ---------- Card ---------- */

.card {
  background: hsl(var(--card));
  color: hsl(var(--card-foreground));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  box-shadow: 0 1px 2px 0 hsl(var(--foreground) / .04);
}

.card-header { padding: 1.25rem 1.25rem 0; display: flex; flex-direction: column; gap: .375rem; }
.card-title { font-size: .9375rem; font-weight: 600; }
.card-description { font-size: .8125rem; color: hsl(var(--muted-foreground)); }
.card-content { padding: 1.25rem; }
.card-header + .card-content { padding-top: 1rem; }
.card-footer { padding: 0 1.25rem 1.25rem; display: flex; gap: .5rem; align-items: center; }

/* ---------- Form controls ---------- */

.label {
  display: block;
  font-size: .875rem;
  font-weight: 500;
  line-height: 1;
  margin-bottom: .5rem;
}

.input, .textarea, .select {
  display: block;
  width: 100%;
  height: 2.25rem;
  padding: 0 .75rem;
  font-size: .875rem;
  font-family: inherit;
  color: hsl(var(--foreground));
  background: hsl(var(--background));
  border: 1px solid hsl(var(--input));
  border-radius: calc(var(--radius) - 2px);
  transition: border-color .15s, box-shadow .15s;
}

.input::placeholder, .textarea::placeholder { color: hsl(var(--muted-foreground)); }

.input:focus, .textarea:focus, .select:focus {
  outline: none;
  border-color: hsl(var(--ring));
  box-shadow: 0 0 0 3px hsl(var(--ring) / .12);
}

.textarea { height: auto; min-height: 5rem; padding: .5rem .75rem; resize: vertical; line-height: 1.5; }

.select {
  appearance: none;
  padding-right: 2rem;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%2371717a' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right .625rem center;
  background-size: 1rem;
  cursor: pointer;
}

.form-item { margin-bottom: 1.25rem; }
.form-description { font-size: .8125rem; color: hsl(var(--muted-foreground)); margin-top: .5rem; }

/* Radix-style switch, built from a visually hidden checkbox. */
.switch { display: inline-flex; align-items: center; gap: .625rem; cursor: pointer; }

.switch input {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

.switch-track {
  position: relative;
  width: 2.25rem; height: 1.25rem;
  border-radius: 999px;
  background: hsl(var(--muted-foreground) / .35);
  transition: background-color .18s;
  flex-shrink: 0;
}

.switch-thumb {
  position: absolute;
  top: 2px; left: 2px;
  width: 1rem; height: 1rem;
  border-radius: 999px;
  background: hsl(var(--background));
  box-shadow: 0 1px 3px hsl(0 0% 0% / .25);
  transition: transform .18s;
}

.switch input:checked + .switch-track { background: hsl(var(--brand)); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(1rem); }
.switch input:focus-visible + .switch-track { outline: 2px solid hsl(var(--ring)); outline-offset: 2px; }

/* ---------- Badge ---------- */

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: .125rem .5rem;
  font-size: .6875rem;
  font-weight: 600;
  line-height: 1.4;
  letter-spacing: .01em;
  border: 1px solid transparent;
  white-space: nowrap;
}

.badge-default     { background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); }
.badge-secondary   { background: hsl(var(--secondary)); color: hsl(var(--secondary-foreground)); }
.badge-outline     { border-color: hsl(var(--border)); color: hsl(var(--foreground)); }
.badge-success     { background: hsl(var(--success) / .12); color: hsl(var(--success)); border-color: hsl(var(--success) / .25); }
.badge-destructive { background: hsl(var(--destructive) / .12); color: hsl(var(--destructive)); border-color: hsl(var(--destructive) / .25); }
.badge-warning     { background: hsl(var(--warning) / .14); color: hsl(var(--warning)); border-color: hsl(var(--warning) / .3); }
.badge-brand       { background: hsl(var(--brand) / .12); color: hsl(var(--brand)); border-color: hsl(var(--brand) / .25); }

/* ---------- Table ---------- */

.table-wrap { width: 100%; overflow-x: auto; }

.table { width: 100%; border-collapse: collapse; font-size: .875rem; }

.table th {
  text-align: left;
  font-size: .75rem;
  font-weight: 500;
  color: hsl(var(--muted-foreground));
  padding: .625rem .75rem;
  border-bottom: 1px solid hsl(var(--border));
  white-space: nowrap;
}

.table td {
  padding: .75rem;
  border-bottom: 1px solid hsl(var(--border));
  vertical-align: middle;
}

.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr { transition: background-color .12s; }
.table tbody tr:hover { background: hsl(var(--muted) / .5); }

/* ---------- Misc primitives ---------- */

.separator { height: 1px; background: hsl(var(--border)); border: 0; }

.skeleton {
  background: hsl(var(--muted));
  border-radius: calc(var(--radius) - 2px);
  animation: pulse 1.8s cubic-bezier(.4, 0, .6, 1) infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .45; } }

.spinner {
  width: 1rem; height: 1rem;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 999px;
  animation: spin .6s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

.alert {
  display: flex;
  gap: .75rem;
  padding: .875rem 1rem;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  font-size: .875rem;
}
.alert-destructive {
  border-color: hsl(var(--destructive) / .35);
  background: hsl(var(--destructive) / .08);
  color: hsl(var(--destructive));
}
.alert-warning {
  border-color: hsl(var(--warning) / .35);
  background: hsl(var(--warning) / .08);
  color: hsl(var(--warning-foreground));
}
.dark .alert-warning { color: hsl(var(--warning)); }

/* Toast, positioned like sonner. */
.toaster { position: fixed; bottom: 1rem; right: 1rem; z-index: 100; display: flex; flex-direction: column; gap: .5rem; }

.toast {
  display: flex; align-items: flex-start; gap: .625rem;
  min-width: 16rem; max-width: 22rem;
  padding: .875rem 1rem;
  background: hsl(var(--card));
  color: hsl(var(--card-foreground));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  box-shadow: 0 10px 30px hsl(0 0% 0% / .12), 0 2px 8px hsl(0 0% 0% / .08);
  font-size: .875rem;
  animation: toast-in .2s cubic-bezier(.16, 1, .3, 1);
}
.toast.leaving { animation: toast-out .16s ease-in forwards; }
.toast-icon { flex-shrink: 0; width: 1rem; height: 1rem; margin-top: .125rem; }
.toast-success .toast-icon { color: hsl(var(--success)); }
.toast-error .toast-icon { color: hsl(var(--destructive)); }

@keyframes toast-in  { from { opacity: 0; transform: translateY(.75rem) scale(.97); } }
@keyframes toast-out { to   { opacity: 0; transform: translateY(.5rem) scale(.97); } }

/* ---------- Utilities ---------- */

.muted { color: hsl(var(--muted-foreground)); }
.mono { font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace; font-size: .8125rem; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}
.hidden { display: none !important; }
.empty-state { text-align: center; padding: 2.5rem 1rem; color: hsl(var(--muted-foreground)); font-size: .875rem; }
