:root {
  --bg: #eef1f5;
  --surface: #ffffff;
  --surface-2: #f6f7f9;
  --text: #17191d;
  --muted: #6f7680;
  --border: #dfe3e8;
  --border-strong: #c9ced6;
  --accent: #2563eb;
  --accent-soft: #eaf1ff;
  --danger: #b42318;
  --danger-soft: #fff0ee;
  --shadow: 0 12px 40px rgba(15, 23, 42, .10);
  --code-bg: #f5f7fa;
  --quote: #d7dde5;
  --table-head: #f7f8fa;
  --table-row-alt: rgba(247, 248, 250, .45);
  --selection: rgba(37, 99, 235, .18);
}

html[data-theme="dark"] {
  --bg: #111318;
  --surface: #181b21;
  --surface-2: #20242b;
  --text: #eef1f5;
  --muted: #9da4ae;
  --border: #30353d;
  --border-strong: #424955;
  --accent: #7aa2ff;
  --accent-soft: #202d4a;
  --danger: #ff8d84;
  --danger-soft: #3a2424;
  --shadow: 0 16px 50px rgba(0, 0, 0, .35);
  --code-bg: #20242b;
  --quote: #4b5360;
  --table-head: #20242b;
  --table-row-alt: rgba(32, 36, 43, .45);
  --selection: rgba(122, 162, 255, .22);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

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

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

.app-shell {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  min-height: 0;
  min-width: 0;
}

.topbar {
  min-height: 72px;
  min-width: 0;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 11px; min-width: max-content; }
.brand-mark {
  display: block;
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border-radius: 11px;
  object-fit: cover;
}
.brand strong { display: block; font-size: 14px; line-height: 1.2; }
.brand span { display: block; margin-top: 3px; color: var(--muted); font-size: 11px; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 7px; }
.filename-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  max-width: 210px;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-2);
  transition: border-color .15s ease, box-shadow .15s ease, background .15s ease;
}
.filename-control:focus-within {
  border-color: var(--accent);
  background: var(--surface);
  box-shadow: 0 0 0 2px var(--accent-soft);
}
.filename-label {
  color: var(--muted);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: .08em;
  white-space: nowrap;
}
.filename-control input {
  width: 122px;
  min-width: 0;
  padding: 0;
  border: 0;
  outline: 0;
  color: var(--text);
  background: transparent;
  font: inherit;
  font-size: 12px;
  font-weight: 650;
}
.toolbar-menu { position: relative; }
.toolbar-menu > summary {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  list-style: none;
  white-space: nowrap;
}
.toolbar-menu > summary::after {
  content: "";
  width: 6px;
  height: 6px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(45deg) translateY(-2px);
  transition: transform .15s ease;
}
.toolbar-menu[open] > summary::after { transform: rotate(225deg) translate(-1px, -1px); }
.toolbar-menu > summary::marker { content: ""; }
.toolbar-menu > summary::-webkit-details-marker { display: none; }
.toolbar-menu[open] > summary { border-color: var(--accent); background: var(--accent-soft); }
.toolbar-menu-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 40;
  display: grid;
  min-width: 170px;
  gap: 4px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.menu-item {
  padding: 8px 10px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font: inherit;
  font-size: 12px;
  font-weight: 650;
}
.menu-item:hover { background: var(--accent-soft); }
.menu-item:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  padding: 8px 11px;
  border-radius: 9px;
  cursor: pointer;
  transition: border-color .15s ease, transform .15s ease, background .15s ease;
  font-size: 12px;
  font-weight: 650;
  line-height: 1;
  white-space: nowrap;
}
.btn:hover { border-color: var(--border-strong); background: var(--surface); transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn:disabled { cursor: not-allowed; opacity: .42; transform: none; }
.btn-icon { width: 36px; height: 36px; padding: 0; font-size: 18px; line-height: 1; }
.btn-danger { color: var(--danger); background: var(--danger-soft); }

.workspace {
  --editor-width: 50%;
  min-height: 0;
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(260px, var(--editor-width)) 7px minmax(320px, 1fr);
  background: var(--bg);
}

.panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr;
  background: var(--surface);
}

