@layer base, layout, components, utilities;

@layer base {
  /* Declaración del orden de las capas */
  :root {
    /* Fuentes */
    --font-sans: 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
    --font-mono: 'JetBrains Mono', monospace;

    /* Tokens de diseño del Modo Oscuro (Por defecto) */
    --color-bg: #0b0f19;
    --color-card-bg: rgba(22, 30, 49, 0.65);
    --color-card-hover: rgba(30, 41, 67, 0.75);
    --color-border: rgba(255, 255, 255, 0.08);
    --color-border-hover: rgba(255, 255, 255, 0.15);
    
    --color-text-primary: #f8fafc;
    --color-text-secondary: #94a3b8;
    --color-text-muted: #64748b;
    
    --accent-primary: #00f2fe;
    --accent-secondary: #6366f1;
    --accent-glow: rgba(0, 242, 254, 0.15);
    
    --shadow-sm: 0 4px 6px -1px rgba(0, 0, 0, 0.2), 0 2px 4px -2px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -4px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.4), 0 8px 10px -6px rgba(0, 0, 0, 0.4);
    
    --accent-grad: linear-gradient(135deg, var(--accent-primary) 0%, var(--accent-secondary) 100%);
    --bg-mesh-opacity: 0.4;
  }

  /* Tokens de diseño del Modo Claro (Clase activada por JS) */
  body.light-theme {
    --color-bg: #f8fafc;
    --color-card-bg: rgba(255, 255, 255, 0.7);
    --color-card-hover: rgba(255, 255, 255, 0.85);
    --color-border: rgba(15, 23, 42, 0.08);
    --color-border-hover: rgba(15, 23, 42, 0.15);
    
    --color-text-primary: #0f172a;
    --color-text-secondary: #475569;
    --color-text-muted: #94a3b8;
    
    --accent-primary: #0284c7;
    --accent-secondary: #4f46e5;
    --accent-glow: rgba(2, 132, 199, 0.1);
    
    --shadow-sm: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -4px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.12), 0 8px 10px -6px rgba(0, 0, 0, 0.12);
    
    --bg-mesh-opacity: 0.15;
  }

  /* Reset global y accesibilidad */
  * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  html {
    scroll-behavior: smooth;
    font-size: 16px;
  }

  body {
    font-family: var(--font-sans);
    background-color: var(--color-bg);
    color: var(--color-text-primary);
    line-height: 1.6;
    overflow-x: hidden;
    transition: background-color 0.4s ease, color 0.4s ease;
    min-height: 100vh;
  }

  /* Foco accesible */
  :focus-visible {
    outline: 2px solid var(--accent-primary);
    outline-offset: 4px;
  }

  /* Custom scrollbars */
  ::-webkit-scrollbar {
    width: 8px;
    height: 8px;
  }

  ::-webkit-scrollbar-track {
    background: var(--color-bg);
  }

  ::-webkit-scrollbar-thumb {
    background: var(--color-text-muted);
    border-radius: 4px;
    transition: background 0.2s;
  }

  ::-webkit-scrollbar-thumb:hover {
    background: var(--accent-primary);
  }

  /* Typografía global */
  h1, h2, h3, h4 {
    font-weight: 700;
    line-height: 1.25;
    color: var(--color-text-primary);
    text-wrap: balance;
  }

  p {
    text-wrap: pretty;
  }
}

