:root {
  --bg: #fbfaf7;
  --fg: #1d1d1b;
  --muted: #6b6b66;
  --line: #dcdad2;
  --accent: #2b5fd9;
  --accent-fg: #fff;
  --panel: #fff;
  --code: #efede6;
  --size: 1.2rem;
}
:root[data-theme="dark"] {
  --bg: #17171a;
  --fg: #e8e6e1;
  --muted: #9a9892;
  --line: #34343a;
  --accent: #7aa2ff;
  --accent-fg: #0d0d10;
  --panel: #1f1f24;
  --code: #26262c;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17171a; --fg: #e8e6e1; --muted: #9a9892; --line: #34343a;
    --accent: #7aa2ff; --accent-fg: #0d0d10; --panel: #1f1f24; --code: #26262c;
  }
}

* { box-sizing: border-box; }
html { color-scheme: light dark; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }

/* ---------- Editor ---------- */
#editor { max-width: 760px; margin: 0 auto; padding: 32px 16px 48px; }
#editor header h1 { margin: 0 0 4px; font-size: 1.8rem; }
#editor header p { margin: 0 0 24px; color: var(--muted); }
label { display: block; font-weight: 600; margin: 16px 0 6px; }
label.inline { display: inline; margin: 0; }
.opt { font-weight: 400; color: var(--muted); }
input[type="text"], textarea, select {
  font: inherit; color: inherit; background: var(--panel);
  border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px;
}
input[type="text"] { width: 100%; }
textarea { width: 100%; min-height: 50vh; resize: vertical; line-height: 1.55; }
input:focus-visible, textarea:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 14px; }
.spacer { flex: 1; }
.hint { color: var(--muted); font-size: .875rem; margin: 8px 0 0; }
.row .hint { margin: 0; }
kbd { background: var(--code); border-radius: 4px; padding: 1px 5px; font-size: .8rem; }

button {
  font: inherit; font-weight: 600; cursor: pointer; border-radius: 8px;
  padding: 10px 18px; border: 1px solid var(--line); background: var(--panel); color: var(--fg);
}
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
button:hover { filter: brightness(.96); }

/* ---------- Reader ---------- */
#toolbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; gap: 8px; align-items: center;
  padding: 10px 16px; background: var(--bg); border-bottom: 1px solid var(--line);
}
#toolbar button { padding: 6px 12px; }
main { padding: 8px 16px 96px; }
#article {
  max-width: 42rem; margin: 0 auto;
  font-family: Georgia, "Iowan Old Style", "Times New Roman", serif;
  font-size: var(--size); line-height: 1.7;
}
#article h1, #article h2, #article h3, #article h4 {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.25; margin: 1.8em 0 .5em;
}
#article h1:first-child { margin-top: 1em; font-size: 2em; }
#article p, #article ul, #article ol, #article blockquote, #article pre, #article table { margin: 0 0 1.1em; }
#article blockquote { border-left: 3px solid var(--line); padding-left: 1em; color: var(--muted); margin-left: 0; }
#article code { background: var(--code); border-radius: 4px; padding: .1em .35em; font-size: .85em; }
#article pre { background: var(--code); padding: 12px 14px; border-radius: 8px; overflow-x: auto; }
#article pre code { background: none; padding: 0; }
#article img { max-width: 100%; height: auto; }
#article a { color: var(--accent); }
#article table { border-collapse: collapse; display: block; overflow-x: auto; }
#article th, #article td { border: 1px solid var(--line); padding: 6px 10px; }
#article hr { border: 0; border-top: 1px solid var(--line); margin: 2em 0; }

@media (max-width: 480px) {
  #editor { padding-top: 20px; }
  textarea { min-height: 40vh; }
}

.status { min-height: 1.5em; margin: 12px 0 0; color: var(--muted); }
.status.error { color: #c0392b; font-weight: 600; }
:root[data-theme="dark"] .status.error { color: #ff8a7a; }
#article .source { font-family: system-ui, sans-serif; font-size: .85rem; color: var(--muted); margin: -.5em 0 1.5em; }

/* Link styled as a button (reader toolbar). */
a.button {
  font: inherit; font-weight: 600; border-radius: 8px; padding: 6px 12px;
  border: 1px solid var(--line); background: var(--panel); color: var(--fg); text-decoration: none;
}
a.button:hover { filter: brightness(.96); }
a.button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Reader: toolbar wrap, split edit layout ---------- */
#toolbar { flex-wrap: wrap; }
#toolbar button, #toolbar a.button { white-space: nowrap; }
#toolbar button[aria-pressed="true"] { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }

#editor-pane { display: none; }
body.editing { height: 100vh; height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
body.editing #toolbar { position: static; flex: none; }
body.editing #workspace {
  flex: 1; min-height: 0; display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}
body.editing #article-pane { overflow: auto; min-height: 0; }
body.editing #editor-pane { display: block; min-height: 0; overflow: hidden; border-left: 1px solid var(--line); }
body.editing #article [data-b] { cursor: text; }
@media (max-width: 900px) {
  body.editing #workspace { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) minmax(0, 1fr); }
  body.editing #editor-pane { border-left: 0; border-top: 1px solid var(--line); }
}

#article .jumped { background: color-mix(in srgb, var(--accent) 22%, transparent); border-radius: 4px; transition: background .3s; }
#article .omitted { color: var(--muted); font-style: italic; font-family: system-ui, sans-serif; font-size: .9em; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  background: var(--fg); color: var(--bg); padding: 8px 14px; border-radius: 8px; font-size: .9rem;
  transition: opacity .2s, transform .2s; z-index: 30;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Ultra-wide screens ---------- */
/* Keep the controls together in the middle (above the seam between article and editor)
   instead of pinned to the far screen edges. */
#toolbar { padding: 10px max(16px, calc((100% - 60rem) / 2)); }

/* Cap the split workspace so each pane stays a readable size on very wide monitors. */
body.editing #workspace { width: 100%; max-width: 2000px; margin-inline: auto; }

/* Wrap the editor text at a comfortable line length rather than at the far edge of a huge pane. */
#editor-pane .cm-content { max-width: 100ch; }
