/* ==========================================================================
   Calculadora de bolo de insulina y corrección - fijate.com
   Estilos canónicos con soporte Dark/Light Mode y diseño táctil optimizado
   ========================================================================== */

:root {
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-heading: 'Outfit', sans-serif;
  --font-mono: 'JetBrains Mono', monospace;

  /* Tema claro por defecto */
  --bg-app: #f8fafc;
  --bg-card: #ffffff;
  --bg-card-subtle: #f1f5f9;
  --border-color: #e2e8f0;
  --border-focus: #0284c7;

  --text-main: #0f172a;
  --text-muted: #475569;
  --text-subtle: #64748b;

  --primary: #0284c7;
  --primary-hover: #0369a1;
  --primary-light: #e0f2fe;

  --teal-accent: #0f766e;
  --teal-light: #ccfbf1;

  --success: #16a34a;
  --success-light: #dcfce7;
  --warning: #d97706;
  --warning-light: #fef3c7;
  --danger: #dc2626;
  --danger-light: #fee2e2;
  --purple: #9333ea;
  --purple-light: #f3e8ff;

  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -1px rgba(0, 0, 0, 0.04);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -2px rgba(0, 0, 0, 0.04);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
}

[data-theme="dark"] {
  --bg-app: #0b0f17;
  --bg-card: #131b29;
  --bg-card-subtle: #1c2638;
  --border-color: #26334a;
  --border-focus: #38bdf8;

  --text-main: #f1f5f9;
  --text-muted: #cbd5e1;
  --text-subtle: #94a3b8;

  --primary: #38bdf8;
  --primary-hover: #7dd3fc;
  --primary-light: rgba(56, 189, 248, 0.15);

  --teal-accent: #14b8a6;
  --teal-light: rgba(20, 184, 166, 0.15);

  --success: #22c55e;
  --success-light: rgba(34, 197, 94, 0.15);
  --warning: #f59e0b;
  --warning-light: rgba(245, 158, 11, 0.15);
  --danger: #ef4444;
  --danger-light: rgba(239, 68, 68, 0.2);
  --purple: #c084fc;
  --purple-light: rgba(192, 132, 252, 0.15);

  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
}

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

body {
  font-family: var(--font-sans);
  background-color: var(--bg-app);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  line-height: 1.5;
  overflow-x: clip;
}

/* Tipografía de utilidad */
.text-mono {
  font-family: var(--font-mono);
}

h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-heading);
  font-weight: 700;
  line-height: 1.25;
}

/* Layout */
.tool-layout {
  max-width: 1200px;
  margin: 1rem auto 3rem;
  padding: 0 1rem;
  width: 100%;
}

.tool-with-sidebar {
  display: flex;
  gap: 1.5rem;
  align-items: flex-start;
}

.tool-main-content {
  flex: 1 1 0%;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

/* Barra de presets de comida */
.meal-preset-bar {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 0.85rem 1.25rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  box-shadow: var(--shadow-sm);
}

.meal-preset-title {
  font-weight: 600;
  font-size: 0.92rem;
  color: var(--text-muted);
}

.meal-chips-group {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  flex: 1;
}

.chip-btn {
  background: var(--bg-card-subtle);
  color: var(--text-main);
  border: 1px solid var(--border-color);
  border-radius: 20px;
  padding: 0.45rem 0.95rem;
  font-size: 0.88rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  min-height: 38px;
}

.chip-btn:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.chip-btn.active {
  background: var(--primary);
  color: #ffffff;
  border-color: var(--primary);
  font-weight: 600;
  box-shadow: 0 2px 4px rgba(2, 132, 199, 0.25);
}

[data-theme="dark"] .chip-btn.active {
  color: #0b0f17;
}

/* Banner de alerta clínica */
.clinical-alert-banner {
  border-radius: var(--radius-md);
  padding: 1rem 1.25rem;
  display: flex;
  align-items: center;
  gap: 0.85rem;
  font-size: 0.95rem;
  font-weight: 500;
  transition: all 0.2s ease;
}

.alert-hidden {
  display: none !important;
}

.alert-danger {
  background-color: var(--danger-light);
  border: 1px solid var(--danger);
  color: var(--danger);
}

.alert-warning {
  background-color: var(--warning-light);
  border: 1px solid var(--warning);
  color: var(--warning);
}

.alert-success {
  background-color: var(--success-light);
  border: 1px solid var(--success);
  color: var(--success);
}

.alert-purple {
  background-color: var(--purple-light);
  border: 1px solid var(--purple);
  color: var(--purple);
}

/* Rejilla de 2 columnas */
.calc-grid-wrapper {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
  align-items: stretch;
}

@media (max-width: 900px) {
  .calc-grid-wrapper {
    grid-template-columns: 1fr;
  }
}

/* Tarjetas */
.card-main {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  box-shadow: var(--shadow-sm);
}

.card-header-flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.25rem;
}

