:root {
  --bg: #f3f5fb;
  --bg2: #e8ecf7;
  --card: #ffffff;
  --ink: #18203a;
  --muted: #5d6888;
  --line: #d9deec;
  --soft: #eef1f9;
  --accent: #6d5dfc;
  --accent-ink: #ffffff;
  --l7: #f59e0b;   /* application */
  --l6: #f97316;
  --l5: #ec4899;
  --l4: #10b981;   /* transport */
  --l3: #3b82f6;   /* network */
  --l2: #8b5cf6;   /* data link */
  --l1: #64748b;   /* physical */
  --chg: #fde047;
  --chg-ink: #713f12;
  --ok: #16a34a;
  --bad: #dc2626;
  --shadow: 0 1px 2px rgba(20,30,60,.06), 0 8px 24px rgba(20,30,60,.07);
  --wire: #b8c0d6;
  --node: #ffffff;
  --mono: ui-monospace, "Cascadia Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1222; --bg2: #121a30; --card: #161e35; --ink: #e6eafa; --muted: #9aa5c6;
    --line: #29334f; --soft: #1d2742; --wire: #3a4668; --node: #1f2944;
    --chg: #facc15; --chg-ink: #1a1400;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 10px 30px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d1222; --bg2: #121a30; --card: #161e35; --ink: #e6eafa; --muted: #9aa5c6;
  --line: #29334f; --soft: #1d2742; --wire: #3a4668; --node: #1f2944;
  --chg: #facc15; --chg-ink: #1a1400;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 10px 30px rgba(0,0,0,.35);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  background: radial-gradient(1200px 600px at 10% -10%, var(--bg2), transparent), var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.45;
  min-height: 100vh;
}
code, kbd, pre, .mono { font-family: var(--mono); }
code { background: var(--soft); padding: 1px 5px; border-radius: 4px; font-size: .92em; }
kbd { border: 1px solid var(--line); border-bottom-width: 2px; padding: 0 5px; border-radius: 4px; font-size: .85em; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 14px 24px; position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--card) 85%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo { width: 36px; height: 36px; }
h1 { font-size: 19px; margin: 0; letter-spacing: -.01em; }
.subtitle { margin: 0; color: var(--muted); font-size: 13px; }
.topbar-actions { display: flex; gap: 10px; align-items: center; }

