/* ============================================================================
   Passerelle — components.css
   One grammar per element: one button, one badge, one table, one modal,
   one toast, one wait block — everywhere.

   Load AFTER tokens.css and BEFORE any page-level <style>, so a page that
   still carries its own rules keeps winning during migration.

   Sources (merged, not invented):
     app_superuser/base.html:94-164   .card .metric-* .chip-* .btn .form-* .tabs
     app_settings/settings.html:5-217 .form-grid .section-card .data-table
                                      .badge-* .btn .fieldset

   RULE: no raw hex in this file. Every colour comes from a token; tints are
   derived with color-mix() so a token change propagates everywhere.
   ========================================================================== */

/* ── Focus: one visible ring, product-wide ──────────────────────────────────
   :focus-visible only, so mouse users never see it but keyboard users always
   do. Every interactive element below inherits this. */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
summary:focus-visible,
[tabindex]:focus-visible {
  outline: var(--focus-ring);
  outline-offset: 2px;
  border-radius: var(--radius-s);
}

/* ============================================================================
   Buttons
   ========================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  /* 40px minimum touch target (WCAG 2.5.5 / mobile ergonomics). */
  min-height: 40px;
  padding: var(--space-2) var(--space-4);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-m);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 0.85rem;
  font-weight: 500;
  line-height: 1.2;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease,
    color 0.15s ease;
}

.btn:hover {
  background: var(--bg-hover);
  border-color: var(--text-muted);
  color: var(--text-bright);
}

.btn:disabled,
.btn[aria-disabled='true'] {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

.btn--primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--text-bright);
}

.btn--primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
  color: var(--text-bright);
}

.btn--danger {
  background: var(--danger);
  border-color: var(--danger);
  color: var(--text-bright);
}

.btn--danger:hover {
  background: color-mix(in srgb, var(--danger) 82%, black);
  border-color: color-mix(in srgb, var(--danger) 82%, black);
  color: var(--text-bright);
}

.btn--ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-secondary);
}

.btn--ghost:hover {
  background: var(--bg-hover);
  border-color: transparent;
  color: var(--text-primary);
}

/* Compact variant — exempt from the 40px rule by design (dense toolbars,
   table row actions). Do not use it for primary page actions. */
.btn--sm {
  min-height: 32px;
  padding: var(--space-1) var(--space-3);
  font-size: 0.78rem;
}

/* ============================================================================
   Cards
   ========================================================================== */

.card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-card);
  overflow: hidden;
}

.card__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border-color);
  font-family: var(--font-ui);
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--text-bright);
}

.card__body {
  padding: var(--space-4);
}

/* ============================================================================
   Badges — semantic status, tinted from the semantic tokens only
   ========================================================================== */

.badge {
  display: inline-block;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-s);
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  font-family: var(--font-ui);
  font-size: 0.72rem;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
}

.badge--success {
  background: color-mix(in srgb, var(--success) 16%, transparent);
  color: color-mix(in srgb, var(--success) 78%, var(--text-bright));
}

.badge--warning {
  background: color-mix(in srgb, var(--warning) 16%, transparent);
  color: color-mix(in srgb, var(--warning) 78%, var(--text-bright));
}

.badge--danger {
  background: color-mix(in srgb, var(--danger) 16%, transparent);
  color: color-mix(in srgb, var(--danger) 72%, var(--text-bright));
}

.badge--info {
  background: color-mix(in srgb, var(--info) 16%, transparent);
  color: color-mix(in srgb, var(--info) 82%, var(--text-bright));
}

.badge--neutral {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

/* ============================================================================
   Chips — data-driven colour: style="--chip-color: #xxxxxx"
   The colour comes from the data (an entity type, a workflow status), so it
   cannot be a token; everything else about the chip is.
   ========================================================================== */

.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-3);
  border: 1px solid
    color-mix(in srgb, var(--chip-color, var(--text-muted)) 40%, transparent);
  border-radius: var(--radius-pill);
  background: color-mix(
    in srgb,
    var(--chip-color, var(--text-muted)) 14%,
    transparent
  );
  color: var(--chip-color, var(--text-secondary));
  font-family: var(--font-ui);
  font-size: 0.74rem;
  font-weight: 600;
  line-height: 1.4;
  white-space: nowrap;
}

