@tailwind base;
@tailwind components;
@tailwind utilities;

/* ==========================================================================
   CALCULADORA SOLAR PRO - CÓDIGO INTEGRAL COMPLETO (CORREGIDO RESPONSIVE)
   ========================================================================== */

/* 1. VARIABLES DE ENTORNO Y DISEÑO */
:root {
  --color-primary: #f59e0b;
  --color-primary-light: #fbbf24;
  --color-primary-dark: #d97706;
  --color-accent: #f97316;
  --color-background: #fefbf7;
  --color-surface: #ffffff;
  --color-surface-alt: #f9f5f0;
  --color-border: #e5e7eb;
  --color-text: #1f2937;
  --color-text-muted: #6b7280;
  --color-text-light: #9ca3af;
  
  --gradient-solar: linear-gradient(135deg, var(--color-primary), var(--color-primary-light));
  --gradient-sky: linear-gradient(180deg, #e0f2fe, #fff3e0);
  
  --shadow-soft: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  --shadow-medium: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
  --shadow-solar: 0 10px 30px -10px rgba(245, 158, 11, 0.3);
  --shadow-glow: 0 0 40px rgba(251, 191, 36, 0.4);
  
  --radius: 12px;
  --radius-lg: 16px;
  --spacing-xs: 0.25rem;
  --spacing-sm: 0.5rem;
  --spacing-md: 1rem;
  --spacing-lg: 1.5rem;
  --spacing-xl: 2rem;
  --spacing-2xl: 3rem;

  --transition-smooth: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

  /* Shadcn compatibility */
  --background: 40 33% 98%;
  --foreground: 220 14% 15%;
  --card: 0 0% 100%;
  --card-foreground: 220 14% 15%;
  --popover: 0 0% 100%;
  --popover-foreground: 220 14% 15%;
  --primary: 38 92% 50%;
  --primary-foreground: 0 0% 100%;
  --secondary: 35 30% 96%;
  --secondary-foreground: 220 14% 15%;
  --muted: 35 20% 96%;
  --muted-foreground: 220 9% 46%;
  --accent: 24 95% 53%;
  --accent-foreground: 0 0% 100%;
  --destructive: 0 84% 60%;
  --destructive-foreground: 0 0% 100%;
  --border: 220 13% 91%;
  --input: 220 13% 91%;
  --ring: 38 92% 50%;
  --sidebar-background: 0 0% 98%;
  --sidebar-foreground: 240 5.3% 26.1%;
  --sidebar-primary: 240 5.9% 10%;
  --sidebar-primary-foreground: 0 0% 98%;
  --sidebar-accent: 240 4.8% 95.9%;
  --sidebar-accent-foreground: 240 5.9% 10%;
  --sidebar-border: 220 13% 91%;
  --sidebar-ring: 38 92% 50%;
}

.dark {
  --background: 220 14% 10%;
  --foreground: 40 33% 98%;
  --card: 220 14% 12%;
  --card-foreground: 40 33% 98%;
  --popover: 220 14% 12%;
  --popover-foreground: 40 33% 98%;
  --primary: 38 92% 50%;
  --primary-foreground: 220 14% 10%;
  --secondary: 220 14% 18%;
  --secondary-foreground: 40 33% 98%;
  --muted: 220 14% 18%;
  --muted-foreground: 220 9% 65%;
  --accent: 24 95% 53%;
  --accent-foreground: 0 0% 100%;
  --destructive: 0 62% 30%;
  --destructive-foreground: 40 33% 98%;
  --border: 220 14% 20%;
  --input: 220 14% 20%;
  --ring: 38 92% 50%;
  --sidebar-background: 240 5.9% 10%;
  --sidebar-foreground: 240 4.8% 95.9%;
  --sidebar-primary: 224.3 76.3% 48%;
  --sidebar-primary-foreground: 0 0% 100%;
  --sidebar-accent: 240 3.7% 15.9%;
  --sidebar-accent-foreground: 240 4.8% 95.9%;
  --sidebar-border: 240 3.7% 15.9%;
  --sidebar-ring: 38 92% 50%;
}

/* 2. RESET CRÍTICO ANTI-DESBORDE - MEJORADO */
*, *::before, *::after {
  box-sizing: border-box;
}

html {
  overflow-x: hidden;
}

body {
  overflow-x: hidden;
  width: 100%;
  min-width: 0;
}

.solar-calculator-wrapper, 
.solar-calculator-wrapper *,
.solar-calculator-wrapper *::before,
.solar-calculator-wrapper *::after {
  box-sizing: border-box;
  max-width: 100%;
  min-width: 0;
}

.solar-calculator-wrapper {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--gradient-sky);
  color: var(--color-text);
  line-height: 1.6;
  min-height: 80vh;
  padding: var(--spacing-lg) 0;
  width: 100%;
  overflow-x: hidden;
  overflow-y: auto;
}

