:root {
  color-scheme: light;
  --page: #f4f1eb;
  --surface: #fbfaf7;
  --surface-strong: #ffffff;
  --ink: #171a1d;
  --muted: #686b6e;
  --line: #d8d4cc;
  --line-strong: #b8b2a8;
  --accent: #1e6652;
  --accent-strong: #164c3e;
  --accent-soft: #dcebe4;
  --signal: #b84f36;
  --shadow: 0 24px 70px rgba(36, 34, 29, .16);
  --editor-ratio: 50%;
  --mono: "SFMono-Regular", "Cascadia Code", "Roboto Mono", Consolas, monospace;
  --sans: Geist, "Avenir Next", Avenir, Helvetica, Arial, sans-serif;
  --serif: Iowan Old Style, Charter, "Bitstream Charter", Georgia, serif;
}

[data-theme="dark"] {
  color-scheme: dark;
  --page: #181a1c;
  --surface: #1f2224;
  --surface-strong: #26292c;
  --ink: #f1eee7;
  --muted: #aaa9a4;
  --line: #383c3f;
  --line-strong: #565b5f;
  --accent: #77c7aa;
  --accent-strong: #a3dfc7;
  --accent-soft: #203e35;
  --signal: #e78b70;
  --shadow: 0 24px 70px rgba(0, 0, 0, .42);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  overflow: hidden;
  background: var(--page);
  color: var(--ink);
  font-family: var(--sans);
  letter-spacing: 0;
}

button, input, textarea { font: inherit; }
button { color: inherit; }

button, a, input, textarea, [tabindex] { outline: none; }
button:focus-visible, a:focus-visible, input:focus-visible, textarea:focus-visible, [tabindex]:focus-visible {
  box-shadow: 0 0 0 3px var(--page), 0 0 0 5px var(--accent);
}

button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .5; }

.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;
}

.skip-link {
  position: fixed;
  z-index: 100;
  top: 10px;
  left: 10px;
  transform: translateY(-160%);
  padding: 10px 14px;
  border-radius: 4px;
  background: var(--ink);
  color: var(--surface);
  text-decoration: none;
}
.skip-link:focus { transform: translateY(0); }

.app-shell {
  width: 100%;
  height: 100dvh;
  display: grid;
  grid-template-rows: 64px 1fr 36px;
  overflow: hidden;
}

.topbar {
  z-index: 10;
  display: grid;
  grid-template-columns: minmax(190px, .7fr) minmax(220px, 1.4fr) auto auto;
  align-items: center;
  gap: 18px;
  padding: 0 22px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 93%, transparent);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  color: var(--ink);
  font-weight: 720;
  text-decoration: none;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
  border: 1px solid var(--ink);
  border-radius: 4px;
  background: var(--ink);
  color: var(--surface);
  font-family: var(--serif);
  font-size: 19px;
}

.title-field input {
  width: 100%;
  min-width: 0;
  padding: 9px 10px;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
  color: var(--ink);
  font-weight: 620;
  text-align: center;
}
.title-field input:hover { border-color: var(--line); }
.title-field input:focus { border-color: var(--accent); background: var(--surface-strong); }

.save-state {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}
.save-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.save-state.is-saving .save-dot { background: var(--signal); }

.top-actions, .toolbar, .drawer-actions, .dialog-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-button, .toolbar button {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
}
.icon-button:hover, .toolbar button:hover { border-color: var(--line); background: var(--surface-strong); }

.command-button, .primary-button, .secondary-button {
  min-height: 38px;
  padding: 0 14px;
  border-radius: 4px;
  font-weight: 680;
}
.command-button, .primary-button { border: 1px solid var(--ink); background: var(--ink); color: var(--surface); }
.command-button:hover, .primary-button:hover { background: var(--accent-strong); border-color: var(--accent-strong); color: var(--surface-strong); }
.secondary-button { border: 1px solid var(--line-strong); background: transparent; color: var(--ink); }
.secondary-button:hover { border-color: var(--ink); background: var(--surface-strong); }

.menu-wrap { position: relative; }
.menu {
  position: absolute;
  z-index: 30;
  top: calc(100% + 8px);
  right: 0;
  width: 190px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-strong);
  box-shadow: var(--shadow);
}
.menu button {
  width: 100%;
  padding: 10px;
  border: 0;
  border-radius: 3px;
  background: transparent;
  text-align: left;
}
.menu button:hover { background: var(--accent-soft); }

.mobile-tabs { display: none; }

