/* Inkwell — clean, minimal, elegant. */

:root {
  --bg: #fbfbfa;
  --bg-side: #f4f4f2;
  --bg-elev: #ffffff;
  --ink: #24282e;
  --ink-soft: #5c6470;
  --ink-faint: #9aa1ab;
  --line: #e7e6e2;
  --line-soft: #efeeea;
  --accent: #b3552f;
  --accent-soft: #b3552f1a;
  --sel: #f0e5d8;
  --code-bg: #f6f5f2;
  --mark: #fdf3d8;
  --radius: 8px;
  --mono: "SF Mono", "Cascadia Code", "JetBrains Mono", ui-monospace, Menlo, Consolas, monospace;
  --serif: "Iowan Old Style", "Palatino Linotype", Charter, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  --side-w: 248px;
}

html[data-theme="dark"] {
  --bg: #17191d;
  --bg-side: #1c1f24;
  --bg-elev: #22262c;
  --ink: #d8dbe0;
  --ink-soft: #99a0aa;
  --ink-faint: #6b7280;
  --line: #2c3037;
  --line-soft: #262a30;
  --accent: #e08d63;
  --accent-soft: #e08d6326;
  --sel: #3a3428;
  --code-bg: #1f2228;
  --mark: #3d3524;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

#app { display: flex; height: 100vh; overflow: hidden; }

button {
  font: inherit;
  color: var(--ink-soft);
  background: none;
  border: none;
  cursor: pointer;
  border-radius: 6px;
  padding: 4px 8px;
  transition: background .12s ease, color .12s ease;
}
button:hover { background: var(--accent-soft); color: var(--ink); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  color: var(--ink-faint);
}
.btn-icon:hover { color: var(--ink); }

.spacer { flex: 1; }
.muted { color: var(--ink-faint); font-size: 12px; }
.hidden { display: none !important; }

/* ------------------------------------------------------------- sidebar */
#sidebar {
  width: var(--side-w);
  min-width: var(--side-w);
  background: var(--bg-side);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 10px 10px;
}

.side-head { display: flex; align-items: center; padding: 0 6px; }
.brand {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: .10em;
  text-transform: uppercase;
  color: var(--ink-soft);
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}
.brand-mark {
  width: 9px;
  height: 9px;
  border-radius: 2px;
  background: var(--accent);
}

#search {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 7px 11px;
  font: inherit;
  font-size: 13px;
  outline: none;
  transition: border-color .12s ease;
}
#search:focus { border-color: var(--accent); }
#search::placeholder { color: var(--ink-faint); }

#file-list { flex: 1; overflow-y: auto; margin: 0 -4px; padding: 0 4px; }