.editor-panel { position: relative; }
.preview-panel {
  grid-template-rows: auto auto 1fr;
  border-left: 1px solid var(--border);
}

.panel-head {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.panel-kicker {
  display: block;
  margin-bottom: 2px;
  color: var(--muted);
  font-size: 9px;
  letter-spacing: .16em;
  font-weight: 800;
}
.panel-head h1 { margin: 0; font-size: 15px; font-weight: 750; }
.panel-meta { color: var(--muted); font-size: 11px; }

.document-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 7px;
  padding: 7px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.format-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
}
.format-group + .format-group {
  margin-left: 0;
}
.format-history .format-btn {
  width: 30px;
  min-width: 30px;
  padding: 0;
  font-size: 18px;
}
.format-group-clear { margin-left: auto; background: transparent; border-color: transparent; }
.format-btn {
  min-width: 29px;
  height: 29px;
  padding: 4px 7px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
}
.format-btn:hover { border-color: var(--border); background: var(--surface); }
.format-btn.active { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.format-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.format-code { font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace; font-size: 10px; }

#markdownInput {
  width: 100%;
  height: 100%;
  min-height: 0;
  padding: 22px 24px 80px;
  resize: none;
  border: 0;
  outline: 0;
  color: var(--text);
  background: var(--surface);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 14px;
  line-height: 1.65;
  tab-size: 2;
}

#markdownInput::placeholder { color: var(--muted); opacity: .65; }

.drop-hint {
  position: absolute;
  inset: 78px 12px 12px;
  z-index: 4;
  display: none;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: 14px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 17px;
  font-weight: 800;
  pointer-events: none;
}
.editor-panel.dragging .drop-hint { display: grid; }

.resizer {
  position: relative;
  cursor: col-resize;
  background: var(--bg);
}
.resizer::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 3px;
  height: 42px;
  transform: translate(-50%, -50%);
  border-radius: 99px;
  background: var(--border-strong);
  transition: background .15s ease, height .15s ease;
}
.resizer:hover::after,
body.resizing .resizer::after { background: var(--accent); height: 64px; }

.preview-scroll {
  min-height: 0;
  overflow: auto;
  padding: 34px clamp(20px, 4vw, 58px) 80px;
  background: var(--surface-2);
}

.markdown-body {
  margin: 0 auto;
  color: #1d232d;
  background: #fff;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 16px;
  line-height: 1.72;
  overflow-wrap: anywhere;
}

.markdown-body[contenteditable="true"] {
  caret-color: var(--accent);
  outline: 0;
}
.markdown-body[contenteditable="true"]:focus {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}
.preview-edit-hint { display: inline-flex; align-items: center; justify-content: flex-end; gap: 5px; text-align: right; }
.live-status { display: inline-flex; align-items: center; margin-left: 1px; }
.live-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #2fca68;
  box-shadow: 0 0 0 0 rgba(47, 202, 104, .28);
  animation: live-pulse 1.8s ease-in-out infinite;
}
@keyframes live-pulse {
  0%, 100% { opacity: .68; box-shadow: 0 0 0 0 rgba(47, 202, 104, .28); }
  50% { opacity: 1; box-shadow: 0 0 0 4px rgba(47, 202, 104, 0); }
}
.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;
}

html[data-theme="dark"] .markdown-body {
  color: #e8ebef;
  background: #181b21;
}

