/* ============================================
   CardVault Elite — main.css
   Base styles, CSS custom properties, resets
   ============================================ */

/* CSS Custom Properties (Blush & Sage default) */
:root {
  /* Brand palette */
  --blush:          #EFD6D2;
  --dusty-rose:     #D8A7A0;
  --soft-sage:      #AFC2B0;
  --muted-olive:    #7E927F;
  --warm-cream:     #FAF7F5;
  --soft-taupe:     #8B817C;
  --charcoal:       #2F2F2F;
  --white:          #FFFFFF;

  /* Semantic tokens — Blush & Sage theme */
  --bg-page:        var(--warm-cream);
  --bg-card:        var(--white);
  --bg-elevated:    var(--white);
  --bg-subtle:      #F3EDEA;
  --bg-accent:      var(--blush);
  --bg-accent-strong: var(--soft-sage);

  --text-primary:   var(--charcoal);
  /* Text colours are tuned to clear WCAG AA (4.5:1) on white, cream, the
     subtle panel and blush chips. The old taupe/greys measured 3.0–3.8:1. */
  --text-secondary: #655E5A;
  --text-muted:     #726B68;
  --text-on-accent: var(--white);
  --text-on-light:  var(--charcoal);

  --border:         #ECE6E3;
  --border-strong:  #DFD7D3;
  --divider:        #F0EAE7;

  --accent:         var(--soft-sage);
  --progress-fill:  #6A876E;   /* darker sage: 3.4:1 on the track */
  --accent-hover:   var(--muted-olive);
  --accent-soft:    var(--blush);
  --accent-text:    #556356;

  /* Primary button + danger button ink, per theme (white on the pastel sage
     measured 3.3:1; Night Vault needs dark ink on its light sage). */
  --btn-primary-bg:    #687969;
  --btn-primary-fg:    #FFFFFF;
  --btn-primary-hover: var(--charcoal);
  --on-error:          #FFFFFF;

  /* Chart series — each ≥3:1 against this theme's cards (WCAG 1.4.11). */
  --chart-1: #6F8A71;
  --chart-2: #B0675C;
  --chart-3: #4F6A86;
  --chart-4: #93763F;
  --chart-5: #8A5F7A;
  --chart-6: #5E7F7A;
  --chart-7: #A5703F;
  --chart-8: #6B6560;

  /* Dashboard welcome panel + picture (components/hero-art.js) */
  --hero-bg: linear-gradient(118deg, var(--success-bg) 0%, var(--bg-card) 55%, var(--accent-soft) 100%);
  --hero-border: var(--border-strong);
  --hero-eyebrow-bg: var(--bg-card); --hero-eyebrow-text: var(--accent-text);
  --art-glow: var(--accent-soft); --art-shadow: var(--charcoal); --art-ink: var(--charcoal);
  --art-window: var(--warm-cream); --art-shine: var(--white);
  --art-card1: var(--chart-3); --art-card2: var(--chart-2); --art-card3: var(--chart-1);
  --art-pack: var(--chart-5); --art-pack2: var(--chart-4); --art-star: #D9B25B;
  --art-case: var(--white); --art-case-edge: var(--border-strong); --art-label: var(--chart-4);
  --art-spark: var(--chart-4);

  /* Status colours double as chip text on their -bg tint: all ≥4.5:1. */
  --success:        #5D6E61;
  --success-bg:     #E8EEE7;
  --warning:        #756A52;
  --warning-bg:     #F5F0E2;
  --error:          #8A574D;
  --error-bg:       #F4E2DC;
  --info:           #5A6A79;
  --info-bg:        #E5ECF2;

  --shadow-sm:      0 1px 2px rgba(47, 47, 47, 0.04);
  --shadow:         0 2px 8px rgba(47, 47, 47, 0.06);
  --shadow-md:      0 4px 16px rgba(47, 47, 47, 0.08);
  --shadow-lg:      0 8px 32px rgba(47, 47, 47, 0.10);
  --shadow-xl:      0 16px 48px rgba(47, 47, 47, 0.12);

  /* Type scale */
  --font-display:   'Fraunces', Georgia, 'Times New Roman', serif;
  --font-body:      'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  /* Numeric font — uses the same sans stack as body (no slashed/crossed zeros,
     lighter weight than DM Mono). Tabular alignment comes from
     font-variant-numeric on .mono / .font-mono utility classes. */
  --font-mono:      'DM Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;

  --text-xs:        0.6875rem;
  --text-sm:        0.8125rem;
  --text-base:      0.9375rem;
  --text-lg:        1.0625rem;
  --text-xl:        1.25rem;
  --text-2xl:       1.5rem;
  --text-3xl:       1.875rem;
  --text-4xl:       2.25rem;

  --leading-tight:  1.2;
  --leading-snug:   1.4;
  --leading-normal: 1.55;
  --leading-relaxed: 1.7;

  /* Spacing scale (4px base) */
  --s-1: 4px;
  --s-2: 8px;
  --s-3: 12px;
  --s-4: 16px;
  --s-5: 20px;
  --s-6: 24px;
  --s-8: 32px;
  --s-10: 40px;
  --s-12: 48px;
  --s-16: 64px;

  /* Radius */
  --radius-sm: 6px;
  --radius:    10px;
  --radius-md: 14px;
  --radius-lg: 18px;
  --radius-xl: 24px;
  --radius-pill: 999px;

  /* Layout */
  --sidebar-w:     240px;
  --sidebar-w-collapsed: 64px;
  --bottom-nav-h:  68px;
  --topbar-h:      56px;
  --content-max:   1320px;

  /* Motion */
  --ease:          cubic-bezier(0.4, 0, 0.2, 1);
  --ease-out:      cubic-bezier(0.16, 1, 0.3, 1);
  --dur-fast:      120ms;
  --dur:           180ms;
  --dur-slow:      300ms;

  /* Font size scale (user controlled in settings) */
  --font-scale: 1;
}