.file-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border-radius: 7px;
  color: var(--ink-soft);
  cursor: pointer;
  position: relative;
}
.file-item .fi-name {
  display: block;
  font-size: 13.5px;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 22px;
}
.file-item .fi-meta { font-size: 11px; color: var(--ink-faint); }
.file-item:hover { background: var(--accent-soft); }
.file-item.active { background: var(--bg-elev); box-shadow: inset 2px 0 0 var(--accent); }
.file-item .fi-del {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  opacity: 0;
  font-size: 14px;
  color: var(--ink-faint);
  width: 20px; height: 20px;
  padding: 0;
}
.file-item:hover .fi-del { opacity: 1; }
.file-item .fi-del:hover { color: #b3402f; }

.side-foot {
  display: flex;
  align-items: center;
  padding: 4px 6px 0;
  border-top: 1px solid var(--line-soft);
}

/* ---------------------------------------------------------------- ai dock
   Discreet agent affordance: collapsed by default, sits above side-foot. */
.ai-dock { padding: 6px 8px 4px; margin-top: auto; }
.ai-dock[hidden] { display: none; }

.ai-dock-toggle {
  display: flex;
  align-items: center;
  gap: 7px;
  width: 100%;
  padding: 5px 8px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink-faint);
  font: 500 12px/1.2 var(--sans);
  cursor: pointer;
  text-align: left;
}
.ai-dock-toggle:hover { background: var(--accent-soft); color: var(--accent); }
.ai-dock-toggle svg { flex: 0 0 auto; }
.ai-dock-toggle .ai-model {
  margin-left: auto;
  font-size: 10px;
  color: var(--ink-faint);
  opacity: .75;
  max-width: 40%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ai-prompt-wrap { margin-top: 6px; }
.ai-prompt-wrap[hidden] { display: none; }
#ai-prompt {
  width: 100%;
  box-sizing: border-box;
  resize: vertical;
  min-height: 54px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg-elev);
  color: var(--ink);
  font: 13px/1.45 var(--sans);
}
#ai-prompt:focus { outline: none; border-color: var(--accent); }
.ai-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 6px; }
.ai-row #ai-hint { font-size: 11px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ai-go {
  padding: 4px 10px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font: 600 11px/1.4 var(--sans);
  cursor: pointer;
}
.ai-go:disabled { opacity: .55; cursor: default; }
.ai-go:not(:disabled):hover { filter: brightness(1.07); }
.ai-dock.busy .ai-go { pointer-events: none; }

/* ---------------------------------------------------------------- main */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}

.doc-title-group { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
#doc-name {
  border: none;
  background: none;
  color: var(--ink);
  font-family: var(--serif);
  font-size: 17px;
  width: 24ch;
  outline: none;
  padding: 2px 0;
  border-bottom: 1px solid transparent;
  transition: border-color .15s;
}
#doc-name:focus { border-bottom-color: var(--accent); }

.tools { display: flex; gap: 2px; }
.tools button {
  min-width: 28px;
  height: 28px;
  padding: 0 6px;
  font-size: 12.5px;
  color: var(--ink-faint);
}
.tools button:hover { color: var(--ink); }

.tool-sep { width: 1px; height: 18px; background: var(--line); margin: 0 4px; }

.view-toggle {
  display: flex;
  background: var(--bg-side);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 2px;
}
.view-toggle button {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 5px;
  color: var(--ink-faint);
}
.view-toggle button[aria-selected="true"] {
  background: var(--bg-elev);
  color: var(--ink);
  box-shadow: 0 1px 2px #0001;
}

/* ------------------------------------------------------------ workspace */
#workspace { flex: 1; display: flex; min-height: 0; position: relative; }

#editor-pane {
  width: 50%;
  min-width: 120px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#editor {
  flex: 1;
  width: 100%;
  border: none;
  resize: none;
  outline: none;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.75;
  padding: 34px 44px 40vh;
  caret-color: var(--accent);
}
#editor::placeholder { color: var(--ink-faint); }

#divider {
  width: 1px;
  background: var(--line);
  cursor: col-resize;
  position: relative;
}
#divider::after {
  content: "";
  position: absolute;
  inset: 0 -4px;
}

#preview-pane {
  flex: 1;
  min-width: 120px;
  overflow-y: auto;
}

.view-read #editor-pane, .view-read #divider { display: none; }
.view-read #preview-pane { padding-left: 0; }
.view-write #preview-pane, .view-write #divider { display: none; }
.view-write #editor-pane { width: 100%; }

/* centered column for read/focus */
#preview { max-width: 720px; margin: 0 auto; padding: 34px 56px 40vh; }

/* typewriter + focus modes */
body.tw #editor { padding-top: 40vh; padding-bottom: 40vh; }
body.focus .tools, body.focus .view-toggle, body.focus #btn-export,
body.focus #btn-help, body.focus #sidebar, body.focus #statusbar .dot,
body.focus #st-read, body.focus #st-chars, body.focus #st-cursor {
  opacity: .25;
  transition: opacity .2s;
}
body.focus:hover .tools,
body.focus:hover .view-toggle,
body.focus:hover #btn-export,
body.focus:hover #btn-help,
body.focus:hover #sidebar { opacity: 1; }

