:root {
  --bg0: #e8efe4;
  --bg1: #f4efe4;
  --ink: #1b2a1c;
  --ink-soft: #3d4f3c;
  --muted: #6a7464;
  --line: #7a8c6f;
  --line-strong: #4f6244;
  --paper: #fffaf2;
  --paper-edge: #d9d2c3;
  --root: #1f3b28;
  --root-ink: #f6f1e6;
  --gold: #c4921a;
  --moss: #2f8a62;
  --stone: #a8a094;
  --focus: #2a6a45;
  --shadow: 0 10px 28px rgba(40, 52, 30, 0.12);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", sans-serif;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  color: var(--ink);
  font-family: var(--sans);
  background:
    radial-gradient(1000px 700px at 50% 0%, #f8f4ea 0%, transparent 60%),
    radial-gradient(900px 800px at 50% 80%, #dce8d6 0%, transparent 55%),
    linear-gradient(180deg, var(--bg1), var(--bg0));
  line-height: 1.45;
}

.skip {
  position: absolute;
  left: -999px;
  top: 0;
  background: var(--paper);
  color: var(--ink);
  padding: 8px 12px;
  z-index: 20;
}
.skip:focus { left: 8px; }

.top {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px 24px;
  padding: 20px 24px 12px;
}
.top-inner { max-width: 44rem; }
h1 {
  margin: 0;
  font-family: var(--serif);
  font-weight: 600;
  font-size: clamp(1.8rem, 3vw, 2.5rem);
  letter-spacing: -0.02em;
  line-height: 1.15;
}
.tagline {
  margin: 6px 0 10px;
  color: var(--ink-soft);
  font-size: 0.98rem;
}
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  list-style: none;
  margin: 0;
  padding: 0;
  color: var(--muted);
  font-size: 0.82rem;
}
.legend li { display: flex; align-items: center; gap: 6px; }
.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  display: inline-block;
  box-shadow: 0 0 0 3px rgba(255,250,242,0.7);
}
.dot.not-started { background: var(--stone); }
.dot.in-progress { background: var(--gold); }
.dot.complete { background: var(--moss); }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.tool-btn {
  appearance: none;
  border: 1px solid var(--paper-edge);
  background: var(--paper);
  color: var(--ink-soft);
  font: inherit;
  font-size: 0.85rem;
  padding: 6px 10px;
  border-radius: 999px;
  cursor: pointer;
  min-width: 2rem;
}
.tool-btn:hover { border-color: var(--line); color: var(--ink); }
.tool-btn:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.stage {
  display: grid;
  grid-template-columns: 1fr;
  height: calc(100vh - 8.5rem);
  min-height: 420px;
  padding: 0 12px 12px;
  gap: 12px;
}
.stage.with-detail {
  grid-template-columns: 1fr min(380px, 38vw);
}

.viewport {
  position: relative;
  overflow: hidden;
  border-radius: 22px;
  border: 1px solid rgba(80, 96, 70, 0.18);
  background:
    radial-gradient(circle at 50% 52%, rgba(255,250,242,0.65), transparent 42%),
    linear-gradient(180deg, rgba(255,255,255,0.28), rgba(220,232,214,0.2));
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.6);
  cursor: grab;
  touch-action: none;
}
.viewport:active { cursor: grabbing; }
.viewport:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.world {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0;
  height: 0;
  transform-origin: 0 0;
}
.links {
  position: absolute;
  overflow: visible;
  pointer-events: none;
}
.link-path {
  fill: none;
  stroke: var(--line);
  stroke-width: 2.2;
  stroke-linecap: round;
  opacity: 0.9;
}
.link-path.strong { stroke: var(--line-strong); stroke-width: 2.6; }

.nodes { position: absolute; inset: 0; }

.node {
  scroll-margin-top: 12px;
  scroll-margin-bottom: 42vh;
  position: absolute;
  transform: translate(-50%, -50%);
  border: 1px solid var(--paper-edge);
  background: var(--paper);
  color: var(--ink);
  box-shadow: var(--shadow);
  cursor: pointer;
  text-align: left;
  font: inherit;
  padding: 10px 14px 10px 16px;
  min-width: 160px;
  max-width: 230px;
  transition: transform 0.35s var(--ease), box-shadow 0.25s var(--ease), border-color 0.2s ease;
}
.node:hover { border-color: var(--line); }
.node:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 3px;
}
.node.is-selected {
  border-color: var(--root);
  box-shadow: 0 0 0 3px rgba(31, 59, 40, 0.16), var(--shadow);
}
.node-label {
  display: block;
  font-weight: 650;
  font-size: 0.92rem;
  line-height: 1.3;
  padding-right: 14px;
}
.node-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  color: var(--muted);
  font-size: 0.72rem;
}
.node-count {
  background: #eef3ea;
  color: var(--ink-soft);
  border-radius: 999px;
  padding: 0 6px;
  font-variant-numeric: tabular-nums;
}

