/* ---------- design tokens ---------- */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --ink: #14181f;
  --muted: #4a5565;
  --line: #d5dae1;
  --brand: #0f6e8c;
  --brand-ink: #ffffff;
  --brand-soft: #e1f0f5;
  --highlight: #ffd66b;
  --danger: #b42318;
  --danger-soft: #fdecea;
  --ok: #1a7f4b;
  --focus: #0b5bd3;
  --shadow: 0 1px 2px rgb(20 24 31 / 8%), 0 4px 16px rgb(20 24 31 / 8%);

  --c-blue: #2f6fdb;
  --c-teal: #0f8a8a;
  --c-green: #2a8a3e;
  --c-orange: #c25a00;
  --c-red: #c4332b;
  --c-purple: #7a45c9;
  --c-pink: #c23a82;
  --c-gray: #5b6573;

  --font-body: "Atkinson Hyperlegible", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-display: "Bricolage Grotesque", "Atkinson Hyperlegible", system-ui, sans-serif;
  --note-scale: 1;
  --radius: 12px;
  --gutter: 16px;
  --header-h: 56px;
  --tabbar-h: 60px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1116;
    --surface: #171b22;
    --surface-2: #1f252e;
    --ink: #eef1f5;
    --muted: #aab4c2;
    --line: #2d3541;
    --brand: #5cc3e3;
    --brand-ink: #06222c;
    --brand-soft: #123240;
    --highlight: #e0b43a;
    --danger: #ff8a80;
    --danger-soft: #3a1b19;
    --ok: #62d08f;
    --focus: #8ab4ff;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 4px 16px rgb(0 0 0 / 40%);
    --c-blue: #6ea0ff;
    --c-teal: #4fd1c5;
    --c-green: #6fd48a;
    --c-orange: #ffa04d;
    --c-red: #ff7d72;
    --c-purple: #b794f6;
    --c-pink: #f687b3;
    --c-gray: #a0aec0;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1116;
  --surface: #171b22;
  --surface-2: #1f252e;
  --ink: #eef1f5;
  --muted: #aab4c2;
  --line: #2d3541;
  --brand: #5cc3e3;
  --brand-ink: #06222c;
  --brand-soft: #123240;
  --highlight: #e0b43a;
  --danger: #ff8a80;
  --danger-soft: #3a1b19;
  --ok: #62d08f;
  --focus: #8ab4ff;
  --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 4px 16px rgb(0 0 0 / 40%);
  --c-blue: #6ea0ff;
  --c-teal: #4fd1c5;
  --c-green: #6fd48a;
  --c-orange: #ffa04d;
  --c-red: #ff7d72;
  --c-purple: #b794f6;
  --c-pink: #f687b3;
  --c-gray: #a0aec0;
}

/* High contrast: secondary text becomes full-strength and every edge is clearly visible. */
:root[data-contrast="high"] {
  --muted: var(--ink);
  --line: var(--ink);
  --brand-soft: var(--surface-2);
}
:root[data-contrast="high"] .btn,
:root[data-contrast="high"] input,
:root[data-contrast="high"] select,
:root[data-contrast="high"] textarea { border-width: 2px; border-color: var(--ink); }

/* Easy-read: rounder, roomier text. */
:root[data-easy-read="true"] body { letter-spacing: 0.02em; word-spacing: 0.08em; }
:root[data-easy-read="true"] .note-body { line-height: 2; }

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 1rem;
  line-height: 1.5;
}
h1, h2, h3 { font-family: var(--font-display); line-height: 1.2; margin: 0; }
h1 { font-size: 1.75rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 0.75rem; }
a { color: var(--brand); }
button, input, select, textarea { font: inherit; color: inherit; }
.icon { flex: none; }
.muted { color: var(--muted); }
.small { font-size: 0.875rem; }
.stack { display: grid; gap: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1 1 auto; min-width: 0; }
.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.boot { padding: 48px var(--gutter); text-align: center; color: var(--muted); }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.skip-link {
  position: absolute; left: 8px; top: -64px; z-index: 100;
  background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 8px; text-decoration: none;
}
.skip-link:focus { top: 8px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; scroll-behavior: auto !important; }
}

