/* Thinkory editor.
   One column, prose type, everything that is not writing kept off screen
   until asked for. Hand-written: no framework, no build step, same deploy. */

:root {
  --bg: #fffdfa;
  --surface: #fff;
  --fg: #1a1a18;
  --muted: #6b6b66;
  --faint: #9a9a94;
  --line: #e8e5df;
  --line-strong: #d6d2ca;
  --accent: #1a1a18;
  --danger: #c0392b;
  --ok: #2f7d4f;

  /* The size knobs. Everything else is relative to these two. */
  --prose: 20px;
  --measure: 66ch;

  /* Stronger than the built-ins, which are too weak to read as intentional. */
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  --ease-drawer: cubic-bezier(0.32, 0.72, 0, 1);

  --radius: 8px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 8px 24px rgba(0, 0, 0, .06);
}

/* A class that sets display (flex, grid, block) would otherwise beat the
   hidden attribute and leave "hidden" elements on screen. Selectors that
   animate out of hidden (.menu) win on specificity. */
[hidden] { display: none !important; }

/* Theme: the system decides until you pick one, and a pick wins in both
   directions. Every colour is defined on bare :root first, so a token can
   never exist only inside a media query. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16161a;
    --surface: #1d1d22;
    --fg: #e9e7e2;
    --muted: #9b9a95;
    --faint: #6e6d69;
    --line: #2b2b31;
    --line-strong: #3a3a42;
    --accent: #e9e7e2;
    --danger: #e26d5c;
    --ok: #6cc08b;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

/* An explicit choice overrides the system, dark and light alike. */
:root[data-theme="dark"] {
  --bg: #16161a;
  --surface: #1d1d22;
  --fg: #e9e7e2;
  --muted: #9b9a95;
  --faint: #6e6d69;
  --line: #2b2b31;
  --line-strong: #3a3a42;
  --accent: #e9e7e2;
  --danger: #e26d5c;
  --ok: #6cc08b;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16.5px/1.65 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

:focus-visible {
  outline: 2px solid var(--fg);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  height: 58px;
  padding: 0 18px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(140%) blur(8px);
  border-bottom: 1px solid var(--line);
}

.topbar .brand {
  color: var(--fg);
  text-decoration: none;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.topbar .spacer { flex: 1; }

.chip {
  font-size: 13px;
  line-height: 1;
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--line-strong);
  color: var(--muted);
  white-space: nowrap;
}
.chip[data-status="published"] { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, var(--line)); }
.chip[data-status="unlisted"] { color: var(--muted); border-style: dashed; }

/* Save state is information, not decoration: it never moves the layout. */
.savestate {
  font-size: 13.5px;
  color: var(--faint);
  min-width: 9ch;
  text-align: right;
  transition: color 200ms ease;
}
.savestate[data-state="saving"] { color: var(--muted); }
.savestate[data-state="error"] { color: var(--danger); }

/* ---------- buttons ---------- */

button, .button {
  font: inherit;
  color: var(--fg);
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 8px 14px;
  cursor: pointer;
  transition: transform 160ms var(--ease-out), background-color 160ms ease, border-color 160ms ease;
}
button:active, .button:active { transform: scale(0.97); }

@media (hover: hover) and (pointer: fine) {
  button:hover, .button:hover { background: color-mix(in srgb, var(--fg) 5%, var(--surface)); }
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--bg);
}

button.quiet {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  padding: 8px 11px;
}

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

/* ---------- menu ---------- */

.menu-wrap { position: relative; }

.menu {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  min-width: 190px;
  padding: 5px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  display: grid;
  gap: 1px;

  /* Scales from the trigger it hangs off, not from its own middle. */
  transform-origin: top right;
  transition: opacity 150ms var(--ease-out), transform 150ms var(--ease-out);
}
.menu[hidden] { display: grid !important; opacity: 0; transform: scale(0.96); pointer-events: none; }

.menu button {
  border: 0;
  background: transparent;
  text-align: left;
  border-radius: 6px;
  padding: 8px 10px;
  width: 100%;
}

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

.editor {
  max-width: var(--measure);
  margin: 0 auto;
  padding: 48px 20px 40vh;
}

#title {
  display: block;
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--fg);
  font-size: 38px;
  line-height: 1.22;
  font-weight: 650;
  letter-spacing: -0.02em;
  padding: 0;
  margin: 0 0 20px;
}
#title::placeholder { color: var(--faint); }
#title:focus { outline: none; }

/* Quiet by default: the toolbar is there when wanted, not competing with the
   text. It sharpens on hover or when anything inside it has focus. */
.mdbar {
  display: flex;
  align-items: center;
  gap: 2px;
  margin: 0 0 14px -6px;
  opacity: .55;
  transition: opacity 160ms ease;
}
.mdbar:hover, .mdbar:focus-within { opacity: 1; }

.mdbar button {
  font-size: 14.5px;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 5px 8px;
  border-radius: 6px;
  line-height: 1;
  min-width: 34px;
}
@media (hover: hover) and (pointer: fine) {
  .mdbar button:hover { background: color-mix(in srgb, var(--fg) 7%, transparent); color: var(--fg); }
}

.mdbar .sep {
  width: 1px;
  height: 16px;
  margin: 0 5px;
  background: var(--line);
}