.markdown-body.paper {
  width: min(100%, 860px);
  min-height: 1080px;
  padding: clamp(42px, 7vw, 78px) clamp(42px, 8vw, 84px);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.markdown-body > :first-child { margin-top: 0 !important; }
.markdown-body > :last-child { margin-bottom: 0 !important; }
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: inherit;
  line-height: 1.25;
  letter-spacing: -.025em;
}
.markdown-body h1 { margin: 0 0 30px; padding-bottom: 18px; border-bottom: 2px solid var(--border); font-size: 2.15em; }
.markdown-body h2 { margin: 42px 0 16px; padding-bottom: 9px; border-bottom: 1px solid var(--border); font-size: 1.48em; }
.markdown-body h3 { margin: 30px 0 12px; font-size: 1.18em; }
.markdown-body h4 { margin: 24px 0 10px; font-size: 1.02em; }
.markdown-body p { margin: 0 0 17px; }
.markdown-body strong { font-weight: 750; }
.markdown-body a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
.markdown-body ul, .markdown-body ol { margin: 0 0 18px; padding-left: 1.6em; }
.markdown-body li { margin: 5px 0; }
.markdown-body li > p { margin-bottom: 8px; }
.markdown-body blockquote {
  margin: 22px 0;
  padding: 3px 0 3px 18px;
  border-left: 4px solid var(--quote);
  color: var(--muted);
}
.markdown-body blockquote > :last-child { margin-bottom: 0; }
.markdown-body hr { height: 1px; margin: 34px 0; border: 0; background: var(--border); }
.markdown-body code {
  padding: .16em .38em;
  border-radius: 5px;
  background: var(--code-bg);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: .88em;
}
.markdown-body pre {
  overflow: auto;
  margin: 20px 0;
  padding: 18px 20px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--code-bg);
  line-height: 1.55;
}
.markdown-body pre code { padding: 0; background: transparent; font-size: 13px; }
.markdown-body table {
  display: table;
  width: 100%;
  margin: 22px 0 28px;
  border-collapse: collapse;
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 13px;
  line-height: 1.45;
}
.markdown-body th,
.markdown-body td { padding: 10px 12px; border: 1px solid var(--border); vertical-align: top; text-align: left; }
.markdown-body th { background: var(--table-head); font-weight: 750; }
.markdown-body tr:nth-child(even) td { background: var(--table-row-alt); }
.markdown-body img { max-width: 100%; height: auto; border-radius: 8px; }
.markdown-body input[type="checkbox"] { transform: translateY(1px); margin-right: 7px; accent-color: var(--accent); }
.markdown-body .empty-state {
  display: grid;
  min-height: 460px;
  place-items: center;
  text-align: center;
  color: var(--muted);
  font-family: Inter, ui-sans-serif, sans-serif;
}
.markdown-body .empty-state strong { display: block; margin-bottom: 8px; color: var(--text); font-size: 20px; }

