:root {
  --barra: #f4f3ef;
  --grupo: #e8e6df;
  --borde: #dcd9d0;
  --texto: #26252b;
  --suave: #75726a;
  --activo: #2b59c3;
  --activo-texto: #fff;
  --peligro: #c62828;
  --fondo: #f4f3ef;      /* fondo de la biblioteca */
  --mesa: #e4e2db;       /* detrás de las páginas */
  --panel: #fff;         /* diálogos */
  --campo: #fff;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --barra: #1c1c20;
    --grupo: #2a2a30;
    --borde: #34343a;
    --texto: #ececf0;
    --suave: #9c9ba3;
    --activo: #6b8cff;
    --activo-texto: #0f1222;
    --peligro: #ff6b6b;
    --fondo: #17171a;
    --mesa: #121214;
    --panel: #232328;
    --campo: #1a1a1e;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  background: var(--fondo);
  color: var(--texto);
  font: 15px/1.2 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

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

svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

/* ---------- Barras ---------- */

.barra {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  min-height: 52px;
  padding: 6px max(10px, env(safe-area-inset-right)) 6px max(10px, env(safe-area-inset-left));
  padding-top: max(6px, env(safe-area-inset-top));
  background: var(--barra);
  border-bottom: 1px solid var(--borde);
  overflow-x: auto;
  scrollbar-width: none;
  touch-action: pan-x;
}
.barra::-webkit-scrollbar { display: none; }

.grupo {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
  padding: 2px;
  border-radius: 10px;
  background: var(--grupo);
}

.espacio { flex: 1; }

.barra button {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex-shrink: 0;
  min-width: 40px;
  height: 40px;
  padding: 0 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  font: inherit;
  white-space: nowrap;
  cursor: pointer;
  touch-action: manipulation;
}
.barra button.texto { background: var(--grupo); padding: 0 12px 0 10px; }
.barra button.primario,
.barra button[aria-pressed="true"] { background: var(--activo); color: var(--activo-texto); }
.barra button:disabled { opacity: 0.35; cursor: default; }

/* La barra se desplaza en horizontal, así que el título no necesita encogerse */
.barra .titulo {
  max-width: min(240px, 45vw);
  font-weight: 650;
}
.barra .titulo span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Los colores marcan la selección con un anillo en lugar de fondo */
.barra button.color[aria-pressed="true"] { background: transparent; }
.color::before {
  content: "";
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--c);
  box-shadow: inset 0 0 0 1px rgba(128, 128, 128, 0.6);
}
.color[aria-pressed="true"]::before {
  box-shadow: 0 0 0 2px var(--grupo), 0 0 0 4px var(--c);
}

.tam span {
  width: var(--d);
  height: var(--d);
  border-radius: 50%;
  background: currentColor;
}

/* ---------- Biblioteca ---------- */

