/* ============================================================
   rete.css — TCP/IP Packet Voyager
   Primitive (panel, seg, field) in lab.css.
   ============================================================ */

:root {
  --c-app: var(--accent);
  --c-tcp: #1f9e8a;
  --c-ip:  #c98a1b;
  --c-eth: #8a5cf0;
  --c-phy: #b5468f;
}
[data-theme="dark"] {
  --c-tcp: #4fd1bb;
  --c-ip:  #e0b24a;
  --c-eth: #b39bff;
  --c-phy: #e89fd0;
}

/* colore per livello (sia data-l del pacchetto, sia data-layer di rail/confronto) */
[data-l="data"], [data-layer="app"] { --lc: var(--c-app); }
[data-l="tcp"],  [data-layer="tcp"] { --lc: var(--c-tcp); }
[data-l="ip"],   [data-layer="ip"]  { --lc: var(--c-ip); }
[data-l="eth"],  [data-layer="eth"] { --lc: var(--c-eth); }
[data-layer="phy"] { --lc: var(--c-phy); }

/* ============================================================
   Scrollytelling: wrapper alto + stage fissato
   ============================================================ */
.voyager { position: relative; }
.stage {
  position: sticky;
  top: 74px;
  padding-block: 1rem 1.5rem;
  background: var(--bg);
}
.stage-inner { display: flex; flex-direction: column; gap: 1.1rem; }

/* ---------- Scena: PC + cavo ---------- */
.scene {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 2fr) minmax(0, 1fr);
  align-items: center;
  gap: 1rem;
  padding: 0.5rem 0;
}
.scene > * { min-width: 0; }
.pc { display: flex; flex-direction: column; align-items: center; gap: 0.4rem; }
.pc-screen {
  width: 100%;
  max-width: 150px;
  aspect-ratio: 4 / 3;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: var(--bg-sunk);
  display: grid;
  place-items: center;
  padding: 0.4rem;
  transition: border-color 0.3s var(--ease), box-shadow 0.3s var(--ease);
}
.pc.active .pc-screen { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-wash); }
.pc-msg {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--ink);
  text-align: center;
  word-break: break-word;
  opacity: 0;
  transform: scale(0.8);
  transition: opacity 0.35s var(--ease), transform 0.35s var(--ease);
}
.pc-msg.show { opacity: 1; transform: scale(1); }
.pc-base { width: 56px; height: 8px; background: var(--ink); border-radius: 0 0 4px 4px; opacity: 0.85; }
.pc-label { font-family: var(--font-mono); font-size: 0.66rem; color: var(--ink-soft); text-align: center; }
.pc-label b { color: var(--ink); }

.wire { position: relative; height: 60px; }
.wire-line {
  position: absolute;
  top: 50%; left: 0; right: 0;
  height: 3px;
  transform: translateY(-50%);
  background: repeating-linear-gradient(90deg, var(--line) 0 8px, transparent 8px 16px);
}
.travel {
  position: absolute;
  top: 50%;
  left: 8%;
  width: 26px; height: 18px;
  transform: translate(-50%, -50%);
  border-radius: 4px;
  background: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-wash);
  opacity: 0;
  transition: left 1.1s cubic-bezier(0.5,0,0.5,1), opacity 0.3s var(--ease);
}
.travel.show { opacity: 1; }
.travel.moving { animation: blink 0.4s steps(2) infinite; }
@keyframes blink { 50% { box-shadow: 0 0 0 7px var(--accent-wash); } }

/* ---------- Griglia centrale ---------- */
.stage-grid {
  display: grid;
  grid-template-columns: 168px minmax(0, 1fr) 270px;
  gap: 1.1rem;
  align-items: start;
}
.stage-grid > * { min-width: 0; }

/* Pila OSI ↔ TCP/IP */
.rail {
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: var(--bg-elev);
  font-family: var(--font-mono);
}
.rail-head {
  display: grid;
  grid-template-columns: 2.4rem 1fr;
  gap: 0.5rem;
  padding: 0.5rem 0.7rem;
  font-size: 0.62rem;
  letter-spacing: 0.06em;
  color: var(--ink-faint);
  border-bottom: 1px solid var(--line);
}
.rail-row {
  display: grid;
  grid-template-columns: 2.4rem 1fr;
  gap: 0.5rem;
  align-items: center;
  padding: 0.7rem;
  border-bottom: 1px solid var(--line-soft);
  border-left: 3px solid transparent;
  transition: background-color 0.3s var(--ease), border-color 0.3s var(--ease);
}
.rail-row:last-child { border-bottom: 0; }
.rail-osi { font-size: 0.72rem; color: var(--ink-faint); text-align: center; }
.rail-tcp { display: flex; align-items: center; gap: 0.45rem; font-size: 0.8rem; color: var(--ink); }
.rail-row .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--lc); flex-shrink: 0; }
.rail-row.active {
  background: color-mix(in srgb, var(--lc) 12%, transparent);
  border-left-color: var(--lc);
}
.rail-row.active .rail-osi, .rail-row.active .rail-tcp { color: var(--lc); font-weight: 600; }