/* Font-scale modifier classes */
html[data-font-size="sm"] { --font-scale: 0.92; }
html[data-font-size="md"] { --font-scale: 1; }
html[data-font-size="lg"] { --font-scale: 1.1; }

/* Reset */
*, *::before, *::after { box-sizing: border-box; }
/* The hidden attribute always wins (component classes like .field and .btn set
   their own display, which would otherwise show a "hidden" element). */
[hidden] { display: none !important; }
* { margin: 0; padding: 0; }

html, body {
  height: 100%;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

/* Settings → Text size scales the root, and every font size is in rem, so
   the whole app follows. A percentage keeps the person's browser size too. */
html {
  font-size: calc(100% * var(--font-scale));
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  font-family: var(--font-body);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--text-primary);
  background: var(--bg-page);
  min-height: 100dvh;
  overflow-x: hidden;
  transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease);
}

img, picture, svg, video {
  display: block;
  max-width: 100%;
  height: auto;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
}

button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.55; }

a {
  color: var(--accent-text);
  text-decoration: none;
}
@media (hover: hover) {
  a:hover { text-decoration: underline; }
}

/* Typography utility classes */
.font-display { font-family: var(--font-display); font-weight: 400; letter-spacing: -0.01em; }
.font-mono    { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }

.t-xs   { font-size: var(--text-xs); }
.t-sm   { font-size: var(--text-sm); }
.t-base { font-size: var(--text-base); }
.t-lg   { font-size: var(--text-lg); }
.t-xl   { font-size: var(--text-xl); }
.t-2xl  { font-size: var(--text-2xl); }
.t-3xl  { font-size: var(--text-3xl); }
.t-4xl  { font-size: var(--text-4xl); }

.t-muted     { color: var(--text-secondary); }
.t-faint     { color: var(--text-muted); }
.t-success   { color: var(--success); }
.t-warning   { color: var(--warning); }
.t-error     { color: var(--error); }
.t-accent    { color: var(--accent-text); }

