/* ════════════════════════════════════════════════════════════════════════════
   Graficador de Series de Fourier — MAT2208
   Diseño: premium dark theme con glassmorphism
   ════════════════════════════════════════════════════════════════════════════ */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap');

/* ── Tokens de diseño ─────────────────────────────────────────────────────── */
:root {
  --bg-base:       #060b17;
  --bg-surface:    #0b1322;
  --bg-card:       rgba(255,255,255,0.032);
  --bg-card-hover: rgba(255,255,255,0.055);
  --border:        rgba(255,255,255,0.08);
  --border-active: rgba(129,140,248,0.45);

  --accent-1:      #818cf8;       /* indigo */
  --accent-2:      #c084fc;       /* violet */
  --accent-grad:   linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  --accent-glow:   rgba(129,140,248,0.25);

  --plot-f:        #60a5fa;       /* azul: f(x) original */
  --plot-sn:       #f472b6;       /* rosa: Sₙ(x)         */

  --text-primary:   #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted:     #475569;

  --success:  #34d399;
  --warning:  #fbbf24;
  --danger:   #f87171;

  --radius-sm:  6px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-xl: 24px;

  --shadow-sm:  0 2px 8px rgba(0,0,0,0.4);
  --shadow-md:  0 4px 24px rgba(0,0,0,0.5);
  --shadow-lg:  0 8px 48px rgba(0,0,0,0.6);
  --shadow-glow:0 0 32px rgba(129,140,248,0.18);

  --transition: 0.22s cubic-bezier(.4,0,.2,1);

  --font:      'Inter', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
}

/* ── Reset ────────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { scroll-behavior: smooth; }

body {
  font-family: var(--font);
  background: var(--bg-base);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.6;
  overflow-x: hidden;
}

/* Fondo animado sutil */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 80% 60% at 20% 10%,  rgba(102,126,234,0.07) 0%, transparent 60%),
    radial-gradient(ellipse 60% 50% at 80% 90%, rgba(192,132,252,0.06) 0%, transparent 60%);
  pointer-events: none;
  z-index: 0;
}

/* ── Header ───────────────────────────────────────────────────────────────── */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 28px;
  background: rgba(6,11,23,0.85);
  backdrop-filter: blur(20px) saturate(1.4);
  border-bottom: 1px solid var(--border);
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
}

.header-icon {
  width: 38px;
  height: 38px;
  border-radius: var(--radius-md);
  background: var(--accent-grad);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: var(--shadow-glow);
}

.header-title {
  font-size: 1.1rem;
  font-weight: 700;
  background: var(--accent-grad);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.02em;
}

.header-subtitle {
  font-size: 0.72rem;
  color: var(--text-muted);
  letter-spacing: 0.05em;
  text-transform: uppercase;
  font-weight: 500;
}

.header-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

/* ── Layout principal ─────────────────────────────────────────────────────── */
.app-layout {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: 410px 1fr;
  grid-template-rows: auto 1fr;
  min-height: calc(100vh - 66px);
  gap: 0;
}

/* ── Panel izquierdo (controles) ──────────────────────────────────────────── */
.control-panel {
  grid-row: 1 / 3;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.control-panel::-webkit-scrollbar { width: 4px; }
.control-panel::-webkit-scrollbar-track { background: transparent; }
.control-panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ── Panel derecho (gráfica) ──────────────────────────────────────────────── */
.chart-panel {
  padding: 20px 20px 10px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#fourier-chart {
  flex: 1;
  min-height: 450px;
  max-height: 650px;
  width: 100%;
  border-radius: var(--radius-lg);
  overflow: hidden;
  border: 1px solid var(--border);
  background: rgba(13,19,40,0.6);
  backdrop-filter: blur(4px);
}

/* ── Panel inferior (datos analíticos) ───────────────────────────────────── */
.data-panel {
  padding: 14px 20px 18px;
  border-top: 1px solid var(--border);
  background: rgba(11,19,34,0.6);
  backdrop-filter: blur(4px);
}

/* ── Cards / Secciones ────────────────────────────────────────────────────── */
.section-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px;
  transition: background var(--transition), border-color var(--transition);
}

