/* ===== LinkMD WYSIWYG editor (TipTap) — refined ===== */

.ed {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  background: var(--gray-100);
}

/* ── Top bar ── */
.ed-topbar {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  height: 54px;
  padding: 0 var(--sp-4);
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  border-bottom: 1px solid var(--gray-200);
  flex-shrink: 0;
  z-index: 5;
}
.ed-back {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--gray-600);
  cursor: pointer;
  flex-shrink: 0;
  transition: background var(--transition-fast), color var(--transition-fast);
}
.ed-back svg { width: 18px; height: 18px; }
.ed-back:hover { background: var(--gray-200); color: var(--gray-900); }

.ed-title-wrap {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  min-width: 0;
  flex: 1;
}
.ed-title {
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--gray-900);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 46vw;
}

/* Save status pill */
.ed-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px 3px 8px;
  border-radius: var(--radius-full);
  font-size: 0.74rem;
  font-weight: 500;
  color: var(--gray-500);
  background: var(--gray-100);
  flex-shrink: 0;
  transition: color var(--transition-fast), background var(--transition-fast);
}
.ed-status-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--gray-400);
  flex-shrink: 0;
  transition: background var(--transition-fast);
}
.ed-status[data-state="saved"]  { color: var(--green); background: var(--green-light); }
.ed-status[data-state="saved"]  .ed-status-dot { background: var(--green); }
.ed-status[data-state="dirty"]  { color: #b45309; background: var(--yellow-light); }
.ed-status[data-state="dirty"]  .ed-status-dot { background: var(--yellow); }
.ed-status[data-state="saving"] { color: var(--brand-600); background: var(--brand-50); }
.ed-status[data-state="saving"] .ed-status-dot { background: var(--brand); animation: ed-pulse 1s ease-in-out infinite; }
.ed-status[data-state="error"]  { color: var(--red); background: var(--red-light); }
.ed-status[data-state="error"]  .ed-status-dot { background: var(--red); }
@keyframes ed-pulse { 0%,100%{opacity:1;transform:scale(1)} 50%{opacity:.4;transform:scale(.7)} }

.ed-actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex-shrink: 0;
}
.ed-count {
  font-size: 0.76rem;
  color: var(--gray-400);
  font-variant-numeric: tabular-nums;
  padding-right: 2px;
}
.ed-ghost {
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--gray-600);
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition-fast);
}
.ed-ghost:hover { background: var(--gray-100); color: var(--gray-900); }
.ed-ghost.active { background: var(--gray-900); color: #fff; border-color: var(--gray-900); }
.ed-save {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  padding: 0 16px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--brand);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(99,102,241,.25);
  transition: background var(--transition-fast), transform var(--transition-fast);
}
.ed-save svg { width: 15px; height: 15px; }
.ed-save:hover { background: var(--brand-600); }
.ed-save:active { transform: translateY(1px); }

/* ── Toolbar ── */
.ed-toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px 12px;
  background: white;
  border-bottom: 1px solid var(--gray-200);
  flex-shrink: 0;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
}
.tb-group { display: inline-flex; align-items: center; gap: 2px; }
.tb-sep {
  width: 1px;
  height: 20px;
  margin: 0 6px;
  background: var(--gray-200);
  flex-shrink: 0;
}
.tb-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 32px;
  height: 32px;
  padding: 0 7px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--gray-700);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  line-height: 1;
  transition: background var(--transition-fast), color var(--transition-fast);
}
.tb-btn .tb-txt { font-family: var(--font-sans); }
.tb-btn[data-ed-action="italic"] .tb-txt { font-style: italic; }
.tb-btn[data-ed-action="underline"] .tb-txt { text-decoration: underline; }
.tb-btn[data-ed-action="strike"] .tb-txt { text-decoration: line-through; }
.tb-btn:hover { background: var(--gray-100); }
.tb-btn.active { background: var(--brand-bg); color: var(--brand-600); }
.tb-btn svg { width: 17px; height: 17px; }
.tb-btn:disabled { opacity: .4; cursor: default; }

