/* ===========================================================================
   Basic Mathematics — the course world (index.html only)
   The contents page's hub: the world stands first in <main>, above the hero and
   the chapter list. assets/map3d.js reveals div.map3d (with a "Loading the map"
   panel that keeps its place) only when the device is to get 3D, and stamps
   [data-map="3d"] on the course index once the world is drawn; until then nothing
   here applies and the list looks exactly as site.css draws it. Tokens only: every
   colour comes from src/styles/tokens.css, and the canvas reads the same tokens
   at run time (src/world/materials.ts).
   =========================================================================== */

/* first in <main>, the world starts where the hero did, at main's own padding */
.map3d { margin-top: 1.75rem; --map3d-row: 3.25rem; }
.map3d:first-child { margin-top: 0; }
/* the contents page's head (the call to action) comes first; the world sits close under it */
.hub-head + .map3d { margin-top: 1rem; }
@media (max-width: 600px) { .hub-head + .map3d { margin-top: .8rem; } }
/* the hero follows the world, closer than it stands at the top of a page */
.map3d:not([hidden]) + .hero, html[data-world] .map3d + .hero { padding-top: 2.25rem; }

/* The world's place, kept from the first paint. The module that draws the world runs
   after the page is painted (late on a slow network), and the world stands above the
   hero, so a box that appeared only then would drop the hero the reader is already
   looking at by the height of the world. Where src/boot.js expects 3D it stamps
   html[data-world] before the first paint, and until assets/map3d.js fills the box (or
   gives it up, and the attribute with it) the box holds a row the height of the Part
   buttons' row and a panel the size of the stage. The buttons' rows are a fixed height
   (--map3d-row) for that reason. tools/game/map.test.js loads the page with its module
   held back and checks the hero does not move. */
html[data-world] .map3d[hidden] { display: block !important; }
/* the row of Part buttons and the tools row (2.75rem and .5rem of margin, see .map3d-tools) */
html[data-world] .map3d:empty::before { content: ""; display: block; height: calc(var(--map3d-row) + 3.25rem); margin-bottom: .9rem; }
html[data-world] .map3d:empty::after {
  content: ""; display: block; width: 100%; aspect-ratio: 16 / 9; max-height: min(66vh, 620px);
  border-radius: var(--radius); background: var(--surface-2);
}

/* wide, the stage is narrower by the panel's column (18rem and a 1rem gap), and so is its placeholder */
@media (min-width: 1000px) { html[data-world] .map3d:empty::after { width: calc(100% - 19rem); } }

/* ------------------------------------------------ the four Part buttons -- */

.map3d-parts { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); grid-auto-rows: minmax(var(--map3d-row), auto); gap: .6rem; margin: 0 0 .9rem; }
.map3d-part {
  --edge: var(--part-deep);
  display: flex; flex-direction: column; gap: .1rem; min-width: 0;
  font: 600 .84rem/1.2 var(--sans); color: var(--text); text-align: left;
  background: var(--surface); border: 2px solid var(--part); border-radius: var(--radius-xs);
  padding: .45rem .7rem .5rem; box-shadow: 0 3px 0 var(--edge); cursor: pointer;
}
.map3d-part b {
  color: var(--part); font-weight: 750; font-size: .68rem; letter-spacing: .08em; text-transform: uppercase;
}
/* the separator keeps the accessible name "Part I · The Foundry" while the tile shows two lines */
.map3d-sep { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
/* not the chosen one: it keeps its solid --part under the pointer, as its --on-part text needs */
.map3d-part:hover:not(:disabled):not([data-on]) { background: var(--part-soft); }
/* until the world is drawn the buttons wait with it (assets/map3d.js skeleton()) */
.map3d-part:disabled { cursor: progress; }
.map3d-part[data-on] { background: var(--part); color: var(--on-part); }
.map3d-part[data-on] b { color: inherit; }
.map3d-part:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 0 0 var(--edge); }
.map3d-part:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: var(--radius-xs); }
@media (prefers-reduced-motion: no-preference) {
  html:not([data-calm]) .map3d-part { transition: transform var(--t-1), box-shadow var(--t-1), background var(--t-2); }
}

