:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #dfe3e8;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #e8effd;
  --shadow: 0 1px 3px rgba(0, 0, 0, .08);

  /* ajustes editables desde ⚙️ Ajustes */
  --editor-font: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  --editor-size: 14.5px;
  --preview-size: 15.5px;
  --preview-max: 780px;
}

[data-theme="dark"] {
  --bg: #0f1420;
  --panel: #151b29;
  --border: #263041;
  --text: #e6e9ef;
  --muted: #98a2b3;
  --accent: #60a5fa;
  --accent-soft: #1b2740;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* ---------- barras ---------- */
.bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.bar.bottom {
  border-bottom: none;
  border-top: 1px solid var(--border);
  justify-content: space-between;
  font-size: 12px;
  color: var(--muted);
}

.brand {
  font-size: 15px;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  display: block;
  box-shadow: var(--shadow);
}

.file-name {
  flex: 1;
  min-width: 120px;
  max-width: 280px;
  padding: 6px 10px;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}
.file-name:focus { border-color: var(--accent); }

.actions { display: flex; gap: 6px; flex-wrap: wrap; margin-left: auto; }

button {
  padding: 7px 12px;
  font-size: 13px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
}
button:hover { background: var(--accent-soft); border-color: var(--accent); }
button:active { transform: translateY(1px); }

.hint {
  opacity: .85;
  text-align: right;
  line-height: 1.45;
}

/* ---------- división en dos ---------- */
.split {
  flex: 1;
  display: flex;
  min-height: 0;
}

.pane {
  flex: 1 1 50%;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--panel);
}

.editor-pane { border-right: 1px solid var(--border); }

.pane-title {
  padding: 8px 14px;
  font-size: 12px;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#editor {
  flex: 1;
  width: 100%;
  padding: 16px 18px;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font: 400 var(--editor-size)/1.65 var(--editor-font);
  tab-size: 2;
}

.preview-pane { overflow: hidden; }

.markdown-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px 26px 60px;
  line-height: 1.7;
  font-size: var(--preview-size);
  max-width: var(--preview-max);
}

/* ---------- estilos del contenido renderizado ---------- */
.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 { line-height: 1.25; margin: 1.3em 0 .5em; }
.markdown-body h1 { font-size: 1.9em; border-bottom: 1px solid var(--border); padding-bottom: .25em; }
.markdown-body h2 { font-size: 1.5em; border-bottom: 1px solid var(--border); padding-bottom: .2em; }
.markdown-body h3 { font-size: 1.2em; }

.markdown-body p { margin: .7em 0; }
.markdown-body a { color: var(--accent); }
.markdown-body ul, .markdown-body ol { padding-left: 1.5em; }
.markdown-body li { margin: .25em 0; }

.markdown-body blockquote {
  margin: .9em 0;
  padding: .2em 1em;
  color: var(--muted);
  border-left: 4px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 6px 6px 0;
}

.markdown-body code {
  padding: .15em .4em;
  background: var(--accent-soft);
  border-radius: 5px;
  font: .9em "SFMono-Regular", Consolas, Menlo, monospace;
}