@layer layout {
  /* Orbes de brillo flotantes en el fondo */
  .bg-gradient-mesh {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: -3;
    opacity: var(--bg-mesh-opacity);
    background-image: 
      radial-gradient(at 10% 20%, rgba(99, 102, 241, 0.15) 0px, transparent 50%),
      radial-gradient(at 90% 10%, rgba(0, 242, 254, 0.15) 0px, transparent 50%),
      radial-gradient(at 50% 80%, rgba(99, 102, 241, 0.1) 0px, transparent 50%);
    pointer-events: none;
    transition: opacity 0.4s ease;
  }

  .bg-glow-orb-1, .bg-glow-orb-2 {
    position: fixed;
    width: 400px;
    height: 400px;
    border-radius: 50%;
    z-index: -2;
    filter: blur(80px);
    opacity: 0.12;
    pointer-events: none;
    animation: float 20s infinite alternate ease-in-out;
  }

  .bg-glow-orb-1 {
    top: -100px;
    left: -100px;
    background: var(--accent-primary);
  }

  .bg-glow-orb-2 {
    bottom: -100px;
    right: -100px;
    background: var(--accent-secondary);
    animation-delay: -10s;
  }

  @keyframes float {
    0% { transform: translate(0, 0) scale(1); }
    50% { transform: translate(80px, 40px) scale(1.1); }
    100% { transform: translate(-40px, 80px) scale(0.95); }
  }

  /* Grid Layout Principal */
  .app-container {
    max-width: 1300px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
    display: grid;
    grid-template-columns: 1fr;
    gap: 2rem;
    align-items: start;
  }

  @media (min-width: 992px) {
    .app-container {
      grid-template-columns: 360px 1fr;
      padding: 3rem 2rem;
      gap: 2.5rem;
    }

    .sidebar {
      position: sticky;
      top: 3rem;
      max-height: calc(100vh - 6rem);
      overflow-y: auto;
      /* Ocultar barra en sidebar */
      scrollbar-width: none;
    }
    .sidebar::-webkit-scrollbar {
      display: none;
    }
  }
}

