:root {
  --bg0: #0a0a0f;
  --bg1: #111118;
  --bg2: #1a1a24;
  --bg3: #22222f;
  --surface: #16161f;
  --border: rgba(255,255,255,0.07);
  --border-hover: rgba(255,255,255,0.14);
  --text: #f0f0f8;
  --text-2: #9090a8;
  --text-3: #5a5a72;
  --accent: #7c6dfa;
  --accent-2: #a855f7;
  --accent-glow: rgba(124,109,250,0.15);
  --green: #22d3a0;
  --red: #f87171;
  --amber: #fbbf24;
  --radius: 14px;
  --radius-sm: 8px;

  /* 📐 MÁRGENES NATIVOS DE APPLE */
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);

  /* Alturas base afinadas (más finas y elegantes) */
  --nav-h-base: 60px;
  --bottom-h-base: 56px;

  /* El cálculo final que usarán TODOS los elementos (Safari y App) */
  --nav-h: calc(var(--nav-h-base) + var(--safe-top));
  --bottom-h: calc(var(--bottom-h-base) + var(--safe-bottom));

  --font-display: 'Syne', sans-serif;
  --font-body: 'DM Sans', sans-serif;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { 
  scroll-behavior: smooth; 
  -webkit-tap-highlight-color: transparent; 
  scrollbar-gutter: stable;
}

/* ── SCROLLBAR GLOBAL ── */
* { scrollbar-width: thin; scrollbar-color: var(--bg3) transparent; }
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--bg3);
  border-radius: 99px;
  border: 2px solid transparent; 
  background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--text-3); }

body {
  font-family: var(--font-body);
  background: var(--bg0);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
  font-size: 15px;
  line-height: 1.6;
}
a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; border: none; background: none; }
input, textarea, select { font-family: inherit; }

/* ── NAV ── */
/* ── NAV ── */
.top-nav {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--nav-h);
  padding-top: var(--safe-top); /* Empuja el contenido bajo la hora de iOS */
  background: var(--bg0); /* Mismo color exacto que el resto de la app */
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center;
  padding-left: 16px; padding-right: 16px;
  gap: 12px; z-index: 100;
}
.nav-logo {
  font-family: var(--font-display);
  font-size: 20px;
  font-weight: 800;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.5px;
  flex-shrink: 0;
}
.nav-spacer { flex: 1; }
.nav-actions { display: flex; align-items: center; gap: 8px; }
.nav-avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--bg3);
  border: 1.5px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600; color: var(--accent);
  text-transform: uppercase;
  cursor: pointer;
  transition: border-color 0.2s;
}
.nav-avatar:hover { border-color: var(--accent); }

/* ── STORAGE BAR ── */
.storage-bar-wrap { 
  display: flex; 
  flex-direction: column; 
  justify-content: center;
  gap: 4px; 
  width: 65px; 
  margin-right: 8px; 
}
.storage-info-row { display: flex; justify-content: center; align-items: center; width: 100%; }
.storage-label { display: none; font-size: 11px; color: var(--text-2); font-weight: 500; }
.storage-percent { font-size: 10px; font-weight: 700; color: var(--accent); transition: color 0.3s ease; }
.storage-percent.critical { color: var(--red); }
.storage-bar-track { width: 100%; height: 4px; background: var(--bg3); border-radius: 99px; overflow: hidden; }
.storage-bar-fill { height: 100%; border-radius: 99px; background: var(--accent); transition: width 0.4s ease, background-color 0.4s ease; }
.storage-bar-fill.critical { background: var(--red); }

@media (min-width: 768px) {
  .storage-bar-wrap { width: 150px; margin-right: 12px; gap: 6px; }
  .storage-info-row { justify-content: space-between; }
  .storage-label { display: block; }
  .storage-percent { font-size: 11px; }
}

/* ── LAYOUT ── */
.page-wrap {
  padding-top: var(--nav-h);
  padding-bottom: calc(var(--bottom-h) + 20px);
  min-height: 100dvh;
}
@media (min-width: 768px) { .page-wrap { padding-bottom: 40px; } }

