/**
 * Aether Finance — design tokens & component styles
 *
 * Accessibility decisions:
 * - Semantic tokens ensure light/dark both meet WCAG AA (≥ 4.5:1 body, ≥ 3:1 UI).
 * - :focus-visible rings use 3px brand glow (not outline:none alone).
 * - Touch targets: min 44×44px on interactive controls (Fitts’s Law + WCAG 2.5.5).
 * - prefers-reduced-motion disables non-essential transitions.
 */

:root {
  --surface: #f6f7fb;
  --surface-raised: #ffffff;
  --surface-muted: #eef0f6;
  --border: #e2e6ef;
  --text-primary: #0f172a;
  --text-muted: #64748b;
  --focus-ring: rgba(99, 102, 241, 0.45);
  --shadow-card: 0 1px 2px rgba(15, 23, 42, 0.04), 0 8px 24px rgba(15, 23, 42, 0.04);
  --success: #059669;
  --danger: #e11d48;
  --warn: #d97706;
  --sidebar-width: 16rem;
  --sidebar-collapsed: 4.5rem;
}

[data-theme="dark"] {
  --surface: #0b0f17;
  --surface-raised: #121826;
  --surface-muted: #1a2233;
  --border: #243044;
  --text-primary: #e8edf7;
  --text-muted: #94a3b8;
  --focus-ring: rgba(129, 140, 248, 0.55);
  --shadow-card: 0 1px 2px rgba(0, 0, 0, 0.35), 0 12px 32px rgba(0, 0, 0, 0.28);
  --success: #34d399;
  --danger: #fb7185;
  --warn: #fbbf24;
}

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

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  line-height: 1.5;
  transition: background-color 0.2s ease, color 0.2s ease;
}

::selection {
  background: rgba(99, 102, 241, 0.25);
}

/* Skip link: off-screen until focused */
.skip-link {
  position: absolute;
  left: 1rem;
  top: -100px;
  z-index: 100;
  padding: 0.75rem 1rem;
  border-radius: 0.75rem;
  background: var(--surface-raised);
  color: var(--text-primary);
  font-weight: 600;
  box-shadow: var(--shadow-card);
  transition: top 0.15s ease;
}

.skip-link:focus {
  top: 1rem;
  outline: none;
  box-shadow: 0 0 0 3px var(--focus-ring);
}

/* Visible focus for keyboard nav only — pointer users keep clean UI */
:focus {
  outline: none;
}

:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--focus-ring);
  border-radius: 0.5rem;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-width);
}

.sidebar.is-collapsed {
  width: var(--sidebar-collapsed);
}

.sidebar.is-collapsed .sidebar-label {
  display: none;
}

.sidebar.is-collapsed .nav-item {
  justify-content: center;
  padding-inline: 0.75rem;
}

/* Mobile: off-canvas by default */
@media (max-width: 1023px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.is-open {
    transform: translateX(0);
  }

  #sidebar-backdrop.is-visible {
    opacity: 1;
    pointer-events: auto;
  }
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-height: 44px;
  padding: 0.625rem 0.875rem;
  border-radius: 0.75rem;
  color: var(--text-muted);
  font-size: 0.9375rem;
  font-weight: 500;
  text-decoration: none;
  transition: background-color 0.15s ease, color 0.15s ease, transform 0.15s ease;
  border: none;
  background: transparent;
  cursor: pointer;
  width: 100%;
  text-align: left;
}

.nav-item:hover {
  background: var(--surface-muted);
  color: var(--text-primary);
}

.nav-item:active {
  transform: scale(0.98);
}

.nav-item-active {
  background: color-mix(in srgb, #6366f1 12%, transparent);
  color: #4f46e5;
}

[data-theme="dark"] .nav-item-active {
  background: color-mix(in srgb, #818cf8 18%, transparent);
  color: #c7d2fe;
}

.nav-icon {
  width: 1.25rem;
  height: 1.25rem;
  flex-shrink: 0;
}

/* ---------- Cards & KPIs ---------- */
.card {
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 1.25rem;
  box-shadow: var(--shadow-card);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.kpi-card {
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 1.25rem;
  box-shadow: var(--shadow-card);
  transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
}

.kpi-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.08);
  border-color: color-mix(in srgb, #6366f1 35%, var(--border));
}

[data-theme="dark"] .kpi-card:hover {
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.35);
}

.kpi-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border-radius: 0.625rem;
}

