:root {
  --bg: #fbfbfa;
  --surface: #ffffff;
  --text: #1f2328;
  --muted: #646b73;
  --faint: #9aa0a6;
  --bullet: #868c93;
  --halo: #e4e6e9;
  --guide: #e7e8ea;
  --accent: #2563eb;
  --accent-soft: #dfe8fd;
  --mark: #fde68a;
  --border: #e5e7eb;
  --ok: #16a34a;
  --busy: #d97706;
  --bad: #dc2626;
  --shadow: 0 10px 30px rgb(15 23 42 / 0.12);
  --gutter: 22px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #17181b;
    --surface: #202226;
    --text: #e7e8ea;
    --muted: #a3a9b0;
    --faint: #6e747b;
    --bullet: #8d939a;
    --halo: #33363b;
    --guide: #2b2e33;
    --accent: #7aa7ff;
    --accent-soft: #24324f;
    --mark: #6b5a12;
    --border: #2f3237;
    --shadow: 0 10px 30px rgb(0 0 0 / 0.45);
    color-scheme: dark;
  }
}

:root[data-theme='dark'] {
  --bg: #17181b;
  --surface: #202226;
  --text: #e7e8ea;
  --muted: #a3a9b0;
  --faint: #6e747b;
  --bullet: #8d939a;
  --halo: #33363b;
  --guide: #2b2e33;
  --accent: #7aa7ff;
  --accent-soft: #24324f;
  --mark: #6b5a12;
  --border: #2f3237;
  --shadow: 0 10px 30px rgb(0 0 0 / 0.45);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

/* ---------- header ---------- */

.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) calc(env(safe-area-inset-right) + 16px) 8px calc(env(safe-area-inset-left) + 16px);
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.crumbs {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  overflow: hidden;
  white-space: nowrap;
  font-size: 14px;
}

.crumb {
  color: var(--muted);
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 14em;
  flex: 0 1 auto;
  min-width: 2.5em;
}

.crumb.home {
  flex-shrink: 0;
  min-width: auto;
}

.crumb + .crumb::before {
  content: '›';
  margin: 0 6px;
  color: var(--faint);
}

.crumb:hover {
  color: var(--text);
}

.crumb.gap {
  display: none;
  min-width: auto;
  flex-shrink: 0;
}

.crumb.current {
  color: var(--text);
  font-weight: 600;
}

.search {
  width: 180px;
  min-width: 0;
  flex: 0 1 180px;
  padding: 5px 10px;
  font-size: 16px; /* 16px stops iOS zooming in on focus */
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  outline: none;
}

.search:focus {
  border-color: var(--accent);
}

.pill {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: 14px;
  cursor: pointer;
}

.pill .count:not(:empty) {
  min-width: 1.4em;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
}

.status {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

.status .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--faint);
}

.status[data-state='saved'] .dot {
  background: var(--ok);
}

.status[data-state='saving'] .dot,
.status[data-state='loading'] .dot {
  background: var(--busy);
}

.status[data-state='offline'] .dot,
.status[data-state='signed-out'] .dot,
.status[data-state='error'] .dot {
  background: var(--bad);
}

.icon-button {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 0;
  border-radius: 8px;
  background: none;
  cursor: pointer;
  fill: currentColor;
}

.icon-button:hover {
  background: var(--halo);
}

.menu {
  position: absolute;
  top: calc(100% + 4px);
  right: calc(env(safe-area-inset-right) + 12px);
  z-index: 30;
  width: 270px;
  max-width: calc(100vw - 24px);
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

.menu button,
.menu label {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border: 0;
  border-radius: 7px;
  background: none;
  font-size: 15px;
  text-align: left;
  cursor: pointer;
}

.menu button:hover,
.menu label:hover {
  background: var(--halo);
}

.menu hr {
  margin: 6px 4px;
  border: 0;
  border-top: 1px solid var(--border);
}

/* ---------- outline ---------- */

.loading-message {
  display: none;
}

body.loading .loading-message {
  display: block;
  margin-top: 30vh;
  text-align: center;
  color: var(--muted);
}

body.loading .outline {
  display: none;
}

.outline {
  max-width: 780px;
  margin: 0 auto;
  padding: 20px calc(env(safe-area-inset-right) + 16px) calc(env(safe-area-inset-bottom) + 160px) calc(env(safe-area-inset-left) + 12px);
}

.line {
  display: flex;
  align-items: flex-start;
}

.toggle,
.bullet {
  flex: 0 0 var(--gutter);
  height: calc(1.5em + 4px);
  display: flex;
  align-items: center;
  justify-content: center;
}

.toggle {
  padding: 0;
  border: 0;
  background: none;
  color: var(--faint);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
}

.toggle svg {
  transition: transform 0.15s;
}

.toggle.open svg {
  transform: rotate(90deg);
}

.toggle.none {
  visibility: hidden;
}

.line:hover .toggle,
.toggle:focus-visible {
  opacity: 1;
}

@media (hover: none) {
  .toggle {
    opacity: 0.55;
  }
}

.bullet {
  text-decoration: none;
}

.bullet::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--bullet);
  transition: box-shadow 0.15s;
}

.bullet.closed::before {
  box-shadow: 0 0 0 4px var(--halo);
}

.bullet:hover::before {
  box-shadow: 0 0 0 4px var(--accent-soft);
}