/* ---------- Pacchetto matrioska ---------- */
.packet-area { display: grid; place-items: center; min-height: 280px; }
.packet { width: 100%; }
.lyr {
  position: relative;
  border: 1.5px solid var(--lc);
  border-radius: 12px;
  padding: 0.65rem;
  background: color-mix(in srgb, var(--lc) 8%, var(--bg-elev));
  transition: padding 0.45s var(--ease), border-color 0.45s var(--ease),
              background-color 0.45s var(--ease), box-shadow 0.45s var(--ease), transform 0.45s var(--ease);
}
.lyr + .lyr, .lyr .lyr { margin-top: 0.55rem; }
.lyr-tag {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--lc);
  margin-bottom: 0.5rem;
  transition: max-height 0.4s var(--ease), opacity 0.3s var(--ease), margin 0.4s var(--ease);
  overflow: hidden;
}
.lyr-fields {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem 0.5rem;
  margin-bottom: 0.55rem;
  transition: max-height 0.4s var(--ease), opacity 0.3s var(--ease), margin 0.4s var(--ease);
  overflow: hidden;
}
.lyr-fields span {
  font-family: var(--font-mono);
  font-size: 0.62rem;
  color: var(--ink-soft);
  background: var(--bg);
  border: 1px solid var(--line-soft);
  border-radius: 5px;
  padding: 0.1rem 0.4rem;
}
.lyr-fields b { color: var(--ink); font-weight: 600; }
.lyr-ftr {
  font-family: var(--font-mono);
  font-size: 0.6rem;
  color: var(--lc);
  text-align: right;
  margin-top: 0.5rem;
  opacity: 0.8;
  transition: max-height 0.4s var(--ease), opacity 0.3s var(--ease), margin 0.4s var(--ease);
  overflow: hidden;
}
.payload {
  font-family: var(--font-mono);
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--ink);
  text-align: center;
  padding: 0.6rem;
  background: color-mix(in srgb, var(--c-app) 14%, var(--bg));
  border-radius: 8px;
  word-break: break-word;
}

/* livello "spento": il telaio collassa, resta visibile solo il contenuto interno */
.lyr.off {
  border-color: transparent;
  background: transparent;
  padding: 0;
}
.lyr.off > .lyr-tag,
.lyr.off > .lyr-fields,
.lyr.off > .lyr-ftr {
  max-height: 0 !important;
  opacity: 0;
  margin: 0;
  pointer-events: none;
}
.lyr:not(.off) > .lyr-tag { max-height: 2rem; }
.lyr:not(.off) > .lyr-fields { max-height: 6rem; }
.lyr:not(.off) > .lyr-ftr { max-height: 2rem; }

.lyr.active {
  box-shadow: 0 0 0 2px var(--lc), 0 16px 36px -20px var(--lc);
  transform: translateY(-1px);
}

/* ---------- Dossier ---------- */
.dossier {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg-elev);
  padding: 1.1rem;
  min-height: 220px;
  /* tetto: se la spiegazione è lunga, scorre dentro senza spingere giù i controlli */
  max-height: 46vh;
  overflow-y: auto;
}
.dossier-phase {
  font-family: var(--font-mono);
  font-size: 0.66rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--lc, var(--accent));
  margin-bottom: 0.4rem;
}
.dossier-title {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 1.2rem;
  letter-spacing: -0.01em;
  margin-bottom: 0.85rem;
}
.dossier-fields { display: grid; gap: 0.4rem; margin-bottom: 0.9rem; }
.dossier-fields .df {
  display: flex; justify-content: space-between; gap: 0.75rem;
  font-family: var(--font-mono); font-size: 0.78rem;
  padding-bottom: 0.35rem; border-bottom: 1px solid var(--line-soft);
}
.dossier-fields .df span { color: var(--ink-soft); }
.dossier-fields .df b { color: var(--ink); font-weight: 600; text-align: right; }
.dossier-proto {
  font-family: var(--font-mono); font-size: 0.7rem; color: var(--ink-soft);
  margin-bottom: 0.8rem;
}
.dossier-proto b { color: var(--lc, var(--accent)); }
.dossier-note { font-size: 0.88rem; color: var(--ink-soft); line-height: 1.6; }
.dossier-note b { color: var(--ink); }

