/*
  Design tokens. Color carries one meaning each: neutrals for structure and
  state (hover, selected, lineage), blue for President, rose for Sensei, gold for the Evan Chen Award.
  Every role color also appears with a text label, never color alone.
*/
:root {
  color-scheme: dark;
  --bg-canvas: #0a0a0b;
  --bg: #0f0f11;
  --bg-raised: #161619;
  --bg-hover: #1d1d21;
  --bg-active: #26262b;
  --border: #232327;
  --border-strong: #34343b;
  --text: #ededef;
  --text-muted: #a1a1aa;
  --text-faint: #8a8a93;
  --president: #7aa5f8;
  --president-fill: #18243b;
  --president-border: #3b5a94;
  --president-strong: #3e6fd8;
  --sensei: #f38799;
  --sensei-fill: #351c23;
  --sensei-border: #8c3e4c;
  --sensei-strong: #c64a60;
  --award: #e0b65a;
  --on-role: #fff;
  --edge: #62626c;
  --edge-active: #ededef;
  --band: #ffffff05;
  --shadow: 0 8px 24px #00000073, 0 1px 2px #0000004d;

  --font-sans: "Avenir Next", Avenir, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-serif: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Georgia, serif;
  --text-xs: 11px;
  --text-sm: 12px;
  --text-md: 13px;
  --text-lg: 15px;
  --text-xl: 24px;
  --text-2xl: 28px;
  --weight-regular: 400;
  --weight-medium: 500;
  --weight-semibold: 600;

  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;

  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --control-h: 32px;
  --header-h: 56px;
  --toolbar-h: 56px;
  --footer-h: 32px;
  --ease: 120ms ease;
}
[data-theme="light"] {
  color-scheme: light;
  --bg-canvas: #f6f6f7;
  --bg: #fff;
  --bg-raised: #fff;
  --bg-hover: #f1f1f3;
  --bg-active: #e7e7ea;
  --border: #e6e6e9;
  --border-strong: #d1d1d6;
  --text: #18181b;
  --text-muted: #5b5b64;
  --text-faint: #6b6b74;
  --president: #2b5cc8;
  --president-fill: #e8effd;
  --president-border: #a9c0ee;
  --president-strong: #2b5cc8;
  --sensei: #bb3650;
  --sensei-fill: #fcebee;
  --sensei-border: #eab0bb;
  --sensei-strong: #bb3650;
  --award: #9a6a12;
  --edge: #8a8a94;
  --edge-active: #18181b;
  --band: #00000005;
  --shadow: 0 8px 24px #1018280f, 0 1px 2px #1018280f;
}

/* Base */
* { box-sizing: border-box; }
body { display: flex; flex-direction: column; margin: 0; height: 100dvh; overflow: hidden; color: var(--text); background: var(--bg-canvas); font: var(--text-md)/1.45 var(--font-sans); -webkit-font-smoothing: antialiased; }
h1, h2, h3, p { margin: 0; }
button, input, select { color: inherit; font: inherit; }
:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; }
[hidden] { display: none !important; }
.eyebrow { color: var(--text-faint); font-size: var(--text-sm); font-weight: var(--weight-medium); }
.class-heading .eyebrow, .lineage-section h3, .lineage-editor h3, .lineage-editor__label { color: var(--text-muted); font-size: var(--text-sm); font-weight: var(--weight-semibold); }
.lineage-section h3 .count { color: var(--text-faint); font-weight: var(--weight-regular); }
.icon { width: 16px; height: 16px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }

/* Buttons: default (bordered), quiet (borderless), primary (one per context). */
button, .import-button { display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2); height: var(--control-h); padding: 0 var(--space-3); border: 1px solid var(--border-strong); border-radius: var(--radius-sm); background: transparent; color: var(--text); font-size: var(--text-md); font-weight: var(--weight-medium); white-space: nowrap; cursor: pointer; transition: background var(--ease), border-color var(--ease), color var(--ease); }
button:hover:not(:disabled), .import-button:hover { background: var(--bg-hover); }
.import-button:focus-within { outline: 2px solid var(--text); outline-offset: 2px; }
button:active:not(:disabled) { background: var(--bg-active); }
button:disabled { opacity: .45; cursor: default; }
button[aria-pressed="true"] { background: var(--bg-active); border-color: var(--border-strong); }
.icon-button { width: var(--control-h); padding: 0; border-color: transparent; color: var(--text-muted); }
.icon-button:hover:not(:disabled) { color: var(--text); }
.primary-button, .lineage-editor__export { border-color: var(--text); background: var(--text); color: var(--bg); }
.primary-button:hover:not(:disabled), .lineage-editor__export:hover:not(:disabled) { background: var(--text-muted); border-color: var(--text-muted); }
.text-button { height: auto; padding: 4px var(--space-1); border: 0; color: var(--text-muted); font-size: var(--text-sm); }
.text-button:hover:not(:disabled) { background: transparent; color: var(--text); }

