:root {
  --page-gutter: clamp(12px, 1.2vw, 20px);
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-muted: #fafafa;
  --surface-subtle: #f8f9f9;
  --border: #f2f3f3;
  --border-strong: #bdc1c4;
  --text: #494e52;
  --text-muted: #7a8288;
  --accent: #52adc8;
  --accent-strong: #2f7f93;
  --accent-soft: rgba(82, 173, 200, 0.1);
  --chapter: #586e75;
  --chapter-soft: rgba(88, 110, 117, 0.08);
  --lo: #52adc8;
  --lo-soft: rgba(82, 173, 200, 0.09);
  --kc: #2aa198;
  --kc-soft: rgba(42, 161, 152, 0.1);
  --danger: #dc322f;
  --danger-soft: rgba(220, 50, 47, 0.08);
  --warning: #cb4b16;
  --warning-soft: rgba(203, 75, 22, 0.08);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  --radius: 12px;
  --font-sans:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif,
    "Apple Color Emoji", "Segoe UI Emoji";
  --font-mono:
    SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
}

body {
  padding: var(--page-gutter);
  overflow: hidden;
}

body.studio-mode {
  overflow: auto;
}

body.modal-open {
  overflow: hidden;
}

button,
input,
select,
textarea {
  font: inherit;
}

a {
  color: var(--accent);
}

.app-shell {
  --sidebar-width: 300px;
  --editor-width: 360px;
  --splitter-size: 12px;
  display: grid;
  gap: 16px;
  width: 100%;
  height: calc(100dvh - (var(--page-gutter) * 2));
  max-height: calc(100dvh - (var(--page-gutter) * 2));
  grid-template-columns:
    var(--sidebar-width)
    var(--splitter-size)
    minmax(540px, 1fr)
    var(--splitter-size)
    var(--editor-width);
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas:
    "topbar topbar topbar topbar topbar"
    "sidebar leftsplit graph rightsplit editor";
  max-width: none;
  margin: 0;
  min-height: 0;
  overflow: hidden;
}

body.panel-resizing {
  cursor: col-resize;
  user-select: none;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.topbar {
  grid-area: topbar;
  display: grid;
  gap: 16px;
  grid-template-columns: minmax(280px, 1fr) auto;
  align-items: start;
  padding: 20px 22px;
}

.topbar-copy h1,
.panel-header h2,
.section-heading h2 {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.subtitle,
.selection-summary,
.results-meta,
.field small,
.validation-list,
.result-snippet,
.chapter-stats,
.node-subtitle {
  color: var(--text-muted);
}

.subtitle {
  max-width: 72ch;
  margin: 8px 0 0;
  font-size: 0.95rem;
  line-height: 1.6;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  letter-spacing: 0;
  text-transform: none;
}

.toolbar,
.status-row,
.panel-actions,
.editor-actions,
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.status-row {
  grid-column: 1 / -1;
  padding-top: 4px;
  border-top: 1px solid var(--border);
}

.button {
  appearance: none;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  padding: 0.62rem 0.86rem;
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}

.button:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: #ffffff;
}

.button.primary:hover {
  border-color: var(--accent-strong);
  background: var(--accent-strong);
  color: #ffffff;
}

.button.ghost {
  background: var(--surface);
}

.button.danger {
  border-color: rgba(220, 50, 47, 0.24);
  color: var(--danger);
  background: var(--danger-soft);
}

.button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0.3rem 0.66rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-subtle);
  font-size: 0.84rem;
}

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

.pill.dirty {
  background: var(--warning-soft);
  color: var(--warning);
  border-color: rgba(203, 75, 22, 0.14);
}

.pill.error {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: rgba(220, 50, 47, 0.14);
}

.pill.success {
  background: var(--accent-soft);
  color: var(--accent-strong);
  border-color: rgba(82, 173, 200, 0.16);
}

.sidebar,
.graph-panel,
.editor-panel {
  min-height: 0;
  min-width: 0;
  height: 100%;
}

.resize-handle {
  position: relative;
  min-height: 0;
  cursor: col-resize;
  border-radius: 999px;
  touch-action: none;
}

.resize-handle::before {
  content: "";
  position: absolute;
  top: 8px;
  bottom: 8px;
  left: 50%;
  width: 2px;
  transform: translateX(-50%);
  border-radius: 999px;
  background: var(--border-strong);
  opacity: 0.65;
  transition: background 0.15s ease, opacity 0.15s ease;
}