/* ============================================================================
   Tables — dense data table. Wrap in .table-wrap for horizontal scroll.
   ========================================================================== */

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

.table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-ui);
  font-size: 0.85rem;
}

.table th {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--border-color);
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-align: left;
  text-transform: uppercase;
  white-space: nowrap;
}

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

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

/* Sortable headers: mark the column with aria-sort, no extra class needed. */
.table th[aria-sort] {
  cursor: pointer;
}

.table th[aria-sort]:hover {
  color: var(--text-primary);
}

.table th[aria-sort='ascending']::after {
  content: ' \2191';
  color: var(--accent);
}

.table th[aria-sort='descending']::after {
  content: ' \2193';
  color: var(--accent);
}

/* ============================================================================
   Forms
   ========================================================================== */

.form-row {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  margin-bottom: var(--space-4);
}

.form-label {
  font-family: var(--font-ui);
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.form-input,
.form-select {
  width: 100%;
  min-height: 40px;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-m);
  background: var(--bg-tertiary);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.88rem;
  transition: border-color 0.15s ease;
}

.form-input:hover,
.form-select:hover {
  border-color: var(--text-muted);
}

.form-input:focus,
.form-select:focus {
  border-color: var(--accent);
}

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

textarea.form-input {
  min-height: 90px;
  resize: vertical;
}

.form-help {
  font-size: 0.72rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.form-error {
  font-size: 0.74rem;
  font-weight: 500;
  color: var(--danger);
}

.form-row:has(.form-error) .form-input,
.form-row:has(.form-error) .form-select,
.form-input[aria-invalid='true'],
.form-select[aria-invalid='true'] {
  border-color: var(--danger);
}

/* ============================================================================
   Empty states — icon + explanation + the next action
   ========================================================================== */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-6) var(--space-4);
  text-align: center;
  color: var(--text-secondary);
}

.empty-state__icon {
  font-size: 2rem;
  line-height: 1;
  opacity: 0.6;
}

.empty-state__text {
  max-width: 46ch;
  font-size: 0.88rem;
  line-height: 1.6;
  color: var(--text-muted);
}

.empty-state__action {
  margin-top: var(--space-1);
}

/* ============================================================================
   KPI / metric tiles  (from app_superuser .metric-*)
   ========================================================================== */

.kpi {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  padding: var(--space-4);
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-card);
}

.kpi__value {
  font-family: var(--font-ui);
  font-size: 1.5rem;
  font-weight: 700;
  line-height: 1.1;
  color: var(--text-bright);
  font-variant-numeric: tabular-nums;
}

.kpi__label {
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

/* ============================================================================
   Grid — responsive, auto-collapsing
   ========================================================================== */

.grid {
  display: grid;
  gap: var(--space-4);
  grid-template-columns: 1fr;
}

.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));
}

@media (max-width: 900px) {
  .grid--3,
  .grid--4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 600px) {
  .grid--2,
  .grid--3,
  .grid--4 {
    grid-template-columns: 1fr;
  }
}

/* ============================================================================
   Modal — dialog semantics live in the markup (role/aria-modal/aria-labelledby),
   focus trap + restore live in passerelle.js.
   ========================================================================== */

.modal[hidden] {
  display: none;
}

.modal__backdrop {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: color-mix(in srgb, var(--bg-primary) 78%, transparent);
}

.modal__panel {
  width: 100%;
  max-width: 34rem;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-card);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.5);
  font-family: var(--font-ui);
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4);
  border-bottom: 1px solid var(--border-color);
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-bright);
}

.modal__body {
  padding: var(--space-4);
  overflow-y: auto;
  font-size: 0.88rem;
  line-height: 1.6;
  color: var(--text-secondary);
}

/* Action bar. Not named in the design contract but required by confirm():
   a dialog needs somewhere to put its verb-named buttons. */
