/* ===== Variables y tema ===== */
:root {
  --bg: #ffffff;
  --bg-soft: #f6f8fa;
  --bg-hover: #eaeef2;
  --fg: #1f2328;
  --fg-muted: #59636e;
  --border: #d1d9e0;
  --accent: #0969da;
  --accent-fg: #ffffff;
  --danger: #cf222e;
  --success-bg: #dafbe1;
  --warning-bg: #fff8c5;
  --error-bg: #ffebe9;
  --code-bg: #f6f8fa;
  --add-bg: #dafbe1;
  --del-bg: #ffebe9;
  --topbar-h: 52px;
  --sidebar-w: 280px;
  --radius: 6px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "Cascadia Code", Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #0d1117;
  --bg-soft: #151b23;
  --bg-hover: #212830;
  --fg: #e6edf3;
  --fg-muted: #9198a1;
  --border: #3d444d;
  --accent: #4493f8;
  --accent-fg: #0d1117;
  --danger: #f85149;
  --success-bg: #12261e;
  --warning-bg: #272115;
  --error-bg: #25171c;
  --code-bg: #151b23;
  --add-bg: #12261e;
  --del-bg: #2d1417;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); font-size: 15px; line-height: 1.6; color: var(--fg); background: var(--bg); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--fg-muted); font-size: .9em; }
[hidden] { display: none !important; }

/* ===== Barra superior ===== */
.topbar {
  position: sticky; top: 0; z-index: 20;
  height: var(--topbar-h); display: flex; align-items: center; gap: .75rem;
  padding: 0 1rem; background: var(--bg-soft); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.15rem; color: var(--fg); }
.topbar-spacer { flex: 1; }
.icon-btn { background: none; border: 1px solid transparent; color: var(--fg); font-size: 1.1rem; cursor: pointer; border-radius: var(--radius); padding: .2rem .5rem; }
.icon-btn:hover { background: var(--bg-hover); }
.sidebar-toggle { display: none; }

.user-menu { position: relative; }
.user-menu summary { cursor: pointer; list-style: none; padding: .3rem .6rem; border-radius: var(--radius); }
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu summary::after { content: " ▾"; color: var(--fg-muted); }
.user-menu summary:hover { background: var(--bg-hover); }
.user-menu .menu {
  position: absolute; right: 0; top: calc(100% + 4px); min-width: 200px; padding: .4rem 0;
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,.15);
}
.user-menu .menu a, .user-menu .menu button.link {
  display: block; width: 100%; padding: .4rem 1rem; text-align: left; color: var(--fg);
  background: none; border: 0; font: inherit; cursor: pointer;
}
.user-menu .menu a:hover, .user-menu .menu button.link:hover { background: var(--bg-hover); text-decoration: none; }

/* ===== Layout ===== */
.layout { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); min-height: calc(100vh - var(--topbar-h)); }
.layout.no-tree { grid-template-columns: minmax(0, 1fr); }
.content { padding: 1.5rem 2rem 4rem; min-width: 0; }

/* ===== Árbol lateral ===== */
.sidebar {
  position: sticky; top: var(--topbar-h); height: calc(100vh - var(--topbar-h)); overflow: auto;
  padding: .75rem .5rem 2rem; border-right: 1px solid var(--border); background: var(--bg-soft);
}
.tree-filter { width: 100%; margin-bottom: .5rem; }
.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree ul { padding-left: .9rem; border-left: 1px solid var(--border); margin-left: .55rem; }
.tree li a { display: block; padding: .15rem .4rem; border-radius: 4px; color: var(--fg); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree li a:hover { background: var(--bg-hover); text-decoration: none; }
.tree li a.active { background: var(--accent); color: var(--accent-fg); }
.tree summary { display: flex; align-items: center; cursor: pointer; list-style: none; }
.tree summary::-webkit-details-marker { display: none; }
.tree summary::before { content: "▸"; width: 1rem; flex: none; color: var(--fg-muted); font-size: .8em; text-align: center; }
.tree details[open] > summary::before { content: "▾"; }
.tree summary a { flex: 1; font-weight: 600; }
.tree li.page { padding-left: 1rem; }
.tree li.page a::before { content: "📄 "; font-size: .85em; }

/* ===== Botones y formularios ===== */
.btn {
  display: inline-flex; align-items: center; gap: .3rem; padding: .35rem .85rem; font: inherit; font-size: .9rem;
  color: var(--fg); background: var(--bg-soft); border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-hover); text-decoration: none; }
.btn.active { border-color: var(--accent); color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.1); }
.btn-danger { color: var(--danger); }
.btn-sm { padding: .15rem .55rem; font-size: .8rem; }

input[type=text], input[type=search], input[type=password], input:not([type]), select, textarea {
  font: inherit; color: var(--fg); background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); padding: .4rem .6rem;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form { display: flex; flex-direction: column; gap: .3rem; }
