/* ── Tokens ──────────────────────────────────────────────────────────────
   Light is the base palette. Dark is redefined twice: once for the system
   preference (unless the user forced light) and once for an explicit choice,
   so the toggle wins in both directions. */
:root {
  --bg:        #f6f7f9;
  --surface:   #ffffff;
  --surface-2: #eef0f4;
  --border:    #d7dbe2;
  --text:      #16181d;
  --text-dim:  #5d646f;
  --accent:    #2f6df6;
  --accent-fg: #ffffff;
  --danger:    #c0392b;
  --mark-bg:   #ffe9a8;
  --mark-fg:   #3a2c00;
  --shadow:    0 1px 2px rgba(16, 20, 30, .06), 0 4px 12px rgba(16, 20, 30, .05);
  --radius:    10px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:        #14161a;
    --surface:   #1b1e24;
    --surface-2: #23272f;
    --border:    #333944;
    --text:      #e6e9ee;
    --text-dim:  #99a1ae;
    --accent:    #6c9bff;
    --accent-fg: #0d1220;
    --danger:    #ff6b5e;
    --mark-bg:   #5a4a12;
    --mark-fg:   #ffeeb0;
    --shadow:    0 1px 2px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .3);
  }
}
:root[data-theme="dark"] {
  --bg:        #14161a;
  --surface:   #1b1e24;
  --surface-2: #23272f;
  --border:    #333944;
  --text:      #e6e9ee;
  --text-dim:  #99a1ae;
  --accent:    #6c9bff;
  --accent-fg: #0d1220;
  --danger:    #ff6b5e;
  --mark-bg:   #5a4a12;
  --mark-fg:   #ffeeb0;
  --shadow:    0 1px 2px rgba(0, 0, 0, .4), 0 4px 14px rgba(0, 0, 0, .3);
}

/* ── Base ────────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 var(--sans);
  -webkit-text-size-adjust: 100%;
  display: flex;
  flex-direction: column;
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; margin: 0 0 .5rem; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.08rem; }
.muted { color: var(--text-dim); }
.small { font-size: .85rem; }
mark { background: var(--mark-bg); color: var(--mark-fg); border-radius: 3px; padding: 0 2px; }

/* ── Top bar ─────────────────────────────────────────────────────────────── */
.topbar {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .55rem max(.75rem, env(safe-area-inset-left)) .55rem max(.75rem, env(safe-area-inset-right));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { font-weight: 650; text-decoration: none; color: var(--text); letter-spacing: -.01em; }
.nav { display: flex; gap: .25rem; flex: 1; overflow-x: auto; }
.navlink {
  text-decoration: none;
  color: var(--text-dim);
  padding: .3rem .6rem;
  border-radius: 999px;
  white-space: nowrap;
  font-size: .92rem;
}
.navlink:hover { background: var(--surface-2); color: var(--text); }
.navlink.current { background: var(--surface-2); color: var(--text); font-weight: 600; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: .4rem; }

.main {
  flex: 1;
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  padding: 1rem max(.75rem, env(safe-area-inset-left)) 2.5rem max(.75rem, env(safe-area-inset-right));
}
body.plain .main { display: flex; }

/* ── Controls ────────────────────────────────────────────────────────────── */
button, .btnlike {
  font: inherit;
  border-radius: 8px;
  border: 1px solid var(--border);
  padding: .5rem .9rem;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  display: inline-block;
  line-height: 1.3;
}
button:hover, .btnlike:hover { background: var(--surface-2); }
button:focus-visible, a:focus-visible, input:focus-visible, textarea:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
.primary:hover { filter: brightness(1.07); background: var(--accent); }
.secondary { background: var(--surface); }
.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.danger:hover { background: color-mix(in srgb, var(--danger) 12%, var(--surface)); }
.linkbtn { border: none; background: none; color: var(--text-dim); padding: .3rem .4rem; }
.linkbtn:hover { background: none; color: var(--text); text-decoration: underline; }
.icon-btn { border: none; background: none; padding: .35rem .45rem; border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); }
.theme-icon::before { content: "◑"; font-size: 1.05rem; }
.inline { display: inline; margin: 0; }

