:root {
  color-scheme: dark;
  --bg: #0c1220;
  --panel: #121a2b;
  --text: #e8eef8;
  --muted: #8fa0b5;
  --accent: #5b9fd4;
  --border: rgba(232, 238, 248, 0.12);
  --danger: #f87171;
  font-family: "Segoe UI", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
body {
  display: flex;
  flex-direction: column;
}

.bar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 0.5rem; padding: 0.55rem 0.85rem; border-bottom: 1px solid var(--border);
  padding-top: calc(0.55rem + env(safe-area-inset-top));
  background: rgba(12, 18, 32, 0.95);
  flex-shrink: 0;
}
.logo { color: var(--text); text-decoration: none; font-weight: 700; font-size: 1.1rem; margin-right: 0.5rem; }
.tag { color: var(--muted); font-size: 0.78rem; }
.save-state {
  font-size: 0.72rem;
  color: var(--muted);
  margin-left: 0.35rem;
}
.save-state.ok { color: #86c8a2; }
.save-state.busy { color: var(--accent); }
.save-state.err { color: var(--danger); }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}
.bar-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 0.45rem; }
.plan { font-size: 0.8rem; color: var(--muted); display: flex; align-items: center; gap: 0.35rem; }
.plan select, .field input, .field select, .field textarea {
  background: var(--panel); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 0.35rem 0.5rem; font-size: 0.88rem;
}
.btn {
  background: var(--panel); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 0.4rem 0.7rem; font-size: 0.85rem; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: #0c1220; border-color: transparent; font-weight: 600; }
.btn.danger { color: var(--danger); border-color: rgba(248, 113, 113, 0.4); }
.file-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 0.3rem; }
/* View switcher — segmented control + sliding pill */
.view-tabs {
  position: relative;
  display: flex;
  align-items: stretch;
  gap: 0;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.25);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}
.tab-pill {
  position: absolute;
  top: 3px;
  bottom: 3px;
  left: 3px;
  width: 0;
  border-radius: 999px;
  background: linear-gradient(145deg, rgba(91, 159, 212, 0.35), rgba(91, 159, 212, 0.12));
  border: 1px solid rgba(91, 159, 212, 0.45);
  box-shadow: 0 0 16px rgba(91, 159, 212, 0.2);
  transition:
    transform 0.38s cubic-bezier(0.22, 1, 0.36, 1),
    width 0.38s cubic-bezier(0.22, 1, 0.36, 1);
  pointer-events: none;
  z-index: 0;
}
.tab {
  position: relative;
  z-index: 1;
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 0.4rem 0.85rem;
  font-size: 0.85rem;
  cursor: pointer;
  border-radius: 999px;
  transition: color 0.25s ease;
}
.tab:hover { color: var(--text); }
.tab.on { color: var(--text); font-weight: 600; }

.layout {
  display: grid; grid-template-columns: 1fr minmax(260px, 320px);
  flex: 1;
  min-height: 0;
}
@media (max-width: 800px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 1fr 45%; }
}

.stage {
  position: relative;
  overflow: hidden;
  min-height: 0;
  background: var(--bg);
}
/* Crossfade stack — both views live in the stage */
.view {
  position: absolute;
  inset: 0;
  height: 100%;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateY(10px) scale(0.985);
  filter: blur(0);
  transition:
    opacity 0.4s cubic-bezier(0.22, 1, 0.36, 1),
    transform 0.45s cubic-bezier(0.22, 1, 0.36, 1),
    visibility 0.4s,
    filter 0.35s ease;
  will-change: opacity, transform;
}
.view.is-active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translateY(0) scale(1);
  z-index: 1;
}
.view.is-exit-left {
  opacity: 0;
  transform: translateX(-18px) scale(0.98);
}
.view.is-exit-right {
  opacity: 0;
  transform: translateX(18px) scale(0.98);
}
.view.is-enter-from-left {
  transform: translateX(-14px) scale(0.99);
}
.view.is-enter-from-right {
  transform: translateX(14px) scale(0.99);
}
/* Brief stage wash on switch */
.stage.view-flash::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  background: radial-gradient(ellipse 80% 60% at 50% 40%, rgba(91, 159, 212, 0.14), transparent 70%);
  animation: stageFlash 0.55s ease-out forwards;
}
@keyframes stageFlash {
  0% { opacity: 0.85; }
  100% { opacity: 0; }
}
.mode-badge {
  position: absolute;
  top: 12px;
  left: 50%;
  z-index: 6;
  transform: translateX(-50%) translateY(-6px);
  padding: 0.28rem 0.75rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text);
  background: rgba(18, 26, 43, 0.85);
  border: 1px solid rgba(91, 159, 212, 0.4);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.35s cubic-bezier(0.22, 1, 0.36, 1);
}
.mode-badge.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.hidden { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  .view, .tab-pill, .mode-badge, .viewport .world, .mnode {
    transition: none !important;
    animation: none !important;
  }
  .stage.view-flash::after { display: none; }
}

