/* ---------- Theme tokens ---------- */
:root,
html[data-theme="bright"] {
  --bg: #fbfaf8;
  --page-bg: #ffffff;
  --text: #2b2b28;
  --text-muted: #8a8681;
  --border: #e7e3dc;
  --accent: #b5651d;
  --accent-contrast: #ffffff;
  --toolbar-bg: rgba(251, 250, 248, 0.92);
  --button-hover: #efece5;
  --shadow: rgba(20, 20, 15, 0.06);
  --selection: #f0dcc0;
}

html[data-theme="medium"] {
  --bg: #ded7c6;
  --page-bg: #e9e3d4;
  --text: #3c342a;
  --text-muted: #8f8570;
  --border: #cabf9f;
  --accent: #8a5a2b;
  --accent-contrast: #fbf7ee;
  --toolbar-bg: rgba(222, 215, 198, 0.92);
  --button-hover: #d3c9ae;
  --shadow: rgba(40, 32, 15, 0.10);
  --selection: #c9b183;
}

html[data-theme="dark"] {
  --bg: #1b1c1d;
  --page-bg: #202224;
  --text: #d8d5cd;
  --text-muted: #7d7a73;
  --border: #333537;
  --accent: #d99a5b;
  --accent-contrast: #1b1c1d;
  --toolbar-bg: rgba(27, 28, 29, 0.92);
  --button-hover: #2c2e30;
  --shadow: rgba(0, 0, 0, 0.35);
  --selection: #5a4326;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  transition: background-color 0.35s ease, color 0.35s ease;
}

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

button {
  font-family: inherit;
  cursor: pointer;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
}

button:hover {
  background: var(--button-hover);
}

/* ---------- Toolbar ---------- */
#toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--toolbar-bg);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  transition: opacity 0.25s ease, transform 0.25s ease, background-color 0.35s ease;
}

.toolbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  max-width: 1100px;
  margin: 0 auto;
  padding: 10px 20px;
  flex-wrap: wrap;
}

.tb-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.header-row {
  padding-bottom: 8px;
}

.button-bar {
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.tb-brand {
  gap: 10px;
}

.app-name {
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: var(--text);
}

.version-pill {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-muted);
  background: var(--button-hover);
  height: auto;
}

.version-pill:hover {
  color: var(--text);
  border-color: var(--accent);
}

.tb-title {
  flex: 1 1 220px;
  min-width: 160px;
  gap: 10px;
}

#docTitle {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  background: transparent;
  border: none;
  outline: none;
  padding: 6px 4px;
  border-radius: 6px;
  min-width: 0;
  width: 100%;
  max-width: 320px;
}

#docTitle:hover, #docTitle:focus {
  background: var(--button-hover);
}

.save-status {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

.tb-format button, .tb-view button {
  min-width: 30px;
  height: 30px;
  font-size: 13px;
  padding: 0 8px;
}

.tb-sep {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 6px;
}

.font-size-ctrl {
  display: flex;
  gap: 2px;
}

.theme-swatches {
  display: flex;
  gap: 6px;
}

.swatch {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  padding: 0;
  border: 1px solid var(--border);
}

.swatch-bright { background: #fbfaf8; }
.swatch-medium { background: #ded7c6; }
.swatch-dark { background: #1b1c1d; }

.swatch.active {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#focusToggle.active {
  color: var(--accent-contrast);
  background: var(--accent);
}

.menu-wrap {
  position: relative;
}

.menu-panel[hidden] {
  display: none;
}

.menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  background: var(--page-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px var(--shadow);
  display: flex;
  flex-direction: column;
  min-width: 200px;
  padding: 6px;
  z-index: 20;
}

.menu-panel button {
  text-align: left;
  padding: 8px 10px;
  font-size: 13px;
  border-radius: 6px;
  white-space: nowrap;
}

.menu-sep {
  height: 1px;
  background: var(--border);
  margin: 4px 2px;
}

/* ---------- Dictation ---------- */
.dictate-btn.recording {
  color: #fff;
  background: #d9534f;
  animation: dictate-pulse 1.4s ease-in-out infinite;
}

@keyframes dictate-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(217, 83, 79, 0.5); }
  50% { box-shadow: 0 0 0 6px rgba(217, 83, 79, 0); }
}

.dictate-panel {
  min-width: 240px;
}

.dictate-status {
  padding: 2px 10px 8px;
  font-size: 11px;
  color: var(--text-muted);
}

.dictate-status.error {
  color: #d9534f;
}

.dictate-hint {
  padding: 8px 10px 4px;
  font-size: 12px;
  line-height: 1.7;
  color: var(--text-muted);
  white-space: normal;
}

.dictate-hint kbd {
  font-family: inherit;
  font-size: 11px;
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--button-hover);
}

/* ---------- Version history modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}

.modal-overlay[hidden] {
  display: none;
}

.modal-panel {
  background: var(--page-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 20px 60px var(--shadow);
  width: 100%;
  max-width: 480px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h2 {
  margin: 0;
  font-size: 15px;
  color: var(--text);
}

.modal-close {
  width: 28px;
  height: 28px;
  font-size: 18px;
  line-height: 1;
  color: var(--text-muted);
}

.version-list {
  overflow-y: auto;
  padding: 4px 20px 16px;
}

.version-entry {
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}

.version-entry:last-child {
  border-bottom: none;
}

.version-entry-header {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 6px;
}

.version-entry-header .v-num {
  font-weight: 700;
  font-size: 14px;
  color: var(--text);
}

.version-entry-header .v-date {
  font-size: 12px;
  color: var(--text-muted);
}

.version-entry ul {
  margin: 0;
  padding-left: 18px;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text);
}

/* ---------- Page / editor ---------- */
#page {
  display: flex;
  justify-content: center;
  padding: 60px 24px 120px;
}

