/* ==================== CSS VARIABLES & THEMES ==================== */
:root {
  --bg-page: #f7f8fa;
  --bg-surface: #ffffff;
  --bg-subtle: #f1f3f6;
  --bg-hover: #eaedf2;
  --bg-editor: #ffffff;
  --bg-preview: #ffffff;
  --bg-code: #f4f6f8;
  --bg-kbd: #f1f3f5;

  --text-primary: #171b24;
  --text-secondary: #525d70;
  --text-muted: #8490a3;

  --border-color: #e4e7ee;
  --border-strong: #cfd6e3;

  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: rgba(37, 99, 235, 0.1);
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --danger-soft: rgba(220, 38, 38, 0.1);
  --success: #16a34a;
  --warning: #d97706;

  --shadow-xs: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-sm: 0 2px 8px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 10px 28px rgba(15, 23, 42, 0.12);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --font-mono: "JetBrains Mono", "Cascadia Code", "Fira Code", Consolas, "Courier New", monospace;
}

[data-theme="dark"] {
  --bg-page: #0f1218;
  --bg-surface: #161b24;
  --bg-subtle: #1d2430;
  --bg-hover: #252e3d;
  --bg-editor: #12161f;
  --bg-preview: #151a23;
  --bg-code: #1d2431;
  --bg-kbd: #232b38;

  --text-primary: #e8edf5;
  --text-secondary: #a2aec0;
  --text-muted: #6c788c;

  --border-color: #252e3e;
  --border-strong: #354156;

  --accent: #3b82f6;
  --accent-hover: #60a5fa;
  --accent-soft: rgba(59, 130, 246, 0.16);
  --danger: #ef4444;
  --danger-hover: #f87171;
  --danger-soft: rgba(239, 68, 68, 0.16);
  --success: #22c55e;
  --warning: #f59e0b;

  --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.25);
  --shadow-sm: 0 4px 12px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 14px 36px rgba(0, 0, 0, 0.5);
}

/* ==================== RESET & BASE ==================== */
*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font-sans);
  background: var(--bg-page);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  line-height: 1.5;
}

button, input, textarea {
  font-family: inherit;
  color: inherit;
}

a {
  color: var(--accent);
  text-decoration: none;
}

.hidden {
  display: none !important;
}

/* Theme Icon Switching */
.icon-sun { display: none; }
.icon-moon { display: block; }
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: none; }

/* ==================== BUTTONS ==================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.1s;
  white-space: nowrap;
}

.btn:active {
  transform: translateY(0.5px);
}

.btn-sm {
  padding: 5px 10px;
  font-size: 13px;
}

.btn-xs {
  padding: 3px 9px;
  font-size: 12px;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--bg-subtle);
  border-color: var(--border-color);
  color: var(--text-primary);
}
.btn-secondary:hover {
  background: var(--bg-hover);
  border-color: var(--border-strong);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}
.btn-ghost:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover {
  background: var(--danger-hover);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.icon-btn:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
}

/* ==================== HOME VIEW ==================== */
.home-header {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
  backdrop-filter: blur(8px);
}

.home-header-inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text-primary);
  font-weight: 700;
  font-size: 17px;
  letter-spacing: -0.02em;
  flex-shrink: 0;
}

.brand-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.28);
}

.search-box {
  position: relative;
  flex: 1;
  max-width: 440px;
}

.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

.search-box input {
  width: 100%;
  height: 38px;
  padding: 0 34px 0 36px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  background: var(--bg-subtle);
  font-size: 14px;
  outline: none;
  transition: background 0.15s, border-color 0.15s, box-shadow 0.15s;
}

.search-box input:focus {
  background: var(--bg-surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.search-clear {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 50%;
  background: var(--bg-hover);
  color: var(--text-secondary);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
}

.home-header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.home-main {
  max-width: 1180px;
  margin: 0 auto;
  padding: 32px 24px 64px;
}

.home-hero {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 24px;
}

.home-title {
  margin: 0;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.home-subtitle {
  margin: 6px 0 0;
  color: var(--text-secondary);
  font-size: 14px;
}

.home-subtitle code {
  background: var(--bg-subtle);
  border: 1px solid var(--border-color);
  padding: 1px 6px;
  border-radius: 5px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-primary);
}

.doc-count-badge {
  padding: 5px 12px;
  border-radius: 999px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
}

/* Document Cards Grid */
.doc-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(310px, 1fr));
  gap: 18px;
}