/* ---------- buttons and fields ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 8px 16px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px;
  cursor: pointer; text-decoration: none; font-weight: 700;
  transition: background-color 0.12s;
}
.btn:hover { background: var(--surface-2); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn-primary:hover { background: var(--brand); filter: brightness(1.08); }
.btn-danger { background: var(--danger-soft); color: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); filter: brightness(0.97); }
.btn-icon { min-width: 44px; padding: 8px; }
.btn-link { min-height: 44px; background: none; border: 0; color: inherit; text-decoration: underline; padding: 8px; }
.btn-link:hover { background: none; }
.btn[aria-pressed="true"] { background: var(--brand-soft); border-color: var(--brand); color: var(--ink); }

label { display: block; font-weight: 700; margin-bottom: 4px; }
input[type="text"], input[type="email"], input[type="password"], input[type="search"],
input[type="date"], input[type="datetime-local"], input[type="time"], input[type="number"],
select, textarea {
  width: 100%; min-height: 44px; padding: 8px 12px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px;
}
textarea { resize: vertical; }
input[type="checkbox"], input[type="radio"] { width: 22px; height: 22px; accent-color: var(--brand); flex: none; margin: 0; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; font-weight: 400; margin: 0; }
.field { display: block; }
.field-hint { color: var(--muted); font-size: 0.875rem; margin: 4px 0 0; }
.error-text { color: var(--danger); font-weight: 700; margin: 0; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin: 0; min-width: 0; }
legend { font-weight: 700; padding: 0 6px; }

/* ---------- toasts ---------- */
.toasts {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tabbar-h) + 12px + env(safe-area-inset-bottom));
  display: grid; gap: 8px; justify-items: center; padding: 0 var(--gutter); z-index: 60; pointer-events: none;
}
.toast {
  pointer-events: auto; display: flex; align-items: center; gap: 4px;
  background: var(--ink); color: var(--bg); padding: 4px 4px 4px 16px;
  border-radius: 12px; box-shadow: var(--shadow); max-width: 560px;
}
.toast .btn-link { color: inherit; font-weight: 700; }
.toast-error { background: var(--danger); color: #fff; }
@media (min-width: 720px) { .toasts { bottom: 20px; } }

/* ---------- dialogs ---------- */
.dlg {
  width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  padding: 0; border: 1px solid var(--line); border-radius: 16px;
  background: var(--surface); color: var(--ink); box-shadow: var(--shadow);
}
.dlg-wide { width: min(860px, calc(100vw - 24px)); }
.dlg::backdrop { background: rgb(10 14 20 / 55%); }
.dlg[open] { display: flex; flex-direction: column; }
.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 12px 12px 20px; border-bottom: 1px solid var(--line); }
.dlg-body { padding: 16px 20px; overflow: auto; flex: 1 1 auto; }
.dlg-foot, .dlg-foot-inline { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.dlg-foot { padding: 12px 20px; border-top: 1px solid var(--line); }
.choice-list { display: grid; gap: 8px; }

/* ---------- app shell ---------- */
.app { min-height: 100dvh; display: flex; flex-direction: column; }
.app-header {
  position: sticky; top: 0; z-index: 20; height: var(--header-h);
  display: flex; align-items: center; gap: 12px; padding: 0 var(--gutter);
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-family: var(--font-display); font-weight: 700; font-size: 1.25rem; color: var(--ink); text-decoration: none; display: flex; align-items: center; gap: 8px; }
.brand-mark { display: grid; place-items: center; line-height: 0; }
.logo-mark { display: block; border-radius: 9px; }
.header-spacer { flex: 1; }
.sync-chip { text-decoration: none; display: inline-flex; align-items: center; gap: 6px; font-size: 0.875rem; color: var(--muted); padding: 4px 8px; }
.sync-chip { white-space: nowrap; }
.sync-chip[data-state="error"] { color: var(--danger); font-weight: 700; }
@media (max-width: 520px) {
  .sync-chip:not([data-state="error"]):not([data-state="offline"]) span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
}

.main-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.main-nav a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: var(--tabbar-h); color: var(--muted); text-decoration: none; font-size: 0.8125rem; font-weight: 700;
}
.main-nav a[aria-current="page"] { color: var(--brand); box-shadow: inset 0 3px 0 var(--brand); }
:root[data-contrast="high"] .main-nav a[aria-current="page"] { background: var(--surface-2); }