.section-card:hover { background: var(--bg-card-hover); }

.section-label {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.section-label::before {
  content: '';
  display: inline-block;
  width: 3px;
  height: 12px;
  border-radius: 2px;
  background: var(--accent-grad);
}

/* ── Botones ──────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.83rem;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
  text-decoration: none;
}

.btn-primary {
  background: var(--accent-grad);
  color: #fff;
  box-shadow: 0 2px 16px rgba(102,126,234,0.35);
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 24px rgba(102,126,234,0.5);
}

.btn-primary:active { transform: translateY(0); }

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

.btn-secondary:hover {
  background: rgba(255,255,255,0.1);
  color: var(--text-primary);
  border-color: rgba(255,255,255,0.15);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  padding: 7px 12px;
}

.btn-ghost:hover {
  background: rgba(255,255,255,0.06);
  color: var(--text-primary);
}

.btn-danger {
  background: rgba(248,113,113,0.12);
  border: 1px solid rgba(248,113,113,0.25);
  color: var(--danger);
}

.btn-danger:hover {
  background: rgba(248,113,113,0.2);
}

.btn-sm { padding: 5px 10px; font-size: 0.78rem; }
.btn-icon { padding: 8px; gap: 0; }
.btn-full { width: 100%; justify-content: center; }

/* ── Inputs ───────────────────────────────────────────────────────────────── */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}

.form-label {
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.form-input {
  background: rgba(0,0,0,0.35);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 10px;
  color: var(--text-primary);
  font-family: var(--font-mono);
  font-size: 0.88rem;
  transition: border-color var(--transition), box-shadow var(--transition);
  width: 100%;
}

.form-input:focus {
  outline: none;
  border-color: var(--border-active);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.form-input.error {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px rgba(248,113,113,0.15);
}

.form-input::placeholder { color: var(--text-muted); }

.form-select {
  background: rgba(0,0,0,0.35);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 0.85rem;
  cursor: pointer;
  width: 100%;
  transition: border-color var(--transition);
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%2394a3b8'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 20px;
  padding-right: 36px;
}

.form-select:focus {
  outline: none;
  border-color: var(--border-active);
}

.form-select option {
  background: #0d1428;
  color: var(--text-primary);
}

/* ── Slider N ─────────────────────────────────────────────────────────────── */
.slider-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.slider-value {
  background: var(--accent-grad);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-weight: 700;
  font-size: 0.95rem;
  font-family: var(--font-mono);
  min-width: 40px;
  text-align: right;
}

.range-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(
    to right,
    #667eea var(--pct, 28%),
    rgba(255,255,255,0.1) var(--pct, 28%)
  );
  outline: none;
  cursor: pointer;
}

.range-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent-grad);
  box-shadow: 0 0 0 3px rgba(102,126,234,0.3), var(--shadow-sm);
  cursor: pointer;
  transition: box-shadow var(--transition), transform var(--transition);
}

.range-slider::-webkit-slider-thumb:hover {
  box-shadow: 0 0 0 6px rgba(102,126,234,0.25), var(--shadow-sm);
  transform: scale(1.1);
}

.range-ticks {
  display: flex;
  justify-content: space-between;
  margin-top: 4px;
}