.doc-card {
  position: relative;
  display: flex;
  flex-direction: column;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 20px;
  cursor: pointer;
  box-shadow: var(--shadow-xs);
  transition: transform 0.16s ease, box-shadow 0.16s ease, border-color 0.16s ease;
  min-height: 168px;
}

.doc-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
  box-shadow: var(--shadow-sm);
}

.doc-card-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.doc-card-title-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.doc-card-icon {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.doc-card-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc-card-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
  opacity: 0.72;
  transition: opacity 0.15s;
}

.doc-card:hover .doc-card-actions {
  opacity: 1;
}

.card-action-btn {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.card-action-btn:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
}

.card-action-btn.delete-btn:hover {
  background: var(--danger-soft);
  color: var(--danger);
}

.doc-card-excerpt {
  margin: 0 0 16px;
  font-size: 13.5px;
  color: var(--text-secondary);
  line-height: 1.6;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  flex: 1;
  word-break: break-word;
}

.doc-card-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-muted);
  padding-top: 12px;
  border-top: 1px solid var(--border-color);
}

/* Empty State */
.empty-state {
  text-align: center;
  padding: 72px 24px;
  background: var(--bg-surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-lg);
}

.empty-icon {
  color: var(--text-muted);
  margin-bottom: 12px;
}

.empty-state h3 {
  margin: 0 0 6px;
  font-size: 18px;
}

.empty-state p {
  margin: 0 0 20px;
  color: var(--text-secondary);
  font-size: 14px;
}

/* ==================== EDITOR VIEW ==================== */
.editor-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  background: var(--bg-surface);
}

.editor-header {
  height: 54px;
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.editor-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.header-divider {
  width: 1px;
  height: 20px;
  background: var(--border-color);
  flex-shrink: 0;
}

.doc-title-input {
  min-width: 120px;
  max-width: 380px;
  flex: 1;
  padding: 5px 10px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  outline: none;
  transition: background 0.15s, border-color 0.15s;
}

.doc-title-input:hover {
  background: var(--bg-subtle);
}

.doc-title-input:focus {
  background: var(--bg-surface);
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.save-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--bg-subtle);
  white-space: nowrap;
  flex-shrink: 0;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--success);
}

.save-status.saving .status-dot {
  background: var(--warning);
  animation: pulse 1s infinite;
}

.save-status.unsaved .status-dot {
  background: var(--warning);
}

.save-status.error .status-dot {
  background: var(--danger);
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

.editor-header-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.segmented-control {
  display: inline-flex;
  padding: 3px;
  border-radius: var(--radius-sm);
  background: var(--bg-subtle);
  border: 1px solid var(--border-color);
}

.seg-btn {
  padding: 4px 10px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.seg-btn:hover {
  color: var(--text-primary);
}

.seg-btn.active {
  background: var(--bg-surface);
  color: var(--text-primary);
  box-shadow: var(--shadow-xs);
}

/* Remote Update Banner */
.remote-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  background: var(--accent-soft);
  border-bottom: 1px solid var(--accent);
  font-size: 13px;
  color: var(--text-primary);
  flex-shrink: 0;
}

.remote-banner-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Formatting Toolbar */
.editor-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 14px;
  background: var(--bg-page);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
  overflow-x: auto;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 3px;
  flex-shrink: 0;
}

.tb-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 28px;
  padding: 0 8px;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12.5px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s, color 0.12s;
}

.tb-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.tb-btn-accent {
  color: var(--accent);
  background: var(--accent-soft);
  font-weight: 500;
}

.tb-btn-accent:hover {
  background: var(--accent);
  color: #fff;
}

.tb-sep {
  width: 1px;
  height: 16px;
  background: var(--border-color);
  margin: 0 4px;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-shrink: 0;
  font-size: 12px;
  color: var(--text-muted);
}

.paste-hint {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--text-secondary);
}