.node-root {
  min-width: 148px;
  max-width: 180px;
  width: 158px;
  height: 158px;
  border-radius: 50%;
  background: var(--root);
  color: var(--root-ink);
  border: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 18px 16px;
  box-shadow: 0 16px 40px rgba(20, 40, 24, 0.28);
}
.node-root .node-label {
  font-family: var(--serif);
  font-size: 1.2rem;
  font-weight: 600;
  padding: 0;
  letter-spacing: -0.02em;
}
.node-root .node-meta { color: rgba(246, 241, 230, 0.78); justify-content: center; }
.node-root .dot { box-shadow: 0 0 0 3px rgba(31, 59, 40, 0.4); }
.node-root.is-selected { box-shadow: 0 0 0 4px #f4efe4, 0 16px 40px rgba(20, 40, 24, 0.28); }

.branch-card { border-radius: 18px; }

/* Mobile vertical tree */
.mobile-tree {
  padding: 16px 14px 8rem;
  overflow: auto;
  height: 100%;
  cursor: auto;
  touch-action: auto;
}
.m-root {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 8px;
}
.m-root .node-root {
  position: static;
  transform: none;
  cursor: default;
}
.m-list { list-style: none; margin: 0; padding: 0; }
.m-item { position: relative; padding-left: 22px; }
.m-item::before {
  content: "";
  position: absolute;
  left: 8px;
  top: 0;
  bottom: 0;
  width: 1px;
  background: var(--line);
}
.m-item:last-child::before { bottom: auto; height: 22px; }
.m-item::after {
  content: "";
  position: absolute;
  left: 8px;
  top: 22px;
  width: 14px;
  height: 1px;
  background: var(--line);
}
.m-list > .m-item > .node {
  position: relative;
  transform: none;
  width: 100%;
  max-width: none;
  margin: 6px 0;
  display: block;
}
.m-children { list-style: none; margin: 0; padding: 0; }

.detail {
  background: var(--paper);
  border: 1px solid var(--paper-edge);
  border-radius: 22px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
.detail[hidden] { display: none !important; }
.detail-bar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 16px 8px;
  border-bottom: 1px solid #eee6d8;
}
.detail-bar h2 {
  margin: 0;
  font-family: var(--serif);
  font-size: 1.25rem;
  font-weight: 600;
  line-height: 1.25;
}
.detail-body {
  padding: 12px 16px 20px;
  overflow: auto;
  color: var(--ink-soft);
  font-size: 0.94rem;
}
.detail-body p { margin: 0 0 10px; }
.detail-status {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 0.82rem;
}
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 12px 0 4px;
}
.chip {
  display: inline-flex;
  flex-direction: column;
  gap: 2px;
  max-width: 100%;
  padding: 8px 12px;
  border-radius: 12px;
  background: #eef3ea;
  color: var(--ink);
  text-decoration: none;
  border: 1px solid transparent;
  font-size: 0.82rem;
}
a.chip:hover { border-color: var(--line); }
.chip .chip-title { font-weight: 650; }
.chip .chip-meta { color: var(--muted); font-size: 0.75rem; }
.chip.is-file { background: #f3eee4; }

.mini-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
  margin: 10px 0 14px;
}
.mini-table th, .mini-table td {
  text-align: left;
  vertical-align: top;
  padding: 6px 8px;
  border-bottom: 1px solid #eee6d8;
}
.mini-table th {
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
}

.load-error {
  margin: 16px 24px;
  color: #8a2f2f;
}

@media (max-width: 720px) {
  .top { padding: 16px 14px 8px; }
  .toolbar .tool-btn[id^="btn-zoom"],
  #btn-zoom-reset { display: none; }
  .stage, .stage.with-detail {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 0;
  }
  .viewport {
    height: auto;
    min-height: 60vh;
    overflow: visible;
    cursor: auto;
  }
  .world { display: none; }
  .stage.with-detail .viewport { min-height: 40vh; }
  .detail {
    position: sticky;
    bottom: 0;
    max-height: 36vh;
    z-index: 5;
  }
}

@media (prefers-reduced-motion: reduce) {
  .node, .link-path { transition: none !important; }
}
