/* ============================================================
   WaifuAI-Hub — MODAL SYSTEM UNIFICADO (Fase 2a)
   Fonte única de TODOS os modais e viewers do site.
   Páginas: index, gallery, tags, favorites, profile.
   Carregar depois de: tokens.css
   ============================================================ */

/* ─────────────────────────────────────────────
   1. BASE — backdrop, painel e estados
   ───────────────────────────────────────────── */
.modal {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.3s ease, visibility 0.3s ease;
  contain: layout style;
}
.modal.active {
  opacity: 1;
  visibility: visible;
  pointer-events: all;
  backdrop-filter: blur(10px);
}
/* Compat: páginas que controlam por .hidden */
.modal.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.modal-content {
  background: #0f0f0f;
  border: 1px solid rgba(255, 255, 255, 0.07);
  border-radius: var(--radius-md);
  width: 95%;
  max-width: 1060px;
  height: 88vh;
  display: flex;
  overflow: hidden;
  will-change: transform;
  transform: scale(0.96);
  transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.modal.active .modal-content { transform: scale(1); }

/* Backdrop interno (variação profile) */
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  backdrop-filter: blur(10px);
}

body.modal-open { overflow: hidden; }

/* ─────────────────────────────────────────────
   2. PACK MODAL — sidebar esquerda (capa + info)
   ───────────────────────────────────────────── */