/* ── BOTTOM NAV ── */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--bottom-h);
  padding-bottom: var(--safe-bottom); /* Se traga la raya blanca de iOS */
  padding-top: 0;
  background: var(--bg0); /* Igualamos el color de fondo para que no haya cortes */
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center; /* Centrado matemático perfecto en el espacio útil */
  justify-content: space-around;
  z-index: 100;
  /* ⚡ ESCUDO ANTI-PARPADEO (Aceleración por GPU) */
  transform: translateZ(0);
  -webkit-transform: translateZ(0);
  backface-visibility: hidden;
  will-change: transform;
}
@media (min-width: 768px) { .bottom-nav { display: none; } }
.bnav-item {
  display: flex; flex-direction: column; align-items: center;
  gap: 3px; 
  padding: 0 16px; /* Quitamos el padding vertical gigante para que bajen a su sitio */
  border-radius: var(--radius-sm);
  color: var(--text-3); transition: color 0.2s; flex: 1;
}
.bnav-item.active { color: var(--accent); }
.bnav-item svg { width: 22px; height: 22px; }
.bnav-label { font-size: 11px; font-weight: 500; letter-spacing: 0.02em; margin-top: 3px; }

/* ── SIDEBAR ── */
.hamburger-btn {
  display: flex; align-items: center; justify-content: center;
  color: var(--text); width: 40px; height: 40px; 
  margin-left: -4px; cursor: pointer;
}
@media (min-width: 768px) { .hamburger-btn { display: none; } }

.sidebar-divider { height: 1px; background: var(--border); margin: 0 16px 20px; }

.sidebar-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.7);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
  z-index: 150; opacity: 0; pointer-events: none; transition: opacity 0.3s ease;
}
.sidebar-overlay.open { opacity: 1; pointer-events: auto; }

.sidebar-close-btn {
  position: absolute; 
  right: 16px; 
  top: calc(16px + var(--safe-top)); /* MAGIA 3: Suma el margen de la cámara al top */
  width: 36px; height: 36px;
  border-radius: var(--radius-sm); color: var(--text-3); display: flex;
  align-items: center; justify-content: center; transition: all 0.2s; z-index: 10;
}
.sidebar-close-btn:hover { background: var(--bg3); color: var(--text); }
@media (min-width: 768px) { .sidebar-close-btn { display: none; } }

.sidebar {
  display: flex; flex-direction: column; position: fixed; top: 0; left: -280px; bottom: 0; 
  width: 270px; background: var(--bg1); border-right: 1px solid var(--border);
  padding: 10px 0; padding-top: calc(var(--nav-h) + 10px);
  overflow-y: hidden; z-index: 200; transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.sidebar.open { transform: translateX(280px); }

@media (min-width: 768px) {
  .sidebar { top: var(--nav-h); left: 0; width: 240px; z-index: 50; padding: 8px 0 20px; overflow-y: hidden; }
  .page-with-sidebar { margin-left: 240px; }
  .sidebar-overlay { display: none; }
}

.sidebar-section { padding: 0 12px 8px; }
.sidebar-label-row { display: flex; align-items: center; justify-content: space-between; padding: 0 12px 12px; }
.sidebar-label { font-size: 12px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-2); }
.sidebar-section:last-of-type { display: flex; flex-direction: column; flex: 1; min-height: 0; }
#sidebar-folders-list { flex: 1; overflow-y: auto; padding-bottom: 20px; }

.folder-name-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.sidebar-add-btn {
  display: flex; align-items: center; justify-content: center; width: 24px; height: 24px; 
  border-radius: 6px; color: var(--text-2); background: rgba(255, 255, 255, 0.03); 
  border: 1px solid rgba(255, 255, 255, 0.05); cursor: pointer; transition: all 0.2s ease; margin-right: -4px;
}
.sidebar-add-btn:hover { background: var(--bg3); color: var(--text); border-color: var(--border-hover); }