.range-tick {
  font-size: 0.68rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

/* ── Radio / Toggle mode ──────────────────────────────────────────────────── */
.mode-toggle {
  display: flex;
  gap: 6px;
}

.mode-btn {
  flex: 1;
  padding: 8px;
  text-align: center;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-family: var(--font);
  transition: all var(--transition);
}

.mode-btn.active {
  background: rgba(129,140,248,0.15);
  border-color: var(--accent-1);
  color: var(--accent-1);
}

/* ── Piezas de función ────────────────────────────────────────────────────── */
.pieces-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.piece-card {
  background: rgba(0,0,0,0.25);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  position: relative;
  transition: border-color var(--transition);
}

.piece-card:hover { border-color: rgba(255,255,255,0.14); }

.piece-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.piece-title {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--accent-1);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.piece-limits {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-top: 8px;
}

.piece-limit-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-bottom: 3px;
}

/* ── Datos analíticos ─────────────────────────────────────────────────────── */
.data-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}

.data-chip {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  transition: background var(--transition);
}

.data-chip:hover { background: var(--bg-card-hover); }

.data-chip-label {
  font-size: 0.68rem;
  color: var(--text-muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 4px;
}

.data-chip-value {
  font-family: var(--font-mono);
  font-size: 0.95rem;
  font-weight: 500;
  color: var(--text-primary);
}

.data-chip-value.highlight {
  background: var(--accent-grad);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* ── Tabla de coeficientes ────────────────────────────────────────────────── */
.coeff-table-wrap {
  margin-top: 12px;
  overflow-x: auto;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
}

.coeff-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8rem;
  font-family: var(--font-mono);
}

.coeff-table th {
  background: rgba(102,126,234,0.12);
  color: var(--accent-1);
  font-weight: 600;
  text-align: center;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.coeff-table td {
  padding: 6px 12px;
  text-align: center;
  color: var(--text-secondary);
  border-bottom: 1px solid rgba(255,255,255,0.03);
  transition: background var(--transition);
}

.coeff-table tr:hover td { background: rgba(255,255,255,0.03); }
.coeff-table tr:last-child td { border-bottom: none; }
.coeff-table td:first-child { color: var(--accent-2); font-weight: 600; }

/* ── Tabs ─────────────────────────────────────────────────────────────────── */
.tabs {
  display: flex;
  gap: 2px;
  background: rgba(0,0,0,0.3);
  border-radius: var(--radius-sm);
  padding: 3px;
  margin-bottom: 14px;
}

.tab-btn {
  flex: 1;
  padding: 7px 10px;
  background: transparent;
  border: none;
  border-radius: 5px;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  font-family: var(--font);
  transition: all var(--transition);
}

.tab-btn.active {
  background: rgba(129,140,248,0.18);
  color: var(--accent-1);
}

/* ── Espectro ─────────────────────────────────────────────────────────────── */
#spectrum-chart {
  min-height: 180px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  overflow: hidden;
}

/* ── Modal historial ──────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.72);
  backdrop-filter: blur(8px);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--transition);
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: all;
}

.modal-box {
  background: #0d1428;
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  width: min(560px, calc(100vw - 40px));
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
  transform: scale(0.95) translateY(20px);
  transition: transform var(--transition);
}

.modal-overlay.open .modal-box {
  transform: scale(1) translateY(0);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
}

.modal-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
}

.modal-body {
  overflow-y: auto;
  padding: 16px 22px;
  flex: 1;
}

.history-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  transition: all var(--transition);
  cursor: pointer;
}

.history-item:hover {
  background: var(--bg-card);
  border-color: var(--border);
}

.history-item-info { flex: 1; }

.history-item-name {
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--text-primary);
}

.history-item-meta {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

/* ── Save modal ───────────────────────────────────────────────────────────── */
.save-modal-body {
  padding: 20px 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ── Toast notifications ──────────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: #1a2340;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 18px;
  font-size: 0.85rem;
  color: var(--text-primary);
  box-shadow: var(--shadow-md);
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 320px;
  animation: slideIn 0.25s ease;
}

.toast.success { border-left: 3px solid var(--success); }
.toast.error   { border-left: 3px solid var(--danger); }
.toast.info    { border-left: 3px solid var(--accent-1); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to   { transform: translateX(0);   opacity: 1; }
}

/* ── Loading spinner ──────────────────────────────────────────────────────── */
.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255,255,255,0.2);
  border-top-color: var(--accent-1);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: none;
}