.solar-container {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 var(--spacing-md);
  width: 100%;
  overflow-x: hidden;
}

/* 3. HEADER & ANIMATIONS */
.solar-header {
  text-align: center;
  margin-bottom: var(--spacing-xl);
  padding: 0 var(--spacing-sm);
}

.header-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--spacing-md);
  width: 100%;
}

.sun-icon {
  font-size: 2.5rem;
  animation: pulse-glow 2s ease-in-out infinite;
}

@keyframes pulse-glow {
  0%, 100% { 
    transform: scale(1);
    filter: drop-shadow(0 0 20px rgba(245, 158, 11, 0.3));
  }
  50% { 
    transform: scale(1.05);
    filter: drop-shadow(0 0 30px rgba(245, 158, 11, 0.5));
  }
}

.solar-header h1 {
  font-size: clamp(1.5rem, 5vw, 2.5rem);
  font-weight: 700;
  background: var(--gradient-solar);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: var(--spacing-sm);
  line-height: 1.2;
  word-break: break-word;
  padding: 0 var(--spacing-xs);
}

.header-subtitle {
  font-size: clamp(0.875rem, 3vw, 1.125rem);
  color: var(--color-text-muted);
  max-width: 600px;
  margin: 0 auto;
  padding: 0 var(--spacing-sm);
}

/* 4. MAIN LAYOUT GRID - CORREGIDO */
.main-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--spacing-lg);
  align-items: start;
  width: 100%;
  min-width: 0;
}

@media (min-width: 1024px) {
  .main-grid {
    grid-template-columns: minmax(0, 1fr) minmax(0, 2fr);
    gap: var(--spacing-xl);
  }
}

/* 5. CARDS SYSTEM - CORREGIDO */
.solar-card {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-soft);
  border: 1px solid var(--color-border);
  overflow: hidden;
  transition: all 0.3s ease;
  margin-bottom: var(--spacing-lg);
  width: 100%;
  min-width: 0;
}

.solar-card:hover {
  box-shadow: var(--shadow-medium);
}

.card-header {
  padding: var(--spacing-md);
  border-bottom: 1px solid var(--color-border);
}

.card-header h2 {
  font-size: clamp(1rem, 4vw, 1.25rem);
  font-weight: 600;
  margin-bottom: var(--spacing-xs);
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  flex-wrap: wrap;
}

.card-header h3 {
  font-size: clamp(0.95rem, 3.5vw, 1.125rem);
  font-weight: 600;
  margin: 0;
}

.card-header p {
  color: var(--color-text-muted);
  font-size: clamp(0.75rem, 2.5vw, 0.875rem);
  margin: 0;
}

.card-content {
  padding: var(--spacing-md);
  overflow-x: auto;
  min-width: 0;
}

/* 6. TAB SYSTEM - CORREGIDO PARA MÓVIL */
.tabs {
  display: grid;
  grid-template-columns: 1fr;
  background: var(--color-surface-alt);
  border-radius: var(--radius);
  padding: 4px;
  margin-bottom: var(--spacing-lg);
  gap: 4px;
  width: 100%;
}

@media (min-width: 400px) {
  .tabs {
    grid-template-columns: repeat(3, 1fr);
    gap: 0;
  }
}