@layer components {
  /* ==========================================
     TARJETA GLASSMORPHIC GENERAL
     ========================================== */
  .card {
    background-color: var(--color-card-bg);
    border: 1px solid var(--color-border);
    border-radius: 16px;
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    box-shadow: var(--shadow-md);
    transition: background-color 0.3s ease, border-color 0.3s ease, box-shadow 0.3s ease, transform 0.3s ease;
  }

  .card:hover {
    background-color: var(--color-card-hover);
    border-color: var(--color-border-hover);
    box-shadow: var(--shadow-lg);
  }

  /* ==========================================
     BARRA LATERAL / SIDEBAR
     ========================================== */
  .sidebar {
    padding: 2rem 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
  }

  .sidebar-header {
    display: flex;
    justify-content: flex-end;
  }

  .theme-btn {
    background: transparent;
    border: 1px solid var(--color-border);
    color: var(--color-text-primary);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
  }

  .theme-btn:hover {
    background: var(--color-border);
    border-color: var(--accent-primary);
    color: var(--accent-primary);
    transform: rotate(15deg);
  }

  /* Toggle Iconos */
  body.light-theme .sun-icon { display: none; }
  body:not(.light-theme) .moon-icon { display: none; }

  /* Info de Perfil */
  .profile-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
  }

  .avatar-container {
    position: relative;
    width: 140px;
    height: 140px;
    margin-bottom: 1.25rem;
  }

  .avatar-img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
    border: 3px solid rgba(255, 255, 255, 0.05);
    background-color: #0f172a;
    z-index: 2;
    position: relative;
  }

  .avatar-pulse {
    position: absolute;
    top: -5px;
    left: -5px;
    right: -5px;
    bottom: -5px;
    border-radius: 50%;
    background: var(--accent-grad);
    z-index: 1;
    opacity: 0.7;
    filter: blur(4px);
    animation: avatarGlow 3s infinite alternate ease-in-out;
  }

  @keyframes avatarGlow {
    0% { transform: scale(0.98); opacity: 0.5; }
    100% { transform: scale(1.03); opacity: 0.85; filter: blur(6px); }
  }

  .profile-name {
    font-size: 1.45rem;
    margin-bottom: 0.5rem;
    font-family: var(--font-sans);
  }

  .profile-title {
    font-size: 0.95rem;
    color: var(--color-text-secondary);
    font-family: var(--font-mono);
    margin-bottom: 0.75rem;
    font-weight: 500;
  }

  .profile-badge {
    background: var(--accent-glow);
    border: 1px solid var(--accent-primary);
    color: var(--accent-primary);
    font-size: 0.75rem;
    font-weight: 700;
    padding: 0.25rem 0.75rem;
    border-radius: 9999px;
    margin-bottom: 1rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }

  .profile-location {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    color: var(--color-text-muted);
  }

  .sidebar-divider {
    border: none;
    border-top: 1px solid var(--color-border);
    width: 100%;
  }

  /* Lista de contacto */
  .contact-info h2 {
    margin-bottom: 0.75rem;
  }

  .sidebar-section-title {
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--color-text-muted);
    font-weight: 700;
  }

  .contact-info ul {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
  }

  .contact-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    color: var(--color-text-secondary);
    text-decoration: none;
    font-size: 0.9rem;
    transition: all 0.2s ease;
    word-break: break-all;
  }

  .icon-wrapper {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    background: var(--color-border);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-text-primary);
    transition: all 0.2s ease;
    flex-shrink: 0;
  }

  .contact-link:hover {
    color: var(--accent-primary);
  }

  .contact-link:hover .icon-wrapper {
    background: var(--accent-primary);
    color: #fff;
    transform: translateY(-2px);
  }

  /* Navegación lateral secciones */
  .sections-nav {
    display: none;
  }

  @media (min-width: 992px) {
    .sections-nav {
      display: flex;
      flex-direction: column;
      gap: 0.25rem;
      margin-top: 0.5rem;
    }

    .nav-item {
      display: block;
      padding: 0.65rem 1rem;
      border-radius: 8px;
      color: var(--color-text-secondary);
      text-decoration: none;
      font-size: 0.95rem;
      font-weight: 500;
      transition: all 0.2s ease;
      border-left: 3px solid transparent;
    }

    .nav-item:hover {
      background: var(--color-border);
      color: var(--color-text-primary);
      padding-left: 1.25rem;
    }

    .nav-item.active {
      background: var(--accent-glow);
      color: var(--accent-primary);
      border-left-color: var(--accent-primary);
      font-weight: 600;
    }
  }

  .sidebar-footer {
    text-align: center;
    font-size: 0.75rem;
    color: var(--color-text-muted);
    margin-top: auto;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
  }

  /* ==========================================
     CONTENIDO PRINCIPAL
     ========================================== */
  .main-content {
    display: flex;
    flex-direction: column;
    gap: 2rem;
  }

  .content-section {
    padding: 2.25rem 2rem;
  }

  .section-title {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
    position: relative;
    padding-bottom: 0.5rem;
    display: inline-block;
  }

  .section-title::after {
    content: '';
    position: absolute;
    left: 0;
    bottom: 0;
    width: 40px;
    height: 3px;
    background: var(--accent-grad);
    border-radius: 2px;
  }

  .lead-text {
    font-size: 1.15rem;
    color: var(--color-text-primary);
    margin-bottom: 1rem;
    font-weight: 500;
    line-height: 1.5;
  }

  .body-text {
    color: var(--color-text-secondary);
    font-size: 1rem;
  }

  /* ==========================================
     LÍNEA DE TIEMPO (EXPERIENCIA)
     ========================================== */
  .section-header-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 1.5rem;
  }

  .section-header-row .section-title {
    margin-bottom: 0;
  }

  .timeline {
    position: relative;
    border-left: 2px solid var(--color-border);
    margin-left: 0.75rem;
    padding-left: 1.75rem;
    display: flex;
    flex-direction: column;
    gap: 2.25rem;
  }

  .timeline-item {
    position: relative;
  }

  .timeline-marker {
    position: absolute;
    left: calc(-1.75rem - 6.5px);
    top: 6px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--color-bg);
    border: 2px solid var(--color-text-muted);
    transition: all 0.3s ease;
  }

  .timeline-item.active .timeline-marker {
    background: var(--accent-primary);
    border-color: var(--accent-primary);
    box-shadow: 0 0 8px var(--accent-primary);
  }

  .timeline-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
  }

  .role-company {
    display: flex;
    flex-direction: column;
  }

  .job-title {
    font-size: 1.15rem;
    font-weight: 600;
  }

  .company-name {
    font-size: 0.95rem;
    color: var(--accent-primary);
    font-weight: 600;
  }

  .job-period {
    font-size: 0.85rem;
    color: var(--color-text-muted);
    font-family: var(--font-mono);
    background: var(--color-border);
    padding: 0.2rem 0.6rem;
    border-radius: 4px;
  }

  .client-badge {
    display: inline-block;
    font-size: 0.8rem;
    color: var(--color-text-secondary);
    background: rgba(99, 102, 241, 0.1);
    border: 1px solid rgba(99, 102, 241, 0.2);
    padding: 0.15rem 0.5rem;
    border-radius: 4px;
    margin-bottom: 0.75rem;
    font-style: italic;
  }

  .job-highlights {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }

  .job-highlights li {
    position: relative;
    padding-left: 1.25rem;
    font-size: 0.95rem;
    color: var(--color-text-secondary);
  }

  .job-highlights li::before {
    content: "•";
    position: absolute;
    left: 0;
    color: var(--accent-secondary);
    font-size: 1.2rem;
    line-height: 1;
    top: -1px;
  }

  /* Expandible interactivo */
  .expand-btn {
    background: none;
    border: 1px solid var(--color-border);
    color: var(--color-text-secondary);
    padding: 0.4rem 0.8rem;
    border-radius: 6px;
    font-size: 0.8rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.4rem;
    margin-top: 0.5rem;
    transition: all 0.2s ease;
  }

  .expand-btn:hover {
    border-color: var(--accent-primary);
    color: var(--color-text-primary);
    background: var(--accent-glow);
  }

  .expand-btn svg {
    transition: transform 0.3s ease;
  }

  .expand-btn[aria-expanded="true"] svg {
    transform: rotate(180deg);
  }

  .timeline-details {
    overflow: hidden;
    transition: max-height 0.4s ease-out, opacity 0.3s ease;
  }

  .timeline-details.collapsed {
    max-height: 0;
    opacity: 0;
    margin-top: 0;
    pointer-events: none;
  }

  .timeline-details:not(.collapsed) {
    max-height: 500px; /* Suficiente para el contenido */
    opacity: 1;
    margin-top: 0.75rem;
  }

  /* ==========================================
     HABILIDADES (FILTROS Y GRILLA)
     ========================================== */
  .skills-filter-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 1.75rem;
  }

  .filter-btn {
    background: var(--color-border);
    border: 1px solid transparent;
    color: var(--color-text-secondary);
    padding: 0.45rem 1rem;
    border-radius: 8px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
  }

  .filter-btn:hover {
    background: var(--color-border-hover);
    color: var(--color-text-primary);
  }

  .filter-btn.active {
    background: var(--accent-grad);
    color: #ffffff;
    font-weight: 600;
    box-shadow: 0 4px 10px rgba(99, 102, 241, 0.3);
  }

  .skills-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }

  @media (min-width: 768px) {
    .skills-grid {
      grid-template-columns: repeat(2, 1fr);
    }
  }

  .skill-category-card {
    background: rgba(15, 23, 42, 0.2);
    border: 1px solid var(--color-border);
    border-radius: 12px;
    padding: 1.25rem;
    transition: all 0.3s ease;
  }

  .skill-category-card.fade-out {
    opacity: 0.15;
    transform: scale(0.97);
  }

  .skill-cat-title {
    font-size: 1rem;
    margin-bottom: 1rem;
    color: var(--color-text-primary);
    font-weight: 600;
    border-bottom: 1px solid var(--color-border);
    padding-bottom: 0.5rem;
  }

  .skill-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .skill-tag {
    background: var(--color-border);
    border: 1px solid var(--color-border);
    color: var(--color-text-secondary);
    font-size: 0.8rem;
    padding: 0.3rem 0.7rem;
    border-radius: 6px;
    transition: all 0.2s ease;
    font-family: var(--font-mono);
  }

  .skill-tag.highlight {
    background: var(--accent-glow);
    border-color: rgba(0, 242, 254, 0.25);
    color: var(--accent-primary);
    font-weight: 600;
  }

  .skill-category-card:hover {
    border-color: var(--accent-secondary);
  }

  /* ==========================================
     EDUCACIÓN Y CERTIFICACIONES
     ========================================== */
  .edu-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1.25rem;
  }

  @media (min-width: 768px) {
    .edu-grid {
      grid-template-columns: repeat(2, 1fr);
    }
  }

  .edu-card {
    display: flex;
    gap: 1rem;
    background: rgba(15, 23, 42, 0.25);
    border: 1px solid var(--color-border);
    border-radius: 12px;
    padding: 1.25rem;
    transition: all 0.3s ease;
  }

  .edu-card:hover {
    transform: translateY(-2px);
    border-color: var(--accent-primary);
  }

  .edu-icon {
    width: 44px;
    height: 44px;
    border-radius: 10px;
    background: var(--accent-glow);
    color: var(--accent-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
  }

  .edu-info {
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .edu-title {
    font-size: 0.95rem;
    font-weight: 600;
    line-height: 1.3;
    margin-bottom: 0.25rem;
  }

  .edu-institution {
    font-size: 0.85rem;
    color: var(--color-text-secondary);
  }

  .edu-year {
    font-size: 0.8rem;
    color: var(--color-text-muted);
    font-family: var(--font-mono);
    margin-top: 0.25rem;
  }

  /* ==========================================
     FORMULARIO DE CONTACTO
     ========================================== */
  .contact-form {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    margin-top: 1.5rem;
  }

  .form-row {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1.25rem;
  }

  @media (min-width: 768px) {
    .form-row {
      grid-template-columns: repeat(2, 1fr);
    }
  }

  .form-group {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
  }

  .form-group label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--color-text-secondary);
  }

  .form-group input, .form-group textarea {
    background: rgba(15, 23, 42, 0.3);
    border: 1px solid var(--color-border);
    border-radius: 8px;
    color: var(--color-text-primary);
    padding: 0.75rem 1rem;
    font-family: var(--font-sans);
    font-size: 0.95rem;
    transition: all 0.2s ease;
  }

  .form-group input:focus, .form-group textarea:focus {
    outline: none;
    border-color: var(--accent-primary);
    background: rgba(15, 23, 42, 0.45);
    box-shadow: 0 0 0 3px var(--accent-glow);
  }

  /* Validación interactiva con :user-invalid */
  .form-group input:user-invalid, .form-group textarea:user-invalid {
    border-color: #ef4444;
    background: rgba(239, 68, 68, 0.05);
  }

  .form-group input:user-invalid ~ .error-msg, 
  .form-group textarea:user-invalid ~ .error-msg {
    display: block;
  }

  .error-msg {
    display: none;
    font-size: 0.75rem;
    color: #ef4444;
    font-weight: 500;
  }

  .btn {
    border: none;
    border-radius: 8px;
    padding: 0.6rem 1.2rem;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: all 0.25s ease;
  }

  .btn-primary {
    background: var(--accent-grad);
    color: #ffffff;
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.25);
  }

  .btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 16px rgba(99, 102, 241, 0.4);
    opacity: 0.95;
  }

  .btn-secondary {
    background: var(--color-border);
    border: 1px solid var(--color-border);
    color: var(--color-text-secondary);
  }

  .btn-secondary:hover {
    background: var(--color-border-hover);
    color: var(--color-text-primary);
  }

  .btn-sm {
    padding: 0.4rem 0.8rem;
    font-size: 0.8rem;
    border-radius: 6px;
  }

  .form-actions {
    display: flex;
    justify-content: flex-end;
  }

  .send-icon {
    transition: transform 0.3s ease;
  }

  .btn-primary:hover .send-icon {
    transform: translate(2px, -2px) scale(1.05);
  }

  /* Alertas */
  .alert {
    padding: 1rem;
    border-radius: 8px;
    display: flex;
    gap: 0.75rem;
    align-items: center;
    font-size: 0.9rem;
    margin-top: 1rem;
  }

  .alert-success {
    background-color: rgba(16, 185, 129, 0.1);
    border: 1px solid rgba(16, 185, 129, 0.2);
    color: #10b981;
  }

  .alert-error {
    background-color: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.2);
    color: #ef4444;
  }
}

@layer utilities {
  /* Clases de utilidad para layout y espaciado */
  .text-center { text-align: center; }
  .w-full { width: 100%; }
}

/* ==========================================
   SOPORTE PARA REDUCCIÓN DE MOVIMIENTO
   ========================================== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-delay: -1ms !important;
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    background-attachment: scroll !important;
    scroll-behavior: auto !important;
    transition-duration: 0s !important;
    transition-delay: 0s !important;
  }
  
  .bg-glow-orb-1, .bg-glow-orb-2 {
    animation: none !important;
  }
  
  .avatar-pulse {
    animation: none !important;
  }
}
