/* 3Blue1Brown Style Backpropagation Demo */
/* Afegit: variable per a l'alçada específica dels botons de fletxa del stepper */
:root { --bp-control-height: 40px;}
.backprop-demo {
  border: 1px solid #e2e8f0;
  padding: 1rem;
  border-radius: 16px;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  margin: 1rem auto;
  max-width: 750px;
  font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  position: relative;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  color: #1e293b;
}

.backprop-demo h2 {
  margin: 0 0 0.5rem;
  font-size: 1.5rem;
  font-weight: 700;
  background: linear-gradient(135deg, #3b82f6, #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.bp-intro {
  margin: 0 0 0.25rem; /* reduced from 0.5rem */
  font-size: 0.95rem;
  color: #64748b;
  line-height: 1.6;
}

/* Enhanced Toolbar */
.bp-toolbar {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.65rem;
  align-items: center;
  margin-bottom: 0.25rem; /* reduced from 0.5rem */
  padding: 0.8rem;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 12px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(59, 130, 246, 0.2);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  overflow: hidden; /* clip inner shadows/edges to toolbar radius */
  grid-auto-flow: row dense; /* keep rows compact */
}

/* Nota: usem > perquè els botons interns (fletxes del stepper) NO heretin aquesta alçada */
.bp-toolbar > button {
  background: linear-gradient(135deg, #1e40af, #3b82f6);
  color: #ffffff;
  border: none;
  border-radius: 10px;
  padding: 0.5rem 1rem; /* adjust for 40px height */
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
  white-space: nowrap;
  height: var(--bp-control-height);
  line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
}
/* Primary buttons slightly wider */
#bp-play, #bp-reset { padding: 0.5rem 1.2rem; }

/* Stepper container replaces prev/next buttons */
 .bp-stepper {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: .55rem;
  background: rgba(255,255,255,0.6);
  padding: 0 .65rem;
  border: 1px solid rgba(59,130,246,0.25);
  border-radius: 10px;
  height: var(--bp-control-height);
  min-height: var(--bp-control-height);
  box-shadow: 0 2px 6px rgba(0,0,0,0.06);
 }
 .bp-stepper-label {
  font-size: .66rem;
  text-transform: none; /* keep capitalization as written (Progrés) */
  letter-spacing: .05em;
  font-weight: 600;
  color: #1e40af;
  background: linear-gradient(135deg,#e5efff,#d0e2ff);
  padding: 0 .55rem; /* remove vertical padding to lower visual height */
  border-radius: 6px;
  box-shadow: 0 1px 1px rgba(0,0,0,0.06) inset;
  display: flex; align-items: center; height: 60%; line-height: 1; 
 }
 .bp-stepper-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px; /* standard track height */
  background: linear-gradient(90deg,#1d4ed8,#3b82f6,#60a5fa);
  border-radius: 4px;
  outline: none;
  position: relative;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.35),0 1px 3px rgba(0,0,0,0.25);
 }
 .bp-stepper-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  background: #ffffff;
  border: 2px solid #1d4ed8;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,0.35);
  transition: transform .15s, box-shadow .2s;
 }
 .bp-stepper-slider::-webkit-slider-thumb:hover { transform: scale(1.13); box-shadow: 0 2px 6px rgba(59,130,246,.45); }
 .bp-stepper-slider::-moz-range-thumb {
  width: 16px; height: 16px; background:#ffffff; border:2px solid #1d4ed8; border-radius:50%; cursor:pointer; box-shadow:0 1px 3px rgba(0,0,0,.35); transition:transform .15s, box-shadow .2s;
 }
 .bp-stepper-slider::-moz-range-thumb:hover { transform: scale(1.13); }
 .bp-stepper-slider:focus-visible { outline: 2px solid #fbbf24; outline-offset: 3px; }
 .bp-stepper-buttons { display:flex; align-items:center; gap:.4rem; }
 .bp-stepper-btn {
  background: #ffffff;
  border: 1px solid #93c5fd;
  color: #1e40af;
  font-weight: 600;
  border-radius: 6px;
  cursor: pointer;
  height: 28px;
  width: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: .8rem;
  padding: 0;
  line-height: 1;
  transition: background .2s, color .2s, box-shadow .2s, transform .15s;
  align-self: center; /* centra verticalment dins el contenidor de 40px */
 }
 .bp-stepper-btn:hover:not(:disabled) { background: #eff6ff; color:#1e3a8a; transform: translateY(-1px); box-shadow:0 2px 6px rgba(59,130,246,.35); }
 .bp-stepper-btn:disabled { opacity:.35; cursor:not-allowed; transform:none; }

/* Integrate into desktop grid: occupy columns of the two old buttons */
@media (min-width: 992px) {
  .bp-toolbar #bp-stepper { grid-column: 4 / span 6; }
}

.bp-toolbar > button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.4);
  background: linear-gradient(135deg, #2563eb, #60a5fa);
}

.bp-toolbar > button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
  background: #94a3b8;
}

/* Control inputs */
 .bp-toolbar .bp-inline {
  font-size: 0.8rem;
  background: rgba(255, 255, 255, 0.9);
  color: #1e293b;
  padding: 0 .7rem;
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(59, 130, 246, 0.2);
  min-width: auto;
  justify-content: space-between;
  box-sizing: border-box;
  justify-self: start;
  width: auto;
  height: var(--bp-control-height);
  line-height: 1;
}
/* Standardized control heights */
.bp-toolbar > button,
.bp-toolbar .bp-inline,
#bp-stepper { min-height: var(--bp-control-height); }

.bp-inline-speed input[type="range"] { width: 110px; }
.bp-inline-lr input[type="range"] { width: 75px; }
.bp-inline-error { min-width: 150px; }
.bp-inline-error code { font-size: 0.85rem; }

/* Speed control: keep single column, left-align content to avoid big empty space */
.bp-toolbar .bp-inline:first-of-type {
  grid-column: auto;
  justify-content: flex-start; /* place label and slider together */
  align-self: center;          /* center this box vertically in grid row */
  justify-self: start;         /* align to left within the grid cell */
  min-width: 0;                /* allow shrinking */
  width: max-content;          /* shrink to content width to avoid empty white */
  padding: 0.3rem 0.5rem;      /* slightly tighter */
}

.bp-toolbar code {
  background: rgba(59, 130, 246, 0.1);
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  font-size: 0.75rem;
  color: #1e40af;
  font-weight: 600;
  font-family: 'SF Mono', monospace;
}

/* Hide the numeric speed value; keep UI minimal */
#speed-value { display: none; }

/* Constrain speed range slider so toolbar items always fit */
.bp-toolbar input[type="range"] {
  width: clamp(90px, 20vw, 120px);
  max-width: 100%;
  align-self: center; /* ensure vertical centering in flex */
  margin-block: 0;    /* remove extra vertical margins */
}

/* Fixed learning rate progress bar */
progress#lr-bar {
  width: 70px;
  height: 6px;
  -webkit-appearance: none;
  appearance: none;
  border: none;
  background: #e2e8f0;
  border-radius: 4px;
  overflow: hidden;
}
progress#lr-bar::-webkit-progress-bar {
  background: #e2e8f0;
  border-radius: 4px;
}
progress#lr-bar::-webkit-progress-value {
  background: linear-gradient(90deg,#3b82f6,#6366f1);
  border-radius: 4px;
}
progress#lr-bar::-moz-progress-bar {
  background: linear-gradient(90deg,#3b82f6,#6366f1);
  border-radius: 4px;
}

