@property --value {
  syntax: '<number>';
  inherits: true;
  initial-value: 0;
}

:root {
  --bg-void: #050811;
  --bg-canvas: #080d1a;
  --bg-panel: #0d1424;
  --bg-elevated: #121c31;
  --bg-node: #0f182b;
  --border-dim: rgba(148, 163, 184, 0.16);
  --border-mid: rgba(56, 189, 248, 0.3);
  --border-glow: rgba(34, 211, 238, 0.72);
  --text-main: #edf4ff;
  --text-sub: #94a7c6;
  --text-muted: #64789a;
  --cyan: #22d3ee;
  --cyan-dim: rgba(34, 211, 238, 0.14);
  --emerald: #10b981;
  --emerald-dim: rgba(16, 185, 129, 0.15);
  --amber: #f59e0b;
  --amber-dim: rgba(245, 158, 11, 0.16);
  --crimson: #f43f5e;
  --crimson-dim: rgba(244, 63, 94, 0.16);
  --violet: #a78bfa;
  --radius-sm: 7px;
  --radius-md: 11px;
  --radius-lg: 14px;
  --font-sans: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", system-ui, -apple-system, sans-serif;
  --font-mono: "JetBrains Mono", "SF Mono", "Cascadia Code", "Fira Code", ui-monospace, Consolas, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100dvw;
  height: 100dvh;
  overflow: hidden;
  background:
    radial-gradient(ellipse 80% 50% at 50% -10%, rgba(34, 211, 238, 0.11), transparent 60%),
    var(--bg-void);
  color: var(--text-main);
  font-family: var(--font-sans);
  font-size: 13.5px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

[hidden] {
  display: none !important;
}

.mono {
  font-family: var(--font-mono);
}

.mt-sm {
  margin-top: 12px;
}

/* --------------------------------------------------------------------------
 * Single-Screen Auto-Scaling Stage (100dvw x 100dvh, zero page scroll)
 * -------------------------------------------------------------------------- */
.viewport-stage {
  position: relative;
  width: 100dvw;
  height: 100dvh;
  overflow: hidden;
}

.cmd-shell {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform-origin: 0 0;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: hidden;
  container-type: inline-size;
  container-name: cmd-shell;
}

/* --------------------------------------------------------------------------
 * Row 1: Compact Header Command Bar
 * -------------------------------------------------------------------------- */
.cmd-header {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 7px 12px;
  background: rgba(13, 20, 36, 0.92);
  border: 1px solid var(--border-dim);
  border-radius: var(--radius-md);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.38);
}

.cmd-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-core {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  background: linear-gradient(135deg, rgba(34, 211, 238, 0.25), rgba(167, 139, 250, 0.25));
  border: 1px solid rgba(34, 211, 238, 0.52);
  box-shadow: 0 0 14px rgba(34, 211, 238, 0.22);
}

.brand-glyph {
  font-size: 15px;
  font-weight: 800;
  color: #e0f7ff;
}

.brand-copy {
  min-width: 0;
}

.brand-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.cmd-header h1 {
  margin: 0;
  font-size: clamp(0.95rem, 0.7rem + 0.6vw, 1.15rem);
  font-weight: 700;
  letter-spacing: 0.02em;
  color: var(--text-main);
  white-space: nowrap;
}

@supports (font-size: 1cqi) {
  .cmd-header h1 {
    font-size: clamp(0.92rem, 0.72rem + 0.45cqi, 1.15rem);
  }
}

.brand-sub {
  margin: 1px 0 0;
  font-size: 11px;
  color: var(--text-sub);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.replay-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  background: rgba(245, 158, 11, 0.2);
  border: 1px solid rgba(245, 158, 11, 0.74);
  color: #fde68a;
  box-shadow: 0 0 12px rgba(245, 158, 11, 0.24);
  white-space: nowrap;
}

.cmd-header-controls {
  display: flex;
  align-items: center;
  gap: 7px;
  flex-shrink: 0;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: var(--bg-canvas);
  border: 1px solid var(--border-dim);
  color: var(--text-sub);
  white-space: nowrap;
}

.status-pill.is-live {
  border-color: rgba(16, 185, 129, 0.5);
  color: #a7f3d0;
}

.status-pill.is-idle {
  border-color: rgba(148, 163, 184, 0.32);
  color: var(--text-sub);
}

.status-pill.is-replay {
  border-color: rgba(245, 158, 11, 0.55);
  color: #fde68a;
}

.status-pill.is-warn {
  border-color: rgba(244, 63, 94, 0.55);
  color: #fecdd3;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  flex-shrink: 0;
}

.status-dot.is-live {
  background: var(--emerald);
  box-shadow: 0 0 9px var(--emerald);
}

.status-dot.is-replay {
  background: var(--amber);
  box-shadow: 0 0 9px var(--amber);
}

.status-dot.is-error {
  background: var(--crimson);
}