.workspace {
  display: grid;
  grid-template-columns: minmax(0, var(--editor-ratio)) 12px minmax(0, calc(100% - var(--editor-ratio) - 12px));
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.pane { min-width: 0; min-height: 0; background: var(--surface); }
.editor-pane, .preview-pane { display: grid; grid-template-rows: 48px 1fr; }
.editor-pane { border-right: 1px solid var(--line); }

.toolbar, .preview-header {
  min-width: 0;
  padding: 0 14px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.toolbar { overflow-x: auto; }
.toolbar button { flex: 0 0 auto; font-family: var(--mono); }
.toolbar button.is-active { border-color: var(--accent); background: var(--accent-soft); color: var(--accent-strong); }
.toolbar .text-tool { width: auto; min-width: 50px; padding: 0 9px; font-family: var(--sans); font-size: 12px; }
.toolbar-spacer { flex: 1 0 18px; }
.exit-focus { color: var(--signal); }

.editor-surface {
  display: grid;
  grid-template-columns: 0 1fr;
  min-height: 0;
  overflow: hidden;
  background: var(--surface-strong);
  transition: grid-template-columns 180ms ease;
}
.editor-surface.show-lines { grid-template-columns: 52px 1fr; }
.line-numbers {
  overflow: hidden;
  padding: 32px 12px 32px 0;
  border-right: 1px solid var(--line);
  color: var(--line-strong);
  font: 14px/1.72 var(--mono);
  text-align: right;
  user-select: none;
  white-space: pre;
}

#editor {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  resize: none;
  padding: 30px clamp(22px, 4vw, 56px) 80px;
  border: 0;
  background: transparent;
  color: var(--ink);
  caret-color: var(--accent);
  font: 15px/1.72 var(--mono);
  tab-size: 2;
}
#editor.no-wrap { white-space: pre; overflow-x: auto; }

.resizer {
  position: relative;
  z-index: 5;
  cursor: col-resize;
  background: var(--page);
  touch-action: none;
}
.resizer span {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 3px;
  height: 42px;
  transform: translate(-50%, -50%);
  border-left: 1px solid var(--line-strong);
  border-right: 1px solid var(--line-strong);
}
.resizer:hover, .resizer:focus-visible { background: var(--accent-soft); }

.preview-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--muted);
  font-size: 12px;
  font-weight: 680;
  text-transform: uppercase;
}
.preview-header button {
  padding: 6px 9px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  text-transform: none;
}
.preview-header button:hover { border-color: var(--accent); color: var(--accent-strong); }

.preview {
  width: 100%;
  height: 100%;
  overflow: auto;
  padding: 40px clamp(28px, 5vw, 78px) 110px;
  background: var(--surface);
  font: 18px/1.72 var(--serif);
}
.preview > :first-child { margin-top: 0; }
.preview h1, .preview h2, .preview h3 { max-width: 25ch; margin: 1.65em 0 .55em; color: var(--ink); font-family: var(--sans); line-height: 1.12; }
.preview h1 { font-size: clamp(36px, 5vw, 66px); }
.preview h2 { font-size: clamp(27px, 3.3vw, 40px); }
.preview h3 { font-size: 24px; }
.preview p, .preview ul, .preview ol, .preview blockquote, .preview table, .preview pre { max-width: 72ch; }
.preview a { color: var(--accent-strong); text-underline-offset: 3px; }
.preview blockquote { margin-left: 0; padding: 4px 0 4px 22px; border-left: 3px solid var(--accent); color: var(--muted); font-size: 1.08em; }
.preview code { padding: .15em .35em; border-radius: 3px; background: var(--accent-soft); font: .84em/1.5 var(--mono); }
.preview pre { overflow-x: auto; padding: 20px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface-strong); }
.preview pre code { padding: 0; background: transparent; }
.preview img { max-width: 100%; height: auto; border-radius: 4px; }
.preview hr { margin: 2.8em 0; border: 0; border-top: 1px solid var(--line); }
.preview table { width: 100%; border-collapse: collapse; font-family: var(--sans); font-size: 14px; }
.preview th, .preview td { padding: 10px 12px; border: 1px solid var(--line); text-align: left; }
.preview th { background: var(--accent-soft); }

.statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 0 18px;
  border-top: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font-size: 11px;
}
.document-stats { display: flex; gap: 16px; }
.privacy-note { white-space: nowrap; }
.privacy-note span { color: var(--accent); }

.scrim {
  position: fixed;
  z-index: 39;
  inset: 0;
  background: rgba(11, 13, 14, .42);
  backdrop-filter: blur(2px);
}