.modal__footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: var(--space-4);
  border-top: 1px solid var(--border-color);
}

@media (max-width: 600px) {
  .modal__footer {
    flex-direction: column-reverse;
  }

  .modal__footer .btn {
    width: 100%;
  }
}

/* ============================================================================
   LLM wait block — spinner + honest expected duration + live elapsed timer
   Markup is produced by Passerelle.llmWait(); sub-elements are internal.
   Source: app_chat/chat.html:361-382 + draft_builder.html:399-521
   ========================================================================== */

.llm-wait {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-4);
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-card);
  font-family: var(--font-ui);
}

.llm-wait[hidden] {
  display: none;
}

.llm-wait__spinner {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  margin-top: 2px;
  border: 2px solid var(--border-color);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: psl-spin 0.7s linear infinite;
}

.llm-wait__body {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  min-width: 0;
}

.llm-wait__title {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-bright);
}

.llm-wait__hint {
  font-size: 0.78rem;
  line-height: 1.55;
  color: var(--text-secondary);
}

.llm-wait__elapsed {
  font-family: var(--font-mono);
  font-size: 0.74rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.llm-wait__actions {
  margin-top: var(--space-2);
}

/* Error state: the spinner stops, the retry button appears. */
.llm-wait.is-error {
  border-color: color-mix(in srgb, var(--danger) 55%, var(--border-color));
}

.llm-wait.is-error .llm-wait__spinner {
  animation: none;
  border-color: var(--danger);
}

.llm-wait.is-error .llm-wait__title {
  color: var(--danger);
}

/* The job is running long but has not failed. */
.llm-wait.is-overrun .llm-wait__elapsed {
  color: var(--warning);
}

@keyframes psl-spin {
  to {
    transform: rotate(360deg);
  }
}

/* ============================================================================
   Save pill — saved / saving / unsaved / error
   Source: app_expe_redaction/partials/_content.html:61-158
   ========================================================================== */

.save-pill {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 28px;
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-pill);
  background: var(--bg-tertiary);
  color: var(--text-muted);
  font-family: var(--font-ui);
  font-size: 0.74rem;
  font-weight: 600;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.save-pill::before {
  content: '';
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
  flex-shrink: 0;
}

.save-pill.is-saved {
  color: var(--success);
  border-color: color-mix(in srgb, var(--success) 40%, transparent);
}

.save-pill.is-saving {
  color: var(--info);
  border-color: color-mix(in srgb, var(--info) 40%, transparent);
}

.save-pill.is-saving::before {
  animation: psl-pulse 1s ease-in-out infinite;
}

.save-pill.is-unsaved {
  color: var(--warning);
  border-color: color-mix(in srgb, var(--warning) 40%, transparent);
}

.save-pill.is-error {
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 50%, transparent);
}

@keyframes psl-pulse {
  50% {
    opacity: 0.35;
  }
}

/* ============================================================================
   .prose — THE READING CANVAS
   Everywhere the product renders intelligence prose (L1 drafts, L2/L3 reports,
   fiches, and above all the Lettre Navale) uses this one canvas. It is the
   single place the design spends its boldness.
   ========================================================================== */

.prose {
  max-width: 68ch;
  font-family: var(--font-reading);
  font-size: 1.02rem;
  line-height: 1.75;
  color: var(--text-primary);
  overflow-wrap: break-word;
}

.prose > * + * {
  margin-top: var(--space-4);
}

/* Quiet heading scale: headings are UI-voice, the body is reading-voice. */
.prose h1,
.prose h2,
.prose h3,
.prose h4 {
  font-family: var(--font-ui);
  font-weight: 600;
  line-height: 1.3;
  color: var(--text-bright);
}

.prose h1 {
  font-size: 1.55rem;
}

.prose h2 {
  font-size: 1.22rem;
  padding-bottom: var(--space-2);
  border-bottom: 1px solid var(--border-color);
}

.prose h3 {
  font-size: 1.05rem;
}

.prose h4 {
  font-size: 0.94rem;
  color: var(--text-secondary);
}

