/* Thicket
   Layout: a single column of type. The open term is the hero at the top, its
   connections read as lines beneath, and one prompt line stays pinned to the
   bottom. On wide screens the list rides in a left column; on phones it slides in.
   Brackets are the only ornament: they mark everything you can open. */

:root {
  --bg: #eef0ea;          /* pale lichen paper */
  --surface: #e3e6dd;
  --fg: #1c2018;          /* bark ink */
  --muted: #687160;
  --line: #c8cdbf;
  --accent: #8d5a07;      /* resin */
  --accent-soft: rgba(141, 90, 7, 0.12);
  --danger: #a33a22;
  --scrim: rgba(28, 32, 24, 0.38);

  --f-hero: 'Martian Mono', 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --f-text: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;

  --s-1: 0.75rem;
  --s0: 0.875rem;
  --s1: 1rem;
  --s2: 1.25rem;
  --hero: clamp(2rem, 6.4vw, 4.25rem);
  --gutter: 16px;
  --radius: 3px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121510;
    --surface: #1a1e17;
    --fg: #dde2d3;
    --muted: #8b947f;
    --line: #2d3428;
    --accent: #e2aa48;
    --accent-soft: rgba(226, 170, 72, 0.14);
    --danger: #e98067;
    --scrim: rgba(0, 0, 0, 0.55);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121510;
  --surface: #1a1e17;
  --fg: #dde2d3;
  --muted: #8b947f;
  --line: #2d3428;
  --accent: #e2aa48;
  --accent-soft: rgba(226, 170, 72, 0.14);
  --danger: #e98067;
  --scrim: rgba(0, 0, 0, 0.55);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 400 var(--s0)/1.6 var(--f-text);
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
::selection { background: var(--accent-soft); }
.dim { color: var(--muted); }
.warn { color: var(--accent); }
.small { font-size: var(--s-1); }
kbd, code {
  font-family: var(--f-text);
  font-size: 0.92em;
  padding: 0 0.3em;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}
[hidden] { display: none !important; }

/* ---------- frame ---------- */

#app { height: 100%; display: flex; flex-direction: column; }

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(8px + env(safe-area-inset-top, 0px)) var(--gutter) 8px;
  border-bottom: 1px solid var(--line);
}
.bar-acts { display: flex; flex-wrap: wrap; gap: 2px 14px; justify-content: flex-end; }
.brand { font-weight: 500; letter-spacing: 0.02em; }
.brand b, .ref b { color: var(--accent); font-weight: 400; }

/* text buttons: words that act */
.tx {
  background: none;
  border: 0;
  padding: 2px 0;
  color: var(--fg);
  text-decoration: underline;
  text-decoration-color: var(--line);
  text-underline-offset: 4px;
}
.tx:hover { text-decoration-color: currentColor; }
.tx.dim { color: var(--muted); }
.tx.accent { color: var(--accent); }
.tx.danger { color: var(--danger); }
.bar .tx { text-decoration: none; color: var(--muted); }
.bar .tx:hover, .bar .brand { color: var(--fg); }
#btn-save.dirty { color: var(--accent); }
#btn-save.dirty::after { content: ' •'; }

.body {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(250px, 300px) minmax(0, 1fr);
  position: relative;
}

/* ---------- list ---------- */

.list {
  border-right: 1px solid var(--line);
  overflow: auto;
  padding: 14px var(--gutter) 24px;
  min-width: 0;
}
.list-scrim { display: none; }
.list-head { display: grid; gap: 10px; margin-bottom: 12px; position: sticky; top: -14px; background: var(--bg); padding-block: 14px 8px; margin-top: -14px; }
.tabs, .sorts { display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: baseline; }
.tab {
  background: none;
  border: 0;
  padding: 2px 0;
  color: var(--muted);
  border-bottom: 1px solid transparent;
}
.tab.on { color: var(--fg); border-bottom-color: var(--accent); }
.tab .ct { color: var(--muted); font-size: var(--s-1); }
.sorts { font-size: var(--s-1); }
.filter {
  width: 100%;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--line);
  padding: 6px 0;
  outline: none;
}
.filter:focus { border-bottom-color: var(--accent); }
.filter::placeholder, .prompt::placeholder { color: var(--muted); opacity: 0.8; }

.items { list-style: none; margin: 0; padding: 0; }
.it {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 10px;
  align-items: baseline;
  padding: 3px 0;
  font-variant-numeric: tabular-nums;
}
.it .ln {
  background: none;
  border: 0;
  padding: 0;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.it .ln:hover { color: var(--accent); }
.it.cur .ln { color: var(--accent); }
.it.cur .ln::before { content: '› '; }
.it .ct, .it .dt { color: var(--muted); font-size: var(--s-1); }
.note { padding: 6px 0; }

/* levels: one axis, levels as headings, items beneath */
.lv-why { margin: 0 0 4px; color: var(--muted); font-size: var(--s-1); }
.lv-h {
  font-size: var(--s-1);
  font-weight: 500;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 16px 0 4px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--line);
}
.lv-h .ct { font-weight: 400; letter-spacing: 0; }
.lv-h .tx { font-weight: 400; letter-spacing: 0; margin-left: 6px; }
.lv-h + .lv-h { margin-top: 6px; }
.lv-under { margin: 6px 0 0; color: var(--muted); font-size: var(--s-1); }
.lv .ref { font-size: inherit; }
.lv .lv-under + .items { padding-left: 1ch; }
.lv-note { grid-column: 1 / -1; color: var(--muted); font-size: var(--s-1); padding-left: 1ch; }