/* Page headings */
h1, h2, h3, h4 {
  font-family: var(--font-display);
  font-weight: 400;
  line-height: var(--leading-tight);
  letter-spacing: -0.015em;
  color: var(--text-primary);
}
h1 { font-size: var(--text-3xl); }
h2 { font-size: var(--text-2xl); }
h3 { font-size: var(--text-xl); }
h4 { font-size: var(--text-lg); }

/* Focus ring (accessibility) */
:focus-visible {
  outline: 2px solid var(--accent-hover);
  outline-offset: 2px;
  border-radius: 4px;
}

/* Selection */
::selection {
  background: var(--accent-soft);
  color: var(--text-primary);
}

/* Scrollbar (subtle) */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }
@media (hover: hover) {
  *::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }
}

/* App layout shell */
.app-shell {
  display: flex;
  min-height: 100dvh;
  position: relative;
}

.app-sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  position: sticky;
  top: 0;
  height: 100dvh;
  overflow-y: auto;
  transition: width var(--dur) var(--ease);
  z-index: 30;
}

.app-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.app-content {
  flex: 1;
  padding: var(--s-8);
  max-width: var(--content-max);
  width: 100%;
  margin: 0 auto;
}

.app-topbar {
  display: none;
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg-page);
  height: var(--topbar-h);
  padding: 0 var(--s-4);
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

/* Page transitions */
.page {
  animation: page-in var(--dur-slow) var(--ease-out);
}

/* Card primitive */
.card {
  background: var(--bg-card);
  border-radius: var(--radius-md);
  padding: var(--s-6);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.card--flat {
  box-shadow: none;
}

.card--interactive {
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
  cursor: pointer;
}
@media (hover: hover) {
  .card--interactive:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
  }
}

/* Button primitives */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-2);
  padding: 11px 18px;
  font-size: var(--text-sm);
  font-weight: 500;
  border-radius: var(--radius);
  transition: all var(--dur) var(--ease);
  white-space: nowrap;
  min-height: 40px;
  user-select: none;
}
.btn:active { transform: scale(0.97); }

.btn--primary {
  background: var(--btn-primary-bg);
  color: var(--btn-primary-fg);
}
@media (hover: hover) {
  .btn--primary:hover { background: var(--btn-primary-hover); }
}

.btn--secondary {
  background: var(--bg-subtle);
  color: var(--text-primary);
}
@media (hover: hover) {
  .btn--secondary:hover { background: var(--border); }
}

.btn--ghost {
  color: var(--text-secondary);
}
@media (hover: hover) {
  .btn--ghost:hover { background: var(--bg-subtle); color: var(--text-primary); }
}

.btn--danger {
  background: var(--error);
  color: var(--on-error);
}
@media (hover: hover) {
  .btn--danger:hover { filter: brightness(0.92); }
}

.btn--icon {
  width: 40px;
  padding: 0;
}

.btn--lg { padding: 14px 24px; min-height: 48px; font-size: var(--text-base); }
.btn--sm { padding: 7px 12px; min-height: 32px; font-size: var(--text-xs); }

/* Form primitives */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: var(--s-4);
}
.field label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text-primary);
}
.field-error {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--error);
}
.field input[aria-invalid="true"] { border-color: var(--error); }
.field .hint {
  font-size: var(--text-xs);
  color: var(--text-muted);
}
.field input[type="text"],
.field input[type="number"],
.field input[type="email"],
.field input[type="date"],
.field input[type="url"],
.field select,
.field textarea {
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 11px 14px;
  font-size: var(--text-base);
  color: var(--text-primary);
  transition: border-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
  width: 100%;
  font-family: var(--font-body);
}
/* iPhone Safari draws date/time boxes at its own width and ignores
   width:100%, so they stuck out of forms. Field children may also shrink. */
.field > *, .field-row > *, .grid > * { min-width: 0; max-width: 100%; }
/* A pasted link or other unbroken text wraps instead of pushing the page
   sideways (break-word only breaks when a word wouldn't fit). */