/* Header */
header { display: flex; flex: none; align-items: center; gap: var(--space-3); min-height: var(--header-h); padding: 0 var(--space-4); border-bottom: 1px solid var(--border); background: var(--bg); }
.brand { display: flex; align-items: center; gap: var(--space-3); color: var(--text); white-space: nowrap; font-weight: var(--weight-semibold); }
.crest { font: var(--weight-medium) 20px/1 var(--font-serif); letter-spacing: .02em; }
.search-wrap { position: relative; flex: 1; max-width: 420px; margin-left: auto; }
.search-wrap input { width: 100%; height: var(--control-h); padding: 0 var(--space-3) 0 34px; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); outline: none; background: var(--bg-canvas); transition: border-color var(--ease); }
.search-wrap input:focus { border-color: var(--text-muted); box-shadow: 0 0 0 1px var(--text-muted); }
.search-wrap input { text-overflow: ellipsis; }
.search-wrap input::placeholder { color: var(--text-faint); }
.search-icon { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--text-faint); pointer-events: none; }
.results { position: absolute; z-index: 8; top: calc(var(--control-h) + var(--space-1)); left: 0; right: 0; max-height: 360px; overflow: auto; padding: var(--space-1); border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--bg-raised); box-shadow: var(--shadow); }
.results p { margin: var(--space-2); color: var(--text-muted); font-size: var(--text-sm); }
.theme-toggle .icon-moon, [data-theme="light"] .theme-toggle .icon-sun { display: none; }
[data-theme="light"] .theme-toggle .icon-moon { display: block; }

/* Rows: search results, related members, and class list share one list style. */
.result, .member-link, #classes button, .view-buttons button { display: flex; flex-wrap: wrap; align-items: baseline; align-content: center; justify-content: flex-start; gap: 0 var(--space-2); width: 100%; white-space: normal; height: auto; min-height: 30px; padding: 6px var(--space-2); border: 0; border-radius: var(--radius-sm); text-align: left; font-weight: var(--weight-regular); }
.result strong, .member-link span { min-width: 0; overflow-wrap: anywhere; font-weight: var(--weight-medium); }
.result span, .member-link small { margin-left: auto; color: var(--text-muted); font-size: var(--text-sm); white-space: nowrap; }
.result:focus-visible { outline-offset: -2px; }

/* Workspace */
.workspace { position: relative; display: flex; flex: 1; min-height: 0; }
.browse { display: flex; flex: 0 0 248px; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); background: var(--bg); }
.browse-heading { padding: var(--space-5) var(--space-4) var(--space-3); }
h1 { margin: 0 0 2px; font: var(--weight-medium) 20px/1.25 var(--font-serif); }
.browse-heading p { color: var(--text-muted); font-size: var(--text-sm); }
.view-buttons { display: grid; gap: 2px; padding: 0 var(--space-2) var(--space-3); border-bottom: 1px solid var(--border); }
.class-heading { display: flex; align-items: center; justify-content: space-between; padding: var(--space-4) var(--space-4) var(--space-1); }
#classes { flex: 1; min-height: 50px; overflow: auto; padding: 0 var(--space-2) var(--space-3); }
#classes button.active { background: var(--bg-active); font-weight: var(--weight-medium); }
.class-number { width: 20px; color: var(--text-faint); font-size: var(--text-xs); font-variant-numeric: tabular-nums; }
.legend { display: grid; gap: 6px; padding: var(--space-3) var(--space-4); border-top: 1px solid var(--border); color: var(--text-muted); font-size: var(--text-sm); }
.legend span { display: flex; align-items: center; gap: var(--space-2); }
.legend i { flex: none; width: 16px; height: 10px; border: 1px solid var(--border-strong); border-radius: 3px; background: var(--bg-raised); }
.legend .president-swatch { border-color: var(--president); background: var(--president-fill); }
.legend .sensei-swatch { border-color: var(--sensei); background: var(--sensei-fill); }
.legend .both-swatch { border-color: var(--text-muted); background: linear-gradient(90deg, var(--president-fill) 50%, var(--sensei-fill) 50%); }
.legend .award-swatch { display: grid; place-items: center; border: 0; background: none; color: var(--award); font-style: normal; font-size: var(--text-sm); line-height: 1; }
.legend .edge-swatch { height: 0; border: 0; border-top: 2px solid var(--edge); border-radius: 0; background: none; }

