/* ─── Theme tokens ─────────────────────────────────────────────────────────
   Same architecture as Tendria: a `.theme-*` class on <html> sets the --th-*
   values, everything downstream references only the semantic names. Adding a
   scheme means adding one block here and one <option> in index.html.        */

.theme-slate {
  --th-paper: #12171a;
  --th-paper-hi: #171d21;
  --th-card: #1c2429;
  --th-card-hi: #232d33;
  --th-ink: #e4ecef;
  --th-ink-soft: #9aabb3;
  --th-ink-mute: #64757d;
  --th-accent: #5fbf82;
  --th-accent-ink: #0d1512;
  --th-accent-soft: rgba(95, 191, 130, 0.13);
  --th-rule: rgba(190, 220, 225, 0.09);
  --th-rule-strong: rgba(190, 220, 225, 0.2);
  --th-wire: rgba(95, 191, 130, 0.34);
  --th-danger: #e8705c;
}

.theme-espresso {
  --th-paper: #1c130a;
  --th-paper-hi: #22180f;
  --th-card: #2a2015;
  --th-card-hi: #322618;
  --th-ink: #ebe0cb;
  --th-ink-soft: #a89880;
  --th-ink-mute: #6b5d47;
  --th-accent: #d4a04a;
  --th-accent-ink: #1c130a;
  --th-accent-soft: rgba(212, 160, 74, 0.14);
  --th-rule: rgba(232, 200, 150, 0.09);
  --th-rule-strong: rgba(232, 200, 150, 0.2);
  --th-wire: rgba(212, 160, 74, 0.34);
  --th-danger: #e8825c;
}

.theme-onyx {
  --th-paper: #000000;
  --th-paper-hi: #0a0a0a;
  --th-card: #121019;
  --th-card-hi: #181522;
  --th-ink: #ffffff;
  --th-ink-soft: rgba(255, 255, 255, 0.66);
  --th-ink-mute: rgba(255, 255, 255, 0.42);
  --th-accent: #6e5df0;
  --th-accent-ink: #ffffff;
  --th-accent-soft: rgba(110, 93, 240, 0.16);
  --th-rule: rgba(255, 255, 255, 0.08);
  --th-rule-strong: rgba(255, 255, 255, 0.18);
  --th-wire: rgba(110, 93, 240, 0.45);
  --th-danger: #ff7a6b;
}

.theme-paper {
  --th-paper: #f2f5f4;
  --th-paper-hi: #ffffff;
  --th-card: #ffffff;
  --th-card-hi: #eaf0ee;
  --th-ink: #16201d;
  --th-ink-soft: #4d5d58;
  --th-ink-mute: #8698a0;
  --th-accent: #1f8a55;
  --th-accent-ink: #ffffff;
  --th-accent-soft: rgba(31, 138, 85, 0.1);
  --th-rule: rgba(20, 45, 40, 0.1);
  --th-rule-strong: rgba(20, 45, 40, 0.2);
  --th-wire: rgba(31, 138, 85, 0.38);
  --th-danger: #c2452f;
}

:root {
  --gap-x: 56px; /* horizontal run between a card and its children */
  --gap-y: 10px; /* vertical breathing room between siblings          */
  --radius: 14px;
  --wire-radius: 14px; /* corner radius on the connector elbows       */
  --bar-h: 52px;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  background: var(--th-paper);
  color: var(--th-ink);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  cursor: pointer;
  border: 0;
  background: none;
  color: inherit;
}

/* ─── Top bar ─────────────────────────────────────────────────────────────*/

.bar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--bar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 14px;
  background: color-mix(in srgb, var(--th-paper) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--th-rule);
  z-index: 40;
}

.bar-mark {
  font-size: 15px;
  letter-spacing: 0.02em;
  color: var(--th-accent);
}
.bar-mark span {
  color: var(--th-ink);
  font-weight: 600;
  margin-left: 2px;
}

.bar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ghost {
  padding: 7px 13px;
  border-radius: 999px;
  border: 1px solid var(--th-rule-strong);
  color: var(--th-ink-soft);
  font-size: 13px;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.ghost:hover {
  color: var(--th-accent);
  border-color: var(--th-accent);
  background: var(--th-accent-soft);
}

.theme-pick {
  appearance: none;
  padding: 7px 11px;
  border-radius: 999px;
  border: 1px solid var(--th-rule-strong);
  background: var(--th-card);
  color: var(--th-ink-soft);
  font: inherit;
  font-size: 13px;
}

/* ─── Canvas ──────────────────────────────────────────────────────────────*/

.canvas {
  position: absolute;
  inset: var(--bar-h) 0 0 0;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  cursor: grab;
}
.canvas.panning {
  cursor: grabbing;
  user-select: none;
}

.wires {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  overflow: visible;
  z-index: 0;
}

.rows {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  gap: 18px;
  align-items: flex-start;
  padding: 28px 120px 160px 24px;
  width: max-content;
}

/* A branch is [card][children]; centring the card against the children column
   is what gives the spidering-out look rather than a flat indent list.      */
.branch {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: var(--gap-x);
}

.kids {
  display: flex;
  flex-direction: column;
  gap: var(--gap-y);
}
.kids:empty {
  display: none;
}

/* ─── Cards ───────────────────────────────────────────────────────────────*/

.card {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 210px;
  max-width: 300px;
  padding: 10px 12px;
  border-radius: var(--radius);
  background: var(--th-card);
  border: 1px solid var(--th-rule);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.18);
  transition: border-color 0.15s, background 0.15s, transform 0.12s;
}
.card:hover {
  background: var(--th-card-hi);
  border-color: var(--th-rule-strong);
}
.card.is-target {
  border-color: var(--th-accent);
  box-shadow: 0 0 0 2px var(--th-accent-soft);
}

