:root {
  --c-bg: #f2f4f7;
  --c-bg-editor: #e6ebf0;
  --c-panel: #ffffff;
  --c-text: #1a2330;
  --c-muted: #5c6b7a;
  --c-border: #d5dce4;
  --c-accent: #ff7a1a;
  --c-accent-ink: #fff;
  --c-primary: #1d6fd6;
  --c-danger: #d63a3a;
  --c-board: #faf7ee;
  --c-board-edge: #b9a98a;
  --c-grid: rgba(0, 0, 0, 0.07);
  --c-ballast: #cfc8ba;
  --c-sleeper: #8d7b64;
  --c-rail: #444;
  --c-aid-grid: rgba(30, 120, 220, 0.32);
  --c-sketch: rgba(255, 122, 26, 0.55);
  --c-pit: #8d8a84;
  --topbar-h: 52px;
  --palette-h: 196px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --c-bg: #121820; --c-bg-editor: #1a2230; --c-panel: #1e2733; --c-text: #e8edf3; --c-muted: #9aa8b8;
    --c-border: #33404f; --c-board: #2a3140; --c-board-edge: #6d6250; --c-grid: rgba(255,255,255,0.07);
    --c-ballast: #6f6a60; --c-sleeper: #5a4a3a; --c-rail: #c9ccd1; --c-aid-grid: rgba(120, 180, 255, 0.35);
  }
}

* { box-sizing: border-box; }
.ic { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: 0 0 auto; vertical-align: -4px; }
.ic.sm { width: 14px; height: 14px; vertical-align: -2px; }
.ic.logo { width: 22px; height: 22px; color: var(--c-accent); }
html, body { height: 100%; margin: 0; overscroll-behavior: none; }
body {
  font: 15px/1.35 -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  color: var(--c-text); background: var(--c-bg);
  display: grid; grid-template-rows: auto 1fr auto; height: 100dvh; width: 100vw; overflow: hidden;
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent;
}
button, select, input { font: inherit; color: inherit; }
button {
  background: var(--c-panel); border: 1px solid var(--c-border); border-radius: 10px; padding: 9px 12px;
  min-height: 40px; cursor: pointer; touch-action: manipulation;
}
button:active { transform: translateY(1px); }
button.primary { background: var(--c-primary); color: #fff; border-color: transparent; font-weight: 600; }
button.danger { color: var(--c-danger); }
button.icon, .hud-btn, .sel-tools button, .draw-bar button:not(.primary) { display: inline-flex; align-items: center; justify-content: center; }
button.icon { width: 40px; padding: 0; }
#btn-add { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
button:disabled { opacity: 0.4; }
select, input[type=text], input[type=number] {
  background: var(--c-panel); border: 1px solid var(--c-border); border-radius: 10px; padding: 8px 10px; min-height: 40px; width: 100%;
}
.hidden { display: none !important; }
.grow { flex: 1; }
.row { display: flex; gap: 8px; align-items: end; }
.row.wrap { flex-wrap: wrap; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--c-muted); min-width: 0; }
.field > span { padding-left: 2px; }