.card-title {
  font-size: 1.25rem;
  color: var(--text-main);
}

.card-subtitle-small {
  font-size: 0.98rem;
  color: var(--text-muted);
  margin: 1.25rem 0 0.75rem;
  border-top: 1px dashed var(--border-color);
  padding-top: 1rem;
}

.btn-text-action {
  background: none;
  border: none;
  color: var(--primary);
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0.3rem 0.5rem;
  border-radius: var(--radius-sm);
  transition: background 0.15s ease;
}

.btn-text-action:hover {
  background: var(--primary-light);
}

/* Formularios y campos */
.form-group {
  margin-bottom: 1.15rem;
}

.form-label {
  display: block;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-main);
  margin-bottom: 0.4rem;
}

.label-row-flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.4rem;
}

.subtext-hint {
  font-size: 0.8rem;
  color: var(--text-subtle);
}

.badge-unit {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--primary);
  background: var(--primary-light);
  padding: 0.15rem 0.45rem;
  border-radius: 4px;
  margin-left: 0.35rem;
}

.form-input {
  width: 100%;
  height: 44px;
  padding: 0 0.85rem;
  font-size: 1rem;
  border: 1.5px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  color: var(--text-main);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-input:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(2, 132, 199, 0.15);
}

.form-input.text-large {
  font-size: 1.25rem;
  font-weight: 700;
  height: 48px;
}

.input-with-status {
  position: relative;
  display: flex;
  align-items: center;
}

.bg-badge {
  position: absolute;
  right: 10px;
  padding: 0.25rem 0.65rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  pointer-events: none;
}

.bg-badge.bg-hypo {
  background: var(--danger-light);
  color: var(--danger);
  border: 1px solid var(--danger);
}

.bg-badge.bg-normal {
  background: var(--success-light);
  color: var(--success);
  border: 1px solid var(--success);
}

.bg-badge.bg-hyper {
  background: var(--warning-light);
  color: var(--warning);
  border: 1px solid var(--warning);
}

.bg-badge.bg-severe {
  background: var(--purple-light);
  color: var(--purple);
  border: 1px solid var(--purple);
}

.input-unit-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-unit-wrapper .form-input {
  padding-right: 65px;
}

.input-suffix {
  position: absolute;
  right: 12px;
  font-size: 0.85rem;
  color: var(--text-subtle);
  font-weight: 600;
  pointer-events: none;
}

/* Desplegables con flecha SVG y texto controlado */
.form-select {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  height: 44px;
  padding: 0 40px 0 0.85rem;
  font-size: 0.92rem;
  border: 1.5px solid var(--border-color);
  border-radius: var(--radius-sm);
  background-color: var(--bg-card);
  color: var(--text-main);
  outline: none;
  appearance: none;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  cursor: pointer;
  transition: border-color 0.15s ease;
}

[data-theme="dark"] .form-select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
}

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

.form-select option, .form-select optgroup {
  background-color: var(--bg-card);
  color: var(--text-main);
}

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

.section-divider-top {
  border-top: 1px dashed var(--border-color);
  padding-top: 1rem;
}

.helper-text {
  display: block;
  font-size: 0.82rem;
  color: var(--text-subtle);
  margin-top: 0.35rem;
}