/* ------------------------------------------------------------ the stage -- */

.map3d-stage {
  position: relative; width: 100%; aspect-ratio: 16 / 9; max-height: min(66vh, 620px);
  border-radius: var(--radius); overflow: hidden;
}
/* a flat placeholder until the first frame is drawn; then the canvas, which paints its
   own sky, covers it */
.map3d-stage:focus { outline: none; }
.map3d-stage:focus-visible { outline: none; }
/* the canvas is absolutely positioned and paints over an outline, so the ring is an overlay above it */
.map3d-stage:focus-visible::after {
  content: ""; position: absolute; inset: 0; z-index: 1; pointer-events: none; border-radius: inherit;
  box-shadow: inset 0 0 0 var(--focus-width, 3px) var(--focus);
}
.map3d-keyhint {
  position: absolute; right: .5rem; bottom: .5rem; margin: 0; padding: .3rem .5rem; pointer-events: none;
  background: var(--surface); color: var(--muted); border-radius: var(--radius-xs); font: 500 .75rem/1.3 var(--sans);
}
@media (max-width: 640px) { .map3d-keyhint { display: none; } }
.map3d-stage[data-state="loading"] { background: var(--surface-2); }
.map3d-wait {
  position: absolute; inset: 0; display: grid; place-items: center; margin: 0;
  font: 500 .9rem var(--sans); color: var(--muted);
}
.map3d-stage[data-state="ready"] .map3d-wait { display: none; }
.map3d-canvas {
  position: absolute; inset: 0; display: block; width: 100%; height: 100%;
  /* vertical swipes scroll the page; horizontal drags slide along the path */
  touch-action: pan-y;
  -webkit-tap-highlight-color: transparent;
}
.map3d[data-drag] .map3d-canvas { cursor: grabbing; }

/* chapter names over the islands; aria-hidden, the list carries the text. On solid
   paper, never see-through: the world behind them is a lit scene, and the pair
   measured is --text on --surface (tools/contrast-pairs.json) */
.map3d-labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.map3d-label {
  position: absolute; left: 0; top: 0; margin-top: -6px; max-width: 70%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font: 600 13px/1.3 var(--sans); color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: var(--radius-xs);
  padding: .2rem .55rem;
}
.map3d-label > span { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.map3d-label-meta { font-weight: 500; font-size: .8rem; color: var(--muted); }
.map3d-label-goal {
  max-width: 18rem; font-weight: 400; font-size: .8rem; line-height: 1.35; white-space: normal !important;
  display: -webkit-box !important; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2;
}
.map3d-label[data-kind="hot"]:has(.map3d-label-goal) { white-space: normal; width: max-content; }
.map3d-label[data-kind="current"] { border-color: var(--part); box-shadow: 0 2px 0 var(--part); }

/* what the marks on the islands mean; aria-hidden (the list carries the same text,
   visually hidden), never over an island's click: pointer-events none */
.map3d-legend {
  position: absolute; left: .5rem; bottom: .5rem; margin: 0; padding: .3rem .5rem; list-style: none;
  pointer-events: none; display: grid; gap: .15rem;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--radius-xs);
  font: 500 .72rem/1.3 var(--sans);
}
.map3d-legend li { display: flex; align-items: center; gap: .4rem; }
.map3d-legend i { flex: none; display: inline-block; width: .6rem; height: .6rem; box-sizing: border-box; }
/* each glyph takes the token the world draws that mark with (src/world/marks.ts): the ring and the
   flag in --ok, the boss in the Part's --part-deep, the beacon in the Part's --region-glow; the
   stage carries the shown Part (data-part), so the Part tokens resolve to its hues */