kbd {
  display: inline-block;
  padding: 1px 5px;
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1.3;
  color: var(--text-primary);
  background: var(--bg-kbd);
  border: 1px solid var(--border-strong);
  border-radius: 4px;
}

.sync-scroll-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  user-select: none;
}

.sync-scroll-toggle input {
  accent-color: var(--accent);
  cursor: pointer;
}

.editor-stats {
  font-variant-numeric: tabular-nums;
  min-width: 44px;
  text-align: right;
}

/* ==================== SPLIT WORKSPACE ==================== */
.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
  overflow: hidden;
}

.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
}

.editor-pane {
  flex: 0 0 50%;
  background: var(--bg-editor);
}

.preview-pane {
  flex: 1 1 50%;
  background: var(--bg-preview);
}

/* View Modes */
.workspace.mode-editor .editor-pane {
  flex: 1 1 100% !important;
}
.workspace.mode-editor .pane-resizer,
.workspace.mode-editor .preview-pane {
  display: none;
}

.workspace.mode-preview .preview-pane {
  flex: 1 1 100% !important;
}
.workspace.mode-preview .pane-resizer,
.workspace.mode-preview .editor-pane {
  display: none;
}

.workspace.mode-preview .markdown-body {
  max-width: 860px;
  margin: 0 auto;
}

.pane-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 18px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
  user-select: none;
}

.pane-label-sub {
  font-weight: 400;
  letter-spacing: 0;
}

/* Resizer Divider */
.pane-resizer {
  width: 6px;
  flex-shrink: 0;
  background: var(--border-color);
  cursor: col-resize;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  z-index: 5;
}

.pane-resizer:hover,
.pane-resizer.dragging {
  background: var(--accent);
}

.resizer-grip {
  width: 2px;
  height: 36px;
  border-radius: 2px;
  background: var(--text-muted);
  opacity: 0.55;
}

.pane-resizer:hover .resizer-grip,
.pane-resizer.dragging .resizer-grip {
  background: #fff;
  opacity: 0.9;
}

/* Editor Textarea & Dropzone */
.editor-textarea-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
}

.md-editor {
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  resize: none;
  padding: 22px 24px 120px;
  background: transparent;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 14.5px;
  line-height: 1.75;
  tab-size: 4;
  overflow-y: auto;
}

.md-editor::placeholder {
  color: var(--text-muted);
}

.drop-overlay {
  position: absolute;
  inset: 12px;
  border-radius: var(--radius-lg);
  background: var(--accent-soft);
  border: 2px dashed var(--accent);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 10;
}

.drop-overlay-card {
  text-align: center;
  color: var(--accent);
  font-weight: 600;
  font-size: 15px;
}

.drop-overlay-card p {
  margin: 8px 0 0;
}

/* ==================== PREVIEW PANE & MARKDOWN BODY ==================== */
.preview-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 24px 32px 120px;
}

.markdown-body {
  color: var(--text-primary);
  font-size: 15.5px;
  line-height: 1.78;
  word-wrap: break-word;
}

.markdown-body > *:first-child {
  margin-top: 0;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  margin: 1.4em 0 0.65em;
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: -0.015em;
}

.markdown-body h1 {
  font-size: 1.9em;
  padding-bottom: 0.3em;
  border-bottom: 1px solid var(--border-color);
}

.markdown-body h2 {
  font-size: 1.48em;
  padding-bottom: 0.25em;
  border-bottom: 1px solid var(--border-color);
}

.markdown-body h3 { font-size: 1.24em; }
.markdown-body h4 { font-size: 1.06em; }

.markdown-body p {
  margin: 0 0 1em;
}

.markdown-body a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.markdown-body ul,
.markdown-body ol {
  margin: 0 0 1em;
  padding-left: 1.8em;
}

.markdown-body li {
  margin: 0.28em 0;
}

.markdown-body li > input[type="checkbox"] {
  margin-right: 7px;
  accent-color: var(--accent);
  transform: scale(1.1);
  cursor: pointer;
}

.markdown-body blockquote {
  margin: 1em 0;
  padding: 10px 16px;
  border-left: 4px solid var(--accent);
  background: var(--bg-subtle);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-secondary);
}

.markdown-body blockquote > *:last-child {
  margin-bottom: 0;
}

