/* === Base reset & variables === */
*, *::before, *::after {
  box-sizing: border-box;
}

:root {
  --app-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --editor-font: "Georgia", "Times New Roman", serif;
  --code-font: "SF Mono", Monaco, "Cascadia Code", "Roboto Mono", Consolas, "Courier New", monospace;
  --radius: 6px;
  --sidebar-width: 240px;
  --titlebar-height: 38px;
  --menubar-height: 28px;
  --statusbar-height: 24px;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: var(--app-font);
  font-size: 14px;
}

body {
  background: var(--bg-color);
  color: var(--text-color);
}

.app {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  background: var(--bg-color);
  color: var(--text-color);
}

/* === Themes === */
.theme-github {
  --bg-color: #f5f5f5;
  --surface-color: #ffffff;
  --panel-color: #f5f5f5;
  --text-color: #24292e;
  --muted-color: #6a737d;
  --border-color: #e1e4e8;
  --accent-color: #0366d6;
  --link-color: #0366d6;
  --code-bg: #f6f8fa;
  --code-text: #24292e;
  --blockquote-border: #dfe2e5;
  --blockquote-text: #6a737d;
  --selection-bg: #0366d6;
  --selection-color: #ffffff;
  --caret-color: #24292e;
  --titlebar-bg: #e8e8e8;
  --menubar-bg: #f5f5f5;
  --statusbar-bg: #f0f0f0;
  --hover-bg: rgba(0, 0, 0, 0.05);
  --focus-dim: rgba(0, 0, 0, 0.04);
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.theme-night {
  --bg-color: #1f1f1f;
  --surface-color: #252526;
  --panel-color: #2d2d30;
  --text-color: #d4d4d4;
  --muted-color: #858585;
  --border-color: #3e3e42;
  --accent-color: #4fc1ff;
  --link-color: #4fc1ff;
  --code-bg: #1e1e1e;
  --code-text: #d4d4d4;
  --blockquote-border: #3e3e42;
  --blockquote-text: #9aa0a6;
  --selection-bg: #264f78;
  --selection-color: #ffffff;
  --caret-color: #d4d4d4;
  --titlebar-bg: #2d2d30;
  --menubar-bg: #252526;
  --statusbar-bg: #2d2d30;
  --hover-bg: rgba(255, 255, 255, 0.06);
  --focus-dim: rgba(0, 0, 0, 0.25);
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
}

.theme-newsprint {
  --bg-color: #f4f1ea;
  --surface-color: #fffdf7;
  --panel-color: #f0ece3;
  --text-color: #2d2a26;
  --muted-color: #8c8278;
  --border-color: #d8d3ca;
  --accent-color: #8b0000;
  --link-color: #8b0000;
  --code-bg: #f2efe9;
  --code-text: #2d2a26;
  --blockquote-border: #c9c3ba;
  --blockquote-text: #6b6560;
  --selection-bg: #8b0000;
  --selection-color: #fffdf7;
  --caret-color: #2d2a26;
  --titlebar-bg: #eae6dd;
  --menubar-bg: #f4f1ea;
  --statusbar-bg: #efece4;
  --hover-bg: rgba(0, 0, 0, 0.04);
  --focus-dim: rgba(0, 0, 0, 0.03);
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.07);
  --editor-font: "Times New Roman", Times, serif;
}

.theme-gothic {
  --bg-color: #1a1a1a;
  --surface-color: #222222;
  --panel-color: #2a2a2a;
  --text-color: #e5e5e5;
  --muted-color: #999999;
  --border-color: #444444;
  --accent-color: #d4a017;
  --link-color: #d4a017;
  --code-bg: #181818;
  --code-text: #e5e5e5;
  --blockquote-border: #555555;
  --blockquote-text: #bbbbbb;
  --selection-bg: #d4a017;
  --selection-color: #1a1a1a;
  --caret-color: #e5e5e5;
  --titlebar-bg: #252525;
  --menubar-bg: #1f1f1f;
  --statusbar-bg: #252525;
  --hover-bg: rgba(255, 255, 255, 0.08);
  --focus-dim: rgba(0, 0, 0, 0.35);
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
}