/* ---------- Controlli ---------- */
.stage-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding-top: 0.5rem;
  border-top: 1px solid var(--line);
}
.progress {
  flex: 1;
  text-align: center;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--ink-soft);
}
.progress b { color: var(--ink); }
.progress .bar { display: flex; gap: 4px; justify-content: center; margin-top: 0.4rem; }
.progress .bar i {
  width: 22px; height: 4px; border-radius: 2px; background: var(--line);
  transition: background-color 0.3s var(--ease);
}
.progress .bar i.done { background: var(--accent); }
.ctrl-btn {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  padding: 0.6rem 1.1rem;
  border: 1px solid var(--line);
  border-radius: 100px;
  background: var(--bg);
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 0.2s var(--ease), background-color 0.2s var(--ease), opacity 0.2s var(--ease);
}
.ctrl-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.ctrl-btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.ctrl-btn.primary:hover { transform: translateY(-1px); }
.ctrl-btn:disabled { opacity: 0.4; cursor: default; }

/* ---------- Legenda ---------- */
.legend-keys { display: flex; flex-wrap: wrap; gap: 0.6rem; margin-top: 1.25rem; }
.legend-key {
  font-family: var(--font-mono); font-size: 0.72rem;
  padding: 0.35rem 0.7rem; border-radius: 100px;
  border: 1px solid var(--lc); color: var(--lc);
  background: color-mix(in srgb, var(--lc) 8%, transparent);
}

/* ---------- Confronto OSI / TCP-IP ---------- */
.cmp-panel { margin-top: 1.25rem; }
.cmp {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(0, 1.7fr) minmax(0, 1.15fr);
  gap: 0.5rem;
  align-items: stretch;
}
.cmp > * { min-width: 0; }
.cmp-h {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--ink-faint);
  padding-bottom: 0.3rem;
}
.osi, .proto, .tcp-box {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 0.6rem 0.75rem;
  background: var(--bg);
  transition: border-color 0.3s var(--ease), background-color 0.3s var(--ease);
}
.osi {
  display: flex; align-items: center; gap: 0.6rem;
  border-left: 3px solid var(--lc);
  font-size: 0.9rem;
}
.osi-n {
  font-family: var(--font-mono); font-size: 0.7rem; font-weight: 600;
  color: var(--accent-ink); background: var(--lc);
  width: 1.4rem; height: 1.4rem; border-radius: 6px;
  display: grid; place-items: center; flex-shrink: 0;
}
.osi b { font-weight: 600; }
.proto { font-family: var(--font-mono); font-size: 0.74rem; color: var(--ink-soft); display: flex; align-items: center; }
.tcp-box {
  display: flex; flex-direction: column; justify-content: center; gap: 0.3rem;
  background: color-mix(in srgb, var(--lc) 9%, var(--bg-elev));
  border-color: var(--lc);
}
.tcp-box b { font-size: 0.92rem; color: var(--lc); }
.tcp-box .pdu { font-family: var(--font-mono); font-size: 0.66rem; color: var(--ink-soft); }

.osi.active, .proto.active { border-color: var(--lc); background: color-mix(in srgb, var(--lc) 12%, transparent); }
.tcp-box.active { box-shadow: 0 0 0 2px var(--lc); }

.msg-field { max-width: 18rem; }

/* ---------- Responsive ---------- */
@media (max-width: 860px) {
  /* niente "pinning": la scena scorre normalmente, si usa coi pulsanti */
  .stage { position: static; padding-block: 0.25rem 0.5rem; }
  .voyager { height: auto !important; }

  .stage-grid { grid-template-columns: minmax(0, 1fr); gap: 0.9rem; }
  .packet-area { order: 1; min-height: 0; }
  .rail { order: 2; }
  .dossier { order: 3; min-height: 0; max-height: none; overflow-y: visible; }

  /* confronto OSI: colonna singola, le intestazioni non servono */
  .cmp { grid-template-columns: minmax(0, 1fr); gap: 0.4rem; }
  .cmp .tcp-box { grid-column: 1 !important; grid-row: auto !important; }
  .cmp-h { display: none; }

  /* controlli: barra di avanzamento su riga propria, pulsanti affiancati */
  .stage-controls { flex-wrap: wrap; gap: 0.6rem; }
  .progress { order: -1; flex-basis: 100%; }
  .stage-controls .ctrl-btn { flex: 1; text-align: center; padding-inline: 0.6rem; }

  /* campo messaggio a tutta larghezza */
  .controls-panel .control-group { flex-wrap: wrap; }
  .msg-field { max-width: none; width: 100%; }
}

