/* ==========================================================================
   ERP Melaapps — hoja de estilos
   Dos temas: claro y oscuro. data-theme="auto" sigue al sistema.
   ========================================================================== */

:root {
  --bg:          #f5f6f8;
  --surface:     #ffffff;
  --surface-2:   #eef0f3;
  --nav-bg:      #ffffff;
  --line:        #e3e6ea;
  --line-soft:   #eef0f3;
  --text:        #16191d;
  --muted:       #667085;
  --accent:      #ED1C2E;
  --accent-ink:  #C10300;
  --accent-soft: #fdecee;
  --ok:          #1b7a3d;
  --ok-soft:     #e9f7ed;
  --warn-soft:   #fff6e0;
  --warn-ink:    #7a5300;
  --code-bg:     #1e2228;
  --code-fg:     #e6e9ee;
  --shadow:      0 1px 2px rgba(16,24,40,.05);
  --shadow-lg:   0 12px 40px rgba(16,24,40,.16);
  --radius:      10px;
  --nav-w:       230px;
  --head-h:      54px;
}

:root[data-theme="dark"] {
  --bg:          #101317;
  --surface:     #191d23;
  --surface-2:   #21262e;
  --nav-bg:      #14181d;
  --line:        #2a3038;
  --line-soft:   #21262e;
  --text:        #e4e8ee;
  --muted:       #8b95a3;
  --accent:      #ff4d5a;
  --accent-ink:  #ff6b76;
  --accent-soft: #2c1a1e;
  --ok:          #4ade80;
  --ok-soft:     #16251c;
  --warn-soft:   #2a2113;
  --warn-ink:    #e0b458;
  --code-bg:     #0c0e11;
  --code-fg:     #d7dce4;
  --shadow:      0 1px 2px rgba(0,0,0,.4);
  --shadow-lg:   0 12px 40px rgba(0,0,0,.55);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg:          #101317;
    --surface:     #191d23;
    --surface-2:   #21262e;
    --nav-bg:      #14181d;
    --line:        #2a3038;
    --line-soft:   #21262e;
    --text:        #e4e8ee;
    --muted:       #8b95a3;
    --accent:      #ff4d5a;
    --accent-ink:  #ff6b76;
    --accent-soft: #2c1a1e;
    --ok:          #4ade80;
    --ok-soft:     #16251c;
    --warn-soft:   #2a2113;
    --warn-ink:    #e0b458;
    --code-bg:     #0c0e11;
    --code-fg:     #d7dce4;
    --shadow:      0 1px 2px rgba(0,0,0,.4);
    --shadow-lg:   0 12px 40px rgba(0,0,0,.55);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

body.fixed { overflow: hidden; }

a { color: var(--accent); }

::selection { background: var(--accent); color: #fff; }

/* ==========================================================================
   Login
   ========================================================================== */

.login {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(1200px 600px at 20% -10%, rgba(237,28,46,.28), transparent 60%),
    var(--bg);
}

.login .card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 34px 32px;
  box-shadow: var(--shadow-lg);
}

.login h1 { margin: 0 0 4px; font-size: 1.35rem; letter-spacing: -.01em; }
.login .notice { margin: 0 0 26px; color: var(--muted); font-size: .84rem; }

.login label {
  display: block;
  font-size: .76rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--muted);
}

.login input {
  width: 100%;
  padding: 11px 12px;
  margin-bottom: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
}