.badge-up,
.badge-down {
  display: inline-flex;
  align-items: center;
  font-weight: 600;
  font-size: 0.8125rem;
  padding: 0.125rem 0.5rem;
  border-radius: 999px;
}

.badge-up {
  color: var(--success);
  background: color-mix(in srgb, var(--success) 12%, transparent);
}

.badge-down {
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 12%, transparent);
}

.legend-dot {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-muted);
}

.legend-dot::before {
  content: "";
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 999px;
  background: currentColor;
}

.legend-dot.bg-brand-500 {
  color: #6366f1;
}

.legend-dot.bg-rose-400 {
  color: #fb7185;
}

/* ---------- Form controls ---------- */
.input {
  appearance: none;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  color: var(--text-primary);
  font: inherit;
  padding: 0.5rem 0.75rem;
  min-height: 44px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

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

.input:hover {
  border-color: color-mix(in srgb, #6366f1 40%, var(--border));
}

.input:focus-visible {
  border-color: #6366f1;
  box-shadow: 0 0 0 3px var(--focus-ring);
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  border-radius: 0.75rem;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.btn-icon:hover {
  background: var(--surface-muted);
  color: var(--text-primary);
}

.btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0.5rem 0.875rem;
  border-radius: 0.75rem;
  border: 1px solid var(--border);
  background: var(--surface-raised);
  color: var(--text-primary);
  font: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.btn-secondary:hover:not(:disabled) {
  background: var(--surface-muted);
  border-color: color-mix(in srgb, #6366f1 35%, var(--border));
}

.btn-secondary:active:not(:disabled) {
  transform: scale(0.98);
}

.btn-secondary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* ---------- Budget bars ---------- */
.budget-row {
  display: grid;
  gap: 0.5rem;
}

.budget-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
}

.budget-track {
  position: relative;
  height: 0.625rem;
  border-radius: 999px;
  background: var(--surface-muted);
  overflow: hidden;
}

.budget-fill {
  height: 100%;
  border-radius: 999px;
  width: 0;
  transition: width 0.7s cubic-bezier(0.22, 1, 0.36, 1);
}

.budget-fill.is-ok {
  background: linear-gradient(90deg, #34d399, #059669);
}

.budget-fill.is-warn {
  background: linear-gradient(90deg, #fbbf24, #d97706);
}

.budget-fill.is-over {
  background: linear-gradient(90deg, #fb7185, #e11d48);
}

/* ---------- Table ---------- */
.sort-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 44px;
  padding: 0.25rem 0.35rem;
  margin: -0.25rem -0.35rem;
  border: none;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: inherit;
  font-weight: 600;
  letter-spacing: inherit;
  text-transform: inherit;
  cursor: pointer;
  border-radius: 0.5rem;
}

.sort-btn:hover {
  color: var(--text-primary);
}

.sort-btn[aria-sort="ascending"] .sort-indicator::after {
  content: "↑";
}

.sort-btn[aria-sort="descending"] .sort-indicator::after {
  content: "↓";
}

.sort-btn:not([aria-sort]) .sort-indicator::after {
  content: "↕";
  opacity: 0.35;
}

#tx-tbody tr {
  transition: background-color 0.12s ease;
}

#tx-tbody tr:hover {
  background: var(--surface-muted);
}

#tx-tbody td {
  padding: 0.875rem 1rem;
  vertical-align: middle;
}

@media (min-width: 640px) {
  #tx-tbody td {
    padding: 0.875rem 1.25rem;
  }
}

.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 1.5rem;
  padding: 0.125rem 0.625rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: capitalize;
}

.status-pill.cleared {
  color: var(--success);
  background: color-mix(in srgb, var(--success) 14%, transparent);
}

.status-pill.pending {
  color: var(--warn);
  background: color-mix(in srgb, var(--warn) 16%, transparent);
}

.category-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-weight: 500;
}

.category-chip::before {
  content: "";
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 999px;
  background: var(--chip-color, #6366f1);
}

.amount-pos {
  color: var(--success);
  font-variant-numeric: tabular-nums;
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-weight: 500;
}

.amount-neg {
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-weight: 500;
}

/* ---------- Utilities ---------- */
.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;
}

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