/*
 * Reusable Connect components, built on brand tokens and the ported brand classes.
 * Each section matches a cotton component in core/templates/cotton/ where one exists.
 * Rules here must not depend on which page they appear on; that belongs in layout or pages.
 */
@layer components {

  /* ---------- Icons (c-icon) ---------- */
  .icon { width: 1.25em; height: 1.25em; flex: none; }

  /* ---------- Buttons (c-button) ----------
     The brand ships .bp-btn and its variants; c-button maps intents onto them.
     These are the two additions intents need. */
  .bp-btn-block { width: 100%; justify-content: center; }
  .bp-btn-block.bp-btn-has-orb { justify-content: space-between; padding-left: var(--space-5); }
  .bp-btn-danger { background: var(--danger); color: var(--paper); }
  .bp-btn-danger:hover:not(:disabled) { background: color-mix(in srgb, var(--danger) 82%, var(--ink)); }

  /* ---------- Icon buttons (c-icon-button) ---------- */
  .icon-btn {
    display: inline-grid; place-items: center; width: 40px; height: 40px; flex: none;
    border-radius: var(--radius-pill); border: 1px solid var(--line-control);
    background: transparent; color: var(--ink); cursor: pointer; text-decoration: none;
    transition: border-color var(--bp-dur-quick) var(--bp-ease-tide), background-color var(--bp-dur-quick) var(--bp-ease-tide);
  }
  .icon-btn:hover { border-color: var(--line-strong); }
  .icon-btn .icon { width: 18px; height: 18px; }
  .icon-btn-sm { width: 28px; height: 28px; }
  .icon-btn-sm .icon { width: 14px; height: 14px; }
  .icon-btn-quiet { border-color: transparent; color: var(--ink-muted); }
  .icon-btn-quiet:hover { border-color: transparent; background: var(--paper-subtle); color: var(--ink); }
  /* Copy variant: a check mark for a moment after copying (static/js/copy.js). */
  .copy-btn .copy-done { display: none; }
  .copy-btn.is-copied { border-color: var(--success); }
  .copy-btn.is-copied .copy-idle { display: none; }
  .copy-btn.is-copied .copy-done { display: block; color: var(--success); }

  /* ---------- Page header (c-page-header) and section headers ---------- */
  .page-header { display: grid; gap: var(--space-4); }
  .page-header-row { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: var(--space-5); }
  .page-title { font-size: var(--fluid-heading-1); line-height: 1.05; font-weight: 700; letter-spacing: -.04em; }
  .page-lead { font-size: var(--fluid-lead); line-height: 1.5; font-weight: 300; color: var(--ink-muted); max-width: 60ch; }
  .page-actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); }
  /* minmax(0, 1fr): a wide child (a table) scrolls in its own frame instead of widening the page. */
  .section { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-4); }
  .section-title { font-size: var(--fluid-heading-2); line-height: 1.15; font-weight: 600; }
  .section-header-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-4); }
  /* A wrapping row of small things: badges, tags, an issue number. */
  .inline-row { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-3); }

  /* ---------- Detail list (<dl class="detail-list">) ---------- */
  .detail-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 220px), 1fr)); gap: var(--space-5); margin: 0; }
  .detail-list dt { font: 500 12px/16px var(--font-mono); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-muted); }
  .detail-list dd { margin: var(--space-2) 0 0; }

  /* ---------- Cards (c-card) ---------- */
  .bp-card-compact { min-height: 0; padding: var(--space-5); gap: var(--space-3); border-radius: var(--radius-md); }
  .bp-card-footer { gap: var(--space-3); }
  .card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 280px), 1fr)); gap: var(--fluid-gap); }

  /* ---------- Forms (c-field) ---------- */
  .form-stack { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-5); }
  .inline-form { display: flex; flex-wrap: wrap; align-items: end; gap: var(--space-4); }
  .inline-form .bp-field { min-width: 240px; }
  /* Fields side by side that stack on narrow screens, e.g. amount, tax and currency. */
  .form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 160px), 1fr)); gap: var(--space-4); align-items: start; }
  /* GET filters above a list. */
  .filter-form { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 180px), 1fr)); gap: var(--space-4); align-items: end; }
  textarea.bp-input { height: auto; min-height: 120px; padding: var(--space-3) var(--space-4); resize: vertical; }
  select.bp-input {
    appearance: none; padding-right: var(--space-7);
    background-image: linear-gradient(45deg, transparent 50%, var(--ink-muted) 50%), linear-gradient(135deg, var(--ink-muted) 50%, transparent 50%);
    background-position: right 22px center, right 16px center;
    background-size: 6px 6px; background-repeat: no-repeat;
  }
  input[type="file"].bp-input { height: auto; padding: var(--space-3); font-size: 15px; }
  /* The native picker button, styled like a compact button. */
  input[type="file"].bp-input::file-selector-button {
    margin-right: var(--space-3); padding: 8px 16px; border: 1.5px solid var(--line-control); border-radius: var(--radius-pill);
    background: transparent; color: var(--ink); font: 600 14px/1 var(--font-sans); cursor: pointer;
  }
  .bp-check { width: 20px; height: 20px; accent-color: var(--forest); }
  /* Box, then label; help text and errors sit under the label. */
  .bp-field-check { grid-template-columns: auto 1fr; align-items: center; column-gap: var(--space-3); row-gap: var(--space-1); }
  .bp-field-check .bp-check { grid-column: 1; grid-row: 1; }
  .bp-field-check .bp-label { grid-column: 2; grid-row: 1; font-weight: 500; }
  .bp-field-check :is(.bp-hint, .bp-error) { grid-column: 2; }
  .fieldset-box { display: grid; gap: var(--space-5); margin: 0; padding: var(--space-5); border: 1px solid var(--line); border-radius: var(--radius-md); }
  .fieldset-box legend { padding: 0 var(--space-2); }
  .disclosure summary { cursor: pointer; font-weight: 600; font-size: 14px; }
  .disclosure[open] summary { margin-bottom: var(--space-4); }

  /* Segmented control: a radio group shown as joined pills. */
  .segmented { display: grid; grid-template-columns: repeat(2, max-content); row-gap: var(--space-2); border: 0; margin: 0; padding: 0; }
  .segmented legend { grid-column: 1 / -1; margin-bottom: var(--space-2); }
  .segmented-option { display: inline-flex; position: relative; }
  .segmented-option span {
    padding: 10px 22px; border: 1.5px solid var(--line-control); cursor: pointer;
    font-weight: 600; font-size: 15px; color: var(--ink-muted); background: var(--paper-raised);
  }
  .segmented-option:first-of-type span { border-radius: var(--radius-pill) 0 0 var(--radius-pill); }
  .segmented-option:last-of-type span { border-radius: 0 var(--radius-pill) var(--radius-pill) 0; margin-left: -1.5px; }
  .segmented-option input { position: absolute; opacity: 0; width: 1px; height: 1px; }
  .segmented-option input:checked + span { background: var(--ink); border-color: var(--ink); color: var(--ink-inverse); }
  .segmented-option input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 3px; }

  /* ---------- Tables (c-table) ---------- */
  .bp-table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius-md); background: var(--paper-raised); }
  .bp-table { width: 100%; border-collapse: collapse; font-size: 15px; }
  .bp-table caption { padding: var(--space-4) var(--space-5) 0; text-align: left; font-weight: 600; }
  .bp-table th {
    text-align: left; padding: var(--space-3) var(--space-5);
    font: 500 12px/16px var(--font-mono); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-muted);
    border-bottom: 1px solid var(--line); white-space: nowrap;
  }
  .bp-table td { padding: var(--space-4) var(--space-5); border-bottom: 1px solid var(--line); vertical-align: middle; }
  .bp-table tbody tr:last-child td { border-bottom: 0; }
  .bp-table tbody tr:hover { background: var(--paper-subtle); }
  .bp-table .num { text-align: right; font-variant-numeric: tabular-nums; }
  .bp-table code { font-family: var(--font-mono); font-size: .9em; }
  .bp-table .nowrap { white-space: nowrap; }
  /* A second, quieter line in a cell (e.g. a description under a name). */
  .cell-detail { display: block; margin-top: 2px; color: var(--ink-muted); font-size: 14px; }
  .cell-muted { color: var(--ink-muted); }

  /* ---------- Dialog (c-modal) ---------- */
  .bp-modal {
    width: min(560px, calc(100vw - 2 * var(--space-4)));
    max-height: calc(100dvh - 2 * var(--space-6));
    padding: 0; border: 1px solid var(--line); border-radius: var(--radius-lg);
    background: var(--paper-raised); color: var(--ink); box-shadow: var(--shadow-float);
    opacity: 0; transform: translateY(12px);
    transition: opacity var(--bp-dur-quick) var(--bp-ease-tide), transform var(--bp-dur-quick) var(--bp-ease-tide),
                overlay var(--bp-dur-quick) allow-discrete, display var(--bp-dur-quick) allow-discrete;
  }
  .bp-modal[open] { opacity: 1; transform: none; }
  @starting-style { .bp-modal[open] { opacity: 0; transform: translateY(12px); } }
  .bp-modal::backdrop { background: rgb(15 20 17 / .5); }
  .bp-modal-header { display: flex; align-items: start; justify-content: space-between; gap: var(--space-4); padding: var(--space-5) var(--space-5) 0; }
  .bp-modal-title { font-size: var(--fluid-heading-2); line-height: 1.15; font-weight: 600; }
  .bp-modal-body { padding: var(--space-4) var(--space-5); color: var(--ink-muted); }
  .bp-modal-footer { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: var(--space-3); padding: 0 var(--space-5) var(--space-5); }
  .bp-modal-footer form { margin: 0; }

  /* ---------- Menu (c-menu) ---------- */
  .bp-menu {
    inset: auto; margin: 0; min-width: 220px; padding: var(--space-2);
    border: 1px solid var(--line); border-radius: var(--radius-md);
    background: var(--paper-raised); color: var(--ink); box-shadow: var(--shadow-float);
    opacity: 0; transform: translateY(-4px);
    transition: opacity var(--bp-dur-quick) var(--bp-ease-tide), transform var(--bp-dur-quick) var(--bp-ease-tide),
                overlay var(--bp-dur-quick) allow-discrete, display var(--bp-dur-quick) allow-discrete;
  }
  .bp-menu:popover-open { opacity: 1; transform: none; }
  @starting-style { .bp-menu:popover-open { opacity: 0; transform: translateY(-4px); } }
  .bp-menu-label { padding: var(--space-2) var(--space-3); }
  .bp-menu-item {
    display: flex; align-items: center; gap: var(--space-3); width: 100%;
    padding: var(--space-2) var(--space-3); border: 0; border-radius: var(--radius-sm);
    background: transparent; color: var(--ink); font-size: 14px; font-weight: 500; text-align: left;
    text-decoration: none; cursor: pointer;
  }
  .bp-menu-item:hover, .bp-menu-item:focus-visible { background: var(--paper-subtle); }
  .bp-menu-item .icon { width: 18px; height: 18px; }
  .bp-menu-item[aria-pressed="true"]::after { content: "✓"; margin-left: auto; color: var(--seaglass-text); font-weight: 700; }
  .bp-menu-sep { height: 1px; margin: var(--space-2) 0; background: var(--line); border: 0; }
  .bp-menu form { margin: 0; }

  /* ---------- Alerts (c-alert): inline, in-page notices ---------- */
  .bp-alert {
    display: flex; gap: var(--space-3); align-items: start;
    padding: var(--space-4) var(--space-5); border-radius: var(--radius-md);
    border: 1px solid var(--line); background: var(--paper-raised); font-size: 15px;
  }
  .bp-alert strong { font-weight: 600; }
  .bp-alert-success { background: var(--success-soft); border-color: transparent; color: var(--success); }
  .bp-alert-warning { background: var(--warning-soft); border-color: transparent; color: var(--warning); }
  .bp-alert-danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
  .bp-alert-info { background: var(--seaglass-soft); border-color: transparent; color: var(--seaglass-text); }

  /* ---------- Toasts (core/_toasts.html): Django messages ---------- */
  .toasts {
    position: fixed; z-index: 50; right: var(--space-5); bottom: var(--space-5);
    display: grid; gap: var(--space-2); justify-items: end;
    width: min(380px, calc(100vw - 2 * var(--space-4)));
    pointer-events: none;
  }
  @media (max-width: 599px) { .toasts { right: var(--space-4); bottom: var(--space-4); } }
  .toast {
    --toast-tone: var(--seaglass-text);
    --toast-life: 5s;
    position: relative; overflow: hidden; pointer-events: auto;
    display: flex; align-items: center; gap: var(--space-3); width: 100%;
    padding: var(--space-3) var(--space-2) var(--space-3) var(--space-4);
    border: 1px solid var(--line); border-radius: var(--radius-md);
    background: var(--paper-raised); color: var(--ink);
    box-shadow: 0 12px 32px -16px rgb(15 20 17 / .25);
    font-size: 14px; line-height: 20px;
    transition: opacity var(--bp-dur-quick) var(--bp-ease-tide), transform var(--bp-dur-tide) var(--bp-ease-tide);
  }
  @starting-style { .toast { opacity: 0; transform: translateY(12px); } }
  .toast.is-leaving { opacity: 0; transform: translateX(16px); }
  .toast-success { --toast-tone: var(--success); }
  .toast-warning { --toast-tone: var(--warning); }
  .toast-danger { --toast-tone: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, var(--line)); }
  .toast-dot { width: 8px; height: 8px; flex: none; border-radius: 50%; background: var(--toast-tone); }
  .toast-text { flex: 1; min-width: 0; }
  /* The tide line drains over the toast's life; its animationend dismisses the toast. */
  .toast-tide {
    position: absolute; left: 0; right: 0; bottom: 0; height: 2px;
    background: var(--toast-tone); opacity: .55; transform-origin: left;
    animation: toast-ebb var(--toast-life) linear forwards;
  }
  .toast:hover .toast-tide, .toast:focus-within .toast-tide { animation-play-state: paused; }
  @keyframes toast-ebb { from { transform: scaleX(1); } to { transform: scaleX(0); } }
  @media (prefers-reduced-motion: reduce) {
    .toast { transition: none; }
    .toast-tide { opacity: 0; } /* still times the dismissal, just not shown */
  }

  /* ---------- Item list: rows with an icon, text and actions (resources, issues) ---------- */
  .item-list { display: grid; margin: 0; padding: 0; list-style: none; border: 1px solid var(--line); border-radius: var(--radius-md); background: var(--paper-raised); }
  .item { display: flex; align-items: start; gap: var(--space-4); padding: var(--space-4) var(--space-5); border-bottom: 1px solid var(--line); }
  .item:last-child { border-bottom: 0; }
  .item-icon { display: inline-grid; place-items: center; width: 40px; height: 40px; flex: none; border-radius: var(--radius-sm); background: var(--paper-subtle); color: var(--ink-muted); }
  .item-main { flex: 1; min-width: 0; display: grid; gap: var(--space-1); }
  .item-title { font-weight: 600; color: var(--ink); text-decoration: none; overflow-wrap: anywhere; }
  .item-title:hover { text-decoration: underline; }
  .item-desc { color: var(--ink-muted); font-size: 15px; }
  .item-meta { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2) var(--space-3); color: var(--ink-muted); font-size: 13px; }
  .item-meta .mono { overflow-wrap: anywhere; }
  .item-actions { display: flex; align-items: center; gap: var(--space-2); flex: none; }
  @media (max-width: 599px) {
    .item { flex-wrap: wrap; padding: var(--space-4); }
    .item-actions { width: 100%; justify-content: flex-end; }
  }

  /* ---------- Prose: rendered Markdown (issue bodies) ---------- */
  .prose { display: grid; gap: var(--space-4); font-size: 16px; line-height: 1.65; overflow-wrap: anywhere; }
  .prose :is(h1, h2, h3, h4) { font-size: 1.15em; line-height: 1.3; letter-spacing: -.01em; }
  .prose :is(ul, ol) { margin: 0; padding-left: 1.4em; display: grid; gap: var(--space-1); }
  .prose a { color: var(--seaglass-text); }
  .prose code { font-family: var(--font-mono); font-size: .88em; padding: .1em .35em; border-radius: var(--radius-sm); background: var(--paper-subtle); }
  .prose pre { margin: 0; padding: var(--space-4); overflow-x: auto; border-radius: var(--radius-md); background: var(--paper-subtle); }
  .prose pre code { padding: 0; background: none; }
  .prose blockquote { margin: 0; padding-left: var(--space-4); border-left: 3px solid var(--line-control); color: var(--ink-muted); }
  .prose hr { border: 0; height: 1px; background: var(--line); width: 100%; }
  .prose table { border-collapse: collapse; }
  .prose :is(th, td) { padding: var(--space-2) var(--space-3); border: 1px solid var(--line); text-align: left; }
  .panel { padding: var(--space-6); border: 1px solid var(--line); border-radius: var(--radius-md); background: var(--paper-raised); }

  /* ---------- Empty state (c-empty-state) ---------- */
  .bp-empty {
    display: grid; justify-items: center; gap: var(--space-4); text-align: center;
    padding: var(--space-8) var(--space-5);
    border: 1.5px dashed var(--line-control); border-radius: var(--radius-lg);
  }
  .bp-empty-mark { width: 48px; opacity: .5; }
  .bp-empty-title { font-size: var(--fluid-heading-2); font-weight: 600; }
  .bp-empty-body { color: var(--ink-muted); max-width: 48ch; }

  /* ---------- Tabs and filter chips ---------- */
  .tabs { display: flex; gap: var(--space-1); border-bottom: 1px solid var(--line); overflow-x: auto; }
  .tab {
    padding: var(--space-3) var(--space-4); margin-bottom: -1px; white-space: nowrap;
    color: var(--ink-muted); font-weight: 500; font-size: 15px; text-decoration: none;
    border-bottom: 2px solid transparent;
  }
  .tab:hover { color: var(--ink); }
  .tab[aria-current="page"] { color: var(--ink); font-weight: 600; border-bottom-color: var(--forest); }
  [data-theme="dark"] .tab[aria-current="page"] { border-bottom-color: var(--seaglass-text); }
  @media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .tab[aria-current="page"] { border-bottom-color: var(--seaglass-text); } }

  .filter-chips { display: flex; flex-wrap: wrap; gap: var(--space-2); }
  .chip {
    padding: 6px 14px; border-radius: var(--radius-pill); border: 1px solid var(--line-control);
    color: var(--ink); font-size: 14px; font-weight: 500; text-decoration: none;
  }
  .chip:hover { border-color: var(--line-strong); }
  .chip[aria-current="true"] { background: var(--ink); color: var(--ink-inverse); border-color: var(--ink); }

  /* ---------- Checkbox list: pick several items, one per row ---------- */
  .check-list { display: grid; gap: var(--space-2); margin: 0; padding: 0; border: 0; }
  .check-list-item {
    display: flex; align-items: center; gap: var(--space-3); padding: var(--space-3) var(--space-4);
    border: 1px solid var(--line); border-radius: var(--radius-md); background: var(--paper-raised); cursor: pointer;
  }
  .check-list-item:has(:checked) { border-color: var(--forest); }

  /* ---------- Money totals under a list of lines (invoices) ---------- */
  .invoice-totals { display: grid; gap: var(--space-2); justify-items: end; }
  .invoice-totals dl { display: grid; gap: var(--space-2); min-width: min(100%, 320px); margin: 0; }
  .invoice-totals dl div { display: flex; justify-content: space-between; gap: var(--space-5); font-variant-numeric: tabular-nums; }
  .invoice-totals dd { margin: 0; }
  .invoice-totals-total { padding-top: var(--space-2); border-top: 1px solid var(--line-strong); font-weight: 700; font-size: 18px; }
  .invoice-totals .bp-hint { max-width: 48ch; text-align: right; }

  /* ---------- Pagination (c-pagination) ---------- */
  .pagination { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: var(--space-4); }
  .pagination-status { color: var(--ink-muted); font-size: 14px; }

  /* ---------- List summary: a count and totals above a table ---------- */
  .list-summary { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--space-2) var(--space-4); color: var(--ink-muted); }
  .list-summary-total { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; }

  /* ---------- Loader (brand TideLoader) ---------- */
  /* The guide staggers dots with inline --i; our CSP forbids inline styles. */
  .bp-tl-dots { --bp-tl: 8px; }
  .bp-tl-dots span:nth-child(2) { animation-delay: 120ms; }
  .bp-tl-dots span:nth-child(3) { animation-delay: 240ms; }

  /* ---------- Connect sub-brand (docs/brand/connect.md) ---------- */
  .app-icon { display: block; flex: none; border-radius: 22.5%; }
  .app-icon-dark { display: none; outline: 1px solid var(--line); outline-offset: -1px; }
  [data-theme="dark"] .app-icon-light { display: none; }
  [data-theme="dark"] .app-icon-dark { display: block; }
  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .app-icon-light { display: none; }
    :root:not([data-theme="light"]) .app-icon-dark { display: block; }
  }
  .connect-lockup { display: inline-flex; align-items: center; gap: var(--space-3); color: var(--ink); text-decoration: none; }
  .connect-lockup-name { font: 600 21px/1 var(--font-poster); letter-spacing: -.02em; }
  .connect-lockup-lg { gap: var(--space-4); }
  .connect-lockup-lg .connect-lockup-name { font-size: 30px; }
  .tidelines { display: block; width: 100%; height: var(--tidelines-height, clamp(64px, 3rem + 6vw, 140px)); }
  .tidelines path { vector-effect: non-scaling-stroke; }
  .tidelines-lead { stroke: var(--seaglass); stroke-width: 3px; }
  .tidelines-follow { stroke: currentColor; stroke-width: 1.5px; opacity: .35; }
  .tidelines-trail { stroke: currentColor; stroke-width: 1px; opacity: .2; }

} /* @layer components */