.resize-handle:hover::before,
.resize-handle:focus-visible::before,
body.panel-resizing .resize-handle::before {
  background: var(--accent);
  opacity: 1;
}

.resize-handle:focus-visible {
  outline: none;
}

.sidebar {
  grid-area: sidebar;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}

.resize-handle-left {
  grid-area: leftsplit;
}

.resize-handle-right {
  grid-area: rightsplit;
}

.sidebar-tabs {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  padding: 14px 14px 0;
  border-bottom: 1px solid var(--border);
}

.sidebar-tab {
  appearance: none;
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 8px 8px 0 0;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.82rem 0.9rem 0.76rem;
  text-align: center;
}

.sidebar-tab:hover {
  color: var(--accent-strong);
  background: var(--surface-subtle);
}

.sidebar-tab.active {
  color: var(--text);
  border-bottom-color: var(--accent);
  background: var(--surface);
}

.sidebar-panel,
.editor,
.validation-panel {
  padding: 18px 20px;
}

.sidebar-panel {
  display: none;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.sidebar-panel.active {
  display: flex;
}

.section-heading {
  margin-bottom: 14px;
}

.section-heading.compact {
  margin-bottom: 12px;
}

.summary-cards {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.summary-card,
.chapter-card,
.result-card,
.detail-card,
.validation-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}

.summary-card {
  padding: 14px;
}

.summary-card h3,
.chapter-card h3,
.detail-card h3,
.validation-card h3 {
  margin: 0 0 6px;
  font-size: 0.86rem;
  font-weight: 600;
}

.summary-value {
  font-size: 1.6rem;
  font-weight: 600;
  letter-spacing: -0.03em;
}

.chapter-strip {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  gap: 10px;
  margin-top: 14px;
  min-height: 0;
  overflow: auto;
  align-items: stretch;
}

.chapter-card {
  display: flex;
  flex: 0 0 auto;
  flex-direction: column;
  gap: 8px;
  min-height: 124px;
  padding: 14px;
}

.chapter-card button {
  width: 100%;
  margin-top: auto;
}

.graph-panel {
  grid-area: graph;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  overflow: hidden;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 16px;
  padding: 18px 20px 10px;
}

.legend {
  padding: 0 20px 12px;
  color: var(--text-muted);
  font-size: 0.88rem;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.swatch {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  display: inline-block;
}

.swatch.root {
  background: var(--accent-strong);
}

.swatch.chapter {
  background: var(--chapter);
}

.swatch.lo {
  background: var(--lo);
}

.swatch.kc {
  background: var(--kc);
}

.graph-canvas {
  min-height: 0;
  height: 100%;
  position: relative;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  background:
    linear-gradient(var(--surface-subtle), var(--surface-subtle)) 0 0 / 100% 1px no-repeat,
    linear-gradient(90deg, rgba(242, 243, 243, 0.75) 1px, transparent 1px) 0 0 / 32px 32px,
    linear-gradient(rgba(242, 243, 243, 0.75) 1px, transparent 1px) 0 0 / 32px 32px,
    var(--surface);
}

.graph-svg {
  width: 100%;
  height: 100%;
  display: block;
}

.selection-summary {
  padding: 14px 20px 18px;
  font-size: 0.92rem;
}

.graph-link {
  fill: none;
  stroke: rgba(122, 130, 136, 0.32);
  stroke-width: 1.5px;
}

.graph-link.match {
  stroke: rgba(82, 173, 200, 0.45);
}

.graph-node {
  cursor: pointer;
}

.node-card {
  stroke-width: 1.5px;
  rx: 10px;
  ry: 10px;
}

.node-card.root {
  fill: var(--surface);
  stroke: var(--accent-strong);
}

.node-card.chapter {
  fill: var(--surface);
  stroke: var(--chapter);
}

.node-card.lo {
  fill: var(--surface);
  stroke: var(--lo);
}

.node-card.kc {
  fill: var(--surface);
  stroke: var(--kc);
}

.graph-node.selected .node-card {
  fill: var(--surface-subtle);
  stroke-width: 2px;
}

.graph-node.match .node-card {
  fill: var(--accent-soft);
}

.node-title {
  fill: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.node-subtitle {
  fill: var(--text-muted);
  font-size: 11px;
}

.editor-panel {
  grid-area: editor;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) minmax(0, 0.52fr);
  overflow: hidden;
}

.editor-actions {
  padding: 0 20px 14px;
}

.editor {
  min-height: 0;
  overflow: auto;
}

.editor-empty {
  border: 1px dashed var(--border-strong);
  border-radius: 10px;
  padding: 18px;
  background: var(--surface-muted);
  color: var(--text-muted);
}

.detail-card {
  padding: 16px;
}

.detail-card + .detail-card {
  margin-top: 14px;
}

.field-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.field-block {
  margin-top: 12px;
}

.field span {
  font-size: 0.82rem;
  color: var(--text-muted);
}

.field-expand-button {
  appearance: none;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 30px;
  width: 30px;
  padding: 0;
}

.field-expand-button:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
}

.field-expand-icon {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.35;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 0.72rem 0.82rem;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(82, 173, 200, 0.14);
}

textarea {
  min-height: 112px;
  resize: vertical;
  line-height: 1.5;
}

.results-meta {
  margin: 12px 0 10px;
  font-size: 0.88rem;
}

.search-results {
  display: grid;
  flex: 1 1 auto;
  gap: 8px;
  min-height: 0;
  height: auto;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
}

.result-card {
  text-align: left;
  padding: 12px;
  cursor: pointer;
}

.result-card:hover {
  border-color: var(--accent);
}

.result-card.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.result-tag {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
  padding: 0 0.5rem;
  border-radius: 999px;
  font-size: 0.74rem;
  margin-bottom: 8px;
  border: 1px solid currentColor;
  background: transparent;
}

.result-tag.root {
  color: var(--accent-strong);
}

.result-tag.chapter {
  color: var(--chapter);
}

.result-tag.lo {
  color: var(--lo);
}

.result-tag.kc {
  color: var(--kc);
}

.result-title {
  margin: 0 0 4px;
  font-size: 0.94rem;
  font-weight: 600;
  color: var(--text);
}

.result-snippet {
  margin: 0;
  font-size: 0.88rem;
  line-height: 1.5;
}

.validation-panel {
  border-top: 1px solid var(--border);
  background: var(--surface);
  min-height: 0;
  overflow: auto;
}

.validation-card {
  padding: 14px;
}

.validation-list {
  margin: 8px 0 0;
  padding-left: 1.15rem;
  font-size: 0.9rem;
  line-height: 1.5;
}

.validation-list li + li {
  margin-top: 4px;
}

.validation-ok {
  margin: 0;
  color: var(--accent-strong);
  font-size: 0.9rem;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
  background: rgba(73, 78, 82, 0.42);
  backdrop-filter: blur(3px);
}

.modal-overlay[hidden] {
  display: none;
}

.modal-card {
  width: min(920px, 100%);
  max-height: calc(100vh - 64px);
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 14px;
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.14);
}