.tab-button {
  background: none;
  border: none;
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: calc(var(--radius) - 4px);
  font-size: clamp(0.75rem, 2.5vw, 0.875rem);
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  color: var(--color-text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.tab-button.active {
  background: var(--color-surface);
  color: var(--color-text);
  box-shadow: var(--shadow-soft);
}

.tab-content {
  display: none;
  width: 100%;
  min-width: 0;
}

.tab-content.active {
  display: block;
}

/* 7. FORMS & INPUTS - CORREGIDO */
.form-group {
  margin-bottom: var(--spacing-md);
  width: 100%;
  min-width: 0;
}

.form-group label {
  display: block;
  margin-bottom: var(--spacing-sm);
  font-weight: 500;
  color: var(--color-text);
  font-size: clamp(0.8rem, 2.5vw, 0.9rem);
}

.form-group input,
.form-group select {
  width: 100%;
  padding: var(--spacing-sm) var(--spacing-md);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: clamp(0.8rem, 2.5vw, 0.875rem);
  transition: all 0.2s ease;
  background: var(--color-surface);
  min-width: 0;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.1);
}

/* DMS Grid - CORREGIDO para móviles pequeños */
.dms-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--spacing-xs);
  width: 100%;
}

@media (max-width: 360px) {
  .dms-grid {
    grid-template-columns: 1fr 1fr;
    gap: var(--spacing-xs);
  }
}

.dms-grid input {
  min-width: 0;
  padding: var(--spacing-sm) var(--spacing-xs);
  font-size: 0.8rem;
}

/* 8. BUTTONS - CORREGIDO */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: var(--spacing-sm) var(--spacing-md);
  border-radius: var(--radius);
  font-size: clamp(0.75rem, 2.5vw, 0.875rem);
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
  border: none;
  gap: var(--spacing-sm);
  min-width: 0;
  white-space: nowrap;
}

.btn-primary {
  background: var(--gradient-solar);
  color: white;
  box-shadow: var(--shadow-solar);
  width: 100%;
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-glow);
}

.btn-outline {
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  width: 100%;
}

.btn-outline:hover {
  background: var(--color-surface-alt);
  border-color: var(--color-primary);
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 9. UTILITIES & DIVIDERS */
.divider {
  height: 1px;
  background: var(--color-border);
  margin: var(--spacing-md) 0;
}

.hidden { display: none; }

.nearest-city-info {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
  width: 100%;
}

@media (min-width: 480px) {
  .nearest-city-info {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
  }
}

.city-name { 
  font-weight: 600; 
  margin-bottom: var(--spacing-xs);
  word-break: break-word;
}
.province-name { 
  color: var(--color-text-muted); 
  font-size: clamp(0.75rem, 2.5vw, 0.875rem); 
}
.population { 
  color: var(--color-text-light); 
  font-size: clamp(0.65rem, 2vw, 0.75rem); 
}

.badge {
  background: var(--color-surface-alt);
  color: var(--color-text-muted);
  padding: var(--spacing-xs) var(--spacing-sm);
  border-radius: calc(var(--radius) / 2);
  font-size: clamp(0.65rem, 2vw, 0.75rem);
  font-weight: 500;
  white-space: nowrap;
}

/* 10. SUMMARY CARDS SYSTEM - CORREGIDO */
.summary-cards {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--spacing-md);
  margin-bottom: var(--spacing-xl);
  width: 100%;
}

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

@media (min-width: 768px) {
  .summary-cards {
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  }
}

.summary-card {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: var(--spacing-md);
  box-shadow: var(--shadow-soft);
  border: 1px solid var(--color-border);
  transition: all 0.3s ease;
  min-width: 0;
  overflow: hidden;
}

.summary-card:hover {
  box-shadow: var(--shadow-medium);
}

.summary-card.primary {
  box-shadow: var(--shadow-glow);
  border-color: var(--color-primary-light);
}

.summary-content {
  display: flex;
  align-items: center;
  gap: var(--spacing-sm);
  min-width: 0;
}