/* Map */
main { display: flex; flex: 1; flex-direction: column; min-width: 0; }
.map-header { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); min-height: var(--toolbar-h); padding: var(--space-2) var(--space-4); border-bottom: 1px solid var(--border); background: var(--bg); }
h2 { font-size: var(--text-lg); font-weight: var(--weight-semibold); }
.view-actions { display: flex; gap: var(--space-2); }
#canvas { position: relative; flex: 1; overflow: hidden; overflow: clip; touch-action: none; user-select: none; cursor: grab; background: var(--bg-canvas); }
#canvas.dragging { cursor: grabbing; }
#canvas:focus-visible { outline-offset: -2px; }
#graph { position: absolute; top: 0; left: 0; overflow: visible; transform-origin: 0 0; }
.band { fill: var(--band); }
.class-label { fill: var(--text-faint); font-size: var(--text-sm); font-weight: var(--weight-semibold); }
.edge { fill: none; stroke: var(--edge); stroke-width: 1.5; }
.edge.active { stroke: var(--edge-active); stroke-width: 2; }
.edge.dim { stroke-opacity: .12; }
.node { cursor: pointer; }
.node .card { fill: var(--bg-raised); stroke: var(--border-strong); stroke-width: 1; rx: 8px; }
.node .name { fill: var(--text); font-size: 14px; font-weight: var(--weight-semibold); }
.node .role { font-size: var(--text-xs); font-weight: var(--weight-semibold); letter-spacing: .02em; }
.node.president .card { fill: var(--president-fill); stroke: var(--president-border); }
.node.president .role { fill: var(--president); }
.node.sensei .card { fill: var(--sensei-fill); stroke: var(--sensei-border); }
.node.sensei .role { fill: var(--sensei); }
.node.both .card { fill: url(#both); stroke: var(--text-faint); }
.node.both .role { fill: var(--text-muted); }
.node .award-mark { fill: var(--award); font-size: 14px; pointer-events: none; }
.both-president { stop-color: var(--president-fill); }
.both-sensei { stop-color: var(--sensei-fill); }
.node:hover .card { stroke: var(--text-muted); }
.node.related .card { stroke: var(--text-muted); stroke-width: 1.5; }
.node.selected .card { stroke: var(--text); stroke-width: 2; }
.node:focus-visible { outline: none; }
/* Fade dimmed nodes per shape; group opacity forces an offscreen layer per node. */
.node.dim .card { fill-opacity: .2; stroke-opacity: .2; }
.node.dim .name, .node.dim .role, .node.dim .award-mark { fill-opacity: .2; }
/* Keyboard focus is dashed so it reads differently from the solid selected outline. */
.node:focus-visible .card { stroke: var(--text); stroke-width: 2; stroke-dasharray: 6 4; stroke-opacity: 1; }
/* Zoomed out: names get larger and role colors get stronger so roles stay visible. */
#world.zoom-far .node .name { font-size: 16px; font-weight: var(--weight-semibold); }
#world.zoom-far .node .role { display: none; }
#world.zoom-far .node:is(.president, .sensei, .both) .name { transform: translateY(8px); }
#world.zoom-far .node.president .card { fill: var(--president-strong); }
#world.zoom-far .node.sensei .card { fill: var(--sensei-strong); }
#world.zoom-far .both-president { stop-color: var(--president-strong); }
#world.zoom-far .both-sensei { stop-color: var(--sensei-strong); }
#world.zoom-far .node:is(.president, .sensei, .both) .name { fill: var(--on-role); }

.map-tools, .minimap { position: absolute; bottom: var(--space-4); border: 1px solid var(--border); border-radius: var(--radius-md); background: var(--bg-raised); box-shadow: var(--shadow); }
.map-tools { left: var(--space-4); display: flex; align-items: center; gap: 2px; padding: var(--space-1); }
.map-tools button { min-width: 28px; height: 28px; padding: 0 var(--space-2); border: 0; }
#zoom-in, #zoom-out { font-size: 16px; }
#zoom-level { min-width: 52px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.map-tools .divider { width: 1px; height: 16px; margin: 0 var(--space-1); background: var(--border-strong); }
.minimap { right: var(--space-4); width: 176px; padding: var(--space-2); }
.minimap span { display: block; margin: 0 0 6px 2px; color: var(--text-faint); font-size: var(--text-xs); font-weight: var(--weight-medium); }
.minimap svg { display: block; width: 158px; height: 112px; border-radius: var(--radius-sm); background: var(--bg-canvas); cursor: crosshair; }
.mini-node { fill: var(--border-strong); }
.mini-window { fill: var(--bg-active); fill-opacity: .5; stroke: var(--text); stroke-width: 1.5; pointer-events: none; }
footer { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); height: var(--footer-h); padding: 0 var(--space-4); border-top: 1px solid var(--border); background: var(--bg); color: var(--text-faint); font-size: var(--text-xs); }
footer #status { color: var(--text-muted); }

/* Details sidebar */
#details { flex: 0 0 300px; overflow: auto; padding: var(--space-5) var(--space-4); border-left: 1px solid var(--border); background: var(--bg); }
#details h2 { margin: var(--space-1) 0 var(--space-2); font: var(--weight-medium) var(--text-xl)/1.2 var(--font-serif); }
.details-empty p { color: var(--text-muted); line-height: 1.6; }
/* Class and badges share the top line when they fit; otherwise both badges drop to the next line together.
   The name always gets its own full-width line. */
.details-heading { position: relative; display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--space-1) var(--space-2); }
.details-heading > div:not(.badges) { display: contents; }
.details-heading .eyebrow { white-space: nowrap; }
.details-heading h2 { order: 1; flex-basis: 100%; }
.details-close { display: none; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; }
.badge { display: inline-flex; white-space: nowrap; align-items: center; gap: 6px; height: 22px; padding: 0 var(--space-2); border: 1px solid var(--border-strong); border-radius: 999px; color: var(--text-muted); font-size: var(--text-xs); font-weight: var(--weight-medium); }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); }
.president-badge { color: var(--text); }
.president-badge::before { background: var(--president); }
.sensei-badge { color: var(--text); }
.sensei-badge::before { background: var(--sensei); }
.both-badge { color: var(--text); }
.both-badge::before { background: linear-gradient(90deg, var(--president) 50%, var(--sensei) 50%); }
.award-badge { color: var(--text); }
.award-badge::before { background: var(--award); }
.lineage-section { margin-top: var(--space-5); }
.lineage-section h3 { margin: 0 0 var(--space-1); }
/* Rows reach into the panel padding so their text lines up with the title and the hover fill has room. */
.lineage-section .member-link { width: calc(100% + 2 * var(--space-2)); margin: 0 calc(-1 * var(--space-2)); }
.lineage-empty { color: var(--text-muted); font-size: var(--text-sm); }