.modal-sidebar {
  width: 220px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--bg-2);
  background: var(--bg-1);
  overflow: hidden;
}
.modal-cover {
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
  background: #0a0a0a;
  max-height: 260px;
}
.modal-cover img {
  width: 100%;
  height: auto;
  display: block;
  object-fit: cover;
}
.modal-cover img.nsfw-cover-blur {
  filter: blur(14px);
  transform: scale(1.08);
}
.modal-cover-gradient {
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, transparent 40%, var(--bg-1) 100%);
  z-index: 1;
}
.modal-cover-badge {
  position: absolute;
  bottom: 10px;
  left: 10px;
  z-index: 2;
  font-size: 0.55rem;
  font-weight: 800;
  padding: 2px 8px;
  border-radius: var(--radius-sm);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.modal-cover-badge.badge-sfw  { background: #16a34a; color: var(--white); }
.modal-cover-badge.badge-soft { background: #ea580c; color: var(--white); }
.modal-cover-badge.badge-nsfw { background: #be185d; color: var(--white); }

.modal-sidebar-body {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scrollbar-width: thin;
  scrollbar-color: var(--border-1) transparent;
}
.modal-sidebar-body::-webkit-scrollbar { width: 2px; }
.modal-sidebar-body::-webkit-scrollbar-thumb { background: var(--border-2); }

.modal-pack-title {
  font-family: var(--font-sans);
  font-size: 0.88rem;
  font-weight: 800;
  color: var(--white);
  margin: 0 0 3px;
  line-height: 1.3;
}
.modal-pack-sub { font-size: 0.7rem; color: var(--text-5); margin: 0; }

.modal-divider { height: 1px; background: var(--bg-3); flex-shrink: 0; }

.modal-section-label {
  font-size: 0.6rem;
  font-weight: 800;
  color: #444;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin: 0 0 6px;
}
.modal-description {
  font-size: 0.75rem;
  color: var(--text-3);
  line-height: 1.6;
  margin: 0;
  display: -webkit-box;
  -webkit-line-clamp: 5;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.modal-tags { display: flex; flex-wrap: wrap; gap: 4px; }
.modal-tag {
  font-size: 0.62rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: var(--radius-lg);
  background: var(--bg-2);
  border: 1px solid var(--bg-4);
  color: var(--text-4);
  cursor: pointer;
  text-decoration: none;
  transition: border-color 0.15s, color 0.15s;
}
.modal-tag:hover { border-color: var(--brand); color: var(--accent); }

.modal-sidebar-actions {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-top: 4px;
}
.modal-fav-action {
  background: transparent;
  border: 1px solid var(--brand);
  color: var(--brand);
  padding: 7px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 0.8rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: background 0.2s, color 0.2s;
}
.modal-fav-action.active { background: var(--brand); color: var(--white); }
.like-modal-btn {
  background: transparent;
  border: 1px solid var(--border-1);
  color: var(--text-5);
  padding: 7px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 0.8rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.like-modal-btn:hover { border-color: var(--danger-soft); color: var(--danger-soft); }
.like-modal-btn.liked { background: rgba(255, 107, 107, 0.1); border-color: var(--danger-soft); color: var(--danger-soft); }

/* ─────────────────────────────────────────────
   3. PACK MODAL — área direita (grade de imagens)
   ───────────────────────────────────────────── */
.modal-right {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}
.modal-right-header {
  padding: 12px 16px;
  background: #141414;
  border-bottom: 1px solid var(--bg-2);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}
.img-counter { font-size: 0.72rem; color: #444; font-family: var(--font-mono); }
.close-modal-btn {
  background: transparent;
  border: none;
  color: var(--text-5);
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
  padding: 0;
  font-family: var(--font-sans);
  transition: color 0.15s;
}
.close-modal-btn:hover { color: var(--danger); }

.modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  background: #0d0d0d;
  overscroll-behavior: contain;
}
.modal-images {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 7px;
  align-items: start;
}
.modal-images img {
  width: 100%;
  height: auto;
  display: block;
  border-radius: var(--radius-sm);
  cursor: zoom-in;
  transition: opacity 0.2s, transform 0.2s;
}
.modal-images img:hover { opacity: 0.85; transform: scale(1.015); }

.modal-warning {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
  color: var(--text-4);
  gap: 8px;
  padding: 40px;
}
.modal-warning.hidden { display: none; }
.modal-warning p { margin: 0; }
.modal-warning .nsfw-btn {
  display: inline-block;
  margin-top: 8px;
  padding: 9px 22px;
  background: var(--brand);
  color: var(--white);
  border-radius: var(--radius-lg);
  text-decoration: none;
  font-weight: 700;
  font-size: 0.85rem;
  transition: background 0.2s;
}
.modal-warning .nsfw-btn:hover { background: var(--accent-2); }

/* ─────────────────────────────────────────────
   4. RODAPÉ DO MODAL — navegação entre packs
   ───────────────────────────────────────────── */
.modal-right-footer {
  padding: 10px 16px;
  background: #141414;
  border-top: 1px solid var(--bg-2);
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}
.modal-nav-btn {
  background: var(--bg-2);
  border: 1px solid var(--border-1);
  color: var(--text-3);
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: var(--font-sans);
  font-size: 0.78rem;
  text-decoration: none;
  transition: border-color 0.15s, color 0.15s;
}
.modal-nav-btn:hover:not(:disabled) { border-color: var(--brand); color: var(--white); }
.modal-nav-btn:disabled { opacity: 0.2; pointer-events: none; }
.modal-page-link {
  font-size: 0.75rem;
  padding: 7px 14px;
  border-radius: var(--radius-lg);
  background: rgba(233, 30, 99, 0.1);
  border: 1px solid rgba(233, 30, 99, 0.35);
  color: var(--accent);
  text-decoration: none;
  font-weight: 700;
  transition: background 0.2s;
}
.modal-page-link:hover { background: rgba(233, 30, 99, 0.2); }

/* ─────────────────────────────────────────────
   5. RESPONSIVO — desktop lado a lado / mobile bottom-sheet
   ───────────────────────────────────────────── */
@media (min-width: 641px) {
  .modal-content {
    flex-direction: row;
    max-width: 1100px;
    height: 88vh;
  }
  .modal-sidebar {
    width: 220px;
    flex-shrink: 0;
    flex-direction: column;
    border-right: 1px solid var(--bg-2);
    border-bottom: none;
    height: 100%;
    overflow: hidden;
  }
  .modal-cover {
    width: 100%;
    height: auto;
    max-height: 45%;
    flex-shrink: 0;
  }
  .modal-cover img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
  }
  .modal-cover-gradient { background: linear-gradient(to bottom, transparent 40%, var(--bg-1) 100%); }
  .modal-sidebar-body { padding: 12px; gap: 10px; overflow-y: auto; flex: 1; max-height: none; }
  .modal-pack-title   { font-size: 0.85rem; }
  .modal-pack-sub     { font-size: 0.68rem; color: var(--text-5); }
  .modal-divider      { display: block; }
  .modal-section-label { font-size: 0.58rem; }
  .modal-description {
    font-size: 0.72rem;
    line-height: 1.6;
    -webkit-line-clamp: 25;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .modal-tags { gap: 3px; }
  .modal-tag  { font-size: 0.58rem; padding: 2px 6px; }
  .modal-sidebar-actions { flex-direction: column; gap: 5px; margin-top: auto; }
  .modal-fav-action, .like-modal-btn { padding: 7px 6px; font-size: 0.75rem; }
  .modal-images { grid-template-columns: repeat(3, 1fr); gap: 6px; }
}

@media (max-width: 640px) {
  .modal-content {
    flex-direction: column;
    height: 92vh;
    border-radius: var(--radius-md) var(--radius-md) 0 0;
    align-self: flex-end;
  }
  .modal-sidebar { width: 100%; height: auto; flex-direction: row; border-right: none; border-bottom: 1px solid var(--bg-2); }
  .modal-cover {
    width: 80px;
    max-height: none;
    height: 110px;
    flex-shrink: 0;
  }
  .modal-cover img { width: 100%; height: 100%; object-fit: cover; }
  .modal-cover-gradient { background: linear-gradient(to right, transparent 40%, var(--bg-1) 100%); }
  .modal-sidebar-body { padding: 10px; gap: 6px; overflow-y: auto; max-height: 160px; }
  .modal-sidebar-actions { flex-direction: row; margin-top: 0; }
  .modal-divider { display: none; }
  .modal-description { -webkit-line-clamp: 3; }
  .modal-nav-btn { padding: 8px 10px; font-size: 0.72rem; }
}

/* ─────────────────────────────────────────────
   6. IMAGE VIEWER — tela cheia unificado
   ───────────────────────────────────────────── */
.image-viewer {
  position: fixed;
  inset: 0;
  z-index: var(--z-toast);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.3s, visibility 0.3s;
  contain: layout style;
}
.image-viewer.active { opacity: 1; visibility: visible; pointer-events: all; }

.viewer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.93);
  backdrop-filter: blur(10px);
  z-index: -1;
}
.viewer-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 84px 18px;
}
.viewer-content { position: relative; z-index: 2001; text-align: center; width: 100%; }
.viewer-content img {
  max-width: 95vw;
  max-height: 85vh;
  object-fit: contain;
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.7);
  will-change: opacity;
  transition: opacity 0.15s ease;
}
.viewer-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 14px;
}
.viewer-counter {
  color: var(--white);
  font-family: var(--font-mono);
  font-size: 0.85rem;
  background: rgba(255, 255, 255, 0.1);
  padding: 4px 14px;
  border-radius: var(--radius-lg);
  display: inline-block;
}
.viewer-like-btn, .viewer-info-btn {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: var(--radius-lg);
  color: var(--text-3);
  padding: 8px 16px;
  font-family: var(--font-sans);
  font-size: var(--text-xs);
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.viewer-like-btn:hover { color: var(--danger-soft); }
.viewer-like-btn.liked { color: var(--danger-soft); background: rgba(255, 107, 107, 0.2); }
.viewer-info-btn:hover { color: var(--white); }
.viewer-info-btn.active { color: var(--accent); background: rgba(233, 30, 99, 0.16); }

.viewer-sidebar {
  width: 360px;
  flex-shrink: 0;
  background: #0d0d0d;
  border-left: 1px solid #1c1c1c;
  display: flex;
  flex-direction: column;
  transition: margin-right 0.3s ease;
  overflow-y: auto;
}
.image-viewer:not(.panel-open) .viewer-sidebar { margin-right: -360px; }

.viewer-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 2010;
  background: rgba(0, 0, 0, 0.3);
  border: none;
  color: var(--white);
  font-size: 3rem;
  padding: 18px;
  cursor: pointer;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  will-change: transform;
  transition: background 0.2s, transform 0.2s;
  user-select: none;
}
.viewer-arrow:hover { background: rgba(233, 30, 99, 0.6); transform: translateY(-50%) scale(1.1); }
.viewer-arrow.left  { left: 15px; }
.viewer-arrow.right { right: 15px; }

.viewer-close {
  position: absolute;
  top: 20px;
  right: 20px;
  z-index: 2010;
  background: rgba(0, 0, 0, 0.3);
  border: none;
  color: var(--white);
  font-size: 2.2rem;
  width: 46px;
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-full);
  cursor: pointer;
  line-height: 1;
  font-family: var(--font-sans);
  transition: background 0.2s, transform 0.2s;
}
.viewer-close:hover { background: var(--danger); transform: rotate(90deg); }

