@layer components {
  /* --- Visualizer layout --- */
  .viz-layout {
    margin-top: var(--space-md);
  }

  /* --- Input section --- */
  .viz-editor {
    position: relative;
    background: var(--bg-surface);
    border: 1px solid var(--border);
  }
  .viz-editor:focus-within {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
  }
  .viz-highlight {
    position: absolute;
    inset: 0;
    margin: 0;
    font-family: var(--font);
    font-size: 0.8rem;
    line-height: 1.5;
    padding: 0.75rem 1rem;
    overflow: hidden;
    white-space: pre-wrap;
    overflow-wrap: break-word;
    pointer-events: none;
    color: var(--text);
    background: transparent;
    border: none;
  }
  .viz-textarea {
    width: 100%;
    font-family: var(--font);
    font-size: 0.8rem;
    line-height: 1.5;
    background: transparent;
    border: none;
    color: transparent;
    caret-color: var(--text);
    padding: 0.75rem 1rem;
    resize: vertical;
    position: relative;
    z-index: 1;
  }
  .viz-textarea:focus {
    outline: none;
  }
  .viz-textarea::selection {
    background: var(--accent-dim);
  }
  .viz-textarea::placeholder {
    color: var(--text-dim);
  }

  .viz-buttons {
    display: flex;
    gap: var(--space-sm);
    margin: 0.75rem 0;
  }

  .viz-btn {
    font-family: var(--font);
    font-size: 0.78rem;
    letter-spacing: 0.05em;
    padding: 0.4rem 0.9rem;
    border: 1px solid var(--border-strong);
    background: var(--bg-surface);
    color: var(--text);
    cursor: pointer;
  }
  .viz-btn:hover {
    border-color: var(--accent);
    color: var(--accent);
  }
  .viz-btn-primary {
    background: transparent;
    color: var(--accent);
    border-color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 0.1em;
    font-weight: 600;
  }
  .viz-btn-primary:hover {
    background: var(--accent);
    color: var(--bg);
  }
  .viz-btn-file {
    cursor: pointer;
  }

  .viz-error {
    font-family: var(--font);
    font-size: 0.8rem;
    color: var(--alert);
    background: color-mix(in srgb, var(--alert) 10%, transparent);
    border: 1px solid color-mix(in srgb, var(--alert) 40%, transparent);
    padding: 0.5rem 0.75rem;
    margin: 0.5rem 0;
  }

  /* --- Controls bar --- */
  .viz-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    margin-bottom: 0.5rem;
    border-bottom: 1px solid var(--border);
  }
  .viz-controls-left,
  .viz-controls-right {
    display: flex;
    gap: var(--space-sm);
    align-items: center;
  }
  .viz-ctrl {
    font-family: var(--font);
    font-size: 0.8rem;
    padding: 0.25rem 0.6rem;
    border: 1px solid var(--border-strong);
    background: var(--bg-surface);
    color: var(--text);
    cursor: pointer;
    line-height: 1;
  }
  .viz-ctrl:hover {
    border-color: var(--accent);
    color: var(--accent);
  }
  .viz-toggle {
    font-family: var(--font);
    font-size: 0.72rem;
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.3rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
  }
  .viz-toggle input {
    accent-color: var(--accent);
  }

  /* --- Canvas --- */
  .viz-canvas-wrap {
    position: relative;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    height: 60vh;
    min-height: 300px;
    overflow: hidden;
  }
  #viz-canvas {
    width: 100%;
    height: 100%;
    display: block;
  }

  /* dagre-d3 node styling */
  #viz-canvas .node rect,
  #viz-canvas .node ellipse {
    rx: 0;
    ry: 0;
  }
  #viz-canvas .node text,
  #viz-canvas .cluster text {
    font-family: var(--font);
    font-size: 11px;
    fill: var(--text);
  }
  #viz-canvas .edgePath path.path {
    fill: none;
  }
  #viz-canvas .edgePath defs marker path {
    fill: var(--text);
  }

  /* --- Tooltip --- */
  .viz-tooltip {
    position: absolute;
    pointer-events: none;
    background: var(--bg-elevated);
    border: 1px solid var(--border-strong);
    padding: 0.6rem 0.8rem;
    font-family: var(--font);
    font-size: 0.75rem;
    line-height: 1.5;
    color: var(--text);
    max-width: 340px;
    z-index: 20;
  }
  .viz-tooltip .tt-label {
    color: var(--text-dim);
    font-size: 0.62rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    margin-top: 0.4rem;
  }
  .viz-tooltip .tt-intent {
    color: var(--text-secondary);
  }
  .viz-tooltip .tt-dead {
    color: var(--alert);
  }

  /* --- Detail panel --- */
  .viz-detail {
    position: fixed;
    top: var(--nav-h);
    right: 0;
    width: var(--viz-detail-width, 380px);
    max-width: 90vw;
    height: calc(100vh - var(--nav-h));
    background: var(--bg);
    border-left: 1px solid var(--border-strong);
    z-index: 30;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
  .viz-detail[hidden] {
    display: none;
  }
  /* Drag handle — pinned to the panel's left edge. 6px-wide hit zone with
     a 1px accent rule that lights up on hover or while dragging. */
  .viz-detail-resize {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 6px;
    cursor: col-resize;
    z-index: 1;
    touch-action: none;
  }
  .viz-detail-resize::after {
    content: "";
    position: absolute;
    inset: 0 auto 0 0;
    width: 1px;
    background: transparent;
    transition: background 120ms ease;
  }
  .viz-detail-resize:hover::after,
  body[data-viz-resizing] .viz-detail-resize::after {
    background: var(--accent);
  }
  /* While dragging, force the resize cursor everywhere and kill text
     selection so the canvas/text doesn't fight the drag. */
  body[data-viz-resizing] {
    cursor: col-resize;
    user-select: none;
  }
  .viz-detail-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem var(--space-md);
    border-bottom: 1px solid var(--border);
    font-family: var(--font);
    font-size: 0.85rem;
  }
  .viz-detail-body {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-md);
    font-family: var(--font);
    font-size: 0.75rem;
    line-height: 1.6;
  }
  .viz-detail-body pre {
    font-size: 0.72rem;
    background: var(--bg-surface);
    border-left: 3px solid var(--accent);
    padding: 0.6rem 0.8rem;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-all;
    margin: 0.5rem 0;
  }

  /* Diff line backgrounds */
  .viz-detail-body .token.inserted {
    display: block;
    background: color-mix(in srgb, var(--syn-string) 18%, transparent);
  }
  .viz-detail-body .token.deleted {
    display: block;
    background: color-mix(in srgb, var(--alert) 14%, transparent);
  }
  .viz-detail-body .token.coord {
    color: var(--text-secondary);
    font-style: italic;
  }
  .viz-detail-body .detail-section {
    margin-bottom: var(--space-md);
  }
  .viz-detail-body .detail-label {
    color: var(--text-dim);
    font-size: 0.62rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
    margin-bottom: 0.2rem;
  }
  .viz-detail-body .detail-nav {
    display: flex;
    gap: 0.4rem;
    flex-wrap: wrap;
  }
  .viz-detail-body .detail-nav a {
    font-size: 0.75rem;
    color: var(--accent);
    cursor: pointer;
    text-decoration: underline;
  }

  /* --- Legend --- */
  .viz-legend {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-md);
    padding: 0.75rem 0;
    margin-top: 0.5rem;
    font-family: var(--font);
    font-size: 0.72rem;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
  }
  .viz-legend-item {
    display: flex;
    align-items: center;
    gap: 0.4rem;
  }
  .viz-swatch {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid;
    flex-shrink: 0;
  }
  .viz-swatch-human {
    background: color-mix(in srgb, var(--accent) 18%, transparent);
    border-color: var(--accent);
  }
  .viz-swatch-agent {
    background: color-mix(in srgb, var(--accent) 30%, transparent);
    border-color: var(--accent);
  }
  .viz-swatch-tool {
    background: color-mix(in srgb, var(--text-secondary) 15%, transparent);
    border-color: var(--text-secondary);
  }
  .viz-swatch-ci {
    background: color-mix(in srgb, var(--text-secondary) 15%, transparent);
    border-color: var(--text-secondary);
    border-style: dashed;
  }
  .viz-swatch-dead {
    background: color-mix(in srgb, var(--alert) 18%, transparent);
    border-color: var(--alert);
    border-style: dashed;
  }
  .viz-swatch-head {
    background: var(--bg-surface);
    border-color: var(--accent);
    border-width: 3px;
  }
  .viz-swatch-base {
    background: var(--bg-elevated);
    border-color: var(--text-secondary);
    border-radius: 50%;
  }

  /* --- Responsive --- */
  @media (max-width: 640px) {
    .viz-controls {
      flex-direction: column;
      gap: 0.5rem;
      align-items: flex-start;
    }
    .viz-detail {
      width: 100vw;
      max-width: 100vw;
    }
    .viz-canvas-wrap {
      height: 45vh;
    }
  }
}