.sidebar-link {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; padding-right: 40px;
  border-radius: 10px; color: var(--text-2); font-size: 14px; font-weight: 400;
  transition: all 0.15s; cursor: pointer; margin-bottom: 2px;
}
.sidebar-link:hover { background: var(--bg3); color: var(--text); }
.sidebar-link.active { background: var(--accent-glow); color: var(--accent); font-weight: 500; }
.sidebar-link svg { width: 17px; height: 17px; flex-shrink: 0; opacity: 0.8; }
.sidebar-link.active svg { opacity: 1; }
.sidebar-count { margin-left: auto; background: var(--bg3); color: var(--text-3); font-size: 11px; padding: 1px 7px; border-radius: 99px; }
.sidebar-link-wrapper { position: relative; }
.folder-menu-btn {
  position: absolute; right: 8px; top: 50%; transform: translateY(-50%); color: var(--text-3);
  width: 24px; height: 24px; border-radius: 6px; display: flex; align-items: center; justify-content: center;
  opacity: 1; transition: all 0.2s; cursor: pointer;
}
.folder-menu-btn:hover { background: var(--bg3); color: var(--text); }

/* ── BUTTONS ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 7px; padding: 10px 16px; border-radius: var(--radius-sm);
  font-size: 14px; font-weight: 500; transition: all 0.2s;
  cursor: pointer; border: none; white-space: nowrap;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: #6b5df0; transform: translateY(-1px); }
.btn-primary:active { transform: translateY(0); }
.btn-ghost { background: transparent; color: var(--text-2); border: 1px solid var(--border); }
.btn-ghost:hover { border-color: var(--border-hover); color: var(--text); background: var(--bg2); }
.btn-danger { background: rgba(248,113,113,0.1); color: var(--red); border: 1px solid rgba(248,113,113,0.2); }
.btn-danger:hover { background: rgba(248,113,113,0.2); }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-icon { width: 36px; height: 36px; padding: 0; border-radius: var(--radius-sm); }

/* ── FORMS ── */
.form-group { margin-bottom: 16px; }
.form-label { display: block; font-size: 13px; font-weight: 500; color: var(--text-2); margin-bottom: 6px; }
.form-control {
  width: 100%; background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text); font-size: 15px; /* Evita zoom iOS */
  padding: 12px 14px; transition: border-color 0.2s, box-shadow 0.2s;
  outline: none; appearance: none;
}
.form-control:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
.form-control::placeholder { color: var(--text-3); }
.form-control.username-input { padding-left: 30px; }

/* ── TOASTS (Notificaciones) ── */
.toast-container {
  position: fixed;
  bottom: calc(var(--bottom-h) + var(--safe-bottom) + 12px);
  left: 50%; transform: translateX(-50%);
  z-index: 999; display: flex; flex-direction: column; gap: 8px;
  pointer-events: none; width: calc(100% - 32px); max-width: 400px;
}
@media (min-width: 768px) { .toast-container { bottom: 24px; } }
.toast {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 12px 16px; font-size: 14px;
  display: flex; align-items: center; gap: 10px;
  pointer-events: all; animation: toast-in 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275); 
  backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
}
.toast.success { border-color: rgba(34,211,160,0.3); }
.toast.error { border-color: rgba(248,113,113,0.3); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(20px) scale(0.95); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}
.messages-list { margin: 0; padding: 0; list-style: none; }

.item-menu {
  position: fixed; background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); min-width: 160px; box-shadow: 0 8px 24px rgba(0,0,0,0.5);
  z-index: 1000; display: none; overflow: hidden;
}
.item-menu.show { display: flex; flex-direction: column; }
.menu-item { padding: 10px 14px; cursor: pointer; font-size: 14px; color: var(--text-2); display: flex; align-items: center; gap: 10px; white-space: nowrap; transition: background 0.2s; }
.menu-item:hover { background: var(--bg3); color: var(--text); }
.menu-item-danger { color: var(--red); }
.menu-item-danger:hover { background: rgba(248,113,113,0.1); color: var(--red); }

/* Limitar el tamaño de los iconos descontrolados en los menús */
.item-menu .menu-item svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}


