/* ---------- Theme tokens ---------- */
:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --text: #0f172a;
  --muted: #5b6577;
  --border: #d9dee8;
  --accent: #4f46e5;
  --arrow: #334155;
  --arrow-faint: rgba(51, 65, 85, 0.16);
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 2px 8px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 20px 50px rgba(15, 23, 42, 0.25);
  --danger: #dc2626;
  --warning: #d97706;
  --ok: #059669;

  /* one colour per OSI layer */
  --l7: #8b5cf6;
  --l6: #ec4899;
  --l5: #f97316;
  --l4: #f59e0b;
  --l3: #10b981;
  --l2: #0ea5e9;
  --l1: #6366f1;

  --radius: 10px;
  --card-w: 148px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1020;
    --surface: #141a2e;
    --surface-2: #1b2340;
    --text: #e6e9f2;
    --muted: #9aa3b8;
    --border: #2a3352;
    --accent: #a5b4fc;
    --arrow: #e2e8f0;
    --arrow-faint: rgba(226, 232, 240, 0.13);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.6);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1020;
  --surface: #141a2e;
  --surface-2: #1b2340;
  --text: #e6e9f2;
  --muted: #9aa3b8;
  --border: #2a3352;
  --accent: #a5b4fc;
  --arrow: #e2e8f0;
  --arrow-faint: rgba(226, 232, 240, 0.13);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 24px;
  padding: 12px 24px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px);
  -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo { width: 34px; height: 34px; flex: none; }
.brand-logo rect:nth-child(1) { fill: var(--l7); }
.brand-logo rect:nth-child(2) { fill: var(--l4); }
.brand-logo rect:nth-child(3) { fill: var(--l3); }
.brand-logo rect:nth-child(4) { fill: var(--l2); }
.brand h1 { margin: 0; font-size: 20px; line-height: 1.2; letter-spacing: -0.01em; }
.tagline { margin: 0; color: var(--muted); font-size: 13px; }

.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.segmented {
  display: inline-flex;
  padding: 3px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
}
.segmented button {
  border: 0;
  background: transparent;
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  white-space: nowrap;
}
.segmented button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); cursor: pointer; }
.toggle input { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }

.search {
  position: relative;
  display: flex;
  align-items: center;
}
.search svg {
  position: absolute;
  left: 10px;
  width: 16px;
  height: 16px;
  fill: none;
  stroke: var(--muted);
  stroke-width: 2;
  stroke-linecap: round;
  pointer-events: none;
}
.search input {
  width: 250px;
  padding: 7px 12px 7px 32px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-size: 14px;
}
.search input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.search-count {
  position: absolute;
  right: 34px;
  font-size: 12px;
  color: var(--muted);
  pointer-events: none;
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
}
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.i-moon { display: none; }
:root[data-theme="dark"] .i-sun { display: none; }
:root[data-theme="dark"] .i-moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .i-sun { display: none; }
  :root:not([data-theme="light"]) .i-moon { display: block; }
}