.page-root, .modal { overflow-wrap: break-word; }
input[type="date"], input[type="time"], input[type="datetime-local"],
input[type="month"], input[type="week"] {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  text-align: left;
}
input::-webkit-date-and-time-value { text-align: left; margin: 0; min-height: 1.25em; }
/* iPhones zoom in on any field with text under 16px. */
@media (pointer: coarse) {
  input, select, textarea { font-size: max(16px, 1rem) !important; }
}
.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.field textarea {
  resize: vertical;
  min-height: 88px;
  font-family: var(--font-body);
}
.field .field-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: var(--s-3);
}

/* Section/Group titles inside forms */
.form-section {
  margin-top: var(--s-6);
  padding-top: var(--s-5);
  border-top: 1px solid var(--divider);
}
.form-section:first-child { border-top: none; padding-top: 0; margin-top: 0; }
.form-section__title {
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: var(--s-3);
}

/* Page header */
.page-header {
  margin-bottom: var(--s-8);
}
.page-header h1 {
  font-size: var(--text-3xl);
  margin-bottom: 4px;
}
.page-header .sub {
  color: var(--text-secondary);
  font-size: var(--text-base);
}

/* Grid */
.grid { display: grid; gap: var(--s-4); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }

/* Dashboard: Portfolio value (left) + Recent activity (right) on desktop,
   stacks vertically below the tablet breakpoint so neither card overflows. */
.dash-main-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.7fr) minmax(0, 1fr);
  gap: var(--s-5);
  margin-bottom: var(--s-6);
  min-width: 0;
}
@media (max-width: 1023px) {
  .dash-main-grid { grid-template-columns: minmax(0, 1fr); gap: var(--s-4); }
}
.dash-main-grid > * { min-width: 0; }

/* Flex helpers */
.row { display: flex; align-items: center; }
.row-between { display: flex; align-items: center; justify-content: space-between; }
.col { display: flex; flex-direction: column; }
.gap-1 { gap: var(--s-1); }
.gap-2 { gap: var(--s-2); }
.gap-3 { gap: var(--s-3); }
.gap-4 { gap: var(--s-4); }
.gap-6 { gap: var(--s-6); }

/* Utility */
.hidden { display: none !important; }
.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;
}
.mono { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.no-wrap { white-space: nowrap; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Pill / chip */
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  background: var(--bg-subtle);
  color: var(--text-secondary);
  font-size: var(--text-xs);
  font-weight: 500;
}
.chip--accent { background: var(--accent-soft); color: var(--accent-text); }
.chip--success { background: var(--success-bg); color: var(--success); }
.chip--warning { background: var(--warning-bg); color: var(--warning); }
.chip--error   { background: var(--error-bg); color: var(--error); }
.chip--info    { background: var(--info-bg); color: var(--info); }

/* Divider */
.divider { height: 1px; background: var(--divider); margin: var(--s-4) 0; border: none; }

/* Safe area insets (iOS) */
.safe-bottom { padding-bottom: max(var(--s-4), env(safe-area-inset-bottom)); }
.safe-top { padding-top: max(var(--s-4), env(safe-area-inset-top)); }

/* ===== Responsive ===== */
/* Same query as the bottom bar (components.css): touch screens get it too. */
@media (max-width: 1023px), (hover: none), (pointer: coarse) {
  .app-sidebar { display: none; }
  .app-topbar { display: flex; }
  .app-content {
    padding-bottom: calc(var(--bottom-nav-h) + var(--s-6) + env(safe-area-inset-bottom));
  }
}

@media (max-width: 1023px) {
  .app-content { padding: var(--s-4); padding-bottom: calc(var(--bottom-nav-h) + var(--s-6) + env(safe-area-inset-bottom)); }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-3 { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 640px) {
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
  .page-header h1 { font-size: var(--text-2xl); }
  .field .field-row { grid-template-columns: minmax(0, 1fr); }
  .card { padding: var(--s-5); border-radius: var(--radius); }
}

/* Reduced motion — system preference OR user toggle in Settings */
@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;
  }
}
html[data-motion="off"] *,
html[data-motion="off"] *::before,
html[data-motion="off"] *::after {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
  scroll-behavior: auto !important;
}
html[data-motion="off"] .confetti { display: none !important; }