.helper-text-xs {
  display: block;
  font-size: 0.76rem;
  color: var(--text-subtle);
  margin-top: 0.25rem;
}

/* Switch de unidades de carbohidratos */
.switch-unit-toggle {
  display: inline-flex;
  background: var(--bg-card-subtle);
  border: 1px solid var(--border-color);
  border-radius: 20px;
  padding: 2px;
}

.toggle-pill {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.25rem 0.65rem;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.toggle-pill.active {
  background: var(--primary);
  color: #ffffff;
  box-shadow: var(--shadow-sm);
}

[data-theme="dark"] .toggle-pill.active {
  color: #0b0f17;
}

/* Bloque IOB (Insulina Activa) */
.iob-accordion-box {
  background: var(--bg-card-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 0.85rem 1rem;
  margin: 1rem 0;
}

.checkbox-container {
  display: flex;
  align-items: center;
  position: relative;
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text-main);
  user-select: none;
}

.checkbox-container input {
  position: absolute;
  opacity: 0;
  cursor: pointer;
  height: 0;
  width: 0;
}

.checkmark {
  height: 20px;
  width: 20px;
  background-color: var(--bg-card);
  border: 1.5px solid var(--border-color);
  border-radius: 4px;
  margin-right: 10px;
  display: inline-block;
  position: relative;
  transition: all 0.15s ease;
}

.checkbox-container:hover input ~ .checkmark {
  border-color: var(--primary);
}

.checkbox-container input:checked ~ .checkmark {
  background-color: var(--primary);
  border-color: var(--primary);
}

.checkmark::after {
  content: "";
  position: absolute;
  display: none;
  left: 6px;
  top: 2px;
  width: 5px;
  height: 10px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

[data-theme="dark"] .checkmark::after {
  border-color: #0b0f17;
}

.checkbox-container input:checked ~ .checkmark::after {
  display: block;
}

.iob-panel-collapsed {
  display: none;
  margin-top: 0.85rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--border-color);
}

.iob-panel-collapsed.active {
  display: block;
}

.iob-calc-summary {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  padding: 0.6rem 0.85rem;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  margin-top: 0.65rem;
}

#iob-calculated-val {
  color: var(--danger);
  font-size: 1.05rem;
}

/* Resultados destacados */
.card-results {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.result-hero-box {
  background: linear-gradient(135deg, var(--primary-light), var(--bg-card-subtle));
  border: 1.5px solid var(--primary);
  border-radius: var(--radius-md);
  padding: 1.5rem;
  text-align: center;
  margin: 1rem 0;
  box-shadow: var(--shadow-sm);
}

.result-hero-label {
  display: block;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.result-hero-number-wrap {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.5rem;
}

.hero-number {
  font-size: 3.2rem;
  font-weight: 800;
  color: var(--primary);
  line-height: 1;
}

.hero-unit {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text-muted);
}

.hero-rounding-badge {
  display: inline-block;
  margin-top: 0.65rem;
  font-size: 0.84rem;
  color: var(--text-subtle);
  background: var(--bg-card);
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  border: 1px solid var(--border-color);
}

/* Desglose de cálculo */
.breakdown-card {
  background: var(--bg-card-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 1.15rem;
  margin-bottom: 1.25rem;
}

.breakdown-title {
  font-size: 0.95rem;
  color: var(--text-muted);
  margin-bottom: 0.85rem;
  font-weight: 600;
}

.breakdown-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.65rem 0;
  border-bottom: 1px dashed var(--border-color);
}

.breakdown-item-info {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.breakdown-icon {
  font-size: 1.1rem;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-card);
  border: 1px solid var(--border-color);
}

.breakdown-name {
  display: block;
  font-size: 0.88rem;
  color: var(--text-main);
}

.breakdown-desc {
  display: block;
  font-size: 0.76rem;
  color: var(--text-subtle);
}

.breakdown-val {
  font-size: 1.05rem;
  font-weight: 700;
}

.text-positive {
  color: var(--primary);
}

.text-neutral {
  color: var(--text-main);
}

.text-negative {
  color: var(--danger);
}

.breakdown-total-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 0.85rem;
  font-size: 0.95rem;
  font-weight: 600;
}

