:root {
  --ink: #1d232a;
  --muted: #56606b;
  --paper: #fbfaf7;
  --rule: #d9dde2;
  --accent: #23406b;
  --parent: #0072b2;
  --minor: #d55e00;
  --mixed: #6b6f76;
  --root: #23406b;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
html { font-size: clamp(15px, 0.8rem + 0.45vw, 34px); }
body {
  display: flex; flex-direction: column;
  background: var(--paper); color: var(--ink);
  font: 1rem/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}
a { color: var(--accent); }

.bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem 1rem;
  padding: 0.5rem 0.9rem; border-bottom: 1px solid var(--rule); background: #fff;
}
.bar h1 { font-size: 1.05rem; margin: 0; flex: 1 1 auto; }
.bar .sub { font-weight: 400; color: var(--muted); font-size: 0.85em; margin-left: 0.4em; }
.back { text-decoration: none; font-weight: 600; white-space: nowrap; }
.tools { display: flex; gap: 0.3rem; flex-wrap: wrap; }
.tools button {
  font: inherit; font-size: 0.85rem; min-height: 2.4em; min-width: 2.4em;
  padding: 0.2em 0.8em; border: 1px solid var(--rule); border-radius: 6px;
  background: #fff; color: var(--ink); cursor: pointer;
}
.tools button:hover, .tools button:focus-visible { background: #eef2f7; }
.notice { margin: 0; padding: 0.35rem 0.9rem; font-size: 0.8rem; background: #fff6e0; border-bottom: 1px solid #ecd9a6; }

#stage { position: relative; flex: 1 1 auto; min-height: 0; }
#map { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#map:active { cursor: grabbing; }

.link { fill: none; stroke-width: 2.6px; }
.link.cuts-parent { stroke: var(--parent); }
.link.cuts-minor { stroke: var(--minor); stroke-dasharray: 9 6; }
.link.cuts-mixed { stroke: var(--mixed); stroke-dasharray: 2 5; stroke-linecap: round; }

.node { cursor: pointer; }
.node .pill { fill: #fff; stroke-width: 2.2px; }
.node.cuts-parent .pill { stroke: var(--parent); }
.node.cuts-minor .pill { stroke: var(--minor); stroke-dasharray: 7 4; }
.node.cuts-mixed .pill { stroke: var(--mixed); stroke-dasharray: 2 4; }
.node.root .pill { fill: var(--root); stroke: var(--root); }
.node.root text { fill: #fff; font-weight: 700; }
.node text { font-size: 14px; fill: var(--ink); pointer-events: none; }
.node .glyph-bg.cuts-parent { fill: var(--parent); }
.node .glyph-bg.cuts-minor { fill: var(--minor); }
.node .glyph-bg.cuts-mixed { fill: var(--mixed); }
.node text.glyph { fill: #fff; font-weight: 700; font-size: 11px; }
.node .toggle { fill: var(--accent); font-size: 13px; font-weight: 700; }
.node.selected .pill { stroke-width: 4px; filter: drop-shadow(0 0 4px rgba(35, 64, 107, 0.45)); }
.node.has-flag .flag { fill: #c98a00; font-size: 13px; font-weight: 700; }
.node:focus { outline: none; }
.node:focus-visible .pill { stroke-width: 4px; }

.legend-box {
  position: absolute; left: 0.6rem; bottom: 0.6rem; padding: 0.35rem 0.7rem;
  background: rgba(255, 255, 255, 0.94); border: 1px solid var(--rule); border-radius: 8px;
  font-size: 0.78rem; max-width: min(24rem, calc(100% - 1.2rem));
}
.legend-box summary { cursor: pointer; font-weight: 600; color: var(--muted); }
.legend { margin: 0.3rem 0 0.15rem; padding: 0; list-style: none; display: grid; gap: 0.25rem; }
.legend li { display: flex; align-items: center; gap: 0.5rem; }
.legend svg { flex: none; }
.legend .flag-key { color: #8a5f00; font-weight: 700; width: 46px; text-align: center; }

#panel {
  position: fixed; right: 0; top: 0; bottom: 0; width: min(30rem, 92vw);
  background: #fff; border-left: 1px solid var(--rule); box-shadow: -6px 0 18px rgba(0, 0, 0, 0.08);
  overflow-y: auto; padding: 1rem 1.1rem 2rem; z-index: 5;
}
#panel h2 { font-size: 1.15rem; margin: 0 2rem 0.4rem 0; line-height: 1.3; }
#panel .summary { font-size: 0.98rem; }
#panel ul { padding-left: 1.2rem; }
#panel li { margin: 0.25rem 0; font-size: 0.9rem; }
#panel .cuts { display: inline-flex; align-items: center; gap: 0.4em; font-size: 0.78rem; font-weight: 600; border: 2px solid currentColor; border-radius: 999px; padding: 0.05em 0.6em 0.05em 0.15em; }
#panel .cuts-parent { color: var(--parent); border-style: solid; }
#panel .cuts-minor { color: var(--minor); border-style: dashed; }
#panel .cuts-mixed { color: var(--mixed); border-style: dotted; }
#panel .g { display: inline-grid; place-items: center; width: 1.4em; height: 1.4em; border-radius: 50%; color: #fff; font-size: 0.85em; }
#panel .cuts-parent .g { background: var(--parent); }
#panel .cuts-minor .g { background: var(--minor); }
#panel .cuts-mixed .g { background: var(--mixed); }
#panel .why { color: var(--muted); font-size: 0.85rem; margin: 0.3rem 0 0.6rem; }
#panel .flags { background: #fff6e0; border-left: 4px solid #c98a00; padding: 0.4rem 0.7rem; font-size: 0.88rem; }
#panel h3 { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 1rem 0 0.2rem; }
#panel .tag { font-size: 0.72rem; background: #eceae4; border-radius: 4px; padding: 0 0.35em; color: #4a4a4a; }
#panel .more { display: inline-block; margin-top: 0.8rem; font-weight: 600; }
.close {
  position: absolute; right: 0.5rem; top: 0.5rem; width: 2.4rem; height: 2.4rem;
  font-size: 1.5rem; line-height: 1; border: 0; background: transparent; cursor: pointer; color: var(--muted);
}

/* Phones: panel becomes a bottom sheet, legend compacts. */
@media (max-width: 700px) {
  .bar .sub { display: none; }
  #panel { top: auto; left: 0; right: 0; width: 100%; height: 58%; border-left: 0; border-top: 1px solid var(--rule); border-radius: 14px 14px 0 0; box-shadow: 0 -6px 18px rgba(0, 0, 0, 0.12); }
  .legend { font-size: 0.7rem; }
}