.app-main { flex: 1; min-width: 0; padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); }
.page { padding: var(--gutter); max-width: 1200px; margin: 0 auto; }

@media (min-width: 720px) {
  .main-nav { position: static; border: 0; background: none; padding: 0; gap: 4px; }
  .main-nav a { flex: none; flex-direction: row; gap: 8px; min-height: 44px; padding: 0 14px; border-radius: 10px; font-size: 1rem; }
  .main-nav a[aria-current="page"] { background: var(--brand-soft); color: var(--ink); box-shadow: none; }
  .app-main { padding-bottom: 0; }
}

.banner {
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
  padding: 10px var(--gutter); background: var(--brand-soft); border-bottom: 1px solid var(--line); font-size: 0.9375rem;
}
.banner-warn { background: var(--danger-soft); }

/* ---------- sign-in ---------- */
.auth-wrap { min-height: 100dvh; display: grid; place-items: center; padding: var(--gutter); }
.auth-card {
  width: min(440px, 100%); background: var(--surface); border: 1px solid var(--line);
  border-radius: 20px; padding: 28px 24px; box-shadow: var(--shadow);
}
.auth-card h1 { font-size: 1.6rem; margin: 16px 0 4px; }
.auth-card .brand { font-size: 1.1rem; }
.auth-switch { margin-top: 16px; text-align: center; }
.notice { padding: 12px 14px; border-radius: 10px; background: var(--brand-soft); margin: 0 0 12px; }
.notice-error { background: var(--danger-soft); color: var(--danger); font-weight: 700; }

