/* ---------- theme tokens ---------- */
:root {
  color-scheme: light;
  --bg: #ffffff;
  --bg-elev: #f6f7f9;
  --bg-sunken: #eef0f4;
  --fg: #16181d;
  --fg-muted: #6b7280;
  --fg-faint: #9aa1ac;
  --line: #e3e6ec;
  --line-strong: #cfd4dd;
  --accent: #2f6df6;
  --accent-fg: #ffffff;
  --ok: #16a34a;
  --warn: #d97706;
  --err: #dc2626;
  --shadow: 0 12px 32px rgba(16, 20, 30, .14);
  --radius: 12px;
  --editor-size: 17px;
  --editor-leading: normal;   /* the font's own line height, as Sublime does */
  --editor-face: ui-sans-serif, -apple-system, "Segoe UI", Roboto, Inter, system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="system"] { color-scheme: dark; }
  :root[data-theme="system"] {
    --bg: #101114;
    --bg-elev: #17191e;
    --bg-sunken: #0b0c0f;
    --fg: #e6e8ec;
    --fg-muted: #9198a5;
    --fg-faint: #6b7280;
    --line: #262a32;
    --line-strong: #363b45;
    --accent: #6b9bff;
    --accent-fg: #0b0c0f;
    --ok: #4ade80;
    --warn: #fbbf24;
    --err: #f87171;
    --shadow: 0 12px 32px rgba(0, 0, 0, .5);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101114;
  --bg-elev: #17191e;
  --bg-sunken: #0b0c0f;
  --fg: #e6e8ec;
  --fg-muted: #9198a5;
  --fg-faint: #6b7280;
  --line: #262a32;
  --line-strong: #363b45;
  --accent: #6b9bff;
  --accent-fg: #0b0c0f;
  --ok: #4ade80;
  --warn: #fbbf24;
  --err: #f87171;
  --shadow: 0 12px 32px rgba(0, 0, 0, .5);
}

/* ---------- base ---------- */
* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;          /* only the editor scrolls */
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.5 var(--editor-face);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

button, input, textarea { font-family: inherit; }

[hidden] { display: none !important; }

.muted { color: var(--fg-muted); }

.hint {
  color: var(--fg-muted);
  font-size: 12.5px;
  line-height: 1.5;
  margin: 6px 0 0;
}

/* ---------- lock screen ---------- */
.lock {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
  z-index: 40;
}

.lock-card {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  text-align: center;
}

.lock-mark {
  font-size: 34px;
  line-height: 1;
  margin-bottom: 2px;
}

.lock-card h1 {
  margin: 0;
  font-size: 22px;
  font-weight: 650;
  letter-spacing: -.01em;
}

.lock-sub {
  margin: 0 0 6px;
  color: var(--fg-muted);
  font-size: 13.5px;
}

.lock-card input {
  width: 100%;
  padding: 12px 14px;
  font-size: 16px; /* 16px stops iOS zooming on focus */
  color: var(--fg);
  background: var(--bg-elev);
  border: 1px solid var(--line-strong);
  border-radius: 10px;
}

.lock-card input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.lock-card button {
  padding: 12px 14px;
  font-size: 15px;
  font-weight: 600;
  color: var(--accent-fg);
  background: var(--accent);
  border: 0;
  border-radius: 10px;
  cursor: pointer;
}

.lock-card button:disabled { opacity: .6; cursor: default; }

.lock-err {
  margin: 2px 0 0;
  color: var(--err);
  font-size: 13px;
}

.lock-note {
  margin: 4px 0 0;
  color: var(--fg-faint);
  font-size: 12px;
}

/* ---------- app shell ---------- */
.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
}

.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: var(--bg-elev);
  flex: none;
  position: sticky;
  top: 0;
  z-index: 5;
}

.bar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.bar-right {
  display: flex;
  gap: 6px;
  flex: none;
}

/* The bar is at capacity on a phone: the right-hand group already fills most
 * of a 375px screen, so the two labelled buttons become icons on narrow
 * screens and give back the room. Undo and redo live in the footer instead --
 * they are the two actions reached mid-sentence, and the bottom of the screen
 * is where a thumb already is. */
.ico { display: none; }

@media (max-width: 560px) {
  .lbl { display: none; }
  .ico { display: inline; }
  .bar-right { gap: 4px; }
}

.ghost:disabled {
  opacity: .35;
  cursor: default;
}

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--fg-faint);
  flex: none;
}