/* Botones de acción */
.results-actions-wrap {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  font-family: var(--font-sans);
  font-size: 0.95rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  padding: 0 1.25rem;
  height: 44px;
  cursor: pointer;
  transition: all 0.15s ease;
  border: none;
  min-height: 40px;
}

.btn-primary {
  background: var(--primary);
  color: #ffffff;
  flex: 2;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

[data-theme="dark"] .btn-primary {
  color: #0b0f17;
}

.btn-secondary {
  background: var(--bg-card-subtle);
  color: var(--text-main);
  border: 1.5px solid var(--border-color);
  flex: 1;
}

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

/* Historial */
.history-accordion-card {
  border-top: 1px solid var(--border-color);
  padding-top: 1rem;
}

.history-header-flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.65rem;
}

.history-title {
  font-size: 0.9rem;
  color: var(--text-muted);
}

.btn-text-danger {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
}

.history-list-box {
  max-height: 180px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--bg-card-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 0.45rem 0.75rem;
  font-size: 0.82rem;
}

.empty-history-text {
  font-size: 0.82rem;
  color: var(--text-subtle);
  text-align: center;
  padding: 1rem;
  font-style: italic;
}

/* FAQ */
.faq-section {
  margin-top: 1.5rem;
}

.faq-item {
  border-bottom: 1px solid var(--border-color);
  padding: 1rem 0;
}

.faq-item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.faq-question {
  font-size: 1.05rem;
  color: var(--text-main);
  margin-bottom: 0.4rem;
}

.faq-answer {
  font-size: 0.92rem;
  color: var(--text-muted);
  line-height: 1.6;
}

.faq-answer a {
  color: var(--primary);
  text-decoration: underline;
}

/* Modales canónicos */
.fijate-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(11, 15, 23, 0.7);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  z-index: 9999;
  opacity: 0;
  visibility: hidden;
  transition: all 0.2s ease;
}

.fijate-modal-overlay.active {
  opacity: 1;
  visibility: visible;
}

.fijate-modal-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  max-width: 540px;
  width: 100%;
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  max-height: 90vh;
  overflow: hidden;
  animation: modalPop 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal-warning-card {
  border-color: var(--danger);
}

@keyframes modalPop {
  0% { transform: scale(0.94); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.fijate-modal-header {
  padding: 1.15rem 1.5rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.fijate-modal-title {
  font-size: 1.15rem;
  color: var(--text-main);
}

.text-danger {
  color: var(--danger);
}

.btn-close-modal {
  background: none;
  border: none;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--text-subtle);
  cursor: pointer;
  padding: 0.2rem 0.5rem;
  border-radius: var(--radius-sm);
}

.fijate-modal-body {
  padding: 1.5rem;
  overflow-y: auto;
  font-size: 0.92rem;
  color: var(--text-muted);
  line-height: 1.6;
}

.modal-section-h4 {
  font-size: 0.98rem;
  color: var(--text-main);
  margin: 1rem 0 0.4rem;
}

.modal-section-h4:first-child {
  margin-top: 0;
}

.formula-box {
  background: var(--bg-card-subtle);
  border: 1px solid var(--border-color);
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  color: var(--primary);
  margin-bottom: 0.45rem;
}

.legal-notice-box {
  background: var(--warning-light);
  border: 1px solid var(--warning);
  color: var(--text-main);
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  margin-top: 1.25rem;
}

.fijate-modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
}

/* Responsividad móvil */
@media (max-width: 640px) {
  .tool-layout {
    margin: 0.5rem auto 2rem;
    padding: 0 0.75rem;
  }

  .card-main {
    padding: 1rem;
  }

  .meal-preset-bar {
    padding: 0.75rem 0.85rem;
  }

  .hero-number {
    font-size: 2.5rem;
  }

  .form-row-2col {
    grid-template-columns: 1fr;
  }

  .results-actions-wrap {
    flex-direction: column;
  }

  .btn {
    width: 100%;
  }
}