.markdown-body code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  padding: 2px 6px;
  border-radius: 5px;
  background: var(--bg-code);
  border: 1px solid var(--border-color);
}

.markdown-body pre {
  position: relative;
  margin: 1.1em 0;
  padding: 16px 18px;
  border-radius: var(--radius-md);
  background: var(--bg-code);
  border: 1px solid var(--border-color);
  overflow-x: auto;
  line-height: 1.6;
}

.markdown-body pre code {
  padding: 0;
  border: none;
  background: transparent;
  font-size: 13.5px;
}

.code-copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 3px 8px;
  font-size: 11.5px;
  border-radius: 5px;
  border: 1px solid var(--border-strong);
  background: var(--bg-surface);
  color: var(--text-secondary);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s, color 0.15s;
}

.markdown-body pre:hover .code-copy-btn {
  opacity: 1;
}

.code-copy-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.markdown-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.2em 0;
  font-size: 14.5px;
  display: block;
  overflow-x: auto;
}

.markdown-body th,
.markdown-body td {
  padding: 9px 14px;
  border: 1px solid var(--border-color);
  text-align: left;
}

.markdown-body th {
  background: var(--bg-subtle);
  font-weight: 600;
}

.markdown-body tr:nth-child(even) td {
  background: rgba(128, 128, 128, 0.03);
}

.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border-color);
  margin: 1.8em 0;
}

.markdown-body img {
  max-width: 100%;
  height: auto;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow-xs);
  margin: 8px 0;
  cursor: zoom-in;
  display: block;
}

/* Mermaid Diagrams */
.mermaid-block {
  position: relative;
  margin: 1.2em 0;
  padding: 20px 16px;
  border-radius: var(--radius-md);
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  overflow-x: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 64px;
}

.mermaid-block svg {
  max-width: 100%;
  height: auto;
  display: block;
  cursor: zoom-in;
}

.mermaid-actions {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 6px;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 2;
}

.mermaid-block:hover .mermaid-actions {
  opacity: 1;
}

.mermaid-action-btn {
  padding: 3px 8px;
  font-size: 11.5px;
  border-radius: 5px;
  border: 1px solid var(--border-strong);
  background: var(--bg-surface);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.mermaid-action-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.mermaid-loading {
  font-size: 13px;
  color: var(--text-muted);
}

.mermaid-error {
  width: 100%;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  border: 1px solid var(--danger);
  color: var(--danger);
  font-size: 13px;
}

.mermaid-error-title {
  font-weight: 600;
  margin-bottom: 6px;
}

.mermaid-error pre {
  margin: 8px 0 0;
  padding: 10px 12px;
  font-size: 12.5px;
  color: var(--text-primary);
  background: var(--bg-code);
}

.preview-empty-placeholder {
  color: var(--text-muted);
  font-style: italic;
  padding-top: 12px;
}

/* ==================== MODALS ==================== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(10, 14, 23, 0.52);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-card {
  width: 100%;
  max-width: 440px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  animation: modalIn 0.16s ease-out;
}

.modal-card-sm {
  max-width: 390px;
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

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

.modal-header h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.modal-close {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 5px;
}

.modal-close:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
}

.modal-body {
  padding: 20px;
}

.form-label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.form-input {
  width: 100%;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--bg-page);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.form-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.confirm-text {
  margin: 0;
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.6;
}

.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 20px;
  background: var(--bg-page);
  border-top: 1px solid var(--border-color);
}

/* ==================== IMAGE LIGHTBOX ==================== */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(8, 11, 18, 0.88);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px;
  cursor: zoom-out;
}

.lightbox img {
  max-width: 92vw;
  max-height: 90vh;
  border-radius: var(--radius-md);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
  cursor: default;
}

.lightbox-svg {
  max-width: 94vw;
  max-height: 90vh;
  overflow: auto;
  background: var(--bg-surface);
  border-radius: var(--radius-md);
  padding: 28px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
  cursor: default;
}

.lightbox-svg svg {
  display: block;
  min-width: min(800px, 84vw);
  max-width: none;
  height: auto;
}

.lightbox-close {
  position: fixed;
  top: 20px;
  right: 24px;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}

