/* ===== RESET Y CONFIGURACIÓN GLOBAL ===== */
*,
*::before,
*::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --color-primary: #0095f6;
  --color-primary-hover: #1877f2;
  --color-danger: #ed4956;
  --color-success: #78de45;
  --color-bg: #fafafa;
  --color-white: #ffffff;
  --color-black: #262626;
  --color-gray: #8e8e8e;
  --color-light-gray: #dbdbdb;
  --color-border: #efefef;
  --color-dark-bg: #1c1e21;
  --color-dark-card: #242526;
  --color-dark-border: #393a3b;
  --color-dark-text: #e4e6eb;

  --font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica,
    Arial, sans-serif;
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-round: 50%;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
}

body {
  font-family: var(--font-family);
  background: var(--color-bg);
  color: var(--color-black);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

img {
  max-width: 100%;
  display: block;
}

a {
  text-decoration: none;
  color: var(--color-primary);
}

/* ===== BARRA DE NAVEGACIÓN SUPERIOR ===== */
.navbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: var(--color-white);
  border-bottom: 1px solid var(--color-border);
  z-index: 100;
  box-shadow: var(--shadow-sm);
  backdrop-filter: blur(10px);
}

.nav-container {
  max-width: 975px;
  height: 100%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.logo {
  font-size: 1.8rem;
  font-weight: 700;
  background: linear-gradient(45deg, #f09433, #e6683c, #dc2743, #cc2366, #bc1888);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.5px;
  transition: opacity 0.3s;
}
.logo:hover {
  opacity: 0.8;
}

.nav-links {
  display: flex;
  gap: 24px;
  list-style: none;
  align-items: center;
}

.nav-links li a {
  display: flex;
  align-items: center;
  font-size: 1.25rem;
  color: var(--color-black);
  padding: 6px;
  border-radius: var(--radius-md);
  transition: color 0.2s, background 0.2s;
  position: relative;
}
.nav-links li a:hover {
  color: var(--color-primary);
  background: #f8f9fa;
}

/* Badge para admin */
.nav-links li a.admin-badge {
  background: var(--color-primary);
  color: white;
  padding: 6px 12px;
  font-size: 0.85rem;
  border-radius: 20px;
  font-weight: 600;
}
.nav-links li a.admin-badge:hover {
  background: var(--color-primary-hover);
  color: white;
}

.menu-toggle {
  display: none;
  background: none;
  border: none;
  font-size: 1.8rem;
  cursor: pointer;
  color: var(--color-black);
}

/* ===== MENÚ INFERIOR MÓVIL ===== */
.mobile-nav {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--color-white);
  border-top: 1px solid var(--color-border);
  z-index: 99;
  box-shadow: 0 -1px 4px rgba(0, 0, 0, 0.05);
  padding: 8px 0 env(safe-area-inset-bottom, 0);
}

.mobile-nav ul {
  display: flex;
  justify-content: space-around;
  align-items: center;
  list-style: none;
}

.mobile-nav ul li a {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 0.7rem;
  color: var(--color-gray);
  padding: 4px 8px;
  transition: color 0.2s;
}
.mobile-nav ul li a span {
  font-size: 1.5rem;
  margin-bottom: 2px;
}
.mobile-nav ul li a:hover,
.mobile-nav ul li a.active {
  color: var(--color-primary);
}

/* ===== CONTENEDOR PRINCIPAL ===== */
.main-content {
  max-width: 600px;
  margin: 80px auto 0;
  padding: 0 16px 80px;
}

/* ===== TARJETA DE PUBLICACIÓN (POST) ===== */
.card {
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  margin-bottom: 24px;
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.3s;
}
.card:hover {
  box-shadow: var(--shadow-md);
}

.post-header {
  display: flex;
  align-items: center;
  padding: 14px;
  gap: 10px;
}
.avatar-small {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-round);
  object-fit: cover;
  border: 1px solid var(--color-border);
}
.post-header .username {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--color-black);
  flex: 1;
}
.post-header .post-date {
  color: var(--color-gray);
  font-size: 0.75rem;
}
.post-content {
  padding: 0 14px 14px;
  font-size: 0.95rem;
  line-height: 1.6;
  word-break: break-word;
}
.post-content img {
  border-radius: var(--radius-sm);
  margin-top: 8px;
  width: 100%;
}

.post-actions {
  padding: 8px 14px;
  display: flex;
  gap: 20px;
  align-items: center;
}
.like-btn,
.comment-toggle {
  cursor: pointer;
  background: none;
  border: none;
  font-size: 1.4rem;
  color: var(--color-black);
  transition: color 0.2s, transform 0.2s;
  display: flex;
  align-items: center;
  gap: 4px;
}
.like-btn.liked {
  color: var(--color-danger);
}
.like-btn:hover,
.comment-toggle:hover {
  color: var(--color-gray);
  transform: scale(1.1);
}
.likes-count {
  font-size: 0.9rem;
  font-weight: 600;
}

