/* ============================================================
   LA MESA — sistema visual (skill diseno-app-movil, de Racha/Casa)

   0. Ningun color a mano: todo sale de tokens.
   1. Cero bordes. Separan el espacio y la luminosidad. Unica linea: el
      pelo --hair DENTRO de una lista.
   2. Capas: --l0 fondo · --l1 superficie · --l2 hoja · --l3 hundida/pulsada.
   3. Un dato manda por pantalla; el resto, mas pequeno y mas gris.
   4. --brass es RELLENO, nunca texto (sobre papel da 1,96:1): el texto en
      laton es --brass-txt.
   5. Un estado nunca es solo color: siempre lleva palabra.
   6. Nada de style= en innerHTML (la CSP lo ignora): la geometria dinamica
      va por CSSOM o por clases.
   ============================================================ */

:root {
  /* Papel (claro) */
  --l0: #EFEAE0;
  --l1: #F8F5ED;
  --l2: #FFFDF8;
  --l3: #E6E0D2;
  --tinta: #171410;
  --suave: #5C564A;
  --hair: rgba(28, 24, 16, .10);
  --brass: #C8A45C;
  --brass-txt: #7A5A1E;
  --aviso: #9A3412;
  --ok: #2F6B3A;
  --sombra: rgba(40, 28, 10, .16);
  --velo: rgba(18, 14, 8, .42);

  --serif: "Iowan Old Style", "Charter", "Palatino", "Georgia", serif;
  --sans: -apple-system, "SF Pro Text", system-ui, "Helvetica Neue", sans-serif;
  --letra: 20px;
  --curva: cubic-bezier(.22, 1, .36, 1);
  color-scheme: light;
}
@media (min-width: 700px) { :root { --letra: 22px; } }

