:root {
  --bg: #ffffff;
  --bg-2: #ffffff;
  --bg-3: #f5f7f8;
  --line: #d7dee3;
  --grid: #e8edf0;
  --text: #16242d;
  --muted: #5f7280;
  --accent: #0b57d0;
  --l2: #a86400;   /* Ethernet / ARP — ochre ink */
  --l3: #1d5fc2;   /* IPv4 — blue ink */
  --l4t: #0e7a5c;  /* TCP — green ink */
  --l4u: #6f3bb0;  /* UDP — violet ink */
  --l7: #b8175a;   /* DNS / HTTP / payload — magenta ink */
  --l6: #c44d14;   /* TLS — rust ink */
  --lost: #c62828;
  --on-accent: #ffffff; /* text on a filled accent/layer color */
  --font: "Gothic A1", "Noto Sans JP", system-ui, sans-serif;
  --mono: "JetBrains Mono", "IBM Plex Mono", ui-monospace, monospace;
}
:root:not([data-theme="light"]) { color-scheme: light; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11181f; --bg-2: #11181f; --bg-3: #1a232c; --line: #2c3a46; --grid: #1b2630;
    --text: #e4ecf2; --muted: #93a6b6; --accent: #6aa6ff;
    --l2: #e0a64a; --l3: #6aa6ff; --l4t: #3fc99a; --l4u: #b98cff; --l7: #ff79b0; --l6: #ff9a5c; --lost: #ff6b6b; --on-accent: #0b1420;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #11181f; --bg-2: #11181f; --bg-3: #1a232c; --line: #2c3a46; --grid: #1b2630;
  --text: #e4ecf2; --muted: #93a6b6; --accent: #6aa6ff;
  --l2: #e0a64a; --l3: #6aa6ff; --l4t: #3fc99a; --l4u: #b98cff; --l7: #ff79b0; --l6: #ff9a5c; --lost: #ff6b6b; --on-accent: #0b1420;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 14px; line-height: 1.5; }
body { min-height: 100vh; padding-block: 0 24px; padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px)); }
h1 { font-size: 20px; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
code, pre, .mono { font-family: var(--mono); }
button { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 3px; padding: 6px 10px; cursor: pointer; }
button:hover { background: var(--line); }
button:focus-visible, select:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); color: var(--on-accent); border-color: transparent; font-weight: 600; }
button.primary:hover { filter: brightness(1.1); }
select { font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 3px; padding: 6px 8px; max-width: 100%; }
.sel { flex: 1 1 420px; min-width: 0; }
.sel select { flex: 1; min-width: 0; width: 100%; text-overflow: ellipsis; }