@media (max-width: 480px) {
  .scene { gap: 0.5rem; }
  .pc-screen { max-width: 92px; border-width: 1.5px; }
  .pc-base { width: 44px; }
  .pc-label { font-size: 0.56rem; }
  .wire { height: 44px; }
  .lyr { padding: 0.5rem; }
  .lyr-tag { font-size: 0.64rem; }
  .lyr-fields { gap: 0.25rem; }
  .lyr-fields span { font-size: 0.56rem; padding: 0.08rem 0.3rem; }
  .payload { font-size: 0.85rem; }
  .dossier, .rail { padding: 0.8rem; }
  .osi, .proto, .tcp-box { font-size: 0.82rem; padding: 0.5rem 0.6rem; }
  .proto { font-size: 0.68rem; }
}

/* ---------- Livello fisico: bit che scorrono sul cavo ---------- */
.bits {
  position: absolute;
  top: 50%; left: 0; right: 0;
  transform: translateY(-50%);
  text-align: center;
  font-family: var(--font-mono); font-size: 0.78rem; letter-spacing: 0.12em;
  color: var(--c-phy); font-weight: 600;
  background: var(--bg);
  padding: 0.1rem 0;
  opacity: 0; transition: opacity 0.3s var(--ease);
}
.bits.show { opacity: 1; animation: bitsflow 0.6s steps(4) infinite; }
@keyframes bitsflow { 50% { opacity: 0.55; } }

/* ---------- Approfondimento: accordion livelli/protocolli ---------- */
.guide-panel { margin-top: 1.25rem; }
.guide-search { max-width: 30rem; margin-bottom: 1.5rem; }
.guide-cat { margin-bottom: 1.75rem; }
.guide-cat:last-child { margin-bottom: 0; }
.guide-cat-title {
  font-family: var(--font-display); font-weight: 500; font-size: 1.2rem;
  padding-bottom: 0.5rem; border-bottom: 1px solid var(--line); margin-bottom: 0.35rem;
}
.guide-cat-intro { color: var(--ink-soft); font-size: 0.9rem; margin-bottom: 1rem; }
.guide-cmd {
  border: 1px solid var(--line); border-left: 3px solid var(--lc, var(--line));
  border-radius: 10px; background: var(--bg); margin-bottom: 0.6rem; overflow: hidden;
  transition: border-color 0.2s var(--ease);
}
.guide-cmd[open] { border-color: color-mix(in srgb, var(--lc, var(--accent)) 45%, var(--line)); }
.guide-cmd summary {
  display: flex; align-items: baseline; gap: 0.85rem; flex-wrap: wrap;
  padding: 0.8rem 1.1rem; cursor: pointer; list-style: none;
}
.guide-cmd summary::-webkit-details-marker { display: none; }
.guide-cmd summary::before { content: "+"; font-family: var(--font-mono); color: var(--ink-faint); }
.guide-cmd[open] summary::before { content: "−"; color: var(--lc, var(--accent)); }
.g-name { font-family: var(--font-mono); font-size: 0.95rem; font-weight: 600; color: var(--lc, var(--accent)); }
.g-syntax { font-family: var(--font-mono); font-size: 0.76rem; color: var(--ink-faint); }
.g-body { padding: 0 1.1rem 1.1rem; }
.g-desc { color: var(--ink-soft); font-size: 0.92rem; line-height: 1.65; }
.g-desc b { color: var(--ink); font-weight: 600; }
.g-desc code { font-family: var(--font-mono); font-size: 0.85em; background: var(--bg-sunk);
  border: 1px solid var(--line-soft); border-radius: 5px; padding: 0.05rem 0.35rem; color: var(--ink); }
.guide-empty { color: var(--ink-soft); font-family: var(--font-mono); font-size: 0.9rem; }
@media (max-width: 560px) { .g-syntax { flex-basis: 100%; } }