/* ── Contextual table toolbar (shown only when caret is in a table) ── */
.ed-table-bar {
  display: none;
  align-items: center;
  gap: 2px;
  padding: 5px 12px;
  background: var(--brand-bg, #eef2ff);
  border-bottom: 1px solid var(--gray-200);
  flex-shrink: 0;
  flex-wrap: wrap;
  animation: tbl-slide-in var(--transition-fast, .12s) ease;
}
.ed-table-bar.show { display: flex; }
.ed-table-bar .tbl-label {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--brand-600, #4f46e5);
  margin-right: 6px;
  text-transform: uppercase;
}
.ed-table-bar .tbl-btn {
  min-width: auto;
  padding: 0 9px;
  font-size: 0.78rem;
}
.ed-table-bar .tbl-btn:hover { background: rgba(99,102,241,.14); color: var(--brand-600, #4f46e5); }
.ed-table-bar .tbl-btn[data-ed-action="table-del"]:hover { background: rgba(239,68,68,.14); color: #dc2626; }
@keyframes tbl-slide-in { from { opacity: 0; transform: translateY(-3px); } to { opacity: 1; transform: none; } }

/* Font select */
.tb-select {
  height: 32px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--gray-700);
  font-size: 0.82rem;
  font-weight: 500;
  padding: 0 4px;
  cursor: pointer;
}
.tb-select:hover { background: var(--gray-100); }

/* Text-color control */
.tb-color { position: relative; display: inline-flex; }
.tb-color-btn { flex-direction: column; gap: 0; min-width: 30px; padding: 0 5px; }
.tb-color-a { font-weight: 800; font-size: 0.82rem; line-height: 1; }
.tb-color-bar { display: block; width: 16px; height: 3px; border-radius: 2px; margin-top: 2px; background: var(--gray-800); }
.tb-color-pop {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 30;
  padding: 10px;
  background: white;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  width: 184px;
}
.tb-color-pop[hidden] { display: none; }
.tb-swatches { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.tb-swatch {
  width: 26px; height: 26px;
  border: 1px solid rgba(0,0,0,.1);
  border-radius: var(--radius-sm);
  cursor: pointer;
  padding: 0;
  transition: transform var(--transition-fast);
}
.tb-swatch:hover { transform: scale(1.12); }
.tb-color-row { display: flex; align-items: center; gap: 8px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--gray-100); }
.tb-color-custom { display: inline-flex; align-items: center; gap: 5px; font-size: 0.76rem; color: var(--gray-600); cursor: pointer; }
.tb-color-custom input[type="color"] { width: 24px; height: 24px; padding: 0; border: 1px solid var(--gray-200); border-radius: var(--radius-sm); background: none; cursor: pointer; }
.tb-color-clear {
  margin-left: auto;
  height: 26px; padding: 0 10px;
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--gray-600);
  font-size: 0.76rem;
  cursor: pointer;
}
.tb-color-clear:hover { background: var(--gray-100); }

/* ── Writing canvas ── */
.ed-canvas {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-8) var(--sp-4) 140px;
  display: flex;
  justify-content: center;
  scroll-behavior: smooth;
}
.ed-canvas .ProseMirror {
  /* A4 page (210 × 297 mm) so on-screen layout matches print/PDF output */
  width: 210mm;
  max-width: 100%;
  min-height: 297mm;
  padding: 20mm 22mm 24mm;
  outline: none;
  background: white;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  font-family: var(--font-sans);
  font-size: 11pt;
  line-height: 1.75;
  color: var(--gray-800);
  letter-spacing: -0.003em;
}
.ed-canvas .ProseMirror > * + * { margin-top: 0.85em; }
.ed-canvas .ProseMirror h1 { font-size: 1.9rem; font-weight: 800; margin-top: 1.3em; line-height: 1.28; letter-spacing: -0.02em; color: var(--gray-900); }
.ed-canvas .ProseMirror h2 { font-size: 1.5rem; font-weight: 700; margin-top: 1.4em; line-height: 1.3; letter-spacing: -0.015em; color: var(--gray-900); padding-bottom: .25em; border-bottom: 1px solid var(--gray-100); }
.ed-canvas .ProseMirror h3 { font-size: 1.22rem; font-weight: 700; margin-top: 1.2em; color: var(--gray-900); }
.ed-canvas .ProseMirror h4 { font-size: 1.05rem; font-weight: 700; margin-top: 1.1em; color: var(--gray-800); }
.ed-canvas .ProseMirror p { margin: 0; }
.ed-canvas .ProseMirror ul,
.ed-canvas .ProseMirror ol { padding-left: 1.5em; }
.ed-canvas .ProseMirror li { margin: 0.25em 0; }
.ed-canvas .ProseMirror li p { margin: 0; }
.ed-canvas .ProseMirror blockquote {
  border-left: 3px solid var(--brand-300);
  padding: 0.1em 0 0.1em 1.1em;
  color: var(--gray-600);
  margin-left: 0;
  font-style: normal;
}
.ed-canvas .ProseMirror code {
  background: var(--gray-100);
  padding: 0.15em 0.4em;
  border-radius: var(--radius-xs);
  font-family: var(--font-mono);
  font-size: 0.86em;
  color: var(--brand-dark);
}
.ed-canvas .ProseMirror pre {
  background: var(--gray-900);
  color: #e6e6e6;
  padding: 16px 18px;
  border-radius: var(--radius-md);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.86rem;
  line-height: 1.6;
}
.ed-canvas .ProseMirror pre code { background: none; padding: 0; color: inherit; font-size: inherit; }
.ed-canvas .ProseMirror a { color: var(--brand-600); text-decoration: underline; text-underline-offset: 2px; }
.ed-canvas .ProseMirror u { text-decoration: underline; text-underline-offset: 2px; }
.ed-canvas .ProseMirror img { max-width: 100%; border-radius: var(--radius-md); box-shadow: var(--shadow-sm); }
.ed-canvas .ProseMirror hr { border: none; border-top: 2px solid var(--gray-100); margin: 1.8em 0; }

/* Tables */
.ed-canvas .ProseMirror table {
  border-collapse: collapse;
  table-layout: fixed;
  width: 100%;
  margin: 1.2em 0;
  overflow: hidden;
  border-radius: var(--radius-sm);
}
.ed-canvas .ProseMirror th,
.ed-canvas .ProseMirror td {
  border: 1px solid var(--gray-300);
  padding: 8px 12px;
  vertical-align: top;
  box-sizing: border-box;
  position: relative;
}
.ed-canvas .ProseMirror th { background: var(--gray-100); font-weight: 700; text-align: left; }
.ed-canvas .ProseMirror .selectedCell:after {
  content: ""; position: absolute; inset: 0;
  background: rgba(99,102,241,.12); pointer-events: none;
}
.ed-canvas .ProseMirror .column-resize-handle {
  position: absolute; right: -2px; top: 0; bottom: 0; width: 4px;
  background: var(--brand); pointer-events: none;
}

/* Placeholder */
.ed-canvas .ProseMirror p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: left;
  color: var(--gray-400);
  pointer-events: none;
  height: 0;
}

