:root {
  --bg: #fbfbfa;
  --panel: #f2f1ee;
  --text: #24292f;
  --muted: #6e7781;
  --line: #dcdad5;
  --accent: #5b4bc4;
  --accent-soft: #ebe8fb;
  --warn-bg: #fff4d6;
  --warn-line: #e3c15a;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1e1e1e;
    --panel: #252526;
    --text: #d4d4d4;
    --muted: #8b949e;
    --line: #3a3a3a;
    --accent: #a99cff;
    --accent-soft: #34305a;
    --warn-bg: #3d3420;
    --warn-line: #8a6d1f;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 var(--sans);
}

a { color: var(--accent); }

button, .button {
  display: inline-block;
  padding: 0.35rem 0.9rem;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: 0.9rem;
  text-decoration: none;
  cursor: pointer;
}
@media (prefers-color-scheme: dark) { button, .button { color: #111; } }

.button.secondary, .logout button {
  background: transparent;
  color: var(--accent);
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0; padding: 0; margin: -1px;
}

.alert {
  background: var(--warn-bg);
  border: 1px solid var(--warn-line);
  border-radius: 6px;
  padding: 0.6rem 0.9rem;
}

/* ---------------------------------------------------------------- layout */

.layout { display: flex; height: 100%; }

.sidebar {
  flex: 0 0 18rem;
  overflow-y: auto;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 0.75rem 0.5rem 2rem;
  font-size: 0.92rem;
}

main {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 2.5rem 4rem;
}

/* The "Notes" summary only matters on small screens. On wide screens the tree
   is shown even while the <details> is closed (newer browsers; app.js opens it
   for the rest). */
.nav > summary { display: none; }
@media (min-width: 801px) {
  .nav::details-content { content-visibility: visible; display: block; }
}

.nav-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0 0.4rem 0.5rem;
}
.nav-head .home { font-weight: 600; color: var(--text); text-decoration: none; }

.tree ul { list-style: none; margin: 0; padding-left: 0.9rem; }
.tree > ul { padding-left: 0; }

.tree summary {
  display: flex; justify-content: space-between; align-items: center;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  cursor: pointer;
  list-style: none;
}
.tree summary::-webkit-details-marker { display: none; }
.tree summary span::before { content: "▸ "; color: var(--muted); }
.tree details[open] > summary span::before { content: "▾ "; }
.tree summary:hover { background: var(--line); }

a.new {
  visibility: hidden;
  text-decoration: none;
  color: var(--muted);
  padding: 0 0.3rem;
  font-weight: 600;
}
summary:hover a.new, .nav-head a.new { visibility: visible; }
a.new:hover { color: var(--accent); }

a.file {
  display: block;
  padding: 0.1rem 0.4rem 0.1rem 1.15rem;
  border-radius: 4px;
  color: var(--text);
  text-decoration: none;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
a.file:hover { background: var(--line); }
a.file.current { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.logout { margin: 1.5rem 0.4rem 0; }

/* ------------------------------------------------------------------ notes */

.note-head {
  display: flex; justify-content: space-between; align-items: center; gap: 1rem;
  max-width: 50rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--line);
  margin-bottom: 1rem;
}
.note-head .path { color: var(--muted); font-size: 0.85rem; word-break: break-all; }
.note-head .actions { display: flex; gap: 0.5rem; flex-shrink: 0; }
.note-head input[type=text] {
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 4px; padding: 0.15rem 0.4rem; width: 16rem;
}

.note { max-width: 50rem; overflow-wrap: break-word; }
.note h1, .note h2, .note h3 { line-height: 1.25; margin: 1.6em 0 0.5em; }
.note h1:first-child, .note h2:first-child { margin-top: 0.3em; }
.note pre, .note code { font-family: var(--mono); font-size: 0.88em; }
.note code { background: var(--panel); padding: 0.1em 0.3em; border-radius: 3px; }
.note pre { background: var(--panel); padding: 0.8rem 1rem; border-radius: 6px; overflow-x: auto; }
.note pre code { background: none; padding: 0; }
.note blockquote { margin: 0; padding-left: 1rem; border-left: 3px solid var(--line); color: var(--muted); }
.note table { border-collapse: collapse; display: block; overflow-x: auto; }
.note th, .note td { border: 1px solid var(--line); padding: 0.3rem 0.6rem; text-align: left; vertical-align: top; }
.note th { background: var(--panel); }
.note img { max-width: 100%; }
.note hr { border: 0; border-top: 1px solid var(--line); }
.note .task-list-item { list-style: none; }
.note .task-list-item input { margin: 0 0.4em 0 -1.3em; }
.wikilink.missing { color: var(--muted); text-decoration: underline dotted; cursor: help; }
.embed { color: var(--muted); font-family: var(--mono); font-size: 0.85em; }

.empty { color: var(--muted); max-width: 50rem; }

/* ----------------------------------------------------------------- editor */

.editor { max-width: 50rem; display: flex; flex-direction: column; height: 100%; }
.editor textarea, textarea.disk {
  flex: 1;
  width: 100%;
  min-height: 60vh;
  padding: 0.8rem 1rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.55 var(--mono);
  resize: vertical;
}
.disk-title { font-size: 1rem; margin: 2rem 0 0.5rem; max-width: 50rem; }
textarea.disk { max-width: 50rem; background: var(--panel); }

/* ------------------------------------------------------------------ login */

.login-page { display: flex; align-items: center; justify-content: center; }
.login {
  display: flex; flex-direction: column; gap: 0.6rem;
  width: min(22rem, 90vw);
  padding: 2rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.login h1 { margin: 0 0 0.5rem; font-size: 1.4rem; }
.login input[type=password] {
  font: inherit; padding: 0.45rem 0.6rem;
  border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg); color: var(--text);
}
.login button { padding: 0.5rem; margin-top: 0.4rem; }

/* ------------------------------------------------------------ small screen */

/* Phones (also as home screen app): a fixed top bar with "Notes" and the note's
   name. Tapping it opens the tree as a panel over the note, with its own
   scrolling. The note scrolls as the page itself, so iOS keeps native scrolling
   and "tap the status bar to go to the top". env(safe-area-inset-*) keeps
   content clear of the notch and the home indicator. */
@media (max-width: 800px) {
  html, body { height: auto; }
  .layout { display: block; height: auto; }

  .sidebar {
    position: sticky;
    top: 0;
    z-index: 10;
    overflow: visible;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .nav > summary {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    height: 3rem;
    padding: 0 1rem;
    cursor: pointer;
    list-style: none;
    user-select: none;
    -webkit-user-select: none;
  }
  .nav > summary::-webkit-details-marker { display: none; }
  .nav > summary .menu { font-weight: 600; flex-shrink: 0; }
  .nav > summary .menu::before { content: "☰  "; color: var(--accent); }
  .nav[open] > summary .menu::before { content: "✕  "; }
  .bar-title {
    min-width: 0;
    color: var(--muted);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }

  .nav-panel {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    max-height: calc(100dvh - 3rem - env(safe-area-inset-top));
    overflow-y: auto;
    overscroll-behavior: contain;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    box-shadow: 0 10px 30px rgb(0 0 0 / 0.2);
    padding: 0.5rem max(0.5rem, env(safe-area-inset-right)) calc(1.5rem + env(safe-area-inset-bottom)) max(0.5rem, env(safe-area-inset-left));
  }

  /* Larger tap targets */
  .nav-head { padding: 0.3rem 0.4rem 0.6rem; }
  .tree summary, a.file { padding-top: 0.45rem; padding-bottom: 0.45rem; }
  a.new { visibility: visible; padding: 0 0.7rem; font-size: 1.15rem; }

  main {
    padding: 1rem max(1rem, env(safe-area-inset-right)) calc(3rem + env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
    overflow: visible;
  }

  /* iOS zooms into text fields with a font smaller than 16 px. */
  .editor textarea, textarea.disk, .note-head input[type=text] { font-size: 16px; }
  .editor { height: auto; }
  .note-head input[type=text] { width: 10rem; }
}

.login-page {
  min-height: 100vh;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