/* Empty state */
#empty { position: absolute; inset: 0; display: grid; place-items: center; overflow: auto; padding: var(--space-8); background: var(--bg-canvas); text-align: center; }
.empty-content { width: min(440px, 100%); }
.empty-content h2 { margin: var(--space-3) 0 var(--space-2); font: var(--weight-medium) var(--text-2xl)/1.2 var(--font-serif); }
.empty-content > p { color: var(--text-muted); line-height: 1.6; }
.primary-button { margin-top: var(--space-5); }
.privacy-note { margin-top: var(--space-3); color: var(--text-faint); font-size: var(--text-sm); }

/* Editor panel */
.editor-panel { position: fixed; z-index: 12; top: calc(var(--header-h) + var(--space-2)); right: var(--space-2); bottom: var(--space-2); display: flex; flex-direction: column; gap: var(--space-3); width: min(440px, calc(100vw - 2 * var(--space-2))); overflow: auto; padding: var(--space-5); border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--bg-raised); box-shadow: var(--shadow); }
.editor-panel__heading { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-3); }
.editor-panel__heading h2 { margin-top: var(--space-1); font: var(--weight-medium) var(--text-xl)/1.2 var(--font-serif); }
.editor-panel__heading button { margin: -6px -8px 0 0; }
.editor-panel__intro, .editor-unavailable { color: var(--text-muted); line-height: 1.6; }
.lineage-editor { display: grid; gap: var(--space-5); }
.lineage-editor > h2, .lineage-editor > .lineage-editor__notice { display: none; }
.lineage-editor__member-form, .lineage-editor__relationships, .lineage-editor__edge-list { display: grid; gap: var(--space-2); padding-top: var(--space-5); border-top: 1px solid var(--border); }
.lineage-editor input:not([type="checkbox"]), .lineage-editor select { width: 100%; min-width: 0; height: var(--control-h); padding: 0 var(--space-3); border: 1px solid var(--border-strong); border-radius: var(--radius-sm); outline: none; background: var(--bg-canvas); }
.lineage-editor input:not([type="checkbox"]):focus, .lineage-editor select:focus { border-color: var(--text-muted); box-shadow: 0 0 0 1px var(--text-muted); }
.lineage-editor input::placeholder { color: var(--text-faint); }
.lineage-editor input[type="checkbox"] { margin: 0; accent-color: var(--text); }
.lineage-editor__label, .lineage-editor h3 { margin: 0; }
.lineage-editor__label { margin-top: var(--space-1); }
.lineage-editor__roles { display: flex; gap: var(--space-4); }
.lineage-editor__roles label { display: flex; align-items: center; gap: 6px; }
.lineage-editor__results { display: grid; max-height: 160px; overflow: auto; gap: 2px; }
.lineage-editor__results:empty { display: none; }
.lineage-editor__results button { justify-content: flex-start; border: 0; font-weight: var(--weight-regular); }
.lineage-editor__results button[aria-selected="true"] { background: var(--bg-active); }
.lineage-editor__empty, .lineage-editor__chosen { color: var(--text-muted); font-size: var(--text-sm); }
.lineage-editor__edge-list ul { display: grid; max-height: 240px; overflow: auto; margin: 0; padding: 0; list-style: none; }
.lineage-editor__edge-list li { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); padding: 6px 0; border-bottom: 1px solid var(--border); font-size: var(--text-sm); }
.lineage-editor__edge-list li:last-child { border-bottom: 0; }
.lineage-editor__edge-list li button { height: 26px; padding: 0 var(--space-2); font-size: var(--text-sm); }
.lineage-editor__export { width: 100%; }
.lineage-editor__status { color: var(--text-muted); font-size: var(--text-sm); }
.lineage-editor__status[data-state="error"] { color: var(--sensei); }

