/* ============================================================
   Gruvbox Word — styles
   Palette: gruvbox by Pavel Pertsev. Type: JetBrains Mono Nerd.
   ============================================================ */

/* ---- Fonts (subset woff2; full Nerd glyph coverage in BMP PUA) ---- */
@font-face {
  font-family: "JetBrains Mono Nerd";
  src: url("../fonts/JetBrainsMonoNerd-Regular.woff2") format("woff2");
  font-weight: 400; font-style: normal; font-display: swap;
}
@font-face {
  font-family: "JetBrains Mono Nerd";
  src: url("../fonts/JetBrainsMonoNerd-Italic.woff2") format("woff2");
  font-weight: 400; font-style: italic; font-display: swap;
}
@font-face {
  font-family: "JetBrains Mono Nerd";
  src: url("../fonts/JetBrainsMonoNerd-Bold.woff2") format("woff2");
  font-weight: 700; font-style: normal; font-display: swap;
}
@font-face {
  font-family: "JetBrains Mono Nerd";
  src: url("../fonts/JetBrainsMonoNerd-BoldItalic.woff2") format("woff2");
  font-weight: 700; font-style: italic; font-display: swap;
}

/* ---- Theme tokens ---- */
:root {
  --font: "JetBrains Mono Nerd", ui-monospace, "Cascadia Code", "Fira Code", Menlo, Consolas, monospace;
  --sidebar-w: 16rem;
  --measure: 46rem;
  --radius: 10px;
  --radius-sm: 7px;
  --shadow: 0 6px 24px rgba(40, 40, 40, .12), 0 2px 6px rgba(40, 40, 40, .08);
  --tr: 160ms cubic-bezier(.4, 0, .2, 1);
}

:root[data-theme="light"] {
  --bg: #fbf1c7;
  --bg-hard: #f9f5d7;
  --bg-soft: #f2e5bc;
  --bg-1: #ebdbb2;
  --bg-2: #d5c4a1;
  --bg-3: #bdae93;
  --fg: #3c3836;
  --fg-strong: #282828;
  --fg-soft: #504945;
  --fg-mute: #7c6f64;
  --fg-faint: #a89984;
  --accent: #af3a03;
  --accent-bright: #d65d0e;
  --green: #79740e;
  --aqua: #427b58;
  --blue: #076678;
  --purple: #8f3f71;
  --red: #9d0006;
  --yellow: #b57614;
  --border: #e6d9ad;
  --border-strong: #d5c4a1;
  --sel: rgba(214, 93, 14, .20);
  --code-bg: #f2e5bc;
}

:root[data-theme="dark"] {
  --bg: #282828;
  --bg-hard: #1d2021;
  --bg-soft: #32302f;
  --bg-1: #3c3836;
  --bg-2: #504945;
  --bg-3: #665c54;
  --fg: #ebdbb2;
  --fg-strong: #fbf1c7;
  --fg-soft: #d5c4a1;
  --fg-mute: #a89984;
  --fg-faint: #7c6f64;
  --accent: #fe8019;
  --accent-bright: #fe8019;
  --green: #b8bb26;
  --aqua: #8ec07c;
  --blue: #83a598;
  --purple: #d3869b;
  --red: #fb4934;
  --yellow: #fabd2f;
  --border: #3c3836;
  --border-strong: #504945;
  --sel: rgba(254, 128, 25, .24);
  --code-bg: #32302f;
  --shadow: 0 8px 30px rgba(0, 0, 0, .45), 0 2px 8px rgba(0, 0, 0, .35);
}

/* ---- Reset ---- */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; } /* must win over component display rules */
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--fg);
  font-size: 16px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow: hidden;
}
::selection { background: var(--sel); }
button { font-family: inherit; }

/* Scrollbars */
* { scrollbar-width: thin; scrollbar-color: var(--bg-3) transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb { background: var(--bg-3); border-radius: 8px; border: 2px solid var(--bg); }
*::-webkit-scrollbar-track { background: transparent; }

/* ============================================================
   Layout
   ============================================================ */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  height: 100dvh;
  transition: grid-template-columns var(--tr);
}
.app.no-sidebar { grid-template-columns: 0 minmax(0, 1fr); }

.main {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  background: var(--bg);
}

/* ============================================================
   Sidebar
   ============================================================ */
.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-soft);
  border-right: 1px solid var(--border);
  overflow: hidden;
}
.app.no-sidebar .sidebar { border-right-color: transparent; }