.text {
  flex: 1 1 auto;
  min-width: 0;
  min-height: calc(1.5em + 4px);
  padding: 2px 4px 2px 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  outline: none;
  cursor: text;
  scroll-margin: 80px 0 140px;
}

.text[contenteditable] {
  caret-color: var(--accent);
}

.text a,
.note a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-color: color-mix(in srgb, var(--accent) 35%, transparent);
  text-underline-offset: 2px;
}

code {
  font: 0.9em ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  padding: 0.05em 0.3em;
  border-radius: 4px;
  background: var(--halo);
}

mark {
  border-radius: 2px;
  background: var(--mark);
  color: inherit;
}

.line.h1 {
  font-size: 1.5em;
  font-weight: 700;
  line-height: 1.3;
}

.line.h2 {
  font-size: 1.25em;
  font-weight: 650;
  line-height: 1.35;
}

.line.h3 {
  font-size: 1.08em;
  font-weight: 650;
}

.row > .line.h1,
.row > .line.h2 {
  margin-top: 0.35em;
}

.note {
  display: block;
  margin: -2px 0 2px calc(var(--gutter) * 2);
  padding: 0 4px 0 0;
  color: var(--muted);
  font-size: 0.9em;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  cursor: text;
  scroll-margin: 80px 0 140px;
}

textarea.note-edit {
  width: calc(100% - var(--gutter) * 2);
  min-height: 1.5em;
  border: 0;
  outline: none;
  background: transparent;
  resize: none;
  overflow: hidden;
  font-size: 0.9em;
}

.children {
  position: relative;
  padding-left: var(--gutter);
}

.children::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 6px;
  left: calc(var(--gutter) * 1.5);
  border-left: 1px solid var(--guide);
}

.children.top {
  padding-left: 0;
}

.children.top::before {
  display: none;
}

.row.done > .line .text {
  text-decoration: line-through;
  text-decoration-color: var(--faint);
  color: var(--muted);
}

.row.done {
  opacity: 0.6;
}

.row.done .row.done {
  opacity: 1;
}

.zoom-head {
  margin: 0 0 14px calc(var(--gutter) * 2);
}

.title {
  font-size: 1.75em;
  font-weight: 700;
  line-height: 1.25;
}

.title:empty::before {
  content: 'Untitled';
  color: var(--faint);
}

.zoom-head .note,
.zoom-head textarea.note-edit {
  width: 100%;
  margin: 4px 0 0;
}

.zoom-head.done .title {
  text-decoration: line-through;
  color: var(--muted);
}

.empty {
  margin: 0 0 4px calc(var(--gutter) * 2);
  color: var(--muted);
}

.add {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  margin: 4px 0 0 calc(var(--gutter) - 5px);
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--faint);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

.add:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

/* On wide screens the expand arrows hang in the margin. */
@media (min-width: 840px) {
  .children.top {
    margin-left: calc(var(--gutter) * -1);
  }

  .zoom-head,
  .empty {
    margin-left: var(--gutter);
  }

  .add {
    margin-left: -5px;
  }
}

@media (max-width: 560px) {
  :root {
    --gutter: 20px;
  }

  .bar {
    gap: 6px;
  }

  .search {
    flex: 0 0 96px;
    width: 96px;
  }

  .crumb.middle {
    display: none;
  }

  .crumb.gap {
    display: inline;
  }

  .search:focus {
    flex-basis: 160px;
    width: 160px;
  }

  .status .label {
    display: none;
  }

  .status[data-state='offline'] .label,
  .status[data-state='signed-out'] .label,
  .status[data-state='error'] .label {
    display: inline;
  }

  .title {
    font-size: 1.5em;
  }
}

/* ---------- mobile editing toolbar ---------- */

.toolbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  display: none;
  justify-content: space-between;
  gap: 2px;
  padding: 6px calc(env(safe-area-inset-right) + 6px) calc(env(safe-area-inset-bottom) + 6px) calc(env(safe-area-inset-left) + 6px);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
}

body.keyboard .toolbar {
  padding-bottom: 6px;
}

@media (pointer: coarse) {
  body.editing .toolbar {
    display: flex;
  }
}

.toolbar button {
  flex: 1 1 0;
  min-width: 0;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: none;
  touch-action: manipulation;
}

.toolbar button:active {
  background: var(--halo);
}

.toolbar svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- messages ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 76px);
  z-index: 50;
  max-width: calc(100vw - 32px);
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  transform: translateX(-50%);
  box-shadow: var(--shadow);
}

.banner {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 20px);
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 12px;
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 10px 12px 10px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 14px;
  transform: translateX(-50%);
}

.banner button {
  flex-shrink: 0;
  padding: 6px 12px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

[hidden] {
  display: none !important;
}

dialog {
  width: min(540px, calc(100vw - 32px));
  padding: 20px 22px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgb(0 0 0 / 0.35);
}

dialog h2 {
  margin: 0 0 12px;
  font-size: 18px;
}

dialog table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

dialog td {
  padding: 5px 0;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

dialog td:first-child {
  padding-right: 16px;
  white-space: nowrap;
}

kbd {
  display: inline-block;
  min-width: 1.6em;
  padding: 0 5px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--bg);
  font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace;
  text-align: center;
}

dialog .hint {
  color: var(--muted);
  font-size: 14px;
}

dialog form {
  text-align: right;
}

dialog form button {
  padding: 7px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  cursor: pointer;
}