/* List */
.view-list { overflow: auto; padding: 0.75rem 1rem 2rem; }
.list-toolbar { margin: 0 0 0.7rem; }
.filter-label { display: block; max-width: 32rem; }
#listFilter {
  width: 100%;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.45rem 0.65rem;
  font-size: 0.88rem;
}
/* List density — slides in only for List mode */
.density-tabs {
  margin-left: 0.15rem;
  max-width: 0;
  opacity: 0;
  overflow: hidden;
  pointer-events: none;
  transform: translateX(-8px) scale(0.96);
  filter: blur(2px);
  transition:
    max-width 0.4s cubic-bezier(0.22, 1, 0.36, 1),
    opacity 0.32s ease,
    transform 0.38s cubic-bezier(0.22, 1, 0.36, 1),
    filter 0.32s ease,
    margin 0.35s ease;
  margin-inline-start: 0;
}
.density-tabs.is-visible {
  max-width: 14rem;
  opacity: 1;
  pointer-events: auto;
  transform: translateX(0) scale(1);
  filter: blur(0);
  margin-inline-start: 0.15rem;
}
@media (prefers-reduced-motion: reduce) {
  .density-tabs {
    transition: opacity 0.15s ease;
    transform: none;
    filter: none;
  }
}
.list-master {
  margin-bottom: 0.65rem; border: 1px solid var(--border); border-radius: 10px;
  background: var(--panel); overflow: hidden;
}
.list-row {
  display: flex; align-items: center; gap: 0.45rem; padding: 0.5rem 0.65rem;
  cursor: pointer; border-left: 3px solid transparent;
}
.list-row:hover { background: rgba(255,255,255,0.03); }
.list-row.selected { background: rgba(91, 159, 212, 0.12); }
.list-row .twisty {
  width: 1.25rem; border: none; background: transparent; color: var(--muted);
  cursor: pointer; font-size: 0.75rem;
}
.list-row .list-code {
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Menlo, monospace;
  font-size: 0.72rem; font-weight: 700; min-width: 1.5rem; letter-spacing: 0.02em;
}
.list-row .dot {
  width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0;
}
.list-row .lab { flex: 1; font-size: 0.9rem; font-weight: 500; }
.list-row .meta { font-size: 0.72rem; color: var(--muted); }
.list-children { padding-left: 0.5rem; }
.list-children.collapsed { display: none; }
.list-node { border-left: 1px solid var(--border); margin-left: 0.85rem; }
.list-sub { margin-left: 1.1rem; border-left: 1px dashed var(--border); }
.list-shared { margin-left: 0.85rem; border-left: 2px dashed rgba(91, 159, 212, 0.45); }
.list-row.is-shared { background: rgba(91, 159, 212, 0.06); }
.list-row .meta-shared {
  color: var(--accent);
  font-weight: 600;
  letter-spacing: 0.02em;
}
.mnode.shared {
  border-style: dashed;
  box-shadow: 0 0 0 1px rgba(91, 159, 212, 0.35), 0 8px 24px rgba(0, 0, 0, 0.35);
}
.check-field .check-line {
  display: flex;
  align-items: flex-start;
  gap: 0.45rem;
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
}
.check-field input { margin-top: 0.15rem; }
.field-hint { font-size: 0.72rem; color: var(--muted); margin-top: 0.25rem; display: block; }
.inherited-shared {
  margin-top: 0.75rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--border);
}
.inherited-shared .kicker { margin-bottom: 0.35rem; }
.inherit-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  width: 100%;
  text-align: left;
  background: rgba(91, 159, 212, 0.08);
  border: 1px solid rgba(91, 159, 212, 0.25);
  border-radius: 8px;
  color: var(--text);
  padding: 0.4rem 0.5rem;
  margin-bottom: 0.35rem;
  font-size: 0.82rem;
  cursor: pointer;
}
.inherit-row:hover { border-color: var(--accent); }
.inherit-row .dot {
  width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
}
.inherit-row em {
  margin-left: auto;
  font-style: normal;
  font-size: 0.68rem;
  color: var(--accent);
  font-weight: 600;
}