.drawer {
  position: fixed;
  z-index: 40;
  top: 0;
  right: 0;
  width: min(430px, 94vw);
  height: 100dvh;
  padding: 22px;
  border-left: 1px solid var(--line);
  background: var(--surface);
  box-shadow: var(--shadow);
  transform: translateX(105%);
  visibility: hidden;
  transition: transform 240ms ease, visibility 240ms;
}
.drawer.is-open { transform: translateX(0); visibility: visible; }
.drawer-head { display: flex; align-items: flex-start; justify-content: space-between; }
.drawer-head p, .dialog-head p { margin: 0 0 4px; color: var(--accent-strong); font-size: 11px; font-weight: 760; text-transform: uppercase; }
.drawer-head h2, .dialog-head h2 { margin: 0; font: 38px/1 var(--serif); }
.drawer-actions { margin: 24px 0 18px; }
.drawer-actions > * { flex: 1; }
.draft-list { display: grid; gap: 8px; max-height: calc(100dvh - 180px); overflow: auto; }
.draft-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 15px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-strong);
}
.draft-card.is-current { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
.draft-open { min-width: 0; padding: 0; border: 0; background: transparent; text-align: left; }
.draft-open strong, .draft-open span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.draft-open strong { margin-bottom: 6px; }
.draft-open span { color: var(--muted); font-size: 12px; }
.draft-tools { display: flex; gap: 2px; }
.draft-tools button { width: 30px; height: 30px; padding: 0; border: 0; border-radius: 3px; background: transparent; }
.draft-tools button:hover { background: var(--accent-soft); }
.empty-library { padding: 32px 16px; border: 1px dashed var(--line-strong); color: var(--muted); text-align: center; }

dialog {
  width: min(600px, calc(100vw - 32px));
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(11, 13, 14, .55); backdrop-filter: blur(3px); }
.dialog-card { padding: 24px; }
.dialog-card.compact { width: min(440px, 100%); }
.dialog-head { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 22px; }
.drop-zone {
  display: grid;
  place-items: center;
  min-height: 128px;
  padding: 20px;
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  background: var(--surface-strong);
  color: var(--muted);
  text-align: center;
  cursor: pointer;
}
.drop-zone:hover, .drop-zone.is-dragging { border-color: var(--accent); background: var(--accent-soft); }
.drop-zone strong { color: var(--ink); }
.drop-icon { font-size: 25px; color: var(--accent-strong); }
.or-divider { display: flex; align-items: center; gap: 10px; margin: 18px 0; color: var(--muted); font-size: 11px; text-transform: uppercase; }
.or-divider::before, .or-divider::after { content: ""; height: 1px; flex: 1; background: var(--line); }
#importText, .stacked-label input {
  width: 100%;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--surface-strong);
  color: var(--ink);
}
#importText { resize: vertical; padding: 13px; font: 14px/1.6 var(--mono); }
.stacked-label { display: grid; gap: 8px; color: var(--muted); font-size: 12px; font-weight: 680; }
.stacked-label input { padding: 11px 12px; font-size: 15px; }
.field-note { min-height: 18px; margin: 8px 0 0; color: var(--signal); font-size: 12px; }
.dialog-actions { justify-content: flex-end; margin-top: 20px; }

.toast {
  position: fixed;
  z-index: 60;
  left: 50%;
  bottom: 52px;
  max-width: min(420px, calc(100vw - 32px));
  transform: translateX(-50%);
  padding: 10px 14px;
  border-radius: 4px;
  background: var(--ink);
  color: var(--surface);
  box-shadow: var(--shadow);
  font-size: 13px;
}

.focus-mode .topbar, .focus-mode .statusbar, .focus-mode .mobile-tabs, .focus-mode .preview-pane, .focus-mode .resizer { display: none; }
.focus-mode { grid-template-rows: 1fr; }
.focus-mode .workspace { display: block; background: var(--surface-strong); }
.focus-mode .editor-pane { width: 100%; height: 100%; border: 0; grid-template-rows: 48px 1fr; }
.focus-mode .toolbar { justify-content: flex-end; border: 0; background: var(--surface-strong); }
.focus-mode .toolbar > :not(.toolbar-spacer):not(.exit-focus) { display: none; }
.focus-mode #editor { max-width: 74ch; margin: 0 auto; padding-top: 12vh; font-size: 17px; line-height: 1.9; }

@media (max-width: 900px) {
  .topbar { grid-template-columns: auto minmax(120px, 1fr) auto; gap: 10px; padding: 0 12px; }
  .brand > span:last-child, .save-state { display: none; }
  .top-actions { gap: 3px; }
  .command-button { padding: 0 10px; }
}

@media (max-width: 700px) {
  body { overflow: hidden; }
  .app-shell { grid-template-rows: 58px 42px 1fr 34px; }
  .topbar { grid-template-columns: 34px minmax(0, 1fr) auto; }
  .title-field input { padding-inline: 4px; text-align: left; }
  .mobile-tabs { display: grid; grid-template-columns: 1fr 1fr; padding: 5px; border-bottom: 1px solid var(--line); background: var(--surface); }
  .mobile-tab { border: 0; border-radius: 3px; background: transparent; color: var(--muted); font-size: 12px; font-weight: 700; }
  .mobile-tab.is-active { background: var(--accent-soft); color: var(--accent-strong); }
  .workspace { display: block; }
  .pane { width: 100%; height: 100%; }
  .pane[hidden] { display: none; }
  .resizer { display: none; }
  .preview { padding: 28px 22px 90px; }
  #editor { padding: 24px 20px 80px; }
  .statusbar { padding-inline: 12px; }
  .document-stats span:nth-child(2) { display: none; }
  .privacy-note { display: none; }
  .focus-mode { grid-template-rows: 1fr; }
}

@media (max-width: 420px) {
  #themeButton { display: none; }
  .preview-header { padding-inline: 10px; }
  .toolbar { padding-inline: 8px; }
  .document-stats { width: 100%; justify-content: space-between; }
}

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