/* ==========================================================================
   Markdown Editor — Ufumbuzi
   Hoja de estilos única. Sin dependencias externas.

   Todo el "look" se controla con variables CSS + atributos en <html>:
     data-theme = light | dark | sepia
     data-view  = code | split | preview
     data-font  = sans | serif | mono
     --scale    = tamaño de texto (1 = 100%)
     --measure  = ancho máximo de línea de lectura (ch)
   Cambiar cualquiera de ellos NO re-renderiza el documento.
   ========================================================================== */

/* --------------------------------------------------------------------------
   Temas
   -------------------------------------------------------------------------- */
:root {
    color-scheme: light;

    /* chrome de la app */
    --bg: #eef1f5;
    --panel: #ffffff;
    --fg: #1f2328;
    --muted: #6b7280;
    --line: #e2e6ea;
    --accent: #0ea5e9;
    --accent-fg: #ffffff;
    --accent-soft: #e0f2fe;

    /* superficie de lectura */
    --paper: #ffffff;
    --paper-fg: #1f2328;
    --paper-muted: #5b6471;
    --paper-line: #e5e7eb;
    --code-bg: #f4f6f8;
    --code-fg: #1f2937;
    --mark: #fff3b0;
    --link: #0369a1;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);

    /* resaltado de sintaxis */
    --tok-com: #6e7781;
    --tok-str: #0a7d3f;
    --tok-num: #9a5b00;
    --tok-kw: #8250df;
    --tok-tag: #b31d28;
    --tok-attr: #8a5a00;

    --measure: 68ch;
    --scale: 1;
    --doc-font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

[data-theme="dark"] {
    color-scheme: dark;

    --bg: #0f1216;
    --panel: #171b21;
    --fg: #e6e9ed;
    --muted: #9aa3ae;
    --line: #262c34;
    --accent: #38bdf8;
    --accent-fg: #06273a;
    --accent-soft: #0b2b3d;

    --paper: #14181d;
    --paper-fg: #e6e9ed;
    --paper-muted: #a3acb7;
    --paper-line: #262c34;
    --code-bg: #1b2027;
    --code-fg: #e6e9ed;
    --mark: #5a4a12;
    --link: #7dd3fc;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);

    --tok-com: #8b949e;
    --tok-str: #7ee787;
    --tok-num: #ffab70;
    --tok-kw: #c792ea;
    --tok-tag: #ff7b72;
    --tok-attr: #d2a8ff;
}

[data-theme="sepia"] {
    color-scheme: light;

    --bg: #ded4bf;
    --panel: #f2e9d6;
    --fg: #40372a;
    --muted: #7b6d57;
    --line: #d8cbb0;
    --accent: #9a6a2f;
    --accent-fg: #fffaf0;
    --accent-soft: #e8dcc2;

    --paper: #fbf5e6;
    --paper-fg: #3b3226;
    --paper-muted: #6f6250;
    --paper-line: #e2d7c0;
    --code-bg: #f2e9d6;
    --code-fg: #3b3226;
    --mark: #f3dfa0;
    --link: #8a5d16;
    --shadow: 0 1px 2px rgba(64, 55, 42, .12);

    --tok-com: #8b7f6b;
    --tok-str: #3f6b2f;
    --tok-num: #8a5d16;
    --tok-kw: #8a4b8a;
    --tok-tag: #a3402f;
    --tok-attr: #7a5a20;
}

/* --------------------------------------------------------------------------
   Base
   -------------------------------------------------------------------------- */
*,
*::before,
*::after {
    box-sizing: border-box;
}

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

body {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
    background: var(--bg);
    color: var(--fg);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 14px;
    -webkit-text-size-adjust: 100%;
}

button,
select,
input,
textarea {
    font: inherit;
    color: inherit;
}

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: 4px;
}

@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: .01ms !important;
        transition-duration: .01ms !important;
    }
}

/* --------------------------------------------------------------------------
   Barra superior
   -------------------------------------------------------------------------- */
.topbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 10px;
    padding: 8px 12px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    box-shadow: var(--shadow);
    position: relative;
    z-index: 5;
}

.brand {
    display: flex;
    align-items: center;
    gap: 6px;
    font-weight: 700;
    letter-spacing: -.01em;
    color: var(--fg);
    white-space: nowrap;
}

.brand svg {
    width: 20px;
    height: 20px;
    stroke: var(--accent);
    fill: none;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.file-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    min-width: 0;
    padding: 3px 10px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--bg);
    font-size: 12.5px;
    color: var(--muted);
    max-width: 42ch;
}

#file-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#file-name[data-untitled="true"] {
    font-style: italic;
    opacity: .8;
}

.dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--accent);
    flex: 0 0 auto;
}

.dot[hidden] {
    display: none;
}

.group {
    display: flex;
    align-items: center;
    gap: 6px;
}

.spacer {
    flex: 1 1 auto;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    padding: 6px 11px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    color: var(--fg);
    font-size: 12.5px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background-color .15s, border-color .15s, transform .05s;
}

.btn:hover {
    background: var(--accent-soft);
    border-color: var(--accent);
}

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

.btn.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-fg);
}

.btn.primary:hover {
    filter: brightness(1.06);
    background: var(--accent);
}

.btn.icon {
    min-width: 34px;
    padding: 6px 8px;
    font-weight: 700;
}

.btn[hidden] {
    display: none;
}

.btn[aria-pressed="true"] {
    background: var(--accent-soft);
    border-color: var(--accent);
}

.btn .count {
    margin-left: 5px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--accent-soft);
    color: var(--fg);
    font-size: 10px;
    font-weight: 700;
}

.btn .count[hidden] {
    display: none;
}

.link-btn {
    padding: 1px 9px;
    border: 1px solid var(--accent);
    border-radius: 999px;
    background: var(--accent-soft);
    color: var(--fg);
    font-size: 11px;
    font-weight: 600;
    white-space: nowrap;
    cursor: pointer;
}

.link-btn:hover {
    filter: brightness(.97);
}

.link-btn[hidden] {
    display: none;
}

.field {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 12px;
    color: var(--muted);
}

.field select {
    padding: 5px 7px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    font-size: 12.5px;
    font-weight: 600;
    color: var(--fg);
    cursor: pointer;
}

/* Selector de vista */
.segmented {
    display: inline-flex;
    padding: 2px;
    border: 1px solid var(--line);
    border-radius: 9px;
    background: var(--bg);
}

.segmented button {
    padding: 4px 10px;
    border: 0;
    border-radius: 7px;
    background: transparent;
    color: var(--muted);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
}

.segmented button[aria-pressed="true"] {
    background: var(--panel);
    color: var(--fg);
    box-shadow: var(--shadow);
}

/* --------------------------------------------------------------------------
   Área de trabajo
   -------------------------------------------------------------------------- */
#workspace {
    flex: 1 1 auto;
    display: grid;
    grid-template-columns: 1fr 1fr;
    min-height: 0;
    overflow: hidden;
}

html[data-view="code"] #workspace {
    grid-template-columns: 1fr;
}

html[data-view="preview"] #workspace {
    grid-template-columns: 1fr;
}

html[data-view="code"] #pane-preview,
html[data-view="preview"] #pane-code {
    display: none;
}

#pane-code {
    display: flex;
    min-width: 0;
    min-height: 0;
    border-right: 1px solid var(--line);
}

html[data-view="preview"] #pane-code {
    border-right: 0;
}

#code {
    flex: 1 1 auto;
    width: 100%;
    min-width: 0;
    min-height: 0;
    margin: 0;
    padding: 18px 20px 40vh;
    border: 0;
    border-radius: 0;
    background: var(--panel);
    color: var(--fg);
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
    font-size: 13.5px;
    line-height: 1.65;
    tab-size: 2;
    resize: none;
    overflow: auto;
    outline: none;
    caret-color: var(--accent);
}