.statusbar {
  min-height: 28px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 0 14px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 10px;
}
.status-separator { opacity: .45; }
.status-copyright { margin-left: auto; color: var(--muted); letter-spacing: .03em; white-space: nowrap; }
.document-status { color: #21864b; white-space: nowrap; }
.document-status[data-state="dirty"] { color: var(--danger); }
.autosave-status { white-space: nowrap; }

.app-dialog {
  width: min(440px, calc(100vw - 32px));
  max-width: calc(100vw - 32px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.app-dialog::backdrop { background: rgba(15, 23, 42, .42); backdrop-filter: blur(2px); }
.dialog-card { margin: 0; }
.dialog-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 18px 13px;
  border-bottom: 1px solid var(--border);
}
.dialog-kicker {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: .16em;
}
.dialog-head h2 { margin: 0; font-size: 17px; line-height: 1.25; }
.dialog-close {
  display: grid;
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  place-items: center;
  padding: 0;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
}
.dialog-close:hover { border-color: var(--border); background: var(--surface-2); color: var(--text); }
.dialog-close:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.dialog-body { display: grid; gap: 13px; padding: 16px 18px; }
.dialog-intro { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.5; }
.dialog-fields { display: grid; gap: 12px; }
.dialog-fields[hidden], .dialog-error[hidden] { display: none; }
.dialog-field { display: grid; gap: 6px; }
.dialog-field > span { color: var(--text); font-size: 11px; font-weight: 750; }
.dialog-field input {
  width: 100%;
  min-height: 38px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: 0;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 12px;
}
.dialog-field input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.dialog-error { margin: 0; color: var(--danger); font-size: 11px; line-height: 1.45; }
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 7px;
  padding: 12px 18px 18px;
  border-top: 1px solid var(--border);
}
.btn-dialog-primary { border-color: var(--accent); background: var(--accent); color: #fff; }
.btn-dialog-primary:hover { border-color: var(--accent); background: var(--accent); color: #fff; }
html[data-theme="dark"] .btn-dialog-primary,
html[data-theme="dark"] .btn-dialog-primary:hover { color: #111318; }
.shortcuts-dialog { width: min(500px, calc(100vw - 32px)); }
.shortcuts-list { display: grid; gap: 1px; padding: 10px 18px 18px; }
.shortcuts-list > div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 35px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  font-size: 12px;
}
.shortcuts-list > div:last-child { border-bottom: 0; }
kbd {
  padding: 4px 7px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--surface-2);
  color: var(--muted);
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 10px;
  white-space: nowrap;
}
.dialog-field small { color: var(--muted); font-size: 10px; font-weight: 500; }
.dialog-checkbox { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-size: 11px; }
.dialog-checkbox input { margin: 0; accent-color: var(--accent); }
.dialog-feedback { min-height: 17px; margin: 0; color: var(--muted); font-size: 11px; line-height: 1.45; }
.dialog-feedback[data-state="error"] { color: var(--danger); }
.search-dialog { width: min(560px, calc(100vw - 32px)); }
.search-actions { flex-wrap: wrap; }

.toast {
  position: fixed;
  right: 18px;
  bottom: 44px;
  z-index: 50;
  max-width: 340px;
  padding: 10px 13px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--text);
  font-size: 12px;
  font-weight: 650;
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}
.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 980px) {
  .topbar { align-items: flex-start; }
  .brand span { display: none; }
  .preview-edit-hint { display: none; }
  .toolbar { gap: 5px; }
  .filename-control { max-width: 180px; }
  .filename-control input { width: 100px; }
  .btn { padding: 7px 9px; font-size: 11px; }
  .toolbar-menu-panel { right: auto; left: 0; }
}

@media (max-width: 760px) {
  body { overflow: auto; }
  .app-shell { height: auto; min-height: 100vh; }
  .topbar { position: sticky; top: 0; flex-direction: column; align-items: stretch; }
  .brand { justify-content: center; }
  .toolbar {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    width: calc(100vw - 32px);
    max-width: calc(100vw - 32px);
    min-width: 0;
    justify-content: stretch;
  }
  .filename-control { grid-column: 1 / -1; width: 100%; max-width: none; }
  .toolbar > .btn,
  .toolbar > .toolbar-menu,
  .toolbar-menu > summary { width: 100%; min-width: 0; }
  .filename-control input { width: auto; flex: 1; }
  .document-toolbar { gap: 6px; }
  .format-group + .format-group { margin-left: 0; padding-left: 6px; }
  .format-group-clear { margin-left: 0; }
  .workspace { display: block; }
  .panel { min-height: 70vh; }
  .editor-panel { height: 70vh; }
  .preview-panel { border-left: 0; border-top: 1px solid var(--border); }
  .resizer { display: none; }
  .preview-scroll { min-height: 70vh; padding: 20px 12px 60px; }
  .markdown-body.paper { min-height: 0; padding: 34px 24px; }
  .statusbar { display: flex; min-height: 32px; justify-content: flex-start; padding: 7px 12px; font-size: 9px; }
  .status-local, .status-shortcuts { display: none; }
  .status-copyright { margin-left: auto; }
}
