:root {
  --paper: #E9EAEE;
  --surface: #F4F5F7;
  --surface-2: #DFE1E7;
  --rule: #C9CCD6;
  --rule-soft: #D8DAE2;
  --ink: #1B1D26;
  --ink-soft: #4A4E5E;
  --ink-faint: #7C8194;
  --accent: #4A56C4;

  --ok: #0E7C5F;
  --ok-bg: #D3EDE4;
  --moved: #35609E;
  --moved-bg: #D6E3F4;
  --ambiguous: #6B4FA1;
  --ambiguous-bg: #E6DCF4;
  --drifted: #A85708;
  --drifted-bg: #F7E3C9;
  --orphaned: #B03429;
  --orphaned-bg: #F6D8D5;

  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  --serif: ui-serif, "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --gutter: 3.25rem;
  --rail: 17rem;
  --gloss: 23rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #131519;
    --surface: #1A1D23;
    --surface-2: #23262E;
    --rule: #333846;
    --rule-soft: #272B34;
    --ink: #E4E6EC;
    --ink-soft: #A8AEBF;
    --ink-faint: #6E7488;
    --accent: #8F99EE;
    --ok: #3FD3A3;
    --ok-bg: #10312A;
    --moved: #7FAAEE;
    --moved-bg: #16243A;
    --ambiguous: #BA94E8;
    --ambiguous-bg: #2B1D3D;
    --drifted: #EDA34C;
    --drifted-bg: #34240F;
    --orphaned: #F0796F;
    --orphaned-bg: #38191A;
  }
}

:root[data-theme="dark"] {
  --paper: #131519;
  --surface: #1A1D23;
  --surface-2: #23262E;
  --rule: #333846;
  --rule-soft: #272B34;
  --ink: #E4E6EC;
  --ink-soft: #A8AEBF;
  --ink-faint: #6E7488;
  --accent: #8F99EE;
  --ok: #3FD3A3;
  --ok-bg: #10312A;
  --moved: #7FAAEE;
  --moved-bg: #16243A;
  --ambiguous: #BA94E8;
  --ambiguous-bg: #2B1D3D;
  --drifted: #EDA34C;
  --drifted-bg: #34240F;
  --orphaned: #F0796F;
  --orphaned-bg: #38191A;
}

:root[data-theme="light"] {
  --paper: #E9EAEE;
  --surface: #F4F5F7;
  --surface-2: #DFE1E7;
  --rule: #C9CCD6;
  --rule-soft: #D8DAE2;
  --ink: #1B1D26;
  --ink-soft: #4A4E5E;
  --ink-faint: #7C8194;
  --accent: #4A56C4;
  --ok: #0E7C5F;
  --ok-bg: #D3EDE4;
  --moved: #35609E;
  --moved-bg: #D6E3F4;
  --ambiguous: #6B4FA1;
  --ambiguous-bg: #E6DCF4;
  --drifted: #A85708;
  --drifted-bg: #F7E3C9;
  --orphaned: #B03429;
  --orphaned-bg: #F6D8D5;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.snapshot {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: .35rem .8rem;
  padding: .55rem 1.25rem;
  background: var(--surface-2);
  border-bottom: 1px solid var(--rule);
  font-size: .76rem;
  color: var(--ink-soft);
}
.snapshot .stamp {
  flex: none;
  text-transform: uppercase;
  letter-spacing: .08em;
  font-size: .68rem;
  color: var(--ink-faint);
}
.snapshot .said { min-width: 0; }
.snapshot code, .snapshot a { font-family: var(--mono); color: var(--accent); }
.snapshot .stamp code { letter-spacing: 0; font-size: .76rem; }