/* Tinta (oscuro): el del sistema salvo que se haya elegido uno */
@media (prefers-color-scheme: dark) {
  :root:not([data-tema="claro"]) {
    --l0: #121110; --l1: #1B1A18; --l2: #252321; --l3: #2F2C29;
    --tinta: #ECE7DB; --suave: #A29B8D; --hair: rgba(237, 231, 219, .09);
    --brass-txt: #D4B26A; --aviso: #F0906B; --ok: #8CC79A;
    --sombra: rgba(0, 0, 0, .5); --velo: rgba(0, 0, 0, .6);
    color-scheme: dark;
  }
}
:root[data-tema="oscuro"] {
  --l0: #121110; --l1: #1B1A18; --l2: #252321; --l3: #2F2C29;
  --tinta: #ECE7DB; --suave: #A29B8D; --hair: rgba(237, 231, 219, .09);
  --brass-txt: #D4B26A; --aviso: #F0906B; --ok: #8CC79A;
  --sombra: rgba(0, 0, 0, .5); --velo: rgba(0, 0, 0, .6);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none;
  background: var(--l0); color: var(--tinta);
  font-family: var(--sans); font-size: 16px; line-height: 1.4;
  -webkit-text-size-adjust: 100%; text-size-adjust: 100%;
}
body { position: fixed; inset: 0; }
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button { border: 0; background: none; padding: 0; cursor: pointer; touch-action: manipulation; }
h1, h2, h3, h4, p, ul, ol { margin: 0; }
svg.sprite { position: absolute; width: 0; height: 0; }
svg.i { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.suave { color: var(--suave); }

#app {
  height: var(--vh, 100dvh);
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
  max-width: 760px; margin: 0 auto;
  position: relative;
}
.vista { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* ---------- botones y campos ---------- */
.icono {
  width: 44px; height: 44px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  transition: transform .18s var(--curva), background .18s;
}
.icono:active, .btn:active, .fila:active { transform: scale(.96); }
.icono:active { background: var(--l3); }
.btn {
  min-height: 48px; padding: 0 22px; border-radius: 14px;
  background: var(--tinta); color: var(--l0); font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  transition: transform .18s var(--curva);
}
.btn.sec { background: var(--l3); color: var(--tinta); font-weight: 500; }
.btn.bloque { width: 100%; }
.btn[disabled] { opacity: .5; }
.campo {
  width: 100%; min-height: 48px; padding: 12px 14px; border: 0; border-radius: 12px;
  background: var(--l1); color: var(--tinta); font-size: 17px; outline: 0;
}
.campo::placeholder { color: var(--suave); opacity: .8; }
.campo:focus-visible, .btn:focus-visible, .icono:focus-visible, .fila:focus-visible, .seg button:focus-visible {
  outline: 2px solid var(--brass); outline-offset: 2px;
}
.hoja .campo { background: var(--l0); }

/* ---------- cabeceras ---------- */
.cab { display: flex; align-items: center; gap: 4px; padding: 10px 10px 6px 18px; }
.cab h1 { font-family: var(--serif); font-size: 30px; line-height: 1.1; font-weight: 600; letter-spacing: -.01em; }
.cab .grupo { flex: 1; min-width: 0; }
.cab .suave { font-size: 14px; margin-top: 2px; font-variant-numeric: tabular-nums; }

/* estado: punto + palabra (nunca solo color) */
.estado { display: inline-flex; align-items: center; gap: 7px; font-size: 14px; color: var(--suave); white-space: nowrap; }
.estado::before { content: ""; width: 9px; height: 9px; border-radius: 50%; background: var(--suave); flex: none; }
.estado[data-s="subido"] { color: var(--tinta); }
.estado[data-s="subido"]::before { background: var(--ok); }
.estado[data-s="guardado"]::before { background: var(--brass); }
.estado[data-s="sinred"]::before { background: transparent; box-shadow: inset 0 0 0 2px var(--suave); }
.estado[data-s="conflicto"], .estado[data-s="error"] { color: var(--aviso); font-weight: 600; }
.estado[data-s="conflicto"]::before, .estado[data-s="error"]::before { background: var(--aviso); }

/* ---------- avisos ---------- */
.avisos { padding: 0 14px; display: flex; flex-direction: column; gap: 8px; }
.aviso {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-radius: 14px;
  background: var(--l1); font-size: 15px; line-height: 1.35; margin-top: 6px;
}
.aviso.mal { background: var(--aviso); color: var(--l0); }
.aviso .txt { flex: 1; min-width: 0; }
.aviso .btn { min-height: 44px; padding: 0 16px; border-radius: 12px; font-size: 15px; }
.aviso.mal .btn { background: var(--l0); color: var(--aviso); }
.aviso .cerrar { margin: -6px -6px -6px 0; }

/* ---------- lista de capitulos ---------- */
.caps { list-style: none; padding: 8px 14px calc(14px + env(safe-area-inset-bottom)); margin: 0; overflow-y: auto; flex: 1; -webkit-overflow-scrolling: touch; }
.caps .grupo-tit { font-size: 13px; color: var(--suave); padding: 14px 6px 6px; letter-spacing: .02em; }
.caps .bloque { background: var(--l1); border-radius: 18px; overflow: hidden; box-shadow: 0 1px 2px var(--sombra); }
.fila {
  display: grid; grid-template-columns: 2.8em minmax(0, 1fr) auto; align-items: center; column-gap: 8px;
  width: 100%; min-height: 62px; padding: 10px 16px 10px 14px; text-align: left;
  transition: transform .18s var(--curva), background .18s;
}
.fila + .fila { box-shadow: inset 0 1px 0 var(--hair); }
.fila:active { background: var(--l3); }
.fila .n { font-variant-numeric: tabular-nums; color: var(--suave); font-size: 15px; white-space: nowrap; }
.fila .t { font-family: var(--serif); font-size: 19px; line-height: 1.25; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fila .s { font-size: 13px; color: var(--suave); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.fila .p { text-align: right; font-variant-numeric: tabular-nums; font-size: 16px; }
.fila .p small { display: block; font-size: 12px; margin-top: 3px; color: var(--suave); }
.fila .p small.mal { color: var(--aviso); font-weight: 600; }
.fila .p small.pend { color: var(--brass-txt); font-weight: 600; }
.vacio { padding: 40px 28px; text-align: center; color: var(--suave); font-size: 16px; line-height: 1.5; }
.vacio strong { color: var(--tinta); display: block; font-family: var(--serif); font-size: 21px; font-weight: 600; margin-bottom: 8px; }

/* ---------- editor ---------- */
.ed-cab { display: flex; align-items: center; gap: 2px; padding: 6px 8px 0 6px; }
.ed-cab .tit { flex: 1; min-width: 0; }
.ed-cab h2 { font-family: var(--serif); font-size: 18px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ed-cab .sub { font-size: 13px; color: var(--suave); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ed-barra { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 2px 18px 8px; font-size: 14px; color: var(--suave); }
.ed-barra .cuenta { font-variant-numeric: tabular-nums; }
.ed-barra .cuenta b { color: var(--tinta); font-weight: 600; }
#pliego { flex: 1; min-height: 0; position: relative; }
#texto {
  display: block; width: 100%; height: 100%; border: 0; outline: 0; resize: none; margin: 0;
  background: transparent; color: var(--tinta); caret-color: var(--brass-txt);
  font-family: var(--serif); font-size: var(--letra); line-height: 1.7;
  /* una sola columna, ~34 em: unos 65 caracteres por linea */
  padding: 14px max(20px, calc((100% - 34em) / 2)) 32vh;
  hyphens: auto; -webkit-hyphens: auto; text-wrap: pretty;
  -webkit-overflow-scrolling: touch;
}
#texto::placeholder { color: var(--suave); opacity: .7; }
#texto:disabled { opacity: .5; }

/* ---------- idea suelta ---------- */
.idea-barra {
  display: flex; gap: 8px; align-items: center;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom)); background: var(--l0);
  box-shadow: 0 -1px 0 var(--hair);
}
.idea-barra .campo { flex: 1; }
.idea-barra .icono { background: var(--brass); color: #1b1608; width: 48px; height: 48px; }
body.escribiendo .idea-barra { display: none; }
body.escribiendo.idea-abierta .idea-barra { display: flex; }
body.en-arranque .idea-barra { display: none; }

/* ---------- arranque ---------- */
.centrado { flex: 1; overflow-y: auto; display: flex; flex-direction: column; justify-content: center; padding: 28px 24px calc(28px + env(safe-area-inset-bottom)); gap: 16px; }
.centrado .marca { width: 72px; height: 72px; border-radius: 18px; background: #161512; display: block; box-shadow: 0 6px 20px var(--sombra); }
.centrado h1 { font-family: var(--serif); font-size: 32px; font-weight: 600; line-height: 1.12; }
.centrado p { font-size: 17px; line-height: 1.5; color: var(--suave); }
.centrado p strong { color: var(--tinta); }
.centrado .error { color: var(--aviso); font-weight: 600; }
.centrado .form { display: flex; flex-direction: column; gap: 10px; }
.centrado .etiqueta { font-size: 14px; color: var(--suave); margin-bottom: -4px; }
.enlace { color: var(--brass-txt); font-weight: 600; text-align: left; min-height: 44px; display: inline-flex; align-items: center; }

/* ---------- hojas que suben desde abajo ---------- */
#velo {
  position: fixed; inset: 0; background: var(--velo); opacity: 0; pointer-events: none;
  transition: opacity .3s var(--curva); z-index: 20;
  -webkit-backdrop-filter: blur(5px); backdrop-filter: blur(5px);
}
#velo.abierta { opacity: 1; pointer-events: auto; }
.hoja {
  position: fixed; left: 0; right: 0; bottom: 0; margin: 0 auto; z-index: 21;
  width: min(100%, 680px); max-height: calc(var(--vh, 100dvh) - 20px);
  background: var(--l2); border-radius: 24px 24px 0 0;
  box-shadow: 0 -14px 44px var(--sombra);
  display: flex; flex-direction: column;
  transform: translateY(104%); visibility: hidden;
  transition: transform .4s var(--curva), visibility 0s .4s;
  padding-bottom: env(safe-area-inset-bottom); outline: 0;
}
.hoja.abierta { transform: none; visibility: visible; transition: transform .4s var(--curva), visibility 0s 0s; }
.hoja.media { height: min(64vh, 620px); }
.hoja.grande { height: calc(var(--vh, 100dvh) - 20px); }
.hoja.arrastrando { transition: none; }
.asa { height: 26px; display: flex; align-items: center; justify-content: center; touch-action: none; cursor: grab; flex: none; }
.asa span { width: 40px; height: 5px; border-radius: 3px; background: var(--suave); opacity: .45; }
.h-cab { display: flex; align-items: center; gap: 6px; padding: 0 8px 4px 22px; flex: none; touch-action: none; }
.h-cab h2 { flex: 1; font-family: var(--serif); font-size: 22px; font-weight: 600; line-height: 1.2; min-width: 0; }
.h-cuerpo { overflow-y: auto; flex: 1; min-height: 0; padding: 6px 22px 26px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }

.seg { display: flex; gap: 4px; padding: 4px; background: var(--l0); border-radius: 13px; margin: 6px 0 14px; }
.seg button { flex: 1; min-height: 40px; border-radius: 10px; font-size: 15px; color: var(--suave); transition: background .2s, color .2s; }
.seg button[aria-selected="true"], .seg button[aria-pressed="true"] { background: var(--tinta); color: var(--l0); font-weight: 600; }

/* texto de lectura (markdown) */
.lectura { font-family: var(--serif); font-size: 17.5px; line-height: 1.62; overflow-wrap: anywhere; }
.lectura h1, .lectura h2, .lectura h3, .lectura h4 { font-family: var(--serif); line-height: 1.25; margin: 1.3em 0 .45em; }
.lectura h1 { font-size: 24px; margin-top: .2em; } .lectura h2 { font-size: 21px; } .lectura h3 { font-size: 18.5px; } .lectura h4 { font-size: 17.5px; font-style: italic; }
.lectura p { margin: .6em 0; }
.lectura ul, .lectura ol { padding-left: 1.3em; margin: .6em 0; }
.lectura li { margin: .3em 0; } .lectura li.sangra { margin-left: 1.1em; list-style: circle; }
.lectura blockquote { margin: .8em 0; padding: .1em 1em; box-shadow: inset 3px 0 0 var(--brass); color: var(--suave); }
.lectura hr { border: 0; height: 1px; background: var(--hair); margin: 1.4em 0; }
.lectura code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .85em; background: var(--l0); padding: .1em .35em; border-radius: 5px; }
.lectura a { color: var(--brass-txt); text-underline-offset: 3px; }
.lectura .tabla { overflow-x: auto; margin: .8em 0; }
.lectura table { border-collapse: collapse; font-family: var(--sans); font-size: 14.5px; min-width: 100%; }
.lectura th, .lectura td { padding: 7px 10px; text-align: left; vertical-align: top; box-shadow: inset 0 -1px 0 var(--hair); }
.lectura th { font-weight: 600; white-space: nowrap; }
.sin-doc { font-family: var(--sans); color: var(--suave); font-size: 16px; line-height: 1.5; padding: 22px 4px; }

/* conflicto */
.nota-h { font-size: 16px; line-height: 1.5; color: var(--suave); margin: 4px 0 14px; }
.nota-h strong { color: var(--tinta); }
.opcion { background: var(--l0); border-radius: 16px; padding: 14px 16px 16px; margin-bottom: 12px; }
.opcion h3 { font-size: 17px; font-weight: 600; }
.opcion .meta { font-size: 14px; color: var(--suave); margin: 2px 0 10px; font-variant-numeric: tabular-nums; }
.opcion .trozo { font-family: var(--serif); font-size: 16px; line-height: 1.5; margin: 0 0 12px; overflow-wrap: anywhere; white-space: pre-wrap; }
.opcion details { margin: -4px 0 12px; }
.opcion summary { color: var(--brass-txt); font-weight: 600; font-size: 15px; min-height: 36px; display: flex; align-items: center; cursor: pointer; }
.opcion details .entero { font-family: var(--serif); font-size: 16px; line-height: 1.55; white-space: pre-wrap; max-height: 40vh; overflow-y: auto; overflow-wrap: anywhere; padding: 8px 0; }
.opcion .btn { width: 100%; }

/* ajustes */
.bloque-aj { margin-bottom: 20px; }
.bloque-aj h3 { font-size: 13px; color: var(--suave); font-weight: 600; letter-spacing: .03em; margin: 0 0 8px; text-transform: uppercase; }
.bloque-aj p { font-size: 15px; color: var(--suave); line-height: 1.45; margin: 8px 0; }
.bloque-aj .btn { margin-top: 6px; }
.fila-aj { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 4px 0; min-height: 48px; }
.fila-aj .btn { margin: 0; min-height: 44px; padding: 0 16px; border-radius: 12px; font-size: 15px; }
.enlace-caja { background: var(--l0); border-radius: 12px; padding: 12px 14px; font-size: 14px; line-height: 1.4; word-break: break-all; font-family: ui-monospace, "SF Mono", Menlo, monospace; user-select: all; -webkit-user-select: all; }
.letra-fila { display: flex; align-items: center; gap: 10px; }
.letra-fila .valor { min-width: 4em; text-align: center; font-variant-numeric: tabular-nums; }
.letra-fila .btn { min-width: 52px; padding: 0 12px; }

/* toast */
#toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); z-index: 40;
  transform: translate(-50%, 12px); opacity: 0; pointer-events: none;
  background: var(--tinta); color: var(--l0); padding: 11px 18px; border-radius: 14px;
  font-size: 15px; max-width: min(92vw, 460px); text-align: center;
  transition: opacity .25s var(--curva), transform .25s var(--curva);
}
#toast.ver { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
.sin-transicion, .sin-transicion * { transition: none !important; }