.login button {
  width: 100%;
  padding: 12px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: .98rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.login button:hover { background: var(--accent-ink); }

.login .error {
  background: var(--accent-soft);
  color: var(--accent-ink);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: .86rem;
  margin: 0 0 18px;
}

.login .langs { margin-top: 22px; text-align: center; }

input:focus-visible, select:focus-visible, textarea:focus-visible,
button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* ==========================================================================
   Estructura: barra lateral + contenido
   ========================================================================== */

.app {
  display: grid;
  grid-template-columns: var(--nav-w) minmax(0, 1fr);
  min-height: 100vh;
  min-height: 100dvh;
}
body.fixed .app { height: 100vh; height: 100dvh; }

.nav {
  background: var(--nav-bg);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.nav-top {
  height: var(--head-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  flex: 0 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 650;
  font-size: .97rem;
  color: var(--text);
  text-decoration: none;
  letter-spacing: -.01em;
}
.brand .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--accent);
  flex: 0 0 auto;
}

.nav-main { padding: 10px; display: flex; flex-direction: column; gap: 1px; flex: 0 0 auto; }

.nav-main a {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 9px 11px;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  font-size: .89rem;
  font-weight: 500;
}
.nav-main a:hover { background: var(--surface-2); color: var(--text); }
.nav-main a.on { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }

.nav-main svg {
  width: 17px;
  height: 17px;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Conversaciones inyectadas por el chat */
.nav-extra {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 0 10px;
}
.nav-extra:empty { flex: 1 1 auto; }

.nav-sep {
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  font-weight: 700;
  padding: 16px 11px 7px;
}

.nav-new {
  width: 100%;
  text-align: left;
  padding: 8px 11px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: none;
  color: var(--muted);
  font-size: .85rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.nav-new:hover { border-color: var(--accent); color: var(--accent); border-style: solid; }

.sessions { list-style: none; margin: 6px 0 0; padding: 0; }
.sessions li { display: flex; align-items: center; border-radius: 7px; }
.sessions li:hover { background: var(--surface-2); }
.sessions li.on { background: var(--accent-soft); }

.sessions button.pick {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: none;
  border: 0;
  padding: 8px 10px;
  font-size: .85rem;
  cursor: pointer;
  font-family: inherit;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sessions li.on button.pick { color: var(--accent-ink); font-weight: 600; }
.sessions li:hover button.pick { color: var(--text); }

.sessions button.del {
  background: none;
  border: 0;
  color: var(--muted);
  cursor: pointer;
  padding: 5px 8px;
  font-size: 1rem;
  line-height: 1;
  opacity: 0;
  font-family: inherit;
}
.sessions li:hover button.del { opacity: .75; }
.sessions button.del:hover { color: var(--accent); opacity: 1; }

.empty-hint { color: var(--muted); font-size: .82rem; padding: 8px 11px; }

.nav-foot {
  flex: 0 0 auto;
  border-top: 1px solid var(--line);
  padding: 12px 14px;
}

.langs { display: flex; gap: 2px; margin-bottom: 8px; }
.langs a {
  color: var(--muted);
  text-decoration: none;
  font-size: .68rem;
  font-weight: 700;
  padding: 3px 7px;
  border-radius: 5px;
  letter-spacing: .04em;
}
.langs a:hover { background: var(--surface-2); }
.langs a.on { color: #fff; background: var(--accent); }

.whoami {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: .78rem;
}
.whoami span {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.whoami a { color: var(--accent); text-decoration: none; font-weight: 600; flex: 0 0 auto; }

/* Cabecera de página */

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.pagehead {
  height: var(--head-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.pagehead h1 {
  font-size: .97rem;
  font-weight: 600;
  margin: 0;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  letter-spacing: -.01em;
}
.pagehead .tools { display: flex; align-items: center; gap: 6px; flex: 0 0 auto; }

.theme-btn {
  background: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  cursor: pointer;
  color: var(--muted);
  padding: 0;
}
.theme-btn:hover { color: var(--text); border-color: var(--muted); }
.theme-btn svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  grid-area: 1/1;
}
/* En claro se ve la luna (lo que vas a activar); en oscuro, el sol. */
:root[data-theme="light"] .i-sun,
:root[data-theme="dark"]  .i-moon { display: none; }
:root[data-theme="auto"]  .i-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] .i-moon { display: block; }
  :root[data-theme="auto"] .i-sun  { display: none; }
}

.content { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
body.fixed .content { overflow: hidden; display: flex; flex-direction: column; }

/* Menú lateral en móvil */

.nav-toggle, .nav-open, .nav-close, .nav-scrim { display: none; }

/* ==========================================================================
   Chat
   ========================================================================== */

.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1;
}

.messages { flex: 1 1 auto; overflow-y: auto; padding: 26px 24px; min-height: 0; }

.empty {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
  padding: 20px;
}
.empty h2 { margin: 0 0 6px; font-size: 1.2rem; color: var(--text); font-weight: 600; }
.empty p { margin: 0; font-size: .9rem; max-width: 380px; line-height: 1.55; }

.msg { max-width: 760px; margin: 0 auto 20px; }
.msg .who-label {
  font-size: .68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: 5px;
}
.msg.user .who-label { color: var(--accent); }

.msg .bubble {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 13px 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.6;
  box-shadow: var(--shadow);
}
.msg.user .bubble { background: var(--accent-soft); border-color: transparent; }
.msg.error .bubble { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-ink); }

.msg .bubble code {
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: .88em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.msg .bubble pre {
  background: var(--code-bg);
  color: var(--code-fg);
  padding: 12px 14px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: .84rem;
  line-height: 1.5;
}
.msg .bubble pre code { background: none; padding: 0; color: inherit; }

.typing { color: var(--muted); font-style: italic; }

/* Zona de escritura, con el contexto integrado */

.composer-wrap {
  flex: 0 0 auto;
  border-top: 1px solid var(--line);
  background: var(--surface);
  padding: 10px 24px 14px;
}

.ctx-bar {
  max-width: 760px;
  margin: 0 auto 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.ctx-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: 1px dashed var(--line);
  border-radius: 20px;
  padding: 5px 12px;
  font-size: .78rem;
  color: var(--muted);
  cursor: pointer;
  font-family: inherit;
  max-width: 100%;
}
.ctx-btn:hover { border-color: var(--accent); color: var(--accent); }
.ctx-btn.active {
  border-style: solid;
  border-color: transparent;
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-weight: 600;
}
.ctx-btn .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ctx-btn .x { font-size: .95rem; line-height: 1; opacity: .7; }
.ctx-btn .x:hover { opacity: 1; }

.ctx-picker {
  max-width: 760px;
  margin: 0 auto 8px;
  display: none;
}
.ctx-picker.open { display: block; }
.ctx-picker select {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: inherit;
  font-size: .86rem;
  background: var(--bg);
  color: var(--text);
}

.composer {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  gap: 9px;
  align-items: flex-end;
}
.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 11px 12px;
  font-family: inherit;
  font-size: .95rem;
  line-height: 1.45;
  max-height: 200px;
  background: var(--bg);
  color: var(--text);
}
.composer button {
  flex: 0 0 auto;
  padding: 11px 20px;
  border: 0;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-size: .9rem;
  cursor: pointer;
  font-family: inherit;
}
.composer button:hover:not(:disabled) { background: var(--accent-ink); }
.composer button:disabled { opacity: .45; cursor: default; }

/* ==========================================================================
   Páginas de contenido
   ========================================================================== */

.docs-wrap { max-width: 900px; margin: 0 auto; padding: 26px 22px 80px; }
.docs-wrap.wide { max-width: 1140px; }

.page-title { font-size: 1.3rem; margin: 0 0 20px; font-weight: 650; letter-spacing: -.01em; }

.flash {
  background: var(--ok-soft);
  color: var(--ok);
  padding: 10px 13px;
  border-radius: 8px;
  font-size: .86rem;
  margin: 0 0 18px;
}
.flash.warn { background: var(--warn-soft); color: var(--warn-ink); }
.flash code {
  background: rgba(128,128,128,.18);
  padding: 2px 7px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  user-select: all;
}

/* Filtros */

.docs-filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 20px; }
.docs-filters input[type=search] { flex: 1; min-width: 200px; }
.docs-filters input[type=search],
.docs-filters select {
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: inherit;
  font-size: .89rem;
  background: var(--surface);
  color: var(--text);
  max-width: 100%;
}
.docs-filters button {
  padding: 9px 18px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  font-size: .89rem;
}
.docs-filters button:hover { background: var(--accent-ink); }

.docs-count { color: var(--muted); font-size: .8rem; margin: 0 0 16px; }
.docs-empty { color: var(--muted); }

/* Listas */

.doc-group { margin-bottom: 24px; }
.doc-group h2 {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin: 0 0 8px;
  font-weight: 700;
}

.doc-list, .doc-results { list-style: none; margin: 0; padding: 0; }

.doc-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 13px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 4px;
}
.doc-list li:hover { border-color: var(--accent); }
.doc-list a { color: var(--text); text-decoration: none; flex: 1; font-size: .9rem; }
.doc-list a:hover { color: var(--accent-ink); }

.tag {
  font-size: .64rem;
  font-weight: 700;
  letter-spacing: .05em;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 2px 6px;
  flex: 0 0 auto;
}

.doc-results li {
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 8px;
}
.doc-results li:hover { border-color: var(--accent); }
.doc-results > li > a { color: var(--text); text-decoration: none; font-weight: 600; font-size: .94rem; }
.doc-results > li > a:hover { color: var(--accent-ink); }
.doc-results .meta { display: block; font-size: .72rem; color: var(--muted); margin: 4px 0 6px; }
.doc-results .excerpt { margin: 0; font-size: .85rem; color: var(--muted); line-height: 1.55; }
.doc-results mark { background: var(--warn-soft); color: var(--text); padding: 0 2px; border-radius: 2px; }

/* Documento */

.crumbs { font-size: .8rem; margin: 0 0 14px; color: var(--muted); }
.crumbs a { color: var(--accent); text-decoration: none; }
.crumbs span { margin: 0 6px; }

.doc-head { display: flex; align-items: flex-start; gap: 14px; margin-bottom: 6px; }
.doc-head h1 { font-size: 1.4rem; margin: 0; flex: 1; line-height: 1.3; font-weight: 650; letter-spacing: -.01em; }

.btn-plain {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 14px;
  font-size: .82rem;
  font-weight: 600;
  text-decoration: none;
  color: var(--text);
  background: var(--surface);
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
  flex: 0 0 auto;
}
.btn-plain:hover { border-color: var(--accent); color: var(--accent); }

.doc-meta { font-size: .76rem; color: var(--muted); margin: 0 0 12px; }
.doc-siblings { font-size: .8rem; margin: 0 0 18px; color: var(--muted); }
.doc-siblings a { color: var(--accent); text-decoration: none; font-weight: 700; margin-right: 8px; }

.doc-body {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 26px 30px;
  line-height: 1.68;
  overflow-wrap: anywhere;
}
.doc-body h1, .doc-body h2, .doc-body h3 { line-height: 1.3; margin: 26px 0 10px; font-weight: 650; }
.doc-body h1:first-child, .doc-body h2:first-child { margin-top: 0; }
.doc-body h2 { font-size: 1.12rem; }
.doc-body h3 { font-size: 1rem; }
.doc-body p { margin: 0 0 12px; }
.doc-body ul, .doc-body ol { margin: 0 0 14px; padding-left: 22px; }
.doc-body li { margin-bottom: 4px; }
.doc-body code {
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: .88em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.doc-body pre {
  background: var(--code-bg);
  color: var(--code-fg);
  padding: 14px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: .84rem;
}
.doc-body pre code { background: none; padding: 0; color: inherit; }

/* Diagramas.

   El estado por defecto es "visible como código": si mermaid.min.js no llega
   a cargar, el lector ve el texto del diagrama en vez de un hueco en blanco.
   Solo se oculta mientras se dibuja, y la clase que lo oculta la pone el
   propio script — de ahí que el caso de fallo se resuelva solo. */
.doc-body pre.mermaid {
  background: var(--code-bg);
  color: var(--code-fg);
  padding: 14px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: .84rem;
  margin: 0 0 18px;
}
.doc-body pre.mermaid.mermaid-pending { visibility: hidden; }
.doc-body pre.mermaid.mermaid-done {
  background: none;
  color: inherit;
  padding: 0;
  text-align: center;
  line-height: normal;
  overflow-x: auto;
}
.doc-body pre.mermaid svg { max-width: 100%; height: auto; }
/* Diagrama con un fallo de sintaxis: se muestra el código, marcado. */
.doc-body pre.mermaid.mermaid-error { border-left: 3px solid var(--accent); }
.doc-body table { border-collapse: collapse; width: 100%; margin-bottom: 16px; display: block; overflow-x: auto; }
.doc-body th, .doc-body td { border: 1px solid var(--line); padding: 7px 10px; text-align: left; font-size: .87rem; }
.doc-body th { background: var(--surface-2); font-weight: 600; }
.doc-body img { max-width: 100%; height: auto; }
.doc-body a { color: var(--accent-ink); }
.doc-body hr { border: 0; border-top: 1px solid var(--line); margin: 20px 0; }

/* Edición */

.doc-edit input[type=text] {
  width: 100%;
  padding: 11px 12px;
  font-size: 1.1rem;
  font-weight: 600;
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 10px;
  font-family: inherit;
  background: var(--surface);
  color: var(--text);
}
.doc-edit textarea {
  width: 100%;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .83rem;
  line-height: 1.6;
  resize: vertical;
  background: var(--surface);
  color: var(--text);
}
.doc-edit .row { display: flex; gap: 10px; align-items: center; margin-top: 12px; }
.doc-edit button {
  padding: 10px 22px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.doc-edit button:hover { background: var(--accent-ink); }

/* Historial */

/* ------------------------------------------------ módulos por departamento */

.page-h1 { font-size: 1.4rem; margin: 0 0 18px; font-weight: 680; }

.dept-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
}
.dept-card {
  display: flex; flex-direction: column; gap: 6px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  text-decoration: none; color: var(--text);
  transition: border-color .15s, transform .1s;
}
.dept-card:hover { border-color: var(--accent); transform: translateY(-1px); }
.dept-card h2 { font-size: 1rem; margin: 0; font-weight: 650; }
.dept-num { margin: 4px 0 0; display: flex; align-items: baseline; gap: 6px; }
.dept-num strong { font-size: 1.7rem; font-weight: 700; line-height: 1; }
.dept-num span { font-size: .78rem; color: var(--muted); }
.dept-tags { margin: 0; display: flex; flex-wrap: wrap; gap: 6px; min-height: 4px; }
.dept-foot { margin: 6px 0 0; font-size: .74rem; color: var(--muted); }

/* Listado de registros. En pantalla ancha es una rejilla alineada; al
   estrecharse cada fila se apila sola, sin scroll horizontal. */
.rec-table { display: flex; flex-direction: column; }
.rec-row {
  display: grid;
  grid-template-columns: 118px 1fr auto auto auto;
  gap: 6px 12px;
  align-items: center;
  padding: 11px 8px;
  border-top: 1px solid var(--line);
  text-decoration: none; color: var(--text);
  font-size: .87rem;
}
.rec-row:hover { background: var(--surface-2); }
.rec-code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .76rem; color: var(--muted); white-space: nowrap; }
.rec-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rec-title em { color: var(--muted); font-style: normal; font-size: .78rem; margin-left: 8px; }
.rec-type { font-size: .76rem; color: var(--muted); white-space: nowrap; }
.rec-owner { font-size: .76rem; color: var(--muted); white-space: nowrap; text-align: right; }

@media (max-width: 720px) {
  .rec-row { grid-template-columns: 1fr auto; }
  .rec-code { grid-column: 1; }
  .rec-owner { grid-column: 2; grid-row: 1; }
  .rec-title { grid-column: 1 / -1; white-space: normal; }
  .rec-type  { grid-column: 1 / -1; }
}

/* Actividad de un registro */
.ev-list { list-style: none; margin: 0; padding: 0; }
.ev-list li {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding: 9px 0; border-top: 1px solid var(--line); font-size: .85rem;
}
.ev-note { flex: 1 1 240px; }
.ev-list .meta { color: var(--muted); font-size: .74rem; margin-left: auto; }

.row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }
@media (max-width: 640px) { .row3 { grid-template-columns: 1fr; } }
.chk { display: inline-flex; align-items: center; gap: 6px; font-size: .82rem; color: var(--muted); white-space: nowrap; }

/* ---------------------------------------------------- control documental */

/* Distintivo de estado. Un solo componente para el listado, la ficha y el
   histórico, para que el mismo estado se vea siempre igual. */
.st {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: .68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .03em;
  white-space: nowrap;
  vertical-align: middle;
}
.st-borrador { background: var(--surface-2); color: var(--muted); }
.st-revision { background: var(--warn-soft); color: var(--warn-ink); }
.st-aprobado { background: var(--ok-soft);   color: var(--ok); }
.st-obsoleto { background: var(--surface-2); color: var(--muted); text-decoration: line-through; }
.st-late     { background: var(--accent-soft); color: var(--accent-ink); }

.docs-overdue { display: flex; align-items: center; gap: 8px; margin: 0 0 14px; font-size: .84rem; color: var(--muted); }

.btn-new {
  margin-left: auto;
  padding: 8px 14px;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  font-size: .85rem;
  font-weight: 650;
  white-space: nowrap;
}

/* Panel de gobierno del documento */
.doc-gov {
  margin: 0 0 22px;
  padding: 16px 18px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-2);
}
.doc-gov h3 { margin: 0 0 12px; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.gov-facts { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0 0 14px; align-items: center; }
.gov-facts dt { font-size: .8rem; color: var(--muted); }
.gov-facts dd { margin: 0; font-size: .86rem; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.gov-facts select, .gov-facts input[type="date"] {
  padding: 5px 8px; border: 1px solid var(--line); border-radius: 7px;
  background: var(--surface); color: var(--text); font-size: .84rem;
}
form.inline { display: inline; margin: 0; }

.gov-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.gov-actions input[type="text"] {
  flex: 1 1 200px; min-width: 0;
  padding: 7px 10px; border: 1px solid var(--line); border-radius: 7px;
  background: var(--surface); color: var(--text); font-size: .84rem;
}
.btn-ok { background: var(--ok); color: #fff; border: 0; }

.gov-log { margin-top: 14px; font-size: .82rem; }
.gov-log summary { cursor: pointer; color: var(--muted); }
.gov-log ul { list-style: none; margin: 10px 0 0; padding: 0; }
.gov-log li { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 5px 0; border-top: 1px solid var(--line); }
.gov-log .cmt { flex-basis: 100%; color: var(--muted); font-style: italic; }

/* Adjuntos */
.doc-files { margin-top: 26px; }
.doc-files h3 { margin: 0 0 12px; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.dropzone {
  position: relative;
  display: flex; align-items: center; justify-content: center;
  padding: 22px; margin-bottom: 12px;
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  color: var(--muted); font-size: .85rem;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.dropzone.over { border-color: var(--accent); background: var(--accent-soft); }
.dropzone.busy { opacity: .6; pointer-events: none; }
/* El <input file> tapa la zona para que valga sin JavaScript. */
.dropzone input[type="file"] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.dz-fallback { margin-left: 12px; }

.file-list { list-style: none; margin: 0; padding: 0; }
.file-list li { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 8px 0; border-top: 1px solid var(--line); font-size: .86rem; }
.file-list a { color: var(--accent); text-decoration: none; font-weight: 600; }
.file-list .meta { color: var(--muted); font-size: .76rem; }
.file-list .empty { color: var(--muted); border: 0; }
.file-list .error { color: var(--accent-ink); }
.btn-link { background: none; border: 0; padding: 0; color: var(--muted); cursor: pointer; font-size: .76rem; text-decoration: underline; margin-left: auto; }

/* Comparación de versiones */
.doc-diff { margin-top: 26px; }
.doc-diff h3 { margin: 0 0 10px; font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.doc-diff pre {
  background: var(--code-bg); border-radius: 8px; padding: 14px;
  overflow-x: auto; font-size: .8rem; line-height: 1.5; margin-bottom: 12px;
}
.doc-diff .d-same { color: var(--code-fg); opacity: .55; }
.doc-diff .d-del  { color: #ff8a8a; background: rgba(255,80,80,.10); display: block; }
.doc-diff .d-add  { color: #7ee2a8; background: rgba(60,220,130,.10); display: block; }

/* Alta de documentos */
.doc-edit label { display: block; margin-bottom: 14px; }
.doc-edit label > span { display: block; font-size: .78rem; color: var(--muted); margin-bottom: 5px; }
.row2 { display: grid; grid-template-columns: 2fr 1fr; gap: 14px; }
@media (max-width: 560px) { .row2 { grid-template-columns: 1fr; } }

/* Cabecera cuando se está viendo una estantería heredada de BookStack. */
.docs-shelf {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 10px;
  margin: 0 0 14px;
  font-weight: 650;
}
.docs-shelf .meta { color: var(--muted); font-weight: 400; font-size: .8rem; }
.docs-shelf a { margin-left: auto; color: var(--accent); text-decoration: none; font-size: .8rem; font-weight: 600; }

/* Anterior / siguiente dentro del libro. Tres columnas: los extremos ceden
   sitio al contador del centro, que nunca se parte. */
.doc-nav {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 12px;
  align-items: center;
  margin-top: 26px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}
.doc-nav a {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;                 /* deja que el título se recorte */
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text);
  background: var(--surface);
  transition: border-color .15s, background .15s;
}
.doc-nav a:hover { border-color: var(--accent); background: var(--surface-2); }
.doc-nav-next { text-align: right; align-items: flex-end; }
.doc-nav-dir { font-size: .72rem; color: var(--accent); font-weight: 700; text-transform: uppercase; letter-spacing: .03em; }
.doc-nav-title {
  font-size: .86rem;
  line-height: 1.35;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.doc-nav-pos { font-size: .75rem; color: var(--muted); white-space: nowrap; }

@media (max-width: 640px) {
  /* En móvil el contador estorba entre los dos botones: va debajo. */
  .doc-nav { grid-template-columns: 1fr 1fr; }
  .doc-nav-pos { grid-row: 2; grid-column: 1 / -1; text-align: center; }
  .doc-nav-title { white-space: normal; }
}

.doc-history { margin-top: 26px; }
.doc-history h3, .panel h3, .kpi-entry h2 {
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin: 0 0 10px;
  font-weight: 700;
}
.doc-history ul { list-style: none; margin: 0; padding: 0; }
.doc-history li {
  display: flex;
  gap: 12px;
  align-items: baseline;
  flex-wrap: wrap;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-size: .85rem;
}
.doc-history a { color: var(--accent); text-decoration: none; font-weight: 600; }
.doc-history .meta { color: var(--muted); font-size: .75rem; }

/* ==========================================================================
   KPIs
   ========================================================================== */

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(185px, 1fr));
  gap: 12px;
  margin-bottom: 28px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
}
.kpi-card h3 {
  margin: 0 0 10px;
  font-size: .71rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  font-weight: 700;
  line-height: 1.35;
}
.kpi-value { font-size: 1.65rem; font-weight: 700; line-height: 1.1; letter-spacing: -.02em; }
.kpi-delta { font-size: .77rem; font-weight: 600; margin-top: 4px; }
.kpi-delta.good { color: var(--ok); }
.kpi-delta.bad { color: var(--accent); }
.kpi-delta.muted { color: var(--muted); font-weight: 400; }
.kpi-card .spark { width: 100%; height: 30px; margin-top: 10px; color: var(--accent); display: block; }
.kpi-period { font-size: .69rem; color: var(--muted); margin-top: 6px; }

/* ==========================================================================
   Formularios y paneles
   ========================================================================== */

.inline-form, .inline-tight { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.inline-tight { gap: 16px; }

.inline-form input[type=text],
.inline-form input[type=email],
.inline-form input[type=month],
.inline-form select {
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: inherit;
  font-size: .87rem;
  background: var(--surface);
  color: var(--text);
}
.inline-form input[type=text] { min-width: 160px; flex: 1; }
.inline-form button {
  padding: 9px 18px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  font-size: .87rem;
}
.inline-form button:hover { background: var(--accent-ink); }

.card-form {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px 24px;
}
.card-form label {
  display: block;
  font-size: .72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  margin: 16px 0 5px;
}
.card-form label:first-child { margin-top: 0; }
.card-form input[type=text],
.card-form input[type=date],
.card-form select,
.card-form textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: inherit;
  font-size: .92rem;
  background: var(--bg);
  color: var(--text);
}
.card-form textarea { resize: vertical; line-height: 1.55; }
.card-form .two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.card-form .row { display: flex; gap: 10px; align-items: center; margin-top: 18px; }
.card-form button {
  padding: 10px 22px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.card-form button:hover { background: var(--accent-ink); }

.danger-row { margin-top: 20px; }
.btn-danger {
  background: none;
  border: 1px solid var(--accent);
  color: var(--accent);
  border-radius: 8px;
  padding: 8px 16px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  font-size: .84rem;
}
.btn-danger:hover { background: var(--accent); color: #fff; }

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-top: 20px;
}

.member-list { list-style: none; margin: 0 0 14px; padding: 0; }
.member-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-size: .89rem;
}
.member-list li:last-child { border-bottom: 0; }

.link-btn {
  background: none;
  border: 0;
  color: var(--accent);
  font-weight: 600;
  font-size: .81rem;
  cursor: pointer;
  font-family: inherit;
  padding: 2px 0;
}
.link-btn:hover { text-decoration: underline; }
.link-btn.danger { color: var(--accent); opacity: .85; }

.timeline { list-style: none; margin: 16px 0 0; padding: 0; }
.timeline li { padding: 10px 0 10px 14px; border-left: 2px solid var(--line); }
.timeline li:hover { border-left-color: var(--accent); }
.timeline p { margin: 0 0 3px; font-size: .89rem; line-height: 1.55; }
.timeline .meta { font-size: .72rem; color: var(--muted); }

.badge {
  display: inline-block;
  font-size: .67rem;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 20px;
}
.badge.s-planning    { background: var(--surface-2); color: var(--muted); }
.badge.s-in-progress { background: var(--accent-soft); color: var(--accent-ink); }
.badge.s-on-hold     { background: var(--warn-soft); color: var(--warn-ink); }
.badge.s-completed   { background: var(--ok-soft); color: var(--ok); }

/* ==========================================================================
   Tabla de usuarios
   ========================================================================== */

.grid-scroll { overflow-x: auto; border-radius: var(--radius); }

.grid {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  font-size: .87rem;
}
.grid th {
  text-align: left;
  padding: 10px 12px;
  background: var(--surface-2);
  font-size: .69rem;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
  font-weight: 700;
  white-space: nowrap;
}
.grid td { padding: 8px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.grid tr:last-child td { border-bottom: 0; }
.grid input[type=text], .grid select {
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-family: inherit;
  font-size: .84rem;
  width: 100%;
  background: var(--bg);
  color: var(--text);
}
.grid .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .79rem; }
.grid .center { text-align: center; }
.grid .muted-cell { color: var(--muted); font-size: .77rem; white-space: nowrap; }
.grid .actions { white-space: nowrap; text-align: right; }
.grid .actions .link-btn { margin-left: 12px; }

.hint-small { font-size: .76rem; color: var(--muted); margin: 12px 0 0; }

/* ==========================================================================
   Adaptación a pantallas pequeñas
   ========================================================================== */

@media (max-width: 900px) {
  /* La barra lateral pasa a cajón deslizante, controlado con CSS puro
     mediante el checkbox #navToggle. */
  .app { grid-template-columns: minmax(0, 1fr); }

  .nav {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(280px, 85vw);
    z-index: 40;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: var(--shadow-lg);
  }
  .nav-toggle:checked ~ .nav { transform: none; }

  .nav-scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.45);
    z-index: 30;
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
  }
  .nav-toggle:checked ~ .nav-scrim { opacity: 1; pointer-events: auto; }

  .nav-open, .nav-close {
    display: grid;
    place-items: center;
    cursor: pointer;
    flex: 0 0 auto;
    color: var(--muted);
  }
  .nav-open { width: 32px; height: 32px; margin-left: -6px; }
  .nav-open svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
  .nav-close { margin-left: auto; width: 30px; height: 30px; font-size: .95rem; border-radius: 7px; }
  .nav-close:hover { background: var(--surface-2); color: var(--text); }

  .pagehead { padding: 0 14px; }
  .messages { padding: 20px 16px; }
  .composer-wrap { padding: 10px 16px 12px; }
  .docs-wrap { padding: 20px 16px 70px; }
}

@media (max-width: 700px) {
  .doc-body { padding: 18px 16px; }
  .page-title, .doc-head h1 { font-size: 1.15rem; }
  .doc-head { flex-wrap: wrap; }
  .doc-head h1 { flex: 1 1 100%; }

  .docs-filters { flex-direction: column; align-items: stretch; }
  .docs-filters input[type=search], .docs-filters select { min-width: 0; width: 100%; }

  .inline-form { flex-direction: column; align-items: stretch; }
  .inline-form input, .inline-form select, .inline-form button { width: 100%; }
  .inline-tight { flex-direction: row; }

  .card-form .two-col { grid-template-columns: 1fr; }
  .card-form { padding: 18px 16px; }
}

@media (max-width: 760px) {
  /* La tabla de usuarios pasa a fichas: seis columnas no se editan
     cómodamente desplazándose en horizontal. */
  .grid-scroll { overflow-x: visible; }
  .grid, .grid tbody, .grid tr, .grid td { display: block; width: 100%; }
  .grid { border: 0; background: none; }
  .grid thead { display: none; }

  .grid tr {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    margin-bottom: 10px;
    padding: 5px 2px;
  }
  .grid td {
    display: flex;
    align-items: center;
    gap: 12px;
    border-bottom: 0;
    padding: 7px 14px;
  }
  .grid td[data-label]::before {
    content: attr(data-label);
    flex: 0 0 38%;
    font-size: .68rem;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--muted);
    font-weight: 700;
  }
  .grid td.center { justify-content: flex-start; }
  .grid td.muted-cell { white-space: normal; }
  .grid input[type=text], .grid select { width: auto; flex: 1; min-width: 0; }
  .grid td.actions {
    justify-content: flex-start;
    gap: 18px;
    border-top: 1px solid var(--line);
    margin-top: 4px;
    padding-top: 10px;
    text-align: left;
  }
  .grid .actions .link-btn { margin-left: 0; }
}

@media (max-width: 380px) {
  .kpi-grid { grid-template-columns: 1fr; }
  .messages { padding: 16px 12px; }
  .composer-wrap { padding: 8px 12px 10px; }
  .composer button { padding: 11px 15px; }
}

@media (prefers-reduced-motion: reduce) {
  .nav, .nav-scrim { transition: none; }
  .messages { scroll-behavior: auto; }
}

/* Fuentes citadas bajo una respuesta */

.sources {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding-left: 2px;
}
.sources-label {
  font-size: .67rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  font-weight: 700;
}
.sources a {
  font-size: .76rem;
  color: var(--muted);
  text-decoration: none;
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 3px 10px;
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sources a:hover { border-color: var(--accent); color: var(--accent); }

.gloss-note {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  font-size: .74rem;
  color: var(--muted);
  margin: -8px 0 16px;
}
.gloss-note span {
  background: var(--surface-2);
  border-radius: 20px;
  padding: 2px 9px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .72rem;
}
