/* ===== Tema ===== */
:root {
  --bg: #faf7fb;
  --bg-2: #ffffff;
  --ink: #2b2233;
  --muted: #8a7f96;
  --line: #ece5f0;
  --brand: #7c3aed;
  --brand-2: #db2777;
  --brand-soft: #f3e8ff;
  --spicy: #e11d48;
  --drama: #7c3aed;
  --star: #f59e0b;
  --shadow: 0 6px 24px rgba(76, 29, 149, 0.10);
  --radius: 16px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 500px at 100% -10%, #fce7f3 0%, transparent 60%),
    radial-gradient(1000px 500px at -10% 0%, #ede9fe 0%, transparent 55%),
    var(--bg);
  min-height: 100vh;
  overflow-x: hidden;
}
/* Áreas seguras para móviles con notch */
.topbar { padding-top: env(safe-area-inset-top); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--spicy); font-size: 0.9rem; }

/* ===== Botones ===== */
.btn {
  border: 0; cursor: pointer; font: inherit; font-weight: 600;
  padding: 0.6rem 1rem; border-radius: 999px; transition: transform .05s ease, box-shadow .2s, background .2s;
  min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: 0.35rem;
  touch-action: manipulation;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; box-shadow: var(--shadow); }
.btn-primary:hover { box-shadow: 0 8px 28px rgba(219, 39, 119, 0.28); }
.btn-ghost { background: transparent; color: var(--ink); border: 1px solid var(--line); }
.btn-ghost:hover { background: #fff; }
.btn-block { width: 100%; }
.btn-danger { background: #fee2e2; color: #b91c1c; }
.link-btn { background: none; border: 0; color: var(--brand-2); cursor: pointer; font: inherit; text-decoration: underline; padding: 0; }

/* ===== Login ===== */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.login-card {
  background: var(--bg-2); border-radius: 22px; box-shadow: var(--shadow);
  padding: 2.2rem; width: 100%; max-width: 360px; text-align: center;
  border: 1px solid var(--line);
}
.login-emoji { font-size: 3rem; }
.login-card h1 { margin: 0.3rem 0 0.2rem; font-size: 1.6rem; }
.login-card input {
  width: 100%; margin: 1.1rem 0 0.9rem; padding: 0.8rem 1rem;
  border: 1px solid var(--line); border-radius: 12px; font: inherit; background: #fff;
}
.login-card input:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }

/* ===== Topbar ===== */
.topbar { position: sticky; top: 0; z-index: 20; background: rgba(255,255,255,0.82); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.topbar-inner { max-width: 1100px; margin: 0 auto; padding: 0.8rem 1.2rem; display: flex; align-items: center; gap: 0.8rem; flex-wrap: wrap; }
.brand { font-weight: 800; font-size: 1.2rem; background: linear-gradient(135deg, var(--brand), var(--brand-2)); -webkit-background-clip: text; background-clip: text; color: transparent; white-space: nowrap; }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; }
#search { padding: 0.55rem 0.9rem; border: 1px solid var(--line); border-radius: 999px; font: inherit; background: #fff; min-width: 220px; }
#search:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
.stats-bar { padding-top: 0; gap: 1.4rem; font-size: 0.85rem; color: var(--muted); }
.stats-bar strong { color: var(--ink); }

/* ===== Grid de libros ===== */
.container { max-width: 1100px; margin: 0 auto; padding: 1.4rem 1.2rem 4rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 1.2rem; }

.card {
  background: var(--bg-2); border-radius: var(--radius); overflow: hidden; cursor: pointer;
  box-shadow: var(--shadow); border: 1px solid var(--line); display: flex; flex-direction: column;
  transition: transform .12s ease, box-shadow .2s;
}
.card:hover { transform: translateY(-4px); box-shadow: 0 14px 34px rgba(124, 58, 237, 0.18); }
.card-cover { aspect-ratio: 2/3; background: linear-gradient(135deg, #ede9fe, #fce7f3); position: relative; overflow: hidden; }
.card-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card-cover .placeholder { position: absolute; inset: 0; display: grid; place-items: center; font-size: 2.4rem; opacity: .5; }
.card-badges { position: absolute; top: 8px; left: 8px; right: 8px; display: flex; gap: 6px; flex-wrap: wrap; }
.badge { font-size: 0.7rem; font-weight: 700; padding: 3px 8px; border-radius: 999px; background: rgba(255,255,255,0.92); box-shadow: 0 2px 6px rgba(0,0,0,.08); }
.badge-spicy { color: var(--spicy); }
.badge-saga { color: var(--brand); }
.card-body { padding: 0.7rem 0.8rem 0.9rem; display: flex; flex-direction: column; gap: 0.35rem; }
.card-title { font-weight: 700; font-size: 0.98rem; line-height: 1.2; }
.card-author { font-size: 0.82rem; color: var(--muted); }
.card-ratings { display: flex; gap: 0.6rem; font-size: 0.78rem; margin-top: 0.15rem; flex-wrap: wrap; }
.card-ratings span { display: inline-flex; align-items: center; gap: 2px; }

/* ===== Estado vacío ===== */
.empty { text-align: center; padding: 4rem 1rem; }
.empty-emoji { font-size: 3.5rem; }

/* ===== Modales ===== */
.modal { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 1rem; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(43, 34, 51, 0.45); backdrop-filter: blur(2px); }
.modal-panel {
  position: relative; background: var(--bg-2); border-radius: 20px; box-shadow: 0 20px 60px rgba(0,0,0,.3);
  width: 100%; max-width: 460px; max-height: 90vh; overflow-y: auto; padding: 1.6rem;
  -webkit-overflow-scrolling: touch;
}
.modal-lg { max-width: 720px; }
.modal-close { position: absolute; top: 12px; right: 14px; border: 0; background: #f3f0f6; width: 32px; height: 32px; border-radius: 50%; cursor: pointer; font-size: 0.9rem; color: var(--muted); }
.modal-close:hover { background: var(--line); }

/* ===== Formularios ===== */
.form { display: flex; flex-direction: column; gap: 0.9rem; margin-top: 0.8rem; }
.form label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.85rem; font-weight: 600; color: var(--ink); }
.form input[type=text], .form input[type=date], .form input[type=password], .form textarea, .form select {
  padding: 0.7rem 0.75rem; border: 1px solid var(--line); border-radius: 10px; background: #fff; font-weight: 400;
  font-family: var(--font); font-size: 16px; /* 16px evita el zoom automático de iOS al enfocar */
  width: 100%;
}
.form input:focus, .form textarea:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
.form textarea { resize: vertical; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 0.8rem; }
.check { flex-direction: row !important; align-items: center; gap: 0.5rem !important; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--brand); }
.form-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 0.4rem; }

/* ===== Subida de imagen ===== */
.cover-uploader { display: flex; flex-direction: column; align-items: center; gap: 0.4rem; }
.img-drop {
  width: 130px; height: 190px; border: 2px dashed var(--line); border-radius: 12px; cursor: pointer;
  display: grid; place-items: center; overflow: hidden; background: #fbf9fc; position: relative; text-align: center;
}
.img-drop-round { width: 120px; height: 120px; border-radius: 50%; }
.img-drop:hover { border-color: var(--brand); }
.img-drop img { width: 100%; height: 100%; object-fit: cover; }
.img-drop-hint { color: var(--muted); font-size: 0.8rem; font-weight: 600; }

/* ===== Estrellas ===== */
.ratings { display: flex; gap: 1.2rem; flex-wrap: wrap; padding: 0.4rem 0; }
.rating-block { display: flex; flex-direction: column; gap: 0.25rem; }
.rating-label { font-size: 0.78rem; font-weight: 700; color: var(--muted); }
.stars { display: flex; gap: 4px; font-size: 1.6rem; cursor: pointer; }
.stars .star {
  filter: grayscale(1) opacity(0.35); transition: filter .1s, transform .1s; user-select: none;
  padding: 4px; line-height: 1; touch-action: manipulation;
}
.stars .star.on { filter: none; }
.stars .star:hover { transform: scale(1.15); }

/* ===== Ficha de detalle ===== */
.detail-head { display: flex; gap: 1.4rem; flex-wrap: wrap; }
.detail-cover { width: 160px; aspect-ratio: 2/3; border-radius: 12px; overflow: hidden; flex-shrink: 0; background: linear-gradient(135deg, #ede9fe, #fce7f3); box-shadow: var(--shadow); }
.detail-cover img { width: 100%; height: 100%; object-fit: cover; }
.detail-cover .placeholder { width:100%; height:100%; display:grid; place-items:center; font-size:2.5rem; opacity:.5; }
.detail-info { flex: 1; min-width: 220px; }
.detail-info h2 { margin: 0 0 0.2rem; font-size: 1.5rem; }
.detail-author { color: var(--muted); font-size: 1rem; margin-bottom: 0.6rem; }
.detail-badges { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-bottom: 0.8rem; }
.detail-meta { font-size: 0.9rem; color: var(--ink); display: grid; gap: 0.3rem; margin-bottom: 0.8rem; }
.detail-meta .k { color: var(--muted); margin-right: 0.4rem; }
.detail-ratings { display: flex; gap: 1.2rem; flex-wrap: wrap; margin: 0.6rem 0; }
.detail-ratings .rb { text-align: center; }
.detail-ratings .rb .lbl { font-size: 0.72rem; color: var(--muted); font-weight: 700; display:block; margin-bottom: 2px;}
.detail-ratings .rb .val { font-size: 1.1rem; letter-spacing: 1px; }
.detail-notes { background: #fbf9fc; border: 1px solid var(--line); border-radius: 12px; padding: 0.8rem 1rem; font-size: 0.92rem; white-space: pre-wrap; margin-top: 0.4rem; }
.detail-actions { display: flex; gap: 0.5rem; margin-top: 1rem; flex-wrap: wrap; }

.section-title { display: flex; align-items: center; justify-content: space-between; margin: 1.6rem 0 0.8rem; border-top: 1px solid var(--line); padding-top: 1.2rem; }
.section-title h3 { margin: 0; font-size: 1.15rem; }

.char-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0.9rem; }
.char-card { background: #fbf9fc; border: 1px solid var(--line); border-radius: 14px; padding: 0.9rem; text-align: center; position: relative; }
.char-avatar { width: 78px; height: 78px; border-radius: 50%; margin: 0 auto 0.5rem; overflow: hidden; background: linear-gradient(135deg, #ede9fe, #fce7f3); display: grid; place-items: center; font-size: 1.6rem; }
.char-avatar img { width: 100%; height: 100%; object-fit: cover; }
.char-name { font-weight: 700; font-size: 0.95rem; }
.char-field { font-size: 0.78rem; color: var(--muted); margin-top: 0.25rem; line-height: 1.35; }
.char-field b { color: var(--ink); font-weight: 600; }
.char-tools { display: flex; gap: 0.4rem; justify-content: center; margin-top: 0.5rem; }
.icon-btn { border: 1px solid var(--line); background: #fff; border-radius: 8px; cursor: pointer; padding: 0.45rem 0.7rem; font-size: 0.9rem; min-height: 38px; touch-action: manipulation; }
.icon-btn:hover { background: var(--brand-soft); }

/* ===== Botón flotante (FAB) ===== */
.fab {
  display: none; position: fixed; z-index: 30;
  right: calc(18px + env(safe-area-inset-right));
  bottom: calc(18px + env(safe-area-inset-bottom));
  width: 60px; height: 60px; border-radius: 50%; border: 0; cursor: pointer;
  font-size: 2rem; line-height: 1; color: #fff;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  box-shadow: 0 8px 24px rgba(219, 39, 119, 0.4);
  touch-action: manipulation;
}
.fab:active { transform: scale(0.94); }

/* ===== Toast ===== */
.toast { position: fixed; bottom: calc(24px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 0.7rem 1.2rem; border-radius: 999px; font-size: 0.9rem; z-index: 100; box-shadow: var(--shadow); max-width: 90vw; text-align: center; }

/* ===== Móvil ===== */
@media (max-width: 700px) {
  /* Barra superior: marca + salir arriba, buscador debajo, "nuevo libro" pasa al FAB */
  .topbar-inner { padding: 0.7rem 0.9rem; gap: 0.5rem; }
  .brand { order: 1; font-size: 1.1rem; }
  #btn-logout { order: 2; margin-left: auto; }
  #btn-new-book { display: none; }        /* se sustituye por el FAB */
  .topbar-actions { order: 3; width: 100%; margin-left: 0; }
  #search { order: 3; width: 100%; min-width: 0; }
  .stats-bar { padding-left: 0.9rem; padding-right: 0.9rem; gap: 1rem; flex-wrap: wrap; }

  .fab { display: flex; align-items: center; justify-content: center; }

  /* Rejilla: 2 columnas cómodas en el teléfono */
  .container { padding: 1rem 0.9rem calc(90px + env(safe-area-inset-bottom)); }
  .grid { grid-template-columns: repeat(2, 1fr); gap: 0.8rem; }
  .card-title { font-size: 0.92rem; }

  /* Modales como hoja inferior (bottom-sheet) */
  .modal { padding: 0; align-items: flex-end; }
  .modal-panel {
    max-width: 100%; width: 100%; max-height: 92vh;
    border-radius: 22px 22px 0 0;
    padding: 1.3rem 1.1rem calc(1.1rem + env(safe-area-inset-bottom));
    animation: sheet-up .22s ease;
  }
  .modal-lg { max-height: 94vh; }
  /* asa visual de la hoja */
  .modal-panel::before {
    content: ""; display: block; width: 42px; height: 5px; border-radius: 999px;
    background: var(--line); margin: -0.3rem auto 0.9rem;
  }
  .modal-close { top: 14px; right: 14px; width: 36px; height: 36px; }

  /* Botones de acción del formulario, fijos y a ancho completo */
  .form-actions {
    position: sticky; bottom: 0; background: var(--bg-2);
    margin: 0.6rem -1.1rem calc(-1.1rem - env(safe-area-inset-bottom));
    padding: 0.8rem 1.1rem calc(0.8rem + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line); gap: 0.6rem;
  }
  .form-actions .btn { flex: 1; }

  .row { grid-template-columns: 1fr; }
  .ratings { gap: 1.5rem; justify-content: space-between; }

  /* Ficha de detalle: portada centrada arriba */
  .detail-head { flex-direction: column; align-items: center; text-align: center; }
  .detail-cover { width: 150px; }
  .detail-badges, .detail-ratings, .detail-actions { justify-content: center; }
  .detail-info h2 { font-size: 1.35rem; }
  .detail-actions .btn { flex: 1; }
  .char-grid { grid-template-columns: repeat(2, 1fr); gap: 0.7rem; }
  .section-title { flex-direction: column; align-items: stretch; gap: 0.6rem; }
  .section-title .btn { width: 100%; }
}

/* Teléfonos muy estrechos */
@media (max-width: 360px) {
  .grid { gap: 0.6rem; }
  .card-title { font-size: 0.86rem; }
}

@keyframes sheet-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
