/* ============================================================================
   BeBravo Echo — Design System
   Theme: Option A — light / blue
   Conformance: WCAG 2.2 Level AA
   Source of truth: shared/ui-tokens/theme.css → served at /shared/theme.css
   ============================================================================ */

/* ---------------------------------------------------------------------------
   1. Custom properties (design tokens)
   --------------------------------------------------------------------------- */

:root {
  color-scheme: light;

  /* Page / surface */
  --bg-page:        #eef2f7;
  --bg-surface:     #ffffff;
  --bg-surface-alt: #f8fafc;

  /* Text — contrast ratios verified against bg-surface and bg-page
     --text-primary  : 17.9:1 on white / 16.2:1 on bg-page  → AA ✓
     --text-secondary:  7.6:1 on white /  6.7:1 on bg-page  → AA ✓
     --text-hint     :  4.8:1 on white /  4.2:1 on bg-page  → large/decorative only */
  --text-primary:   #0f172a;
  --text-secondary: #475569;
  --text-hint:      #64748b;

  /* Primary blue
     --primary       : 6.7:1 on white → AA ✓
     --on-primary    : 6.7:1 on --primary → AA ✓
     --primary-hover : 8.7:1 on white → AA ✓ */
  --primary:        #1d4ed8;
  --on-primary:     #ffffff;
  --primary-hover:  #1e40af;
  --accent-text:    #1d4ed8;

  /* Danger / error
     --danger        : 6.5:1 on white → AA ✓
     --on-danger     : 6.5:1 on --danger → AA ✓ */
  --danger:         #b91c1c;
  --on-danger:      #ffffff;
  --danger-hover:   #991b1b;

  /* Success / warning families */
  --success:        #166534;
  --on-success:     #ffffff;
  --warning:        #92400e;
  --on-warning:     #ffffff;

  /* Borders & focus */
  --border:               #e2e8f0;
  --border-strong:        #94a3b8;
  --focus-ring:           #1d4ed8;
  --focus-glow:           rgba(29, 78, 216, 0.15);
  --focus-glow-danger:    rgba(185, 28, 28, 0.15);

  /* Info state */
  --info-bg:        #eff6ff;
  --info-border:    rgba(29, 78, 216, 0.25);
  --info-text:      #1e40af;

  /* Badge fills & text — each pair ≥ 4.5:1
     active    : #166534 on #dcfce7 → 6.5:1 ✓
     suspended : #92400e on #fef3c7 → 6.4:1 ✓
     revoked   : #991b1b on #fee2e2 → 6.8:1 ✓ */
  --badge-active-bg:        #dcfce7;
  --badge-active-text:      #166534;
  --badge-suspended-bg:     #fef3c7;
  --badge-suspended-text:   #92400e;
  --badge-revoked-bg:       #fee2e2;
  --badge-revoked-text:     #991b1b;

  /* Typography */
  --font-stack:    -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --font-normal:   400;
  --font-medium:   500;
  --font-semibold: 600;
  --text-sm:      0.875rem;
  --text-base:    1rem;
  --text-lg:      1.125rem;
  --text-xl:      1.25rem;
  --text-2xl:     1.5rem;
  --text-3xl:     1.875rem;

  /* Spacing */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-6: 1.5rem;
  --space-8: 2rem;

  /* Border radius */
  --radius-sm: 0.375rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-xl: 1rem;

  /* Shadows */
  --shadow-card:  0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-modal: 0 20px 60px rgba(0, 0, 0, 0.18), 0 8px 24px rgba(0, 0, 0, 0.08);
}

/* ---------------------------------------------------------------------------
   2. Global resets (opt-in — only applied when the design system is loaded)
   --------------------------------------------------------------------------- */

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

/* ---------------------------------------------------------------------------
   3. Skip-to-content link (WCAG 2.4.1)
   --------------------------------------------------------------------------- */

.echo-skip-link {
  position: absolute;
  top: -48px;
  left: var(--space-4);
  z-index: 9999;
  background: var(--primary);
  color: var(--on-primary);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-md);
  font-family: var(--font-stack);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  text-decoration: none;
  white-space: nowrap;
  transition: top 120ms;
}

.echo-skip-link:focus {
  top: var(--space-2);
  outline: 3px solid var(--on-primary);
  outline-offset: 2px;
}

/* ---------------------------------------------------------------------------
   4. App shell — sticky page header
   --------------------------------------------------------------------------- */