/* ── MODALS ── */
.modal-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.88); z-index: 500;
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  align-items: center; justify-content: center; padding: 16px;
}
.modal-overlay.open { display: flex; }
.modal-box {
  background: var(--bg1); border: 1px solid var(--border);
  border-radius: var(--radius); width: 100%; max-width: 500px;
  max-height: 90dvh; overflow-y: auto; animation: modal-in 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.1);
}
@keyframes modal-in {
  from { opacity: 0; transform: scale(0.95) translateY(10px); }
  to   { opacity: 1; transform: scale(1) translateY(0); }
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.modal-title { font-family: var(--font-display); font-size: 18px; font-weight: 700; flex: 1; }
.modal-close {
  width: 32px; height: 32px; border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  color: var(--text-3); transition: all 0.2s; cursor: pointer;
}
.modal-close:hover { background: var(--bg3); color: var(--text); }
.modal-body { padding: 20px; }

/* ── MEDIA VIEWER ── */
.media-viewer-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.95); z-index: 600;
  flex-direction: column; align-items: center; justify-content: center;
}
.media-viewer-overlay.open { display: flex; }
.viewer-top {
  position: absolute; top: 0; left: 0; right: 0;
  padding: 16px; padding-top: calc(16px + env(safe-area-inset-top, 0px));
  display: flex; align-items: center; gap: 12px;
  background: linear-gradient(to bottom, rgba(0,0,0,0.7), transparent); z-index: 1;
}
.viewer-title { flex: 1; font-size: 15px; font-weight: 500; color: #fff; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-close {
  width: 38px; height: 38px; background: rgba(255,255,255,0.1);
  border-radius: 50%; display: flex; align-items: center; justify-content: center;
  color: #fff; cursor: pointer; flex-shrink: 0;
}
.viewer-media { max-width: 100%; max-height: 80dvh; object-fit: contain; border-radius: 4px; }
.viewer-actions {
  position: absolute; bottom: 0; left: 0; right: 0;
  padding: 16px; padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(to top, rgba(0,0,0,0.7), transparent);
  display: flex; align-items: center; justify-content: center; gap: 12px;
}
.viewer-btn {
  display: flex; align-items: center; gap: 6px; padding: 9px 18px;
  background: rgba(255,255,255,0.12); border: 1px solid rgba(255,255,255,0.15);
  border-radius: 99px; color: #fff; font-size: 14px; font-weight: 500;
  cursor: pointer; backdrop-filter: blur(10px); transition: background 0.2s;
}
.viewer-btn:hover { background: rgba(255,255,255,0.2); }
.viewer-btn.primary { background: var(--accent); border-color: var(--accent); }


/* ── ZONA DE ARRASTRE (DROPZONE) GLOBAL ── */
.upload-dropzone {
  border: 2px dashed var(--border);
  border-radius: 12px;
  padding: 30px 20px; 
  text-align: center;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; cursor: pointer;
  transition: border-color 0.2s, background-color 0.2s;
  background: rgba(255, 255, 255, 0.02);
  width: 100%;
}
.upload-dropzone:hover, .upload-dropzone.dragover { border-color: var(--accent); background-color: var(--accent-glow); }
.upload-dropzone span { font-size: 14px; color: var(--text); font-weight: 500; line-height: 1.4; }
.upload-dropzone small { font-size: 11px; color: var(--text-3); line-height: 1.5; display: block; max-width: 100%; margin: 0 auto; word-wrap: break-word; }
#uploadPlaceholder { display: flex; flex-direction: column; align-items: center; gap: 8px; }

/* ── CONTADOR INTEGRADO EN EL TÍTULO ── */
.lib-count-inline { font-size: 14px; font-weight: 500; color: var(--text-3); margin-left: 10px; letter-spacing: normal; text-transform: none; }

/* ── BOTÓN FLOTANTE (FAB) MEJORADO ── */
.fab-upload {
  position: fixed;
  bottom: calc(var(--bottom-h) + var(--safe-bottom) + 20px);
  right: 20px;
  width: 56px; height: 56px; border-radius: 50%;
  background-color: var(--accent); color: var(--bg0); border: none; 
  box-shadow: 0 4px 16px rgba(124, 109, 250, 0.4);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; z-index: 90;
  transition: transform 0.2s ease, background-color 0.2s ease, box-shadow 0.2s ease;
}
.fab-upload:hover { transform: scale(1.05) translateY(-2px); background-color: var(--accent-2); box-shadow: 0 6px 20px rgba(124, 109, 250, 0.6); }
@media (min-width: 768px) { .fab-upload { bottom: 30px; right: 30px; } }

/* ── CORRECCIÓN ALINEACIÓN DE TAGS ── */
.tags-input-wrap { padding: 8px 12px; }
.search-input-wrap .tags-input-wrap { padding-left: 42px; }
.tags-real-input { padding: 2px 0; margin: 0; min-width: 120px; flex: 1; }