#body {
  display: block;
  width: 100%;
  min-height: 60vh;
  border: 0;
  padding: 0;
  resize: none;
  background: transparent;
  color: var(--fg);
  font-family: inherit;
  font-size: var(--prose);
  line-height: 1.7;
}
#body::placeholder { color: var(--faint); }
#body:focus { outline: none; }

/* Preview: a reader's-eye view of the post. Narrow screens swap it with the
   editor; wide screens (.split) put it in a sticky pane beside the editor. */
#preview { font-size: 17px; line-height: 1.7; }
.pv-cover { display: block; width: 100%; aspect-ratio: 1920 / 800; object-fit: cover; border-radius: var(--radius); margin: 0 0 24px; }
.pv-title { font-size: 34px; line-height: 1.2; letter-spacing: -0.02em; margin: 0 0 12px; }
.pv-lede { color: var(--muted); margin: 0 0 24px; }
#preview .prose > :first-child { margin-top: 0; }
#preview h1, #preview h2, #preview h3, #preview h4 { line-height: 1.3; letter-spacing: -0.015em; margin: 1.6em 0 .5em; }
#preview h2 { font-size: 1.5em; }
#preview h3 { font-size: 1.2em; }
#preview p, #preview ul, #preview ol, #preview table, #preview pre, #preview blockquote { margin: 0 0 1.1em; }
#preview ul, #preview ol { padding-left: 1.5em; }
#preview li { margin-bottom: .3em; }
#preview a { color: var(--fg); text-decoration: underline; text-underline-offset: 2px; }
#preview hr { border: 0; border-top: 1px solid var(--line-strong); margin: 2em 0; }
#preview pre {
  background: color-mix(in srgb, var(--fg) 5%, var(--surface));
  padding: 12px 14px;
  border-radius: var(--radius);
  overflow-x: auto;
  font-size: 14px;
  line-height: 1.55;
}
#preview code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.9em; }
#preview :not(pre) > code { background: color-mix(in srgb, var(--fg) 6%, var(--surface)); padding: 1px 5px; border-radius: 4px; }
#preview blockquote { margin-inline: 0; padding-left: 16px; border-left: 2px solid var(--line-strong); color: var(--muted); }
#preview img { max-width: 100%; height: auto; }
#preview table { border-collapse: collapse; display: block; overflow-x: auto; font-size: .95em; }
#preview th, #preview td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
#preview th { background: color-mix(in srgb, var(--fg) 4%, var(--surface)); }

@media (min-width: 1100px) {
  .editor.split {
    max-width: 1500px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    column-gap: 48px;
    grid-auto-rows: min-content;
    align-content: start;
  }
  .editor.split > * { grid-column: 1; }
  .editor.split > #preview {
    grid-column: 2;
    grid-row: 1 / span 40;
    position: sticky;
    top: 24px;
    align-self: start;
    max-height: calc(100dvh - 48px);
    overflow-y: auto;
    padding: 24px 28px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
  }
}

.slug-row { display: flex; gap: 10px; margin: 0 0 12px; }
.slug-row select { width: auto; }
.slug-field {
  flex: 1;
  display: flex;
  align-items: center;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--bg);
  overflow: hidden;
}
.slug-field:focus-within { border-color: var(--fg); }
.slug-prefix { padding: 0 2px 0 12px; color: var(--faint); white-space: nowrap; }
.slug-field input { border: 0; background: transparent; padding-left: 0; }
.slug-field input:focus { outline: none; }

#excerpt { resize: vertical; margin: 0 0 18px; }

.ed-card {
  display: grid;
  gap: 12px;
  margin-top: 28px;
  padding: 16px 18px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.ed-card p { margin: 0; }
.ed-card__head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.ed-card__head .muted { font-size: 13px; margin-top: 2px; }
.cover-form { display: flex; gap: 8px; }
.cover-box {
  aspect-ratio: 1920 / 800;
  display: grid;
  place-items: center;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  overflow: hidden;
}
.cover-box[data-state="set"] { border-style: solid; }
.cover-box img { width: 100%; height: 100%; object-fit: cover; }
#cover-remove { justify-self: start; }

.hint {
  margin-top: 30px;
  font-size: 13.5px;
  color: var(--faint);
}
.hint kbd {
  font: inherit;
  border: 1px solid var(--line-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 5px;
}

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

.ed-card h2, .pf-section h2 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }

.field { display: grid; gap: 6px; }
.field label { font-size: 13.5px; color: var(--muted); }

input, textarea, select {
  font: inherit;
  width: 100%;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 9px 12px;
  transition: border-color 160ms ease;
}
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { border-color: var(--fg); }

/* ---------- post list ---------- */

.list-head {
  display: flex;
  align-items: baseline;
  gap: 12px;
  max-width: 820px;
  margin: 40px auto 8px;
  padding: 0 20px;
}
.list-head h1 { flex: 1; margin: 0; font-size: 26px; letter-spacing: -0.02em; }

.posts {
  max-width: 820px;
  margin: 0 auto;
  padding: 0 20px 80px;
}

.post-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 15px 12px;
  margin: 0 -12px;
  border-radius: var(--radius);
  color: inherit;
  text-decoration: none;
  border-bottom: 1px solid var(--line);
  transition: background-color 140ms ease;
}
@media (hover: hover) and (pointer: fine) {
  .post-row:hover { background: color-mix(in srgb, var(--fg) 4%, transparent); }
}
.post-row .t { flex: 1; font-weight: 500; }
.post-row .meta { color: var(--faint); font-size: 13.5px; font-variant-numeric: tabular-nums; }

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--line-strong);
  flex: none;
}
.dot[data-status="published"] { background: var(--ok); }
.dot[data-status="unlisted"] { background: var(--muted); }