.lightbox-close:hover {
  background: rgba(255, 255, 255, 0.28);
}

/* ==================== TOASTS ==================== */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  padding: 10px 16px;
  border-radius: var(--radius-md);
  background: var(--text-primary);
  color: var(--bg-surface);
  font-size: 13.5px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  animation: toastIn 0.2s ease-out;
  max-width: 340px;
}

.toast.toast-error {
  background: var(--danger);
  color: #fff;
}

.toast.toast-success {
  background: #15803d;
  color: #fff;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ==================== SCROLLBARS ==================== */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ==================== RESPONSIVE ==================== */
@media (max-width: 860px) {
  .hide-md { display: none; }
  .paste-hint { display: none; }
  .home-hero { flex-direction: column; align-items: flex-start; }
  .workspace.mode-split {
    flex-direction: column;
  }
  .workspace.mode-split .editor-pane {
    flex: 0 0 50% !important;
  }
  .workspace.mode-split .pane-resizer {
    display: none;
  }
  .workspace.mode-split .preview-pane {
    border-top: 1px solid var(--border-color);
  }
}

@media (max-width: 600px) {
  .hide-sm { display: none; }
  .home-header-inner {
    flex-wrap: wrap;
    padding: 10px 14px;
  }
  .search-box {
    order: 3;
    max-width: none;
    flex-basis: 100%;
  }
  .home-main {
    padding: 20px 14px 48px;
  }
  .preview-scroll {
    padding: 18px 16px 80px;
  }
  .md-editor {
    padding: 16px;
  }
}

/* ==================== PRINT / EXPORT SINGLE-PAGE PDF ==================== */
.print-measure-host {
  position: fixed;
  left: -99999px;
  top: 0;
  width: 182mm;
  visibility: hidden;
  pointer-events: none;
  z-index: -1;
}

.print-measure-host .markdown-body {
  font-size: 13.5px;
  line-height: 1.7;
  max-width: none;
}

.print-measure-host .markdown-body pre {
  white-space: pre-wrap;
  word-break: break-word;
  overflow: visible;
}

.print-measure-host .markdown-body table {
  display: table;
  width: 100%;
  overflow: visible;
}

.print-measure-host .markdown-body .mermaid-block {
  padding: 12px;
  overflow: visible;
}

.print-measure-host .markdown-body .mermaid-block svg,
.print-measure-host .markdown-body img {
  max-width: 100%;
  height: auto;
}

@page {
  margin: 14mm;
}

@media print {
  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }

  html, body {
    height: auto !important;
    overflow: visible !important;
    background: #ffffff !important;
    color: #171b24 !important;
  }

  #home-view,
  .editor-header,
  .editor-toolbar,
  .remote-banner,
  .editor-pane,
  .pane-resizer,
  .pane-label,
  .code-copy-btn,
  .mermaid-actions,
  .modal-backdrop,
  .lightbox,
  .toast-container,
  .print-measure-host {
    display: none !important;
  }

  #editor-view,
  .editor-shell,
  .workspace,
  .preview-pane,
  .preview-scroll {
    display: block !important;
    position: static !important;
    width: 100% !important;
    height: auto !important;
    min-height: 0 !important;
    max-width: none !important;
    overflow: visible !important;
    background: #ffffff !important;
    border: none !important;
    margin: 0 !important;
    padding: 0 !important;
    flex: none !important;
  }

  .markdown-body {
    max-width: none !important;
    font-size: 13.5px;
    line-height: 1.7;
    color: #171b24 !important;
  }

  .markdown-body pre {
    white-space: pre-wrap;
    word-break: break-word;
    overflow: visible !important;
  }

  .markdown-body table {
    display: table !important;
    width: 100% !important;
    overflow: visible !important;
  }

  .markdown-body .mermaid-block {
    border: 1px solid #e4e7ee;
    background: #ffffff !important;
    padding: 12px;
    overflow: visible !important;
  }

  .markdown-body .mermaid-block svg {
    max-width: 100% !important;
    height: auto !important;
  }

  .markdown-body img {
    max-width: 100% !important;
    height: auto !important;
    box-shadow: none !important;
  }

  .markdown-body a {
    color: #2563eb !important;
  }
}
