/* Layer: components | ui.css
 * Responsibility: Shared native UI primitives (topbar, badges, snapshot metadata, action buttons, empty states, pagination, contextual open notes).
 * Loaded: Globally via templates/layouts/base.html on every page.
 * Limit: Hard < 700 lines.
 */

@layer components {
  /* ========================================================================
   * Topbar: Brand, Version Pill, Status Links & Theme Toggle
   * ======================================================================== */

  .app-brand-group {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    min-width: 0;
  }

  .app-brand {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    color: var(--text-primary);
    font-size: var(--text-md);
    font-weight: 650;
    letter-spacing: -0.02em;

    &:hover {
      color: var(--text-primary);
    }
  }

  .app-brand-mark {
    width: 24px;
    height: 24px;
    display: inline-grid;
    place-items: center;
    border-radius: var(--radius-sm);
    background: var(--accent-soft);
    border: 1px solid var(--accent-border);
    color: var(--accent);
  }

  .app-version {
    display: inline-flex;
    align-items: center;
    padding: 0.125rem 0.45rem;
    border-radius: var(--radius-pill);
    background: var(--bg-inset);
    border: 1px solid var(--border-default);
    color: var(--text-muted);
    font-family: var(--font-mono);
    font-size: var(--text-2xs);
    font-weight: 500;
    line-height: 1.3;
  }

  .app-header-actions {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
  }

  .header-link {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.375rem 0.625rem;
    border-radius: var(--radius-sm);
    font-size: var(--text-xs);
    font-weight: 500;
    color: var(--text-secondary);
    border: 1px solid transparent;

    &:hover {
      color: var(--text-primary);
      background: var(--bg-surface);
      border-color: var(--border-subtle);
    }
  }

  /* Progressive theme toggle — revealed once theme.js hydrates */
  .theme-toggle {
    display: none;
    align-items: center;
    gap: 0.4rem;
    padding: 0.375rem 0.675rem;
    border-radius: var(--radius-sm);
    background: var(--bg-surface);
    border: 1px solid var(--border-default);
    color: var(--text-secondary);
    font-size: var(--text-xs);
    font-weight: 500;
    line-height: 1;
    box-shadow: var(--shadow-xs);
    transition:
      background-color var(--transition-fast),
      border-color var(--transition-fast),
      color var(--transition-fast);

    &:hover {
      background: var(--bg-surface-hover);
      border-color: var(--border-strong);
      color: var(--text-primary);
    }
  }

  :root[data-jeloxy-theme-ready="true"] .theme-toggle {
    display: inline-flex;
  }

  .theme-toggle-icon {
    width: 14px;
    height: 14px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Dark mode shows sun icon (action: switch to light); Light mode shows moon icon */
  .theme-toggle-icon--moon {
    display: none;
  }

  :root[data-theme="light"] .theme-toggle-icon--sun {
    display: none;
  }

  :root[data-theme="light"] .theme-toggle-icon--moon {
    display: inline-flex;
  }

  @media (prefers-color-scheme: light) {
    :root:not([data-theme="dark"]) .theme-toggle-icon--sun {
      display: none;
    }

    :root:not([data-theme="dark"]) .theme-toggle-icon--moon {
      display: inline-flex;
    }
  }

  /* ========================================================================
   * Badges, Slugs & Shared Lox Snapshot Primitives
   * ======================================================================== */

  .repo-slug {
    display: inline-flex;
    align-items: center;
    width: fit-content;
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--text-muted);
  }

  .badge {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.175rem 0.5rem;
    border-radius: var(--radius-pill);
    border: 1px solid var(--border-default);
    font-family: var(--font-mono);
    font-size: var(--text-2xs);
    font-weight: 600;
    letter-spacing: 0.02em;
    line-height: 1.35;
    white-space: nowrap;
  }

  .badge-dot {
    width: 6px;
    height: 6px;
    border-radius: var(--radius-pill);
    background: currentColor;
  }

  .badge-verified[data-state="verified"] {
    color: var(--status-verified-fg);
    background: var(--status-verified-bg);
    border-color: var(--status-verified-border);

    & .badge-dot {
      background: var(--status-verified-dot);
    }
  }

  .badge-verified[data-state="failed"] {
    color: var(--status-failed-fg);
    background: var(--status-failed-bg);
    border-color: var(--status-failed-border);

    & .badge-dot {
      background: var(--status-failed-dot);
    }
  }

  .badge-verified[data-state="unverified"] {
    color: var(--status-unverified-fg);
    background: var(--status-unverified-bg);
    border-color: var(--status-unverified-border);

    & .badge-dot {
      background: var(--status-unverified-dot);
    }
  }

  .badge-status[data-state="unavailable"],
  .badge[data-state="archived"] {
    color: var(--status-unavailable-fg);
    background: var(--status-unavailable-bg);
    border-color: var(--status-unavailable-border);

    & .badge-dot {
      background: var(--status-unavailable-dot);
    }
  }

  .snapshot-id {
    padding: 0.1rem 0.4rem;
    border-radius: var(--radius-xs);
    background: var(--accent-soft);
    border: 1px solid var(--accent-border);
    color: var(--accent);
    font-size: var(--text-xs);
    font-weight: 600;
  }

  .snapshot-kind {
    padding: 0.1rem 0.4rem;
    border-radius: var(--radius-xs);
    background: var(--bg-surface);
    border: 1px solid var(--border-subtle);
    color: var(--text-secondary);
    font-family: var(--font-mono);
    font-size: var(--text-2xs);
  }

  .snapshot-author {
    font-size: var(--text-2xs);
    color: var(--text-muted);
  }

  .snapshot-time {
    font-family: var(--font-mono);
    font-size: var(--text-2xs);
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
  }

  .snapshot-message {
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-primary);
    line-height: var(--leading-snug);
    overflow-wrap: anywhere;
  }

  .snapshot-intent {
    display: flex;
    align-items: baseline;
    gap: 0.45rem;
    padding-top: var(--space-2);
    border-top: 1px solid var(--border-subtle);
    font-size: var(--text-xs);
    color: var(--text-secondary);
    line-height: var(--leading-snug);
  }

  .snapshot-intent-flag {
    flex-shrink: 0;
    font-family: var(--font-mono);
    font-size: var(--text-2xs);
    font-weight: 600;
    color: var(--accent);
  }

  /* ========================================================================
   * Action Links, Empty State & Pagination
   * ======================================================================== */

  .card-action-group {
    display: inline-flex;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--space-2);
  }

  .card-action-link {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.325rem 0.65rem;
    border-radius: var(--radius-sm);
    background: var(--bg-inset);
    border: 1px solid var(--border-default);
    color: var(--text-secondary);
    font-size: var(--text-xs);
    font-weight: 500;

    &:hover {
      color: var(--text-primary);
      border-color: var(--border-strong);
      background: var(--bg-subtle);
    }

    &.card-action-link--primary {
      color: var(--accent);
      background: var(--accent-soft);
      border-color: var(--accent-border);

      &:hover {
        color: var(--accent-hover);
        border-color: var(--border-accent);
      }
    }
  }

  .empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: var(--space-3);
    padding: var(--space-12) var(--space-6);
    border-radius: var(--radius-lg);
    background: var(--bg-surface);
    border: 1px dashed var(--border-strong);
  }

  .empty-state-icon {
    width: 44px;
    height: 44px;
    display: grid;
    place-items: center;
    border-radius: var(--radius-md);
    background: var(--bg-inset);
    border: 1px solid var(--border-default);
    color: var(--text-muted);
  }

  .empty-state-title {
    font-size: var(--text-lg);
    font-weight: 600;
    color: var(--text-primary);
  }

  .empty-state-copy {
    max-width: 46ch;
    font-size: var(--text-sm);
    color: var(--text-secondary);

    & code {
      padding: 0.1rem 0.35rem;
      border-radius: var(--radius-xs);
      background: var(--bg-inset);
      border: 1px solid var(--border-subtle);
      color: var(--text-primary);
      font-size: var(--text-xs);
    }
  }

  .empty-state-snippet {
    width: min(100%, 380px);
    text-align: left;
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    background: var(--bg-inset);
    border: 1px solid var(--border-subtle);
    color: var(--text-secondary);
    font-size: var(--text-xs);
    line-height: 1.6;
    overflow-x: auto;
  }

  .pagination-notice {
    font-size: var(--text-xs);
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
  }

  .pagination-controls {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
  }

  .pagination-link {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.35rem 0.7rem;
    border-radius: var(--radius-sm);
    background: var(--bg-surface);
    border: 1px solid var(--border-default);
    color: var(--text-primary);
    font-size: var(--text-xs);
    font-weight: 500;

    &:hover {
      background: var(--bg-surface-hover);
      border-color: var(--border-strong);
      color: var(--text-primary);
    }
  }

  .pagination-meta {
    font-family: var(--font-mono);
    font-size: var(--text-2xs);
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
  }

  /* ========================================================================
   * Contextual Open lox notes (/blob/, /snap/, /compare/ diff headers)
   * ======================================================================== */

  .related-notes-panel {
    display: flex;
    flex-direction: column;
    border-radius: var(--radius-md);
    background: var(--bg-surface);
    border: 1px solid var(--border-default);
    box-shadow: var(--shadow-xs);
    overflow: hidden;
  }

  .related-notes-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    padding: 0.5rem var(--space-4);
    background: var(--bg-subtle);
    border-bottom: 1px solid var(--border-subtle);

    & h2 {
      font-size: var(--text-xs);
      font-weight: 600;
      text-transform: uppercase;
      letter-spacing: 0.04em;
      color: var(--text-secondary);
    }
  }

  .related-notes-panel--diff {
    border-radius: 0;
    border: 0;
    border-bottom: 1px solid var(--border-subtle);
    box-shadow: none;
    background: var(--bg-subtle);
  }

  .related-notes-list {
    display: flex;
    flex-direction: column;
  }

  .related-note-item {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    padding: 0.55rem var(--space-4);
    border-bottom: 1px solid var(--border-subtle);
    border-left: 3px solid var(--note-neutral-fg);
    background: var(--bg-surface);

    &:last-child { border-bottom: 0; }
    &[data-kind="bug"] {
      border-left-color: var(--note-bug-fg);
      background: color-mix(in srgb, var(--note-bug-bg) 58%, var(--bg-surface));
    }
    &[data-kind="dead_end"] {
      border-left-color: var(--note-dead-end-fg);
      background: color-mix(in srgb, var(--note-dead-end-bg) 55%, var(--bg-surface));
    }
    &[data-kind="decision"] {
      border-left-color: var(--note-decision-fg);
      background: color-mix(in srgb, var(--note-decision-bg) 32%, var(--bg-surface));
    }
    &[data-kind="todo"] {
      border-left-color: var(--note-todo-fg);
      background: color-mix(in srgb, var(--note-todo-bg) 32%, var(--bg-surface));
    }
    &[data-kind="note"] { border-left-color: var(--note-neutral-fg); }
  }

  .related-notes-panel--diff .related-note-item {
    padding: 0.45rem var(--space-4);
  }

  .related-note-head {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.45rem;
    font-size: var(--text-xs);
  }

  .related-note-title {
    color: var(--text-primary);
    font-weight: 600;
    overflow-wrap: anywhere;
  }

  .related-note-id {
    padding: 0.06rem 0.35rem;
    border-radius: var(--radius-xs);
    background: var(--bg-inset);
    border: 1px solid var(--border-subtle);
    color: var(--text-muted);
    font-size: var(--text-2xs);
  }

  .related-note-kind {
    display: inline-flex;
    align-items: center;
    padding: 0.1rem 0.45rem;
    border-radius: var(--radius-pill);
    border: 1px solid var(--border-default);
    font-family: var(--font-mono);
    font-size: var(--text-2xs);
    font-weight: 600;
    line-height: 1.3;
    white-space: nowrap;

    &[data-kind="decision"] { color: var(--note-decision-fg); background: var(--note-decision-bg); border-color: var(--note-decision-border); }
    &[data-kind="dead_end"] { color: var(--note-dead-end-fg); background: var(--note-dead-end-bg); border-color: var(--note-dead-end-border); }
    &[data-kind="bug"] { color: var(--note-bug-fg); background: var(--note-bug-bg); border-color: var(--note-bug-border); }
    &[data-kind="todo"] { color: var(--note-todo-fg); background: var(--note-todo-bg); border-color: var(--note-todo-border); }
    &[data-kind="note"] { color: var(--note-neutral-fg); background: var(--note-neutral-bg); border-color: var(--note-neutral-border); }
  }

  .related-note-preview {
    font-size: var(--text-xs);
    color: var(--text-secondary);
    line-height: var(--leading-snug);
    overflow-wrap: anywhere;
  }

  .related-note-meta {
    display: inline-flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.55rem;
    font-size: var(--text-2xs);
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;

    & time,
    & code {
      font-family: var(--font-mono);
      font-size: var(--text-2xs);
    }

    & a {
      color: var(--accent);
      font-weight: 500;

      &:hover {
        color: var(--accent-hover);
      }
    }
  }

  @media (max-width: 640px) {
    .app-header-actions {
      flex-wrap: wrap;
      gap: var(--space-1);
    }

    .app-brand,
    .header-link,
    .theme-toggle,
    .card-action-link,
    .pagination-link {
      min-height: 2.5rem;
    }

    .pagination-controls {
      flex-wrap: wrap;
    }

    .empty-state {
      padding: var(--space-8) var(--space-4);
    }

    .related-note-item {
      padding: 0.5rem var(--space-3);
    }
  }
}