.echo-app-shell {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--space-6);
  height: 56px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

.echo-app-shell__brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.echo-app-shell__logo {
  height: 32px;
  width: auto;
  display: block;
}

.echo-app-shell__app-name {
  font-family: var(--font-stack);
  font-size: var(--text-base);
  font-weight: var(--font-medium);
  color: var(--text-primary);
}

.echo-app-shell__nav {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.echo-app-shell__user {
  font-family: var(--font-stack);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--text-secondary);
}

/* ---------------------------------------------------------------------------
   5. Buttons
   All buttons: min 40 px tall (≥ 24 × 24 target per WCAG 2.5.8);
   :focus-visible ring ≥ 2 px, offset ≥ 2 px, ≥ 3:1 contrast (WCAG 2.4.11)
   --------------------------------------------------------------------------- */

.echo-btn-primary,
.echo-btn-secondary,
.echo-btn-danger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: 0.625rem var(--space-4);
  min-height: 40px;
  min-width: 24px;
  border-radius: var(--radius-md);
  font-family: var(--font-stack);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  line-height: 1;
  cursor: pointer;
  border: none;
  text-decoration: none;
  white-space: nowrap;
  transition: background 120ms, box-shadow 120ms, border-color 120ms;
  -webkit-appearance: none;
}

.echo-btn-primary:focus-visible,
.echo-btn-secondary:focus-visible,
.echo-btn-danger:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.echo-btn-primary:disabled,
.echo-btn-secondary:disabled,
.echo-btn-danger:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  pointer-events: none;
}

/* Primary */
.echo-btn-primary {
  background: var(--primary);
  color: var(--on-primary);
}

.echo-btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

/* Secondary */
.echo-btn-secondary {
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
}

.echo-btn-secondary:hover:not(:disabled) {
  background: var(--bg-surface-alt);
  border-color: var(--text-secondary);
}

/* Danger */
.echo-btn-danger {
  background: var(--danger);
  color: var(--on-danger);
}

.echo-btn-danger:hover:not(:disabled) {
  background: var(--danger-hover);
}

/* Clearer, consistent hover feedback across ALL themed buttons — the plain
   background change alone read as "no hover" on some variants. */
.echo-btn-primary:hover:not(:disabled),
.echo-btn-secondary:hover:not(:disabled),
.echo-btn-danger:hover:not(:disabled) {
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18);
}

/* Secondary buttons blend into the surface in dark mode — give them a stronger
   border for contrast (applies to both system-dark and explicit dark, but not
   an explicit light choice). */
[data-theme="dark"] .echo-btn-secondary {
  border-color: var(--text-secondary);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .echo-btn-secondary {
    border-color: var(--text-secondary);
  }
}

/* ---------------------------------------------------------------------------
   6. Form controls
   --------------------------------------------------------------------------- */

.echo-input,
.echo-select,
.echo-textarea {
  display: block;
  width: 100%;
  padding: 0.5rem 0.75rem;
  min-height: 40px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-family: var(--font-stack);
  font-size: var(--text-base);
  line-height: 1.5;
  transition: border-color 120ms, box-shadow 120ms;
  -webkit-appearance: none;
}

.echo-input::placeholder,
.echo-textarea::placeholder {
  color: var(--text-hint);
}

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

.echo-input[aria-invalid="true"],
.echo-select[aria-invalid="true"],
.echo-textarea[aria-invalid="true"] {
  border-color: var(--danger);
}

.echo-input[aria-invalid="true"]:focus,
.echo-select[aria-invalid="true"]:focus,
.echo-textarea[aria-invalid="true"]:focus {
  box-shadow: 0 0 0 3px var(--focus-glow-danger);
}

.echo-textarea {
  min-height: 80px;
  resize: vertical;
}

/* ---------------------------------------------------------------------------
   7. Card
   --------------------------------------------------------------------------- */

.echo-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
}

/* ---------------------------------------------------------------------------
   8. Table
   --------------------------------------------------------------------------- */

.echo-table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-stack);
  font-size: var(--text-sm);
}

.echo-table th {
  text-align: left;
  font-weight: var(--font-medium);
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
  padding: var(--space-2) var(--space-3);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
}