.sidebar__brand {
  display: flex; align-items: center; gap: .55rem;
  padding: .95rem 1rem .8rem;
  font-weight: 700; color: var(--fg-strong);
  letter-spacing: -.01em;
}
.brand__mark { width: 26px; height: 26px; flex: none; fill: none; stroke: var(--accent); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.brand__name { font-size: .98rem; }

.sidebar__section { padding: .35rem .55rem; min-height: 0; display: flex; flex-direction: column; }
.sidebar__section--grow { flex: 1 1 auto; overflow: hidden; }
.sidebar__head {
  display: flex; align-items: center; justify-content: space-between;
  padding: .35rem .45rem; margin-top: .25rem;
  font-size: .72rem; text-transform: uppercase; letter-spacing: .08em;
  color: var(--fg-mute);
}

.doc-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; max-height: 34vh; }
.doc-item {
  display: flex; align-items: center; gap: .4rem;
  padding: .42rem .55rem; border-radius: var(--radius-sm);
  cursor: pointer; color: var(--fg-soft); font-size: .86rem;
  transition: background var(--tr), color var(--tr);
}
.doc-item:hover { background: var(--bg-1); }
.doc-item.is-active { background: var(--bg-1); color: var(--fg-strong); box-shadow: inset 2px 0 0 var(--accent); }
.doc-item__title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-item__actions { display: flex; gap: .1rem; opacity: 0; transition: opacity var(--tr); }
.doc-item:hover .doc-item__actions, .doc-item.is-active .doc-item__actions { opacity: 1; }

.outline { overflow-y: auto; padding: .15rem .25rem .5rem; }
.outline a {
  display: block; padding: .26rem .5rem; border-radius: var(--radius-sm);
  color: var(--fg-mute); text-decoration: none; font-size: .82rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  transition: background var(--tr), color var(--tr);
}
.outline a:hover { background: var(--bg-1); color: var(--fg); }
.outline a.lvl-2 { padding-left: 1.3rem; font-size: .79rem; }
.outline a.lvl-3 { padding-left: 2.1rem; font-size: .77rem; color: var(--fg-faint); }
.outline:empty::after { content: "Headings appear here."; display: block; padding: .4rem .5rem; color: var(--fg-faint); font-size: .78rem; }

.sidebar__foot {
  margin-top: auto; padding: .7rem 1rem;
  display: flex; justify-content: space-between; gap: .5rem;
  border-top: 1px solid var(--border); font-size: .76rem;
}
.textlink { background: none; border: 0; cursor: pointer; color: var(--fg-mute); text-decoration: none; font: inherit; font-size: .76rem; padding: 0; }
.textlink:hover { color: var(--accent); }

/* ============================================================
   Toolbar
   ============================================================ */
.toolbar {
  display: flex; align-items: center; gap: .15rem; flex-wrap: wrap;
  padding: .4rem .6rem; min-height: 3rem;
  background: var(--bg-soft);
  border-bottom: 1px solid var(--border);
}
.toolbar__group { display: flex; align-items: center; gap: .1rem; }
.toolbar__sep { width: 1px; height: 1.4rem; background: var(--border-strong); margin: 0 .35rem; }
.toolbar__spacer { flex: 1 1 auto; }

.iconbtn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 2rem; height: 2rem; padding: 0;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: transparent; color: var(--fg-soft); cursor: pointer;
  transition: background var(--tr), color var(--tr), border-color var(--tr);
}
.iconbtn svg { width: 18px; height: 18px; pointer-events: none; }
.iconbtn:hover { background: var(--bg-1); color: var(--fg-strong); }
.iconbtn:active { transform: translateY(1px); }
.iconbtn.is-active { background: var(--bg-2); color: var(--accent); border-color: var(--border-strong); }
.iconbtn--text { font-size: .8rem; font-weight: 700; width: auto; padding: 0 .5rem; color: var(--fg-soft); }
.iconbtn--sm { width: 1.55rem; height: 1.55rem; }
.iconbtn--sm svg { width: 14px; height: 14px; }

/* theme button shows the icon for the theme you'll switch TO */
.iconbtn .i-moon, .iconbtn .i-sun { display: none; }
:root[data-theme="light"] [data-cmd="theme"] .i-moon { display: block; }
:root[data-theme="dark"] [data-cmd="theme"] .i-sun { display: block; }

/* ============================================================
   Editor surface
   ============================================================ */
.editor-wrap { overflow-y: auto; position: relative; scroll-padding-top: 2rem; }
.editor {
  max-width: var(--measure);
  margin: 0 auto;
  padding: clamp(1.5rem, 4vw, 3.5rem) clamp(1.1rem, 4vw, 2rem) 40vh;
  outline: none;
  font-size: 1.0625rem;
  line-height: 1.75;
  color: var(--fg);
  caret-color: var(--accent-bright);
  position: relative;
}
.editor.is-empty::before {
  content: attr(data-placeholder);
  position: absolute;
  color: var(--fg-faint);
  pointer-events: none;
}