/* ── Markdown source view ── */
.ed-source {
  flex: 1;
  overflow-y: auto;
  padding: var(--sp-8) 15%;
  margin: 0;
  background: var(--gray-50);
  font-family: var(--font-mono);
  font-size: 0.86rem;
  line-height: 1.7;
  color: var(--gray-700);
  white-space: pre-wrap;
  word-break: break-word;
}
.ed-source[hidden] { display: none; }

.ed-fail { padding: 60px 20px; text-align: center; color: var(--gray-500); line-height: 1.7; }

/* ── New-document template picker (modal) ── */
.doc-create-modal { width: 460px; max-width: 94vw; }
.doc-create-modal .tpl-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: var(--sp-4) 0 var(--sp-5);
}
.tpl-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  padding: 14px;
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-md);
  background: var(--gray-50);
  cursor: pointer;
  text-align: left;
  transition: border-color var(--transition-fast), background var(--transition-fast), transform var(--transition-fast);
}
.tpl-card:hover { border-color: var(--brand-300); transform: translateY(-1px); }
.tpl-card.active { border-color: var(--brand); background: var(--brand-bg); }
.tpl-ic {
  display: inline-flex;
  color: var(--gray-500);
  margin-bottom: 2px;
}
.tpl-card.active .tpl-ic { color: var(--brand-600); }
.tpl-ic i, .tpl-ic svg { width: 20px; height: 20px; }
.tpl-name { font-weight: 700; font-size: 0.9rem; color: var(--gray-900); }
.tpl-desc { font-size: 0.76rem; color: var(--gray-500); line-height: 1.3; }
.tpl-field-label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--gray-500);
  margin-bottom: 6px;
}

