@import "./theme.css";
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  /* Legacy tokens for existing pages — bridged from theme.css canonical values.
     These keep legacy Tailwind classnames (bg-surface-nav, border-edge, etc.)
     rendering correctly while the new shell uses the canonical theme tokens. */
  :root {
    /* Surface aliases */
    --surface-nav: var(--surface-raised);
    --surface-input: color-mix(in srgb, var(--surface-overlay) 60%, var(--surface-raised) 40%);
    --surface-input-hover: var(--surface-overlay);

    /* Shadow */
    --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px 2px rgba(0, 0, 0, 0.2);
    --shadow-card-hover: 0 2px 8px rgba(0, 0, 0, 0.4), 0 1px 4px rgba(0, 0, 0, 0.25);
    --shadow-header: 0 1px 0 var(--edge-hairline);

    /* Content alias */
    --content-inverse: #ffffff;

    /* Edge aliases — map legacy names to canonical theme tokens */
    --edge-default: var(--edge-hairline);
    --edge-subtle: var(--edge-hairline);

    /* Interactive */
    --interactive-primary: var(--accent);
    --interactive-primary-hover: #d4922e;
    --interactive-secondary: var(--surface-overlay);
    --interactive-secondary-hover: var(--surface-raised);
    --interactive-focus: rgba(232, 163, 61, 0.22);

    /* Status bg variants (not in canonical theme — kept for legacy pages) */
    --status-success-bg: rgba(79, 180, 119, 0.1);
    --status-running-bg: rgba(91, 168, 245, 0.1);
    --status-error: var(--status-failure);
    --status-error-bg: rgba(229, 96, 76, 0.1);
    --status-warning: var(--status-pending);
    --status-warning-bg: rgba(232, 163, 61, 0.1);
    --status-selected: #a78bfa;
    --status-selected-bg: rgba(167, 139, 250, 0.1);

    /* Role colors */
    --role-researcher: #4fb477;
    --role-implementer: #a78bfa;
    --role-reviewer: #14b8a6;
    --role-critic: var(--accent);
    --role-analyst: #5ba8f5;
    --role-architect: #f472b6;
    --role-tester: #22d3ee;

    /* DAG SVG palette */
    --dag-pending-bg: #1c1c1e;
    --dag-pending-border: #38383f;
    --dag-pending-label: #62626e;
    --dag-pending-dot: #38383f;
    --dag-running-bg: #0d1a2d;
    --dag-running-border: #5ba8f5;
    --dag-running-label: #93c5fd;
    --dag-running-dot: #5ba8f5;
    --dag-completed-bg: #0c1e14;
    --dag-completed-border: #4fb477;
    --dag-completed-label: #86efac;
    --dag-completed-dot: #4fb477;
    --dag-failed-bg: #200f0f;
    --dag-failed-border: #e5604c;
    --dag-failed-label: #fca5a5;
    --dag-failed-dot: #e5604c;
    --dag-edge-pending: var(--edge-hairline);
    --dag-edge-done: #4fb477;
    --dag-hover-border: #62626e;
    --dag-assign-text: #848484;
  }

  /* Light theme — legacy aliases inherit from theme.css [data-theme="light"] */
  [data-theme="light"] {
    --surface-nav: var(--surface-raised);
    --surface-input: var(--surface-overlay);
    --surface-input-hover: #e5e5e3;

    --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-card-hover: 0 2px 6px rgba(0, 0, 0, 0.08), 0 1px 3px rgba(0, 0, 0, 0.05);
    --shadow-header: 0 1px 0 var(--edge-hairline);

    --interactive-primary: var(--accent);
    --interactive-primary-hover: #d4922e;
    --interactive-secondary: var(--surface-overlay);
    --interactive-secondary-hover: var(--surface-raised);

    --role-researcher: #16a34a;
    --role-implementer: #7c3aed;
    --role-reviewer: #0d9488;
    --role-critic: #c27f10;
    --role-analyst: #2563eb;
    --role-architect: #db2777;
    --role-tester: #0891b2;

    --dag-pending-bg: #f5f5f4;
    --dag-pending-border: #d4d4d4;
    --dag-pending-label: #737373;
    --dag-pending-dot: #d4d4d4;
    --dag-running-bg: #eff6ff;
    --dag-running-border: #2563eb;
    --dag-running-label: #1d4ed8;
    --dag-running-dot: #2563eb;
    --dag-completed-bg: #f0fdf4;
    --dag-completed-border: #16a34a;
    --dag-completed-label: #15803d;
    --dag-completed-dot: #16a34a;
    --dag-failed-bg: #fef2f2;
    --dag-failed-border: #dc2626;
    --dag-failed-label: #b91c1c;
    --dag-failed-dot: #dc2626;
    --dag-edge-pending: #d4d4d4;
    --dag-edge-done: #16a34a;
    --dag-hover-border: #737373;
    --dag-assign-text: #707070;
  }

  /* Legacy .dark class — mirrors :root (dark is default now) so old
     theme-toggle code that sets/removes .dark still works correctly. */
  .dark {
    --surface-nav: var(--surface-raised);
    --surface-input: color-mix(in srgb, var(--surface-overlay) 60%, var(--surface-raised) 40%);
    --surface-input-hover: var(--surface-overlay);
    --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px 2px rgba(0, 0, 0, 0.2);
    --shadow-card-hover: 0 2px 8px rgba(0, 0, 0, 0.4), 0 1px 4px rgba(0, 0, 0, 0.25);
  }
}