.echo-table td {
  padding: var(--space-3);
  color: var(--text-primary);
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.echo-table tbody tr:last-child td {
  border-bottom: none;
}

.echo-table tbody tr:hover td {
  background: var(--bg-surface-alt);
}

/* ---------------------------------------------------------------------------
   9. Badges / status pills
   --------------------------------------------------------------------------- */

.echo-badge--active,
.echo-badge--suspended,
.echo-badge--revoked {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 0.1875rem 0.5rem;
  border-radius: 9999px;
  font-family: var(--font-stack);
  font-size: 0.75rem;
  font-weight: var(--font-medium);
  line-height: 1;
  white-space: nowrap;
}

.echo-badge--active {
  background: var(--badge-active-bg);
  color: var(--badge-active-text);
}

.echo-badge--suspended {
  background: var(--badge-suspended-bg);
  color: var(--badge-suspended-text);
}

.echo-badge--revoked {
  background: var(--badge-revoked-bg);
  color: var(--badge-revoked-text);
}

/* ---------------------------------------------------------------------------
   10. Modal
   --------------------------------------------------------------------------- */

.echo-modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  padding: var(--space-4);
}

.echo-modal {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-modal);
  max-width: 560px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}

/* ---------------------------------------------------------------------------
   11. Reduced-motion overrides (WCAG 1.4.1, 2.3.3)
   --------------------------------------------------------------------------- */

@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;
  }
}

/* ---------------------------------------------------------------------------
   12. Dark-mode semantic palette
   Auto: follows OS preference via media query.
   Manual: [data-theme="dark"] / [data-theme="light"] on <html> wins via
   source order (same specificity 0,1,0 as :root, placed later).
   --------------------------------------------------------------------------- */

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;

    --bg-page:        #0f172a;
    --bg-surface:     #1e293b;
    --bg-surface-alt: #172033;

    --text-primary:   #f1f5f9;
    --text-secondary: #cbd5e1;
    --text-hint:      #94a3b8;

    --primary:        #2563eb;
    --on-primary:     #ffffff;
    --primary-hover:  #1d4ed8;
    --accent-text:    #60a5fa;

    --danger:         #dc2626;
    --on-danger:      #ffffff;
    --danger-hover:   #b91c1c;

    /* Success / warning families — dark
       The light values (#166534 / #92400e) are near-black in dark mode: used as
       alert text on --badge-*-bg they land at ~1.4:1. These mirror the badge
       text tokens, which are already verified ≥ 4.5:1 on their dark fills.
       --on-success / --on-warning flip to dark ink so any
       `background: var(--success); color: var(--on-success)` pairing still passes. */
    --success:        #86efac;
    --on-success:     #0f172a;
    --warning:        #fcd34d;
    --on-warning:     #0f172a;

    --border:               #334155;
    --border-strong:        #475569;
    --focus-ring:           #60a5fa;
    --focus-glow:           rgba(96, 165, 250, 0.25);
    --focus-glow-danger:    rgba(239, 68, 68, 0.25);

    --info-bg:        rgba(37, 99, 235, 0.15);
    --info-border:    rgba(96, 165, 250, 0.3);
    --info-text:      #93c5fd;

    --badge-active-bg:        #16351f;
    --badge-active-text:      #86efac;
    --badge-suspended-bg:     #3a2e10;
    --badge-suspended-text:   #fcd34d;
    --badge-revoked-bg:       #3a1717;
    --badge-revoked-text:     #fca5a5;

    --shadow-card:  0 1px 0 rgba(255, 255, 255, 0.05), 0 1px 3px rgba(0, 0, 0, 0.3);
    --shadow-modal: 0 20px 60px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.3);
  }
}

[data-theme="dark"] {
  color-scheme: dark;

  --bg-page:        #0f172a;
  --bg-surface:     #1e293b;
  --bg-surface-alt: #172033;

  --text-primary:   #f1f5f9;
  --text-secondary: #cbd5e1;
  --text-hint:      #94a3b8;

  --primary:        #2563eb;
  --on-primary:     #ffffff;
  --primary-hover:  #1d4ed8;
  --accent-text:    #60a5fa;

  --danger:         #dc2626;
  --on-danger:      #ffffff;
  --danger-hover:   #b91c1c;

  /* Success / warning families — dark (see the @media block above for rationale) */
  --success:        #86efac;
  --on-success:     #0f172a;
  --warning:        #fcd34d;
  --on-warning:     #0f172a;

  --border:               #334155;
  --border-strong:        #475569;
  --focus-ring:           #60a5fa;
  --focus-glow:           rgba(96, 165, 250, 0.25);
  --focus-glow-danger:    rgba(239, 68, 68, 0.25);

  --info-bg:        rgba(37, 99, 235, 0.15);
  --info-border:    rgba(96, 165, 250, 0.3);
  --info-text:      #93c5fd;

  --badge-active-bg:        #16351f;
  --badge-active-text:      #86efac;
  --badge-suspended-bg:     #3a2e10;
  --badge-suspended-text:   #fcd34d;
  --badge-revoked-bg:       #3a1717;
  --badge-revoked-text:     #fca5a5;

  --shadow-card:  0 1px 0 rgba(255, 255, 255, 0.05), 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-modal: 0 20px 60px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.3);
}