/* ------------------------------------------------------------- status */
#statusbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 16px;
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.dot { width: 3px; height: 3px; border-radius: 50%; background: var(--ink-faint); }

/* --------------------------------------------------- markdown preview */
.md { font-family: var(--serif); font-size: 17px; line-height: 1.72; color: var(--ink); }
.md > * + * { margin-top: 1.05em; }

.md h1, .md h2, .md h3, .md h4, .md h5, .md h6 {
  font-weight: 650;
  line-height: 1.28;
  letter-spacing: -.008em;
  color: var(--ink);
  margin-top: 1.7em;
  scroll-margin-top: 24px;
}
.md h1 { font-size: 1.95em; margin-top: .3em; }
.md h2 { font-size: 1.5em; }
.md h3 { font-size: 1.22em; }
.md h4 { font-size: 1.05em; }
.md h5, .md h6 { font-size: .95em; color: var(--ink-soft); }

.md p { margin: 0; }
.md a { color: var(--accent); text-decoration: none; border-bottom: 1px solid var(--accent-soft); }
.md a:hover { border-bottom-color: var(--accent); }
.md strong { font-weight: 640; }
.md em { font-style: italic; }
.md del { color: var(--ink-faint); }
.md mark { background: var(--mark); padding: 0 2px; border-radius: 2px; }

.md code {
  font-family: var(--mono);
  font-size: .82em;
  background: var(--code-bg);
  border: 1px solid var(--line-soft);
  border-radius: 4px;
  padding: .12em .38em;
}

.md pre {
  background: var(--code-bg);
  border: 1px solid var(--line-soft);
  border-radius: 10px;
  padding: 15px 18px;
  overflow-x: auto;
  font-size: .82em;
  line-height: 1.6;
  position: relative;
}
.md pre code { border: none; padding: 0; background: none; font-size: 1em; }
.md pre[data-lang]::before {
  content: attr(data-lang);
  position: absolute;
  top: 6px;
  right: 12px;
  font-family: var(--sans);
  font-size: 10px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.md blockquote {
  margin: 0;
  padding: 2px 0 2px 18px;
  border-left: 3px solid var(--accent);
  color: var(--ink-soft);
}
.md blockquote p { margin: 0; }

.md ul, .md ol { margin: 0; padding-left: 1.45em; }
.md li { margin: .2em 0; }
.md li::marker { color: var(--ink-faint); }
.md li.task { list-style: none; }
.md li.task input[type="checkbox"] {
  margin-right: .55em;
  accent-color: var(--accent);
  transform: translateY(1px);
}
.md li.task.done { color: var(--ink-faint); }

.md hr { border: none; border-top: 1px solid var(--line); margin: 2.2em 0; }

.md img { max-width: 100%; border-radius: 6px; }

.md table {
  border-collapse: collapse;
  font-family: var(--sans);
  font-size: .86em;
  width: 100%;
}
.md th, .md td {
  text-align: left;
  padding: 7px 12px;
  border-bottom: 1px solid var(--line);
}
.md th {
  font-weight: 600;
  color: var(--ink-soft);
  border-bottom: 1px solid var(--ink-faint);
}
.md th[align="center"], .md td[align="center"] { text-align: center; }
.md th[align="right"], .md td[align="right"] { text-align: right; }
.md td > input[type="checkbox"] { display: none; }

.md .footnotes { margin-top: 2.5em; font-size: .85em; color: var(--ink-soft); }
.md .footnotes hr { margin-bottom: 1em; }
.md sup a { font-family: var(--sans); font-size: .72em; padding: 0 .15em; }

/* -------------------------------------------------------------- overlays */
#overlay {
  position: fixed;
  inset: 0;
  background: #0007;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  backdrop-filter: blur(2px);
}
#help-card {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 22px 26px;
  max-width: 720px;
  box-shadow: 0 24px 60px #0004;
}
/* ---------------- outline popover ---------------- */
#outline-wrap {
  position: fixed; inset: 0; z-index: 60; display: flex;
  align-items: flex-start; justify-content: center; padding-top: 12vh;
  background: rgba(20,22,26,.28); backdrop-filter: blur(2px);
}
#outline-card {
  width: min(420px, 90vw); max-height: 60vh; overflow: auto;
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 6px 14px;
  box-shadow: 0 18px 50px rgba(0,0,0,.25);
}
.outline-list { display: flex; flex-direction: column; padding: 4px 10px; }
.ol-item {
  display: block; padding: 5px 8px; border-radius: 6px;
  color: var(--ink); text-decoration: none; font-size: 13px; line-height: 1.4;
  cursor: pointer; border: 0; background: none; text-align: left; width: 100%;
}
.ol-item:hover { background: var(--line-soft); }
.ol-item.lv-1 { font-weight: 600; }
.ol-item.lv-2 { padding-left: 22px; }
.ol-item.lv-3 { padding-left: 38px; }
.ol-item.lv-4 { padding-left: 54px; font-size: 12px; }
.ol-item.lv-5 { padding-left: 68px; font-size: 12px; color: var(--ink-soft); }
.ol-item.lv-6 { padding-left: 80px; font-size: 12px; color: var(--ink-soft); }