#pane-preview {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    background: var(--paper);
}

#preview-scroll {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    overflow: auto;
    background: var(--paper);
    overscroll-behavior: contain;
}

/* --------------------------------------------------------------------------
   Índice (TOC)
   -------------------------------------------------------------------------- */
.toc {
    flex: 0 0 auto;
    max-height: 46%;
    overflow: auto;
    padding: 6px;
    border-bottom: 1px solid var(--line);
    background: var(--bg);
}

.toc[hidden] {
    display: none;
}

.toc-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 8px 5px;
    color: var(--muted);
    font-size: 10.5px;
    font-weight: 700;
    letter-spacing: .07em;
    text-transform: uppercase;
}

.toc-close {
    padding: 1px 5px;
    border: 0;
    border-radius: 4px;
    background: transparent;
    color: var(--muted);
    font-size: 12px;
    cursor: pointer;
}

.toc-close:hover {
    background: var(--accent-soft);
    color: var(--fg);
}

.toc-list {
    margin: 0;
    padding: 0;
    list-style: none;
}

.toc-link {
    display: block;
    width: 100%;
    padding: 5px 8px;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: var(--fg);
    font-size: 12.5px;
    text-align: left;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
}

.toc-link:hover {
    background: var(--accent-soft);
}

.toc-link[data-level="1"] {
    font-weight: 700;
}

.toc-empty {
    padding: 8px 10px;
    color: var(--muted);
    font-size: 12px;
}

/* --------------------------------------------------------------------------
   Documento renderizado
   -------------------------------------------------------------------------- */
.doc {
    max-width: var(--measure);
    margin: 0 auto;
    padding: 2.2rem 1.6rem 40vh;
    color: var(--paper-fg);
    font-family: var(--doc-font);
    font-size: calc(16px * var(--scale));
    line-height: 1.65;
    overflow-wrap: break-word;
}

[data-font="serif"] .doc {
    --doc-font: Georgia, "Iowan Old Style", "Times New Roman", serif;
}

[data-font="mono"] .doc {
    --doc-font: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
    line-height: 1.55;
}

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

.doc h1,
.doc h2,
.doc h3,
.doc h4,
.doc h5,
.doc h6 {
    margin: 1.6em 0 .55em;
    line-height: 1.25;
    font-weight: 700;
    letter-spacing: -.012em;
    color: var(--paper-fg);
}

.doc h1 {
    font-size: 1.85em;
    padding-bottom: .25em;
    border-bottom: 1px solid var(--paper-line);
}

.doc h2 {
    font-size: 1.45em;
    padding-bottom: .2em;
    border-bottom: 1px solid var(--paper-line);
}

.doc h3 {
    font-size: 1.2em;
}

.doc h4 {
    font-size: 1.05em;
}

.doc h5,
.doc h6 {
    font-size: 1em;
    color: var(--paper-muted);
}

.doc p {
    margin: 0 0 .9em;
}

.doc a {
    color: var(--link);
    text-decoration: underline;
    text-decoration-thickness: 1px;
    text-underline-offset: 2px;
}

.doc a:hover {
    text-decoration-thickness: 2px;
}

.doc strong {
    font-weight: 700;
}

.doc em {
    font-style: italic;
}

.doc del {
    opacity: .7;
}

.doc ul,
.doc ol {
    margin: 0 0 .9em;
    padding-left: 1.5em;
}

.doc li {
    margin: .2em 0;
}

.doc li>ul,
.doc li>ol {
    margin: .25em 0;
}

.doc li>p {
    margin: 0 0 .4em;
}

.doc li.task {
    list-style: none;
}

.doc li input[type="checkbox"] {
    margin-right: .4em;
    vertical-align: baseline;
    accent-color: var(--accent);
}

.doc blockquote {
    margin: 0 0 .9em;
    padding: .15em 0 .15em 1em;
    border-left: 3px solid var(--accent);
    color: var(--paper-muted);
}