.write-result, .write-warning {
  margin: 1rem var(--gutter) 0;
  padding: .55rem .75rem;
  border: 1px solid var(--rule);
  background: var(--surface);
  font-size: .82rem;
}
.write-warning { color: var(--drifted); border-color: var(--drifted); }
.write-card {
  margin: 1rem var(--gutter);
  padding: .7rem .85rem;
  border: 1px solid var(--rule);
  background: var(--surface);
}
.write-card summary { cursor: pointer; font-weight: 650; }
.write-card form { display: grid; gap: .7rem; margin-top: .8rem; }
.write-card label { display: grid; gap: .25rem; font-size: .76rem; color: var(--ink-soft); }
.write-card input, .write-card select, .write-card textarea, .write-card button {
  width: 100%;
  padding: .45rem .55rem;
  border: 1px solid var(--rule);
  background: var(--paper);
  color: var(--ink);
  font: inherit;
}
.write-card button { width: auto; justify-self: start; cursor: pointer; color: var(--accent); }

.app {
  display: grid;
  grid-template-columns: var(--rail) minmax(0, 1fr);
  min-height: 100vh;
}

.rail {
  background: var(--surface);
  border-right: 1px solid var(--rule);
  padding: 1.1rem 0 .8rem;
  max-height: 100vh;
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
}

.brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  padding: 0 1.1rem .9rem;
}
.brand .mark {
  display: flex;
  align-items: center;
  gap: .45rem;
  min-width: 0;
  font-family: var(--mono);
  font-size: 1.02rem;
  letter-spacing: -.02em;
  font-weight: 700;
  color: var(--ink);
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-mark { width: 1.45rem; height: 1.45rem; flex: none; }
.brand-label { overflow: hidden; text-overflow: ellipsis; }
.brand .mark:hover { color: var(--accent); }
.tally-count { font-size: .7rem; color: var(--ink-faint); font-variant-numeric: tabular-nums; flex: none; }

.switcher {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .25rem .35rem .25rem .65rem;
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: .55rem;
  box-shadow: 0 1px 2px color-mix(in srgb, var(--ink) 8%, transparent);
}
.switcher label {
  display: inline;
  font-size: .66rem;
  text-transform: uppercase;
  letter-spacing: .09em;
  color: var(--ink-faint);
}
.switcher select {
  max-width: 15rem;
  font: inherit;
  font-size: .82rem;
  font-family: var(--mono);
  color: var(--ink);
  background: var(--surface);
  border: 0;
  border-radius: .35rem;
  padding: .28rem 1.65rem .28rem .5rem;
}
.switcher select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.switcher-plain { list-style: none; margin: .4rem 0 0; padding: 0; font-size: .82rem; }
.switcher-plain a { color: var(--accent); font-family: var(--mono); }

.pills {
  display: flex;
  flex-wrap: wrap;
  gap: .3rem;
  padding: 0 1.1rem .9rem;
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: .18rem .5rem;
  border-radius: 2px;
  font-size: .73rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.moved { background: var(--moved-bg); color: var(--moved); }
.pill.ambiguous { background: var(--ambiguous-bg); color: var(--ambiguous); }
.pill.drifted { background: var(--drifted-bg); color: var(--drifted); }
.pill.orphaned { background: var(--orphaned-bg); color: var(--orphaned); }

.tree { flex: 1; min-height: 0; overflow-y: auto; padding-bottom: 1rem; }

.source-link {
  display: block;
  margin: .7rem 1.1rem 0;
  padding-top: .75rem;
  border-top: 1px solid var(--rule-soft);
  color: var(--ink-faint);
  font-family: var(--mono);
  font-size: .68rem;
  text-decoration: none;
}
.source-link:hover { color: var(--accent); }
.source-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

.dir > summary {
  display: flex;
  align-items: center;
  gap: .45rem;
  padding: .28rem 1.1rem;
  font-family: var(--mono);
  font-size: .78rem;
  color: var(--ink-soft);
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.dir > summary::-webkit-details-marker { display: none; }
.dir > summary::before {
  content: "";
  flex: none;
  width: 0;
  height: 0;
  border-left: 4px solid currentColor;
  border-top: 3.5px solid transparent;
  border-bottom: 3.5px solid transparent;
  opacity: .55;
  transition: transform .12s ease;
}
.dir[open] > summary::before { transform: rotate(90deg); }
.dir > summary:hover { background: var(--surface-2); color: var(--ink); }
.dir > summary:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.dir-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

.dir-body {
  margin-left: 1.55rem;
  border-left: 1px solid var(--rule-soft);
}
.dir-body > .dir > summary,
.dir-body > .file { padding-left: .65rem; }

.file {
  display: flex;
  align-items: center;
  gap: .45rem;
  padding: .26rem 1.1rem .26rem .65rem;
  font-family: var(--mono);
  font-size: .78rem;
  color: var(--ink-soft);
  text-decoration: none;
  border-left: 2px solid transparent;
  margin-left: -1px;
}
.file:hover { background: var(--surface-2); color: var(--ink); }
.file:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.file[aria-current="page"] {
  background: var(--surface-2);
  color: var(--ink);
  border-left-color: var(--accent);
  font-weight: 600;
}
.file .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.count { font-size: .7rem; color: var(--ink-faint); font-variant-numeric: tabular-nums; flex: none; }

.dot { width: .45rem; height: .45rem; border-radius: 50%; flex: none; }
.dot.ok { background: var(--ok); }
.dot.moved { background: var(--moved); }
.dot.ambiguous { background: var(--ambiguous); }
.dot.drifted { background: var(--drifted); }
.dot.orphaned { background: var(--orphaned); }

.panel { min-width: 0; display: flex; flex-direction: column; }

.panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .6rem 1rem;
  padding: .95rem 1.5rem;
  border-bottom: 1px solid var(--rule);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 3;
}
.panel-head h1 {
  margin: 0;
  font-family: var(--mono);
  font-size: .95rem;
  font-weight: 600;
  letter-spacing: -.01em;
  min-width: 0;
  overflow-wrap: anywhere;
  flex: 1;
}

.panel-actions { margin-left: auto; display: flex; align-items: center; gap: .65rem; }

.search-trigger {
  display: inline-flex;
  align-items: center;
  gap: .9rem;
  padding: .38rem .45rem .38rem .65rem;
  border: 1px solid var(--rule);
  border-radius: .55rem;
  background: var(--paper);
  color: var(--ink-soft);
  font: inherit;
  font-size: .76rem;
  cursor: pointer;
}
.search-trigger:hover { border-color: var(--accent); color: var(--ink); }
.search-trigger:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.search-trigger kbd, .search-help kbd {
  padding: .1rem .32rem;
  border: 1px solid var(--rule);
  border-bottom-width: 2px;
  border-radius: .25rem;
  background: var(--surface);
  color: var(--ink-faint);
  font-family: var(--mono);
  font-size: .64rem;
}

.search-dialog {
  width: min(42rem, calc(100vw - 2rem));
  max-height: min(42rem, calc(100vh - 3rem));
  padding: 0;
  border: 1px solid var(--rule);
  border-radius: .8rem;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1.5rem 5rem rgba(0, 0, 0, .3);
}
.search-dialog::backdrop { background: rgba(13, 15, 22, .55); backdrop-filter: blur(3px); }
.search-dialog[open] { animation: search-in .12s ease-out; }
.search-surface { display: flex; flex-direction: column; max-height: inherit; }
.search-head { display: flex; align-items: start; justify-content: space-between; gap: 1rem; padding: 1rem 1rem .75rem; }
.search-head h2 { margin: 0; font-family: var(--mono); font-size: .96rem; }
.search-head p { margin: .15rem 0 0; color: var(--ink-faint); font-size: .74rem; }
.search-close { border: 0; background: transparent; color: var(--ink-faint); font: .68rem var(--mono); cursor: pointer; padding: .25rem; }
.search-close:hover { color: var(--ink); }
.search-box { display: flex; align-items: center; gap: .5rem; margin: 0 1rem .75rem; padding: .55rem .7rem; border: 1px solid var(--accent); border-radius: .5rem; background: var(--paper); }
.search-icon { color: var(--accent); font-size: 1.15rem; }
.search-box input { width: 100%; border: 0; outline: 0; background: transparent; color: var(--ink); font: .9rem var(--sans); }
.search-results { min-height: 4rem; overflow-y: auto; border-top: 1px solid var(--rule-soft); }
.search-result { display: grid; grid-template-columns: .55rem minmax(0, 1fr) auto; align-items: center; gap: .7rem; padding: .65rem 1rem; color: var(--ink); text-decoration: none; border-bottom: 1px solid var(--rule-soft); }
.search-result:hover, .search-result.selected { background: var(--surface-2); }
.search-result:focus { outline: 0; }
.search-result-path { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font: .78rem var(--mono); }
.search-result-meta { color: var(--ink-faint); font-size: .68rem; }
.search-empty { margin: 0; padding: 1.25rem 1rem; color: var(--ink-faint); text-align: center; font-size: .8rem; }
.search-help { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: .8rem; padding: .55rem 1rem; color: var(--ink-faint); font-size: .65rem; }
.search-help span { display: inline-flex; align-items: center; gap: .25rem; }

@keyframes search-in { from { opacity: 0; transform: translateY(-.5rem) scale(.985); } }

.drawer-state, .drawer-toggle { display: none; }

.detached {
  padding: 1rem 1.5rem 0;
  max-width: 46rem;
}
.detached-head {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .09em;
  color: var(--ink-faint);
  margin-bottom: .5rem;
}

.reading {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--gloss);
  align-items: start;
  gap: 0 1.5rem;
  padding: 1.1rem 1.5rem 6rem 0;
}

.code-scroll { overflow-x: auto; min-width: 0; }
.code { min-width: max-content; }

.row {
  display: grid;
  grid-template-columns: var(--gutter) minmax(0, 1fr);
  align-items: start;
  column-gap: .75rem;
  scroll-margin-top: 4rem;
}
.row.marked { background: color-mix(in srgb, var(--accent) 5%, transparent); }
.row.marked.ok { --marker: var(--ok); }
.row.marked.moved { --marker: var(--moved); }
.row.marked.ambiguous { --marker: var(--ambiguous); }
.row.marked.drifted { --marker: var(--drifted); }
.row.marked.orphaned { --marker: var(--orphaned); }
.row.lit, .row:target { background: color-mix(in srgb, var(--accent) 12%, transparent); }

.ln {
  font-family: var(--mono);
  font-size: .74rem;
  line-height: 1.65;
  color: var(--ink-faint);
  text-align: right;
  padding: .08rem .5rem .08rem 0;
  font-variant-numeric: tabular-nums;
  user-select: none;
  position: relative;
  text-decoration: none;
}
.ln:hover { color: var(--accent); }
.row.marked .ln { color: var(--ink); font-weight: 600; }
.row.marked .ln::before {
  content: "";
  position: absolute;
  left: .55rem;
  top: .38rem;
  width: 3px;
  height: .95em;
  border-radius: 1px;
  background: var(--marker);
}

.src {
  display: block;
  font-family: var(--mono);
  font-size: .82rem;
  line-height: 1.65;
  white-space: pre;
  padding: .08rem 0;
  tab-size: 4;
}
.src.void { color: var(--ink-faint); }

.gloss {
  min-width: 0;
  position: relative;
  display: flex;
  flex-direction: column;
  gap: .75rem;
}
.gloss.aligned { gap: 0; display: block; }

.note {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--rule-soft);
  border-left: 2px solid var(--note-marker);
  border-radius: 2px;
  padding: .6rem .8rem .55rem;
}
.gloss.aligned .note { position: absolute; width: 100%; }
.note.ok { --note-marker: var(--ok); }
.note.moved { --note-marker: var(--moved); }
.note.ambiguous { --note-marker: var(--ambiguous); }
.note.drifted { --note-marker: var(--drifted); }
.note.orphaned { --note-marker: var(--orphaned); }
.note.lit { border-color: var(--accent); border-left-color: var(--note-marker); }