.ctrl-btn {
  appearance: none;
  border: 1px solid var(--border-dim);
  background: var(--bg-elevated);
  color: var(--text-main);
  font-family: inherit;
  font-size: 11.5px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  transition: border-color 0.2s, background-color 0.2s, color 0.2s;
}

.ctrl-btn:hover:not(:disabled) {
  border-color: var(--border-mid);
  background: #17233d;
}

.ctrl-btn-accent {
  border-color: rgba(34, 211, 238, 0.45);
  background: rgba(34, 211, 238, 0.12);
  color: #a5f3fc;
}

.ctrl-btn-accent:hover:not(:disabled) {
  border-color: var(--cyan);
  background: rgba(34, 211, 238, 0.2);
}

.ctrl-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* --------------------------------------------------------------------------
 * Row 2: Ultra-Compact Top Telemetry Bar (Quota Rings + Production KPIs)
 * -------------------------------------------------------------------------- */
.telemetry-bar {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: minmax(470px, 1.15fr) minmax(0, 1.25fr);
  gap: 8px;
}

.quota-panel,
.canvas-panel,
.terminal-panel,
.works-dock,
.shelf-col,
.book-panel,
.reader-panel {
  background: var(--bg-panel);
  border: 1px solid var(--border-dim);
  border-radius: var(--radius-md);
}