main { padding: 18px 24px 8px; max-width: 1680px; margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); padding: 16px; }
h2 { font-size: 15px; margin: 0 0 10px; }
h3 { font-size: 13px; margin: 0 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.sect { margin-top: 16px; }

/* segmented control */
.seg { display: inline-flex; background: var(--soft); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button { border: 0; background: transparent; color: var(--muted); padding: 7px 12px; border-radius: 8px; font: inherit; font-size: 13.5px; cursor: pointer; font-weight: 600; }
.seg button.active { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg button:disabled { cursor: not-allowed; opacity: .55; }
.icon-btn { border: 1px solid var(--line); background: var(--soft); color: var(--ink); width: 38px; height: 38px; border-radius: 10px; font-size: 18px; cursor: pointer; }

/* diagram */
.diagram-card { padding: 6px; }
.diagram-scroll { overflow-x: auto; }
.diagram { position: relative; width: 100%; min-width: 980px; aspect-ratio: 1200 / 400; }
.diagram svg { position: absolute; inset: 0; width: 100%; height: 100%; }
#nodeCards { position: absolute; inset: 0; pointer-events: none; }
.ncard {
  position: absolute; transform: translate(-50%, 0);
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  padding: 6px 9px; font-size: 11.5px; pointer-events: auto; box-shadow: var(--shadow);
  white-space: nowrap; transition: border-color .2s, box-shadow .2s;
}
.ncard.active { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent), var(--shadow); }
.ncard .nm { font-weight: 700; font-size: 12.5px; margin-bottom: 2px; }
.ncard .ifn { color: var(--muted); font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; margin-top: 3px; }
.ncard .row { display: grid; grid-template-columns: 30px auto; gap: 4px; align-items: center; }
.ncard .row span { color: var(--muted); font-size: 10.5px; }
.ncard .val { font-family: var(--mono); background: var(--soft); border: 1px solid var(--line); border-radius: 5px; padding: 1px 5px; font-size: 11px; }
.ncard input.val { width: 128px; color: var(--ink); outline: none; }
.ncard input.val:focus { border-color: var(--accent); }
.ncard input.val.invalid { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 12%, var(--soft)); }
.ncard .err { color: var(--bad); font-size: 10.5px; max-width: 190px; white-space: normal; }
.ncard.cols { display: grid; grid-template-columns: auto auto; column-gap: 12px; }
.ncard.cols .nm { grid-column: 1 / -1; }

svg .wire { stroke: var(--wire); stroke-width: 4; stroke-linecap: round; fill: none; transition: stroke .25s; }
svg .wire.hot { stroke: var(--accent); stroke-dasharray: 2 10; animation: dash .6s linear infinite; }
svg .wire.flood { stroke: var(--l2); stroke-dasharray: 2 10; animation: dash .6s linear infinite; }
@keyframes dash { to { stroke-dashoffset: -12; } }
svg .portlbl { fill: var(--muted); font-size: 12px; font-weight: 700; font-family: var(--mono); }
svg .nodelbl { fill: var(--ink); font-size: 14px; font-weight: 700; text-anchor: middle; }
svg .halo { fill: var(--accent); opacity: 0; transition: opacity .25s; }
svg .halo.on { opacity: .16; }
svg .nodebg { fill: var(--node); stroke: var(--line); stroke-width: 1.5; }
svg .icon-stroke { stroke: var(--ink); }
svg .token rect { filter: drop-shadow(0 3px 5px rgba(0,0,0,.25)); }
svg .token text { fill: #fff; font-size: 12px; font-weight: 700; text-anchor: middle; font-family: var(--mono); }

/* setup */
.controls-card { display: flex; flex-direction: column; gap: 12px; }
.setup { display: flex; gap: 18px; flex-wrap: wrap; align-items: flex-end; }
.setup.locked { opacity: .6; pointer-events: none; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.grow { flex: 1; min-width: 300px; }
.field > label { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.payload-row { display: flex; gap: 8px; flex-wrap: wrap; }
#payload { flex: 1; min-width: 200px; font-family: var(--mono); font-size: 14px; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--soft); color: var(--ink); outline: none; }
#payload:focus { border-color: var(--accent); }
.chip { border: 1px dashed var(--line); background: transparent; color: var(--muted); border-radius: 999px; padding: 6px 12px; cursor: pointer; font-family: var(--mono); font-size: 12.5px; }
.chip:hover { color: var(--ink); border-color: var(--accent); }
.options { gap: 4px; }
.check { font-size: 13.5px; display: flex; gap: 6px; align-items: center; cursor: pointer; }
.runbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; border-top: 1px solid var(--line); padding-top: 12px; }
.btn { border: 1px solid var(--line); background: var(--soft); color: var(--ink); font: inherit; font-weight: 650; padding: 9px 16px; border-radius: 10px; cursor: pointer; transition: transform .06s, background .2s; }
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.accent { background: linear-gradient(135deg, var(--accent), #38bdf8); color: #fff; border-color: transparent; min-width: 110px; }
.btn.ghost { background: transparent; }
.btn.on { background: var(--ink); color: var(--card); }
.progress { flex: 1; min-width: 180px; height: 30px; position: relative; background: var(--soft); border-radius: 999px; overflow: hidden; border: 1px solid var(--line); }
.progress-bar { position: absolute; inset: 0 auto 0 0; width: 0; background: linear-gradient(90deg, color-mix(in srgb, var(--accent) 35%, transparent), color-mix(in srgb, #38bdf8 35%, transparent)); transition: width .3s; }
#progressTxt { position: relative; display: block; text-align: center; line-height: 28px; font-size: 12.5px; font-weight: 600; color: var(--muted); }

/* status */
.status { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px; box-shadow: var(--shadow); min-width: 0; }
.stat span { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.stat b { font-size: 15px; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* workspace */
.workspace { display: grid; grid-template-columns: 250px minmax(0, 1fr) 330px; gap: 14px; align-items: start; }

.stack { display: flex; flex-direction: column; gap: 5px; }
.layer { position: relative; border-radius: 10px; padding: 8px 10px; border: 1px solid var(--line); background: var(--soft); display: flex; align-items: center; gap: 10px; transition: all .25s; }
.layer .num { width: 26px; height: 26px; border-radius: 7px; display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 13px; flex: none; }
.layer .nm { font-weight: 650; font-size: 13.5px; }
.layer .pd { font-size: 11px; color: var(--muted); }
.layer.unused { opacity: .35; }
.layer.unused .num { background: var(--muted) !important; }
.layer.cur { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--card)); transform: scale(1.03); box-shadow: 0 4px 14px color-mix(in srgb, var(--accent) 30%, transparent); }
.layer.cur::after { content: attr(data-arrow); position: absolute; right: 10px; font-size: 18px; font-weight: 800; color: var(--accent); }
.hint { color: var(--muted); font-size: 12.5px; margin: 10px 0 0; }

.explain h2 { font-size: 18px; margin-bottom: 6px; }
.extext { font-size: 14.5px; }
.extext p { margin: 6px 0; }
.extext ul, .extext ol { margin: 6px 0; padding-left: 22px; }
.extext .note { border-left: 3px solid var(--accent); background: var(--soft); padding: 6px 10px; border-radius: 0 8px 8px 0; margin: 8px 0; font-size: 13.5px; }
.extext .decision { border-left: 3px solid var(--l7); background: color-mix(in srgb, var(--l7) 10%, var(--card)); padding: 6px 10px; border-radius: 0 8px 8px 0; margin: 8px 0; }
.extext table { border-collapse: collapse; font-size: 12.5px; margin: 6px 0; }
.extext td, .extext th { border: 1px solid var(--line); padding: 3px 8px; text-align: left; font-family: var(--mono); }
.extext th { background: var(--soft); font-family: inherit; }
.extext tr.hit td { background: color-mix(in srgb, var(--l7) 22%, transparent); font-weight: 700; }

/* PDU nested view */
.pdu { padding: 4px 0 2px; overflow-x: auto; }
.pdu-empty { color: var(--muted); font-style: italic; padding: 18px; text-align: center; border: 1px dashed var(--line); border-radius: 12px; }
.box { display: flex; align-items: stretch; gap: 4px; border: 2px solid; border-radius: 10px; padding: 4px; padding-bottom: 20px; position: relative; flex: 1; min-width: 0; animation: pop .35s ease; }
.box > .tag { position: absolute; bottom: 2px; left: 8px; font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; }
.box.l1 { border-color: var(--l1); border-style: dashed; } .box.l1 > .tag { color: var(--l1); }
.box.l2 { border-color: var(--l2); } .box.l2 > .tag { color: var(--l2); }
.box.l3 { border-color: var(--l3); } .box.l3 > .tag { color: var(--l3); }
.box.l4 { border-color: var(--l4); } .box.l4 > .tag { color: var(--l4); }
.box.l7 { border-color: var(--l7); } .box.l7 > .tag { color: var(--l7); }
.hdr { border-radius: 7px; color: #fff; padding: 6px 8px; font-size: 12px; font-weight: 700; min-width: 92px; display: flex; flex-direction: column; justify-content: center; cursor: pointer; animation: pop .35s ease; flex: none; }
.hdr small { font-weight: 500; opacity: .9; font-family: var(--mono); font-size: 10.5px; }
.hdr.l1 { background: var(--l1); } .hdr.l2 { background: var(--l2); } .hdr.l3 { background: var(--l3); } .hdr.l4 { background: var(--l4); } .hdr.arp { background: #0ea5e9; }
.hdr.focus { outline: 3px solid var(--chg); outline-offset: 1px; }
.hdr.removed { opacity: .35; text-decoration: line-through; }
.data { flex: 1; min-width: 120px; background: color-mix(in srgb, var(--l7) 18%, var(--card)); border: 1px solid var(--l7); border-radius: 7px; padding: 6px 8px; font-family: var(--mono); font-size: 11.5px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; display: flex; flex-direction: column; justify-content: center; }
.data b { font-family: system-ui, sans-serif; font-size: 12px; color: var(--l7); }
.bits { font-family: var(--mono); font-size: 11px; color: var(--l1); letter-spacing: 1px; white-space: nowrap; overflow: hidden; margin-top: 6px; mask-image: linear-gradient(90deg, #000 70%, transparent); }
@keyframes pop { from { transform: scale(.96); opacity: 0; } to { transform: none; opacity: 1; } }

.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; margin-top: 12px; }
.fcard { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: var(--card); }
.fcard.focus { box-shadow: 0 0 0 2px var(--chg); }
.fcard h4 { margin: 0; padding: 6px 10px; color: #fff; font-size: 12.5px; display: flex; justify-content: space-between; }
.fcard h4 small { font-weight: 500; opacity: .9; }
.fcard.l1 h4 { background: var(--l1); } .fcard.l2 h4 { background: var(--l2); } .fcard.l3 h4 { background: var(--l3); } .fcard.l4 h4 { background: var(--l4); } .fcard.l7 h4 { background: var(--l7); } .fcard.arp h4 { background: #0ea5e9; }
.fcard table { width: 100%; border-collapse: collapse; font-size: 12px; }
.fcard td { padding: 3px 10px; border-top: 1px solid var(--line); vertical-align: top; }
.fcard td:first-child { color: var(--muted); width: 42%; }
.fcard td:last-child { font-family: var(--mono); word-break: break-all; }
.fcard tr.chg td { background: var(--chg); color: var(--chg-ink); font-weight: 700; }
.fcard tr.chg td:first-child { color: var(--chg-ink); }
.fcard .old { text-decoration: line-through; opacity: .65; margin-right: 4px; font-weight: 500; }
.fcard pre { margin: 0; padding: 8px 10px; font-size: 11.5px; white-space: pre-wrap; word-break: break-word; max-height: 260px; overflow: auto; }
.fcard pre .crlf { color: var(--muted); opacity: .7; }

.hex { margin-top: 12px; }
.hex summary { cursor: pointer; color: var(--muted); font-size: 13px; font-weight: 600; }
.hex pre { background: var(--soft); border-radius: 10px; padding: 10px; font-size: 11.5px; overflow-x: auto; margin: 8px 0 0; }
.hex .h-l1 { color: var(--l1); } .hex .h-l2 { color: var(--l2); } .hex .h-l3 { color: var(--l3); } .hex .h-l4 { color: var(--l4); } .hex .h-l7 { color: var(--l7); }

/* device tables */
.dtable { margin-bottom: 14px; }
.dtable h3 { margin-bottom: 4px; }
.dtable table { width: 100%; border-collapse: collapse; font-size: 11.5px; font-family: var(--mono); }
.dtable th { text-align: left; font-family: system-ui, sans-serif; font-size: 11px; color: var(--muted); font-weight: 600; padding: 3px 6px; border-bottom: 1px solid var(--line); }
.dtable td { padding: 3px 6px; border-bottom: 1px solid var(--line); }
.dtable tr.new td { background: var(--chg); color: var(--chg-ink); }
.dtable .empty { color: var(--muted); font-style: italic; font-family: system-ui, sans-serif; font-size: 12px; padding: 2px 6px; }
.legend { border-top: 1px solid var(--line); padding-top: 10px; }
.lg { display: flex; align-items: center; gap: 8px; font-size: 12.5px; margin: 3px 0; }
.lg i { width: 14px; height: 14px; border-radius: 4px; display: inline-block; }
.lg i.chg { background: var(--chg); }

/* browser preview */
.preview { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; margin-top: 8px; }
.preview .bar { background: var(--soft); padding: 5px 10px; font-family: var(--mono); font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--line); }
.preview .page { background: #fff; color: #222; padding: 4px 18px 14px; font-family: Georgia, 'Times New Roman', serif; min-height: 120px; }
.preview .page h1 { font-size: 26px; margin: 12px 0 8px; }

footer { text-align: center; color: var(--muted); font-size: 12.5px; padding: 14px; }

@media (max-width: 1250px) {
  .workspace { grid-template-columns: 230px minmax(0, 1fr); }
  .tables-card { grid-column: 1 / -1; }
}
@media (max-width: 820px) {
  main { padding: 12px 16px; }
  .topbar { padding: 10px 16px; flex-wrap: wrap; }
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .status { grid-template-columns: repeat(2, 1fr); }
}

/* ===== V2: diagram on the left third, simulation on the right two thirds ===== */
main.layout { max-width: none; display: grid; grid-template-columns: minmax(360px, 1fr) minmax(0, 2fr); gap: 14px; align-items: start; }
.left { position: sticky; top: 78px; max-height: calc(100vh - 90px); overflow-y: auto; display: flex; flex-direction: column; gap: 14px; scrollbar-width: thin; border-radius: 16px; }
.right { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.diagram-card { padding: 8px; }
/* the diagram keeps its proportions and always fits the screen height */
.diagram { min-width: 0; aspect-ratio: 600 / 870; width: min(100%, max(360px, calc((100vh - 130px) * 600 / 870))); margin: 0 auto; container-type: inline-size; }
/* while the simulation runs, the setup collapses into one summary line */
.setup.locked { display: none; }
.setup.locked + .runinfo + .runbar { border-top: 0; padding-top: 0; }
.runinfo:empty { display: none; }
.runinfo { font-size: 13.5px; color: var(--muted); }
.runinfo b { color: var(--ink); }
.stat b { font-size: 14px; }
/* node cards scale with the diagram */
.ncard { font-size: clamp(6px, 1.85cqw, 14px); padding: .45em .75em; border-radius: .8em; }
.ncard .nm { font-size: 1.08em; margin-bottom: .15em; }
.ncard .ifn { font-size: .9em; margin-top: .25em; }
.ncard .row { grid-template-columns: 2.7em auto; gap: .35em; margin-top: .1em; }
.ncard .row span { font-size: .9em; }
.ncard .val { font-size: .95em; padding: .05em .45em; border-radius: .4em; }
.ncard input.val { width: 11.5em; font-size: .95em; }
.ncard .err { font-size: .9em; max-width: 16em; }
.workspace { grid-template-columns: 220px minmax(0, 1fr); }
.status { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.tables-card .dtable table { font-size: 11px; }
@media (max-width: 1100px) {
  main.layout { grid-template-columns: minmax(0, 1fr); }
  .left { position: static; max-height: none; overflow: visible; }
  .diagram { width: min(100%, 560px); }
}
@media (max-width: 820px) {
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .status { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
