@font-face {
  font-family: "Noto Sans";
  src: url("fonts/NotoSans-Regular.woff2") format("woff2");
}

:root {
  --bar: #f1f3f5;
  --line: #ced4da;
  --text: #212529;
  --muted: #6c757d;
  --canvas: #ffffff;
}

html, body { height: 100%; margin: 0; }
body { font-family: "Noto Sans", "Segoe UI", sans-serif; font-size: 14px; color: var(--text); overflow: hidden; }

/* The whole window is the editor: top bar, canvas and side panel, status bar. Only the side panel can scroll. */
.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 240px;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas: "top top" "canvas side" "status status";
  height: 100vh;
}

.topbar, .statusbar { display: flex; align-items: center; gap: 12px; padding: 0 12px; background: var(--bar); }
.topbar { grid-area: top; height: 40px; border-bottom: 1px solid var(--line); overflow: hidden; }
.statusbar { grid-area: status; height: 26px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); }
.spacer { flex: 1; }
.appname { font-weight: bold; }
.toolbar { display: flex; align-items: center; gap: 4px; margin-left: 8px; min-width: 0; overflow-x: auto; scrollbar-width: none; }   /* in a very narrow window the toolbar scrolls sideways instead of widening the page */
.appname { white-space: nowrap; }
@media (max-width: 1100px) { .appname { display: none; } }
.separator { width: 1px; height: 20px; margin: 0 6px; background: var(--line); }
.segmented { display: inline-flex; gap: 2px; padding: 2px; border: 1px solid var(--line); border-radius: 6px; background: #e9ecef; }
.hints { display: flex; gap: 16px; min-width: 0; overflow: hidden; white-space: nowrap; }   /* the Draw mode list is long: a narrow window clips it instead of widening the page */

.canvasarea { grid-area: canvas; position: relative; background: var(--canvas); overflow: hidden; }
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; outline: none; touch-action: none; }

.sidepanel { grid-area: side; border-left: 1px solid var(--line); background: var(--bar); overflow-y: auto; }
.panel { padding: 10px 12px; border-bottom: 1px solid var(--line); }
.panel h2 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.panel .empty { margin: 0; color: var(--muted); }