.quota-panel {
  padding: 7px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.quota-panel-label {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex-shrink: 0;
  padding-right: 6px;
  border-right: 1px solid var(--border-dim);
}

.panel-mini-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.panel-mini-title {
  font-size: 11.5px;
  font-weight: 700;
  color: var(--text-main);
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.panel-mini-note {
  font-size: 10.5px;
  color: var(--text-muted);
  white-space: nowrap;
}

.quota-rings {
  flex: 1;
  min-width: 0;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}

.quota-ring-card {
  background: var(--bg-canvas);
  border: 1px solid var(--border-dim);
  border-radius: var(--radius-sm);
  padding: 5px 7px;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 7px;
  min-width: 0;
  transition: border-color 0.25s, box-shadow 0.25s;
}

.quota-ring-card.is-active-model {
  border-color: rgba(34, 211, 238, 0.72);
  box-shadow: 0 0 14px rgba(34, 211, 238, 0.16);
}

.quota-ring-meta {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
  text-align: left;
}

/* Native <progress> ring with @property, border-area & mask fallback */
.progress-ring-wrapper {
  position: relative;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  flex-shrink: 0;
}

progress.progress-ring::-webkit-progress-bar {
  display: none;
  background: none;
}

progress.progress-ring::-webkit-progress-value {
  display: none;
  background: none;
}

progress.progress-ring::-moz-progress-bar {
  display: none;
  background: none;
}

progress.progress-ring::slider-fill {
  display: none;
  background: none;
}

progress.progress-ring {
  appearance: none;
  --size: 38px;
  --thickness: 4.5px;
  --track-color: rgba(148, 163, 184, 0.16);
  --fill-color: var(--emerald);
  --value: attr(value type(<number>));
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
  background: conic-gradient(
    var(--fill-color) calc(var(--value) * 1%),
    var(--track-color) 0
  );
  background-clip: border-area;
  border: var(--thickness) solid transparent;
  background-origin: border-box;
  transition: --value 0s ease-in-out;
}

@media (prefers-reduced-motion: no-preference) {
  progress.progress-ring {
    transition-duration: 0.45s;
  }
}

@supports not (background-clip: border-area) {
  progress.progress-ring {
    mask-image: radial-gradient(
      transparent calc(50% - var(--thickness)),
      black calc(50% - var(--thickness) + 0.5px)
    );
    border: 0;
  }
}

@media (forced-colors: active) {
  progress.progress-ring {
    forced-color-adjust: none;
    --fill-color: CanvasText;
    --track-color: Canvas;
  }
}

progress.progress-ring.is-warn { --fill-color: var(--amber); }
progress.progress-ring.is-danger { --fill-color: var(--crimson); }
progress.progress-ring.is-empty { --fill-color: rgba(148, 163, 184, 0.25); }

.progress-ring-content {
  position: absolute;
  font-size: 10px;
  font-weight: 700;
  color: var(--text-main);
  pointer-events: none;
}

.quota-ring-name {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-main);
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.quota-ring-sub {
  font-size: 9.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.quota-empty {
  grid-column: 1 / -1;
  text-align: center;
  padding: 10px 6px;
  font-size: 11.5px;
  color: var(--text-muted);
}

.kpi-strip {
  display: grid;
  grid-template-columns: minmax(150px, 1.45fr) repeat(4, minmax(0, 1fr));
  gap: 6px;
}

.kpi-tile {
  background: var(--bg-panel);
  border: 1px solid var(--border-dim);
  border-radius: var(--radius-md);
  padding: 7px 9px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 2px;
  min-width: 0;
}

.kpi-model-tile {
  border-color: rgba(34, 211, 238, 0.3);
  background: linear-gradient(180deg, rgba(34, 211, 238, 0.07), var(--bg-panel));
}

.kpi-k {
  font-size: 10.5px;
  color: var(--text-sub);
  font-weight: 600;
  white-space: nowrap;
}

.kpi-v {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-main);
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.kpi-model-tile .kpi-v {
  font-size: 12px;
  color: #67e8f9;
  line-height: 1.25;
}

.kpi-s {
  font-size: 10px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.kpi-mini-track {
  width: 100%;
  height: 4px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.16);
  overflow: hidden;
}

.kpi-mini-fill {
  display: block;
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--cyan), var(--emerald));
  transition: width 0.35s ease;
}

/* --------------------------------------------------------------------------
 * Dynamic Model Switch / Backoff / Circuit Breaker Ribbon
 * -------------------------------------------------------------------------- */
.model-event-banner {
  flex: 0 0 auto;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(245, 158, 11, 0.58);
  background: linear-gradient(90deg, rgba(245, 158, 11, 0.17), rgba(34, 211, 238, 0.08));
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.model-event-banner.kind-quota_exhausted {
  border-color: rgba(244, 63, 94, 0.72);
  background: linear-gradient(90deg, rgba(244, 63, 94, 0.2), rgba(244, 63, 94, 0.06));
}

.model-event-banner.kind-model_restored {
  border-color: rgba(16, 185, 129, 0.65);
  background: linear-gradient(90deg, rgba(16, 185, 129, 0.18), rgba(34, 211, 238, 0.06));
}

.model-event-banner.kind-rate_limit_retry {
  border-color: rgba(34, 211, 238, 0.65);
  background: linear-gradient(90deg, rgba(34, 211, 238, 0.16), rgba(245, 158, 11, 0.1));
}

.meb-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  overflow: hidden;
}

.meb-pill {
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 10.5px;
  font-weight: 700;
  background: rgba(5, 8, 17, 0.55);
  border: 1px solid currentColor;
  white-space: nowrap;
  flex-shrink: 0;
}

.meb-route {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 700;
  font-size: 11.5px;
  white-space: nowrap;
  flex-shrink: 0;
}

.meb-arrow {
  color: var(--cyan);
}

.meb-reason {
  font-size: 11.5px;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.meb-time {
  font-size: 10.5px;
  color: var(--text-sub);
  white-space: nowrap;
  flex-shrink: 0;
}

/* --------------------------------------------------------------------------
 * Row 3 (flex: 1): Main Split Workspace (Workflow Canvas + Log Terminal)
 * -------------------------------------------------------------------------- */
.command-grid {
  flex: 1 1 0;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 2.02fr) minmax(320px, 0.92fr);
  gap: 8px;
  align-items: stretch;
}

/* Workflow Blueprint Canvas */
.canvas-panel {
  min-height: 0;
  padding: 10px 12px;
  background:
    radial-gradient(rgba(56, 189, 248, 0.14) 1px, transparent 1px),
    linear-gradient(180deg, rgba(12, 19, 36, 0.96), rgba(8, 13, 26, 0.98));
  background-size: 20px 20px, 100% 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: relative;
  overflow: hidden;
}

.canvas-head {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.canvas-head-left,
.canvas-head-right {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.canvas-badge {
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  background: rgba(148, 163, 184, 0.15);
  border: 1px solid rgba(148, 163, 184, 0.35);
  color: var(--text-sub);
  white-space: nowrap;
  flex-shrink: 0;
}

.canvas-panel[data-pipeline-state="running"] .canvas-badge {
  background: var(--cyan-dim);
  border-color: var(--cyan);
  color: #a5f3fc;
}

.canvas-panel[data-pipeline-state="rewriting"] .canvas-badge {
  background: var(--amber-dim);
  border-color: var(--amber);
  color: #fde68a;
}

.canvas-panel[data-pipeline-state="isolated"] .canvas-badge,
.canvas-panel[data-pipeline-state="paused_quota"] .canvas-badge {
  background: var(--crimson-dim);
  border-color: var(--crimson);
  color: #fecdd3;
}

.canvas-title {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.canvas-meta {
  font-size: 11px;
  color: var(--text-sub);
  white-space: nowrap;
}

.canvas-attempt-pill {
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  background: var(--cyan-dim);
  border: 1px solid rgba(34, 211, 238, 0.5);
  color: #a5f3fc;
  white-space: nowrap;
}

.canvas-attempt-pill.is-rewrite {
  background: var(--amber-dim);
  border-color: rgba(245, 158, 11, 0.65);
  color: #fde68a;
}

.idle-strip {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  background: rgba(15, 24, 43, 0.88);
  border: 1px dashed rgba(148, 163, 184, 0.34);
  font-size: 11.5px;
  color: var(--text-sub);
}

.idle-icon {
  color: var(--text-muted);
}

.idle-ts {
  margin-left: auto;
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}

/* 8-Node Workflow Stage + SVG Layer */
.workflow-stage {
  flex: 1 1 0;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  padding: 10px 22px 10px 6px;
}

.workflow-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
  z-index: 1;
}

.wf-edge-base {
  fill: none;
  stroke: rgba(148, 163, 184, 0.24);
  stroke-width: 2;
  stroke-linecap: round;
}

.wf-edge-base.is-done { stroke: rgba(16, 185, 129, 0.55); }
.wf-edge-base.is-active { stroke: rgba(34, 211, 238, 0.76); stroke-width: 2.5; }
.wf-edge-base.is-rewrite { stroke: rgba(245, 158, 11, 0.24); stroke-dasharray: 5 5; }
.wf-edge-base.is-rewrite.is-active { stroke: rgba(245, 158, 11, 0.92); stroke-width: 2.5; }

.wf-edge-flow {
  fill: none;
  stroke: var(--cyan);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-dasharray: 7 15;
  opacity: 0;
}

.wf-edge-flow.is-active { opacity: 1; }
.wf-edge-flow.is-rewrite { stroke: var(--amber); }

.wf-edge-dot { fill: #e0faff; opacity: 0; }
.wf-edge-dot.is-active { opacity: 1; }
.wf-edge-dot.is-rewrite { fill: #fef08a; }

.rewrite-loop-badge {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  z-index: 4;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  background: rgba(245, 158, 11, 0.24);
  border: 1px solid rgba(245, 158, 11, 0.85);
  color: #fef3c7;
  box-shadow: 0 0 16px rgba(245, 158, 11, 0.35);
  white-space: nowrap;
}

.workflow-grid {
  position: relative;
  z-index: 2;
  width: 100%;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: auto auto;
  gap: 36px 22px;
  align-items: stretch;
}

/* Landscape Snake: Row 1 L->R (1..4), Row 2 R->L (5 on col 4 .. 8 on col 1) */
#node-plan    { grid-row: 1; grid-column: 1; }
#node-outline { grid-row: 1; grid-column: 2; }
#node-compile { grid-row: 1; grid-column: 3; }
#node-draft   { grid-row: 1; grid-column: 4; }
#node-gate    { grid-row: 2; grid-column: 4; }
#node-polish  { grid-row: 2; grid-column: 3; }
#node-lock    { grid-row: 2; grid-column: 2; }
#node-export  { grid-row: 2; grid-column: 1; }

.wf-node {
  position: relative;
  background: rgba(15, 24, 43, 0.95);
  border: 1px solid rgba(148, 163, 184, 0.22);
  border-radius: var(--radius-md);
  padding: 9px 10px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-height: 88px;
  transition: border-color 0.25s, box-shadow 0.25s, background-color 0.25s;
}

.wf-node[data-status="done"] {
  border-color: rgba(16, 185, 129, 0.55);
  background: linear-gradient(180deg, rgba(16, 185, 129, 0.08), rgba(15, 24, 43, 0.95));
}

.wf-node[data-status="running"] {
  border-color: var(--cyan);
  background: linear-gradient(180deg, rgba(34, 211, 238, 0.14), rgba(15, 24, 43, 0.98));
  box-shadow: 0 0 0 1px rgba(34, 211, 238, 0.45), 0 0 22px rgba(34, 211, 238, 0.28);
}

.wf-node[data-status="rewriting"] {
  border-color: var(--amber);
  background: linear-gradient(180deg, rgba(245, 158, 11, 0.16), rgba(15, 24, 43, 0.98));
  box-shadow: 0 0 0 1px rgba(245, 158, 11, 0.5), 0 0 22px rgba(245, 158, 11, 0.3);
}

.wf-node[data-status="failed"] {
  border-color: var(--crimson);
  background: linear-gradient(180deg, rgba(244, 63, 94, 0.16), rgba(15, 24, 43, 0.98));
  box-shadow: 0 0 16px rgba(244, 63, 94, 0.24);
}

.wf-node[data-status="running"]::after,
.wf-node[data-status="rewriting"]::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: calc(var(--radius-md) + 4px);
  border: 2px solid var(--cyan);
  opacity: 0.35;
  pointer-events: none;
}

.wf-node[data-status="rewriting"]::after {
  border-color: var(--amber);
}

.wf-node-head {
  display: flex;
  align-items: center;
  gap: 5px;
}

.wf-step {
  font-size: 10px;
  font-weight: 700;
  color: var(--text-muted);
  background: rgba(148, 163, 184, 0.12);
  padding: 1px 4px;
  border-radius: 4px;
  flex-shrink: 0;
}

.wf-node[data-status="running"] .wf-step { color: #a5f3fc; background: rgba(34, 211, 238, 0.2); }
.wf-node[data-status="done"] .wf-step { color: #a7f3d0; background: rgba(16, 185, 129, 0.2); }

.wf-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-main);
  white-space: nowrap;
}

.wf-state-tag {
  margin-left: auto;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.12);
  color: var(--text-muted);
  white-space: nowrap;
}

.wf-node[data-status="running"] .wf-state-tag   { background: var(--cyan-dim); color: #67e8f9; }
.wf-node[data-status="done"] .wf-state-tag      { background: var(--emerald-dim); color: #6ee7b7; }
.wf-node[data-status="rewriting"] .wf-state-tag { background: var(--amber-dim); color: #fde68a; }
.wf-node[data-status="failed"] .wf-state-tag    { background: var(--crimson-dim); color: #fda4af; }

.wf-detail {
  margin: 0;
  font-size: 11px;
  color: var(--text-sub);
  line-height: 1.35;
  word-break: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Draft Node Thinking & Real Token Counter */
.draft-thinking-row {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 3px 7px;
  border-radius: 5px;
  background: rgba(34, 211, 238, 0.14);
  border: 1px solid rgba(34, 211, 238, 0.4);
  font-size: 11px;
  font-weight: 700;
  color: #a5f3fc;
}

.thinking-pulse {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--cyan);
  flex-shrink: 0;
}

.thinking-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.thinking-elapsed {
  margin-left: auto;
  font-size: 10.5px;
  color: #67e8f9;
  flex-shrink: 0;
}

.draft-token-box {
  margin-top: auto;
  padding: 5px 7px;
  border-radius: 6px;
  background: rgba(5, 8, 17, 0.68);
  border: 1px solid rgba(148, 163, 184, 0.16);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.token-main {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 10.5px;
}

.token-k { color: var(--text-muted); }
.token-v { font-size: 12.5px; color: #67e8f9; }

.token-sub {
  display: flex;
  justify-content: space-between;
  gap: 4px;
  font-size: 9.5px;
  color: var(--text-muted);
}

.token-sub b { color: var(--text-sub); font-weight: 600; }

/* Gate Rule-by-Rule Compact Checklist */
.gate-matrix-panel {
  flex: 0 0 auto;
  background: rgba(8, 13, 26, 0.88);
  border: 1px solid var(--border-dim);
  border-radius: var(--radius-sm);
  padding: 7px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.gate-rules-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}

.gate-rule-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 7px;
  border-radius: 6px;
  background: rgba(15, 24, 43, 0.9);
  border: 1px solid var(--border-dim);
  min-width: 0;
}

.gate-rule-chip.is-pass { border-color: rgba(16, 185, 129, 0.45); background: rgba(16, 185, 129, 0.08); }
.gate-rule-chip.is-fail { border-color: rgba(244, 63, 94, 0.72); background: rgba(244, 63, 94, 0.14); }
.gate-rule-chip.is-warn { border-color: rgba(245, 158, 11, 0.55); background: rgba(245, 158, 11, 0.1); }

.gate-mark {
  width: 17px;
  height: 17px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 10.5px;
  font-weight: 800;
  flex-shrink: 0;
}

.gate-rule-chip.is-pass .gate-mark { background: rgba(16, 185, 129, 0.22); color: #34d399; }
.gate-rule-chip.is-fail .gate-mark { background: rgba(244, 63, 94, 0.24); color: #fb7185; }
.gate-rule-chip.is-warn .gate-mark { background: rgba(245, 158, 11, 0.24); color: #fbbf24; }

.gate-rule-info {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.gate-rule-id {
  font-size: 10.5px;
  font-weight: 700;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.gate-rule-metric {
  font-size: 9.5px;
  color: var(--text-sub);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.gate-empty {
  grid-column: 1 / -1;
  font-size: 11px;
  color: var(--text-muted);
  padding: 4px 2px;
}

/* Machine Log Terminal (Full-Height Flex Companion) */
.terminal-panel {
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  background: #070b15;
}

.terminal-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 7px 10px;
  background: #0c1222;
  border-bottom: 1px solid var(--border-dim);
}

.term-dots {
  display: inline-flex;
  gap: 4px;
  flex-shrink: 0;
}

.td { width: 8px; height: 8px; border-radius: 50%; }
.td-r { background: #ef4444; }
.td-y { background: #f59e0b; }
.td-g { background: #10b981; }

.term-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-sub);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.term-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.term-count {
  font-size: 10.5px;
  color: var(--text-muted);
  white-space: nowrap;
}

.term-btn {
  appearance: none;
  border: 1px solid var(--border-dim);
  background: rgba(15, 24, 43, 0.9);
  color: var(--text-sub);
  font-family: inherit;
  font-size: 10.5px;
  padding: 2px 7px;
  border-radius: 5px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
}

.term-btn.is-on {
  border-color: rgba(34, 211, 238, 0.5);
  color: #67e8f9;
}

.terminal-body {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto;
  padding: 8px 10px;
  font-size: 11px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.term-line {
  display: flex;
  align-items: baseline;
  gap: 5px;
  padding: 1.5px 4px;
  border-radius: 4px;
  word-break: break-word;
}

.term-line.is-latest { background: rgba(34, 211, 238, 0.08); }
.term-ts { color: var(--text-muted); flex-shrink: 0; }

.term-tag {
  font-size: 9.5px;
  font-weight: 700;
  padding: 0 4px;
  border-radius: 4px;
  flex-shrink: 0;
  background: rgba(148, 163, 184, 0.14);
  color: var(--text-sub);
}

.term-line.lvl-info .term-tag { background: var(--cyan-dim); color: #67e8f9; }
.term-line.lvl-pass .term-tag { background: var(--emerald-dim); color: #34d399; }
.term-line.lvl-warn .term-tag { background: var(--amber-dim); color: #fbbf24; }
.term-line.lvl-fail .term-tag { background: var(--crimson-dim); color: #fb7185; }
.term-msg { color: #dbe7fb; }
.term-line.lvl-muted .term-msg { color: var(--text-muted); }
.term-empty { color: var(--text-muted); padding: 12px 4px; }

.term-jump-bottom {
  position: absolute;
  right: 12px;
  bottom: 10px;
  appearance: none;
  border: 1px solid rgba(34, 211, 238, 0.6);
  background: rgba(8, 13, 26, 0.94);
  color: #a5f3fc;
  font-family: inherit;
  font-size: 11px;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 999px;
  cursor: pointer;
}

/* --------------------------------------------------------------------------
 * Row 4: Compact Bottom Approved-Works & Quick-Read Dock
 * -------------------------------------------------------------------------- */
.works-dock {
  flex: 0 0 auto;
  padding: 6px 10px;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  background: rgba(13, 20, 36, 0.94);
}

.dock-book {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
  flex: 1 1 auto;
  overflow: hidden;
}

.dock-kicker {
  font-size: 10px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--emerald-dim);
  color: #6ee7b7;
  white-space: nowrap;
  flex-shrink: 0;
}

.dock-book-title {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text-main);
  white-space: nowrap;
  flex-shrink: 0;
}

.dock-book-meta {
  font-size: 11px;
  color: var(--text-sub);
  white-space: nowrap;
  flex-shrink: 0;
}

.dock-hook {
  font-size: 11px;
  color: #fde68a;
  padding-left: 8px;
  border-left: 2px solid rgba(245, 158, 11, 0.65);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dock-chapters {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-shrink: 0;
}

.dock-ch-btn {
  appearance: none;
  border: 1px solid var(--border-dim);
  background: var(--bg-canvas);
  color: var(--text-main);
  font-family: inherit;
  font-size: 11px;
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 0.2s, background-color 0.2s;
}

.dock-ch-btn:hover {
  border-color: var(--cyan);
  background: rgba(34, 211, 238, 0.12);
}

.dock-open-btn {
  flex-shrink: 0;
}

/* --------------------------------------------------------------------------
 * PORTRAIT / NARROW PANE SINGLE-SCREEN AUTO-SCALING MODE
 * (Covers 590x1024 IDE split pane AND 390x844 mobile portrait!)
 * -------------------------------------------------------------------------- */
html[data-layout="portrait"] .cmd-shell {
  padding: 8px 10px;
  gap: 6px;
}

html[data-layout="portrait"] .cmd-header {
  padding: 6px 9px;
  flex-wrap: wrap;
  row-gap: 5px;
}

html[data-layout="portrait"] .cmd-brand {
  flex: 1 1 100%;
  justify-content: flex-start;
}

html[data-layout="portrait"] .brand-title-row {
  flex-wrap: nowrap;
  justify-content: space-between;
  width: 100%;
}

html[data-layout="portrait"] .cmd-header-controls {
  width: 100%;
  justify-content: space-between;
}

html[data-layout="portrait"] .brand-sub {
  display: none;
}

html[data-layout="portrait"] .telemetry-bar {
  grid-template-columns: 1fr;
  gap: 6px;
}

html[data-layout="portrait"] .quota-panel {
  padding: 5px 7px;
  gap: 6px;
}

html[data-layout="portrait"] .quota-panel-label {
  display: none;
}

html[data-layout="portrait"] .quota-ring-card {
  flex-direction: column;
  padding: 5px 4px;
  gap: 3px;
  text-align: center;
}

html[data-layout="portrait"] .quota-ring-meta {
  align-items: center;
  text-align: center;
  width: 100%;
}

html[data-layout="portrait"] .progress-ring-wrapper {
  width: 34px;
  height: 34px;
}

html[data-layout="portrait"] progress.progress-ring {
  --size: 34px;
  --thickness: 4px;
}

html[data-layout="portrait"] .kpi-strip {
  grid-template-columns: minmax(118px, 1.35fr) repeat(4, minmax(0, 1fr));
  gap: 5px;
}

html[data-layout="portrait"] .kpi-tile {
  padding: 5px 6px;
}

html[data-layout="portrait"] .kpi-v {
  font-size: 13px;
}

html[data-layout="portrait"] .kpi-model-tile .kpi-v {
  font-size: 11px;
}

html[data-layout="portrait"] .command-grid {
  grid-template-columns: 1fr;
  grid-template-rows: auto minmax(120px, 1fr);
  gap: 6px;
}

html[data-layout="portrait"] .canvas-panel {
  padding: 8px 9px;
  gap: 6px;
}

html[data-layout="portrait"] .workflow-stage {
  flex: 0 0 auto;
  padding: 4px 4px;
}

/* Portrait 2x4 Snake Layout: Spacious ~245px nodes, Draft (R2C1) directly above Gate (R3C1) */
html[data-layout="portrait"] .workflow-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(4, auto);
  gap: 20px 18px;
}

html[data-layout="portrait"] #node-plan    { grid-row: 1; grid-column: 1; }
html[data-layout="portrait"] #node-outline { grid-row: 1; grid-column: 2; }
html[data-layout="portrait"] #node-compile { grid-row: 2; grid-column: 2; }
html[data-layout="portrait"] #node-draft   { grid-row: 2; grid-column: 1; }
html[data-layout="portrait"] #node-gate    { grid-row: 3; grid-column: 1; }
html[data-layout="portrait"] #node-polish  { grid-row: 3; grid-column: 2; }
html[data-layout="portrait"] #node-lock    { grid-row: 4; grid-column: 2; }
html[data-layout="portrait"] #node-export  { grid-row: 4; grid-column: 1; }

html[data-layout="portrait"] .wf-node {
  min-height: 62px;
  padding: 6px 8px;
  gap: 3px;
}

html[data-layout="portrait"] .wf-title {
  font-size: 12px;
}

html[data-layout="portrait"] .wf-detail {
  font-size: 10.5px;
  -webkit-line-clamp: 1;
}

html[data-layout="portrait"] .draft-token-box {
  padding: 3px 6px;
}

html[data-layout="portrait"] .gate-matrix-panel {
  padding: 6px 8px;
  gap: 4px;
}

html[data-layout="portrait"] .gate-rules-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 4px;
}

html[data-layout="portrait"] .gate-rule-chip {
  padding: 3px 6px;
}

html[data-layout="portrait"] .gate-rule-info {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  width: 100%;
}

html[data-layout="portrait"] .dock-hook {
  display: none;
}

html[data-layout="portrait"] .works-dock {
  gap: 6px;
  padding: 5px 8px;
}

html[data-layout="portrait"] .dock-book {
  flex: 0 1 auto;
}

html[data-layout="portrait"] .dock-ch-btn {
  padding: 3px 7px;
  font-size: 10.5px;
}

/* --------------------------------------------------------------------------
 * Native 1:1 Full-Viewport Works & Approved-Chapter Reader Drawer
 * -------------------------------------------------------------------------- */
.works-drawer {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(5, 8, 17, 0.96);
  backdrop-filter: blur(8px);
  overflow-y: auto;
  padding: 16px 18px 40px;
}

.drawer-shell {
  max-width: 1320px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.drawer-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--bg-panel);
  border: 1px solid var(--border-dim);
  border-radius: var(--radius-md);
  position: sticky;
  top: 0;
  z-index: 10;
}

.drawer-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-main);
}

.drawer-hint {
  margin-left: auto;
  font-size: 11px;
  color: var(--text-muted);
}

.works-layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}

.shelf-col {
  padding: 14px;
}

.section-heading {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
}

.shelf {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.shelf button {
  width: 100%;
  text-align: left;
  background: var(--bg-canvas);
  border: 1px solid var(--border-dim);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  color: var(--text-main);
  font-family: inherit;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: border-color 0.2s, background-color 0.2s;
}

.shelf button:hover { border-color: var(--border-mid); }
.shelf button.is-active { border-color: var(--cyan); background: rgba(34, 211, 238, 0.08); }
.shelf .t { font-size: 14px; font-weight: 700; }

.shelf .s {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  font-size: 11.5px;
  color: var(--text-sub);
}

.badge {
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  background: rgba(148, 163, 184, 0.16);
  color: var(--text-sub);
}

.badge.is-active { background: var(--emerald-dim); color: #6ee7b7; }
.badge.is-paused { background: var(--amber-dim); color: #fde68a; }

.book-panel,
.reader-panel {
  padding: 18px 20px;
}

.book-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
}

.book-head h2 { margin: 0; font-size: 20px; }
.meta { margin: 4px 0 0; font-size: 12.5px; color: var(--text-sub); }

.book-prog {
  min-width: 200px;
  display: flex;
  flex-direction: column;
  gap: 5px;
  align-items: flex-end;
  font-size: 12px;
  color: var(--text-sub);
}

.book-prog-track {
  width: 200px;
  height: 7px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.16);
  overflow: hidden;
}

.book-prog-track span {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--cyan), var(--emerald));
  border-radius: 999px;
  transition: width 0.35s ease;
}

.blurb {
  margin: 12px 0 14px;
  font-size: 13.5px;
  color: #cbd8ee;
  line-height: 1.7;
}

.plan-card {
  background: var(--bg-canvas);
  border: 1px solid rgba(34, 211, 238, 0.24);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  margin-bottom: 14px;
}

.plan-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.plan-kicker { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--cyan-dim); color: #67e8f9; }
.plan-title { font-size: 14px; font-weight: 700; }
.plan-events { margin: 8px 0 0; padding-left: 18px; color: #d6e2f5; font-size: 13px; }
.plan-events li { margin-bottom: 4px; }

.plan-hook {
  margin: 10px 0 0;
  padding: 7px 10px;
  border-left: 3px solid var(--amber);
  background: rgba(245, 158, 11, 0.08);
  border-radius: 0 6px 6px 0;
  font-size: 12.5px;
  color: #fde68a;
}

.tabs {
  display: flex;
  gap: 8px;
  border-bottom: 1px solid var(--border-dim);
  margin-bottom: 14px;
}

.tab {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-sub);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 12px;
  border-bottom: 2px solid transparent;
  cursor: pointer;
}

.tab.is-active { color: #67e8f9; border-bottom-color: var(--cyan); }

.chapters-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 8px;
}

.chapters-grid button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--bg-canvas);
  border: 1px solid var(--border-dim);
  color: var(--text-main);
  font-family: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  transition: border-color 0.2s, background-color 0.2s;
}

.chapters-grid button:hover { border-color: var(--cyan); background: rgba(34, 211, 238, 0.08); }
.chapters-grid .n { font-size: 11.5px; color: var(--text-muted); white-space: nowrap; flex-shrink: 0; }

.info-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
}

.card {
  background: var(--bg-canvas);
  border: 1px solid var(--border-dim);
  border-radius: var(--radius-md);
  padding: 12px;
}

.card h4 { margin: 0 0 4px; font-size: 14px; color: #a5f3fc; }
.card p { margin: 4px 0 0; font-size: 12.5px; color: #cbd8ee; }
.card .k { color: var(--amber); font-size: 11.5px; font-weight: 600; }
.empty-msg { color: var(--text-muted); font-size: 12.5px; margin: 10px 0 0; }

/* Comfortable Dark-Mode Reader */
.reader-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border-dim);
}

.reader-font-controls {
  display: inline-flex;
  gap: 4px;
  background: var(--bg-canvas);
  border: 1px solid var(--border-dim);
  border-radius: var(--radius-sm);
  padding: 2px;
}

.font-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-sub);
  font-family: inherit;
  font-size: 12px;
  padding: 4px 9px;
  border-radius: 6px;
  cursor: pointer;
}

.font-btn.is-active { background: var(--cyan-dim); color: #a5f3fc; font-weight: 700; }
.reader-head { max-width: 720px; margin: 18px auto 16px; }
.reader-head h2 { margin: 0; font-size: 22px; line-height: 1.35; }
.prose { max-width: 720px; margin: 0 auto; color: #e2ebf8; letter-spacing: 0.02em; }
.prose-sm { font-size: 15px; line-height: 1.85; }
.prose-md { font-size: 17px; line-height: 1.95; }
.prose-lg { font-size: 19px; line-height: 2.05; }
.prose p { margin: 0 0 1.05em; text-indent: 2em; }

.reader-nav {
  max-width: 720px;
  margin: 26px auto 4px;
  padding-top: 16px;
  border-top: 1px solid var(--border-dim);
  display: flex;
  justify-content: space-between;
  gap: 12px;
}

@media (max-width: 860px) {
  .works-layout {
    grid-template-columns: 1fr;
  }
  .drawer-hint {
    display: none;
  }
}

/* --------------------------------------------------------------------------
 * Opt-in Motion (prefers-reduced-motion: no-preference + WCAG 2.2.2 Pause)
 * -------------------------------------------------------------------------- */
@keyframes node-breathe {
  0%, 100% { opacity: 0.2; transform: scale(0.995); }
  50% { opacity: 0.72; transform: scale(1.012); }
}

@keyframes edge-flow {
  from { stroke-dashoffset: 44; }
  to { stroke-dashoffset: 0; }
}

@keyframes think-beat {
  0%, 100% { opacity: 0.35; transform: scale(0.85); }
  50% { opacity: 1; transform: scale(1.2); }
}

@media (prefers-reduced-motion: no-preference) {
  body:not(.motion-paused) .wf-node[data-status="running"]::after,
  body:not(.motion-paused) .wf-node[data-status="rewriting"]::after {
    animation: node-breathe 2.2s ease-in-out infinite;
  }

  body:not(.motion-paused) .wf-edge-flow.is-active {
    animation: edge-flow 1.15s linear infinite;
  }

  body:not(.motion-paused) .thinking-pulse {
    animation: think-beat 1.4s ease-in-out infinite;
  }

  body:not(.motion-paused) .status-dot.is-live {
    animation: think-beat 2s ease-in-out infinite;
  }
}
