:root {
  --bg: #0f1722;
  --panel: #1a2433;
  --panel2: #233047;
  --ink: #e7eef7;
  --muted: #8aa0bc;
  --accent: #ffb43a;
  --accent2: #4ec3ff;
  --good: #4cd07a;
  --bad: #ff6b6b;
  --grid: #324a6b;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg); color: var(--ink);
  font-family: 'Segoe UI', Roboto, system-ui, sans-serif;
  font-size: 14px;
}

a { color: var(--accent2); }

.center {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
}
.card {
  background: var(--panel); padding: 24px; border-radius: 12px;
  box-shadow: 0 6px 22px rgba(0,0,0,0.35);
  min-width: 360px;
}
.card h1 {
  margin: 0 0 16px 0; font-size: 22px; color: var(--accent);
}
.card .sub { color: var(--muted); margin-bottom: 18px; font-size: 13px; }

label { display: block; margin: 8px 0 4px; color: var(--muted); font-size: 12px; }
input[type=text], input[type=number], select {
  width: 100%;
  padding: 9px 11px; border-radius: 6px; border: 1px solid var(--panel2);
  background: #0c1320; color: var(--ink); font-size: 14px;
  outline: none;
}
input:focus, select:focus { border-color: var(--accent2); }

button.primary {
  background: var(--accent); color: #1a1305;
  border: none; padding: 10px 16px; border-radius: 8px;
  font-weight: 600; cursor: pointer; font-size: 14px;
  margin-top: 10px;
}
button.primary:hover { filter: brightness(1.08); }
button.ghost {
  background: transparent; color: var(--ink);
  border: 1px solid var(--panel2); padding: 6px 10px; border-radius: 6px;
  cursor: pointer; font-size: 12px;
}
button.ghost:hover { border-color: var(--accent2); color: var(--accent2); }
button.danger { background: var(--bad); color: white; border:none; padding: 6px 10px; border-radius:6px; cursor:pointer; }
button.go { background: var(--good); color: #062510; border:none; padding: 6px 10px; border-radius:6px; cursor:pointer; }

/* layout app */
.appbar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 18px; background: var(--panel);
  border-bottom: 1px solid var(--panel2);
}
.appbar h1 { font-size: 16px; margin: 0; color: var(--accent); }
.appbar .who { color: var(--muted); font-size: 12px; }
.appbar .who b { color: var(--ink); }

.layout {
  display: grid;
  grid-template-columns: 320px 1fr 320px;
  gap: 12px; padding: 12px;
}
.col { background: var(--panel); padding: 12px; border-radius: 10px; }
.col h2 { margin: 0 0 10px; font-size: 14px; color: var(--accent2); border-bottom: 1px solid var(--panel2); padding-bottom: 6px; }
.col h3 { margin: 12px 0 6px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }

.row { display: flex; gap: 8px; align-items: center; }
.row > * { flex: 1; }

.metric { display: flex; justify-content: space-between; padding: 4px 0; font-size: 13px; }
.metric span:first-child { color: var(--muted); }
.metric span:last-child { color: var(--ink); font-weight: 600; }

.tabs {
  display: flex; gap: 4px; margin-bottom: 8px;
}
.tab {
  flex: 1; padding: 6px; cursor: pointer; text-align: center;
  background: var(--panel2); border-radius: 6px; font-size: 12px; color: var(--muted);
  border: 1px solid transparent;
}
.tab.active { background: var(--bg); color: var(--accent); border-color: var(--accent); }

.charts { display: flex; flex-direction: column; gap: 12px; }
.chart-box {
  background: #0a1220; border: 1px solid var(--panel2);
  border-radius: 8px; padding: 6px;
}
.chart-box .title {
  font-size: 12px; color: var(--muted);
  display: flex; justify-content: space-between; padding: 0 4px;
}
.chart-box canvas { width: 100%; height: 240px; display: block; }