/* The properties form: a label above each control, so the narrow side panel never runs out of room. */
.props { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.prop { display: flex; flex-direction: column; gap: 2px; }
.prop > label { font-size: 12px; color: var(--muted); }
.ctl { font: inherit; }
.ctl.text { width: 100%; box-sizing: border-box; resize: vertical; padding: 4px 6px; border: 1px solid var(--line); border-radius: 4px; }
.number-wrap { display: flex; align-items: center; gap: 6px; }
.ctl.number { width: 84px; padding: 3px 6px; border: 1px solid var(--line); border-radius: 4px; }
.unit { color: var(--muted); font-size: 12px; }
.slider-wrap { display: flex; align-items: center; gap: 8px; }
.ctl.range { flex: 1; min-width: 0; }
.readout { width: 38px; text-align: right; font-variant-numeric: tabular-nums; }
.ctl.colour { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.swatch { width: 18px; height: 18px; padding: 0; border: 1px solid #adb5bd; border-radius: 3px; cursor: pointer; }
.swatch.current { outline: 2px solid #228be6; outline-offset: 1px; }
.custom { width: 28px; height: 22px; padding: 0; border: 1px solid #adb5bd; border-radius: 3px; background: none; cursor: pointer; }
.segmented { display: inline-flex; }
.seg { font: inherit; padding: 3px 10px; border: 1px solid var(--line); background: #fff; cursor: pointer; }
.seg + .seg { margin-left: -1px; }
.seg:first-child { border-radius: 4px 0 0 4px; }
.seg:last-child { border-radius: 0 4px 4px 0; }
.seg.current { background: #e7f0fb; border-color: #8fb4e8; position: relative; }
.order-row { flex-direction: row; gap: 6px; }
.order { flex: 1; font: inherit; padding: 4px 6px; border: 1px solid var(--line); border-radius: 4px; background: #fff; cursor: pointer; }

.is-hidden { display: none; }

.tool {
  display: inline-flex; align-items: center; justify-content: center;
  width: 30px; height: 28px; padding: 0;
  border: 1px solid transparent; border-radius: 4px; background: transparent; color: var(--text); font-size: 14px; cursor: pointer;
}
.tool.wide { width: auto; padding: 0 10px; gap: 6px; }
.tool:hover { background: #e2e6ea; }
.tool.active { background: #d0ebff; border-color: #74c0fc; }
.tool:focus-visible { outline: 2px solid #339af0; outline-offset: 1px; }

#canvas[data-mode=draw] { cursor: crosshair; }
#canvas[data-mode=view] .node { display: none; }   /* the circles are an editing aid */
#canvas.can-pan { cursor: grab; }
#canvas.panning { cursor: grabbing; }
/* The readouts never wrap and never give up room: the hints (which clip) and the message (which is cut short) are what shrink. */
.position, .zoom { flex: none; white-space: nowrap; text-align: right; font-variant-numeric: tabular-nums; }
.position { min-width: 12em; }
.zoom { min-width: 4.5em; }

/* The prompt dialog */
dialog.prompt { border: 1px solid var(--line); border-radius: 8px; padding: 18px 20px; max-width: 420px; font: inherit; color: var(--text); box-shadow: 0 8px 28px rgba(0, 0, 0, 0.25); }
dialog.prompt::backdrop { background: rgba(0, 0, 0, 0.25); }
dialog.prompt p { margin: 0 0 16px; }
dialog.prompt .buttons { display: flex; justify-content: flex-end; gap: 8px; }
.point-chooser { position: absolute; z-index: 5; display: flex; flex-direction: column; gap: 2px; padding: 4px; background: #fff; border: 1px solid var(--line); border-radius: 6px; box-shadow: 0 2px 8px rgba(0, 0, 0, .18); pointer-events: none; }
.point-chooser.open { pointer-events: auto; }
.point-chooser .choice { font: inherit; text-align: left; padding: 3px 10px; border: 1px solid transparent; border-radius: 4px; background: transparent; color: var(--text); cursor: pointer; white-space: pre; }
.point-chooser .choice.active { background: #e7f0fb; border-color: #8fb4e8; }
.edge.fixed, .node.fixed { opacity: .92; }
#printPages { display: none; }
/* Print: every sheet is an SVG exactly the paper's size (the page rule is added with the sheets), one per printed page. */
@media print {
  html, body { background: #fff; margin: 0; height: auto; overflow: visible; }
  body > :not(#printPages) { display: none !important; }
  #printPages { display: block; }
  #printPages svg { display: block; break-after: page; }
  #printPages svg:last-child { break-after: auto; }
}
/* The Print dialog: choices on the left, the sheets on the right. */
dialog.print { max-width: none; }
dialog.print .print-body { display: flex; gap: 16px; align-items: flex-start; }
dialog.print .print-controls { min-width: 240px; }
dialog.print .print-controls select { width: 100%; }
dialog.print .print-count { margin: 4px 0 12px; color: var(--muted); }
/* A fixed window, tall enough for a portrait A4 sheet: more sheets or a change of orientation scroll instead of resizing the dialog. */
dialog.print .print-sheets { box-sizing: border-box; flex: none; width: 380px; height: min(560px, 75vh); overflow-y: auto; padding: 8px; background: #e9ecef; border-radius: 4px; }
dialog.print .sheet { margin-bottom: 8px; background: #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, .3); }
dialog.print .sheet svg { display: block; width: 100%; height: auto; }
dialog.print .print-more { margin: 0; text-align: center; color: var(--muted); }
dialog.options { min-width: 340px; }
dialog.options fieldset { margin: 0 0 10px; border: 1px solid var(--line); border-radius: 4px; }
dialog.options legend { padding: 0 4px; color: #495057; font-size: 12px; }
dialog.options .option { display: block; padding: 2px 0; }
dialog.options input[type=number] { width: 64px; }
dialog.options h2 { margin: 0 0 8px; font-size: 16px; }
dialog.options .buttons { display: flex; justify-content: flex-end; gap: 8px; }
dialog.parts { max-width: 560px; max-height: 80vh; overflow-y: auto; }
dialog.parts table { width: 100%; border-collapse: collapse; margin: 8px 0 12px; }
dialog.parts th, dialog.parts td { padding: 3px 8px; text-align: left; }
dialog.parts thead th { border-bottom: 1px solid var(--line); color: #495057; font-size: 12px; }
dialog.parts .parts-section th { padding-top: 10px; font-size: 12px; color: #495057; background: #f1f3f5; }
dialog.parts .count { text-align: right; width: 56px; }
dialog.parts .parts-total td { border-top: 2px solid var(--line); font-weight: 600; }
dialog.parts .parts-warnings { margin: 0 0 12px; padding-left: 18px; color: #c92a2a; }
dialog.parts .parts-saved { margin: 0 0 8px; color: var(--muted); min-height: 1em; }
dialog.parts .parts-empty { color: var(--muted); }
dialog.prompt button { font: inherit; padding: 5px 16px; border: 1px solid var(--line); border-radius: 4px; background: #fff; cursor: pointer; }
dialog.prompt button:first-child { background: #228be6; border-color: #228be6; color: #fff; }
dialog.prompt button:focus-visible { outline: 2px solid #339af0; outline-offset: 2px; }

/* The plan. Lines keep their on-screen width when zooming; the node dots are in plan units, so they stay in proportion to the track spacing. */
.edge { fill: none; stroke: #212529; stroke-width: 2px; stroke-linecap: round; vector-effect: non-scaling-stroke; }
.edge.ghost { opacity: 0.25; }
/* Sleepers: the dash pattern and width come from the edge itself (geometry/sleepers.js); they scale with the plan and fade out when zoomed far out. */
.tie { fill: none; stroke: #495057; stroke-linecap: butt; }
.tie.ghost { opacity: 0.25; }
#world.no-sleepers #ties { display: none; }
.node { fill: #fff; stroke: #212529; stroke-width: 1.5px; vector-effect: non-scaling-stroke; }
.preview-edge { fill: none; stroke: #1c7ed6; stroke-width: 2px; stroke-linecap: round; vector-effect: non-scaling-stroke; }
.preview-edge.invalid { stroke: #e03131; stroke-dasharray: 6 4; }
.message { color: #c92a2a; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.snap-target { fill: none; stroke: #1c7ed6; stroke-width: 1.5px; vector-effect: non-scaling-stroke; }
.gap-label { fill: #1864ab; paint-order: stroke; stroke: #fff; stroke-width: 3px; stroke-linejoin: round; }
.edge.neighbour { stroke: #1c7ed6; }
.edge.selected { stroke: #e8590c; stroke-width: 3px; }
.node.selected { stroke: #e8590c; stroke-width: 3px; fill: #ffe8cc; }
.label text { user-select: none; }
.label .label-box { fill: none; stroke: none; }
.label text { white-space: pre; }   /* keep repeated spaces */
.label.selected .label-box { stroke: #e8590c; stroke-width: 1.5px; stroke-dasharray: 5 3; vector-effect: non-scaling-stroke; }
.label.ghost { opacity: 0.25; }
.ghost-item { opacity: 0.7; }
.ghost-item .label-box { stroke: #1c7ed6; stroke-width: 1px; stroke-dasharray: 4 3; vector-effect: non-scaling-stroke; }
.board.ghost { opacity: 0.25; }
.select-outline { fill: none; stroke: #e8590c; stroke-width: 1px; stroke-dasharray: 5 3; vector-effect: non-scaling-stroke; }
.handle { fill: #fff; stroke: #e8590c; stroke-width: 1.5px; vector-effect: non-scaling-stroke; }
.handle.rotate { fill: #ffe8cc; }
.handle-stem { stroke: #e8590c; stroke-width: 1px; vector-effect: non-scaling-stroke; }
.marquee { fill: rgba(28, 126, 214, .08); stroke: #1c7ed6; stroke-width: 1px; stroke-dasharray: 4 3; vector-effect: non-scaling-stroke; }
.preview-node { fill: #d0ebff; stroke: #1c7ed6; stroke-width: 1.5px; vector-effect: non-scaling-stroke; }

/* The Pieces panel */
.pieces { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
.piece { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 2px 3px; border: 1px solid var(--line); border-radius: 5px; background: #fff; color: var(--text); font: inherit; font-size: 11px; line-height: 1.1; text-align: center; cursor: pointer; }
.piece:hover { background: #f1f3f5; }
.piece.active { background: #e7f0fb; border-color: #8fb4e8; }
.piece svg { display: block; }
.piece svg path { vector-effect: non-scaling-stroke; }
#canvas[data-tool]:not([data-tool=track]) { cursor: copy; }
.radius { display: block; margin-top: 8px; font-size: 12px; color: var(--muted); }

/* Palette groups: General sits on top; each family of pieces folds */
.pieces.general { margin-bottom: 4px; }
.group { margin-top: 8px; }
.group-head { display: flex; align-items: center; gap: 6px; padding: 3px 2px; font-size: 12px; font-weight: 600; color: var(--muted); cursor: pointer; user-select: none; }
.group-head .count { font-weight: 400; font-size: 11px; background: #e9ecef; border-radius: 8px; padding: 0 6px; }
.group > .pieces { margin-top: 4px; }

/* The Help menu (top right) and its dialogs */
.helpbar { display: inline-flex; }
.help-menu { position: fixed; inset: auto; margin: 0; min-width: 170px; padding: 4px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--text); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2); }
.help-menu:popover-open { display: flex; flex-direction: column; }
.help-item { display: block; padding: 6px 12px; border: 0; border-radius: 4px; background: transparent; color: inherit; font: inherit; text-align: left; text-decoration: none; cursor: pointer; }
.help-item:hover, .help-item:focus-visible { background: #e7f5ff; outline: none; }
dialog.help { width: min(760px, 92vw); max-width: none; }
dialog.help .help-body { display: flex; gap: 16px; min-height: 0; }
dialog.help .help-topics { flex: none; width: 190px; display: flex; flex-direction: column; gap: 2px; }
dialog.help .help-topics button { padding: 6px 10px; border: 0; border-radius: 4px; background: transparent; color: inherit; font: inherit; text-align: left; cursor: pointer; }
dialog.help .help-topics button:hover { background: #f1f3f5; }
dialog.help .help-topics button.active { background: #d0ebff; }
dialog.help[open] { display: flex; flex-direction: column; }
dialog.help .help-body { flex: 1 1 auto; min-height: 0; }
dialog.help .help-content { flex: 1; min-width: 0; max-height: 60vh; overflow-y: auto; line-height: 1.45; }
/* The FAQ: one fixed size (the tallest the dialog gets), with Close always at the bottom right. */
dialog.help.help-fixed { height: calc(60vh + 110px); max-height: 94vh; }
dialog.help.help-fixed .help-content { max-height: none; }
dialog.help .help-content h3 { margin: 0 0 8px; font-size: 15px; }
dialog.help .help-content h4 { margin: 14px 0 4px; font-size: 13px; }
dialog.help .help-content p, dialog.help .help-content ul, dialog.help .help-content ol { margin: 0 0 10px; }
dialog.help .help-content li { margin-bottom: 3px; }
dialog.help kbd { padding: 1px 5px; border: 1px solid #adb5bd; border-bottom-width: 2px; border-radius: 4px; background: #f8f9fa; font: 12px/1.3 inherit; white-space: nowrap; }
dialog.help .about { text-align: center; padding: 8px 24px; }
dialog.help .about p { margin: 0 0 10px; }
dialog.help .about-name { font-size: 22px; font-weight: bold; }
dialog.help .about-version { color: var(--muted); }
dialog.help .about-small { font-size: 12px; color: var(--muted); }
dialog.help .buttons { flex: none; margin-top: 12px; justify-content: flex-end; }

/* The page shown instead of the editor where it cannot run (a phone, a tablet, Firefox, Safari) */
.unsupported { box-sizing: border-box; max-width: 560px; margin: 12vh auto 0; padding: 0 24px; text-align: center; color: var(--text); line-height: 1.5; }
.unsupported-name { margin: 0 0 16px; font-size: 18px; font-weight: bold; color: var(--muted); }
.unsupported h1 { margin: 0 0 12px; font-size: 22px; line-height: 1.3; }
.unsupported p { margin: 0 0 12px; }
.unsupported-links a { color: #1971c2; }