@media (max-width: 640px) {
  .viewer-close { top: 12px; right: 12px; width: 38px; height: 38px; font-size: 1.8rem; }
  .viewer-arrow { font-size: 2rem; padding: 12px; }
  .viewer-main  { padding: 60px 12px 14px; }
  .viewer-sidebar { width: 300px; }
  .image-viewer:not(.panel-open) .viewer-sidebar { margin-right: -300px; }
}

/* ─────────────────────────────────────────────
   7. VARIANTE SHEET — modal simples (profile)
   ───────────────────────────────────────────── */
.modal-sheet {
  width: min(720px, 100%);
  height: auto;
  max-height: 85vh;
  flex-direction: column;
}
.modal-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border-1);
  background: var(--bg-2);
  flex-shrink: 0;
}
.modal-title {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-1);
  font-weight: 800;
  font-size: var(--text-sm);
}
.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-3);
  font-size: 1.5rem;
  cursor: pointer;
  line-height: 1;
  padding: 0;
  font-family: var(--font-sans);
  transition: color var(--t-fast);
}
.icon-btn:hover { color: var(--danger); }
.thumbs-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 8px;
  padding: 14px;
  overflow-y: auto;
}
.thumbs-grid img {
  width: 100%;
  height: auto;
  display: block;
  border-radius: var(--radius-sm);
  cursor: zoom-in;
  transition: opacity var(--t-fast), transform var(--t-fast);
}
.thumbs-grid img:hover { opacity: 0.85; transform: scale(1.02); }
.modal-empty {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-4);
  font-size: var(--text-sm);
}
.viewer-top {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 2010;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
}