/* ---------- Legend ---------- */
.legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 20px;
  padding: 10px 24px;
  font-size: 12.5px;
  color: var(--muted);
}
.hint { font-weight: 600; color: var(--text); }
.legend-items { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.lg { display: inline-flex; align-items: center; gap: 6px; }
.arrow-sample { width: 34px; height: 10px; }
.arrow-sample path { stroke: var(--arrow); stroke-width: 2; fill: none; }
.arrow-sample path.head { fill: var(--arrow); stroke: none; }
.sample-card { display: inline-block; width: 22px; height: 14px; border: 2px solid var(--muted); border-radius: 4px; }
.sample-card.dashed { border-style: dashed; }

/* ---------- Badges (icons drawn with CSS masks) ---------- */
.badge {
  display: inline-block;
  width: 13px;
  height: 13px;
  background: currentColor;
  -webkit-mask: var(--icon) center / contain no-repeat;
  mask: var(--icon) center / contain no-repeat;
  flex: none;
}
.b-lock { color: var(--ok); --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M7 11V8a5 5 0 0 1 10 0v3h1a1 1 0 0 1 1 1v9a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1v-9a1 1 0 0 1 1-1zm2 0h6V8a3 3 0 0 0-6 0z'/%3E%3C/svg%3E"); }
.b-optlock { color: var(--muted); --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M7 11V8a5 5 0 0 1 9.9-1h-2.1A3 3 0 0 0 9 8v3h9a1 1 0 0 1 1 1v9a1 1 0 0 1-1 1H6a1 1 0 0 1-1-1v-9a1 1 0 0 1 1-1z'/%3E%3C/svg%3E"); }
.b-warn { color: var(--danger); --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill-rule='evenodd' d='M12 2 1 21h22zm-1 7h2v6h-2zm0 8h2v2h-2z'/%3E%3C/svg%3E"); }
.b-legacy { color: var(--warning); --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill-rule='evenodd' d='M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20zm-1 4h2v5.4l3.7 2.2-1 1.7L11 12.6z'/%3E%3C/svg%3E"); }
.tag {
  display: inline-block;
  padding: 0 5px;
  border-radius: 4px;
  font-size: 10.5px;
  font-weight: 700;
  line-height: 16px;
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
}

/* ---------- Diagram ---------- */
main { flex: 1; padding: 0 24px 24px; }
.diagram {
  position: relative;
  z-index: 0;
}
.arrows {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  pointer-events: none;
  overflow: visible;
}
.arrows path.edge {
  fill: none;
  stroke: var(--arrow);
  stroke-width: 1.6;
  opacity: 0.85;
}
.arrows path.edge.direct { stroke-width: 2.4; opacity: 1; }
.arrows path.edge.faint { stroke: var(--arrow-faint); stroke-width: 1; opacity: 1; }
.arrows .head { fill: var(--arrow); }
.arrows .head-faint { fill: var(--arrow-faint); }
.arrows path.edge.draw { stroke-dasharray: var(--len); stroke-dashoffset: var(--len); animation: draw 0.35s ease-out forwards; }
@keyframes draw { to { stroke-dashoffset: 0; } }
@media (prefers-reduced-motion: reduce) {
  .arrows path.edge.draw { animation: none; stroke-dashoffset: 0; }
}

.band {
  --lc: var(--l7);
  display: grid;
  grid-template-columns: 150px 1fr;
  margin-bottom: 8px;
  border-radius: 14px;
  background: color-mix(in srgb, var(--lc) 9%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--lc) 30%, var(--border));
}
.band[data-color="7"] { --lc: var(--l7); }
.band[data-color="6"] { --lc: var(--l6); }
.band[data-color="5"] { --lc: var(--l5); }
.band[data-color="4"] { --lc: var(--l4); }
.band[data-color="3"] { --lc: var(--l3); }
.band[data-color="2"] { --lc: var(--l2); }
.band[data-color="1"] { --lc: var(--l1); }

.band-label {
  padding: 10px 14px;
  border-right: 1px solid color-mix(in srgb, var(--lc) 25%, var(--border));
}
.band-num {
  display: inline-grid;
  place-items: center;
  min-width: 30px;
  height: 30px;
  padding: 0 6px;
  border-radius: 8px;
  background: var(--lc);
  color: #fff;
  font-weight: 800;
  font-size: 15px;
}
.band-name { display: block; margin-top: 8px; font-weight: 700; font-size: 15px; line-height: 1.25; }
.band-sub { display: block; font-size: 12px; color: var(--muted); }
.band-desc { display: none; }

.band-body {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 8px 10px;
  padding: 8px 10px;
  min-width: 0;
}
.cluster {
  min-width: 0;
  padding: 5px 8px 8px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--surface) 55%, transparent);
  border: 1px dashed color-mix(in srgb, var(--lc) 25%, var(--border));
}
.cluster-title {
  margin: 0 0 4px;
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.cards { display: flex; flex-wrap: wrap; gap: 6px; }

/* ---------- Cards ---------- */
.card {
  --cc: var(--l7);
  position: relative;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  width: var(--card-w);
  min-height: 44px;
  padding: 4px 8px 4px 10px;
  text-align: left;
  background: var(--surface);
  border: 1.5px solid color-mix(in srgb, var(--cc) 45%, var(--border));
  border-left: 4px solid var(--cc);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  transition: opacity 0.15s, transform 0.15s, box-shadow 0.15s, background 0.15s;
}
.card.l7 { --cc: var(--l7); }
.card.l6 { --cc: var(--l6); }
.card.l5 { --cc: var(--l5); }
.card.l4 { --cc: var(--l4); }
.card.l3 { --cc: var(--l3); }
.card.l2 { --cc: var(--l2); }
.card.l1 { --cc: var(--l1); }
.card.kind-framework { border-style: dashed; border-left-style: solid; }
.card:hover { transform: translateY(-1px); }
.card-name { font-weight: 700; font-size: 13.5px; line-height: 1.25; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-meta { font-size: 11px; color: var(--muted); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-badges { position: absolute; bottom: 5px; right: 6px; display: flex; gap: 3px; align-items: center; }
.card-badges .tag { font-size: 9.5px; line-height: 14px; padding: 0 3px; }
.card-meta { padding-right: 30px; }
.card.has-tag .card-meta { padding-right: 52px; }

/* big important protocols get wider cards */
.card.wide { width: calc(var(--card-w) * 1.6); }

/* highlight states */
.diagram.has-focus .card { opacity: 0.2; }
.diagram.has-focus .card.is-related { opacity: 1; background: color-mix(in srgb, var(--cc) 14%, var(--surface)); }
.diagram.has-focus .card.is-focus {
  opacity: 1;
  background: color-mix(in srgb, var(--cc) 24%, var(--surface));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--cc) 55%, transparent), var(--shadow);
}
.card.search-miss { opacity: 0.15; }
.diagram.has-focus .card.is-related.search-miss { opacity: 0.6; }
.card.search-hit { box-shadow: 0 0 0 2px var(--accent), var(--shadow); }

/* ---------- Footer ---------- */
.footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 6px 20px;
  padding: 12px 24px 18px;
  font-size: 12px;
  color: var(--muted);
}
.load-error { padding: 24px; color: var(--danger); font-weight: 600; }

/* ---------- Dialogs ---------- */
.dlg {
  --cc: var(--l7);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  max-height: calc(100vh - 48px);
  overflow: auto;
}
.dlg::backdrop { background: rgba(15, 23, 42, 0.18); }
.dlg-detail::backdrop { background: rgba(15, 23, 42, 0.45); }
.dlg.l7 { --cc: var(--l7); } .dlg.l6 { --cc: var(--l6); } .dlg.l5 { --cc: var(--l5); }
.dlg.l4 { --cc: var(--l4); } .dlg.l3 { --cc: var(--l3); } .dlg.l2 { --cc: var(--l2); } .dlg.l1 { --cc: var(--l1); }

/* the first popup sits on the right so the highlighted chain stays visible */
.dlg-summary {
  width: min(420px, calc(100vw - 32px));
  margin: 90px 24px auto auto;
}
.dlg-detail { width: min(760px, calc(100vw - 32px)); }

.dlg-head {
  position: relative;
  padding: 18px 52px 14px 20px;
  background: linear-gradient(135deg, color-mix(in srgb, var(--cc) 22%, var(--surface)), var(--surface));
  border-bottom: 1px solid var(--border);
}
.dlg-head h2 { margin: 0; font-size: 22px; line-height: 1.2; }
.dlg-head .full { margin: 2px 0 0; color: var(--muted); font-size: 13.5px; }
.dlg-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 18px;
  line-height: 1;
}
.dlg-body { padding: 16px 20px 20px; }
.dlg-body p { margin: 0 0 12px; }
.pills { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 0; }
.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.pill.layer { background: var(--cc); border-color: var(--cc); color: #fff; }
.pill.warn { color: var(--danger); }
.pill.legacy { color: var(--warning); }
.pill.ok { color: var(--ok); }

.section-title { margin: 16px 0 6px; font-size: 11.5px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.ports { margin: 0; padding: 0; list-style: none; display: flex; flex-wrap: wrap; gap: 6px; }
.ports li { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; padding: 2px 8px; border-radius: 6px; background: var(--surface-2); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  --cc: var(--l7);
  padding: 3px 10px;
  border-radius: 999px;
  border: 1.5px solid color-mix(in srgb, var(--cc) 55%, var(--border));
  background: color-mix(in srgb, var(--cc) 10%, var(--surface));
  font-size: 12.5px;
  font-weight: 600;
}
.chip:hover { background: color-mix(in srgb, var(--cc) 22%, var(--surface)); }
.chip.l7 { --cc: var(--l7); } .chip.l6 { --cc: var(--l6); } .chip.l5 { --cc: var(--l5); }
.chip.l4 { --cc: var(--l4); } .chip.l3 { --cc: var(--l3); } .chip.l2 { --cc: var(--l2); } .chip.l1 { --cc: var(--l1); }
.none { color: var(--muted); font-size: 13px; }
.note { padding: 10px 12px; border-radius: 10px; background: var(--surface-2); font-size: 13.5px; }

.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.btn {
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 600;
  font-size: 14px;
}
.btn.primary { background: var(--cc); border-color: var(--cc); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }

.attacks { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.attacks li {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--danger) 30%, var(--border));
  background: color-mix(in srgb, var(--danger) 6%, var(--surface));
}
.attacks strong { display: block; font-size: 14px; }
.attacks span { font-size: 13.5px; color: var(--muted); }
.std a { color: var(--accent); }

/* ---------- Small screens ---------- */
@media (max-width: 900px) {
  .topbar { position: static; padding: 12px 16px; }
  .legend, main, .footer { padding-left: 16px; padding-right: 16px; }
  .band { grid-template-columns: 1fr; }
  .band-label { border-right: 0; border-bottom: 1px solid color-mix(in srgb, var(--lc) 25%, var(--border)); display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; padding: 10px 14px; }
  .band-name { margin-top: 0; }
  .band-desc { flex-basis: 100%; margin-top: 0; }
  .search input { width: 100%; }
  .search { flex: 1 1 220px; }
  .hide-sm { display: none; }
  .dlg-summary { margin: auto; }
}
@media (max-width: 520px) {
  :root { --card-w: 118px; }
  .legend-items { display: none; }
}