.spinner.active { display: inline-block; }

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ── Legend dots ──────────────────────────────────────────────────────────── */
.legend-dots {
  display: flex;
  gap: 16px;
  align-items: center;
  margin-top: 8px;
}

.legend-dot {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.legend-dot-color {
  width: 24px;
  height: 3px;
  border-radius: 2px;
}

.legend-dot-color.dashed {
  background: repeating-linear-gradient(
    to right,
    var(--plot-f) 0, var(--plot-f) 6px,
    transparent 6px, transparent 10px
  );
}

.legend-dot-color.solid {
  background: var(--plot-sn);
}

/* ── Divider ──────────────────────────────────────────────────────────────── */
.divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

/* ── Error message ────────────────────────────────────────────────────────── */
.error-msg {
  font-size: 0.78rem;
  color: var(--danger);
  margin-top: 4px;
  display: none;
}

.error-msg.show { display: block; }

/* ── Formula display ──────────────────────────────────────────────────────── */
.formula-box {
  background: rgba(0,0,0,0.3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-secondary);
  overflow-x: auto;
  white-space: nowrap;
  min-height: 36px;
}

/* ── Badge ────────────────────────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 100px;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.badge-trig    { background: rgba(129,140,248,0.15); color: var(--accent-1); }
.badge-complex { background: rgba(192,132,252,0.15); color: var(--accent-2); }
.badge-db-off  { background: rgba(251,191,36,0.12);  color: var(--warning); }

/* ── Responsive ───────────────────────────────────────────────────────────── */
@media (max-width: 950px) {
  .app-layout {
    display: flex;
    flex-direction: column;
    min-height: auto;
  }

  .control-panel {
    border-right: none;
    border-bottom: 1px solid var(--border);
    max-height: none;
    overflow-y: visible;
    padding: 20px;
  }

  .chart-panel {
    padding: 16px 16px 0;
  }

  #fourier-chart {
    min-height: 450px;
  }

  .header-title { font-size: 0.95rem; }
  .data-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 600px) {
  .app-header { padding: 12px 16px; }
  .control-panel { padding: 16px 12px; }
  .chart-panel { padding: 12px 12px 0; }
  .data-panel { padding: 12px; }
  
  .data-grid { grid-template-columns: repeat(2, 1fr); }
  #fourier-chart { min-height: 350px; }
  
  /* Reducir el tamaño de la fuente de la ecuación en móviles si es muy larga */
  math-field { font-size: 1.1rem; }
}

/* ── Modo Claro ───────────────────────────────────────────────────────────── */
body.light-mode {
  --bg-base:       #f1f5f9;
  --bg-surface:    #ffffff;
  --bg-card:       rgba(0,0,0,0.035);
  --bg-card-hover: rgba(0,0,0,0.06);
  --border:        rgba(0,0,0,0.12);
  --border-active: rgba(99,102,241,0.6);

  --accent-1:      #6366f1;
  --accent-2:      #a855f7;
  --accent-glow:   rgba(99,102,241,0.25);

  --plot-f:        #2563eb;
  --plot-sn:       #db2777;

  --text-primary:   #0f172a;
  --text-secondary: #475569;
  --text-muted:     #64748b;
}

body.light-mode .app-header {
  background: rgba(255,255,255,0.85);
}

body.light-mode #fourier-chart {
  background: rgba(255,255,255,0.6);
}

body.light-mode .data-panel {
  background: rgba(255,255,255,0.6);
}

body.light-mode .form-input,
body.light-mode .form-select {
  background: rgba(0,0,0,0.03);
}

body.light-mode .form-select option {
  background: #ffffff;
}

body.light-mode .modal-box {
  background: #ffffff;
}

body.light-mode .toast {
  background: #ffffff;
}