.empty { color: var(--faint); padding: 40px 0; text-align: center; }

/* portfolio list: grouped, drag-reorderable */
.pf-msg { max-width: 820px; margin: 0 auto; padding: 0 20px; min-height: 1.2em; }
.pf-group { margin-bottom: 28px; }
.pf-group-title { margin: 24px 0 4px; font-size: 12px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--faint); }
.pf-group .post-row { position: relative; }
.pf-group .post-row .t { color: inherit; text-decoration: none; }
.grip { color: var(--faint); cursor: grab; user-select: none; flex: none; }
.dragging { opacity: 0.4; }
.drop-before { box-shadow: inset 0 2px 0 var(--fg); }
.drop-after { box-shadow: inset 0 -2px 0 var(--fg); }
.pf-block { margin: 4px 0 18px; border-radius: var(--radius); }
.pf-block-head {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px 6px; margin: 0 -12px;
  border-bottom: 1px solid var(--line-strong);
}
.pf-block-name { flex: 1; margin: 0; font-size: 15px; font-weight: 600; letter-spacing: -0.01em; }
.row-btn {
  flex: none; width: 28px; height: 28px; padding: 0; line-height: 1;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: transparent; color: var(--muted); cursor: pointer;
}
.row-btn:hover:not(:disabled) { color: var(--fg); border-color: var(--line-strong); }
.row-btn:disabled { opacity: 0.3; cursor: default; }
.badge {
  flex: none; font-size: 11.5px; color: var(--muted);
  border: 1px solid var(--line); border-radius: 999px; padding: 1px 8px;
}

/* ---------- login ---------- */

.card {
  max-width: 320px;
  margin: 18vh auto 0;
  display: grid;
  gap: 12px;
  padding: 0 20px;
}
.card h1 { margin: 0 0 4px; font-size: 24px; letter-spacing: -0.02em; }
.error { color: var(--danger); font-size: 13.5px; margin: 0; }
.muted { color: var(--muted); }

@media (max-width: 640px) {
  .editor { padding-top: 28px; }
  #title { font-size: 30px; }
}

/* ---------- sidebar ---------- */

.shell { display: grid; grid-template-columns: 200px 1fr; min-height: 100dvh; }

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 18px 12px;
  border-right: 1px solid var(--line);
  background: color-mix(in srgb, var(--fg) 2%, var(--bg));
}
.sidebar .brand { margin-bottom: 14px; font-weight: 600; color: var(--fg); text-decoration: none; }
.sidebar a {
  padding: 7px 10px;
  border-radius: 7px;
  color: var(--muted);
  text-decoration: none;
  transition: background-color 140ms ease, color 140ms ease;
}
@media (hover: hover) and (pointer: fine) {
  .sidebar a:hover { background: color-mix(in srgb, var(--fg) 6%, transparent); color: var(--fg); }
}
.sidebar .sub { margin-left: 10px; font-size: 0.92em; }
.sidebar a[aria-current="page"] {
  background: color-mix(in srgb, var(--fg) 8%, transparent);
  color: var(--fg);
}
.sidebar button { margin-top: auto; text-align: left; }
.pane { min-width: 0; }

@media (max-width: 720px) {
  .shell { grid-template-columns: 1fr; }
  /* Wrapping matters now that Posts has three status sub-links: six links plus
     two buttons do not fit one phone-width line. */
  .sidebar { flex-direction: row; flex-wrap: wrap; align-items: center; border-right: 0; border-bottom: 1px solid var(--line); }
  .sidebar .brand { margin: 0 8px 0 0; }
  .sidebar button { margin-top: 0; margin-left: auto; }
  /* The indent reads as hierarchy in a column and as stray gap in a row. */
  .sidebar .sub { margin-left: 0; }
}

/* Reduced motion keeps opacity, drops movement — gentler, not absent. */
@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 120ms !important; }
  button:active, .button:active { transform: none; }
}

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

.pf-editor {
  max-width: 720px;
  margin: 0 auto;
  padding: 32px 20px 40vh;
  display: grid;
  gap: 18px;
}

.pf-section {
  display: grid;
  gap: 14px;
  padding: 18px 20px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.pf-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  padding: 6px 8px;
  background: var(--bg);
  transition: border-color 160ms ease;
}
.tags:focus-within { border-color: var(--fg); }

.tags input {
  flex: 1;
  min-width: 10ch;
  border: 0;
  padding: 4px 2px;
  background: transparent;
}
.tags input:focus { outline: none; }

.tag {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-size: 13px;
  line-height: 1.4;
  padding: 3px 6px 3px 10px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--fg) 6%, var(--surface));
  border: 1px solid var(--line);
  white-space: nowrap;
}
.tag button {
  border: 0;
  background: transparent;
  padding: 2px 4px;
  color: var(--faint);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
}
.tag button:hover { color: var(--danger); }