.markdown-body pre {
  padding: 14px 16px;
  overflow-x: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.markdown-body pre code { padding: 0; background: none; }

.markdown-body table {
  border-collapse: collapse;
  margin: 1em 0;
  width: 100%;
}
.markdown-body th, .markdown-body td {
  border: 1px solid var(--border);
  padding: 6px 12px;
  text-align: left;
}
.markdown-body th { background: var(--accent-soft); }

.markdown-body img { max-width: 100%; border-radius: 8px; }
.markdown-body hr { border: none; border-top: 1px solid var(--border); margin: 1.5em 0; }

.markdown-body input[type="checkbox"] { margin-right: .5em; }

/* ---------- modos de vista (editor | dividido | vista) ---------- */
.modos {
  display: flex;
  gap: 4px;
  margin-left: auto;
  padding-left: 6px;
}
.modos button { padding: 5px 8px; font-size: 13px; border-radius: 6px; }
.modos button.on {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.split.modo-editor .preview-pane,
.split.modo-editor .outline { display: none; }
.split.modo-preview .editor-pane { display: none; }

/* ---------- buscar y reemplazar ---------- */
.buscar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  padding: 7px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.buscar[hidden] { display: none; }

.buscar input[type="text"] {
  padding: 6px 10px;
  font-size: 13.5px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 7px;
  outline: none;
  min-width: 150px;
}
.buscar input[type="text"]:focus { border-color: var(--accent); }
#reemplazarTxt { min-width: 180px; }

.buscar-count {
  font-size: 12px;
  color: var(--muted);
  min-width: 48px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

#btnCase.on {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

/* ---------- subtítulo de menús ---------- */
.pop-subtitle {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  padding: 4px 9px 3px;
}

/* ---------- panel de ajustes ---------- */
.modal-ajustes { width: min(470px, 100%); }

.ajustes-body {
  padding: 16px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
}

.ajuste {
  display: flex;
  flex-direction: column;
  gap: 7px;
  font-size: 13.5px;
}
.ajuste > span {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
}
.ajuste b {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.ajuste select {
  padding: 8px 10px;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  outline: none;
}
.ajuste select:focus { border-color: var(--accent); }
.ajuste input[type="range"] { width: 100%; accent-color: var(--accent); }

.ajuste-botones {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  padding-top: 4px;
  border-top: 1px dashed var(--border);
}
.ajuste-botones .modal-note { padding: 0; }

/* ---------- zona de soltar archivos ---------- */
.drop-zone {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 20, 32, .55);
  backdrop-filter: blur(3px);
  pointer-events: none;
}
.drop-zone[hidden] { display: none; }

.drop-card {
  padding: 26px 34px;
  font-size: 17px;
  color: var(--text);
  background: var(--panel);
  border: 2px dashed var(--accent);
  border-radius: 16px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .35);
}

/* ---------- accesibilidad ---------- */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
#editor:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------- barra de formato ---------- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: thin;
}

.toolbar button {
  padding: 5px 9px;
  font-size: 13px;
  border-radius: 6px;
  white-space: nowrap;
}

.toolbar .sep {
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 5px;
  flex: 0 0 auto;
}

button:disabled {
  opacity: .42;
  cursor: not-allowed;
  background: var(--panel);
  border-color: var(--border);
}
button:disabled:hover { background: var(--panel); border-color: var(--border); }

/* ---------- botones pequeños ---------- */
.mini {
  padding: 4px 9px !important;
  font-size: 12px !important;
  border-radius: 6px !important;
}
.mini.on {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

/* ---------- indicador de guardado ---------- */
.save-info { font-weight: 600; }
.save-info.dirty { color: #e11d48; }
.save-info.ok { color: #16a34a; }

/* crédito discreto al final de la página */
.credito {
  font-size: 11px;
  color: var(--muted);
  opacity: .8;
  white-space: nowrap;
  letter-spacing: .01em;
}
.credito a {
  color: inherit;
  text-decoration: underline dotted;
  text-underline-offset: 2px;
}
.credito a:hover { color: var(--accent); opacity: 1; }

/* ---------- esquema de títulos ---------- */
.outline {
  flex: 0 0 210px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow: hidden;
}
.outline.oculto { display: none; }

.outline-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.outline-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 6px 8px;
  font-size: 13px;
  line-height: 1.35;
  color: var(--text);
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.outline-item:hover { background: var(--accent-soft); border-color: var(--border); }
.outline-item.activo {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}
.outline-item.nivel-2 { padding-left: 20px; color: var(--muted); }
.outline-item.nivel-3 { padding-left: 32px; color: var(--muted); font-size: 12.5px; }
.outline-item.nivel-4 { padding-left: 44px; color: var(--muted); font-size: 12px; }

.outline-empty, .docs-empty {
  font-size: 12.5px;
  color: var(--muted);
  line-height: 1.5;
  padding: 6px 4px;
  margin: 0;
}
.outline-empty code {
  background: var(--accent-soft);
  padding: 1px 5px;
  border-radius: 4px;
}

/* ---------- modal: mis documentos ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(15, 20, 32, .55);
  backdrop-filter: blur(2px);
}
.modal[hidden] { display: none; }

.modal-card {
  width: min(560px, 100%);
  max-height: min(80vh, 640px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .35);
  overflow: hidden;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 17px; }

.modal-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 18px 4px;
  flex-wrap: wrap;
}

.modal-note {
  margin: 0;
  padding: 8px 18px 14px;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}
.modal-note code {
  background: var(--accent-soft);
  padding: 1px 5px;
  border-radius: 4px;
}

.docs-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.doc-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  transition: border-color .15s, background .15s;
}
.doc-item:hover { border-color: var(--accent); }
.doc-item.activo { border-color: var(--accent); background: var(--accent-soft); }

.doc-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  cursor: pointer;
}
.doc-name {
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.doc-meta { font-size: 11.5px; color: var(--muted); }

.doc-tools { display: flex; gap: 4px; }

/* ---------- selector de tamaño de tabla (estilo Word) ---------- */
.table-pop {
  position: fixed;
  z-index: 60;
  padding: 11px 13px 9px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 14px 34px rgba(0, 0, 0, .28);
}
.table-pop[hidden] { display: none; }

.pop-title {
  font-size: 11.5px;
  color: var(--muted);
  margin-bottom: 9px;
}

.table-grid {
  display: grid;
  gap: 4px;
}

.table-grid .cell {
  width: 20px;
  height: 20px;
  padding: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}
.table-grid .cell:hover { border-color: var(--accent); }
.table-grid .cell.on {
  background: var(--accent);
  border-color: var(--accent);
}

.pop-size {
  margin-top: 9px;
  text-align: center;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
}

/* ---------- menú desplegable (exportar / compartir) ---------- */
.menu-pop {
  position: fixed;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 232px;
  padding: 7px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 14px 34px rgba(0, 0, 0, .28);
}
.menu-pop[hidden] { display: none; }

.menu-pop button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 11px;
  font-size: 13.5px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 7px;
}
.menu-pop button:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
}

/* ---------- guía rápida de Markdown ---------- */
.guide-body {
  overflow-y: auto;
  padding: 14px 20px 22px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.guide-group h3 {
  margin: 0 0 7px;
  font-size: 12px;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--muted);
}

.guide-row {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 6px 0;
  border-bottom: 1px dashed var(--border);
  font-size: 14px;
}
.guide-row:last-child { border-bottom: none; }

.guide-row > code {
  flex: 0 0 46%;
  padding: 4px 8px;
  background: var(--accent-soft);
  border-radius: 6px;
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
}

.guide-row > .vista { flex: 1; min-width: 0; }
.guide-row > span { flex: 1; min-width: 0; }
.vista-h1 { font-size: 1.35em; font-weight: 700; }
.vista-h2 { font-size: 1.2em; font-weight: 700; }
.vista-h3 { font-size: 1.05em; font-weight: 700; }
.vista code { padding: 1px 5px; background: var(--accent-soft); border-radius: 5px; font-size: .88em; }
.vista a { color: var(--accent); }
.vista hr { border: none; border-top: 1px solid var(--border); margin: .4em 0; }
.vista .cita {
  display: inline-block;
  padding: 1px 10px;
  color: var(--muted);
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
}
.fake-check { margin-right: 4px; }

.mini-tabla { border-collapse: collapse; font-size: 13px; }
.mini-tabla th, .mini-tabla td { border: 1px solid var(--border); padding: 3px 10px; }
.mini-tabla th { background: var(--accent-soft); }

.guide-nota {
  margin: 8px 0 0;
  font-size: 12.5px;
  color: var(--muted);
  line-height: 1.5;
}

/* ---------- responsive: apilado en móvil ---------- */
@media (max-width: 760px) {
  .split { flex-direction: column; }
  .editor-pane { border-right: none; border-bottom: 1px solid var(--border); }
  .outline { display: none; }
  .hint { display: none; }
  .brand { font-size: 0; gap: 0; }
  .brand .logo { width: 24px; height: 24px; }
}

/* ---------- impresión / PDF ---------- */
@media print {
  .bar, .toolbar, .buscar, .editor-pane, .outline, .modal, .mini, .table-pop, .menu-pop, .drop-zone { display: none !important; }
  .split, .preview-pane, .markdown-body { display: block; overflow: visible; height: auto; }
  .markdown-body { padding: 0; }
  body { background: #fff; color: #000; }
}

/* ---------- modal personalizado ---------- */
.dialog-card {
  max-width: 400px;
}
.dialog-body {
  padding: 16px 20px;
}
.dialog-input {
  width: 100%;
  padding: 8px;
  margin-top: 12px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
}
.btn-aceptar {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  font-weight: 500;
}
.btn-cancelar {
  background: transparent;
  color: var(--muted);
  border-color: transparent;
}
.btn-cancelar:hover {
  background: var(--bg);
  color: var(--text);
}