/* ── Semantic typography utilities ──────────────────────────────────────────
   text-meta   → --t-xs (11px) — chips, badges, counters, timestamps
   text-body   → --t-sm (12px) — content paragraphs, tool summaries
   text-label  → --t-base (13px) — step names, prominent identifiers
   text-heading → --t-md (14px) — section headers; use sparingly
   Do NOT use text-[9px] / text-[10px] / text-[11px] / text-[12.5px] inline.
*/
@layer utilities {
  .text-meta {
    font-size: var(--t-xs);
    line-height: 1.4;
    letter-spacing: 0.01em;
  }
  .text-body {
    font-size: var(--t-sm);
    line-height: 1.55;
  }
  .text-label {
    font-size: var(--t-base);
    line-height: 1.4;
    letter-spacing: -0.005em;
  }
  .text-heading {
    font-size: var(--t-md);
    line-height: 1.3;
    letter-spacing: -0.01em;
  }

  /* Card shadow utilities */
  .shadow-card {
    box-shadow: var(--shadow-card);
  }
  .shadow-card-hover {
    box-shadow: var(--shadow-card-hover);
  }

  /* Meta-label caps — en gets uppercase + wide tracking; zh keeps case and
     nearly no tracking (wide-tracked hanzi read as broken spacing). */
  .ui-uppercase {
    text-transform: uppercase;
    letter-spacing: var(--tracking-meta);
  }
  :lang(zh) .ui-uppercase {
    text-transform: none;
  }

  /* One keyboard-focus contract. Hover may use surface-overlay;
     focus-visible always shows the accent ring. */
  .focus-ring {
    outline: none;
  }
  .focus-ring:focus-visible {
    box-shadow: var(--focus-ring);
  }

  /* Designer canvas substrate — 16px technical dot grid. Alignment
     authority, not decoration; Designer-only. The vignette glow lives in
     world space (anchored to the graph, not the container) so container
     resizes never shift it. */
  .designer-canvas {
    background: radial-gradient(var(--canvas-grid) 0.65px, transparent 0.65px), var(--surface-base);
    background-size:
      16px 16px,
      auto;
  }
  .designer-vignette {
    background: radial-gradient(circle, var(--canvas-vignette), transparent 58%);
    pointer-events: none;
  }

  /* Node inspector expands from its card — quick, no layout motion */
  @keyframes inspector-in {
    from {
      opacity: 0;
      transform: scale(0.97);
    }
    to {
      opacity: 1;
      transform: scale(1);
    }
  }
  .inspector-in {
    animation: inspector-in 120ms ease-out;
    transform-origin: top left;
  }

  /* Skeleton pulse */
  .skeleton {
    background: linear-gradient(
      90deg,
      var(--surface-overlay) 25%,
      var(--surface-input) 50%,
      var(--surface-overlay) 75%
    );
    background-size: 200% 100%;
    animation: skeleton-shimmer 1.4s ease-in-out infinite;
  }
}