/* Editor typography */
.editor > :first-child { margin-top: 0; }
.editor h1, .editor h2, .editor h3 { color: var(--fg-strong); font-weight: 700; line-height: 1.25; letter-spacing: -.015em; }
.editor h1 { font-size: 1.9rem; margin: 1.8rem 0 .9rem; }
.editor h2 { font-size: 1.45rem; margin: 1.6rem 0 .7rem; }
.editor h3 { font-size: 1.18rem; margin: 1.3rem 0 .5rem; }
.editor p { margin: 0 0 1.05rem; }
.editor a { color: var(--blue); text-decoration: underline; text-underline-offset: 2px; text-decoration-color: color-mix(in srgb, var(--blue) 45%, transparent); }
.editor a:hover { text-decoration-color: var(--blue); }
.editor strong, .editor b { color: var(--fg-strong); font-weight: 700; }
.editor em, .editor i { font-style: italic; }
.editor del, .editor s { color: var(--fg-mute); }
.editor ul, .editor ol { margin: 0 0 1.05rem; padding-left: 1.6rem; }
.editor li { margin: .25rem 0; }
.editor li::marker { color: var(--accent); }
.editor blockquote {
  margin: 0 0 1.05rem; padding: .3rem 0 .3rem 1.1rem;
  border-left: 3px solid var(--accent); color: var(--fg-soft); font-style: italic;
}
.editor code {
  font-size: .92em; background: var(--code-bg); color: var(--accent);
  padding: .1em .38em; border-radius: 5px; border: 1px solid var(--border);
}
.editor pre {
  margin: 0 0 1.15rem; padding: .9rem 1.05rem; overflow-x: auto;
  background: var(--code-bg); border: 1px solid var(--border); border-radius: var(--radius-sm);
  line-height: 1.55;
}
.editor pre code { background: none; border: 0; color: var(--fg); padding: 0; font-size: .92rem; }
.editor hr { border: 0; border-top: 2px dashed var(--border-strong); margin: 1.8rem 0; }
.editor img { max-width: 100%; border-radius: var(--radius-sm); }
.editor :focus { outline: none; }
.editor mark.find-hit { background: var(--yellow); color: var(--bg-hard); border-radius: 3px; }
.editor mark.find-hit.is-current { background: var(--accent-bright); color: #fff; }

/* ============================================================
   Status bar
   ============================================================ */
.statusbar {
  display: flex; align-items: center; gap: .55rem;
  padding: .4rem 1rem; min-height: 2.2rem;
  background: var(--bg-soft); border-top: 1px solid var(--border);
  font-size: .78rem; color: var(--fg-mute);
}
.status__dot { color: var(--fg-faint); }
.status__spacer { flex: 1; }
.status__save { color: var(--aqua); display: inline-flex; align-items: center; gap: .3rem; }
.status__save.is-saving { color: var(--fg-mute); }
.status__save::before { content: "✓"; }
.status__save.is-saving::before { content: "…"; }
.status__zenexit { display: none; }

/* ============================================================
   Find & replace bar
   ============================================================ */
.findbar {
  position: fixed; top: .7rem; right: .8rem; z-index: 40;
  display: flex; align-items: center; gap: .3rem;
  padding: .4rem .5rem; background: var(--bg-soft);
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.findbar__in {
  width: 9rem; padding: .35rem .5rem; font: inherit; font-size: .85rem;
  color: var(--fg); background: var(--bg); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); outline: none;
}
.findbar__in:focus { border-color: var(--accent); }
.findbar__count { font-size: .76rem; color: var(--fg-mute); min-width: 2.6rem; text-align: center; }

.btn {
  font: inherit; cursor: pointer; color: var(--fg-soft);
  background: var(--bg-1); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); padding: .35rem .6rem;
  transition: background var(--tr), color var(--tr);
}
.btn:hover { background: var(--bg-2); color: var(--fg-strong); }
.btn--sm { padding: .3rem .5rem; font-size: .8rem; }
.btn--primary { background: var(--accent); color: var(--bg-hard); border-color: var(--accent); font-weight: 700; }
.btn--primary:hover { background: var(--accent-bright); color: var(--bg-hard); }

/* ============================================================
   Export menu
   ============================================================ */