.summary-content .icon { 
  font-size: clamp(1.5rem, 5vw, 2rem);
  flex-shrink: 0;
}

.summary-content .info {
  min-width: 0;
  overflow: hidden;
}

.summary-content .value {
  font-size: clamp(1.25rem, 4vw, 1.75rem);
  font-weight: 700;
  line-height: 1.2;
  margin-bottom: var(--spacing-xs);
  word-break: break-word;
}

.summary-card.primary .value { color: var(--color-primary); }
.summary-card.accent .value { color: var(--color-accent); }
.summary-card.muted .value { color: var(--color-text-muted); }

.summary-content .label {
  font-size: clamp(0.625rem, 2vw, 0.75rem);
  color: var(--color-text-muted);
  text-transform: uppercase;
  font-weight: 500;
  letter-spacing: 0.05em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 11. CHARTS & TABLES - CORREGIDO */
.chart-tabs {
  display: flex;
  gap: var(--spacing-xs);
  margin-bottom: var(--spacing-lg);
  flex-wrap: wrap;
  width: 100%;
}

.chart-tab {
  background: none;
  border: 1px solid var(--color-border);
  padding: var(--spacing-xs) var(--spacing-sm);
  border-radius: var(--radius);
  font-size: clamp(0.7rem, 2.5vw, 0.875rem);
  cursor: pointer;
  transition: all 0.2s ease;
  color: var(--color-text-muted);
  white-space: nowrap;
  flex: 1 1 auto;
  min-width: fit-content;
  text-align: center;
}

@media (min-width: 480px) {
  .chart-tab {
    flex: 0 0 auto;
    padding: var(--spacing-sm) var(--spacing-md);
  }
}

.chart-tab.active {
  background: var(--color-primary);
  color: white;
  border-color: var(--color-primary);
}

.chart-container {
  position: relative;
  height: 250px;
  margin-bottom: var(--spacing-lg);
  width: 100%;
  min-width: 0;
}

@media (min-width: 768px) {
  .chart-container {
    height: 300px;
  }
}

/* Table Container - CORREGIDO con scroll horizontal */
.table-container {
  overflow-x: auto;
  overflow-y: visible;
  border-radius: var(--radius);
  border: 1px solid var(--color-border);
  width: 100%;
  -webkit-overflow-scrolling: touch;
  margin: 0;
  position: relative;
}

/* Indicador visual de scroll en móvil */
.table-container::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 20px;
  background: linear-gradient(to right, transparent, rgba(0,0,0,0.05));
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.3s ease;
}

@media (min-width: 768px) {
  .table-container::after {
    display: none;
  }
}

#data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: clamp(0.7rem, 2.5vw, 0.875rem);
  min-width: 500px;
}

#data-table th, 
#data-table td {
  padding: var(--spacing-sm) var(--spacing-md);
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  white-space: nowrap;
}

@media (min-width: 768px) {
  #data-table th, 
  #data-table td {
    padding: var(--spacing-md);
  }
}

#data-table th {
  background: var(--color-surface-alt);
  font-weight: 600;
  color: var(--color-text);
  position: sticky;
  top: 0;
  z-index: 1;
}

#data-table td:not(:first-child) { text-align: right; }
#data-table tr:hover { background: var(--color-surface-alt); }

/* 12. FAQ ACCORDION SYSTEM - CORREGIDO */
.faq-section {
  max-width: 1200px;
  margin: var(--spacing-2xl) auto;
  padding: 0 var(--spacing-md);
  width: 100%;
}

.faq-accordion {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
  width: 100%;
}

.faq-item {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 0.75rem;
  overflow: hidden;
  box-shadow: var(--shadow-soft);
  transition: var(--transition-smooth);
  width: 100%;
}

.faq-question {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: var(--spacing-md);
  font-size: clamp(0.875rem, 3vw, 1rem);
  font-weight: 600;
  background: var(--color-surface-alt);
  color: var(--color-text);
  cursor: pointer;
  border: none;
  width: 100%;
  text-align: left;
  transition: var(--transition-smooth);
  gap: var(--spacing-sm);
}

