:root {
  --bg: #ffffff;
  --surface: #f6f7f9;
  --border: #e2e5ea;
  --text: #1c2024;
  --muted: #6b7280;
  --accent: #2f6fed;
  --danger: #c0392b;
  --radius: 8px;
  --maxw: 1200px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f26;
    --border: #2a2f3a;
    --text: #e6e8ec;
    --muted: #9aa2b1;
    --accent: #6ea0ff;
    --danger: #e57373;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.muted { color: var(--muted); font-size: 0.9em; }

.skip {
  position: absolute; left: -999px;
}
.skip:focus { left: 8px; top: 8px; background: var(--accent); color: #fff; padding: 6px 10px; border-radius: 6px; }

.layout {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  max-width: var(--maxw);
  margin: 0 auto;
  min-height: 100vh;
}

.sidebar {
  border-right: 1px solid var(--border);
  padding: 20px 18px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.brand { font-weight: 700; font-size: 1.3rem; color: var(--text); }
.search input {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--surface); color: var(--text);
}
.nav-heading { margin: 8px 0 4px; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.nav-list { list-style: none; margin: 0; padding: 0; }
.nav-list li { padding: 3px 0; }

.content { padding: 28px 32px; min-width: 0; }

.page-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; margin-bottom: 20px; flex-wrap: wrap;
}
.page-head h1 { margin: 0; font-size: 1.6rem; }

.button {
  display: inline-block; padding: 7px 14px; border: 1px solid var(--border);
  border-radius: var(--radius); background: var(--surface); color: var(--text);
  cursor: pointer; font-size: 0.95rem;
}
.button:hover { text-decoration: none; border-color: var(--accent); }
.button.primary, .button.new { background: var(--accent); color: #fff; border-color: var(--accent); }
.button.danger { color: var(--danger); }
.actions { display: flex; gap: 10px; align-items: center; }
.actions form { margin: 0; }

.note-list { list-style: none; margin: 0; padding: 0; }
.note-list li {
  padding: 12px 0; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; gap: 12px; align-items: baseline;
}
.note-link { font-weight: 600; font-size: 1.05rem; }
.meta { color: var(--muted); font-size: 0.85rem; white-space: nowrap; }

.tag {
  display: inline-block; background: var(--surface); border: 1px solid var(--border);
  border-radius: 100px; padding: 1px 9px; font-size: 0.8rem; margin-left: 6px; color: var(--muted);
}
.tag-index { list-style: none; padding: 0; }
.tag-index li { display: flex; gap: 8px; align-items: center; padding: 4px 0; }

.note { max-width: 44rem; }
.note-body h1, .note-body h2, .note-body h3 { line-height: 1.25; }
.note-body pre {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 14px; overflow-x: auto;
}
.note-body code { background: var(--surface); padding: 1px 5px; border-radius: 4px; }
.note-body pre code { background: none; padding: 0; }
.note-body table { border-collapse: collapse; }
.note-body th, .note-body td { border: 1px solid var(--border); padding: 6px 10px; }
.note-body img { max-width: 100%; height: auto; }
.wikilink.broken { color: var(--danger); border-bottom: 1px dotted var(--danger); }
.backlinks { list-style: none; margin: 0; padding: 0; }
.backlinks li { padding: 1px 0; }

.context {
  margin-top: 28px; padding-top: 16px; border-top: 1px solid var(--border);
  max-width: 44rem;
}
.context dl { display: grid; grid-template-columns: 120px 1fr; gap: 6px 12px; margin: 0; }
.context dt { color: var(--muted); font-size: 0.85rem; }
.context dd { margin: 0; }

/* Full-screen side-by-side editor — uses the whole viewport width. */
.editor-body { height: 100vh; overflow: hidden; }
.editor-shell { display: flex; flex-direction: column; height: 100vh; }
.editor-bar {
  flex: 0 0 auto; display: flex; align-items: center; gap: 10px;
  padding: 8px 14px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.editor-back { font-weight: 700; color: var(--text); text-decoration: none; }
.editor-title {
  flex: 1 1 40%; min-width: 120px; font-size: 1.05rem; font-weight: 600;
  padding: 7px 10px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg); color: var(--text);
}
.editor-bar select, .editor-tags {
  padding: 7px 10px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg); color: var(--text);
}
.editor-tags { flex: 1 1 20%; min-width: 120px; }
.editor-error { color: var(--danger); font-size: 0.85rem; }
.editor-bar .spacer { flex: 1 1 auto; }
.editor-split { flex: 1 1 auto; display: grid; grid-template-columns: 1fr 1fr; min-height: 0; }
.editor-input {
  height: 100%; width: 100%; resize: none; border: none; outline: none;
  padding: 20px 22px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.95rem; line-height: 1.65; background: var(--bg); color: var(--text);
  border-right: 1px solid var(--border); overflow: auto;
}
.editor-preview { height: 100%; overflow: auto; padding: 20px 28px; max-width: none; }

/* Segment management */
.inline-form { display: flex; gap: 8px; margin: 14px 0 22px; flex-wrap: wrap; }
.inline-form input {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
}
.grid-table { width: 100%; border-collapse: collapse; }
.grid-table th, .grid-table td {
  text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle;
}
.grid-table th { font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.grid-table td.right, .grid-table .right { text-align: right; }
.grid-table form { margin: 0; }
.grid-table tr.archived { opacity: 0.55; }
.rename-form { display: flex; gap: 6px; }
.rename-form input {
  padding: 6px 8px; border: 1px solid var(--border); border-radius: 6px;
  background: var(--surface); color: var(--text); min-width: 160px;
}
.button.small { padding: 4px 9px; font-size: 0.8rem; }
.nav-manage { font-size: 0.7rem; text-transform: none; letter-spacing: 0; margin-left: 8px; font-weight: 400; }

/* Tokens */
.inline-form .check { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.9rem; }
.token-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin: 16px 0; }
.token-card h2 { font-size: 1.05rem; }
.token-reveal { background: color-mix(in srgb, var(--accent) 12%, transparent); border: 1px solid var(--accent); border-radius: var(--radius); padding: 12px 14px; margin: 12px 0; }
.token-secret { display: block; margin-top: 6px; padding: 8px 10px; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; word-break: break-all; }
.grant-form { display: inline-flex; gap: 6px; }
.grant-form select { padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); }

.error { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); padding: 10px 12px; border-radius: var(--radius); }
.error:empty { display: none; }