/* ---------- hero ---------- */

.hero-wrap {
  overflow: auto;
  padding: clamp(20px, 5vh, 56px) var(--gutter) 40px;
  min-width: 0;
}
.node, .welcome { max-width: 46rem; margin: 0 auto; }
.eyebrow {
  margin: 0 0 6px;
  color: var(--muted);
  font-size: var(--s-1);
  letter-spacing: 0.06em;
  text-transform: lowercase;
}
.hero {
  font-family: var(--f-hero);
  font-weight: 300;
  font-size: var(--hero);
  line-height: 1.08;
  letter-spacing: -0.02em;
  margin: 0;
  text-wrap: balance;
  overflow-wrap: anywhere;
}
.acts { display: flex; flex-wrap: wrap; gap: 4px 16px; margin: 14px 0 0; font-size: var(--s-1); }
.acts .tx { color: var(--muted); }
.acts .tx.danger { color: var(--danger); }

.conn { margin-top: 34px; }
.conn h2 {
  font-size: var(--s-1);
  font-weight: 500;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 0 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--line);
}
.row {
  display: grid;
  grid-template-columns: minmax(5.5rem, 9rem) minmax(0, 1fr);
  gap: 4px 18px;
  padding: 6px 0;
  align-items: baseline;
}
.ty { color: var(--muted); text-align: right; overflow-wrap: anywhere; }
.ty .tx { color: inherit; text-align: inherit; }
.ty .tx:hover { color: var(--fg); }
.refs { display: flex; flex-wrap: wrap; gap: 4px 14px; min-width: 0; }
.link { display: inline-flex; align-items: baseline; gap: 2px; max-width: 100%; }
.ref {
  background: none;
  border: 0;
  padding: 0;
  font-size: var(--s1);
  text-align: left;
  overflow-wrap: anywhere;
}
.ref:hover { color: var(--accent); }
.ord { color: var(--muted); font-size: var(--s-1); margin-left: 2px; }
.x {
  background: none;
  border: 0;
  color: var(--muted);
  opacity: 0.55;
  padding: 0 4px;
  font-size: var(--s0);
  line-height: 1;
}
.x:hover, .x:focus-visible { opacity: 1; color: var(--danger); }

.welcome .guide { max-width: 38rem; margin-top: 22px; display: grid; gap: 10px; }
.welcome .guide p { margin: 0; }
.welcome .alt { margin-top: 26px; }

/* ---------- prompt ---------- */

.foot { border-top: 1px solid var(--line); background: var(--bg); padding-bottom: env(safe-area-inset-bottom, 0px); }
.status {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 12px;
  align-items: baseline;
  padding: 6px var(--gutter) 0;
  font-size: var(--s-1);
  min-height: 1.6em;
}
.prompt-row { display: flex; align-items: center; gap: 10px; padding: 4px var(--gutter) 10px; }
.caret { color: var(--accent); font-size: var(--s2); }
.prompt-box { position: relative; flex: 1; min-width: 0; }
.prompt {
  width: 100%;
  background: transparent;
  border: 0;
  outline: none;
  padding: 8px 0;
  font-size: var(--s1);
  caret-color: var(--accent);
}

