/* atlas — typography-first utilitarian interface.
 *
 * Principles
 *   1. One typeface family, many weights. Hierarchy comes from size, weight and
 *      space — never from decoration.
 *   2. Measure is capped at ~68ch. Long lines are the commonest legibility bug
 *      in "information dense" apps.
 *   3. Accent colour carries meaning (focus, links, the current item) and is
 *      never used as decoration.
 *   4. Every interactive state is visible without colour alone.
 */

/* ---------- tokens ---------- */

:root {
  --font-sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-serif: ui-serif, Georgia, "Iowan Old Style", "Source Serif 4", "Times New Roman", serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;

  /* Type scale — 1.200 minor third, clamped for small screens. */
  --t--1: clamp(.72rem, .70rem + .1vw, .78rem);
  --t-0:  clamp(.875rem, .84rem + .16vw, .95rem);
  --t-1:  clamp(1rem, .96rem + .2vw, 1.075rem);
  --t-2:  clamp(1.2rem, 1.12rem + .35vw, 1.35rem);
  --t-3:  clamp(1.45rem, 1.32rem + .6vw, 1.7rem);
  --t-4:  clamp(1.75rem, 1.5rem + 1.1vw, 2.25rem);

  --sp-1: .25rem; --sp-2: .5rem;  --sp-3: .75rem; --sp-4: 1rem;
  --sp-5: 1.5rem; --sp-6: 2rem;   --sp-7: 3rem;   --sp-8: 4rem;

  --measure: 68ch;
  --radius: 6px;
  --radius-lg: 10px;
  --border: 1px solid var(--line);
  --ring: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent);
  --speed: 160ms;
  --ease: cubic-bezier(.2, .7, .3, 1);
}

/* Light — warm paper white, near-black ink. Contrast 16.1:1 on body text. */
:root, [data-theme="light"] {
  --bg:        #fbfaf8;
  --bg-raised: #ffffff;
  --bg-sunken: #f2f0ec;
  --bg-hover:  #eeebe6;
  --ink:       #17161a;
  --ink-2:     #45434c;
  --ink-3:     #6f6c78;
  --ink-4:     #94919d;
  --line:      #e2ded7;
  --line-2:    #cfcabf;
  --shadow:    0 1px 2px rgba(23,22,26,.06), 0 4px 12px rgba(23,22,26,.05);
  --mark:      #fdf2a8;
  --danger:    #b4231f;
  --danger-bg: #fdf1f0;
  --ok:        #1d6f42;
  color-scheme: light;
}

/* Dark — not pure black; #14141a reduces halation on OLED while keeping
   body text at 13.9:1. */
[data-theme="dark"] {
  --bg:        #131318;
  --bg-raised: #1b1b22;
  --bg-sunken: #0e0e12;
  --bg-hover:  #26262f;
  --ink:       #eceaf2;
  --ink-2:     #c2bfcd;
  --ink-3:     #918da0;
  --ink-4:     #6c6879;
  --line:      #2c2b35;
  --line-2:    #3d3c48;
  --shadow:    0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.35);
  --mark:      #5a4d13;
  --danger:    #ff8178;
  --danger-bg: #2a1614;
  --ok:        #6cd39a;
  color-scheme: dark;
}