.top { padding: 16px 0 10px; display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: center; justify-content: space-between; }
.top-right { display: flex; gap: 8px; align-items: center; }
.theme-btn { display: grid; place-items: center; width: 32px; height: 30px; padding: 0; color: var(--muted); }
.theme-btn:hover { color: var(--text); }
.theme-btn svg { width: 17px; height: 17px; }
/* show the moon in light mode (click → dark) and the sun in dark mode (click → light) */
.theme-btn .sun, :root[data-theme="dark"] .theme-btn .moon { display: none; }
:root[data-theme="dark"] .theme-btn .sun { display: block; }
.langs { display: flex; border: 1px solid var(--line); border-radius: 3px; overflow: hidden; }
.langs button { border: 0; border-radius: 0; padding: 5px 11px; font-size: 12.5px; background: var(--bg); color: var(--muted); }
.langs button + button { border-left: 1px solid var(--line); }
.langs button[aria-pressed="true"] { background: var(--text); color: var(--bg); font-weight: 700; }
.brand { display: flex; gap: 14px; align-items: center; --cloud: var(--bg-2); --lens: color-mix(in srgb, var(--l3) 18%, var(--bg-2)); --ink: var(--text); }
.brand-mark { width: 48px; height: 40px; flex: none; }
h1 { font-size: 24px; }
.tagline { margin: 0; color: var(--muted); font-size: 12.5px; }
.controls { display: flex; flex-wrap: wrap; gap: 10px 18px; align-items: center; padding: 10px 0 14px; }
.controls label { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
.transport { display: flex; gap: 4px; }
.speed input { width: 110px; accent-color: var(--accent); }
.speed output { min-width: 3.5em; color: var(--text); font-family: var(--mono); }
.clock { font-family: var(--mono); font-size: 18px; min-width: 7ch; text-align: right; margin-left: auto; }
.clock .unit { color: var(--muted); font-size: 12px; margin-left: 3px; }

.brief { padding: 12px 2px 8px; max-width: 1100px; }
.brief p { margin: 0; }
#summary { color: var(--muted); }
.phase { margin-top: 6px !important; color: var(--accent); font-weight: 500; min-height: 1.5em; }

.grid { display: grid; gap: 28px 32px; grid-template-columns: minmax(0, 1.25fr) minmax(360px, 1fr); grid-template-areas: "topo insp" "ladder insp" "log log"; }
.panel { background: var(--bg-2); min-width: 0; overflow: hidden; }
.topo { grid-area: topo; }
#topo { display: block; width: 100%; min-width: 640px; height: 450px; }
.topo-scroll { overflow-x: auto; }
.topo canvas, .ladder canvas { display: block; width: 100%; background-image: linear-gradient(var(--grid) 1px, transparent 1px), linear-gradient(90deg, var(--grid) 1px, transparent 1px); background-size: 24px 24px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.legend { display: flex; flex-wrap: wrap; gap: 2px 14px; font-size: 11px; }
.legend span { color: var(--c); font-weight: 700; }
.ladder { grid-area: ladder; }
.ladder-scroll { height: 440px; overflow: auto; }
.ladder-scroll canvas { min-width: 640px; }
.panel-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-end; gap: 6px 16px; padding-bottom: 8px; }
.panel-head > div:first-child { min-width: 0; flex: 1 1 320px; }
.panel-head h2 { font-size: 14px; font-weight: 700; margin: 0; text-wrap: balance; }
.panel-head p { margin: 2px 0 0; color: var(--muted); font-size: 12px; max-width: 70ch; }
.inspector { grid-area: insp; display: flex; flex-direction: column; contain: size; } /* its height follows the two diagrams */
.log { grid-area: log; max-height: 320px; display: flex; flex-direction: column; }

.tabs { display: flex; border-bottom: 1px solid var(--line); padding: 0; flex: none; gap: 6px; }
.tab { background: none; border: 0; border-bottom: 2px solid transparent; border-radius: 0; padding: 8px 4px; color: var(--muted); }
.tab.active { color: var(--text); border-bottom-color: var(--text); font-weight: 700; }
.tabpane { display: none; overflow: auto; padding: 12px 0; flex: 1; }
.tabpane.active { display: block; }
.empty { color: var(--muted); margin: 0; }

.pkt-head { font-size: 13px; margin-bottom: 10px; }
.pkt-head .route { color: var(--muted); font-family: var(--mono); font-size: 12px; }
.pkt-head b { color: var(--accent); font-weight: 500; }
.stack { display: flex; flex-direction: column; margin-bottom: 14px; }
.layer { display: grid; grid-template-columns: 110px 1fr auto; gap: 8px; align-items: baseline; padding: 5px 0 5px calc(var(--depth, 0) * 18px); border-bottom: 1px dotted var(--grid); font-size: 12.5px; transition: opacity .25s, transform .25s; }
.layer.dim { opacity: .3; }
.layer.hot .ltitle { text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 4px; }
.layer.hot { animation: layer-in .35s ease-out; }
@keyframes layer-in { from { opacity: .3; } to { opacity: 1; } }
.tcp-ep .state { transition: background .3s; }
.tcp-ep .state.changed { animation: flash .6s ease-out; }
@keyframes flash { from { background: var(--accent); color: var(--on-accent); } }
.layer .ltitle { font-weight: 700; color: var(--c); white-space: nowrap; }
.layer .lsum { color: var(--text); font-family: var(--mono); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.layer .lsize { color: var(--muted); font-family: var(--mono); font-size: 11px; }
.stack-note { color: var(--muted); font-size: 12px; margin: 2px 0 0; }

.hex { font-family: var(--mono); font-size: 12px; line-height: 1.55; padding: 8px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); overflow-x: auto; margin-bottom: 14px; }
.hex .row { white-space: nowrap; }
.hex .off { color: var(--muted); margin-right: 10px; }
.hex .b { color: var(--c); padding: 0 1px; border-radius: 2px; }
.hex .b.hl { background: var(--c); color: var(--on-accent); }
.hex .asc { color: var(--muted); margin-left: 12px; }
.fields details { margin-bottom: 8px; }
.fields summary { cursor: pointer; color: var(--c); font-weight: 700; font-size: 12.5px; }
.fields table { border-collapse: collapse; width: 100%; font-size: 12px; margin-top: 4px; }
.fields td { padding: 2px 6px 2px 0; vertical-align: top; }
.fields td:first-child { padding-left: 14px; }
.fields td:first-child { color: var(--muted); white-space: nowrap; width: 150px; }
.fields td:last-child { font-family: var(--mono); word-break: break-all; white-space: pre-wrap; }
.fields tr:hover td { background: var(--bg-3); cursor: default; }

.tcp-ep { padding: 0 0 12px; margin-bottom: 12px; border-bottom: 1px solid var(--line); }
.tcp-ep h3 { margin: 0 0 6px; font-size: 13px; display: flex; gap: 10px; align-items: baseline; }
.tcp-ep .state { font-family: var(--mono); color: var(--l4t); font-weight: 600; }
.tcp-ep .nums { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 4px 12px; font-size: 12px; margin-bottom: 8px; }
.tcp-ep .nums span { font-family: var(--mono); font-size: 11.5px; }
.tcp-ep .nums em { font-style: normal; color: var(--muted); font-size: 11px; margin-left: 4px; }
.tcp-ep .nums > div { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 4px; }
.tcp-ep .nums b { flex-basis: 100%; }
.tcp-ep .nums b { font-family: var(--mono); font-weight: 500; }
.seqbar { position: relative; height: 16px; background: var(--bg-3); overflow: hidden; }
.seqbar i { position: absolute; top: 0; bottom: 0; }
.seqbar .acked { background: var(--l4t); opacity: .55; }
.seqbar .flight { background: var(--l3); opacity: .7; }
.seqbar .ooo { background: var(--l6); opacity: .8; }
.seqbar .cw { border-right: 2px dashed var(--accent); }
.seqlegend { font-size: 11px; color: var(--muted); margin-top: 4px; }
.seqlegend i { display: inline-block; width: 10px; height: 10px; vertical-align: -1px; margin: 0 4px 0 10px; border-radius: 2px; }
.chart-wrap { padding: 4px 0; }
.chart-title { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
#cwnd { width: 100%; height: 190px; display: block; }

.rt { margin-bottom: 12px; }
.rt h3 { margin: 0 0 4px; font-size: 13px; }
.rt table { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 12px; }
.rt th { text-align: left; color: var(--muted); font-weight: 500; padding: 2px 6px; border-bottom: 1px solid var(--line); }
.rt td { padding: 2px 6px; }
.rt tr.chosen td { font-weight: 700; color: var(--l3); }
.rt .down { color: var(--lost); }
.rt .lookup { font-size: 12px; color: var(--muted); margin: 0 0 4px; }

.code-tabs { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.code-tabs button { font-size: 12px; padding: 4px 8px; }
.code-tabs button.active { background: var(--text); color: var(--bg); border-color: var(--text); }
#code { margin: 0; padding: 10px 0; border-top: 1px solid var(--line); font-size: 12px; line-height: 1.5; overflow: auto; white-space: pre; }
#code .cm { color: var(--muted); }
#code .kw { color: var(--l3); }
#code .ty { color: var(--l4t); }
#code .st { color: var(--l6); }

.log-head { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--line); font-weight: 700; flex: none; }
.filters { display: flex; gap: 10px; font-size: 12px; font-weight: 400; }
.filters label { display: flex; align-items: center; gap: 4px; color: var(--c); cursor: pointer; }
.filters input { accent-color: var(--c); }
#log { list-style: none; margin: 0; padding: 6px 0; overflow: auto; font-size: 12.5px; }
#log li { display: grid; grid-template-columns: 52px 56px 34px 1fr; gap: 8px; padding: 2px 0; border-bottom: 1px dotted var(--grid); }
#log li .t { font-family: var(--mono); color: var(--muted); }
#log li .n { font-family: var(--mono); }
#log li .l { font-family: var(--mono); color: var(--c); }
#log li.new { background: var(--bg-3); }
#log li.evt { color: var(--accent); }

.foot { color: var(--muted); font-size: 12px; margin-top: 16px; max-width: 1100px; }
.foot code { color: var(--text); }

@media (max-width: 1100px) {
  .grid { grid-template-columns: 1fr; grid-template-areas: "topo" "insp" "ladder" "log"; }
  .inspector { contain: none; height: 640px; }
  #topo { height: 380px; }
  .ladder-scroll { height: 400px; }
}
@media (prefers-reduced-motion: reduce) { .layer, .layer.hot, .tcp-ep .state.changed { transition: none; animation: none; } }

/* ---- glossary */
.term { text-decoration: underline dotted; text-decoration-color: color-mix(in srgb, currentColor 35%, transparent); text-underline-offset: 3px; cursor: help; border-radius: 2px; }
.term:hover, .term:focus-visible { background: var(--bg-3); outline: none; }
#tip { position: absolute; z-index: 50; background: var(--bg); color: var(--text); border: 1px solid var(--text); padding: 10px 12px; font-size: 12.5px; line-height: 1.5; box-shadow: 0 6px 20px rgba(0,0,0,.12); }
#tip .tip-name { font-weight: 700; margin-bottom: 2px; }
#tip .tip-short { margin-bottom: 6px; }
#tip .tip-long { color: var(--muted); }
#tip .tip-more { margin-top: 8px; font-size: 12px; color: var(--accent); cursor: pointer; }
.gl-intro { color: var(--muted); font-size: 12.5px; margin: 0 0 8px; }
#gl-filter { font: inherit; width: 100%; padding: 6px 8px; border: 1px solid var(--line); border-radius: 3px; background: var(--bg); color: var(--text); margin-bottom: 12px; }
.gl-cat h3 { font-size: 12px; color: var(--muted); font-weight: 700; margin: 14px 0 6px; padding-bottom: 4px; border-bottom: 1px solid var(--line); }
.gl-item { padding: 6px 0 8px; border-bottom: 1px dotted var(--grid); }
.gl-item h4 { margin: 0 0 2px; font-size: 13px; }
.gl-item p { margin: 0; font-size: 12.5px; }
.gl-item .gl-long { color: var(--muted); margin-top: 3px; }
.gl-item.flash { animation: flash .9s ease-out; }
#log .term { text-decoration-color: color-mix(in srgb, currentColor 22%, transparent); }

/* Japanese: kana and kanji come from Noto Sans JP; Latin still reads fine in it */
:root:lang(ja) { --font: "Noto Sans JP", "Gothic A1", system-ui, sans-serif; }