.map3d-legend [data-mark="ring"] i { border: 2px solid var(--ok); border-radius: 50%; }
.map3d-legend [data-mark="boss"] i { background: var(--part-deep); border-radius: 2px; }
.map3d-legend [data-mark="beacon"] i { width: .25rem; height: .8rem; background: var(--region-glow); border-radius: 2px; margin: 0 .175rem; }
.map3d-legend [data-mark="flag"] i { background: var(--ok); clip-path: polygon(0 0, 100% 30%, 0 60%); height: .8rem; }
html[data-calm] .map3d-legend [data-mark="boss"], html[data-calm] .map3d-legend-sr [data-mark="boss"] { display: none; }
@media (max-width: 640px) { .map3d-legend { display: none; } }

/* ------------------------------------- the tools row and the selection panel -- */

.map3d-tools { display: flex; justify-content: flex-end; margin: 0 0 .5rem; }
.map3d-tools .btn { min-height: 2.75rem; }
.map3d-panel {
  margin: .9rem 0 0; padding: .8rem 1rem; display: grid; gap: .45rem; align-content: start;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-tile-sm);
}
.map3d-panel[hidden] { display: none; }
.map3d-panel p { margin: 0; }
.map3d-panel-title { font: 700 .8rem/1.3 var(--display, var(--sans)); letter-spacing: .08em; text-transform: uppercase; color: var(--text); }
.map3d-panel-state { font: 600 .9rem/1.4 var(--sans); color: var(--muted); }
.map3d-panel-goal, .map3d-panel-builds, .map3d-panel-due { font: 400 .9rem/1.45 var(--sans); }
.map3d-panel a:not(.btn) { color: var(--accent); }
.map3d-seg { white-space: nowrap; }
.map3d-panel-req { display: block; }
/* the column's prompt while nothing is selected: only where the column is kept (below, ≥1000 px) */
.map3d-panel-empty { display: none; margin: 0; font: 400 .9rem/1.45 var(--sans); color: var(--muted); }
.map3d-panel-actions { display: flex; flex-wrap: wrap; gap: .6rem; margin-top: .2rem !important; }
.map3d-panel-actions .btn { min-height: 2.75rem; }
/* narrow: the tools row stands above the stage though it follows it in the markup (the stage is first in the tab order) */
@media (max-width: 999.98px) {
  .map3d:not([hidden]) { display: flex; flex-direction: column; }
  .map3d > .map3d-parts { order: 1; }
  .map3d > .map3d-tools { order: 2; }
  .map3d > .map3d-stage { order: 3; }
  .map3d > .map3d-panel { order: 4; }
}
/* wide: the panel is a column beside the stage (the stage keeps its 16:9); the column is kept while the panel is
   hidden, so the stage does not change width when a chapter is first hovered or selected */
@media (min-width: 1000px) {
  .map3d:has(.map3d-panel) { display: grid; grid-template-columns: minmax(0, 1fr) 18rem; column-gap: 1rem; align-items: start; }
  .map3d:has(.map3d-panel) > .map3d-parts { grid-column: 1 / -1; grid-row: 1; }
  .map3d:has(.map3d-panel) > .map3d-tools { grid-column: 1 / -1; grid-row: 2; }
  .map3d:has(.map3d-panel) > .map3d-stage { grid-column: 1; grid-row: 3; }
  .map3d:has(.map3d-panel) > .map3d-panel { grid-column: 2; grid-row: 3; margin: 0; }
  .map3d-panel[hidden] + .map3d-panel-empty { display: block; grid-column: 2; grid-row: 3; }
}

/* The List / Map control's reason line (src/ui/hub-mode.ts). The rule is here because this sheet loads
   on the contents page and site.css is not this card's file. */
.hub-mode:has(.hub-mode-why) { flex-wrap: wrap; }
.hub-mode-why { flex: 1 1 100%; margin: 0; font: 500 .86rem/1.4 var(--sans); color: var(--muted); }

.map3d-probe { position: absolute; width: 0; height: 0; overflow: hidden; visibility: hidden; }