/* ---------------- help modal ---------------- */
.help-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  margin-bottom: 16px;
}
.help-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 26px; }
.help-col h4 {
  margin: 0 0 10px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--ink-faint);
}
.kbd-row {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 7px 0;
  font-size: 12.5px;
  color: var(--ink-soft);
}
.kbd-row span { flex: 1; }
kbd {
  font-family: var(--sans);
  font-size: 11px;
  background: var(--bg-side);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 6px;
  color: var(--ink-soft);
}

#palette-wrap {
  position: fixed;
  inset: 0;
  background: #0003;
  display: flex;
  justify-content: center;
  padding-top: 12vh;
  z-index: 60;
}
#palette {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 12px;
  width: min(560px, 92vw);
  height: fit-content;
  box-shadow: 0 24px 60px #0005;
  overflow: hidden;
}
#palette-input {
  width: 100%;
  border: none;
  outline: none;
  background: none;
  color: var(--ink);
  font: inherit;
  padding: 15px 18px;
  border-bottom: 1px solid var(--line);
}
#palette-list { list-style: none; margin: 0; padding: 6px; max-height: 44vh; overflow-y: auto; }
#palette-list li {
  padding: 9px 14px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 13.5px;
  color: var(--ink-soft);
  display: flex;
  gap: 10px;
  align-items: baseline;
}
#palette-list li small { color: var(--ink-faint); margin-left: auto; }
#palette-list li.sel, #palette-list li:hover { background: var(--accent-soft); color: var(--ink); }

#toast {
  position: fixed;
  bottom: 26px;
  left: 50%;
  transform: translate(-50%, 12px);
  background: var(--ink);
  color: var(--bg);
  font-size: 12.5px;
  padding: 9px 16px;
  border-radius: 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
  z-index: 70;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

::selection { background: var(--sel); }

/* thin scrollbars */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 5px; border: 2px solid transparent; background-clip: content-box; }
::-webkit-scrollbar-thumb:hover { background: var(--ink-faint); border: 2px solid transparent; background-clip: content-box; }

.only-narrow { display: none; }
@media (max-width: 880px) {
  :root { --side-w: 0px; }
  #sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform .2s ease;
    box-shadow: 0 0 40px #0003;
    width: 250px;
  }
  body.side-open #sidebar { transform: translateX(0); }
  .only-narrow { display: inline-flex; }
  .view-split #editor-pane { display: none; }
  #doc-name { width: 130px; }
  .help-grid { grid-template-columns: 1fr; }
}