@media (max-width: 600px) {
  .pf-row { grid-template-columns: 1fr; }
  .pf-editor { padding-top: 20px; }
}

/* posts: view toggle + year/month chips, by-date year headings */
.view-bar { max-width: 820px; margin: 12px auto 0; padding: 0 20px; display: flex; flex-direction: column; gap: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font: inherit; font-size: 13px; padding: 4px 11px; border: 1px solid var(--line-strong); border-radius: 999px; background: transparent; color: var(--faint); cursor: pointer; }
.chip[aria-pressed="true"] { background: var(--fg); border-color: var(--fg); color: var(--bg); }
.chip:disabled { opacity: 0.35; cursor: default; }
.year-title { margin: 22px 0 2px; font-size: 12px; font-weight: 500; color: var(--faint); }
.chips[hidden], .view-bar[hidden] { display: none; }

/* Cover sits between the excerpt and the body, so it is visible without scrolling. */
#cover { margin: 0 0 20px; }

.src { display: flex; gap: 8px; align-items: baseline; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.src:last-child { border-bottom: none; }
.src input[type="checkbox"] { flex: none; width: auto; margin: 0; align-self: center; accent-color: var(--fg); }
.src { cursor: pointer; }
.src.off { cursor: default; }
.src span:first-of-type { flex: 1; }
.ai-draft, .ai-notes, .ai-reasoning {
  font: inherit; font-size: 14px; white-space: pre-wrap; word-break: break-word;
  background: color-mix(in srgb, var(--fg) 4%, transparent);
  border-radius: var(--radius); padding: 14px; max-height: 32rem; overflow: auto;
  margin: 0;
}
.ai-notes { max-height: 14rem; }
.ai-reasoning { font-size: 13px; color: var(--muted); }
.chip.sm { font-size: 11.5px; padding: 2px 8px; }
.src.off { opacity: .5; }


/* ---------- Links page ----------
   One 820px column like Posts. Spacing runs on a 4/8/16/24 rhythm: tools sit 16
   under the title, 8 between controls, the count 8 under the tools, rows 16 tall. */

.link-tools,
.link-count,
.cat-panel { max-width: 820px; margin-inline: auto; padding-inline: 20px; }

.link-tools { margin-top: 20px; display: grid; grid-template-columns: 1fr auto; gap: 10px; }
.link-tools input[type="search"] { padding: 9px 12px; font-size: 14.5px; }
.link-count { margin-top: 14px; min-height: 1.2em; font-size: 13px; font-variant-numeric: tabular-nums; }
.link-count + .pf-msg { padding-block: 0; min-height: 0; }

/* Link rows: title over source, badges on the right. Two lines scan faster than
   one crowded line, and a long title truncates instead of pushing the meta off. */
.link-row { padding-block: 12px; align-items: center; }
.link-main { flex: 1; min-width: 0; display: grid; gap: 2px; }
.link-main .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link-main .note { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link-tags { display: flex; gap: 6px; flex: none; }
.link-row.is-failed .t { color: var(--muted); }

/* Categories panel. Occasional, so it eases in; tens-of-times-a-day controls don't. */
.cat-panel { margin-top: 16px; }
.cat-panel .ed-card { margin-top: 0; gap: 16px; }
.cat-panel .muted { font-size: 13px; }
.cat-form { display: flex; gap: 8px; }
.cat-form input { max-width: 14rem; }
.hint-sm { font-size: 13px; color: var(--faint); margin: -8px 0 0; }
.chip.cat { display: inline-flex; align-items: center; gap: 6px; padding: 5px 6px 5px 11px; }
.chip.cat .n { font-variant-numeric: tabular-nums; color: var(--faint); }
.chip.cat button {
  all: unset; box-sizing: border-box; display: grid; place-items: center; width: 20px; height: 20px;
  border-radius: 50%; cursor: pointer; color: var(--faint); line-height: 1;
  transition: background-color 120ms ease, color 120ms ease, transform 120ms var(--ease-out);
}
.chip.cat button:active { transform: scale(0.9); }
@media (hover: hover) and (pointer: fine) {
  .chip.cat button:hover { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
}
.cat-panel { transition: opacity 160ms var(--ease-out), transform 160ms var(--ease-out); }
@starting-style { .cat-panel { opacity: 0; transform: translateY(-4px); } }

@media (max-width: 600px) {
  .link-tools { grid-template-columns: 1fr; }
  .sel-btn, #link-category-wrap { width: 100%; }
  .link-tags { display: none; } /* the badges are in the filter; keep the row to title + source */
}
@media (prefers-reduced-motion: reduce) {
  .cat-panel { transition: opacity 160ms ease; transform: none; }
}

#ai-sources { margin-top: 8px; max-height: 14rem; overflow-y: auto; border-top: 1px solid var(--line); }
.src { padding: 9px 2px; }
.src:hover { background: color-mix(in srgb, var(--fg) 3%, transparent); }
.cat-panel .error:empty { display: none; }

/* Category dropdown. A listbox popover on the same .menu surface as the editor's
   ⋯ menu, so it looks like the rest of the app and scales from its trigger. A
   native <select> scales to any count but its popup is the OS's, not ours. */
.link-tools :focus-visible, .ai-panel textarea:focus-visible, .model-form input:focus-visible { outline: none; border-color: var(--fg); box-shadow: 0 0 0 3px color-mix(in srgb, var(--fg) 14%, transparent); }
#link-category-wrap { display: grid; }
.sel-btn {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-width: 12.5rem; padding: 9px 12px; font-size: 14.5px; text-align: left;
  background: var(--bg); border-color: var(--line-strong);
}
.sel-btn svg { flex: none; color: var(--faint); transition: transform 160ms var(--ease-out); }
.sel-btn[aria-expanded="true"] svg { transform: rotate(180deg); }
.menu.sel {
  top: calc(100% + 6px); left: auto; right: 0; min-width: 100%; width: max-content; max-width: 22rem;
  max-height: min(20rem, 60vh); overflow-y: auto; transform-origin: top right; z-index: 5;
  visibility: visible; transition: opacity 150ms var(--ease-out), transform 150ms var(--ease-out), visibility 0s;
}
/* Hidden items must leave the tab order too, but only after the fade-out ends. */
.menu.sel[hidden] { visibility: hidden; transition: opacity 150ms var(--ease-out), transform 150ms var(--ease-out), visibility 0s linear 150ms; }
.menu .opt { display: flex; align-items: center; gap: 8px; }
.opt .ck { width: 14px; flex: none; font-size: 12px; color: var(--muted); visibility: hidden; }
.opt[aria-selected="true"] { font-weight: 500; }
.opt[aria-selected="true"] .ck { visibility: visible; }
.opt .lbl { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.opt .n { font-size: 13px; color: var(--faint); font-variant-numeric: tabular-nums; }
.menu .opt:focus-visible { outline: none; background: color-mix(in srgb, var(--fg) 8%, transparent); box-shadow: none; }
#cat-toggle { margin-right: -11px; } /* the quiet button's own padding must not push its text off the column edge */
@media (prefers-reduced-motion: reduce) {
  .sel-btn svg { transition: none; }
  .menu.sel, .menu.sel[hidden] { transition: opacity 150ms ease, visibility 0s linear 150ms; transform: none; }
}

/* ---------- AI panel ----------
   A non-modal drawer over the right edge: the body stays editable beside it, so
   there is no scrim — a translucent surface and a shadow carry the depth.
   `inert` (not hidden) keeps it animatable and out of the tab order when closed. */

.ai-panel {
  position: fixed; top: 58px; right: 0; bottom: 0; z-index: 20; /* 58px = the sticky top bar, so AI / Preview / ⋯ stay clickable */
  width: min(27rem, 100vw);
  display: flex; flex-direction: column;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  -webkit-backdrop-filter: blur(18px) saturate(160%); backdrop-filter: blur(18px) saturate(160%);
  border-left: 1px solid var(--line); border-top: 1px solid var(--line); box-shadow: var(--shadow);
  transform: translateX(100%); visibility: hidden;
  transition: transform 220ms var(--ease-drawer), visibility 0s linear 220ms;
}
.ai-panel[data-open="true"] { transform: none; visibility: visible; transition: transform 220ms var(--ease-drawer), visibility 0s; }
@media (prefers-reduced-transparency: reduce) { .ai-panel { background: var(--surface); backdrop-filter: none; } }
@media (prefers-reduced-motion: reduce) {
  .ai-panel, .ai-panel[data-open="true"] { transition: opacity 160ms ease, visibility 0s; transform: none; opacity: 0; }
  .ai-panel[data-open="true"] { opacity: 1; }
}

.ai-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 10px 20px; }
.ai-head h2 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.ai-scroll { flex: 1; overflow-y: auto; padding: 4px 20px 24px; display: grid; gap: 16px; align-content: start; }
.ai-field { display: grid; gap: 8px; }
.ai-field label { font-size: 13.5px; color: var(--muted); }
#ai-idea { min-height: 6.5rem; padding: 10px 12px; }
#ai-sources-wrap summary { cursor: pointer; font-size: 14px; }
#ai-sources { margin-top: 8px; max-height: 12rem; overflow-y: auto; border-top: 1px solid var(--line); }
.ai-note { margin: 0; font-size: 13px; line-height: 1.5; color: var(--muted); } /* --faint is 4:1 on this surface; policy text must clear AA */
.ai-foot { display: flex; align-items: center; gap: 8px; padding: 12px 20px 16px; border-top: 1px solid var(--line); }
.ai-keys { margin-left: auto; color: var(--faint); font-size: 12.5px; }
.ai-keys kbd { font: inherit; border: 1px solid var(--line-strong); border-bottom-width: 2px; border-radius: 5px; padding: 1px 5px; }

#ai-status:empty { display: none; }
#ai-status { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }
#ai-status.is-error { color: var(--danger); }
#ai-status .spin { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--line-strong); border-top-color: var(--fg); animation: ai-spin 700ms linear infinite; flex: none; }
@keyframes ai-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { #ai-status .spin { animation-duration: 2.4s; } }

.ai-result { display: grid; gap: 12px; }
.ai-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.ai-actions button { padding: 6px 12px; font-size: 14px; }
.ai-render {
  font-size: 15px; line-height: 1.65; padding: 14px 16px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--bg); overflow-wrap: anywhere;
}
.ai-render > :first-child { margin-top: 0; } .ai-render > :last-child { margin-bottom: 0; }
.ai-render h1, .ai-render h2, .ai-render h3 { font-size: 1.1em; line-height: 1.3; margin: 1.2em 0 .4em; letter-spacing: -0.01em; }
.ai-render p, .ai-render ul, .ai-render ol, .ai-render pre, .ai-render blockquote { margin: .7em 0; }
.ai-render pre { overflow-x: auto; font-size: 13px; padding: 10px 12px; border-radius: 6px; background: color-mix(in srgb, var(--fg) 5%, transparent); }
.ai-render img { max-width: 100%; height: auto; }
.ai-result details summary { cursor: pointer; font-size: 13.5px; color: var(--muted); }
.ai-result .muted { font-size: 13px; margin: 0; }
#ai-open[aria-pressed="true"] { color: var(--fg); }
button:disabled { opacity: .5; cursor: default; }
button:disabled:active { transform: none; }

/* AI panel on small screens: a full sheet. The nav stacks on top there, so a
   panel hung under a 58px bar would cover it; own the whole screen instead. */
.ai-head .quiet { min-width: 44px; min-height: 44px; }
@media (max-width: 720px) {
  .ai-panel { top: 0; left: 0; right: 0; width: auto; border-left: 0; border-top: 0; z-index: 30; } /* left/right, not 100vw: vw includes the scrollbar */
  .ai-head { padding-top: max(14px, env(safe-area-inset-top)); }
  .ai-foot { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
}
@media (pointer: coarse) {
  .ai-keys { display: none; }
  .ai-actions button, .ai-foot button { min-height: 44px; }
  .opt { min-height: 44px; }
}

/* ---------- Writer flow: modes, rewrite, from-a-link ---------- */

.ai-tabs { display: inline-flex; padding: 3px; gap: 2px; border-radius: 10px; background: color-mix(in srgb, var(--fg) 7%, transparent); }
.ai-tabs button {
  border: 0; background: transparent; color: var(--muted); padding: 6px 14px; border-radius: 8px; font-size: 14px;
  transition: background-color 140ms ease, color 140ms ease, transform 160ms var(--ease-out);
}
.ai-tabs button[aria-selected="true"] { background: var(--surface); color: var(--fg); box-shadow: 0 1px 2px rgba(0,0,0,.18); }
@media (hover: hover) and (pointer: fine) { .ai-tabs button[aria-selected="false"]:hover { color: var(--fg); background: transparent; } }

#ai-mode-draft, #ai-mode-rewrite { display: grid; gap: 16px; }
.ai-from { margin: 0; font-size: 13.5px; color: var(--muted); padding: 8px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: color-mix(in srgb, var(--fg) 3%, transparent); }
.ai-from b { color: var(--fg); font-weight: 500; }
.ai-hint { margin: 0; font-size: 14px; color: var(--muted); }
.ai-hint kbd, .ai-keys kbd { font: inherit; border: 1px solid var(--line-strong); border-bottom-width: 2px; border-radius: 5px; padding: 1px 5px; }
.ai-quote {
  margin: 0; padding: 10px 14px; font-size: 14px; line-height: 1.55; color: var(--muted); max-height: 9rem; overflow: auto;
  border-left: 2px solid var(--line-strong); background: color-mix(in srgb, var(--fg) 3%, transparent); border-radius: 0 var(--radius) var(--radius) 0; overflow-wrap: anywhere;
}
#ai-rewrite-chips { gap: 8px; }
#ai-rewrite-chips .chip { font-size: 13.5px; padding: 7px 13px; color: var(--muted); cursor: pointer; }
@media (hover: hover) and (pointer: fine) { #ai-rewrite-chips .chip:hover:not(:disabled) { color: var(--fg); border-color: var(--fg); } }
.ai-foot > div { display: flex; gap: 8px; align-items: center; }
.ai-streaming .ai-render { opacity: .92; white-space: pre-wrap; } /* plain text until the final render */
.ai-stopped { color: var(--muted); font-size: 13px; margin: 0; }
.ai-actions button:disabled { opacity: .45; }

/* Links page: a row is a link plus a "Write" action, so the action cannot live
   inside the <a> (a button in a link is invalid and eats the click). */
.link-row { gap: 8px; }
.link-row > a.link-main { color: inherit; text-decoration: none; flex: 1; min-width: 0; display: grid; gap: 2px; }
.link-row .write-btn { flex: none; padding: 5px 12px; font-size: 13.5px; opacity: 0; transition: opacity 140ms ease, transform 160ms var(--ease-out); }
.link-row:hover .write-btn, .link-row:focus-within .write-btn { opacity: 1; }
@media (hover: none) { .link-row .write-btn { opacity: 1; } }

/* What the model is working through, live. aria-hidden: it is texture for sighted
   authors while they wait, not content to read out; the status line carries the news. */
.ai-thought {
  margin: -6px 0 0; font-size: 12.5px; line-height: 1.5; color: var(--faint); font-style: italic;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; line-clamp: 3; overflow: hidden; overflow-wrap: anywhere;
}

/* ---------- Settings ----------
   A section nav on the left and one section at a time on the right, on the same
   820px column as every other admin page. New settings are new sections: add a nav
   link and a <section data-section>. Models are cards, one per connection. */

.settings { max-width: 820px; margin: 20px auto 0; padding: 0 20px 80px; display: grid; grid-template-columns: 9.5rem 1fr; gap: 32px; }
.settings-nav { display: grid; gap: 2px; align-content: start; position: sticky; top: 24px; align-self: start; }
.settings-nav a { display: block; padding: 7px 11px; border-radius: 8px; color: var(--muted); text-decoration: none; font-size: 14.5px; transition: background-color 140ms ease, color 140ms ease; }
.settings-nav a[aria-current="true"] { background: color-mix(in srgb, var(--fg) 7%, transparent); color: var(--fg); }
@media (hover: hover) and (pointer: fine) { .settings-nav a:not([aria-current="true"]):hover { color: var(--fg); } }
.settings-body { min-width: 0; }
.settings-sec { display: grid; gap: 16px; }
.sec-head h2 { margin: 0; font-size: 18px; letter-spacing: -0.01em; }
.sec-head p { margin: 4px 0 0; font-size: 14px; color: var(--muted); max-width: 52ch; }

.model-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.model-card { display: grid; gap: 14px; padding: 16px 18px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
.model-head { display: flex; align-items: center; gap: 12px; }
.model-mark { flex: none; width: 36px; height: 36px; display: grid; place-items: center; border-radius: 9px; font-weight: 600; font-size: 15px; background: color-mix(in srgb, var(--fg) 8%, transparent); color: var(--fg); }
.model-title { flex: 1; min-width: 0; display: grid; gap: 1px; }
.model-title b { font-weight: 600; letter-spacing: -0.005em; }
.model-title span { font-size: 13px; color: var(--muted); }
.model-status { display: inline-flex; align-items: center; gap: 8px; font-size: 13.5px; color: var(--muted); white-space: nowrap; }
.key-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.key-dot[data-state="ok"] { background: var(--ok); }
.key-dot[data-state="rejected"] { background: var(--danger); }
.key-dot[data-state="checking"] { background: var(--muted); animation: dot-pulse 1s ease-in-out infinite; }
@keyframes dot-pulse { 50% { opacity: .3; } }
@media (prefers-reduced-motion: reduce) { .key-dot[data-state="checking"] { animation: none; } }

.model-meta { margin: 0; font-size: 13.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.model-meta code { font: inherit; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; color: var(--fg); }
.model-warn { margin: 0; padding: 10px 12px; font-size: 14px; border: 1px solid var(--danger); border-radius: var(--radius); color: var(--danger); background: color-mix(in srgb, var(--danger) 8%, transparent); }
.model-actions { display: flex; flex-wrap: wrap; gap: 4px; margin-left: -11px; }
.model-actions button { font-size: 14px; }

/* The key form appears in place. It eases in with opacity and a few pixels of
   travel — never a height animation, which reflows everything below it. */
.model-form { display: grid; gap: 8px; transition: opacity 160ms var(--ease-out), transform 160ms var(--ease-out); }
@starting-style { .model-form { opacity: 0; transform: translateY(-4px); } }
.model-form label { font-size: 13.5px; color: var(--muted); }
.model-row { display: flex; gap: 8px; }
.model-row input { flex: 1; min-width: 0; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 14px; }
.model-msg { margin: 0; font-size: 14px; }
.model-msg:empty { display: none; }
.model-msg.is-ok { color: var(--ok); }

/* A label that changes ("Save and test" → "Checking…" → "Saved") is blurred through
   the swap instead of hard-cut, so it reads as one button changing state rather than
   two labels trading places. */
.btn-lbl { display: inline-block; transition: opacity 110ms ease, filter 110ms ease; }
.btn-lbl.swap { opacity: 0; filter: blur(2px); }
@media (prefers-reduced-motion: reduce) { .btn-lbl { transition: none; } .btn-lbl.swap { filter: none; } }

@media (max-width: 720px) {
  .settings { grid-template-columns: 1fr; gap: 16px; }
  .settings-nav { position: static; display: flex; gap: 4px; overflow-x: auto; }
  .settings-nav a { white-space: nowrap; }
  .model-row { flex-direction: column; }
  .model-head { flex-wrap: wrap; }
}

/* The AI panel's heads-up: a missing or rejected key found before a long generation. */
.ai-banner { margin: 0; padding: 10px 12px; font-size: 14px; line-height: 1.5; border: 1px solid var(--danger); border-radius: var(--radius); color: var(--fg); background: color-mix(in srgb, var(--danger) 9%, transparent); }
.ai-banner a, #ai-status a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }

/* ![caption](url "center") renders as a centered figure; the caption is the alt text. */
#preview figure.img-center { margin: 1.5em auto; text-align: center; }
#preview figure.img-center img { margin-inline: auto; }
#preview figure.img-center figcaption { margin-top: .5em; font-size: .875em; opacity: .7; text-align: center; }
.ai-render figure.img-center { margin: 1.5em auto; text-align: center; }
.ai-render figure.img-center img { margin-inline: auto; }
.ai-render figure.img-center figcaption { margin-top: .5em; font-size: .875em; opacity: .7; text-align: center; }

/* Ollama page: reuses the Settings column and .model-card; only the spine is new. */
.ol { max-width: 820px; margin: 20px auto 0; padding: 0 20px 80px; display: grid; gap: 16px; }
.ol-flow { gap: 20px; }
.ol-flow > li { position: relative; }
.ol-flow > li + li::before { content: ""; position: absolute; left: 35px; top: -20px; height: 20px; border-left: 1px solid var(--line-strong); }
.ol-flow .model-title span { font-variant-numeric: tabular-nums; }
.ol-count { font-variant-numeric: tabular-nums; color: var(--fg); }

/* ---------- gallery categories: card grid + edit popover ---------- */
/* Hex values below are only the two preview backgrounds (the gallery's cream public page
   and dark app) and the pill ink, which must not follow this page's theme. */

.gcat { max-width: 820px; margin: 20px auto 0; padding: 0 20px 80px; }
.gc-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; }
.gc-head .primary { flex: none; }
.gcat > .error:empty { display: none; }
.gcat > .error { margin: 12px 0 0; }

.gc-grid { list-style: none; margin: 20px 0 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.gc-empty { grid-column: 1 / -1; padding: 28px 16px; border: 1px dashed var(--line-strong); border-radius: 12px; color: var(--muted); text-align: center; }
.gc-card { position: relative; }
.gc-card.dragging { opacity: .4; }
.gc-card.drop-target .gc-open { border-color: var(--fg); }
button.gc-open {
  width: 100%; min-height: 112px; display: flex; flex-direction: column; align-items: flex-start; gap: 20px;
  padding: 16px; text-align: left; color: var(--fg);
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  transition: transform 160ms var(--ease-out), border-color 160ms ease;
}
button.gc-open:active { transform: scale(.98); }
button.gc-open[aria-expanded="true"] { border-color: var(--fg); }
.gc-foot { margin-top: auto; width: 100%; display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.gc-foot code { font-size: 12px; color: var(--faint); letter-spacing: .02em; background: none; padding: 0; }

.gc-pill {
  display: inline-flex; align-items: center; max-width: 100%; height: 28px; padding: 0 12px;
  border-radius: 999px; background: var(--pill); color: var(--ink);
  font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.gc-pop {
  position: fixed; z-index: 30; width: 300px;
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: 12px; box-shadow: var(--shadow);
  transform-origin: var(--origin, top left);
  transition: opacity 160ms var(--ease-out), transform 160ms var(--ease-out);
}
.gc-pop[hidden] { display: none; }
.gc-pop[data-state="closed"] { opacity: 0; transform: scale(.96); transition-duration: 120ms; }
.gc-pop-body { padding: 16px; display: grid; gap: 16px; }
.gc-field { display: grid; gap: 6px; }
.gc-field label, .gc-label { font-size: 12px; font-weight: 500; color: var(--muted); }
.gc-pop input:not([type="color"]) { height: 36px; padding: 0 10px; }

.gc-trans { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.gc-trans:empty { display: none; }
.gc-trans li { display: grid; grid-template-columns: 52px 1fr; gap: 8px; }
.gc-trans .gc-code { text-transform: lowercase; font-family: ui-monospace, Menlo, monospace; font-size: 13px; text-align: center; padding: 0 4px; }
button.gc-add-lang { justify-self: start; padding: 4px 0; font-size: 13px; }

.gc-swatches { display: grid; grid-template-columns: repeat(8, 24px); gap: 8px; }
.gc-swatch { width: 24px; height: 24px; padding: 0; border-radius: 6px; border: 0; background: var(--sw); box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--fg) 14%, transparent); }
.gc-swatch[aria-checked="true"] { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--fg); }
.gc-hexrow { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.gc-chip { position: relative; flex: none; width: 36px; height: 36px; border-radius: 8px; background: var(--pill); box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--fg) 14%, transparent); cursor: pointer; overflow: hidden; }
.gc-chip input { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; padding: 0; border: 0; }
.gc-pop input.gc-hex { width: 120px; font-family: ui-monospace, Menlo, monospace; font-size: 13px; text-transform: uppercase; }
.gc-hex[aria-invalid="true"] { border-color: var(--danger); }

.gc-prev { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.gc-tile { margin: 0; height: 72px; border-radius: 8px; padding: 10px 8px 8px; display: grid; grid-template-rows: 1fr auto; justify-items: center; align-items: center; min-width: 0; }
.gc-tile figcaption { font-size: 11px; }
.gc-tile--light { background: #f2ebe1; color: #6b6b66; }
.gc-tile--dark { background: #0e0e10; color: #9b9a95; box-shadow: inset 0 0 0 1px var(--line); }

.gc-bar { border-top: 1px solid var(--line); padding: 12px 16px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.gc-move { display: flex; align-items: center; gap: 2px; }
.gc-move button { padding: 6px 10px; }
.gc-pos { margin-left: 6px; font-size: 12px; color: var(--faint); font-variant-numeric: tabular-nums; }
.gc-bar[data-mode="create"] .gc-move, .gc-bar[data-mode="create"] #gc-delete { display: none; }
.gc-bar[data-mode="create"] { justify-content: flex-end; }

@media (max-width: 600px) {
  .gc-head { flex-direction: column; align-items: stretch; }
  .gc-pop { left: 0 !important; right: 0; top: auto !important; bottom: 0; width: auto; border-radius: 12px 12px 0 0; max-height: 85vh; overflow: auto; transform-origin: bottom center; }
  .gc-pop[data-state="closed"] { transform: translateY(24px); }
}
@media (prefers-reduced-motion: reduce) {
  .gc-pop, button.gc-open { transition: none; }
}
