/* ---------- 基础与主题变量 ---------- */
* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg: #ffffff;
  --bg-soft: #f4f5f8;
  --bg-elev: #ffffff;
  --text: #16181d;
  --text-2: #62697a;
  --text-3: #9aa1ad;
  --border: #e7e9ee;
  --accent: #4f46e5;
  --accent-weak: #eef0ff;
  --accent-text: #ffffff;
  --code-bg: #f4f5f8;
  --code-inline: #eef0ff;
  --shadow: 0 10px 34px rgba(16, 24, 40, 0.14);
  --radius: 14px;
  --topbar-h: 56px;
}

@media (prefers-color-scheme: dark) {
  html[data-theme="auto"] {
    --bg: #101217;
    --bg-soft: #181b22;
    --bg-elev: #15181e;
    --text: #e6e8ee;
    --text-2: #9aa2b1;
    --text-3: #6b7381;
    --border: #262a33;
    --accent: #8b8cf7;
    --accent-weak: rgba(124, 126, 246, 0.16);
    --accent-text: #101217;
    --code-bg: #191d25;
    --code-inline: rgba(124, 126, 246, 0.2);
    --shadow: 0 10px 34px rgba(0, 0, 0, 0.55);
  }
}

html[data-theme="dark"] {
  --bg: #101217;
  --bg-soft: #181b22;
  --bg-elev: #15181e;
  --text: #e6e8ee;
  --text-2: #9aa2b1;
  --text-3: #6b7381;
  --border: #262a33;
  --accent: #8b8cf7;
  --accent-weak: rgba(124, 126, 246, 0.16);
  --accent-text: #101217;
  --code-bg: #191d25;
  --code-inline: rgba(124, 126, 246, 0.2);
  --shadow: 0 10px 34px rgba(0, 0, 0, 0.55);
}

[hidden] {
  display: none !important;
}

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", sans-serif;
}

button {
  font-family: inherit;
  cursor: pointer;
}

/* ---------- 应用骨架 ---------- */
.app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- 顶栏 ---------- */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(env(safe-area-inset-top) + 6px) 6px 6px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.icon-btn {
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  padding: 0;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text-2);
  display: grid;
  place-items: center;
  transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease;
}

.icon-btn:active {
  background: var(--bg-soft);
  color: var(--text);
  transform: scale(0.94);
}

#btnMode[aria-pressed="true"] {
  color: var(--accent);
  background: var(--accent-weak);
}

.ic-edit {
  display: none;
}

body.preview-mode .ic-eye {
  display: none;
}

body.preview-mode .ic-edit {
  display: block;
}

.title-wrap {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 0 2px;
}

.doc-title {
  width: 100%;
  border: 0;
  outline: none;
  background: transparent;
  padding: 0;
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  line-height: 1.35;
  text-overflow: ellipsis;
}

.doc-title::placeholder {
  color: var(--text-3);
  font-weight: 500;
}

.save-state {
  font-size: 11px;
  line-height: 1.3;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.save-state.is-saving {
  color: var(--accent);
}

/* ---------- 编辑 / 预览 ---------- */
.workspace {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  position: relative;
  overflow: hidden;
}

.editor {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  border: 0;
  outline: none;
  resize: none;
  padding: 14px 16px 40px calc(16px + env(safe-area-inset-left));
  background: var(--bg);
  color: var(--text);
  font-family: ui-monospace, "SF Mono", "Roboto Mono", Menlo, Consolas,
    "PingFang SC", "Microsoft YaHei", monospace;
  font-size: 16px;
  line-height: 1.78;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  caret-color: var(--accent);
}

.editor::placeholder {
  color: var(--text-3);
  opacity: 1;
}

.preview {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 6px 18px 80px;
  overscroll-behavior: contain;
}

/* ---------- 工具栏 ---------- */
.toolbar {
  flex: 0 0 auto;
  background: var(--bg);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}

.toolbar-scroll {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}

.toolbar-scroll::-webkit-scrollbar {
  display: none;
}

.tool-btn {
  flex: 0 0 auto;
  min-width: 44px;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--bg-soft);
  color: var(--text);
  font-size: 14px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.14s ease, color 0.14s ease, border-color 0.14s ease,
    transform 0.1s ease;
}

.tool-btn:active {
  background: var(--accent-weak);
  border-color: var(--accent);
  color: var(--accent);
  transform: scale(0.95);
}

.tool-divider {
  flex: 0 0 auto;
  width: 1px;
  height: 22px;
  margin: 0 2px;
  background: var(--border);
}

/* ---------- 抽屉 ---------- */
.scrim {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(12, 15, 22, 0.4);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.24s ease;
  backdrop-filter: blur(1px);
}

.scrim.show {
  opacity: 1;
  pointer-events: auto;
}

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 40;
  width: min(86vw, 348px);
  display: flex;
  flex-direction: column;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  box-shadow: var(--shadow);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  transform: translateX(-102%);
  transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  will-change: transform;
}

.drawer.open {
  transform: translateX(0);
}

.drawer-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 16px 12px;
}

.drawer-title h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.2px;
}

.drawer-title p {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-3);
}

.btn-primary {
  flex: 0 0 auto;
  height: 36px;
  padding: 0 16px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 14px;
  font-weight: 600;
  transition: filter 0.15s ease, transform 0.1s ease;
}