/* pasek górny */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(6px + var(--sat)) calc(10px + var(--sar)) 6px calc(10px + var(--sal));
  background: #1d2b3a; color: #fff; min-height: calc(var(--topbar-h) + var(--sat));
}
.brand { font-weight: 700; letter-spacing: 0.2px; white-space: nowrap; display: flex; align-items: center; gap: 6px; }
.brand .sub { font-weight: 400; opacity: 0.7; font-size: 12px; }
.topbar-actions { display: flex; gap: 6px; align-items: center; }
.topbar button { background: rgba(255,255,255,0.08); border-color: rgba(255,255,255,0.15); color: #fff; }
.seg { display: flex; border: 1px solid rgba(255,255,255,0.2); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: transparent; padding: 0 12px; }
.seg button.active { background: var(--c-accent); color: var(--c-accent-ink); font-weight: 600; }
.wide-only { display: none; }

/* obszar roboczy */
.workspace { position: relative; display: grid; min-height: 0; grid-template-columns: 1fr; }
.pane { position: relative; min-width: 0; min-height: 0; overflow: hidden; }
.workspace.mode-2d #pane-3d, .workspace.mode-3d #pane-2d { display: none; }
.workspace.mode-split { grid-template-columns: 1fr 1fr; }
#canvas2d { display: block; width: 100%; height: 100%; }
#view3d { width: 100%; height: 100%; }
#view3d canvas { display: block; width: 100% !important; height: 100% !important; touch-action: none; }
.hud { position: absolute; right: calc(10px + var(--sar)); top: 10px; display: flex; flex-direction: column; gap: 6px; }
.hud-btn { width: 40px; height: 40px; padding: 0; box-shadow: 0 2px 8px rgba(0,0,0,0.15); }
.sel-tools {
  position: absolute; left: calc(10px + var(--sal)); bottom: 10px; display: flex; gap: 6px; align-items: center;
  background: var(--c-panel); border: 1px solid var(--c-border); border-radius: 12px; padding: 6px; box-shadow: 0 2px 10px rgba(0,0,0,0.15);
  max-width: calc(100% - 20px);
}
.sel-tools .sel-name { padding: 0 8px; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sel-tools button { width: 40px; padding: 0; }
.sel-tools .size { display: flex; align-items: center; gap: 4px; font-size: 12px; color: var(--c-muted); }
.sel-tools .size input { width: 60px; min-height: 36px; padding: 4px 6px; }
.sel-tools .size span { white-space: nowrap; }
@media (max-width: 560px) { .sel-tools { flex-wrap: wrap; max-width: calc(100% - 20px); } .sel-tools .sel-name { flex: 1 0 100%; } }

.hud-btn.active { background: var(--c-accent); color: #fff; border-color: transparent; }
.draw-bar {
  position: absolute; left: calc(10px + var(--sal)); right: calc(60px + var(--sar)); bottom: 10px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap;
  background: var(--c-panel); border: 1px solid var(--c-border); border-radius: 12px; padding: 6px; box-shadow: 0 2px 10px rgba(0,0,0,0.15);
}
.draw-bar .primary { margin-left: auto; }
#train-bar .primary { margin-left: 0; width: 44px; padding: 0; }
#levels-bar select { max-width: 60vw; }
.badge-mini { position: absolute; right: 2px; bottom: 2px; font-size: 10px; line-height: 1; padding: 1px 3px; border-radius: 6px; background: var(--c-accent, #ff7a1a); color: #fff; pointer-events: none; }
.badge-mini:empty { display: none; }
.hud-btn { position: relative; }
#train-bar .speed { gap: 8px; }
#train-bar input[type=range] { width: 120px; }
#train-bar .hint { flex: 1 0 100%; }
.hud-btn.active { background: var(--c-accent); color: #fff; }
.chk { display: flex; align-items: center; gap: 6px; font-size: 13px; padding: 0 6px; min-height: 40px; }
.chk input { width: 18px; height: 18px; }
#canvas2d.drawing { cursor: crosshair; }

/* paleta */
.palette {
  background: var(--c-panel); border-top: 1px solid var(--c-border);
  padding: 0 calc(12px + var(--sar)) calc(10px + var(--sab)) calc(12px + var(--sal));
  transition: max-height 0.2s ease; overflow: hidden;
}
.palette-grip { display: flex; justify-content: center; padding: 6px 0 2px; }
.palette-grip span { width: 40px; height: 5px; border-radius: 3px; background: var(--c-border); }
.palette-body { display: flex; flex-direction: column; gap: 8px; }
.palette.collapsed .palette-body { display: none; }
/* zakładki palety */
.tabs { align-self: stretch; }
.tabs button { flex: 1; min-height: 38px; }
.seg.tabs { border-color: var(--c-border); }
.seg.tabs button.active { background: var(--c-primary); color: #fff; }
.row.compact .field { font-size: 12px; }
.row.compact select { min-height: 36px; padding: 4px 8px; }

/* lista elementów */
.pal-list { display: flex; flex-direction: column; gap: 4px; overflow-y: auto; -webkit-overflow-scrolling: touch; max-height: 34dvh; padding-right: 2px; }
.pal-section { position: sticky; top: 0; z-index: 1; background: var(--c-panel); font-size: 11px; text-transform: uppercase; letter-spacing: 0.4px; color: var(--c-muted); padding: 6px 4px 2px; }
.pal-item { display: flex; align-items: center; gap: 10px; width: 100%; text-align: left; padding: 6px 8px; min-height: 52px; border-radius: 10px; cursor: pointer; -webkit-tap-highlight-color: transparent; }
.pal-item:hover { background: var(--c-bg); }
.pal-item:active { background: var(--c-bg); }
.pal-icon { width: 60px; height: 34px; flex: 0 0 auto; }
.pal-icon path { fill: none; stroke: var(--c-rail); stroke-width: 2.2; stroke-linecap: round; }
.pal-icon .fill { fill: currentColor; stroke: none; opacity: 0.85; }
.pal-text { flex: 1; min-width: 0; }
.pal-title { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pal-title .pal-id { font-weight: 400; color: var(--c-muted); font-size: 12px; margin-left: 4px; }
.pal-desc { font-size: 12px; color: var(--c-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pal-meta { font-size: 12px; color: var(--c-text); font-variant-numeric: tabular-nums; white-space: nowrap; }
.pal-actions { display: flex; gap: 4px; flex: 0 0 auto; }
.pal-actions button { width: 36px; min-height: 36px; padding: 0; }
.hint { margin: 0; font-size: 12px; color: var(--c-muted); }

input[type=color] { width: 56px; min-height: 40px; padding: 2px 4px; border: 1px solid var(--c-border); border-radius: 10px; background: var(--c-panel); }
.swatches { display: flex; gap: 6px; align-items: end; padding-bottom: 2px; }
.swatches button { width: 32px; height: 32px; min-height: 0; padding: 0; border-radius: 50%; border: 2px solid var(--c-border); }
.swatches button.active { border-color: var(--c-accent); box-shadow: 0 0 0 2px var(--c-accent); }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--palette-h) + var(--sab) + 12px); transform: translateX(-50%); z-index: 30;
  max-width: min(92vw, 560px); padding: 10px 14px; border-radius: 12px; background: #b3261e; color: #fff; font-size: 13px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3); -webkit-user-select: text; user-select: text;
}
@media (min-width: 900px) { .toast { bottom: 20px; } }

/* problemy */
.badge { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--c-danger); color: #fff; font-size: 12px; text-align: center; vertical-align: middle; }
.badge.ok { background: #2e9e5b; }
.menu-badge { position: absolute; top: -4px; right: -4px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--c-danger); color: #fff; font-size: 11px; line-height: 18px; font-weight: 700; }
#btn-menu { position: relative; }
.problems { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.problems .prob { display: flex; gap: 8px; align-items: center; padding: 6px 8px; border-radius: 8px; background: var(--c-bg); cursor: pointer; }
.problems .prob .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c-danger); flex: 0 0 auto; }
.problems .prob.warn .dot { background: #e0a020; }
.problems .none { color: var(--c-muted); font-size: 13px; }

/* arkusz menu */
.sheet { position: fixed; inset: 0; z-index: 20; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.4); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 88dvh; overflow: auto;
  background: var(--c-panel); border-radius: 16px 16px 0 0; padding: 16px calc(16px + var(--sar)) calc(16px + var(--sab)) calc(16px + var(--sal));
  display: flex; flex-direction: column; gap: 10px;
}
.sheet-panel h2 { margin: 6px 0 0; font-size: 16px; }
.bom { display: grid; grid-template-columns: auto auto 1fr auto auto; gap: 4px 10px; font-size: 14px; font-variant-numeric: tabular-nums; align-items: center; }
.bom .have { width: 56px; min-height: 32px; padding: 2px 6px; text-align: right; }
.bom .buy { font-weight: 600; color: var(--c-danger); text-align: right; }
.bom .buy.ok { color: #2e9e5b; }
.bom .head { font-size: 11px; color: var(--c-muted); text-transform: uppercase; letter-spacing: 0.3px; }
.bom .n { text-align: right; font-weight: 600; }
.bom .id { color: var(--c-muted); }
.bom .total { grid-column: 1 / -1; border-top: 1px solid var(--c-border); padding-top: 6px; margin-top: 4px; color: var(--c-muted); }

/* telefon: ciaśniejszy pasek górny */
@media (max-width: 560px) {
  .brand .sub { display: none; }
  .brand { font-size: 15px; }
  .topbar { gap: 4px; }
  .topbar-actions { gap: 4px; }
  .topbar button.icon { width: 36px; }
  .seg button { padding: 0 8px; }
  .ic.logo { width: 18px; height: 18px; }
  .brand { gap: 4px; }
}

/* przełącznik chowania panelu bocznego (tylko szerokie ekrany) */
.side-toggle { display: none; }

/* tablet / desktop: paleta z boku, split 2D+3D */
@media (min-width: 900px) {
  body { grid-template-columns: 1fr 340px; grid-template-rows: auto 1fr; grid-template-areas: "top top" "work side"; }
  body.side-collapsed { grid-template-columns: 1fr 0; }
  body.side-collapsed .palette { display: none; }
  .side-toggle {
    display: inline-flex; position: fixed; z-index: 15; top: calc(50% - 22px); right: calc(340px + var(--sar) - 1px);
    width: 26px; height: 44px; padding: 0; border-radius: 10px 0 0 10px; border-right: 0; box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  }
  body.side-collapsed .side-toggle { right: calc(var(--sar)); border-radius: 10px 0 0 10px; }
  body.side-collapsed .side-toggle .ic { transform: rotate(180deg); }
  .topbar { grid-area: top; }
  .workspace { grid-area: work; }
  .palette { grid-area: side; border-top: 0; border-left: 1px solid var(--c-border); overflow: auto; padding-top: 12px; }
  .palette-grip { display: none; }
  .palette.collapsed .palette-body { display: flex; }
  .wide-only { display: inline-block; }
  .pal-list { max-height: none; flex: 1; }
  .palette-body { height: 100%; }
  .sheet-panel { left: auto; right: 0; top: 0; bottom: 0; width: 420px; max-height: none; border-radius: 16px 0 0 16px; }
}

/* druk: kafelki A4 1:1 lub cały plan na stronie */
#print-root { display: none; }
@media print {
  @page { size: A4 portrait; margin: 10mm; }
  body { display: block; height: auto; overflow: visible; background: #fff; }
  body.printing > *:not(#print-root) { display: none !important; }
  #print-root { display: block; }
  #print-root .page { width: 190mm; height: 277mm; page-break-after: always; break-after: page; position: relative; overflow: hidden; }
  #print-root .page.landscape { width: 277mm; height: 190mm; }
  #print-root .page-title { position: absolute; left: 0; top: 0; font: 9px/1.2 system-ui, sans-serif; color: #333; background: rgba(255,255,255,0.85); padding: 1mm 2mm; }
  #print-root canvas { display: block; }
}