.comments-section {
  padding: 0 14px 14px;
  border-top: 1px solid var(--color-border);
  display: none;
  background: #fafafa;
  border-radius: 0 0 var(--radius-md) var(--radius-md);
}
.comment {
  padding: 8px 0;
  font-size: 0.9rem;
}
.comment strong {
  font-weight: 600;
  margin-right: 6px;
}
.comment-form {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}
.comment-form input {
  flex: 1;
  border: 1px solid var(--color-light-gray);
  background: var(--color-white);
  padding: 8px 12px;
  border-radius: 20px;
  font-size: 0.85rem;
  outline: none;
}
.comment-form input:focus {
  border-color: var(--color-primary);
}
.comment-form button {
  background: var(--color-primary);
  border: none;
  color: white;
  padding: 8px 16px;
  border-radius: 20px;
  font-weight: 600;
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.2s;
}
.comment-form button:hover {
  background: var(--color-primary-hover);
}

/* ===== BOTONES GENERALES ===== */
.btn-primary {
  background: var(--color-primary);
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  display: inline-block;
  text-align: center;
}
.btn-primary:hover {
  background: var(--color-primary-hover);
  transform: translateY(-1px);
}
.btn-secondary {
  background: #efefef;
  color: var(--color-black);
  border: none;
  padding: 10px 20px;
  border-radius: var(--radius-md);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-secondary:hover {
  background: #dbdbdb;
}

/* ===== FORMULARIOS ===== */
input,
textarea,
select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--color-light-gray);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-family: var(--font-family);
  margin: 6px 0;
  transition: border-color 0.2s;
  background: var(--color-white);
  color: var(--color-black);
}
input:focus,
textarea:focus,
select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 2px rgba(0, 149, 246, 0.1);
}

/* ===== PÁGINAS DE AUTENTICACIÓN ===== */
.auth-container {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}
.auth-container .card {
  width: 380px;
  padding: 40px;
  text-align: center;
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-md);
}
.auth-container .logo {
  font-size: 2.5rem;
  margin-bottom: 20px;
}

/* ===== WIDGETS LATERALES (ocultos en móvil) ===== */
.widgets {
  display: none; /* Se puede mostrar en desktop con media query */
}

/* ===== PIE DE PÁGINA ===== */
.footer {
  text-align: center;
  color: var(--color-gray);
  font-size: 0.75rem;
  padding: 30px;
  border-top: 1px solid var(--color-border);
}

/* ===== ADMIN PANEL (TEMA OSCURO) ===== */
.admin-layout {
  display: flex;
  min-height: 100vh;
}
.sidebar {
  width: 260px;
  background: var(--color-dark-card);
  padding: 30px 20px;
  border-right: 1px solid var(--color-dark-border);
  color: var(--color-dark-text);
}
.sidebar h2 {
  color: var(--color-primary);
  margin-bottom: 30px;
  font-size: 1.4rem;
  font-weight: 700;
}
.sidebar ul {
  list-style: none;
}
.sidebar ul li {
  margin: 8px 0;
}
.sidebar ul li a {
  color: #b0b3b8;
  text-decoration: none;
  display: block;
  padding: 10px 12px;
  border-radius: var(--radius-md);
  font-weight: 500;
  transition: background 0.2s, color 0.2s;
}
.sidebar ul li a:hover {
  background: #3a3b3c;
  color: white;
}
.main-panel {
  flex: 1;
  padding: 40px;
  background: var(--color-dark-bg);
  color: var(--color-dark-text);
  overflow-y: auto;
}
.main-panel .card {
  background: var(--color-dark-card);
  border: 1px solid var(--color-dark-border);
  color: var(--color-dark-text);
}
.main-panel .table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 20px;
}
.main-panel .table th,
.main-panel .table td {
  border: 1px solid var(--color-dark-border);
  padding: 12px;
  text-align: left;
  font-size: 0.9rem;
}
.main-panel .table th {
  background: #2d2e30;
  font-weight: 600;
}
.main-panel input,
.main-panel textarea,
.main-panel select {
  background: #3a3b3c;
  border-color: var(--color-dark-border);
  color: var(--color-dark-text);
}
.main-panel input:focus,
.main-panel textarea:focus,
.main-panel select:focus {
  border-color: var(--color-primary);
}