.btn-primary:active {
  filter: brightness(0.92);
  transform: scale(0.96);
}

.doc-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 4px 10px 16px;
  -webkit-overflow-scrolling: touch;
}

.doc-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 11px 12px;
  margin-bottom: 4px;
  border-radius: 12px;
  border: 1px solid transparent;
  transition: background 0.15s ease;
  cursor: pointer;
}

.doc-item:active {
  background: var(--bg-soft);
}

.doc-item.active {
  background: var(--accent-weak);
  border-color: color-mix(in srgb, var(--accent) 26%, transparent);
}

.doc-item-main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.doc-item-title {
  font-size: 14.5px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.doc-item.active .doc-item-title {
  color: var(--accent);
}

.doc-item-meta {
  font-size: 11.5px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.doc-del {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  border-radius: 9px;
  color: var(--text-3);
  font-size: 13px;
}

.doc-del:active {
  background: rgba(239, 68, 68, 0.14);
  color: #ef4444;
}

.drawer-empty {
  padding: 40px 20px;
  text-align: center;
  font-size: 13px;
  color: var(--text-3);
}

.drawer-foot {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 10px 12px 12px;
  border-top: 1px solid var(--border);
}

.foot-btn {
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-soft);
  color: var(--text-2);
  font-size: 13px;
}

.foot-btn:active {
  background: var(--accent-weak);
  border-color: var(--accent);
  color: var(--accent);
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(78px + env(safe-area-inset-bottom));
  z-index: 60;
  max-width: 78vw;
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(22, 26, 34, 0.94);
  color: #fff;
  font-size: 13px;
  line-height: 1.4;
  text-align: center;
  opacity: 0;
  transform: translate(-50%, 10px);
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- Markdown 排版 ---------- */
.preview {
  color: var(--text);
  font-size: 16px;
  line-height: 1.8;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.preview > :first-child {
  margin-top: 8px;
}

.preview h1,
.preview h2,
.preview h3,
.preview h4,
.preview h5,
.preview h6 {
  margin: 1.5em 0 0.55em;
  line-height: 1.35;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.preview h1 {
  font-size: 1.62em;
  padding-bottom: 0.3em;
  border-bottom: 1px solid var(--border);
}

.preview h2 {
  font-size: 1.34em;
}

.preview h3 {
  font-size: 1.16em;
}

.preview h4,
.preview h5,
.preview h6 {
  font-size: 1em;
}

.preview p {
  margin: 0.85em 0;
}

.preview a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid color-mix(in srgb, var(--accent) 40%, transparent);
}

.preview strong {
  font-weight: 700;
}

.preview em {
  font-style: italic;
}

.preview del {
  color: var(--text-3);
}

.preview mark {
  background: rgba(250, 204, 21, 0.35);
  color: inherit;
  padding: 0 2px;
  border-radius: 3px;
}

.preview code {
  padding: 0.15em 0.4em;
  border-radius: 5px;
  background: var(--code-inline);
  color: var(--accent);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.88em;
  word-break: break-word;
}

.preview pre.code-block {
  position: relative;
  margin: 1em 0;
  padding: 14px 14px;
  border-radius: 12px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.preview pre.code-block code {
  display: block;
  padding: 0;
  background: transparent;
  color: var(--text);
  font-size: 13.5px;
  line-height: 1.65;
  white-space: pre;
  word-break: normal;
}

.code-lang {
  display: block;
  margin-bottom: 8px;
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-3);
}

.preview blockquote {
  margin: 1em 0;
  padding: 0.2em 0 0.2em 14px;
  border-left: 3px solid color-mix(in srgb, var(--accent) 55%, transparent);
  color: var(--text-2);
  background: linear-gradient(
    90deg,
    color-mix(in srgb, var(--accent) 6%, transparent),
    transparent 60%
  );
  border-radius: 0 8px 8px 0;
}

.preview blockquote p {
  margin: 0.4em 0;
}

.preview ul,
.preview ol {
  margin: 0.75em 0;
  padding-left: 1.35em;
}

.preview li {
  margin: 0.3em 0;
}

.preview li > ul,
.preview li > ol {
  margin: 0.3em 0;
}

.preview li.task-item {
  list-style: none;
  margin-left: -1.25em;
}

.preview .task {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.preview .task input[type="checkbox"] {
  flex: 0 0 auto;
  width: 17px;
  height: 17px;
  margin: 5px 0 0;
  accent-color: var(--accent);
}

.preview hr {
  height: 1px;
  margin: 1.8em 0;
  border: 0;
  background: var(--border);
}

.table-wrap {
  margin: 1em 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border);
  border-radius: 10px;
}

.preview table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.preview th,
.preview td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.preview th:last-child,
.preview td:last-child {
  border-right: 0;
}

.preview tbody tr:last-child td {
  border-bottom: 0;
}

.preview thead th {
  background: var(--bg-soft);
  font-weight: 600;
  white-space: nowrap;
}

.preview img {
  max-width: 100%;
  height: auto;
  border-radius: 10px;
  display: block;
  margin: 0.8em 0;
}

/* ---------- 桌面 / 横屏 ---------- */
@media (min-width: 760px) {
  .editor {
    padding: 20px max(24px, calc((100% - 760px) / 2)) 60px;
  }

  .preview {
    padding: 12px max(24px, calc((100% - 760px) / 2)) 100px;
  }

  .toolbar-scroll {
    justify-content: center;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
