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

  /* Drop-target state — set on .viz-layout while a file drag is over the
     visualizer. The accent outline frames the page; the overlay pseudo
     announces the drop intent without rebuilding the DOM. */
  .viz-layout.viz-drag-active {
    position: relative;
    outline: 2px dashed var(--accent);
    outline-offset: 4px;
  }
  .viz-layout.viz-drag-active::after {
    content: "Drop Toolpath JSON here";
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: color-mix(in srgb, var(--accent) 12%, transparent);
    color: var(--accent);
    font-family: var(--font);
    font-size: 1rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    pointer-events: none;
    z-index: 40;
  }

  /* --- 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 1rem;
    resize: none;
    position: relative;
    z-index: 1;
  }
  .viz-textarea:focus {
    outline: none;
  }
  .viz-textarea::selection {
    background: var(--accent-dim);
  }
  .viz-textarea::placeholder {
    color: var(--text-dim);
  }
  /* Brand scrollbar — matches the terminal-on-surface look from
     playground.css. Firefox honors scrollbar-width/color; WebKit (Chrome,
     Safari, Edge) uses the pseudo-elements below. macOS overlay scrollbars
     ignore both and stay system-default, which is fine. */
  .viz-textarea {
    scrollbar-width: thin;
    scrollbar-color: color-mix(in srgb, var(--text-secondary) 25%, transparent)
      transparent;
  }
  .viz-textarea::-webkit-scrollbar {
    width: 6px;
    height: 6px;
  }
  .viz-textarea::-webkit-scrollbar-track {
    background: transparent;
  }
  .viz-textarea::-webkit-scrollbar-thumb {
    background: color-mix(in srgb, var(--text-secondary) 25%, transparent);
  }
  .viz-textarea:hover::-webkit-scrollbar-thumb,
  .viz-textarea::-webkit-scrollbar-thumb:hover {
    background: color-mix(in srgb, var(--text-secondary) 60%, transparent);
  }
  /* Custom resize handle — full-width hit zone along the bottom of the
     editor with a centered dotted grip that matches the brand palette.
     Replaces the native textarea resizer (disabled via resize: none).
     Drag logic + height persistence lives in visualizer.js. */
  .viz-editor-resize {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    height: 8px;
    cursor: ns-resize;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2;
    touch-action: none;
  }
  .viz-editor-resize::after {
    content: "";
    width: 32px;
    border-top: 2px dotted var(--text-secondary);
    opacity: 0.4;
    transition:
      opacity 120ms ease,
      border-top-color 120ms ease;
  }
  .viz-editor-resize:hover::after,
  body[data-viz-editor-resizing] .viz-editor-resize::after {
    opacity: 1;
    border-top-color: var(--accent);
  }
  body[data-viz-editor-resizing] {
    cursor: ns-resize;
    user-select: none;
  }

  /* Format toggle — floats in the editor's upper-right. Sits above the
     textarea via z-index so it stays clickable; small + low-contrast so it
     reads as a chrome control rather than a primary action. The right offset
     adds --viz-scrollbar-w (set from JS by measuring the textarea's
     scrollbar) so the button clears classic scrollbars on Windows/Linux
     while still hugging the edge when overlay scrollbars are in use. */
  .viz-format-toggle {
    position: absolute;
    top: 0.4rem;
    right: calc(0.4rem + var(--viz-scrollbar-w, 0px));
    z-index: 2;
    font-family: var(--font);
    font-size: 0.65rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    padding: 0.2rem 0.5rem;
    border: 1px solid var(--border);
    background: var(--bg-elevated);
    color: var(--text-secondary);
    cursor: pointer;
    line-height: 1;
  }
  .viz-format-toggle:hover {
    border-color: var(--accent);
    color: var(--accent);
  }

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