.form.narrow { max-width: 460px; }
.form label { font-weight: 600; margin-top: .6rem; }
.form label.checkbox, label.checkbox { font-weight: normal; display: flex; align-items: center; gap: .4rem; }
.form-actions { margin-top: 1rem; display: flex; gap: .5rem; }
.field-error { color: var(--danger); font-size: .85rem; }
form.inline { display: inline-flex; gap: .3rem; align-items: center; margin: 0; }
.alert { padding: .7rem 1rem; border-radius: var(--radius); margin: .75rem 0; border: 1px solid var(--border); }
.alert:empty, .alert.validation-summary-valid { display: none; }
.alert ul { margin: 0; padding-left: 1.2rem; }
.alert-success { background: var(--success-bg); }
.alert-warning { background: var(--warning-bg); }
.alert-error { background: var(--error-bg); }

.login-card { max-width: 380px; margin: 8vh auto; padding: 2rem; border: 1px solid var(--border); border-radius: 10px; background: var(--bg-soft); }
.login-card h1 { margin-top: 0; }

table.grid { border-collapse: collapse; width: 100%; margin: 1rem 0; }
table.grid th, table.grid td { padding: .45rem .6rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
table.grid tr.selected { background: var(--bg-hover); }
table.grid td.actions { display: flex; flex-wrap: wrap; gap: .35rem; }

/* ===== Página de entrada ===== */
.breadcrumbs { font-size: .85rem; color: var(--fg-muted); display: flex; gap: .35rem; flex-wrap: wrap; margin-bottom: .5rem; }
.page-actions { display: flex; gap: .5rem; align-items: center; margin-bottom: 1rem; flex-wrap: wrap; }
.article-layout { display: grid; grid-template-columns: minmax(0, 1fr) 220px; gap: 2rem; align-items: start; }
.article-layout:not(:has(.toc)) { grid-template-columns: minmax(0, 1fr); }
.toc { position: sticky; top: calc(var(--topbar-h) + 1rem); font-size: .85rem; border-left: 1px solid var(--border); padding-left: 1rem; max-height: calc(100vh - var(--topbar-h) - 2rem); overflow: auto; }
.toc-title { font-weight: 700; margin-bottom: .4rem; }
.toc ul { list-style: none; margin: 0; padding: 0; }
.toc li { padding: .1rem 0; }
.toc .toc-l3 { padding-left: .8rem; }
.toc .toc-l4 { padding-left: 1.6rem; }
.toc a { color: var(--fg-muted); }
.folder-listing { list-style: none; padding: 0; columns: 2; }
.folder-listing li.folder a::before { content: "📁 "; }
.folder-listing li.page a::before { content: "📄 "; }

/* ===== Markdown renderizado ===== */
.markdown-body { max-width: 980px; word-wrap: break-word; }
.markdown-body h1, .markdown-body h2 { border-bottom: 1px solid var(--border); padding-bottom: .3em; }
.markdown-body h1 { font-size: 2em; margin-top: 0; }
.markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4, .markdown-body h5, .markdown-body h6 { line-height: 1.25; margin: 1.5em 0 .75em; scroll-margin-top: calc(var(--topbar-h) + 1rem); }
.markdown-body code { font-family: var(--mono); font-size: .875em; background: var(--code-bg); padding: .15em .35em; border-radius: 4px; border: 1px solid var(--border); }
.markdown-body pre { position: relative; background: var(--code-bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; overflow: auto; line-height: 1.45; }
.markdown-body pre code { background: none; border: 0; padding: 0; font-size: .85em; }
.markdown-body pre code.hljs { background: none; padding: 0; }
.markdown-body blockquote { margin: 0 0 1rem; padding: 0 1em; color: var(--fg-muted); border-left: .25em solid var(--border); }
.markdown-body table { border-collapse: collapse; margin: 1rem 0; display: block; overflow: auto; max-width: 100%; }
.markdown-body th, .markdown-body td { border: 1px solid var(--border); padding: .4rem .8rem; }
.markdown-body tr:nth-child(2n) { background: var(--bg-soft); }
.markdown-body img { max-width: 100%; height: auto; }
.markdown-body hr { border: 0; border-top: 2px solid var(--border); margin: 1.5rem 0; }
.markdown-body ul.contains-task-list { list-style: none; padding-left: 1.2rem; }
.markdown-body .footnotes { font-size: .85em; color: var(--fg-muted); }
.markdown-body .mermaid { background: var(--bg); text-align: center; margin: 1rem 0; }
.markdown-body .math.block, .markdown-body div.math { overflow-x: auto; }
.markdown-body .markdown-alert { padding: .5rem 1rem; margin-bottom: 1rem; border-left: .25em solid var(--accent); background: var(--bg-soft); border-radius: 0 var(--radius) var(--radius) 0; }
.markdown-body .markdown-alert-warning, .markdown-body .markdown-alert-caution { border-left-color: #d29922; }
.markdown-body .markdown-alert-important { border-left-color: #8250df; }
.markdown-body .markdown-alert-tip { border-left-color: #1a7f37; }
.markdown-body .markdown-alert-title { font-weight: 600; }

.copy-btn {
  position: absolute; top: .4rem; right: .4rem; font-size: .75rem; padding: .1rem .5rem; opacity: 0;
  background: var(--bg); color: var(--fg-muted); border: 1px solid var(--border); border-radius: 4px; cursor: pointer; transition: opacity .15s;
}
pre.has-copy:hover .copy-btn { opacity: 1; }

/* ===== Editor ===== */
.editor-form { display: flex; flex-direction: column; height: calc(100vh - var(--topbar-h) - 3rem); }
.editor-toolbar { display: flex; align-items: center; gap: .5rem; margin-bottom: .75rem; flex-wrap: wrap; }
.editor-title { font-size: 1.1rem; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mode-switch { display: inline-flex; }
.mode-switch .btn:first-child { border-radius: var(--radius) 0 0 var(--radius); }
.mode-switch .btn:last-child { border-radius: 0 var(--radius) var(--radius) 0; border-left: 0; }
.editor-source { display: none; }
.editor-host { flex: 1; min-height: 400px; }
[data-theme="dark"] .toastui-editor-defaultUI { border-color: var(--border); }

/* ===== Historial ===== */
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin: 1rem 0; }
.tab { background: none; border: 0; border-bottom: 2px solid transparent; padding: .5rem 1rem; font: inherit; color: var(--fg-muted); cursor: pointer; }
.tab.active { color: var(--fg); border-bottom-color: var(--accent); }
pre.diff, pre.source { font-family: var(--mono); font-size: .85rem; background: var(--code-bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; overflow: auto; white-space: pre-wrap; }
.diff-add { background: var(--add-bg); display: block; }
.diff-del { background: var(--del-bg); display: block; }
p .diff-add, p .diff-del { display: inline; padding: 0 .3rem; border-radius: 3px; }
.diff-same { color: var(--fg-muted); display: block; }

/* ===== Responsive ===== */
@media (max-width: 1100px) {
  .article-layout { grid-template-columns: minmax(0, 1fr); }
  .toc { display: none; }
}
@media (max-width: 800px) {
  .sidebar-toggle { display: inline-block; }
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; left: 0; top: var(--topbar-h); width: min(85vw, var(--sidebar-w)); z-index: 15; transform: translateX(-100%); transition: transform .2s; }
  body.sidebar-open .sidebar { transform: none; box-shadow: 0 0 24px rgba(0,0,0,.3); }
  .content { padding: 1rem; }
  .folder-listing { columns: 1; }
}

/* ===== Menú de usuario, opciones y 2FA ===== */
.user-menu .menu-separator { border-top: 1px solid var(--border); margin: .3rem 0; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .5rem 1rem 1rem; margin: 0 0 1rem; display: flex; flex-direction: column; gap: .3rem; }
legend { font-weight: 700; padding: 0 .4rem; }
.settings-form { max-width: 720px; }
.settings-form input.wide { width: 100%; }
.form small.muted { font-size: .8rem; }
.option-links li { margin: .3rem 0; }
.steps li { margin-bottom: 1.25rem; }
.key-data { display: grid; grid-template-columns: max-content 1fr; gap: .3rem 1rem; margin: .75rem 0; }
.key-data dt { font-weight: 600; }
.key-data dd { margin: 0; display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.totp-key { font-family: var(--mono); font-size: 1.1rem; letter-spacing: .05em; padding: .2rem .5rem; background: var(--code-bg); border: 1px solid var(--border); border-radius: 4px; }
.otp-uri { display: block; word-break: break-all; font-size: .8rem; margin-top: .4rem; }
.recovery-codes { font-family: var(--mono); background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem 1rem; columns: 2; }

/* ===== Árbol: nodo raíz ===== */
.tree > li.root > details > summary a { font-weight: 700; }
.tree > li.root > details > ul { margin-left: .2rem; }

/* ===== Diálogo de imágenes comunes ===== */
.shared-dialog { width: min(900px, 92vw); max-height: 85vh; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); color: var(--fg); padding: 1rem 1.25rem; }
.shared-dialog::backdrop { background: rgba(0, 0, 0, .45); }
.shared-dialog-header { display: flex; align-items: center; justify-content: space-between; }
.shared-dialog-header h2 { margin: 0; font-size: 1.2rem; }
.shared-dialog input[type=search] { width: 100%; margin: .75rem 0; }
.shared-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: .6rem; overflow: auto; max-height: 55vh; }
.shared-item { display: flex; flex-direction: column; align-items: center; gap: .3rem; padding: .4rem; background: var(--bg-soft); border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer; color: var(--fg); font: inherit; }
.shared-item:hover { border-color: var(--accent); }
.shared-item img { width: 100%; height: 90px; object-fit: contain; }
.shared-file-icon { font-size: 2.5rem; height: 90px; display: flex; align-items: center; }
.shared-name { font-size: .75rem; word-break: break-all; text-align: center; }