.dot[data-state="synced"]  { background: var(--ok); }
.dot[data-state="syncing"] { background: var(--warn); animation: pulse 1s ease-in-out infinite; }
.dot[data-state="offline"] { background: var(--fg-faint); }
.dot[data-state="error"]   { background: var(--err); }

@keyframes pulse { 50% { opacity: .35; } }

@media (prefers-reduced-motion: reduce) {
  .dot[data-state="syncing"] { animation: none; }
}

.status {
  font-size: 13px;
  color: var(--fg-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.peers {
  font-size: 11.5px;
  color: var(--fg-muted);
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 1px 8px;
  white-space: nowrap;
  flex: none;
}

/* ---------- buttons ---------- */
.ghost, .primary, .danger {
  font-size: 13px;
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid var(--line-strong);
  background: var(--bg);
  color: var(--fg);
}

.ghost:hover, .primary:hover, .danger:hover { border-color: var(--fg-faint); }

.primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
  font-weight: 600;
}

.danger { color: var(--err); }

.close {
  font-size: 20px;
  line-height: 1;
  padding: 2px 9px 5px;
}

.btns {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

/* ---------- editor ---------- */
.editor-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

.editor {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 0;
  outline: 0;
  padding: 16px clamp(14px, 4vw, 28px);
  /* --kb-pad: whatever the on-screen keyboard covers (set by app.js); on
     both panes alike, or the mirror stops scrolling where the editor goes on */
  padding-bottom: calc(max(16px, env(safe-area-inset-bottom)) + var(--kb-pad, 0px));
  background: var(--bg);
  color: var(--fg);
  font-size: var(--editor-size);
  font-family: var(--editor-face);
  line-height: var(--editor-leading);
  overflow-y: scroll;
  overscroll-behavior: contain;   /* a flick past the end must not scroll the page */
  tab-size: 4;
  caret-color: var(--accent);
}

.editor::placeholder { color: var(--fg-faint); }

.foot {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 12px;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--fg-muted);
  font-size: 12px;
  flex: none;
}

.foot-actions { display: flex; gap: 6px; flex: none; }
.foot-meta {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

/* The footer is a 12px strip; the toolbar's button padding would double its
 * height, so trim it back rather than let the editor lose the room. */
.foot .ghost {
  padding: 3px 9px;
  font-size: 12px;
  line-height: 1.45;
}

/* On a phone the footer holds six buttons (Sync and Timestamp move down
 * here), and the word count and save time beside them wrapped onto four
 * lines and squeezed the buttons. There the buttons share the full width
 * and are taller for the thumb, and the counts become one small line below. */
@media (max-width: 560px) {
  .foot {
    flex-direction: column;
    gap: 4px;
    padding: 3px 8px;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
  }
  .foot-meta {
    justify-content: space-between;
    gap: 8px;
    font-size: 10px;
    line-height: 1.3;
    white-space: nowrap;
  }
  .foot-meta > span { overflow: hidden; text-overflow: ellipsis; min-width: 0; }
  .foot-actions { width: 100%; gap: 6px; }
  .foot .foot-actions .ghost {
    flex: 1 1 0;
    min-width: 0;
    padding: 14px 0;
    font-size: 20px;
    line-height: 1.2;
  }
}

/* ---------- sheets (settings / history / preview) ---------- */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(8, 10, 14, .45);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.sheet-card {
  width: 100%;
  max-width: 460px;
  max-height: min(85vh, 720px);
  overflow: auto;
  padding: 18px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.sheet-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 650;
}

.row {
  padding: 14px 0;
  border-top: 1px solid var(--line);
}

.row:first-of-type { border-top: 0; }

.row > label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 8px;
}

.row input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.seg {
  display: inline-flex;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  overflow: hidden;
}

.seg button {
  padding: 7px 14px;
  font-size: 13px;
  border: 0;
  border-left: 1px solid var(--line-strong);
  background: var(--bg);
  color: var(--fg-muted);
  cursor: pointer;
}

.seg button:first-child { border-left: 0; }

.seg button[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
}

/* ---------- version history ---------- */
.vlist {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 10px;
}

.vitem {
  display: block;
  width: 100%;
  text-align: left;
  padding: 10px 12px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  color: inherit;
}

.vitem:hover { border-color: var(--line-strong); }

.vitem b {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 2px;
}

.vitem span {
  display: block;
  font-size: 12px;
  color: var(--fg-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.preview-text {
  max-height: 40vh;
  overflow: auto;
  margin: 10px 0 14px;
  padding: 12px;
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: 10px;
  font: 13px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 50;
  max-width: 90vw;
  padding: 10px 16px;
  background: var(--fg);
  color: var(--bg);
  border-radius: 999px;
  font-size: 13px;
  box-shadow: var(--shadow);
}

/* typeface preference */
:root[data-face="mono"] {
  --editor-face: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

/* ---------- note title & list ---------- */
.notetitle {
  font-size: 13.5px;
  font-weight: 650;
  color: var(--fg);
  background: none;
  border: 0;
  padding: 3px 6px;
  border-radius: 7px;
  cursor: pointer;
  max-width: 34vw;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.notetitle:hover { background: var(--bg-sunken); }

/* On a phone the title was left one letter wide by everything else in the
 * header. There, app.js moves Sync and Timestamp to the footer
 * (placeForWidth), the build stamp hides (Settings still shows it), the
 * device count shrinks to a number, and the status words appear only when
 * something is wrong -- the dot's colour already says "synced". What is left
 * of the header goes to the title. */
@media (max-width: 560px) {
  .bar-left { flex: 1 1 auto; }
  .notetitle { max-width: none; min-width: 0; flex: 0 1 auto; }
  .ver, .pw { display: none; }
  .peers { padding: 1px 7px; }
  /* A shade bigger than the footer's 54px thumb buttons. Four of them at 48px
   * still leave the title ~60px on a 320px screen. */
  .bar .ghost {
    min-width: 48px;
    padding: 14px 10px;
    font-size: 22px;
    line-height: 1.2;
  }
  .status { display: none; }
  .dot[data-state="error"] + .status,
  .dot[data-state="offline"] + .status { display: block; min-width: 0; flex: 0 2 auto; }
}

/* Desktop buttons are double the base size, header and footer alike. Scoped
 * to min-width so none of it reaches the phone layout above. */
@media (min-width: 561px) {
  .bar .ghost {
    font-size: 26px;
    padding: 14px 24px;
    border-radius: 14px;
  }
  .bar-right { gap: 10px; }
  .foot .ghost {
    padding: 6px 18px;
    font-size: 24px;
  }
}

/* At double size the labelled History and Settings push the header to ~600px
 * on their own, so a narrower desktop window gets the phone's icons and loses
 * the build stamp; under 800px the buttons also give back width (height stays
 * doubled) and the status words hide as on a phone, or the device count ends
 * up drawn over the Sync button. */
@media (min-width: 561px) and (max-width: 1099px) {
  .lbl, .ver { display: none; }
  .ico { display: inline; }
}

@media (min-width: 561px) and (max-width: 799px) {
  .bar .ghost { padding: 14px 12px; }
  .status { display: none; }
  .dot[data-state="error"] + .status,
  .dot[data-state="offline"] + .status { display: block; min-width: 0; flex: 0 2 auto; }
}

.nlist {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 10px;
  max-height: 52vh;
  overflow: auto;
}

.nrow {
  display: flex;
  align-items: stretch;
  gap: 6px;
}

.nrow.active .nopen {
  border-color: var(--accent);
}

.nopen {
  flex: 1;
  min-width: 0;
  text-align: left;
  padding: 10px 12px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  color: inherit;
}

.nopen:hover { border-color: var(--line-strong); }

.nopen b {
  display: block;
  font-size: 13.5px;
  font-weight: 600;
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nopen span {
  display: block;
  font-size: 12px;
  color: var(--fg-muted);
}

.ndel {
  flex: none;
  font-size: 16px;
  line-height: 1;
  padding: 0 11px;
  color: var(--fg-muted);
}

.ndel:hover { color: var(--err); border-color: var(--err); }

/* ---------- find in note ---------- */
.searchbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-elev);
  flex: none;
}

.searchbar input {
  flex: 1;
  min-width: 0;
  padding: 8px 12px;
  font-size: 16px;               /* 16px stops iOS zooming on focus */
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--line-strong);
  border-radius: 8px;
}

.searchbar .ghost { padding: 6px 10px; }

.scount {
  font-size: 12px;
  color: var(--fg-muted);
  white-space: nowrap;
  flex: none;
  min-width: 54px;
  text-align: right;
}

/* A textarea cannot style its own text, so matches are drawn on a mirror
 * layer sitting exactly behind it. Every metric here must match .editor
 * or the highlights drift away from the words. */
.editor-wrap {
  position: relative;
  background: var(--bg);
}

.hl {
  position: absolute;
  inset: 0;                 /* pinned by CSS -- never sized from JS */
  margin: 0;
  border: 0;
  overflow-y: scroll;       /* same scrollbar gutter as .editor, so the text
                               wraps at exactly the same width */
  overflow-x: hidden;
  pointer-events: none;
  color: transparent;
  background: transparent;
  padding: 16px clamp(14px, 4vw, 28px);
  /* --kb-pad: whatever the on-screen keyboard covers (set by app.js); on
     both panes alike, or the mirror stops scrolling where the editor goes on */
  padding-bottom: calc(max(16px, env(safe-area-inset-bottom)) + var(--kb-pad, 0px));
  font-size: var(--editor-size);
  font-family: var(--editor-face);
  line-height: var(--editor-leading);
  tab-size: 4;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
}

/* The mirror's scrollbar must occupy exactly as much width as the editor's,
 * or the two wrap at different points and every highlight below the first
 * difference sits beside the wrong word. `width: 0` here used to hide it --
 * which also took the reserved width away, so the bug showed on Windows
 * (space-taking scrollbars) and not on phones (overlay ones). Pin both to the
 * same width instead and make the mirror's invisible by colour. */
.editor::-webkit-scrollbar,
.hl::-webkit-scrollbar { width: 12px; height: 0; }

.editor::-webkit-scrollbar-track,
.hl::-webkit-scrollbar-track,
.hl::-webkit-scrollbar-thumb { background: transparent; }

.editor::-webkit-scrollbar-thumb {
  background: var(--line-strong);
  border: 3px solid transparent;
  background-clip: content-box;
  border-radius: 999px;
}

.editor::-webkit-scrollbar-thumb:hover { background: var(--fg-faint); background-clip: content-box; }

/* Firefox has no ::-webkit-scrollbar; 'thin' on both is the same deal. */
.editor, .hl { scrollbar-width: thin; }
.hl { scrollbar-color: transparent transparent; }

/* A phone has overlay scrollbars and no room to spare, so take the gutter back
 * -- from both, or the wrapping problem above comes straight back. */
@media (max-width: 560px) {
  .editor::-webkit-scrollbar,
  .hl::-webkit-scrollbar { width: 0; }
  .editor, .hl { scrollbar-width: none; }
}

.hl mark {
  background: rgba(255, 214, 0, .40);
  color: transparent;
  border-radius: 2px;
}

.hl mark.on {
  background: rgba(255, 145, 0, .70);
}

/* Transparent so the mirror shows through; .editor-wrap paints the page. */
.editor { background: transparent; }

/* Sublime-style option toggles in the find bar */
.opt {
  flex: none;
  min-width: 30px;
  padding: 6px 7px;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--fg-muted);
  background: var(--bg);
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  cursor: pointer;
}

.opt[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
}

.opt:disabled { opacity: .55; cursor: progress; }

/* Results from every note, listed under the search bar. Capped and scrollable
 * so a common word cannot push the editor off the screen. */
.sresults {
  flex: none;
  max-height: 38vh;
  overflow-y: auto;
  padding: 2px 12px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-elev);
}

.sres-empty {
  padding: 8px 2px;
  font-size: 13px;
  color: var(--fg-muted);
}

.sres-row {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 8px 10px;
  text-align: left;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
}

.sres-row:hover { border-color: var(--line-strong); }
.sres-row.here { border-color: var(--accent); }

.sres-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.sres-title {
  font-size: 13px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.sres-n {
  margin-left: auto;
  flex: none;
  font-size: 12px;
  color: var(--fg-muted);
}

.sres-snip {
  display: block;
  margin-top: 3px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--fg-muted);
  word-break: break-word;
}

.sres-snip mark {
  background: rgba(255, 214, 0, .40);
  color: var(--fg);
  border-radius: 2px;
}

.searchbar { border-top: 1px solid var(--line); border-bottom: 0; }

.searchbar input.bad { border-color: var(--err); }

/* On a phone the eight controls beside the box took ~330px of a ~360-410px
 * screen, squeezing the box to a sliver that hid what was typed. Give the box
 * its own full-width row; the controls wrap onto a second row beneath it. */
@media (max-width: 560px) {
  .searchbar { flex-wrap: wrap; row-gap: 6px; }
  .searchbar input { flex: 1 0 100%; }
  .scount { margin-left: auto; min-width: 0; }
}

/* Build stamp, so a screenshot identifies which build it is */
.ver {
  font-size: 10.5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--fg-faint);
  white-space: nowrap;
  flex: none;
}