.modal-header,
.modal-footer {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 16px;
}

.modal-header h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.modal-hint {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.5;
}

.modal-textarea {
  min-height: 0;
  height: min(62vh, 760px);
  font-family: var(--font-sans);
  font-size: 0.96rem;
  line-height: 1.6;
}

@media (max-width: 1360px) {
  .app-shell {
    grid-template-columns: 300px minmax(420px, 1fr);
    grid-template-rows: auto minmax(0, 1fr) minmax(0, 0.92fr);
    grid-template-areas:
      "topbar topbar"
      "sidebar graph"
      "editor editor";
  }

  .resize-handle {
    display: none;
  }

  .editor-panel {
    min-height: 0;
  }

  /* Narrow windows: let the page scroll instead of squeezing the graph to nothing. */
  body {
    overflow: auto;
  }

  .app-shell {
    height: auto;
    max-height: none;
    min-height: calc(100dvh - (var(--page-gutter) * 2));
    grid-template-rows: auto minmax(460px, 62vh) minmax(420px, auto);
  }
}

@media (max-width: 980px) {
  body {
    --page-gutter: 16px;
    padding: var(--page-gutter);
  }

  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(420px, 60vh) minmax(360px, 55vh) minmax(420px, auto);
    grid-template-areas:
      "topbar"
      "graph"
      "sidebar"
      "editor";
  }

  .topbar {
    grid-template-columns: 1fr;
  }

  .sidebar-tabs {
    padding-inline: 10px;
  }

  .modal-overlay {
    padding: 16px;
  }

  .field-grid,
  .summary-cards {
    grid-template-columns: 1fr;
  }

  .graph-canvas {
    min-height: 0;
  }

  .search-results {
    min-height: 0;
  }

  .modal-card {
    max-height: calc(100vh - 32px);
    padding: 16px;
  }

  .modal-header,
  .modal-footer,
  .field-label-row {
    flex-direction: column;
    align-items: stretch;
  }

  .modal-textarea {
    height: min(54vh, 620px);
  }
}