.pop {
  position: absolute;
  left: -8px;
  right: 0;
  bottom: calc(100% + 6px);
  max-height: min(42vh, 22rem);
  overflow: auto;
  margin: 0;
  padding: 4px 0;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  z-index: 30;
  box-shadow: 0 -8px 24px -12px var(--scrim);
}
.pop.down { bottom: auto; top: calc(100% + 4px); box-shadow: 0 8px 24px -12px var(--scrim); }
.pop li {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 4px 10px;
  cursor: pointer;
}
.pop li .pl { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pop li .pl b { color: var(--accent); font-weight: 400; }
.pop li .ph { color: var(--muted); font-size: var(--s-1); flex: none; }
.pop li.on { background: var(--accent-soft); }
.pop li.on .pl::before { content: '› '; color: var(--accent); }
.pop li.new .pl { font-style: italic; }

.ask {
  padding: 10px var(--gutter);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.ask p { margin: 0 0 6px; }
.ask .opts { display: flex; flex-wrap: wrap; gap: 6px 18px; }
.ask .opt .k { color: var(--muted); margin-right: 6px; font-size: var(--s-1); }

/* ---------- compact keyboard (touch) ---------- */

.kb {
  padding: 4px 4px 6px;
  border-top: 1px solid var(--line);
  background: var(--surface);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.kb-row { display: flex; gap: 4px; margin-top: 4px; justify-content: center; }
.kb-k {
  flex: 1 1 0;
  max-width: 3rem;
  min-width: 0;
  height: 38px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg);
  font-size: var(--s1);
  -webkit-tap-highlight-color: transparent;
}
.kb-k.mod { color: var(--muted); font-size: var(--s0); flex-grow: 1.4; max-width: 4.2rem; }
.kb-k.wide { flex-grow: 4; max-width: none; }
.kb-k.br { color: var(--accent); }
.kb-k.on { color: var(--accent); border-color: var(--accent); }
.kb-k.down { background: var(--accent-soft); }

/* ---------- modal ---------- */

.modal {
  position: fixed;
  inset: 0;
  background: var(--scrim);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: max(5vh, env(safe-area-inset-top, 0px)) var(--gutter) var(--gutter);
  z-index: 50;
}
.modal-card {
  width: 100%;
  max-width: 46rem;
  max-height: 90%;
  overflow: auto;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px clamp(16px, 4vw, 28px) 18px;
}
.modal-card h2 { font-family: var(--f-hero); font-weight: 300; font-size: var(--s2); margin: 0 0 10px; letter-spacing: -0.01em; }
.modal-card h3 { font-size: var(--s-1); font-weight: 500; letter-spacing: 0.08em; color: var(--muted); margin: 20px 0 6px; }
.modal-card p { margin: 0 0 10px; max-width: 65ch; }
.btns { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
.btns.end { justify-content: flex-end; margin-bottom: 0; }
.btn {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px 12px;
}
.btn:hover { border-color: var(--fg); }
.btn.primary { border-color: var(--accent); color: var(--accent); }
.btn.primary:hover { background: var(--accent-soft); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 2px 8px; font-size: var(--s-1); }
.json {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px;
  font-size: var(--s-1);
  resize: vertical;
}
ul.plain { margin: 0 0 10px; padding-left: 1.2em; }
ul.plain li { margin: 2px 0; }
.conflicts { display: grid; gap: 6px; }
.cf { display: flex; flex-wrap: wrap; gap: 6px; }
.pick { background: none; border: 1px solid var(--line); border-radius: var(--radius); padding: 3px 10px; color: var(--muted); }
.pick.on { border-color: var(--accent); color: var(--fg); }

.help { display: grid; gap: 4px; }
.hr { display: grid; grid-template-columns: minmax(8rem, 13rem) minmax(0, 1fr); gap: 12px; align-items: baseline; }
.hr code { justify-self: start; }

.rep-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.rep-tabs { margin: 4px 0 10px; }
.rep-q { margin: 4px 0 10px; }
.rep-out {
  overflow-x: auto;
  border-top: 1px solid var(--line);
  padding-top: 10px;
  min-height: 6rem;
  font-variant-numeric: tabular-nums;
}
.rl { white-space: pre; line-height: 1.75; }
.rl .ref { font-size: inherit; }
.tbar { color: var(--accent); opacity: 0.75; letter-spacing: -0.08em; }

/* ---------- toast & drop ---------- */

.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  top: calc(52px + env(safe-area-inset-top, 0px));
  z-index: 60;
  display: flex;
  gap: 14px;
  align-items: baseline;
  max-width: calc(100% - 32px);
  padding: 8px 14px;
  background: var(--fg);
  color: var(--bg);
  border-radius: var(--radius);
  font-size: var(--s-1);
}
.toast .tx.accent { color: var(--bg); text-decoration-color: currentColor; font-weight: 600; }
body.dropping #app { outline: 2px dashed var(--accent); outline-offset: -8px; }

/* ---------- narrow screens ---------- */

@media (max-width: 820px) {
  .body { grid-template-columns: minmax(0, 1fr); }
  .list {
    position: absolute;
    inset: 0 auto 0 0;
    width: min(86vw, 340px);
    background: var(--bg);
    z-index: 20;
    transform: translateX(-102%);
    transition: transform 0.18s ease;
    border-right: 1px solid var(--line);
  }
  body.list-open .list { transform: none; }
  body.list-open .list-scrim { display: block; position: absolute; inset: 0; background: var(--scrim); z-index: 15; }
  .row { grid-template-columns: minmax(0, 1fr); gap: 2px; }
  .ty { text-align: left; font-size: var(--s-1); }
  .hr { grid-template-columns: minmax(0, 1fr); gap: 0; margin-bottom: 6px; }
  /* when the compact keyboard is up, give the hero less padding */
  body.kb-open .hero-wrap { padding-top: 14px; }
}
@media (min-width: 821px) {
  #btn-list { display: none; }
}

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

@media (max-width: 480px) {
  .wide-only { display: none; }
  .bar-acts { gap: 2px 12px; }
}