@keyframes skeleton-shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* ── Page enter animation ────────────────────────────────────────────────── */
@keyframes page-enter {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
.animate-page-enter {
  animation: page-enter 0.25s ease-out both;
}

/* ── Markdown rendering ────────────────────────────────────────────────────
   Used by <Markdown /> for any rendered markdown content (show.md, task
   descriptions, etc). Tokenized so dark/light theme follows automatically.
*/
.markdown-body {
  color: var(--content-secondary);
  font-size: var(--t-base);
  line-height: 1.65;
  font-family:
    ui-sans-serif,
    system-ui,
    -apple-system,
    sans-serif;
}
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  color: var(--content-primary);
  font-weight: 600;
  margin-top: 1.5em;
  margin-bottom: 0.5em;
  line-height: 1.3;
  letter-spacing: -0.01em;
}
.markdown-body h1 {
  font-size: 1.4em;
}
.markdown-body h2 {
  font-size: 1.2em;
  border-bottom: 1px solid var(--edge-subtle);
  padding-bottom: 0.3em;
}
.markdown-body h3 {
  font-size: 1.05em;
}
.markdown-body h4 {
  font-size: 1em;
}
.markdown-body > :first-child {
  margin-top: 0;
}
.markdown-body p {
  margin: 0.65em 0;
}
.markdown-body strong {
  color: var(--content-primary);
  font-weight: 600;
}
.markdown-body em {
  font-style: italic;
  color: var(--content-secondary);
}
.markdown-body a {
  color: var(--status-running);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 0.15s ease;
}
.markdown-body a:hover {
  border-bottom-color: currentColor;
}
.markdown-body ul,
.markdown-body ol {
  margin: 0.65em 0;
  padding-left: 1.4em;
}
.markdown-body li {
  margin: 0.25em 0;
}
.markdown-body ul {
  list-style: disc;
}
.markdown-body ol {
  list-style: decimal;
}
.markdown-body code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  font-size: 0.88em;
  padding: 0.15em 0.4em;
  background: var(--surface-overlay);
  border: 1px solid var(--edge-default);
  border-radius: 4px;
  color: var(--content-primary);
}
.markdown-body pre {
  margin: 0.85em 0;
  padding: 0.8em 1em;
  background: var(--surface-overlay);
  border: 1px solid var(--edge-default);
  border-radius: 6px;
  overflow-x: auto;
  font-size: 0.85em;
  line-height: 1.55;
}
.markdown-body pre code {
  padding: 0;
  background: transparent;
  border: 0;
  border-radius: 0;
  color: var(--content-secondary);
}
.markdown-body blockquote {
  margin: 0.85em 0;
  padding: 0.5em 0.75em 0.5em 1em;
  border-left: 2px solid var(--edge-strong);
  background: var(--surface-overlay);
  border-radius: 0 4px 4px 0;
  color: var(--content-muted);
}
.markdown-body hr {
  margin: 1.5em 0;
  border: 0;
  border-top: 1px solid var(--edge-default);
}
.markdown-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.85em 0;
  font-size: 0.9em;
}
.markdown-body th,
.markdown-body td {
  border: 1px solid var(--edge-default);
  padding: 0.4em 0.75em;
  text-align: left;
}
.markdown-body th {
  background: var(--surface-overlay);
  color: var(--content-primary);
  font-weight: 600;
}

/* Thin scrollbars — webkit (Chrome/Safari) */
*::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
*::-webkit-scrollbar-track {
  background: transparent;
}
*::-webkit-scrollbar-thumb {
  background: var(--edge-default);
  border-radius: 3px;
}
*::-webkit-scrollbar-thumb:hover {
  background: var(--edge-strong);
}
*::-webkit-scrollbar-corner {
  background: transparent;
}

/* Firefox */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--edge-default) transparent;
}

/* ── Reduced-motion guard ────────────────────────────────────────────────── */
@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;
  }
}

/* ── Session message feed ──────────────────────────────────────────────────── */
@keyframes fade-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
.animate-fade-in {
  animation: fade-in 0.15s ease-out both;
}

/* ── Live pulse (running indicators) — terminal states never animate ──────── */
@keyframes live-pulse {
  0%,
  100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.35;
    transform: scale(0.75);
  }
}
.live-pulse-dot {
  animation: live-pulse 1.8s ease-in-out infinite;
}

/* Leo markdown — compact rendering for the side panel */
.leo-md p {
  margin: 0.25em 0;
}
.leo-md p:first-child {
  margin-top: 0;
}
.leo-md h1,
.leo-md h2,
.leo-md h3 {
  font-size: var(--t-base);
  font-weight: 600;
  margin: 0.5em 0 0.25em;
}
.leo-md ul,
.leo-md ol {
  margin: 0.25em 0;
  padding-left: 1.25em;
}
.leo-md li {
  margin: 0.1em 0;
}
.leo-md code {
  font-family: var(--font-data);
  font-size: 0.9em;
  background: var(--surface-overlay);
  padding: 0.1em 0.3em;
  border-radius: 3px;
}
.leo-md pre {
  background: var(--surface-base);
  border: 1px solid var(--edge-hairline);
  border-radius: 4px;
  padding: 0.5em;
  overflow-x: auto;
  margin: 0.4em 0;
  font-size: var(--t-xs);
}
.leo-md pre code {
  background: none;
  padding: 0;
}
.leo-md a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}
.leo-md blockquote {
  border-left: 2px solid var(--edge-strong);
  padding-left: 0.75em;
  margin: 0.4em 0;
  color: var(--content-secondary);
}
.leo-md table {
  border-collapse: collapse;
  font-size: var(--t-xs);
  margin: 0.4em 0;
}
.leo-md th,
.leo-md td {
  border: 1px solid var(--edge-hairline);
  padding: 0.25em 0.5em;
}
.leo-md th {
  font-weight: 600;
  background: var(--surface-overlay);
}