.doc blockquote p:last-child {
    margin-bottom: 0;
}

.doc hr {
    height: 0;
    margin: 1.8em 0;
    border: 0;
    border-top: 1px solid var(--paper-line);
}

.doc code {
    padding: .12em .38em;
    border-radius: 4px;
    background: var(--code-bg);
    color: var(--code-fg);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
    font-size: .875em;
}

.doc pre {
    margin: 0 0 1em;
    padding: .9em 1em;
    border: 1px solid var(--paper-line);
    border-radius: 8px;
    background: var(--code-bg);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.doc pre code {
    padding: 0;
    background: none;
    font-size: .85em;
    line-height: 1.55;
    white-space: pre;
}

/* Resaltado de sintaxis */
.doc .tok-com {
    color: var(--tok-com);
    font-style: italic;
}

.doc .tok-str {
    color: var(--tok-str);
}

.doc .tok-num {
    color: var(--tok-num);
}

.doc .tok-kw {
    color: var(--tok-kw);
    font-weight: 600;
}

.doc .tok-tag {
    color: var(--tok-tag);
}

.doc .tok-attr {
    color: var(--tok-attr);
}

.doc img {
    max-width: 100%;
    height: auto;
    border-radius: 6px;
}

.doc mark {
    background: var(--mark);
    color: inherit;
    padding: 0 .15em;
    border-radius: 3px;
}

.doc table {
    width: 100%;
    margin: 0 0 1em;
    border-collapse: collapse;
    font-size: .94em;
}

.doc th,
.doc td {
    padding: .45em .6em;
    border: 1px solid var(--paper-line);
    text-align: left;
    vertical-align: top;
}

.doc th {
    background: var(--code-bg);
    font-weight: 700;
}

.table-wrap {
    margin: 0 0 1em;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.table-wrap table {
    margin: 0;
    min-width: 100%;
}

.doc .empty {
    color: var(--paper-muted);
    font-style: italic;
}

/* --------------------------------------------------------------------------
   Barra de estado
   -------------------------------------------------------------------------- */
.statusbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 5px 12px;
    background: var(--panel);
    border-top: 1px solid var(--line);
    color: var(--muted);
    font-size: 11.5px;
    white-space: nowrap;
    overflow: hidden;
}

#status-file {
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 40ch;
    font-weight: 600;
}

#status-hint {
    flex: 1 1 auto;
    overflow: hidden;
    text-overflow: ellipsis;
    opacity: .9;
}

#status-count {
    margin-left: auto;
    flex: 0 0 auto;
}

/* --------------------------------------------------------------------------
   Menú de recientes
   -------------------------------------------------------------------------- */
.recent-menu {
    position: absolute;
    top: 100%;
    left: 12px;
    z-index: 20;
    width: min(360px, calc(100vw - 24px));
    margin-top: 4px;
    padding: 6px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: var(--panel);
    box-shadow: 0 10px 30px rgba(16, 24, 40, .18);
}

.recent-menu[hidden] {
    display: none;
}

.recent-menu .empty {
    padding: 10px;
    color: var(--muted);
    font-size: 12.5px;
}

.recent-item {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 8px 10px;
    border: 0;
    border-radius: 7px;
    background: transparent;
    color: var(--fg);
    font-size: 12.5px;
    text-align: left;
    cursor: pointer;
}

.recent-item:hover {
    background: var(--accent-soft);
}

.recent-item .name {
    flex: 1 1 auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: 600;
}

.recent-item .when {
    flex: 0 0 auto;
    color: var(--muted);
    font-size: 11px;
}

/* --------------------------------------------------------------------------
   Aviso flotante (toast)
   -------------------------------------------------------------------------- */