/* Shown only on phones, where it opens the class list as a drawer. */
.browse-toggle { display: none; }

/* Narrower screens: details become an overlay on the map. */
@media (max-width: 1100px) {
  .browse { flex-basis: 224px; }
  h1 { font-size: 18px; }
  #details { display: none; position: absolute; z-index: 4; top: var(--map-header-h, var(--toolbar-h)); right: 0; bottom: var(--footer-h); width: 320px; border-left: 1px solid var(--border); box-shadow: var(--shadow); }
  #details.open { display: block; }
  #details .details-close { display: inline-flex; position: absolute; top: -6px; right: -8px; }
  #details .badges { margin-right: 36px; }
  footer span:last-child { display: none; }
}
/* Two-row header: brand on its own row, then search and buttons in page order. */
@media (max-width: 720px) {
  :root { --header-h: 92px; }
  header { flex-wrap: wrap; align-content: center; gap: var(--space-2); padding: var(--space-2) var(--space-3); }
  .brand { flex-basis: 100%; }
  .search-wrap { flex: 1 1 100px; max-width: none; margin-left: 0; }
}
/* Phones: the map takes the full width and the class list slides in over it. */
@media (max-width: 700px) {
  header button, .import-button { height: 32px; padding: 0 var(--space-2); font-size: var(--text-sm); }
  .search-wrap input { height: 32px; }
  .theme-toggle { width: 32px; padding: 0; }
  .theme-toggle span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }
  .browse-toggle { display: inline-flex; }
  .browse { position: absolute; z-index: 6; top: 0; bottom: 0; left: 0; width: min(280px, 85vw); box-shadow: var(--shadow); transform: translateX(-100%); visibility: hidden; transition: transform 160ms ease, visibility 160ms; }
  .browse.open { transform: none; visibility: visible; }
  .map-header { flex-wrap: wrap; padding: var(--space-2) var(--space-3); }
  .view-actions { flex-wrap: wrap; }
  .view-actions button { height: 32px; padding: 0 var(--space-2); font-size: var(--text-sm); }
  .map-tools { bottom: var(--space-2); left: var(--space-2); }
  .minimap { display: none; }
  #details { width: min(320px, calc(100vw - 40px)); padding: var(--space-4) var(--space-3); }
  footer { padding: 0 var(--space-3); }
  #status { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
}
/* Short screens such as landscape phones: scroll the whole sidebar and drop the minimap. */
@media (max-height: 560px) {
  .browse { overflow-y: auto; }
  #classes { flex: none; overflow: visible; }
  .minimap { display: none; }
}
/* Touch screens get larger tap targets. */
@media (pointer: coarse) {
  button, .import-button, .search-wrap input { min-height: 40px; }
  .result, .member-link, #classes button, .view-buttons button { min-height: 40px; }
  .map-tools button { min-width: 40px; height: 40px; }
  .text-button { min-height: 32px; padding: 0 var(--space-2); }
  .theme-toggle { min-width: 40px; }
  .lineage-editor input:not([type="checkbox"]), .lineage-editor select { height: 40px; }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; } }