/* === Title bar === */
.title-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--titlebar-height);
  padding: 0 12px;
  background: var(--titlebar-bg);
  border-bottom: 1px solid var(--border-color);
  -webkit-app-region: drag;
  user-select: none;
}

.title-left, .title-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.title-center {
  font-weight: 600;
  opacity: 0.7;
  flex: 0 0 auto;
  text-align: center;
}

.traffic-lights {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-right: 12px;
}

.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: inline-block;
}

.dot.red { background: #ff5f56; border: 1px solid #e0443e; }
.dot.yellow { background: #ffbd2e; border: 1px solid #dea123; }
.dot.green { background: #27c93f; border: 1px solid #1aab29; }

.doc-title {
  font-size: 13px;
  opacity: 0.9;
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unsaved-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent-color);
  opacity: 0;
  margin-left: 4px;
}

.unsaved-dot.visible { opacity: 1; }

/* === Menu bar === */
.menu-bar {
  display: flex;
  align-items: center;
  height: var(--menubar-height);
  padding: 0 8px;
  background: var(--menubar-bg);
  border-bottom: 1px solid var(--border-color);
  font-size: 13px;
  user-select: none;
}

.menu {
  position: relative;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
}

.menu:hover, .menu.open {
  background: var(--hover-bg);
}

.menu-dropdowns {
  position: absolute;
  top: calc(var(--titlebar-height) + var(--menubar-height));
  left: 0;
  z-index: 1000;
  pointer-events: none;
}

.menu-dropdown {
  position: absolute;
  min-width: 220px;
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px 0;
  pointer-events: auto;
}

.menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 16px;
  cursor: pointer;
  font-size: 13px;
}

.menu-item:hover {
  background: var(--accent-color);
  color: var(--selection-color);
}

.menu-item .shortcut {
  opacity: 0.6;
  font-size: 12px;
  margin-left: 16px;
}

.menu-item:hover .shortcut { opacity: 0.9; }

.menu-separator {
  height: 1px;
  background: var(--border-color);
  margin: 6px 0;
}

.menu-item.disabled {
  opacity: 0.45;
  pointer-events: none;
}

/* === Main layout === */
.main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* === Sidebar === */
.sidebar {
  width: var(--sidebar-width);
  flex: 0 0 var(--sidebar-width);
  background: var(--panel-color);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  user-select: none;
  transition: transform 0.2s ease;
}

.sidebar.hidden {
  display: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-tabs {
  display: flex;
  gap: 4px;
}

.tab {
  padding: 4px 10px;
  font-size: 12px;
  border: none;
  background: transparent;
  color: var(--muted-color);
  cursor: pointer;
  border-radius: 4px;
}

.tab.active {
  background: var(--hover-bg);
  color: var(--text-color);
  font-weight: 600;
}

.sidebar-close {
  border: none;
  background: transparent;
  color: var(--muted-color);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sidebar-close:hover { background: var(--hover-bg); border-radius: 4px; }

.sidebar-actions {
  display: flex;
  gap: 6px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border-color);
}

.sidebar-action {
  flex: 1;
  padding: 5px 8px;
  font-size: 12px;
  border: 1px solid var(--border-color);
  background: var(--surface-color);
  color: var(--text-color);
  border-radius: 4px;
  cursor: pointer;
}

.sidebar-action:hover { background: var(--hover-bg); }

.sidebar-content {
  flex: 1;
  overflow-y: auto;
  padding: 6px 0;
  font-size: 13px;
}

.sidebar-content:focus { outline: none; }

.tree, .outline-tree {
  list-style: none;
  margin: 0;
  padding: 0;
}

.tree-item, .outline-item {
  padding: 5px 12px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  gap: 6px;
}

.tree-item:hover, .outline-item:hover { background: var(--hover-bg); }

.tree-item.active { background: var(--accent-color); color: var(--selection-color); }

.tree-item .icon, .outline-item .icon {
  opacity: 0.7;
  font-size: 12px;
  flex: 0 0 16px;
  text-align: center;
}

.tree-item .name { flex: 1; }

.tree-children { padding-left: 14px; }

.tree-item.editing .name { display: none; }

.tree-item input {
  flex: 1;
  font: inherit;
  border: 1px solid var(--accent-color);
  background: var(--surface-color);
  color: var(--text-color);
  padding: 2px 4px;
  border-radius: 3px;
  outline: none;
}

.empty-sidebar {
  padding: 16px;
  text-align: center;
  color: var(--muted-color);
  font-size: 13px;
}

.outline-item.h1 { padding-left: 12px; font-weight: 700; }
.outline-item.h2 { padding-left: 24px; }
.outline-item.h3 { padding-left: 36px; }
.outline-item.h4 { padding-left: 48px; }
.outline-item.h5 { padding-left: 60px; }
.outline-item.h6 { padding-left: 72px; }

.outline-item.active-outline { background: var(--hover-bg); border-left: 2px solid var(--accent-color); }

/* === Editor frame === */
.editor-frame {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

.editor-scroller {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  scroll-behavior: smooth;
}

/* === Editor / document === */
.editor {
  max-width: 860px;
  margin: 0 auto;
  padding: 50px 60px 100px;
  min-height: 100%;
  outline: none;
  font-family: var(--editor-font);
  font-size: 17px;
  line-height: 1.7;
  color: var(--text-color);
  background: var(--surface-color);
  box-shadow: none;
  transition: background 0.2s, color 0.2s;
  position: relative;
}

#app.theme-night .editor,
#app.theme-gothic .editor { background: var(--surface-color); }

/* selection */
.editor ::selection,
.source-editor textarea::selection {
  background: var(--selection-bg);
  color: var(--selection-color);
}

.editor *::selection {
  background: var(--selection-bg);
  color: var(--selection-color);
}

.editor ::-moz-selection {
  background: var(--selection-bg);
  color: var(--selection-color);
}

/* Typewriter mode scroll behavior */
.app.typewriter-mode .editor-scroller { scroll-behavior: auto; }

/* Focus mode dimming */
.app.focus-mode .editor > .block:not(.focused) {
  opacity: 0.35;
  filter: blur(0.3px);
  transition: opacity 0.25s ease;
}

.app.focus-mode .editor > .block.focused {
  opacity: 1;
}

/* === Blocks === */
.block {
  position: relative;
  margin: 0;
  padding: 4px 0;
  outline: none;
  min-height: 1.6em;
  transition: opacity 0.2s ease;
}

.block:hover {
  border-radius: 4px;
  background: var(--hover-bg);
}

.block.focused {
  background: transparent;
  border-radius: 0;
}

.block:empty::before,
.block[data-type="paragraph"]:empty::before {
  content: attr(data-placeholder);
  color: var(--muted-color);
  opacity: 0.55;
  pointer-events: none;
  position: absolute;
  left: 0;
  top: 4px;
}

/* source view inside block */
.block-source {
  font-family: var(--code-font);
  font-size: 15px;
  line-height: 1.6;
  min-height: 1.6em;
  white-space: pre-wrap;
  word-break: break-word;
  outline: none;
}

/* Heading blocks */
.block[data-type="heading"] h1,
.block[data-type="heading"] h2,
.block[data-type="heading"] h3,
.block[data-type="heading"] h4,
.block[data-type="heading"] h5,
.block[data-type="heading"] h6 {
  margin: 0;
  padding: 0;
  line-height: 1.25;
  font-weight: 600;
}

.block[data-type="heading"] h1 { font-size: 2.2em; border-bottom: 2px solid var(--border-color); padding-bottom: 0.3em; }
.block[data-type="heading"] h2 { font-size: 1.75em; border-bottom: 1px solid var(--border-color); padding-bottom: 0.3em; }
.block[data-type="heading"] h3 { font-size: 1.45em; }
.block[data-type="heading"] h4 { font-size: 1.2em; }
.block[data-type="heading"] h5 { font-size: 1.05em; }
.block[data-type="heading"] h6 { font-size: 0.95em; color: var(--muted-color); }

/* Lists */
.block ul, .block ol {
  margin: 0;
  padding-left: 1.8em;
}

.block li {
  margin: 0.25em 0;
}

.block li > p {
  margin: 0;
}

.task-list-item {
  list-style: none;
  margin-left: -1.4em;
  display: flex;
  align-items: baseline;
  gap: 0.35em;
}

.task-checkbox {
  width: 14px;
  height: 14px;
  margin-top: 0.35em;
  cursor: pointer;
  accent-color: var(--accent-color);
}

/* Blockquote */
.block blockquote {
  margin: 0;
  padding: 0.2em 0 0.2em 1em;
  border-left: 4px solid var(--blockquote-border);
  color: var(--blockquote-text);
}

.block blockquote p { margin: 0.35em 0; }
.block blockquote blockquote { margin-left: 0.5em; }

/* Code blocks */
.block pre {
  margin: 0;
  padding: 1em;
  background: var(--code-bg);
  border-radius: var(--radius);
  overflow-x: auto;
  font-family: var(--code-font);
  font-size: 14px;
  line-height: 1.5;
  border: 1px solid var(--border-color);
  position: relative;
}

.block pre .code-lang {
  position: absolute;
  top: 0;
  right: 0;
  padding: 2px 8px;
  font-size: 11px;
  color: var(--muted-color);
  background: var(--surface-color);
  border-bottom-left-radius: var(--radius);
  border-left: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
  user-select: none;
}

.block code {
  font-family: var(--code-font);
  font-size: 0.9em;
  background: var(--code-bg);
  color: var(--code-text);
  padding: 0.15em 0.35em;
  border-radius: 4px;
}

.block pre code {
  background: transparent;
  padding: 0;
  font-size: inherit;
  border-radius: 0;
}

/* Syntax highlighting colors */
.hljs-keyword, .token.keyword { color: #d73a49; }
.theme-night .hljs-keyword, .theme-night .token.keyword { color: #c586c0; }
.hljs-string, .token.string { color: #032f62; }
.theme-night .hljs-string, .theme-night .token.string { color: #ce9178; }
.hljs-number, .token.number { color: #005cc5; }
.theme-night .hljs-number, .theme-night .token.number { color: #b5cea8; }
.hljs-comment, .token.comment { color: #6a737d; font-style: italic; }
.theme-night .hljs-comment, .theme-night .token.comment { color: #6a9955; }
.hljs-function, .token.function { color: #6f42c1; }
.theme-night .hljs-function, .theme-night .token.function { color: #dcdcaa; }
.hljs-operator, .token.operator { color: #d73a49; }
.hljs-punctuation, .token.punctuation { color: var(--text-color); }

/* Tables */
.block table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.5em 0;
}

.block th, .block td {
  border: 1px solid var(--border-color);
  padding: 6px 12px;
  text-align: left;
}

.block th {
  background: var(--code-bg);
  font-weight: 600;
}

.block tr:nth-child(even) { background: var(--hover-bg); }

.block th.align-center, .block td.align-center { text-align: center; }
.block th.align-right, .block td.align-right { text-align: right; }

/* Horizontal rule */
.block[data-type="hr"] hr {
  border: none;
  border-top: 2px solid var(--border-color);
  margin: 1.2em 0;
}

/* Math */
.block .math-block {
  text-align: center;
  padding: 0.8em;
  font-family: var(--code-font);
  font-size: 1.05em;
  color: var(--text-color);
  overflow-x: auto;
}

.block .math-inline {
  font-family: var(--code-font);
  background: var(--code-bg);
  padding: 0.1em 0.3em;
  border-radius: 4px;
  font-size: 0.95em;
}

/* Footnotes */
.footnote-ref {
  vertical-align: super;
  font-size: 0.75em;
  color: var(--accent-color);
  text-decoration: none;
}

.footnotes {
  border-top: 1px solid var(--border-color);
  padding-top: 1em;
  margin-top: 2em;
  font-size: 0.9em;
}

.footnotes ol { padding-left: 1.5em; }

.footnote-back { color: var(--accent-color); text-decoration: none; margin-left: 0.4em; }

/* Inline styles */
.editor a {
  color: var(--link-color);
  text-decoration: none;
}

.editor a:hover { text-decoration: underline; }

.editor strong, .editor b { font-weight: 600; }
.editor em, .editor i { font-style: italic; }
.editor del, .editor s { text-decoration: line-through; }

/* Image */
.editor img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0.5em 0;
  border-radius: 4px;
}

/* Source mode */
.source-editor {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
}

.source-editor textarea {
  flex: 1;
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  padding: 50px 60px;
  font-family: var(--code-font);
  font-size: 15px;
  line-height: 1.6;
  background: var(--surface-color);
  color: var(--text-color);
  resize: none;
  tab-size: 4;
}

/* Empty placeholder */
.empty-placeholder {
  position: absolute;
  top: 30%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: var(--muted-color);
  pointer-events: none;
  max-width: 400px;
  padding: 0 20px;
}

.empty-placeholder p { margin: 0.5em 0; }
.empty-hint { font-size: 13px; opacity: 0.8; }
.empty-placeholder kbd {
  display: inline-block;
  padding: 2px 6px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-family: var(--code-font);
  font-size: 12px;
  background: var(--code-bg);
}

/* === Status bar === */
.status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--statusbar-height);
  padding: 0 12px;
  background: var(--statusbar-bg);
  border-top: 1px solid var(--border-color);
  font-size: 12px;
  user-select: none;
}

.status-left, .status-center, .status-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.status-btn {
  border: none;
  background: transparent;
  color: var(--text-color);
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
  cursor: pointer;
}

.status-btn:hover { background: var(--hover-bg); }
.status-btn.active { background: var(--accent-color); color: var(--selection-color); }

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

/* === Find bar === */
.find-bar {
  position: absolute;
  bottom: var(--statusbar-height);
  right: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: var(--panel-color);
  border-top: 1px solid var(--border-color);
  border-left: 1px solid var(--border-color);
  border-top-left-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 900;
}

.find-bar input {
  border: 1px solid var(--border-color);
  background: var(--surface-color);
  color: var(--text-color);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 13px;
  min-width: 160px;
}

.find-bar button {
  border: 1px solid var(--border-color);
  background: var(--surface-color);
  color: var(--text-color);
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}

.find-bar button:hover { background: var(--hover-bg); }
.find-close { font-size: 16px; padding: 2px 8px !important; }

/* === Dialogs === */
.dialog-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.dialog {
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 24px;
  min-width: 320px;
  max-width: 90vw;
}

.dialog h3 { margin: 0 0 8px; font-size: 16px; }
.dialog p { margin: 0 0 16px; color: var(--muted-color); font-size: 13px; }

#dialog-input {
  width: 100%;
  border: 1px solid var(--border-color);
  background: var(--bg-color);
  color: var(--text-color);
  padding: 8px 10px;
  border-radius: 4px;
  font-size: 14px;
  margin-bottom: 16px;
}

.dialog-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.dialog-buttons button {
  padding: 6px 14px;
  border-radius: 4px;
  border: 1px solid var(--border-color);
  background: var(--bg-color);
  color: var(--text-color);
  cursor: pointer;
  font-size: 13px;
}

.dialog-ok { background: var(--accent-color) !important; color: var(--selection-color) !important; border-color: var(--accent-color) !important; }

/* === Menu dropdown positioning === */
.menu-dropdown {
  display: none;
}

.menu-dropdown.open { display: block; }

/* === Mobile / responsive === */
@media (max-width: 768px) {
  :root {
    --sidebar-width: 200px;
  }

  .editor {
    padding: 24px 20px 60px;
    font-size: 16px;
  }

  .menu-bar {
    overflow-x: auto;
    scrollbar-width: none;
  }

  .menu-bar::-webkit-scrollbar { display: none; }

  .sidebar {
    position: absolute;
    top: calc(var(--titlebar-height) + var(--menubar-height));
    left: 0;
    height: calc(100% - var(--titlebar-height) - var(--menubar-height) - var(--statusbar-height));
    z-index: 500;
    box-shadow: var(--shadow);
  }

  .sidebar.hidden-mobile { transform: translateX(-100%); }

  .find-bar {
    left: 0;
    right: 0;
    bottom: var(--statusbar-height);
    border-radius: 0;
    flex-wrap: wrap;
  }

  .status-left, .status-center, .status-right {
    gap: 6px;
  }

  .status-bar { font-size: 11px; padding: 0 8px; }
  .status-btn { font-size: 11px; padding: 2px 4px; }
}

@media (max-width: 480px) {
  .title-center { display: none; }
  .doc-title { max-width: 140px; }
  .status-center { display: none; }
  .status-right { gap: 4px; }
}