.prose > h2,
.prose > h3,
.prose > h4 {
  margin-top: var(--space-6);
}

.prose p {
  margin-top: var(--space-4);
}

.prose a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.prose a:hover {
  color: var(--accent-hover);
}

.prose strong {
  color: var(--text-bright);
  font-weight: 700;
}

.prose ul,
.prose ol {
  padding-left: var(--space-6);
}

.prose li + li {
  margin-top: var(--space-2);
}

.prose blockquote {
  padding: var(--space-2) 0 var(--space-2) var(--space-4);
  border-left: 3px solid var(--accent);
  color: var(--text-secondary);
  font-style: italic;
}

.prose hr {
  border: 0;
  border-top: 1px solid var(--border-color);
}

.prose code {
  padding: 0.1em 0.35em;
  border-radius: var(--radius-s);
  background: var(--bg-tertiary);
  font-family: var(--font-mono);
  font-size: 0.86em;
  color: var(--text-bright);
}

.prose pre {
  padding: var(--space-3);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-m);
  background: var(--bg-tertiary);
  overflow-x: auto;
}

.prose pre code {
  padding: 0;
  background: none;
}

.prose table {
  width: 100%;
  border-collapse: collapse;
  font-family: var(--font-ui);
  font-size: 0.86rem;
}

.prose th,
.prose td {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border-color);
  text-align: left;
}

.prose th {
  background: var(--bg-tertiary);
  color: var(--text-bright);
  font-weight: 600;
}

.prose img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-m);
}

.prose figcaption {
  font-family: var(--font-ui);
  font-size: 0.76rem;
  color: var(--text-muted);
  text-align: center;
}

/* ── Citation anchors ────────────────────────────────────────────────────────
   Convention (no extra class needed): a citation is a <sup><a href="#..."></a>
   </sup> pointing at its source note. Jumping to a note highlights it, so the
   reader never loses the reference. Mirrors app_chat's footnote/:target
   pattern. */
.prose sup a {
  padding: 0 0.15em;
  color: var(--accent);
  font-family: var(--font-ui);
  font-size: 0.72em;
  font-weight: 600;
  text-decoration: none;
}

.prose sup a:hover,
.prose sup a:focus-visible {
  text-decoration: underline;
}

.prose :target {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  border-radius: var(--radius-s);
  scroll-margin-top: 4rem;
}

/* ── Print: the product's deliverable is a printed / PDF letter ───────────── */
@media print {
  .prose {
    max-width: none;
    color: black;
    font-size: 11pt;
    line-height: 1.5;
  }

  .prose h1,
  .prose h2,
  .prose h3,
  .prose h4 {
    color: black;
    /* Never orphan a heading at the foot of a page. */
    break-after: avoid;
    page-break-after: avoid;
  }

  .prose h2 {
    border-bottom: 1px solid black;
  }

  .prose strong {
    color: black;
  }

  .prose a {
    color: black;
    text-decoration: none;
  }

  .prose blockquote {
    border-left: 2px solid black;
    color: black;
  }

  .prose code,
  .prose pre,
  .prose th {
    background: none;
    color: black;
    border-color: black;
  }

  .prose th,
  .prose td {
    border: 1px solid black;
  }

  .prose :target {
    background: none;
  }

  .prose p,
  .prose blockquote,
  .prose li,
  .prose table,
  .prose figure {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  /* Chrome that must never reach paper. */
  .llm-wait,
  .save-pill,
  .modal__backdrop {
    display: none !important;
  }
}

/* ============================================================================
   Utilities
   ========================================================================== */

.u-visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.u-stack {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.u-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

/* ============================================================================
   Motion — honoured globally, per the design principle "motion only where it
   informs". Spinners keep a single frame so state is still legible.
   ========================================================================== */

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

/* ============================================================================
   Mobile: 16px form fields stop iOS from zooming on focus.
   Source: app_step1_capture/capture_dashboard.html:70-328
   ========================================================================== */

@media (max-width: 600px) {
  .form-input,
  .form-select {
    font-size: 16px;
  }
}