/* Terminal / CLI list density */
.list-root.cli {
  font-family: ui-monospace, "SF Mono", "Cascadia Code", "Consolas", Menlo, monospace;
  padding: 0.35rem 0.25rem 1.5rem;
}
.cli-tree {
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.65rem 0.75rem 0.85rem;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}
.cli-head {
  margin-bottom: 0.45rem;
  font-size: 0.72rem;
}
.cli-muted { color: var(--muted); opacity: 0.9; }
.cli-empty {
  margin: 0;
  color: var(--muted);
  font-family: inherit;
  font-size: 0.85rem;
  line-height: 1.5;
}
.cli-row {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.35rem 0.5rem;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  border-radius: 4px;
  color: var(--text);
  font: inherit;
  font-size: 0.82rem;
  line-height: 1.45;
  padding: 0.18rem 0.35rem;
  cursor: pointer;
}
.cli-row:hover { background: rgba(255, 255, 255, 0.04); }
.cli-row.selected {
  background: rgba(91, 159, 212, 0.12);
  box-shadow: inset 2px 0 0 var(--accent);
}
.cli-code {
  flex: 0 0 1.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.cli-prefix {
  color: var(--muted);
  white-space: pre;
  flex: 0 0 auto;
}
.cli-kind {
  flex: 0 0 3.6rem;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--muted);
  text-transform: uppercase;
}
.cli-row .cli-kind:first-of-type { /* keep */ }
.cli-tree .cli-row[data-shared="1"] .cli-kind {
  color: var(--accent);
}
.cli-name {
  font-weight: 600;
  flex: 1 1 auto;
  min-width: 6rem;
}
.cli-meta {
  flex: 1 1 100%;
  padding-left: 2.25rem;
  font-size: 0.72rem;
  color: var(--muted);
  opacity: 0.85;
}
@media (min-width: 720px) {
  .cli-meta {
    flex: 1 1 auto;
    padding-left: 0;
    text-align: right;
  }
}
.cli-gap {
  color: var(--muted);
  opacity: 0.35;
  padding: 0.15rem 0.35rem;
  font-size: 0.7rem;
  user-select: none;
}

/* Map */
.view-map { height: 100%; }
.viewport {
  height: 100%; position: relative; overflow: hidden; cursor: grab;
  background:
    radial-gradient(ellipse 70% 50% at 50% 40%, rgba(91, 159, 212, 0.07), transparent 60%),
    radial-gradient(circle, rgba(255,255,255,0.03) 1px, transparent 1px);
  background-size: auto, 28px 28px;
  touch-action: none;
  transition: perspective 0.5s ease;
}
.viewport:active { cursor: grabbing; }
.viewport.mode-3d { perspective: 900px; }
.world {
  position: absolute; left: 0; top: 0; transform-origin: 0 0; will-change: transform;
  transition: filter 0.45s ease;
}
.mode-3d .world { transform-style: preserve-3d; }
.viewport.mode-switch-pulse .world {
  filter: brightness(1.12) saturate(1.15);
}
.edges { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
.edges line {
  stroke-width: 2;
  transition: stroke 0.35s ease, stroke-opacity 0.35s ease;
}
.edges line.dashed { stroke-dasharray: 6 5; }
.nodes { position: absolute; left: 0; top: 0; transform-style: preserve-3d; }
.mnode {
  position: absolute; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  text-align: center; padding: 0.3rem; cursor: pointer; border: 2px solid rgba(255,255,255,0.2);
  box-shadow: 0 8px 24px rgba(0,0,0,0.35); transform: translate(-50%, -50%);
  font-size: 0.72rem; font-weight: 600; line-height: 1.15; user-select: none;
  transition:
    box-shadow 0.3s ease,
    border-color 0.3s ease,
    transform 0.45s cubic-bezier(0.22, 1, 0.36, 1);
}
.mnode.master { width: 108px; height: 108px; font-size: 0.8rem; }
.mnode.node { width: 78px; height: 78px; }
.mnode.sub { width: 58px; height: 58px; font-size: 0.65rem; }
.mnode.selected { box-shadow: 0 0 0 2px var(--accent), 0 12px 28px rgba(0,0,0,0.45); z-index: 2; }
.mnode.pop-in {
  animation: nodePop 0.45s cubic-bezier(0.22, 1, 0.36, 1) both;
}
@keyframes nodePop {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.7); }
  100% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}
.viewport.mode-3d .mnode {
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.45), 0 0 0 1px rgba(255, 255, 255, 0.08);
}
.hint {
  position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%);
  font-size: 0.72rem; color: var(--muted); pointer-events: none; white-space: nowrap;
}

/* Panel */
.panel {
  border-left: 1px solid var(--border); background: var(--panel);
  padding: 0.85rem 1rem; overflow: auto;
}
@media (max-width: 800px) {
  .panel { border-left: none; border-top: 1px solid var(--border); }
}
.kicker { margin: 0; font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--accent); }
.panel h2 { margin: 0.2rem 0 0.35rem; font-size: 1.15rem; }
.muted { color: var(--muted); }
.small { font-size: 0.78rem; }
.field { display: flex; flex-direction: column; gap: 0.25rem; margin-bottom: 0.55rem; font-size: 0.78rem; color: var(--muted); }
.field em { font-style: normal; opacity: 0.75; }
.field textarea { resize: vertical; min-height: 3.5rem; }
.swatches { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.swatch {
  width: 26px; height: 26px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0;
  font-size: 0.65rem; font-weight: 700; font-family: ui-monospace, Menlo, monospace;
  display: inline-flex; align-items: center; justify-content: center;
}
.swatch.on { border-color: #fff; box-shadow: 0 0 0 1px var(--accent); }
.form-actions { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 0.65rem 0; }
.help p { font-size: 0.85rem; color: var(--muted); margin: 0.4rem 0; }
.help strong { color: var(--text); }
hr { border: none; border-top: 1px solid var(--border); margin: 0.85rem 0; }