.led { width: 10px; height: 10px; border-radius: 50%; display: inline-block; margin-right:6px; }
.led.on { background: var(--good); box-shadow: 0 0 6px var(--good); }
.led.off { background: #444; }

.slider { -webkit-appearance: none; appearance: none; width: 100%; height: 6px; background: var(--panel2); border-radius: 4px; outline: none; }
.slider::-webkit-slider-thumb { -webkit-appearance:none; appearance:none; width: 14px; height: 14px; border-radius:50%; background: var(--accent); cursor: pointer; }

.toolbar { display:flex; gap:6px; margin-top: 10px; flex-wrap: wrap; }

.score-box { background: var(--panel2); padding: 10px; border-radius: 8px; margin-top: 10px; }
.score-box .big { font-size: 28px; color: var(--accent); font-weight: bold; }
.score-box .small { color: var(--muted); font-size: 11px; }

table.lb { width: 100%; border-collapse: collapse; font-size: 12px; }
table.lb th, table.lb td { padding: 5px 6px; border-bottom: 1px solid var(--panel2); }
table.lb th { color: var(--muted); text-align: left; font-weight: normal; }
table.lb td.score { color: var(--accent); font-weight: 600; }

.split { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; padding: 12px; }
.full { padding: 12px; }
.full table { background: var(--panel); border-radius: 10px; padding: 12px; }
.section-title { font-size: 14px; color: var(--accent2); margin: 0 0 8px; }

.alert { padding: 8px 10px; border-radius: 6px; background: var(--panel2); margin: 8px 0; font-size: 12px; }
.alert.ok { color: var(--good); border-left: 3px solid var(--good); }
.alert.bad { color: var(--bad); border-left: 3px solid var(--bad); }

/* === Currículum === */
.lesson {
  background: var(--panel2); border-radius: 6px;
  padding: 8px 10px; margin-bottom: 6px;
  border-left: 4px solid var(--muted);
  font-size: 12px; cursor: pointer;
  transition: background 0.15s;
}
.lesson:hover { background: #2c3a55; }
.lesson .ltop { display: flex; justify-content: space-between; align-items: center; }
.lesson .lbadge { font-size: 11px; padding: 2px 6px; border-radius: 3px; background: var(--bg); }
.lesson .lobj { color: var(--muted); margin-top: 4px; font-size: 11px; line-height: 1.35; display:none; }
.lesson.expanded .lobj { display: block; }
.lesson.completed { border-left-color: var(--good); }
.lesson.completed .lbadge { background: var(--good); color: #062510; }
.lesson.unlocked   { border-left-color: var(--accent); }
.lesson.unlocked .lbadge { background: var(--accent); color: #1a1305; }
.lesson.locked     { border-left-color: #555; opacity: 0.55; cursor: not-allowed; }
.lesson.locked .lbadge { background: #444; color: #aaa; }
.lesson .ltitle { font-weight: 600; }
.lesson .lweek { color: var(--accent2); font-size: 10px; margin-right: 6px; }

.progress-bar {
  height: 8px; background: var(--panel2); border-radius: 4px;
  overflow: hidden; margin: 4px 0 12px;
}
.progress-bar .fill {
  height: 100%; background: linear-gradient(90deg, var(--accent2), var(--good));
  transition: width 0.4s;
}

/* ========== RESPONSIVE base ==========
   Reglas necesarias en TODOS los anchos (resuelven "grid blowout":
   por defecto los items de un grid tienen min-width:auto y sus
   hijos con tamaño intrínseco — SVG, canvas — fuerzan el ancho
   del grid superior, rompiendo cualquier media query). */

.layout > .col, .split > .col { min-width: 0; min-height: 0; }
.layout, .split { width: 100%; max-width: 100%; }
svg, canvas, img { max-width: 100%; }
.chart-box { min-width: 0; }
.chart-box canvas { width: 100% !important; height: 240px; display: block; }
#pid { width: 100%; height: auto; }
html, body { overflow-x: hidden; }

/* row debe poder envolver cuando la pantalla es estrecha */
.row { flex-wrap: wrap; }
.row > * { min-width: 0; }

/* — Tablet ancho (≤1100 px): 2 columnas (controles + retos arriba, gráficos full ancho debajo) — */
@media (max-width: 1100px) {
  .layout {
    grid-template-columns: 1fr 1fr;
    grid-template-areas: "controls retos" "charts charts";
    gap: 10px; padding: 10px;
  }
  .layout > .col:nth-child(1) { grid-area: controls; }
  .layout > .col:nth-child(2) { grid-area: charts;   }
  .layout > .col:nth-child(3) { grid-area: retos;    }
  .split { grid-template-columns: 1fr; }
}

/* — Tablet vertical / móvil grande (≤768 px): 1 columna ya — */
@media (max-width: 768px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-areas: "controls" "charts" "retos";
  }
  .appbar {
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 12px;
  }
  .appbar h1 {
    font-size: 14px;
    flex: 1 1 100%;
    line-height: 1.25;
  }
  .appbar h1 img { height: 40px !important; flex-shrink: 0; }
  .appbar h1 span { flex: 1; min-width: 0; }
  .appbar .who {
    width: 100%;
    font-size: 11px;
    text-align: left;
    border-top: 1px solid var(--panel2);
    padding-top: 6px;
    flex-wrap: wrap;
    display: flex;
    gap: 6px;
    align-items: center;
  }
  .col { padding: 10px; }
  .col h2 { font-size: 13px; }
  /* Forzar scroll horizontal a las tablas (panel instructor) */
  table.lb { display: block; overflow-x: auto; white-space: nowrap; }
  /* Reducir tamaño del SVG del P&ID en móvil */
  #pid { max-height: 360px; }
}

/* — Móvil pequeño (≤480 px) — */
@media (max-width: 480px) {
  body { font-size: 13px; }
  .card { min-width: 0; width: calc(100vw - 24px); padding: 16px; }
  .card h1 { font-size: 18px; }
  .appbar h1 { font-size: 13px; gap: 8px !important; }
  .appbar h1 img { height: 32px !important; }
  .appbar h1 span div { font-size: 10px !important; }
  .col { padding: 8px; }
  .chart-box canvas { height: 180px !important; }
  .toolbar { gap: 4px; }
  .toolbar button { padding: 5px 8px; font-size: 11px; }
  /* En móvil cada item del row toma todo el ancho */
  .row > div { flex: 1 1 100%; }
  .metric { font-size: 12px; }
  /* Modales casi a pantalla completa en móvil */
  #replayModal > div, #historyModal > div, #spyModal > div {
    width: calc(100vw - 16px) !important;
    padding: 12px !important;
  }
}
