:root {
  --paper: #ece9e1;
  --panel: #f8f6f1;
  --card: #ffffff;
  --ink: #1c1e21;
  --muted: #666b73;
  --line: #d8d3c7;
  --accent: #d9291c;
  --accent-ink: #ffffff;
  --ok: #1d7f47;
  --warn: #b86500;
  --asphalt: #2b2e33;
  --rail: #b9bec6;
  --feed: #ffc400;
  --display: "Bahnschrift", "DIN Alternate", "Arial Narrow", "Segoe UI", sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  flex-direction: column;
  font: 14px/1.45 "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: var(--paper);
}

button, input, select { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover:not(:disabled) { border-color: var(--ink); }
button:disabled { opacity: .35; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.primary:hover:not(:disabled) { background: #b81f14; border-color: #b81f14; }
button.danger:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Kopfzeile */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 16px;
  background: var(--ink);
  color: #f3f0e8;
  border-bottom: 3px solid var(--accent);
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.flag {
  align-self: center;
  width: 28px; height: 20px; flex: none;
  background:
    conic-gradient(#f3f0e8 25%, transparent 0 50%, #f3f0e8 0 75%, transparent 0) 0 0 / 10px 10px;
  border-radius: 2px;
}
.brand h1 {
  margin: 0;
  font: italic 700 24px/1 var(--display);
  letter-spacing: .04em;
  text-transform: uppercase;
}
.brand .sub { color: #b9b5aa; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; gap: 6px; }
.top-actions button { background: transparent; border-color: #555a62; color: inherit; }
.top-actions button:hover { border-color: #f3f0e8; }

/* Grundraster */
.app {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 372px minmax(0, 1fr) 310px;
}
.panel { background: var(--panel); overflow-y: auto; padding: 4px 14px 20px; }
.panel.left { border-right: 1px solid var(--line); }
.panel.right { border-left: 1px solid var(--line); }
.panel section { padding: 12px 0; border-bottom: 1px solid var(--line); }
.panel section:last-child { border-bottom: 0; }
.panel section:empty { display: none; }
h2 {
  margin: 0 0 8px;
  font: 700 13px/1.2 var(--display);
  letter-spacing: .09em;
  text-transform: uppercase;
}
h3 {
  margin: 12px 0 4px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
}
.h2-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.h2-row h2 { margin: 0; }
.hint { margin: 6px 0 0; font-size: 12px; color: var(--muted); }
.check { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; cursor: pointer; white-space: nowrap; }
.check input { accent-color: var(--accent); margin: 0; }

/* Set-Suche */
.set-form { display: flex; gap: 6px; }
.set-form input, .set-card input[type="text"] {
  flex: 1; min-width: 0;
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 9px; background: var(--card);
}
.set-form input { font-family: var(--mono); }
.set-card {
  margin-top: 10px; padding: 10px;
  background: var(--card); border: 1px solid var(--line); border-left: 3px solid var(--accent); border-radius: 6px;
}
.set-card .title { font-weight: 600; }
.set-card .art { font-family: var(--mono); font-size: 12px; color: var(--muted); }
.set-card .note { margin: 6px 0 0; font-size: 12px; color: var(--warn); }
.set-card table { width: 100%; border-collapse: collapse; margin: 8px 0; }
.set-card td { padding: 2px 0; font-size: 13px; }
.set-card td:last-child { width: 60px; text-align: right; }
.set-rows { max-height: 260px; overflow-y: auto; }
.num {
  width: 54px; text-align: right;
  border: 1px solid var(--line); border-radius: 5px; padding: 3px 5px;
  background: var(--card); font-family: var(--mono); font-size: 13px;
}
.set-card .opts { display: flex; flex-direction: column; gap: 6px; font-size: 12px; margin-bottom: 8px; }
.set-card .opts label { display: flex; align-items: center; gap: 6px; }
.set-card .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.linkish { border: 0; background: none; padding: 0; color: var(--muted); text-decoration: underline; font-size: 12px; }
.known { margin-top: 10px; font-size: 12px; }
.known summary { cursor: pointer; color: var(--muted); }
.known ul { list-style: none; margin: 6px 0 0; padding: 0; max-height: 220px; overflow-y: auto; }
.known li button {
  display: flex; gap: 8px; width: 100%; text-align: left;
  border: 0; background: none; padding: 3px 4px; border-radius: 4px; white-space: normal;
}
.known li button:hover { background: #ebe7dd; }
.known li code { font-family: var(--mono); color: var(--muted); flex: none; }

/* Bestand */
.row {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr) 48px 40px 57px;
  align-items: center;
  gap: 6px;
  padding: 4px 0;
}
.row .ico { display: grid; place-items: center; color: var(--asphalt); }
.row .nm { min-width: 0; line-height: 1.2; }
.row .nm b { display: block; font-weight: 500; overflow-wrap: anywhere; }
.row .nm small { display: block; color: var(--muted); font-size: 11px; }
.row .num { width: 100%; }
.row .used { font: 12px var(--mono); color: var(--muted); text-align: right; }
.row .used.short { color: var(--accent); font-weight: 700; }
.row .btns { display: flex; gap: 3px; justify-content: flex-end; }
.row .btns button { width: 27px; height: 27px; padding: 0; font-size: 15px; line-height: 1; }
.row.zero .nm b, .row.zero .ico { opacity: .55; }

/* Bühne */
.stage-wrap { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.toolbar {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.toolbar .sep { width: 1px; height: 20px; background: var(--line); margin: 0 2px; }
.toolbar .grow { flex: 1; }
.stage { position: relative; flex: 1; min-height: 320px; background: #f4f1ea; overflow: hidden; }
#stage { position: absolute; inset: 0; width: 100%; height: 100%; cursor: grab; touch-action: none; user-select: none; }
#stage.drag { cursor: grabbing; }
.grid-s { fill: none; stroke: #e2ddd1; stroke-width: 3; }
.grid-l { fill: none; stroke: #cdc7b9; stroke-width: 6; }

.piece { cursor: pointer; }
.piece .body { fill: var(--asphalt); stroke: #5b6068; stroke-width: 4; stroke-linejoin: round; }
.piece.steep .body { fill: #3d3f52; }
.piece .rail { fill: none; stroke: var(--rail); stroke-width: 15; }
.piece .slot { fill: none; stroke: #101113; stroke-width: 4; }
.piece .xover { fill: none; stroke: var(--feed); stroke-width: 9; stroke-linecap: round; }
.piece .startline { stroke: #ffffff; stroke-width: 22; stroke-dasharray: 22 22; }
.piece:hover .body { fill: #3a3e45; }
.ring { fill: none; stroke-linejoin: round; pointer-events: none; }
.ring.hit { stroke: #ff8a00; stroke-width: 14; stroke-dasharray: 40 24; }
.ring.sel { stroke: var(--accent); stroke-width: 16; }
.badge-cu circle { fill: var(--accent); stroke: #fff; stroke-width: 8; }
.badge-cu text { fill: #fff; font: 700 62px var(--display); text-anchor: middle; dominant-baseline: central; }
.nums text {
  fill: #fff; opacity: .75; font: 600 58px var(--mono);
  text-anchor: middle; dominant-baseline: central; pointer-events: none;
}
.gap { stroke: var(--accent); stroke-width: 8; stroke-dasharray: 36 28; fill: none; pointer-events: none; }
.cursor { fill: var(--accent); stroke: #fff; stroke-width: 8; stroke-linejoin: round; pointer-events: none; }
.feed { pointer-events: none; }
.feed circle { fill: var(--feed); stroke: var(--ink); stroke-width: 10; }
.feed path { fill: var(--ink); }
.feed text { fill: var(--ink); font: 700 84px var(--display); paint-order: stroke; stroke: #f4f1ea; stroke-width: 22; }

.empty {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(380px, calc(100% - 32px)); padding: 18px 20px;
  background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(28, 30, 33, .12);
}
.empty[hidden] { display: none; }
.empty h3 { margin: 0 0 6px; font: 700 17px var(--display); letter-spacing: .04em; color: var(--ink); }
.empty ol { margin: 0 0 12px; padding-left: 18px; color: var(--muted); }
.scale {
  position: absolute; left: 10px; bottom: 8px; font-size: 11px; color: var(--muted);
  background: rgba(244, 241, 234, .85); padding: 2px 6px; border-radius: 4px; pointer-events: none;
}
.toast {
  position: absolute; left: 50%; bottom: 18px; transform: translate(-50%, 20px);
  max-width: calc(100% - 32px); padding: 8px 14px; border-radius: 8px;
  background: var(--ink); color: #fff; font-size: 13px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Auswertung */
.state { display: flex; align-items: center; gap: 8px; font: 700 16px var(--display); letter-spacing: .04em; text-transform: uppercase; }
.state::before { content: ""; width: 12px; height: 12px; border-radius: 50%; background: var(--muted); flex: none; }
.state.ok { color: var(--ok); }
.state.ok::before { background: var(--ok); }
.state.open { color: var(--accent); }
.state.open::before { background: var(--accent); }
#status p { margin: 6px 0 0; font-size: 13px; }
#status ul { margin: 4px 0 0; padding-left: 18px; font-size: 13px; }
.warn { color: var(--warn); }
.stats { margin: 0; display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; }
.stats dt { color: var(--muted); }
.stats dd { margin: 0; font-family: var(--mono); text-align: right; }
.stats .big { font-size: 20px; font-weight: 700; color: var(--ink); }
.range { display: block; font-size: 13px; }
.range b { white-space: nowrap; }
.range input { width: 100%; accent-color: var(--accent); margin: 6px 0 2px; }
.feed-sum { margin: 8px 0 4px; font-weight: 600; }
.feed-list { list-style: none; margin: 0; padding: 0; }
.feed-list li {
  display: grid; grid-template-columns: 34px 1fr; gap: 8px; align-items: start;
  padding: 6px; border-radius: 6px; cursor: pointer;
}
.feed-list li:hover { background: #ebe7dd; }
.feed-list .tag {
  display: grid; place-items: center; height: 24px; border-radius: 12px;
  background: var(--feed); border: 2px solid var(--ink); font: 700 12px var(--display);
}
.feed-list small { display: block; color: var(--muted); font-size: 12px; }
.shop { width: 100%; border-collapse: collapse; font-size: 13px; }
.shop td { padding: 3px 0; border-bottom: 1px solid var(--line); }
.shop td:last-child { text-align: right; font-family: var(--mono); color: var(--accent); font-weight: 700; }
.shop code { font-family: var(--mono); color: var(--muted); font-size: 11px; }

.foot { padding: 6px 16px; font-size: 11px; color: var(--muted); background: var(--panel); border-top: 1px solid var(--line); }

@media (max-width: 1100px) {
  body { height: auto; }
  .app { grid-template-columns: 1fr; }
  .stage-wrap { order: -1; }
  .stage { height: 62vh; flex: none; }
  .panel { overflow: visible; border: 0 !important; border-top: 1px solid var(--line) !important; }
  .brand .sub { display: none; }
}

@media print {
  body { height: auto; background: #fff; }
  .top-actions, .toolbar, .panel.left, .scale, .toast, .empty, .foot, .range input, .cursor { display: none !important; }
  .app { display: block; }
  .top { background: #fff; color: #000; border-bottom: 2px solid #000; }
  .brand .sub { color: #444; }
  .flag { display: none; }
  .stage { height: 150mm; background: #fff; border: 1px solid #999; }
  .panel.right { border: 0; overflow: visible; background: #fff; columns: 2; column-gap: 12mm; }
  .panel section { break-inside: avoid; }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