.migas {
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
}
.migas h1 {
  margin: 0 6px;
  font-size: 20px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.migas .volver { color: var(--activo); padding-left: 2px; }

.estante {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.rejilla {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  align-items: end;
  gap: 28px 20px;
  padding: 24px max(20px, env(safe-area-inset-right)) 48px max(20px, env(safe-area-inset-left));
}

.ficha {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.ficha .abrir {
  appearance: none;
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  transition: transform 0.12s;
  touch-action: manipulation;
}
.ficha .abrir:active { transform: scale(0.97); }

.pie {
  display: flex;
  align-items: flex-start;
  gap: 4px;
}
.pie .textos { flex: 1; min-width: 0; }
.pie strong {
  display: block;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pie small { color: var(--suave); font-size: 12px; }
.pie .mas {
  width: 32px;
  height: 32px;
  margin: -6px -6px 0 0;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--suave);
  cursor: pointer;
  touch-action: manipulation;
}
.pie .mas svg { fill: currentColor; stroke: none; }

.vacio {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 60px 24px;
  color: var(--suave);
  text-align: center;
}
.vacio p { margin: 0; max-width: 360px; line-height: 1.4; }

/* ---------- Portadas ---------- */

.portada {
  position: relative;
  display: block;
  width: 100%;
  aspect-ratio: 800 / 1131;
  overflow: hidden;
  border-radius: 3px 10px 10px 3px;
  background-color: var(--pc, #2b59c3);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2), 0 6px 16px rgba(0, 0, 0, 0.12);
  container-type: inline-size;
}
/* Lomo */
.portada::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 9%;
  background: rgba(0, 0, 0, 0.18);
  box-shadow: 1px 0 0 rgba(255, 255, 255, 0.15);
}
.portada[data-diseno="rayas"] {
  background-image: repeating-linear-gradient(135deg, rgba(255, 255, 255, 0.14) 0 8px, transparent 8px 16px);
}
.portada[data-diseno="puntos"] {
  background-image: radial-gradient(rgba(255, 255, 255, 0.32) 18%, transparent 21%);
  background-size: 12px 12px;
}
.portada[data-diseno="cuadros"] {
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.14) 50%, transparent 50%),
    linear-gradient(90deg, rgba(255, 255, 255, 0.14) 50%, transparent 50%);
  background-size: 20px 20px;
}
.portada .etiqueta {
  position: absolute;
  top: 18%;
  left: 20%;
  right: 11%;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  padding: 7% 6%;
  border-radius: 6px;
  background: #fffdf8;
  color: #26252b;
  font-size: 13px;
  font-size: max(10px, 9cqw);
  font-weight: 650;
  line-height: 1.15;
  text-align: center;
  overflow-wrap: anywhere;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

/* ---------- Carpetas ---------- */

.carpeta {
  position: relative;
  display: block;
  width: 100%;
  aspect-ratio: 800 / 1131;
}
.carpeta-fondo,
.carpeta-frente {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
}
.carpeta-fondo {
  height: 70%;
  border-radius: 4px 12px 12px 12px;
  background: linear-gradient(rgba(0, 0, 0, 0.25), rgba(0, 0, 0, 0.25)), var(--cc);
}
/* Pestaña */
.carpeta-fondo::before {
  content: "";
  position: absolute;
  left: 0;
  bottom: 100%;
  width: 42%;
  height: 9%;
  border-radius: 8px 8px 0 0;
  background: inherit;
}
.carpeta-hojas {
  position: absolute;
  left: 9%;
  right: 9%;
  bottom: 20%;
}
.carpeta-hojas .mini {
  position: absolute;
  bottom: 0;
  width: 58%;
  aspect-ratio: 800 / 1131;
  border-radius: 2px 5px 5px 2px;
  background: var(--pc);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.carpeta-hojas .mini:nth-child(1) { left: 0; transform: rotate(-7deg); }
.carpeta-hojas .mini:nth-child(2) { left: 21%; transform: rotate(-1deg); }
.carpeta-hojas .mini:nth-child(3) { left: 42%; transform: rotate(6deg); }
.carpeta-frente {
  height: 52%;
  border-radius: 10px;
  background: linear-gradient(rgba(255, 255, 255, 0.12), rgba(255, 255, 255, 0)), var(--cc);
  box-shadow: 0 -1px 0 rgba(255, 255, 255, 0.25) inset, 0 6px 16px rgba(0, 0, 0, 0.15);
}

/* ---------- Cuaderno ---------- */

.hoja {
  position: relative;
  flex: 1;
  overflow: hidden;
  background: var(--mesa);
  touch-action: none;
}

.hoja canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.indicador {
  position: absolute;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  height: 36px;
  min-width: 64px;
  padding: 0 14px;
  border: 0;
  border-radius: 999px;
  background: rgba(38, 37, 43, 0.82);
  color: #fff;
  font: 600 14px/1 system-ui, -apple-system, sans-serif;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

/* ---------- Diálogos ---------- */

.dialogo {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: var(--panel);
  color: var(--texto);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.dialogo.estrecho { width: min(380px, calc(100vw - 32px)); }
.dialogo::backdrop { background: rgba(0, 0, 0, 0.35); }

.dialogo form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 20px;
}
.dialogo h2 { margin: 0; font-size: 18px; }
.dialogo p { margin: -8px 0 0; color: var(--suave); line-height: 1.4; }

.dialogo-cuerpo { display: flex; gap: 20px; }
.dialogo .previa { width: 130px; flex-shrink: 0; align-self: flex-start; }
.campos { display: flex; flex-direction: column; gap: 14px; flex: 1; min-width: 0; }

@media (max-width: 520px) {
  .dialogo-cuerpo { flex-direction: column; align-items: center; }
  .dialogo .previa { width: 96px; align-self: center; }
  .campos { width: 100%; }
}

.campo {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--suave);
  font-size: 13px;
}
.campo input {
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--borde);
  border-radius: 10px;
  background: var(--campo);
  color: var(--texto);
  font: inherit;
  font-size: 16px;  /* con menos, iOS hace zoom al enfocar */
  -webkit-user-select: text;
  user-select: text;
}
.campo input:focus { outline: 2px solid var(--activo); outline-offset: -1px; }

fieldset { margin: 0; padding: 0; border: 0; }
legend { margin-bottom: 6px; padding: 0; color: var(--suave); font-size: 13px; }

.muestras, .opciones { display: flex; flex-wrap: wrap; gap: 8px; }
.muestra, .opcion { position: relative; cursor: pointer; }
.muestra input, .opcion input { position: absolute; opacity: 0; pointer-events: none; }
.muestra span {
  display: block;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--c);
}
.muestra input:checked + span { box-shadow: 0 0 0 2px var(--panel), 0 0 0 4px var(--c); }
.opcion span {
  display: inline-block;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--grupo);
}
.opcion input:checked + span { background: var(--activo); color: var(--activo-texto); }
.muestra input:focus-visible + span,
.opcion input:focus-visible + span { outline: 2px solid var(--activo); outline-offset: 3px; }

.dialogo footer { display: flex; justify-content: flex-end; gap: 8px; }

.boton {
  height: 44px;
  padding: 0 16px;
  border: 0;
  border-radius: 10px;
  background: var(--grupo);
  color: inherit;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}
.boton.primario { background: var(--activo); color: var(--activo-texto); }
.boton.peligro { color: var(--peligro); }
.boton:disabled { opacity: 0.4; cursor: default; }

.acciones .lista { display: flex; flex-direction: column; gap: 6px; }
.acciones .lista .boton { text-align: left; font-weight: 500; }

/* ---------- Aviso ---------- */

#aviso {
  position: fixed;
  left: 50%;
  bottom: calc(max(16px, env(safe-area-inset-bottom)) + 48px);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 999px;
  background: #26252b;
  color: #fff;
  font-size: 14px;
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transform: translate(-50%, 16px);
  transition: opacity 0.25s, transform 0.25s;
  z-index: 10;
}
#aviso.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}