.note-head {
  display: flex;
  align-items: center;
  gap: .45rem;
  margin-bottom: .4rem;
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--ink-faint);
}
.kind { color: var(--note-marker); font-weight: 700; }
.note-head .sep { opacity: .45; }
.note-head .at {
  margin-left: auto;
  font-family: var(--mono);
  color: var(--ink-faint);
  text-decoration: none;
  letter-spacing: 0;
}
.note-head .at:hover { color: var(--accent); }

.body {
  font-family: var(--serif);
  font-size: .91rem;
  line-height: 1.5;
  margin: 0 0 .5rem;
}
.body:last-of-type { margin-bottom: 0; }
.note.stale .body { color: var(--ink-soft); }

.note-foot {
  display: flex;
  justify-content: space-between;
  gap: .5rem;
  margin-top: .5rem;
  font-family: var(--mono);
  font-size: .62rem;
  color: var(--ink-faint);
}
.note-id { overflow: hidden; text-overflow: ellipsis; }

.was {
  font-family: var(--mono);
  font-size: .73rem;
  color: var(--drifted);
  background: var(--drifted-bg);
  padding: .3rem .45rem;
  margin-bottom: .5rem;
  white-space: pre-wrap;
  word-break: break-all;
}
.was .label {
  display: block;
  font-family: var(--sans);
  font-size: .62rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  opacity: .85;
  margin-bottom: .2rem;
}