#editor {
  width: 100%;
  max-width: 720px;
  min-height: 70vh;
  outline: none;
  font-family: "Iowan Old Style", "Palatino Linotype", Georgia, "Times New Roman", serif;
  font-size: 19px;
  line-height: 1.75;
  color: var(--text);
  caret-color: var(--accent);
}

body.font-sans #editor {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

#editor:empty:before {
  content: attr(data-placeholder);
  color: var(--text-muted);
}

#editor h1 {
  font-size: 1.7em;
  line-height: 1.3;
  margin: 0.9em 0 0.4em;
}

#editor h2 {
  font-size: 1.3em;
  line-height: 1.3;
  margin: 0.9em 0 0.4em;
}

#editor p {
  margin: 0 0 1em;
}

#editor blockquote {
  margin: 1em 0;
  padding-left: 1em;
  border-left: 3px solid var(--accent);
  color: var(--text-muted);
  font-style: italic;
}

#editor ul, #editor ol {
  margin: 0 0 1em;
  padding-left: 1.4em;
}

/* ---------- Status bar ---------- */
#statusbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 10px;
  font-size: 12px;
  color: var(--text-muted);
  background: linear-gradient(to top, var(--bg) 60%, transparent);
  pointer-events: none;
}

.status-sep { opacity: 0.6; }

/* ---------- Focus (distraction-free) mode ---------- */
#focusBar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: none;
  justify-content: center;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--toolbar-bg);
  border-bottom: 1px solid var(--border);
  opacity: 0;
  transform: translateY(-100%);
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 30;
}

.focus-sep {
  width: 1px;
  height: 14px;
  background: var(--border);
}

#focusBar button {
  font-size: 12px;
  padding: 4px 10px;
}

body.distraction-free #toolbar {
  display: none;
}

body.distraction-free #statusbar {
  display: none;
}

body.distraction-free #focusBar {
  display: flex;
}

body.distraction-free #focusBar:hover,
body.distraction-free.show-focus-bar #focusBar {
  opacity: 1;
  transform: translateY(0);
}

body.distraction-free #page {
  padding-top: 100px;
}

body.distraction-free #editor {
  max-width: 680px;
}

/* ---------- Fullscreen ---------- */
:fullscreen body,
body:fullscreen {
  background: var(--bg);
}

@media (max-width: 640px) {
  .toolbar-row { gap: 8px; padding: 8px 12px; }
  .tb-title { flex-basis: 100%; }
  #editor { font-size: 17px; }
}

/* ---------- Print / PDF export ---------- */
.print-only {
  display: none;
}

@media print {
  #toolbar, #statusbar, #focusBar {
    display: none !important;
  }
  html, body {
    background: #fff;
  }
  #page {
    padding: 0;
    display: block;
  }
  .print-only {
    display: block;
    font-family: Georgia, "Times New Roman", serif;
    font-size: 24pt;
    margin: 0 0 20pt;
    color: #000;
  }
  #editor {
    max-width: 100%;
    color: #000;
    font-size: 12pt;
    line-height: 1.6;
  }
  #editor blockquote {
    border-left-color: #999;
    color: #333;
  }
}