/* ------------------------------------- the list, compact, as a quest log -- */

[data-map="3d"] .part { margin-top: 2.2rem; }
[data-map="3d"] .part:first-child { margin-top: 0; }
[data-map="3d"] .part-head { gap: .6rem; padding-bottom: .45rem; }
[data-map="3d"] .part-head .roman { font-size: 1.35rem; }
[data-map="3d"] .part-head h2 { font-size: 1.05rem; }
[data-map="3d"] .part-blurb { display: none; }
[data-map="3d"] .path { gap: .4rem; margin-top: .8rem; }
[data-map="3d"] .path::before { left: 17px; margin-left: 0; top: 18px; bottom: 18px; width: 2px; }
[data-map="3d"] .stop-link { grid-template-columns: 36px minmax(0, 1fr); gap: .7rem; align-items: center; }
[data-map="3d"] .stop:nth-child(n) .stop-node { grid-column: 1; grid-row: 1; }
[data-map="3d"] .stop:nth-child(n) .stop-card { grid-column: 2; grid-row: 1; }
[data-map="3d"] .stop + .stop { margin-top: 0; }
[data-map="3d"] .stop-node { width: 36px; height: 36px; margin-top: 0; box-shadow: 0 0 0 4px var(--bg); }
[data-map="3d"] .stop-node > span { width: 28px; height: 28px; font-size: .78rem; }
[data-map="3d"] .stop[data-state="current"] .stop-node { box-shadow: 0 0 0 4px var(--bg), 0 0 0 6px var(--part-soft); animation: none; }
[data-map="3d"] .stop-card { gap: .15rem; padding: .5rem .8rem; border-radius: var(--radius-sm); box-shadow: var(--shadow-tile-sm); }
[data-map="3d"] .stop-card p { display: none; }
[data-map="3d"] .stop-card h3 { font-size: 1rem; }
[data-map="3d"] .stop-card .label { font-size: .64rem; }
[data-map="3d"] .stop-card .meta { margin-top: 0; font-size: .74rem; }
[data-map="3d"] .stop:nth-child(n) .stop-hub { margin: .2rem 0 0 calc(36px + .7rem); width: auto; padding: 0 .8rem; font-size: .8rem; }
[data-map="3d"] .stop-link:hover .stop-card { transform: none; }
[data-map="3d"] .stop-link:active .stop-card { transform: translateY(2px); box-shadow: 0 0 0 var(--tile-edge); }
[data-map="3d"] .stop-link:hover .stop-node { transform: none; }
/* the chapter selected on the map, or focused here */
[data-map="3d"] .stop[data-map-hot] .stop-card { border-color: var(--part); box-shadow: inset 4px 0 0 var(--part), var(--shadow-tile-sm); }
[data-map="3d"] .stop-link:focus-visible { outline: none; }
[data-map="3d"] .stop-link:focus-visible .stop-card { outline: 3px solid var(--focus); outline-offset: 2px; }

/* tablets and wider: the world spans the page above, and the four Parts of the list
   sit two by two below the hero */
@media (min-width: 720px) {
  [data-map="3d"] { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 2.2rem 2rem; margin-top: 2rem; }
  [data-map="3d"] .part, [data-map="3d"] .part:first-child { margin-top: 0; }
}

/* phones: a taller stage, and the buttons two by two */
@media (max-width: 600px) {
  .map3d { margin-top: 1.25rem; }
  .map3d:not([hidden]) + .hero, html[data-world] .map3d + .hero { padding-top: 1.75rem; }
  .map3d-parts { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .5rem; }
  .map3d-part { font-size: .8rem; padding: .4rem .6rem .45rem; }
  .map3d-stage, html[data-world] .map3d:empty::after { aspect-ratio: 4 / 5; }
  html[data-world] .map3d:empty::before { height: calc(2 * var(--map3d-row) + .5rem + 3.25rem); }
}

@media print {
  .map3d { display: none !important; }
}