.warn {
  margin-top: .5rem;
  font-size: .71rem;
  line-height: 1.45;
  color: var(--drifted);
  font-weight: 600;
}
.note.orphaned .warn { color: var(--orphaned); }

.hollow { max-width: 40rem; margin: 6rem auto; padding: 0 1.5rem; }
.hollow h1 { font-family: var(--mono); font-size: 1.1rem; }
.hollow pre,
.hollow-inline code {
  font-family: var(--mono);
  font-size: .8rem;
  background: var(--surface);
  border: 1px solid var(--rule-soft);
  padding: .7rem .8rem;
  overflow-x: auto;
}
.hollow-inline { padding: 1.5rem; color: var(--ink-soft); }
.hollow-inline code { padding: .1rem .3rem; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }

  .rail {
    position: fixed;
    z-index: 5;
    inset: 0 auto 0 0;
    width: min(85vw, var(--rail));
    transform: translateX(-100%);
    transition: transform .18s ease;
    box-shadow: 0 0 2rem rgba(0, 0, 0, .28);
    max-height: none;
  }
  .drawer-state:checked ~ .rail { transform: translateX(0); }

  .drawer-toggle {
    display: inline-flex;
    align-items: center;
    flex: none;
    padding: .2rem .55rem;
    border: 1px solid var(--rule);
    border-radius: 3px;
    font-size: .74rem;
    color: var(--ink-soft);
    cursor: pointer;
    background: var(--paper);
  }
  .drawer-state:checked ~ .panel .drawer-toggle {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--paper);
  }

  .panel-head { padding: .75rem; }
  .panel-actions { gap: .35rem; }
  .search-trigger span { display: none; }
  .search-trigger { gap: 0; padding: .33rem; }
  .switcher { padding-left: .4rem; }
  .switcher label { display: none; }
  .switcher select { max-width: 8.5rem; }

  .reading { grid-template-columns: minmax(0, 1fr); padding: .75rem 0 4rem; }
  .code-scroll { padding: 0 .75rem; }
  .gloss { padding: 0 .75rem; margin-top: .9rem; }
  .gloss::before {
    content: "Annotations";
    font-size: .68rem;
    text-transform: uppercase;
    letter-spacing: .09em;
    color: var(--ink-faint);
  }
  .detached { padding: 1rem .75rem 0; }
}

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