.toast {
    position: fixed;
    left: 50%;
    bottom: 26px;
    z-index: 60;
    max-width: min(520px, calc(100vw - 32px));
    padding: 10px 16px;
    border-radius: 999px;
    background: #111827;
    color: #f9fafb;
    font-size: 12.5px;
    font-weight: 600;
    text-align: center;
    box-shadow: 0 10px 30px rgba(0, 0, 0, .28);
    opacity: 0;
    transform: translate(-50%, 12px);
    pointer-events: none;
    transition: opacity .18s, transform .18s;
}

[data-theme="dark"] .toast,
[data-theme="sepia"] .toast {
    background: #f9fafb;
    color: #111827;
}

.toast.show {
    opacity: 1;
    transform: translate(-50%, 0);
}

/* --------------------------------------------------------------------------
   Ayuda
   -------------------------------------------------------------------------- */
dialog {
    width: min(620px, calc(100vw - 32px));
    max-height: 86vh;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--panel);
    color: var(--fg);
    box-shadow: 0 24px 60px rgba(16, 24, 40, .3);
}

dialog::backdrop {
    background: rgba(9, 12, 16, .55);
}

.dialog-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 14px 18px;
    border-bottom: 1px solid var(--line);
}

.dialog-head h2 {
    margin: 0;
    font-size: 15px;
}

.dialog-body {
    padding: 16px 18px;
    overflow-y: auto;
    max-height: calc(86vh - 120px);
    font-size: 13px;
    line-height: 1.55;
}

.dialog-body h3 {
    margin: 16px 0 6px;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--muted);
}

.dialog-body h3:first-child {
    margin-top: 0;
}

.dialog-body p,
.dialog-body ul {
    margin: 0 0 8px;
}

.dialog-body ul {
    padding-left: 18px;
}

.dialog-body code {
    padding: .1em .35em;
    border-radius: 4px;
    background: var(--code-bg);
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: .9em;
}

.dialog-foot {
    display: flex;
    justify-content: flex-end;
    padding: 12px 18px;
    border-top: 1px solid var(--line);
}

/* --------------------------------------------------------------------------
   Responsive
   -------------------------------------------------------------------------- */
@media (max-width: 900px) {
    .topbar {
        gap: 6px 8px;
        padding: 6px 8px;
    }

    .brand span,
    .field>span {
        display: none;
    }

    .file-meta {
        flex: 1 1 auto;
        max-width: none;
        font-size: 11.5px;
    }

    #workspace {
        grid-template-columns: 1fr;
    }

    html[data-view="split"] #workspace {
        grid-template-rows: 1fr 1fr;
    }

    html[data-view="split"] #pane-code {
        border-right: 0;
        border-bottom: 1px solid var(--line);
    }

    .doc {
        padding: 1.4rem 1rem 30vh;
        max-width: 100%;
    }

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

@media (max-width: 560px) {
    .segmented button {
        padding: 4px 8px;
    }

    #btn-save-as {
        display: none;
    }
}

/* --------------------------------------------------------------------------
   Impresión: siempre claro, sin chrome, solo el documento
   -------------------------------------------------------------------------- */
@media print {
    html,
    body {
        height: auto;
        overflow: visible;
        background: #fff;
    }

    .topbar,
    .statusbar,
    #pane-code,
    .toc,
    .recent-menu,
    .toast {
        display: none !important;
    }

    #workspace {
        display: block;
        overflow: visible;
    }

    #pane-preview {
        display: block !important;
        overflow: visible;
        background: #fff;
    }

    #preview-scroll {
        overflow: visible;
        background: #fff;
    }

    .doc {
        max-width: none;
        padding: 0;
        color: #000;
        font-size: 11pt;
        line-height: 1.5;
    }

    .doc h1,
    .doc h2,
    .doc a {
        color: #000;
    }

    .doc pre,
    .doc table,
    .doc blockquote {
        border-color: #bbb;
        background: #fafafa;
        break-inside: avoid;
    }

    .doc th {
        background: #f0f0f0;
    }

    .doc blockquote {
        border-left-color: #999;
    }

    .doc h1,
    .doc h2,
    .doc h3 {
        break-after: avoid;
    }
}