/* ===== ASISTENTE VIRTUAL (WIDGET) ===== */
.assistant-widget {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 200;
}
.assistant-toggle {
  width: 56px;
  height: 56px;
  border-radius: var(--radius-round);
  background: linear-gradient(135deg, var(--color-primary), #1877f2);
  color: white;
  border: none;
  font-size: 1.6rem;
  cursor: pointer;
  box-shadow: var(--shadow-lg);
  transition: transform 0.2s;
}
.assistant-toggle:hover {
  transform: scale(1.1);
}
.assistant-panel {
  display: none;
  width: 320px;
  height: 450px;
  background: var(--color-white);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--color-border);
}
.assistant-panel.open {
  display: flex;
}
.assistant-messages {
  flex: 1;
  overflow-y: auto;
  padding: 15px;
  background: #f5f5f5;
  font-size: 0.9rem;
}
.assistant-messages .msg {
  margin-bottom: 10px;
  padding: 10px 14px;
  border-radius: 18px;
  max-width: 80%;
  word-wrap: break-word;
  line-height: 1.4;
}
.msg.user {
  background: var(--color-primary);
  color: white;
  margin-left: auto;
  border-bottom-right-radius: 4px;
}
.msg.bot {
  background: #e4e6eb;
  color: var(--color-black);
  margin-right: auto;
  border-bottom-left-radius: 4px;
}
.assistant-input {
  display: flex;
  padding: 10px;
  border-top: 1px solid var(--color-border);
  background: var(--color-white);
}
.assistant-input input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--color-light-gray);
  border-radius: 20px;
  font-size: 0.85rem;
}
.assistant-input input:focus {
  border-color: var(--color-primary);
}
.assistant-input button {
  margin-left: 8px;
  background: var(--color-primary);
  border: none;
  color: white;
  padding: 10px 16px;
  border-radius: 20px;
  font-weight: 600;
  cursor: pointer;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
  .nav-links {
    display: none;
  }
  .menu-toggle {
    display: block;
  }
  .nav-links.active {
    display: flex;
    flex-direction: column;
    position: absolute;
    top: 60px;
    left: 0;
    background: var(--color-white);
    width: 100%;
    padding: 20px;
    border-bottom: 1px solid var(--color-border);
    box-shadow: var(--shadow-md);
  }
  .mobile-nav {
    display: block;
  }
  .main-content {
    padding-bottom: 80px;
  }
  .admin-layout {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    padding: 20px;
  }
  .main-panel {
    padding: 20px;
  }
  .auth-container .card {
    width: 90%;
  }
}

/* ===== ANIMACIONES Y UTILIDADES ===== */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
.card {
  animation: fadeIn 0.3s ease-out;
}

.delete-post {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.2rem;
  margin-left: auto;
  color: var(--color-gray);
  transition: color 0.2s;
}
.delete-post:hover {
  color: var(--color-danger);
}

.stat-card {
  background: var(--color-dark-card);
  border: 1px solid var(--color-dark-border);
  border-radius: var(--radius-md);
  padding: 20px;
  text-align: center;
  transition: transform 0.2s;
}
.stat-card:hover {
  transform: translateY(-2px);
}
document.addEventListener('DOMContentLoaded', function() {
    // Menu toggle
    document.getElementById('menuToggle')?.addEventListener('click', () => {
        document.getElementById('navLinks').classList.toggle('active');
    });
    // Like button
    document.querySelectorAll('.like-btn').forEach(btn => {
        btn.addEventListener('click', async function() {
            const postId = this.dataset.postId;
            const formData = new FormData();
            formData.append('post_id', postId);
            const resp = await fetch('/like.php', { method: 'POST', body: formData });
            const data = await resp.json();
            this.classList.toggle('liked', data.liked);
            this.querySelector('.likes-count').textContent = data.count;
        });
    });
    // Comment toggle
    document.querySelectorAll('.comment-toggle').forEach(btn => {
        btn.addEventListener('click', function() {
            const postId = this.dataset.postId;
            const section = document.getElementById('comments-' + postId);
            section.style.display = section.style.display === 'none' ? 'block' : 'none';
            if (section.style.display === 'block') {
                loadComments(postId);
            }
        });
    });
    // Comment submit
    document.querySelectorAll('.comment-form').forEach(form => {
        form.addEventListener('submit', async function(e) {
            e.preventDefault();
            const postId = this.dataset.postId;
            const content = this.querySelector('input').value;
            const formData = new FormData();
            formData.append('post_id', postId);
            formData.append('content', content);
            const resp = await fetch('/comment.php', { method: 'POST', body: formData });
            const comment = await resp.json();
            const list = document.querySelector('#comments-' + postId + ' .comments-list');
            list.innerHTML += `<p><strong>${comment.name}</strong>: ${comment.content}</p>`;
            this.reset();
        });
    });
    // Delete post (admin)
    document.querySelectorAll('.delete-post').forEach(btn => {
        btn.addEventListener('click', async function() {
            if (confirm('¿Eliminar publicación?')) {
                const formData = new FormData();
                formData.append('post_id', this.dataset.postId);
                await fetch('/delete_post.php', { method: 'POST', body: formData });
                this.closest('.post').remove();
            }
        });
    });
});

async function loadComments(postId) {
    const resp = await fetch('/get_comments.php?post_id=' + postId);
    const comments = await resp.json();
    const list = document.querySelector('#comments-' + postId + ' .comments-list');
    list.innerHTML = comments.map(c => `<p><strong>${c.name}</strong>: ${c.content}</p>`).join('');
}