.settings-link { margin-top: auto; font-size: 0.85rem; color: var(--muted); }
.settings-section { margin-bottom: 32px; }

/* Embedding map */
.map-wrap {
  width: 100%; max-width: 820px; margin: 12px auto 0; aspect-ratio: 1 / 1;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface);
}
#map-svg { width: 100%; height: 100%; display: block; }
.map-dot { cursor: pointer; transition: r 0.1s ease; }
.map-dot:hover { r: 13; fill-opacity: 1; }
.map-legend { display: flex; flex-wrap: wrap; gap: 8px 14px; margin: 8px 0 4px; }
.map-chip { display: inline-flex; align-items: center; gap: 6px; font-size: 0.85rem; color: var(--muted); }
.map-chip-dot { width: 11px; height: 11px; border-radius: 50%; display: inline-block; }
.settings-section h2 { font-size: 1.1rem; margin-bottom: 4px; }

/* Mobile chrome: a fixed top bar and an iOS-style bottom tab bar. Hidden on
   desktop, where the sidebar does this job. */
.mobile-topbar { display: none; }
.tabbar { display: none; }

/* Auth pages (login / setup) */
body.auth { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.auth-card {
  width: 100%; max-width: 360px; border: 1px solid var(--border); border-radius: 14px;
  padding: 32px 28px; background: var(--surface); display: flex; flex-direction: column; gap: 16px;
}
.auth-card .brand { margin: 0; font-size: 1.6rem; }
.button.block { width: 100%; text-align: center; padding: 11px 14px; }
label.stack { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; color: var(--muted); }
label.stack input {
  padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg); color: var(--text); font-size: 1rem;
}
.empty { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  /* The sidebar's job moves to the top bar + bottom tab bar on mobile. */
  .sidebar { display: none; }
  .content {
    padding: 18px 18px calc(72px + env(safe-area-inset-bottom));
    max-width: 40rem;
    margin: 0 auto;
  }

  .mobile-topbar {
    display: flex; align-items: center; justify-content: center;
    position: sticky; top: 0; z-index: 20;
    height: calc(48px + env(safe-area-inset-top));
    padding-top: env(safe-area-inset-top);
    font-weight: 700; letter-spacing: 0.01em;
    background: color-mix(in srgb, var(--bg) 82%, transparent);
    -webkit-backdrop-filter: saturate(180%) blur(14px);
    backdrop-filter: saturate(180%) blur(14px);
    border-bottom: 1px solid var(--border);
  }

  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    padding-bottom: env(safe-area-inset-bottom);
    background: color-mix(in srgb, var(--bg) 82%, transparent);
    -webkit-backdrop-filter: saturate(180%) blur(14px);
    backdrop-filter: saturate(180%) blur(14px);
    border-top: 1px solid var(--border);
  }
  .tabbar a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px;
    padding: 7px 0 5px; font-size: 0.62rem; color: var(--muted); text-decoration: none;
  }
  .tabbar a:hover { text-decoration: none; }
  .tabbar svg { width: 23px; height: 23px; }
  .tabbar a.active { color: var(--accent); }
  .tabbar a.tab-new { color: var(--accent); }

  /* Typography-first reading on a phone. */
  .note { max-width: none; }
  .note-body { font-size: 1.05rem; }
  .page-head h1 { font-size: 1.4rem; }

  .editor-split { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
  .editor-input { border-right: none; border-bottom: 1px solid var(--border); }
  .editor-bar { flex-wrap: wrap; }
  .editor-title { flex-basis: 100%; }
}

/* On a phone in standalone (installed) mode, lean into the native feel. */
@media (display-mode: standalone) {
  .mobile-topbar { position: fixed; left: 0; right: 0; top: 0; }
  .content { padding-top: calc(60px + env(safe-area-inset-top)); }
}