@media (min-width: 768px) {
  .faq-question {
    padding: 1.25rem 1.5rem;
    align-items: center;
  }
}

.faq-question:hover {
  background: #f3f4f6;
}

.faq-icon {
  font-size: 1.25rem;
  font-weight: bold;
  transition: transform 0.3s ease;
  flex-shrink: 0;
  line-height: 1;
}

.faq-question[aria-expanded="true"] .faq-icon {
  transform: rotate(45deg);
}

.faq-answer {
  max-height: 0;
  overflow: hidden;
  padding: 0 var(--spacing-md);
  background: var(--color-surface);
  color: var(--color-text);
  font-size: clamp(0.8rem, 2.5vw, 0.95rem);
  line-height: 1.6;
  transition: max-height 0.3s ease-out;
}

@media (min-width: 768px) {
  .faq-answer {
    padding: 0 1.5rem;
  }
}

.faq-answer p { 
  margin: var(--spacing-md) 0; 
}

/* 13. RESPONSIVE MEDIA QUERIES - COMPLETAS */

/* Extra pequeño (menos de 360px) */
@media (max-width: 359px) {
  .solar-calculator-wrapper {
    padding: var(--spacing-sm) 0;
  }
  
  .solar-container {
    padding: 0 var(--spacing-sm);
  }
  
  .card-header,
  .card-content {
    padding: var(--spacing-sm);
  }
  
  .sun-icon {
    font-size: 2rem;
  }
  
  .summary-content {
    flex-direction: column;
    text-align: center;
    gap: var(--spacing-xs);
  }
  
  .summary-content .icon {
    margin-bottom: var(--spacing-xs);
  }
}

/* Móvil pequeño (360px - 479px) */
@media (min-width: 360px) and (max-width: 479px) {
  .solar-container {
    padding: 0 var(--spacing-sm);
  }
  
  .summary-content {
    flex-direction: row;
    text-align: left;
  }
}

/* Móvil grande (480px - 767px) */
@media (min-width: 480px) and (max-width: 767px) {
  .solar-container {
    padding: 0 var(--spacing-md);
  }
  
  .card-header,
  .card-content {
    padding: var(--spacing-md);
  }
}

/* Tablet (768px - 1023px) */
@media (min-width: 768px) and (max-width: 1023px) {
  .solar-calculator-wrapper {
    padding: var(--spacing-xl) 0;
  }
  
  .solar-container {
    padding: 0 var(--spacing-lg);
  }
  
  .main-grid {
    gap: var(--spacing-xl);
  }
  
  .card-header,
  .card-content {
    padding: var(--spacing-lg);
  }
  
  .summary-cards {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* Desktop (1024px+) */
@media (min-width: 1024px) {
  .solar-calculator-wrapper {
    padding: var(--spacing-xl) 0;
  }
  
  .card-header,
  .card-content {
    padding: var(--spacing-lg);
  }
  
  .chart-container {
    height: 350px;
  }
}

/* 14. ORIENTACIÓN LANDSCAPE EN MÓVIL */
@media (max-width: 767px) and (orientation: landscape) {
  .solar-header {
    margin-bottom: var(--spacing-md);
  }
  
  .sun-icon {
    font-size: 2rem;
  }
  
  .solar-header h1 {
    font-size: 1.5rem;
  }
  
  .chart-container {
    height: 200px;
  }
}

/* 15. PREVENCIÓN DE OVERFLOW GLOBAL */
img, video, iframe, embed, object {
  max-width: 100%;
  height: auto;
}

input, select, textarea, button {
  max-width: 100%;
}

/* Prevenir palabras largas que causen overflow */
p, h1, h2, h3, h4, h5, h6, li, td, th, label, span {
  overflow-wrap: break-word;
  word-wrap: break-word;
  hyphens: auto;
}

/* 16. TAILWIND BASE OVERRIDES */
@layer base {
  * {
    @apply border-border;
  }

  body {
    @apply bg-background text-foreground;
  }
}