:root {
  color-scheme: light dark;
  --bg: #f4f0e8;
  --panel: rgba(255, 255, 255, 0.84);
  --ink: #232522;
  --muted: #78756d;
  --line: #d8d1c5;
  --accent: #176f69;
  --accent-ink: #ffffff;
  --danger: #a9443f;
  --dialog-bg: #ffffff;
  --shadow: 0 18px 45px rgba(55, 48, 38, 0.13);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #20211f;
    --panel: rgba(38, 39, 36, 0.9);
    --ink: #efeee8;
    --muted: #aaa69b;
    --line: #48463f;
    --accent: #58b8ad;
    --accent-ink: #11201e;
    --danger: #f07f77;
    --dialog-bg: #262724;
    --shadow: 0 18px 45px rgba(0, 0, 0, 0.32);
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 18px;
  background:
    linear-gradient(135deg, rgba(23, 111, 105, 0.08), transparent 38%),
    var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

.shell {
  width: min(860px, 100%);
  height: min(540px, calc(100vh - 36px));
  min-height: 380px;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}

.brand {
  min-width: 0;
}

.title-input {
  width: min(340px, 42vw);
  border: 0;
  outline: 0;
  padding: 2px 0;
  background: transparent;
  color: var(--ink);
  font-size: 16px;
  font-weight: 650;
}

.save-state {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.tool,
.icon-tool {
  height: 30px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  color: var(--ink);
  background: transparent;
  cursor: pointer;
}

.tool.active {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
}

.icon-tool:hover,
.tool:hover {
  border-color: var(--accent);
}

.danger {
  color: var(--danger);
}

.menu-wrap {
  position: relative;
}

.gear {
  width: 40px;
  padding: 0;
  font-size: 21px;
  line-height: 1;
}

.settings-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 4px;
  z-index: 4;
  width: 168px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.insert-menu {
  right: auto;
  left: 0;
  width: 118px;
}

.settings-menu button {
  width: 100%;
  border: 0;
  border-radius: 6px;
  padding: 8px 9px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.settings-menu button:hover {
  background: rgba(127, 127, 127, 0.14);
}

.settings-menu button.danger {
  color: var(--danger);
}

.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(32, 33, 31, 0.28);
}

.dialog-backdrop.hidden {
  display: none;
}

.dialog {
  width: min(360px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: var(--dialog-bg);
  box-shadow: var(--shadow);
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.dialog h2,
.dialog p {
  margin: 0;
}

.dialog h2 {
  font-size: 16px;
}

.dialog-close {
  width: 28px;
  height: 28px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.dialog-section {
  margin-top: 14px;
}

.dialog-section p {
  color: var(--muted);
  font-size: 12px;
}

.dialog-input {
  width: 100%;
  height: 34px;
  margin-top: 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: transparent;
  color: var(--ink);
  outline: 0;
}

.dialog-input:focus {
  border-color: var(--accent);
}

.advanced-settings {
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.advanced-settings summary {
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}

.advanced-settings label {
  display: block;
  margin-top: 10px;
}

.advanced-settings span {
  color: var(--muted);
  font-size: 12px;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-top: 7px;
}

.segmented label {
  min-height: 34px;
  display: flex;
  align-items: center;
  gap: 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  cursor: pointer;
}

.dialog-primary {
  width: 100%;
  height: 34px;
  margin-top: 16px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
}

.dialog-actions {
  display: grid;
  gap: 8px;
  margin-top: 16px;
}

.dialog-actions .dialog-primary {
  margin-top: 0;
}

.dialog-secondary {
  width: 100%;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.app-body {
  min-height: 0;
  display: grid;
  grid-template-columns: 148px 1fr;
}

.note-sidebar {
  min-height: 0;
  padding: 10px;
  border-right: 1px solid var(--line);
  background: rgba(127, 127, 127, 0.06);
  overflow: auto;
}

.new-note-btn {
  position: sticky;
  top: 0;
  z-index: 2;
  width: 100%;
  height: 30px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
}

.note-list {
  display: grid;
  gap: 5px;
  margin-top: 10px;
}

.note-item {
  position: relative;
  width: 100%;
  height: 54px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 7px 8px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  overflow: hidden;
}

.note-item:hover {
  border-color: var(--line);
}

.note-item.active {
  border-color: rgba(23, 111, 105, 0.3);
  background: rgba(23, 111, 105, 0.12);
}

.pin-badge {
  position: absolute;
  right: 6px;
  bottom: 6px;
  width: 11px;
  height: 11px;
  border: 1px solid var(--danger);
  border-radius: 50%;
  background: transparent;
  box-shadow: 0 0 0 2px var(--panel);
}

.pin-badge:hover {
  transform: scale(1.12);
}

.note-item.pinned .pin-badge {
  background: var(--danger);
}

.note-item.pinned .note-item-title::before {
  content: "置顶 · ";
  color: var(--danger);
  font-weight: 700;
}

.note-item-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 16px;
  font-size: 13px;
  font-weight: 650;
}

.note-item-meta {
  display: block;
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 15px;
  color: var(--muted);
  font-size: 11px;
}

.workspace {
  position: relative;
  min-height: 0;
}

#editor,
.preview,
.focus-layer {
  width: 100%;
  height: 100%;
  border: 0;
  outline: 0;
  resize: none;
  padding: 18px 22px 28px;
  background: transparent;
  color: var(--ink);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 14px;
  line-height: 1.58;
  overflow-wrap: anywhere;
  word-break: break-word;
}

#editor {
  position: relative;
  z-index: 1;
}

.focus-layer {
  position: absolute;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  pointer-events: none;
}

.focus-line {
  min-height: 1.58em;
  color: var(--ink);
  white-space: pre;
}

.focus-mode #editor {
  color: transparent;
  caret-color: var(--ink);
}

.focus-mode #editor::selection {
  background: rgba(23, 111, 105, 0.2);
}

.preview {
  overflow: auto;
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
}

.hidden {
  display: none;
}

.preview h1,
.preview h2,
.preview h3 {
  margin: 0.9em 0 0.35em;
  line-height: 1.2;
}

.preview h1 {
  font-size: 24px;
}

.preview h2 {
  font-size: 19px;
}

.preview h3 {
  font-size: 18px;
}

.preview p,
.preview ul,
.preview ol,
.preview blockquote,
.preview pre {
  margin: 0.75em 0;
}

.preview code {
  padding: 0.12em 0.32em;
  border-radius: 4px;
  background: rgba(127, 127, 127, 0.16);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

.preview pre {
  overflow: auto;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
}

.preview pre code {
  padding: 0;
  background: transparent;
}

.preview blockquote {
  padding-left: 12px;
  border-left: 3px solid var(--accent);
  color: var(--muted);
}

.preview img,
.preview video {
  max-width: 100%;
  border-radius: 6px;
  border: 1px solid var(--line);
  cursor: zoom-in;
}

.preview video {
  display: block;
}

.preview .media-size-small {
  max-width: 28%;
}

.preview .media-size-medium {
  max-width: 58%;
}

.preview .media-size-original {
  max-width: 100%;
}

.preview a {
  color: var(--accent);
  cursor: pointer;
}

.drop-hint {
  position: absolute;
  inset: 14px;
  display: none;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: 8px;
  background: rgba(23, 111, 105, 0.09);
  color: var(--accent);
  font-weight: 700;
  pointer-events: none;
}

.workspace.dragging .drop-hint {
  display: grid;
}

@media (max-width: 680px) {
  body {
    padding: 10px;
  }

  .shell {
    height: calc(100vh - 20px);
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
  }

  .title-input {
    width: 100%;
  }

  .actions {
    justify-content: flex-start;
  }

  .app-body {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }

  .note-sidebar {
    display: grid;
    grid-template-columns: auto 1fr;
    align-items: stretch;
    gap: 8px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    overflow: hidden;
  }

  .new-note-btn {
    width: auto;
    flex: 0 0 auto;
    padding: 0 12px;
  }

  .note-list {
    display: flex;
    margin-top: 0;
    overflow-x: auto;
    min-width: 0;
    padding-bottom: 2px;
  }

  .note-item {
    width: 132px;
    height: 54px;
    flex: 0 0 auto;
  }

  #editor,
  .preview,
  .focus-layer {
    padding: 18px;
  }
}