/* Enhanced Canvas */
#bp-network {
  background: linear-gradient(135deg, #ffffff, #f8fafc);
  border: 2px solid #3b82f6;
  border-radius: 16px;
  width: 100%;
  max-width: 100%;
  height: auto;
  aspect-ratio: 5/2; /* 2.5:1 ratio for wide, shallow canvas */
  display: block;
  box-shadow: 0 4px 16px rgba(59, 130, 246, 0.1);
  order: 2;
  cursor: crosshair;
}

/* Phase Label Enhancement */
.bp-phase-label {
  margin: 0.2rem 0; /* reduced from 0.4rem */
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.025em;
  color: #ffffff;
  background: linear-gradient(135deg, #1e40af, #3b82f6);
  display: inline-block;
  padding: 0.6rem 1.2rem;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(59, 130, 246, 0.3);
  border: 1px solid rgba(59, 130, 246, 0.4);
  transition: all 0.3s ease;
  text-align: center;
  width: 100%;
  box-sizing: border-box;
}

/* Canvas Wrapper & Equation Overlay */
.bp-canvas-wrapper {
  position: relative;
  margin: 0.2rem auto; /* tighter */
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem; /* tighter */
  max-width: 100%;
  width: 100%;
}

/* Tooltip for nodes/edges */
.bp-tooltip {
  position: absolute;
  pointer-events: none;
  background: #0f172a;
  color: #e2e8f0;
  border: 1px solid rgba(148,163,184,0.4);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.4;
  box-shadow: 0 6px 20px rgba(2,6,23,0.5);
  z-index: 20;
  min-width: 160px;
}
.bp-tip-row { display: flex; align-items: center; gap: 6px; margin: 2px 0; }
.bp-tip-label { color: #93c5fd; font-weight: 600; min-width: 64px; }
.bp-color { display: inline-block; width: 12px; height: 12px; border-radius: 3px; border: 1px solid rgba(255,255,255,0.4); margin-right: 4px; vertical-align: -2px; }

.bp-equation-overlay {
  position: static;
  font-family: 'SF Mono', 'Monaco', 'Inconsolata', 'Roboto Mono', monospace;
  font-size: 0.9rem;
  background: rgba(255, 255, 255, 0.95);
  color: #1e293b;
  padding: 0.9rem 1rem;
  border: 2px solid #3b82f6;
  border-radius: 12px;
  width: 100%;
  line-height: 1.6;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(20px);
  transition: all 0.4s ease;
  z-index: 10;
  order: 1;
  box-sizing: border-box;
}

.eq-line {
  margin: 0.12rem 0; /* tighter */
  padding: 0.15rem 0; /* tighter */
  transition: all 0.5s ease;
  border-left: 3px solid transparent;
  padding-left: 0.6rem;
}

.eq-line.active {
  background: rgba(59, 130, 246, 0.1);
  border-left-color: #3b82f6;
  border-radius: 0 6px 6px 0;
  font-weight: 600;
  color: #1e40af;
}

/* Interactive Questions Section */
.bp-questions {
  margin-top: 0.3rem; /* tighter */
  padding: 1rem;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 12px;
  border: 1px solid rgba(59, 130, 246, 0.2);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.bp-questions h3 {
  margin: 0 0 1rem;
  color: #1e40af;
  font-size: 1.1rem;
  font-weight: 700;
}

.bp-questions details {
  margin: 0.5rem 0;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  overflow: hidden;
}

.bp-questions summary {
  padding: 0.6rem 0.8rem;
  background: linear-gradient(135deg, #f8fafc, #e2e8f0);
  cursor: pointer;
  font-weight: 600;
  color: #1e40af;
  transition: all 0.2s ease;
  border-bottom: 1px solid transparent;
}

.bp-questions summary:hover {
  background: linear-gradient(135deg, #dbeafe, #bfdbfe);
}

.bp-questions details[open] summary {
  border-bottom-color: #e2e8f0;
}

.bp-questions details p {
  padding: 0.75rem;
  margin: 0;
  background: #ffffff;
  color: #475569;
  line-height: 1.6;
}

/* Remove redundant elements */
.bp-formulas,
.bp-mini-legend {
  display: none;
}

/* Responsive Design */
@media (min-width: 1200px) {
  .bp-canvas-wrapper {
    max-width: 750px;
  margin: 1rem auto;
  gap: 0.75rem;
  }
  
  .bp-equation-overlay {
    font-size: 1rem;
  padding: 1rem 1.25rem;
  }
}

/* On desktop, force 4 columns so only the 4 buttons fit on the first row */
@media (min-width: 992px) {
  .bp-toolbar {
    /* initial definition; will be overridden by refined layout below */
    grid-template-columns: repeat(12, 1fr);
    column-gap: .6rem;
    row-gap: .55rem;
  }
  /* Balanced heights */
  /* Heights already standardized globally */
  /* Distribution: compact Play, generous Progress, medium Reset */
  .bp-toolbar #bp-play    { grid-column: 1 / span 2; grid-row: 1; }
  .bp-toolbar #bp-stepper { grid-column: 3 / span 5; grid-row: 1; }
  .bp-toolbar #bp-reset   { grid-column: 8 / span 3; grid-row: 1; }
  /* Stepper refinement */
  #bp-stepper { padding: .38rem .6rem; }
  #bp-stepper .bp-stepper-label { padding: .34rem .6rem; font-size: .66rem; }
  #bp-stepper .bp-stepper-slider { height:6px; }
  /* Second row alignment */
  /* Intercanvi d'ordre: velocitat a l'esquerra, LR a la dreta */
  .bp-toolbar .bp-inline-speed  { grid-column: 1 / span 2; grid-row: 2; }
  .bp-toolbar .bp-inline-lr     { grid-column: 3 / span 3; grid-row: 2; }
  .bp-toolbar .bp-inline-error  { grid-column: 6 / span 2; grid-row: 2; }
  .bp-toolbar .bp-inline-lr,
  .bp-toolbar .bp-inline-speed,
  .bp-toolbar .bp-inline-error { width: max-content; justify-self: start; }
}

/* Refined wide-screen toolbar layout: single row with adaptive stepper */
@media (min-width: 1080px) {
  .bp-toolbar {
    display: flex; /* override grid */
    flex-wrap: wrap;
    align-items: center;
    gap: .6rem; /* uniform medium gap */
    padding: .7rem .85rem; /* slightly more breathing room */
  }
  /* Order kept natural; widths standardized */
  #bp-play, #bp-reset { flex: 0 0 auto; min-width: 150px; text-align: center; }
  #bp-stepper {
    flex: 0 0 auto;
    width: 300px; /* moderate slider length */
    max-width: 320px;
    min-width: 280px;
    padding: .34rem .6rem; /* align with standard height */
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: .5rem;
  }
  #bp-stepper .bp-stepper-slider { width: 100%; }
  .bp-toolbar .bp-inline { flex: 0 0 auto; }
  .bp-inline-error { min-width: 130px; }
  /* Heights standardized globally */
}

/* Medium screens: two rows, stepper full width first row */
@media (min-width: 768px) and (max-width: 1079px) {
  .bp-toolbar {
    grid-template-columns: auto 1fr auto auto; /* play | stepper | reset | error */
    grid-template-areas:
      "play stepper stepper reset"
      "speed speed lr error"; /* velocitat ara primera */
  }
  #bp-play { grid-area: play; }
  #bp-stepper { grid-area: stepper; width: 100%; max-width: 420px; justify-self: start; }
  #bp-play { max-width: 150px; }
  #bp-reset { grid-area: reset; }
  .bp-inline-speed { grid-area: speed; }
  .bp-inline-lr { grid-area: lr; }
  .bp-inline-error { grid-area: error; }
  /* Heights standardized globally */
  .bp-inline-error { min-width: 140px; }
}

/* Small screens already handled; tighten inline min widths */
@media (max-width: 767px) {
  .bp-inline-error { min-width: 0; width: 100%; }
  .bp-toolbar .bp-inline { width: 100%; }
}

@media (max-width: 1199px) and (min-width: 768px) {
  .bp-canvas-wrapper {
    max-width: 100%;
  margin: 1rem auto;
  gap: 0.75rem;
  }
}

@media (max-width: 767px) {
  .bp-canvas-wrapper {
  gap: 0.5rem;
  margin: 0.5rem auto;
    max-width: calc(100% - 1rem);
  }
  
  .bp-equation-overlay {
    font-size: 0.8rem;
  padding: 0.75rem 0.9rem;
    margin: 0;
  }
  
  #bp-network { 
    max-width: 100%;
    aspect-ratio: 4/2; /* slightly taller ratio for mobile */
  }
  
  .bp-toolbar { 
  grid-template-columns: 1fr;
  gap: 0.5rem;
  }
  /* Reset to normal span on mobile so it stacks cleanly */
  .bp-toolbar .bp-inline:first-of-type {
    grid-column: auto;
  }
  .bp-toolbar input[type="range"] {
    width: 100%;
    max-width: 100%;
  }
  
  .backprop-demo { 
  padding: 1rem; 
  margin: 0.75rem; 
    max-width: calc(100% - 2rem);
  }
  
  .bp-phase-label {
    font-size: 0.95rem;
    padding: 0.6rem 1rem;
  }
  
  .bp-mini-legend {
    grid-template-columns: repeat(2, 1fr);
  }
  
  .legend-chip {
    font-size: 0.65rem;
    padding: 0.4rem 0.6rem;
  }
}

@media (max-width: 480px) {
  .bp-mini-legend {
    grid-template-columns: 1fr;
  }
  
  .bp-toolbar .bp-inline {
    min-width: auto;
    width: 100%;
  }
  
  .bp-equation-overlay {
  font-size: 0.75rem;
  padding: 0.6rem 0.8rem;
  }
  
  #bp-network {
    height: 320px;
  }
  
  .backprop-demo {
    margin: 0.5rem;
    max-width: calc(100% - 1rem);
  }
}