[data-theme="light"] {
  color-scheme: light;

  --bg-page:        #eef2f7;
  --bg-surface:     #ffffff;
  --bg-surface-alt: #f8fafc;

  --text-primary:   #0f172a;
  --text-secondary: #475569;
  --text-hint:      #64748b;

  --primary:        #1d4ed8;
  --on-primary:     #ffffff;
  --primary-hover:  #1e40af;
  --accent-text:    #1d4ed8;

  --danger:         #b91c1c;
  --on-danger:      #ffffff;
  --danger-hover:   #991b1b;

  /* Must be restated here: the @media dark block above sets dark success/warning
     on :root, and [data-theme="light"] wins only for the properties it declares.
     Without these, an explicit light choice under a dark OS keeps dark tokens. */
  --success:        #166534;
  --on-success:     #ffffff;
  --warning:        #92400e;
  --on-warning:     #ffffff;

  --border:               #e2e8f0;
  --border-strong:        #94a3b8;
  --focus-ring:           #1d4ed8;
  --focus-glow:           rgba(29, 78, 216, 0.15);
  --focus-glow-danger:    rgba(185, 28, 28, 0.15);

  --info-bg:        #eff6ff;
  --info-border:    rgba(29, 78, 216, 0.25);
  --info-text:      #1e40af;

  --badge-active-bg:        #dcfce7;
  --badge-active-text:      #166534;
  --badge-suspended-bg:     #fef3c7;
  --badge-suspended-text:   #92400e;
  --badge-revoked-bg:       #fee2e2;
  --badge-revoked-text:     #991b1b;

  --shadow-card:  0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-modal: 0 20px 60px rgba(0, 0, 0, 0.18), 0 8px 24px rgba(0, 0, 0, 0.08);
}

/* Modal backdrop scrim deepens in dark mode */
@media (prefers-color-scheme: dark) {
  .echo-modal-backdrop { background: rgba(0, 0, 0, 0.7); }
}
[data-theme="dark"] .echo-modal-backdrop { background: rgba(0, 0, 0, 0.7); }
[data-theme="light"] .echo-modal-backdrop { background: rgba(15, 23, 42, 0.5); }

/* ---------------------------------------------------------------------------
   13. Theme toggle button
   Lives in .echo-app-shell__nav; cycled by theme-toggle.js
   --------------------------------------------------------------------------- */

.echo-app-shell__theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 120ms, color 120ms;
  flex-shrink: 0;
}

.echo-app-shell__theme-toggle:hover {
  background: var(--bg-surface-alt);
  color: var(--text-primary);
}

.echo-app-shell__theme-toggle:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.echo-app-shell__theme-toggle svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

/* ---------------------------------------------------------------------------
   14. Legacy token aliases
   Kept so any remaining code using --echo-* variables doesn't break silently
   while the full migration is in progress. Remove after all pages are migrated.
   --------------------------------------------------------------------------- */

:root {
  --echo-bg:           var(--bg-page);
  --echo-surface:      var(--bg-surface);
  --echo-surface-alt:  var(--bg-surface-alt);
  --echo-text:         var(--text-primary);
  --echo-muted:        var(--text-secondary);
  --echo-border:       var(--border);
  --echo-accent:       var(--primary);
  --echo-accent-strong: var(--primary-hover);
}

/* Backward-compat class aliases */
.echo-button-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: 0.625rem var(--space-4);
  min-height: 40px;
  border-radius: var(--radius-md);
  font-family: var(--font-stack);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  cursor: pointer;
  border: none;
  background: var(--primary);
  color: var(--on-primary);
  text-decoration: none;
  transition: background 120ms;
  -webkit-appearance: none;
}

.echo-button-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.echo-button-primary:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}