/* A colour rail on the left edge is the only kind cue — cheaper to read at a
   glance than an icon, and it survives the card being narrow.              */
.card::before {
  content: "";
  position: absolute;
  left: 0;
  top: 10px;
  bottom: 10px;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--th-ink-mute);
}
.card.kind-category::before {
  background: var(--th-accent);
}
.card.kind-node::before {
  background: color-mix(in srgb, var(--th-accent) 55%, var(--th-ink-mute));
}
.card.kind-note::before {
  background: var(--th-ink-soft);
}
.card.kind-doc::before {
  background: var(--th-ink-mute);
}

.card.kind-category {
  min-width: 230px;
  padding: 13px 14px;
  background: var(--th-card-hi);
}
.card.kind-category .label {
  font-size: 15px;
  font-weight: 650;
}

.card.leaf {
  cursor: pointer;
}

.label {
  flex: 1;
  min-width: 0;
  font-size: 13.5px;
  line-height: 1.35;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.label.empty {
  color: var(--th-ink-mute);
  font-style: italic;
}

.twist {
  flex: none;
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  color: var(--th-ink-mute);
  font-size: 10px;
  transition: transform 0.18s ease, color 0.15s;
}
.twist:hover {
  color: var(--th-accent);
  background: var(--th-accent-soft);
}
.twist.open {
  transform: rotate(90deg);
}
.twist.none {
  visibility: hidden;
}

.count {
  flex: none;
  font-size: 11px;
  color: var(--th-ink-mute);
  font-variant-numeric: tabular-nums;
}

.plus {
  flex: none;
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  border: 1px dashed var(--th-rule-strong);
  color: var(--th-ink-mute);
  font-size: 15px;
  line-height: 1;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.plus:hover {
  color: var(--th-accent);
  border-color: var(--th-accent);
  border-style: solid;
  background: var(--th-accent-soft);
}

.empty-hint {
  color: var(--th-ink-mute);
  font-size: 14px;
  padding: 40px 8px;
  line-height: 1.6;
}

/* ─── Modals ──────────────────────────────────────────────────────────────*/

.modal-scrim {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(3px);
}
.modal-scrim[hidden] {
  display: none;
}

.sheet {
  width: min(520px, 100%);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px;
  border-radius: 18px;
  background: var(--th-card);
  border: 1px solid var(--th-rule-strong);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}
.sheet-sm {
  width: min(420px, 100%);
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.sheet-head h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
}

.x {
  width: 30px;
  height: 30px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 9px;
  font-size: 20px;
  line-height: 1;
  color: var(--th-ink-mute);
}
.x:hover {
  color: var(--th-ink);
  background: var(--th-card-hi);
}

.pick-grid {
  display: grid;
  gap: 8px;
}
.pick {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 13px 15px;
  border-radius: 12px;
  text-align: left;
  background: var(--th-card-hi);
  border: 1px solid var(--th-rule);
  transition: border-color 0.15s, background 0.15s;
}
.pick:hover {
  border-color: var(--th-accent);
  background: var(--th-accent-soft);
}
.pick b {
  font-size: 14px;
  font-weight: 600;
}
.pick span {
  font-size: 12px;
  color: var(--th-ink-mute);
}

.field {
  width: 100%;
  padding: 11px 13px;
  border-radius: 11px;
  background: var(--th-paper-hi);
  border: 1px solid var(--th-rule-strong);
  color: var(--th-ink);
  font: inherit;
  font-size: 14px;
}
.field:focus {
  outline: none;
  border-color: var(--th-accent);
}

.sheet-foot {
  display: flex;
  justify-content: flex-end;
}
.primary {
  padding: 9px 20px;
  border-radius: 999px;
  background: var(--th-accent);
  color: var(--th-accent-ink);
  font-size: 13.5px;
  font-weight: 600;
}

.move-list {
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.move-list button {
  text-align: left;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 13px;
  color: var(--th-ink-soft);
  border: 1px solid transparent;
}
.move-list button:hover {
  background: var(--th-accent-soft);
  border-color: var(--th-accent);
  color: var(--th-ink);
}
.move-list .crumb {
  color: var(--th-ink-mute);
}
.move-list .snippet {
  display: block;
  margin-top: 3px;
  font-size: 12px;
  color: var(--th-ink-mute);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Search opens near the top rather than dead-centre: the result list grows
   downwards, so a centred sheet would jump as you type. */
.search-scrim {
  align-items: start;
  padding-top: 12vh;
}
.search-scrim .sheet {
  max-height: 70vh;
}
.search-scrim .sheet-head .field {
  flex: 1;
}

/* ─── Editors (full-screen) ───────────────────────────────────────────────*/

.editor-scrim {
  padding: 0;
  place-items: stretch;
}

.editor {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  background: var(--th-paper);
}

.editor-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  height: var(--bar-h);
  padding: 0 12px 0 20px;
  border-bottom: 1px solid var(--th-rule);
}
.editor-kind {
  font-size: 11px;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--th-accent);
}
.saved {
  flex: 1;
  font-size: 12px;
  color: var(--th-ink-mute);
}

.doc-title {
  flex: none;
  width: min(760px, 100%);
  margin: 26px auto 0;
  padding: 0 20px;
  background: none;
  border: 0;
  color: var(--th-ink);
  font: inherit;
  font-size: 26px;
  font-weight: 650;
}
.doc-title:focus {
  outline: none;
}

.doc-body {
  flex: 1;
  width: min(760px, 100%);
  margin: 14px auto 0;
  padding: 0 20px 40px;
  background: none;
  border: 0;
  resize: none;
  color: var(--th-ink-soft);
  font: inherit;
  font-size: 15.5px;
  line-height: 1.7;
}
.doc-body:focus {
  outline: none;
}

.bullets {
  flex: 1;
  overflow: auto;
  width: min(760px, 100%);
  margin: 0 auto;
  padding: 26px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.bullet {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 3px 0;
}
.tick {
  flex: none;
  width: 22px;
  height: 26px;
  display: grid;
  place-items: center;
  color: var(--th-ink-mute);
  font-size: 13px;
  border-radius: 6px;
}
.tick:hover {
  color: var(--th-accent);
  background: var(--th-accent-soft);
}
.tick.done {
  color: var(--th-accent);
}

.bullet textarea {
  flex: 1;
  background: none;
  border: 0;
  resize: none;
  overflow: hidden;
  color: var(--th-ink);
  font: inherit;
  font-size: 15.5px;
  line-height: 1.65;
  padding: 2px 0;
}
.bullet textarea:focus {
  outline: none;
}
.bullet.done textarea {
  color: var(--th-ink-mute);
  text-decoration: line-through;
}

.editor-foot {
  flex: none;
  display: flex;
  align-items: center;
  gap: 14px;
  width: min(760px, 100%);
  margin: 0 auto;
  padding: 12px 20px 24px;
}
.hint {
  font-size: 12px;
  color: var(--th-ink-mute);
}

/* ─── Context menu ────────────────────────────────────────────────────────*/

.menu {
  position: fixed;
  z-index: 70;
  min-width: 160px;
  padding: 5px;
  border-radius: 12px;
  background: var(--th-card);
  border: 1px solid var(--th-rule-strong);
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.45);
  display: flex;
  flex-direction: column;
}
.menu[hidden] {
  display: none;
}
.menu button {
  text-align: left;
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--th-ink-soft);
}
.menu button:hover {
  background: var(--th-card-hi);
  color: var(--th-ink);
}
.menu .danger:hover {
  color: var(--th-danger);
}

/* ─── Login ───────────────────────────────────────────────────────────────*/

.gate-body {
  display: grid;
  place-items: center;
}
.gate {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(320px, calc(100vw - 40px));
  text-align: center;
}
.gate-mark {
  font-size: 34px;
  color: var(--th-accent);
}
.gate h1 {
  margin: 0 0 6px;
  font-size: 19px;
  font-weight: 600;
  letter-spacing: 0.01em;
}
.gate input {
  padding: 12px 14px;
  border-radius: 11px;
  background: var(--th-card);
  border: 1px solid var(--th-rule-strong);
  color: var(--th-ink);
  font: inherit;
  text-align: center;
}
.gate input:focus {
  outline: none;
  border-color: var(--th-accent);
}
.gate button {
  padding: 12px;
  border-radius: 11px;
  background: var(--th-accent);
  color: var(--th-accent-ink);
  font-weight: 600;
}
.gate-err {
  margin: 0;
  font-size: 13px;
  color: var(--th-danger);
}

/* ─── Narrow screens ──────────────────────────────────────────────────────
   The tree stays left-to-right and pans horizontally; only the chrome and the
   card widths tighten. Keeping one layout means the phone and the desktop
   share a mental model (and, later, one native screen).                    */

@media (max-width: 720px) {
  :root {
    --gap-x: 34px;
  }
  .card,
  .card.kind-category {
    min-width: 172px;
    max-width: 210px;
  }
  .rows {
    padding: 18px 90px 120px 14px;
  }
  /* The bar has to survive 390px: drop both text labels, keep the affordances. */
  .bar-mark span,
  #add-category span,
  #open-search span {
    display: none;
  }
  .bar {
    padding: 0 10px;
  }
  .theme-pick {
    max-width: 104px;
    padding: 7px 9px;
  }
  .doc-title {
    font-size: 21px;
  }
  .hint {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