input[type="text"], input[type="password"], input[type="search"], textarea {
  font: inherit;
  width: 100%;
  padding: .55rem .7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
textarea { resize: vertical; }
label { display: block; margin: .8rem 0 .25rem; font-size: .9rem; color: var(--text-dim); }
.checkline { display: flex; align-items: center; gap: .5rem; margin-top: .9rem; }
.checkline input { width: auto; }

/* ── Cards / messages ────────────────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
}
.card button[type="submit"] { margin-top: 1rem; }
.centred { margin: auto; width: 100%; max-width: 400px; }
.auth-card h1 { margin-bottom: .75rem; }
.error, .notice {
  padding: .6rem .8rem;
  border-radius: 8px;
  margin: .5rem 0;
  font-size: .92rem;
  border: 1px solid var(--border);
}
.error { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); background: color-mix(in srgb, var(--danger) 8%, var(--surface)); }
.notice { background: var(--surface-2); }
.flash {
  max-width: 900px;
  margin: .75rem auto -.25rem;
  padding: .55rem .85rem;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: .92rem;
  width: calc(100% - 1.5rem);
}
.flash-error { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.page-head { display: flex; align-items: baseline; gap: .75rem; flex-wrap: wrap; margin-bottom: .9rem; }
.page-head h1 { margin: 0; }

/* ── Quick capture + toolbar ─────────────────────────────────────────────── */
.capture textarea { font-family: var(--mono); font-size: .93rem; }
.capture-actions { display: flex; align-items: center; gap: .75rem; margin-top: .5rem; }
.capture-actions .primary { margin-left: auto; }
.toolbar { display: flex; gap: .5rem; align-items: center; margin: 1.25rem 0 .75rem; flex-wrap: wrap; }
.search { display: flex; gap: .4rem; flex: 1; min-width: 220px; margin: 0; }
.search input { min-width: 0; }

/* ── Note list ───────────────────────────────────────────────────────────── */
.note-list, .file-list, .version-list, .session-list { list-style: none; margin: 0; padding: 0; }
.note-row, .file-row {
  display: flex;
  align-items: stretch;
  gap: .5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: .5rem;
  overflow: hidden;
}
.note-row.pinned { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.note-main, .file-main {
  flex: 1;
  min-width: 0;
  display: grid;
  gap: .15rem;
  padding: .7rem .85rem;
  text-decoration: none;
  color: inherit;
}
.note-main:hover, .file-main:hover { background: var(--surface-2); }
.note-title, .file-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pin-mark { color: var(--accent); font-size: .7em; vertical-align: middle; }
.note-excerpt {
  color: var(--text-dim);
  font-size: .88rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.note-meta, .file-meta { color: var(--text-dim); font-size: .8rem; }
.note-actions, .file-actions { display: flex; align-items: center; padding-right: .5rem; gap: .15rem; }
.iconlink {
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: .82rem;
  padding: .35rem .5rem;
  border-radius: 6px;
  text-decoration: none;
}
.iconlink:hover { background: var(--surface-2); color: var(--text); }
.empty {
  padding: 1.5rem;
  text-align: center;
  color: var(--text-dim);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

/* ── Editor ──────────────────────────────────────────────────────────────── */
body.editing .main { display: flex; flex-direction: column; max-width: 1000px; }
.editor {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.editor-head { display: flex; align-items: center; gap: .5rem; padding: .55rem .65rem; border-bottom: 1px solid var(--border); }
.title-input { border: none; background: none; font-size: 1.05rem; font-weight: 650; padding: .25rem .3rem; }
.title-input:focus { background: var(--surface-2); border-radius: 6px; }
.save-status { font-size: .8rem; color: var(--text-dim); white-space: nowrap; padding-right: .25rem; }
.save-status.saving { color: var(--accent); }
.save-status.error { color: var(--danger); font-weight: 600; }

.editor-tabs { display: flex; align-items: center; gap: .25rem; padding: .35rem .5rem; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.tab { border: none; background: none; padding: .3rem .7rem; border-radius: 999px; font-size: .88rem; color: var(--text-dim); }
.tab.current { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }
.editor-tabs .spacer { flex: 1; }

.editor-panes { flex: 1; min-height: 0; display: flex; }
.body-input {
  flex: 1;
  border: none;
  border-radius: 0;
  resize: none;
  font-family: var(--mono);
  font-size: .95rem;
  line-height: 1.6;
  padding: .9rem 1rem;
  min-height: 45vh;
}
.body-input:focus-visible { outline: none; }
.preview { flex: 1; overflow: auto; padding: .9rem 1.1rem; min-height: 45vh; }

.editor-foot { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-top: .75rem; }
.foot-left, .foot-right { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; }
.foot-right { margin-left: auto; }
.editor-foot .btnlike, .editor-foot button { font-size: .88rem; padding: .4rem .7rem; }

/* ── Rendered markdown ───────────────────────────────────────────────────── */
.markdown > :first-child { margin-top: 0; }
.markdown h1, .markdown h2, .markdown h3 { margin: 1.4em 0 .5em; }
.markdown p, .markdown ul, .markdown ol, .markdown blockquote, .markdown table { margin: 0 0 1em; }
.markdown code { font-family: var(--mono); font-size: .88em; background: var(--surface-2); padding: .1em .35em; border-radius: 4px; }
.markdown pre { background: var(--surface-2); padding: .8rem 1rem; border-radius: 8px; overflow-x: auto; }
.markdown pre code { background: none; padding: 0; }
.markdown blockquote { border-left: 3px solid var(--border); padding-left: .9rem; color: var(--text-dim); margin-left: 0; }
.markdown table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: .35rem .6rem; text-align: left; }
.markdown th { background: var(--surface-2); }
.markdown img { max-width: 100%; height: auto; border-radius: 6px; }
.markdown li:has(> input[type="checkbox"]) { list-style: none; margin-left: -1.1em; }
.markdown li > input[type="checkbox"] { margin-right: .45em; }
.markdown hr { border: none; border-top: 1px solid var(--border); margin: 1.5em 0; }

/* ── Versions ────────────────────────────────────────────────────────────── */
.version { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: .5rem; }
.version summary { cursor: pointer; padding: .7rem .85rem; display: flex; gap: .6rem; flex-wrap: wrap; align-items: baseline; }
.version-time { font-weight: 600; }
.version-body { padding: 0 .85rem .85rem; }
.version-pre {
  font-family: var(--mono);
  font-size: .85rem;
  background: var(--surface-2);
  padding: .75rem;
  border-radius: 8px;
  max-height: 340px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── Files ───────────────────────────────────────────────────────────────── */
.upload { padding: .9rem; }
.dropzone {
  display: grid;
  place-items: center;
  gap: .2rem;
  padding: 1.6rem 1rem;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  margin: 0;
  text-align: center;
  background: var(--surface-2);
}
.dropzone.dragover { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--surface)); }
.dz-title { font-weight: 600; color: var(--text); }
.dropzone input[type="file"] { display: none; }
.upload-actions { display: flex; align-items: center; gap: .75rem; margin-top: .75rem; }
.upload-actions .primary { margin-left: auto; }

/* ── Sessions ────────────────────────────────────────────────────────────── */
.session-row { padding: .55rem 0; border-bottom: 1px solid var(--border); display: grid; gap: .15rem; }
.session-row:last-child { border-bottom: none; }
.session-row.current { font-weight: 500; }
.session-agent { font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Small screens ───────────────────────────────────────────────────────── */
@media (max-width: 620px) {
  .main { padding-top: .75rem; }
  .note-actions form:first-child { display: none; }  /* pin needs a wider row */
  .foot-right { margin-left: 0; width: 100%; }
  .brand { font-size: .95rem; }
  .body-input { font-size: 16px; }                    /* stops iOS zoom on focus */
}

/* On a phone the brand, nav, theme button and Log out cannot all fit, and the
   nav is the part you actually navigate with — so the wordmark goes. Signed-out
   pages have no nav, so they keep it. */
@media (max-width: 480px) {
  body:not(.plain) .brand { display: none; }
  .topbar { gap: .5rem; }
  .navlink { padding: .3rem .5rem; }
}