.menu {
  position: fixed; z-index: 45; min-width: 14rem;
  padding: .35rem; background: var(--bg-soft);
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.menu__item {
  display: flex; align-items: center; gap: .55rem; width: 100%;
  padding: .5rem .6rem; border: 0; border-radius: var(--radius-sm);
  background: none; color: var(--fg-soft); cursor: pointer; font-size: .87rem; text-align: left;
}
.menu__item svg { width: 16px; height: 16px; flex: none; color: var(--fg-mute); fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.menu__item:hover { background: var(--bg-1); color: var(--fg-strong); }
.menu__hint { margin-left: auto; color: var(--fg-faint); font-size: .78rem; }

/* ============================================================
   Modal (help / about)
   ============================================================ */
.modal { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 1.2rem; }
.modal__backdrop { position: absolute; inset: 0; background: rgba(40, 40, 40, .45); backdrop-filter: blur(2px); }
.modal__panel {
  position: relative; max-width: 44rem; width: 100%; max-height: 88vh; overflow-y: auto;
  background: var(--bg); border: 1px solid var(--border-strong);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 2rem clamp(1.2rem, 4vw, 2.4rem);
}
.modal__close { position: absolute; top: .8rem; right: .8rem; }
#about h1 { margin: 0 0 .4rem; font-size: 1.7rem; color: var(--fg-strong); }
#about .lede { color: var(--fg-soft); font-size: 1rem; margin: 0 0 1.4rem; }
#about h2 { font-size: .82rem; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); margin: 0 0 .6rem; }
.about__grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.6rem 2rem; }
#about ul { margin: 0; padding-left: 1.1rem; font-size: .9rem; color: var(--fg-soft); }
#about li { margin: .4rem 0; }
#about code { background: var(--code-bg); border: 1px solid var(--border); border-radius: 4px; padding: .05em .35em; font-size: .85em; color: var(--accent); }
.keys { width: 100%; border-collapse: collapse; font-size: .85rem; }
.keys td { padding: .28rem .2rem; color: var(--fg-soft); border-bottom: 1px solid var(--border); }
.keys td:last-child { text-align: right; white-space: nowrap; }
kbd {
  font: inherit; font-size: .76rem; background: var(--bg-1); color: var(--fg);
  border: 1px solid var(--border-strong); border-bottom-width: 2px;
  border-radius: 5px; padding: .05em .4em;
}
.modal__foot { margin: 1.6rem 0 0; padding-top: 1rem; border-top: 1px solid var(--border); font-size: .82rem; color: var(--fg-mute); }

/* ============================================================
   Toast
   ============================================================ */
.toast {
  position: fixed; bottom: 1.4rem; left: 50%; transform: translateX(-50%) translateY(1rem);
  z-index: 70; padding: .6rem 1rem; font-size: .85rem;
  background: var(--fg-strong); color: var(--bg-hard);
  border-radius: var(--radius); box-shadow: var(--shadow);
  opacity: 0; transition: opacity var(--tr), transform var(--tr); pointer-events: none;
}
.toast.is-show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ============================================================
   Zen mode
   ============================================================ */
/* sidebar leaves grid flow (display:none), so main needs a single full column */
.app.zen { grid-template-columns: 1fr; }
.app.zen .sidebar, .app.zen .toolbar { display: none; }
.app.zen .statusbar { background: transparent; border-top: 0; opacity: .35; transition: opacity var(--tr); }
.app.zen .statusbar:hover { opacity: 1; }
.app.zen .status__zenexit { display: inline-block; margin-right: auto; background: none; border: 0; color: var(--fg-mute); cursor: pointer; font: inherit; font-size: .78rem; }
.app.zen .editor { padding-top: clamp(2rem, 9vh, 6rem); }

/* ============================================================
   Responsive
   ============================================================ */
@media (max-width: 820px) {
  :root { --sidebar-w: 15rem; }
  .app { grid-template-columns: 1fr; } /* sidebar is position:fixed → out of grid flow */
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: var(--sidebar-w); z-index: 50;
    transform: translateX(-100%); transition: transform var(--tr); box-shadow: var(--shadow);
  }
  .app.show-sidebar .sidebar { transform: translateX(0); }
  .app.show-sidebar::after { content: ""; position: fixed; inset: 0; background: rgba(40,40,40,.4); z-index: 45; }
  .toolbar__group--wrap-hide { display: none; }
}

/* ============================================================
   Print  →  clean PDF export
   ============================================================ */
@media print {
  body { overflow: visible; background: #fff; }
  .sidebar, .toolbar, .statusbar, .findbar, .menu, .toast, #help-modal { display: none !important; }
  .app, .main { display: block; height: auto; }
  .editor-wrap { overflow: visible; }
  .editor {
    max-width: none; margin: 0; padding: 0;
    color: #000; font-family: Georgia, "Times New Roman", serif; font-size: 12pt; line-height: 1.5;
  }
  .editor h1, .editor h2, .editor h3 { color: #000; }
  .editor a { color: #000; text-decoration: underline; }
  .editor code, .editor pre { background: #f4f4f4; border-color: #ccc; color: #000; }
  .editor pre code { color: #000; }
  @page { margin: 2cm; }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