/* ---------- notes ---------- */
.notes { display: grid; align-content: start; grid-template-columns: minmax(0, 1fr); min-height: calc(100dvh - var(--header-h) - var(--tabbar-h)); }
.notes .folders-pane { padding: 12px var(--gutter) 0; }
.folders { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 8px; scrollbar-width: thin; }
.folder-btn {
  display: inline-flex; align-items: center; gap: 8px; flex: none; min-height: 44px; padding: 6px 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px; cursor: pointer; font-weight: 700;
  color: var(--ink); text-decoration: none;
}
.folder-btn[aria-current="true"] { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.folder-btn .count { font-weight: 400; opacity: 0.85; font-size: 0.875rem; }
.folder-btn-new { border-style: dashed; color: var(--muted); font-weight: 400; }

.list-pane, .editor-pane { padding: 12px var(--gutter) var(--gutter); min-width: 0; }
.notes[data-pane="editor"] .folders-pane,
.notes[data-pane="editor"] .list-pane { display: none; }
.notes[data-pane="list"] .editor-pane { display: none; }

.pane-head { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.pane-head h2 { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; }
.search-box { position: relative; margin-bottom: 12px; }
.search-box input { padding-left: 42px; }
.search-box .icon { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }

.note-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.note-list > li { min-width: 0; }
.list-heading { font-size: 0.875rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin: 14px 4px 6px; }
.note-row {
  display: block; padding: 12px 14px; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); color: var(--ink); text-decoration: none; min-width: 0;
}
.note-row:hover { background: var(--surface-2); }
.note-row[aria-current="true"] { border-color: var(--brand); box-shadow: inset 4px 0 0 var(--brand); }
.note-row-title { display: flex; align-items: center; gap: 6px; font-weight: 700; min-width: 0; }
.note-row-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note-row-meta { display: flex; gap: 8px; min-width: 0; color: var(--muted); font-size: 0.9375rem; }
.note-row-meta .snip { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.note-row-meta .when { flex: none; }
.tag-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.tag { font-size: 0.8125rem; padding: 2px 10px; border-radius: 999px; background: var(--brand-soft); color: var(--ink); }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty h3 { color: var(--ink); margin-bottom: 6px; }
.empty .icon { margin-bottom: 8px; }

.editor { display: grid; gap: 10px; }
.editor-bar { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.editor-bar .btn { min-height: 44px; }
.editor-bar .back-btn { display: inline-flex; }
.editor-title { font-family: var(--font-display); font-size: 1.6rem; font-weight: 700; border: 0; background: none; padding: 4px 0; border-bottom: 1px solid var(--line); border-radius: 0; }
.editor-title:focus-visible { border-radius: 4px; }
.editor-meta { display: grid; gap: 10px; grid-template-columns: minmax(0, 1fr); }
.note-body {
  min-height: 50dvh; padding: 12px 14px; line-height: 1.65;
  font-size: calc(1.0625rem * var(--note-scale));
}
:root[data-easy-read="true"] .note-body { font-size: calc(1.1875rem * var(--note-scale)); }
.editor-status { color: var(--muted); font-size: 0.875rem; display: flex; gap: 12px; flex-wrap: wrap; }
.ai-result h3 { margin: 12px 0 6px; }
.ai-result ul { padding-left: 1.25rem; margin: 0 0 12px; }

@media (min-width: 960px) {
  .notes {
    grid-template-columns: 190px 270px minmax(0, 1fr);
    min-height: calc(100dvh - var(--header-h));
    align-content: stretch;
  }
  .notes .folders-pane { padding: 16px 12px; border-right: 1px solid var(--line); background: var(--surface); }
  .folders { flex-direction: column; overflow: visible; }
  .folder-btn { border-radius: 10px; width: 100%; justify-content: flex-start; }
  .folder-btn .count { margin-left: auto; }
  .list-pane { border-right: 1px solid var(--line); overflow-y: auto; max-height: calc(100dvh - var(--header-h)); padding-top: 16px; }
  .editor-pane { padding-top: 16px; }
  .notes[data-pane="editor"] .folders-pane,
  .notes[data-pane="editor"] .list-pane,
  .notes[data-pane="list"] .editor-pane { display: block; }
  .editor-bar .back-btn { display: none; }
  .editor-meta { grid-template-columns: 1fr 1fr; }
  .note-body { min-height: 60dvh; }
}

/* ---------- colors on folders and notes ---------- */
.color-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; display: inline-block; }
.folder-btn[aria-current="true"] .color-dot { outline: 2px solid var(--brand-ink); }
.editor { border-top: 4px solid var(--accent, transparent); padding-top: 8px; }
.meta-color { grid-column: 1 / -1; }
.color-field { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; }
.color-none[aria-pressed="true"] { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }

/* ---------- connected notes ---------- */
.connected { border: 1px solid var(--line); border-radius: 12px; background: var(--surface); padding: 0 12px; }
.connected[hidden] { display: none; }
.connected summary { display: flex; align-items: center; gap: 8px; min-height: 44px; cursor: pointer; font-weight: 600; }
.connected-count { margin-left: auto; min-width: 1.6em; text-align: center; border-radius: 999px; background: var(--brand-soft, var(--surface-2)); padding: 0 8px; font-size: 0.875rem; }
.connected-hint { margin: 0 0 6px; color: var(--muted); font-size: 0.9rem; }
.connected-list { list-style: none; margin: 0 0 12px; padding: 0; display: grid; gap: 6px; }
.connected-link { display: grid; gap: 2px; min-height: 44px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg, var(--surface)); color: var(--ink); text-decoration: none; }
.connected-link:hover { border-color: var(--brand); }
.connected-name { display: flex; align-items: center; gap: 8px; font-weight: 600; overflow-wrap: anywhere; }
.connected-why { color: var(--muted); font-size: 0.875rem; overflow-wrap: anywhere; }