/* Empty-state create action */
.quick-view-zone .qv-actions { margin-top: var(--sp-5); }

@media (prefers-color-scheme: dark) {
  .tpl-card { background: var(--gray-800); border-color: var(--gray-700); }
  .tpl-card.active { background: rgba(99,102,241,.12); border-color: var(--brand); }
  .tpl-name { color: #fafafa; }
}
@media (max-width: 480px) {
  .doc-create-modal .tpl-grid { grid-template-columns: 1fr; }
}

/* ── Floating save button (mobile) ── */
.editor-fab {
  position: fixed;
  right: 20px;
  bottom: 24px;
  width: 52px; height: 52px;
  border: none;
  border-radius: var(--radius-full);
  background: var(--brand);
  color: #fff;
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
}

/* ── Dark mode ── */
@media (prefers-color-scheme: dark) {
  .ed { background: #0f0f14; }
  .ed-topbar { background: rgba(24,24,27,.72); border-bottom-color: var(--gray-800); }
  .ed-title, .ed-back:hover { color: #f4f4f5; }
  .ed-back:hover { background: var(--gray-800); }
  .ed-status { background: var(--gray-800); }
  .ed-ghost { border-color: var(--gray-700); color: var(--gray-300); }
  .ed-ghost:hover { background: var(--gray-800); color: #fff; }
  .ed-toolbar { background: var(--gray-900); border-bottom-color: var(--gray-800); }
  .ed-table-bar { background: rgba(99,102,241,.10); border-bottom-color: var(--gray-800); }
  .ed-table-bar .tbl-label { color: #a5b4fc; }
  .ed-table-bar .tbl-btn:hover { background: rgba(99,102,241,.22); color: #c7d2fe; }
  .tb-btn { color: var(--gray-300); }
  .tb-btn:hover { background: var(--gray-800); }
  .tb-sep { background: var(--gray-800); }
  .tb-select { border-color: var(--gray-700); color: var(--gray-300); background: transparent; }
  .tb-select:hover { background: var(--gray-800); }
  .tb-select option { background: var(--gray-900); }
  .tb-color-pop { background: var(--gray-800); border-color: var(--gray-700); }
  .tb-color-row { border-top-color: var(--gray-700); }
  .tb-color-clear { border-color: var(--gray-700); color: var(--gray-300); }
  .tb-color-clear:hover { background: var(--gray-900); }
  .ed-canvas .ProseMirror { background: var(--gray-900); box-shadow: none; color: var(--gray-200); }
  .ed-canvas .ProseMirror h1,
  .ed-canvas .ProseMirror h2,
  .ed-canvas .ProseMirror h3,
  .ed-canvas .ProseMirror h4 { color: #fafafa; }
  .ed-canvas .ProseMirror h2 { border-bottom-color: var(--gray-800); }
  .ed-canvas .ProseMirror code { background: var(--gray-800); color: var(--brand-300); }
  .ed-canvas .ProseMirror th { background: var(--gray-800); }
  .ed-canvas .ProseMirror th, .ed-canvas .ProseMirror td { border-color: var(--gray-700); }
  .ed-source { background: #0f0f14; color: var(--gray-400); }
}

/* ── Responsive ── */
@media (max-width: 768px) {
  .ed-topbar { height: 50px; padding: 0 var(--sp-2); gap: var(--sp-2); }
  .ed-title { max-width: 34vw; font-size: 0.88rem; }
  .ed-count { display: none; }
  .ed-ghost { display: none; }
  .ed-status-text { display: none; }
  .ed-status { padding: 6px; }
  .ed-toolbar { padding: 5px 8px; gap: 1px; overflow-x: auto; flex-wrap: nowrap; }
  .ed-canvas { padding: var(--sp-3) 0 140px; }
  .ed-canvas .ProseMirror { padding: 28px 22px 60px; border-radius: 0; box-shadow: none; font-size: 1rem; }
  .ed-source { padding: var(--sp-4); }
}