/* Botão fullscreen (viewer do feed) */
.viewer-fs {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: var(--radius-lg);
  color: var(--text-3);
  padding: 8px 14px;
  font-size: var(--text-sm);
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.viewer-fs:hover { color: var(--white); background: rgba(255, 255, 255, 0.18); }

/* ─────────────────────────────────────────────
   8. VARIANTE SPLIT — viewer da galeria
   (imagem + painel de detalhes lado a lado)
   ───────────────────────────────────────────── */
.image-viewer.viewer-split { align-items: stretch; justify-content: flex-start; }
.image-viewer.viewer-split .viewer-content {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-width: 0;
  min-height: 0;
}
.image-viewer.viewer-split .viewer-content img {
  max-width: 100%;
  max-height: calc(100vh - 150px);
  border-radius: 8px;
  box-shadow: 0 0 60px rgba(0, 0, 0, 0.8);
}
.image-viewer.viewer-split .viewer-meta { justify-content: center; margin-top: 0; }
.image-viewer.viewer-split .viewer-like-btn,
.image-viewer.viewer-split .viewer-info-btn {
  padding: 4px 14px;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  gap: 5px;
}
.image-viewer.viewer-split .viewer-sidebar {
  z-index: 2005;
  transition: margin-right 0.28s ease, transform 0.28s ease;
}

/* Desktop com painel aberto: botões desviam do painel */
@media (min-width: 1025px) {
  .image-viewer.viewer-split.panel-open .viewer-arrow.right { right: 372px; }
  .image-viewer.viewer-split.panel-open .viewer-close { right: 372px; }
  .image-viewer.viewer-split.panel-open .viewer-main { padding-right: 24px; }
}

/* Sidebar vira drawer deslizante em telas menores */
@media (max-width: 1024px) {
  .image-viewer.viewer-split .viewer-sidebar {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(360px, 88vw);
    box-shadow: -16px 0 48px rgba(0, 0, 0, 0.6);
    transform: translateX(105%);
    margin-right: 0 !important;
  }
  .image-viewer.viewer-split.panel-open .viewer-sidebar { transform: translateX(0); }
  .image-viewer.viewer-split .viewer-main { padding: 56px 14px 16px; }
  .image-viewer.viewer-split .viewer-content img { max-height: calc(100vh - 160px); }
}

@media (max-width: 600px) {
  .image-viewer.viewer-split .viewer-arrow { font-size: 2rem; padding: 10px; }
  .image-viewer.viewer-split .viewer-arrow.left { left: 6px; }
  .image-viewer.viewer-split .viewer-arrow.right { right: 6px; }
  .image-viewer.viewer-split .viewer-close { top: 12px; right: 12px; width: 36px; height: 36px; font-size: 1.6rem; }
  .image-viewer.viewer-split .viewer-sidebar { width: 100%; }
  .image-viewer.viewer-split .viewer-meta { gap: 6px; }
  .image-viewer.viewer-split .viewer-counter,
  .image-viewer.viewer-split .viewer-like-btn,
  .image-viewer.viewer-split .viewer-info-btn { font-size: 0.72rem; padding: 4px 10px; }
}