/* Accents chosen to clear 4.5:1 against both backgrounds. */
[data-accent="indigo"] { --accent: #4f52d4; --accent-ink: #fff; --accent-soft: #ececfb; }
[data-accent="teal"]   { --accent: #0f766e; --accent-ink: #fff; --accent-soft: #e2f4f2; }
[data-accent="amber"]  { --accent: #a35a00; --accent-ink: #fff; --accent-soft: #fdf1e0; }
[data-accent="rose"]   { --accent: #b62655; --accent-ink: #fff; --accent-soft: #fceaf0; }
[data-accent="forest"] { --accent: #2f6b34; --accent-ink: #fff; --accent-soft: #e8f3e9; }
[data-accent="slate"]  { --accent: #4a5568; --accent-ink: #fff; --accent-soft: #edf0f4; }

[data-theme="dark"][data-accent="indigo"] { --accent: #9a9cf5; --accent-ink: #14141a; --accent-soft: #232346; }
[data-theme="dark"][data-accent="teal"]   { --accent: #5ecfc2; --accent-ink: #14141a; --accent-soft: #123330; }
[data-theme="dark"][data-accent="amber"]  { --accent: #e8a552; --accent-ink: #14141a; --accent-soft: #33240f; }
[data-theme="dark"][data-accent="rose"]   { --accent: #f090ad; --accent-ink: #14141a; --accent-soft: #3a1826; }
[data-theme="dark"][data-accent="forest"] { --accent: #7cc985; --accent-ink: #14141a; --accent-soft: #16301a; }
[data-theme="dark"][data-accent="slate"]  { --accent: #a3b1c6; --accent-ink: #14141a; --accent-soft: #232a35; }

[data-density="compact"] { --sp-4: .75rem; --sp-5: 1.1rem; --sp-6: 1.5rem; }

/* ---------- reset ---------- */

*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; }

html {
  font-size: calc(100% * var(--font-scale, 1));
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-sans);
  font-size: var(--t-0);
  line-height: 1.55;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  font-variant-numeric: tabular-nums;
  overscroll-behavior-y: none;
  min-height: 100dvh;
}

h1, h2, h3, h4 { line-height: 1.2; font-weight: 640; letter-spacing: -.015em; text-wrap: balance; }
h1 { font-size: var(--t-4); letter-spacing: -.025em; }
h2 { font-size: var(--t-2); }
h3 { font-size: var(--t-1); }
p, li { text-wrap: pretty; }

a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }

button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: none; box-shadow: var(--ring); border-radius: var(--radius); }
::selection { background: var(--accent-soft); color: var(--ink); }

mark { background: var(--mark); color: var(--ink); padding: 0 .15em; border-radius: 2px; }
hr { border: 0; border-top: var(--border); margin: var(--sp-5) 0; }
code, kbd { font-family: var(--font-mono); font-size: .9em; }
kbd {
  background: var(--bg-sunken); border: var(--border); border-bottom-width: 2px;
  border-radius: 4px; padding: .1em .4em; font-size: .8em; color: var(--ink-2);
}

/* ---------- shell ---------- */

.app { display: grid; grid-template-rows: auto 1fr; min-height: 100dvh; }

.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-4);
  padding-top: max(var(--sp-3), env(safe-area-inset-top));
  background: color-mix(in oklab, var(--bg) 88%, transparent);
  backdrop-filter: saturate(160%) blur(12px);
  border-bottom: var(--border);
}

.wordmark {
  font-family: var(--font-serif); font-size: var(--t-1); font-weight: 600;
  letter-spacing: .01em; margin-right: var(--sp-2);
}
.wordmark span { color: var(--accent); }

.nav { display: flex; gap: var(--sp-1); margin-left: auto; }
.nav button {
  background: none; border: 0; padding: var(--sp-2) var(--sp-3);
  border-radius: var(--radius); color: var(--ink-3); cursor: pointer;
  font-size: var(--t-0); font-weight: 520; white-space: nowrap;
  transition: color var(--speed) var(--ease), background var(--speed) var(--ease);
}
.nav button:hover { color: var(--ink); background: var(--bg-hover); }
.nav button[aria-current="page"] { color: var(--ink); background: var(--bg-sunken); font-weight: 620; }

main { padding: var(--sp-5) var(--sp-4) var(--sp-8); }
.wrap { max-width: 1160px; margin-inline: auto; }
.prose { max-width: var(--measure); }

/* Mobile: nav becomes a bottom bar — thumb reach beats visual tidiness. */
@media (max-width: 720px) {
  .nav {
    position: fixed; inset: auto 0 0 0; z-index: 50;
    background: color-mix(in oklab, var(--bg) 94%, transparent);
    backdrop-filter: blur(12px);
    border-top: var(--border); margin: 0;
    padding: var(--sp-1) var(--sp-2);
    padding-bottom: max(var(--sp-1), env(safe-area-inset-bottom));
    justify-content: space-around;
  }
  .nav button { flex: 1; font-size: var(--t--1); padding: var(--sp-2) var(--sp-1); }
  main { padding-bottom: 6rem; }
}

/* ---------- controls ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-2);
  padding: .5rem .9rem; border: var(--border); border-radius: var(--radius);
  background: var(--bg-raised); color: var(--ink); font-size: var(--t-0);
  font-weight: 540; cursor: pointer; white-space: nowrap;
  transition: background var(--speed) var(--ease), border-color var(--speed) var(--ease), transform 80ms var(--ease);
}
.btn:hover { background: var(--bg-hover); border-color: var(--line-2); }
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: .45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.btn-primary:hover { background: color-mix(in oklab, var(--accent) 88%, var(--ink)); }
.btn-ghost { background: none; border-color: transparent; color: var(--ink-3); }
.btn-ghost:hover { color: var(--ink); background: var(--bg-hover); }
.btn-danger { color: var(--danger); border-color: color-mix(in oklab, var(--danger) 35%, var(--line)); }
.btn-danger:hover { background: var(--danger-bg); }
.btn-sm { padding: .3rem .55rem; font-size: var(--t--1); }

.input, .select, .textarea {
  width: 100%; padding: .5rem .65rem; background: var(--bg-raised);
  border: var(--border); border-radius: var(--radius); color: var(--ink);
  transition: border-color var(--speed) var(--ease);
}
.input:hover, .textarea:hover { border-color: var(--line-2); }
.input::placeholder, .textarea::placeholder { color: var(--ink-4); }
.textarea { resize: vertical; min-height: 5.5rem; line-height: 1.6; font-family: inherit; }

.field { display: grid; gap: var(--sp-1); margin-bottom: var(--sp-4); }
.label {
  font-size: var(--t--1); font-weight: 600; color: var(--ink-3);
  letter-spacing: .04em; text-transform: uppercase;
}
.hint { font-size: var(--t--1); color: var(--ink-4); }

.toolbar { display: flex; flex-wrap: wrap; gap: var(--sp-2); align-items: center; }
.spacer { flex: 1; }

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

.panel {
  background: var(--bg-raised); border: var(--border);
  border-radius: var(--radius-lg); padding: var(--sp-5);
}
.split { display: grid; grid-template-columns: minmax(260px, 340px) 1fr; gap: var(--sp-5); align-items: start; }
@media (max-width: 900px) { .split { grid-template-columns: 1fr; } }

.stack { display: grid; gap: var(--sp-4); }
.row { display: flex; align-items: center; gap: var(--sp-3); }

/* ---------- lists ---------- */

.list { list-style: none; padding: 0; border-top: var(--border); }
.list-item {
  display: flex; gap: var(--sp-3); align-items: baseline;
  padding: var(--sp-3) var(--sp-2); border-bottom: var(--border);
  cursor: pointer; transition: background var(--speed) var(--ease);
}
.list-item:hover { background: var(--bg-hover); }
.list-item[aria-selected="true"] {
  background: var(--accent-soft);
  box-shadow: inset 3px 0 0 var(--accent);
}
.list-item .name { font-weight: 580; }
.list-item .meta { font-size: var(--t--1); color: var(--ink-3); margin-left: auto; text-align: right; }
.list-item .sub {
  font-size: var(--t--1); color: var(--ink-3);
  display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden;
}

.group-heading {
  position: sticky; top: 0; z-index: 2;
  font-size: var(--t--1); font-weight: 700; letter-spacing: .08em; text-transform: uppercase;
  color: var(--ink-4); background: var(--bg); padding: var(--sp-3) var(--sp-2) var(--sp-1);
  border-bottom: var(--border);
}

.avatar {
  flex: none; width: 2rem; height: 2rem; border-radius: 50%;
  display: grid; place-items: center; font-size: var(--t--1); font-weight: 650;
  background: var(--bg-sunken); color: var(--ink-3); border: var(--border);
  align-self: center;
}

/* ---------- record sheet ---------- */

.sheet-title { font-size: var(--t-4); font-family: var(--font-serif); font-weight: 600; letter-spacing: -.02em; }
.sheet-sub { color: var(--ink-3); font-size: var(--t-1); margin-top: var(--sp-1); }

.section { margin-top: var(--sp-6); }
.section > h2 {
  font-size: var(--t--1); font-weight: 700; letter-spacing: .1em; text-transform: uppercase;
  color: var(--ink-4); padding-bottom: var(--sp-2); border-bottom: var(--border); margin-bottom: var(--sp-3);
  display: flex; align-items: center; gap: var(--sp-2);
}
.section > h2 .btn { margin-left: auto; }

/* Definition grid: label left on desktop, stacked on mobile. Reads like a
   dossier rather than a form. */
.defs { display: grid; grid-template-columns: 12rem 1fr; gap: var(--sp-2) var(--sp-4); align-items: baseline; }
@media (max-width: 640px) { .defs { grid-template-columns: 1fr; gap: var(--sp-1); } .defs dd { margin-bottom: var(--sp-3); } }
.defs dt { font-size: var(--t--1); font-weight: 600; color: var(--ink-3); letter-spacing: .02em; }
.defs dd { margin: 0; max-width: var(--measure); }
.defs dd.rich { white-space: pre-wrap; line-height: 1.65; }
.defs dd.empty { color: var(--ink-4); font-style: italic; }

.tag {
  display: inline-block; font-size: var(--t--1); padding: .1rem .45rem; margin: 0 .2rem .2rem 0;
  background: var(--bg-sunken); border: var(--border); border-radius: 100px; color: var(--ink-2);
}
.chip {
  display: inline-flex; align-items: center; gap: .3rem; font-size: var(--t--1);
  padding: .2rem .5rem; border-radius: 100px; background: var(--accent-soft);
  color: var(--ink); border: 1px solid color-mix(in oklab, var(--accent) 30%, transparent);
}

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

.editor {
  width: 100%; min-height: 24rem; padding: var(--sp-4);
  background: var(--bg-raised); border: var(--border); border-radius: var(--radius);
  font-family: var(--font-serif); font-size: var(--t-1); line-height: 1.7;
  resize: vertical; tab-size: 2;
}
.editor:focus-visible { box-shadow: var(--ring); }
.editor-bar {
  display: flex; gap: var(--sp-1); flex-wrap: wrap; align-items: center;
  padding: var(--sp-2); border: var(--border); border-bottom: 0;
  border-radius: var(--radius) var(--radius) 0 0; background: var(--bg-sunken);
}
.editor-bar + .editor { border-radius: 0 0 var(--radius) var(--radius); }
.editor-bar button {
  background: none; border: 0; padding: .25rem .5rem; border-radius: 4px;
  color: var(--ink-3); cursor: pointer; font-size: var(--t--1); font-weight: 600;
}
.editor-bar button:hover { background: var(--bg-hover); color: var(--ink); }

/* @mention autocomplete */
.mention-pop {
  position: absolute; z-index: 60; min-width: 15rem; max-height: 16rem; overflow-y: auto;
  background: var(--bg-raised); border: var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: var(--sp-1);
}
.mention-pop li {
  list-style: none; padding: var(--sp-2); border-radius: 4px; cursor: pointer;
  display: flex; gap: var(--sp-2); align-items: center;
}
.mention-pop li[aria-selected="true"] { background: var(--accent-soft); }
.mention-pop .new { color: var(--ink-3); font-style: italic; }

.mention {
  color: var(--accent); font-weight: 600; cursor: pointer;
  border-bottom: 1px dotted color-mix(in oklab, var(--accent) 50%, transparent);
}

/* Rendered journal body */
.rendered { font-family: var(--font-serif); font-size: var(--t-1); line-height: 1.75; max-width: var(--measure); }
.rendered > * + * { margin-top: .85em; }
.rendered h2 { font-size: var(--t-2); margin-top: 1.4em; }
.rendered h3 { font-size: var(--t-1); margin-top: 1.2em; }
.rendered ul, .rendered ol { padding-left: 1.4em; }
.rendered li + li { margin-top: .25em; }
.rendered blockquote {
  border-left: 3px solid var(--line-2); padding-left: var(--sp-4);
  color: var(--ink-2); font-style: italic;
}
.rendered code { background: var(--bg-sunken); padding: .1em .35em; border-radius: 3px; }
.rendered hr { margin: 1.5em 0; }

/* ---------- ask ---------- */

.ask-box { position: relative; }
.ask-input {
  width: 100%; padding: var(--sp-4) var(--sp-5); font-size: var(--t-2);
  font-family: var(--font-serif); background: var(--bg-raised);
  border: 2px solid var(--line); border-radius: var(--radius-lg);
  transition: border-color var(--speed) var(--ease);
}
.ask-input:hover { border-color: var(--line-2); }
.ask-input:focus-visible { border-color: var(--accent); box-shadow: none; outline: none; }

.answer { border-left: 3px solid var(--accent); padding-left: var(--sp-4); margin-top: var(--sp-5); }
.answer h2 { font-size: var(--t-2); margin-bottom: var(--sp-3); }
.answer ul { list-style: none; padding: 0; }
.answer li { padding: var(--sp-2) 0; border-bottom: var(--border); }
.answer .note { font-size: var(--t--1); color: var(--ink-4); margin-top: var(--sp-3); font-style: italic; }
.sources { font-size: var(--t--1); color: var(--ink-3); margin-top: var(--sp-3); }

/* ---------- suggestions ---------- */

.suggestion {
  display: grid; gap: var(--sp-2); padding: var(--sp-3);
  border: var(--border); border-radius: var(--radius); background: var(--bg-raised);
}
.suggestion .why { font-size: var(--t--1); color: var(--ink-3); }
.confidence {
  height: 3px; background: var(--bg-sunken); border-radius: 2px; overflow: hidden;
}
.confidence > i { display: block; height: 100%; background: var(--accent); }

/* ---------- micro-interactions ---------- */

@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
  width: 1em; height: 1em; border: 2px solid var(--line-2);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin .7s linear infinite; display: inline-block;
}

@keyframes pulse-dot { 0%, 80%, 100% { opacity: .25; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }
.thinking { display: inline-flex; gap: 3px; align-items: center; }
.thinking i { width: 4px; height: 4px; border-radius: 50%; background: var(--accent); animation: pulse-dot 1.2s infinite; }
.thinking i:nth-child(2) { animation-delay: .15s; }
.thinking i:nth-child(3) { animation-delay: .3s; }

/* Saving indicator: appears, settles, fades. Never blocks typing. */
.save-state {
  font-size: var(--t--1); color: var(--ink-4); display: inline-flex;
  align-items: center; gap: var(--sp-1); min-width: 7rem;
  transition: opacity var(--speed) var(--ease);
}
.save-state[data-state="saved"] { color: var(--ok); }
.save-state[data-state="idle"] { opacity: 0; }

@keyframes toast-in { from { opacity: 0; transform: translateY(8px) scale(.98); } }
.toasts {
  position: fixed; z-index: 90; left: 50%; translate: -50% 0;
  bottom: max(var(--sp-5), calc(env(safe-area-inset-bottom) + 4.5rem));
  display: grid; gap: var(--sp-2); width: min(92vw, 30rem);
}
.toast {
  background: var(--ink); color: var(--bg); padding: var(--sp-3) var(--sp-4);
  border-radius: var(--radius); box-shadow: var(--shadow); font-size: var(--t-0);
  animation: toast-in var(--speed) var(--ease); display: flex; gap: var(--sp-3); align-items: center;
}
.toast[data-kind="error"] { background: var(--danger); color: #fff; }
.toast button { background: none; border: 0; color: inherit; text-decoration: underline; cursor: pointer; margin-left: auto; }

dialog {
  border: var(--border); border-radius: var(--radius-lg); background: var(--bg-raised);
  color: var(--ink); padding: var(--sp-5); max-width: min(94vw, 34rem); width: 100%;
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0,0,0,.45); backdrop-filter: blur(2px); }

/* Skeletons while decrypting */
@keyframes shimmer { to { background-position: 200% 0; } }
.skeleton {
  height: 1em; border-radius: 3px;
  background: linear-gradient(90deg, var(--bg-sunken) 25%, var(--bg-hover) 50%, var(--bg-sunken) 75%);
  background-size: 200% 100%; animation: shimmer 1.4s infinite;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

/* ---------- lock screen ---------- */

.lock { display: grid; place-items: center; min-height: 100dvh; padding: var(--sp-5); }
.lock-card { width: min(100%, 26rem); }
.lock h1 { font-family: var(--font-serif); font-size: var(--t-3); margin-bottom: var(--sp-2); }
.lock p { color: var(--ink-3); margin-bottom: var(--sp-5); }
.pin-dots { display: flex; gap: var(--sp-3); justify-content: center; margin: var(--sp-5) 0; }
.pin-dots i { width: .7rem; height: .7rem; border-radius: 50%; border: 2px solid var(--line-2); transition: all var(--speed) var(--ease); }
.pin-dots i[data-filled] { background: var(--accent); border-color: var(--accent); transform: scale(1.15); }
@keyframes shake { 10%, 90% { transform: translateX(-2px); } 30%, 70% { transform: translateX(4px); } 50% { transform: translateX(-4px); } }
.shake { animation: shake .4s; }

.phrase {
  font-family: var(--font-mono); font-size: var(--t-1); line-height: 2.2;
  background: var(--bg-sunken); border: var(--border); border-radius: var(--radius);
  padding: var(--sp-4); word-spacing: .5em; user-select: all;
}

.warn {
  border-left: 3px solid var(--danger); background: var(--danger-bg);
  padding: var(--sp-3) var(--sp-4); border-radius: 0 var(--radius) var(--radius) 0;
}
.warn strong { color: var(--danger); }

/* ---------- utility ---------- */

.muted { color: var(--ink-3); }
.small { font-size: var(--t--1); }
.mono { font-family: var(--font-mono); }
.center { text-align: center; }
.empty-state { text-align: center; padding: var(--sp-8) var(--sp-4); color: var(--ink-3); }
.empty-state h3 { color: var(--ink-2); margin-bottom: var(--sp-2); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.selectable { display: flex; gap: var(--sp-2); align-items: flex-start; }
.selectable input { margin-top: .35rem; accent-color: var(--accent); }
