/* Cpiper – layout and views. No external fonts: system font stack only. */

:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1d232b;
  --muted: #5d6875;
  --border: #d5dbe2;
  --accent: #2563a6;
  --accent-text: #ffffff;
  --error: #b42318;
  --error-bg: #fdecea;
  --focus: #f0a400;

  /* drawing colours */
  --line: #1d232b;
  --edge: #3b5b7a;
  --hidden: #9aa5b1;
  --cut: #c62828;
  --center: #2e7d32;
  --break: #6d5a3a;
  --fill-main: #dfe8f1;
  --fill-bore: #ffffff;
  --fill-branch: #f3e6cf;
  --paper: #ffffff;

  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181d;
    --panel: #1c2128;
    --text: #e6eaef;
    --muted: #9aa5b1;
    --border: #333b45;
    --accent: #5b9bd5;
    --accent-text: #0d1117;
    --error: #ff8a80;
    --error-bg: #3a1d1b;
    --line: #e6eaef;
    --edge: #8fb3d6;
    --hidden: #5d6875;
    --cut: #ff6b6b;
    --center: #7bc67e;
    --break: #c9a36a;
    --fill-main: #2a3542;
    --fill-bore: #1c2128;
    --fill-branch: #4a3f2c;
    --paper: #1c2128;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { display: flex; flex-direction: column; min-height: 100vh; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}
.muted { color: var(--muted); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 1px; }

button, select, input { font: inherit; color: inherit; }
button {
  background: var(--panel); border: 1px solid var(--border); border-radius: 6px;
  padding: .3rem .7rem; cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.link { border: 0; background: none; color: var(--accent); padding: 0; text-decoration: underline; }

/* ---- top bar ---- */
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem 1rem;
  padding: .5rem 1rem; background: var(--panel); border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 1.25rem; margin: 0; display: flex; align-items: center; }
/* Seapiper mascot (cpiper.com). Its navy outline would vanish on the dark bar, so it gets a light halo there. */
.mascot { height: 2.75rem; width: auto; margin: -.25rem .6rem -.25rem 0; }
/* The pecking bird on the right, mirrored so it faces the title. */
.mascot-peck { margin: -.25rem 0 -.25rem .6rem; transform: scaleX(-1); }
@media (prefers-color-scheme: dark) { .mascot { filter: drop-shadow(0 0 1px rgba(255, 255, 255, .7)); } }
.tagline { font-size: .85rem; font-weight: normal; color: var(--muted); margin-left: .5rem; }
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem; }
.segmented { display: inline-flex; }
.segmented button { border-radius: 0; margin-left: -1px; }
.segmented button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.segmented button:last-child { border-radius: 0 6px 6px 0; }
.segmented button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
/* Same width for "inch" and "Zoll", so nothing moves when the language changes. */
[data-unit-choice] { min-width: 3.6rem; }
/* Language switch: the country's flag as the background (local SVG files, no third-party requests).
   The language not in use is dimmed; white text with a shadow stays readable on both flags. */
[data-lang-choice] {
  --flag: none;
  min-width: 2.8rem; color: #fff; font-weight: 700; text-shadow: 0 0 2px #000, 0 1px 2px #000;
  background: linear-gradient(rgba(0, 0, 0, .5), rgba(0, 0, 0, .5)), var(--flag) center / cover no-repeat;
}
[data-lang-choice="de"] { --flag: url("../img/flag-de.svg"); }
[data-lang-choice="en"] { --flag: url("../img/flag-gb.svg"); }
.segmented [data-lang-choice][aria-pressed="true"] {
  color: #fff; border-color: var(--accent); position: relative; z-index: 1;
  box-shadow: 0 0 0 2px var(--accent);
  background: linear-gradient(rgba(0, 0, 0, .12), rgba(0, 0, 0, .12)), var(--flag) center / cover no-repeat;
}
.inch-format select { padding: .25rem; border: 1px solid var(--border); border-radius: 6px; background: var(--panel); min-width: 6.5rem; } /* same width in both languages */

.presets { position: relative; }
.presets summary {
  list-style: none; cursor: pointer; border: 1px solid var(--border); border-radius: 6px; padding: .3rem .7rem;
  /* As wide as the longest label ("Voreinstellungen"), so the switches beside it don't move when the language changes. */
  box-sizing: border-box; min-width: 10.5rem; text-align: center; white-space: nowrap;
}
.presets summary::after { content: " ▾"; }
.presets-menu {
  position: absolute; right: 0; z-index: 10; min-width: 16rem; margin-top: .25rem; padding: .75rem;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.15);
  display: grid; gap: .5rem;
}
.presets-menu ul { list-style: none; margin: 0; padding: 0; display: grid; gap: .25rem; }
.presets-menu li { display: flex; gap: .25rem; align-items: center; }
.presets-menu li .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.presets-menu p { margin: 0; }

/* ---- main layout ---- */
/* Tab bar across the top. Pipe and intersection parameters on the left on both
   tabs; on the Templates and Sleeves tabs a right-hand panel (template or sleeve options, with Export). */
.layout { flex: 1; display: grid; grid-template-columns: 22rem minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); min-height: 0; }
.layout > .tabs { grid-column: 1 / -1; grid-row: 1; padding: .5rem .75rem 0; }
.layout > #params { grid-column: 1; grid-row: 2; }
.layout > .views { grid-column: 2; grid-row: 2; }
.layout > .params-side { display: none; }
.layout[data-side] { grid-template-columns: 22rem minmax(0, 1fr) 19rem; }
.layout > .params-side.active { display: block; grid-column: 3; grid-row: 2; border-right: 0; border-left: 1px solid var(--border); }
.params {
  overflow-y: auto; padding: .75rem; border-right: 1px solid var(--border); background: var(--panel);
  max-height: calc(100vh - 8.5rem);
}
/* min-width: 0 — fieldsets default to min-content width and would push the page wider than a phone screen */
.group { border: 1px solid var(--border); border-radius: 8px; margin: 0 0 .75rem; padding: .5rem .75rem .75rem; min-width: 0; }
.params { min-width: 0; }
.group legend { font-weight: 600; padding: 0 .25rem; }
.row { display: grid; grid-template-columns: 1fr 9rem; align-items: center; gap: .15rem .5rem; margin-top: .4rem; }
.row[hidden] { display: none; }
.row label { font-size: .9rem; }
.input { display: flex; align-items: center; gap: .25rem; }
.input input, .input select {
  width: 100%; min-width: 0; padding: .3rem .4rem; border: 1px solid var(--border); border-radius: 6px; background: var(--bg);
}
.input input { text-align: right; font-variant-numeric: tabular-nums; }
.unit { font-size: .8rem; color: var(--muted); min-width: 1.6rem; }
.field-error { grid-column: 1 / -1; font-size: .8rem; color: var(--error); }
.field-error:empty { display: none; }
.invalid input, .invalid select { border-color: var(--error); background: var(--error-bg); }
/* Sleeve options: a collapsible group; checkboxes sit at the right like the inputs. */
.params-sleeve .hint { font-size: .8rem; margin: .35rem 0 0; }
.row-check .input { justify-content: flex-end; padding-right: 1.85rem; }
.row-check .input input { width: auto; }
.row.row-bed, .row.row-text { grid-template-columns: 1fr; }
.row-text .input input { text-align: left; }
.input.bed input { width: 0; flex: 1 1 0; }

.views { display: flex; flex-direction: column; min-width: 0; min-height: 0; padding: .75rem; gap: .5rem; }
.tabs { display: flex; flex-wrap: wrap; gap: .25rem; border-bottom: 1px solid var(--border); }
.tabs button { border-radius: 6px 6px 0 0; border-bottom: 0; background: transparent; }
.tabs button[aria-selected="true"] { background: var(--panel); border-color: var(--border); font-weight: 600; }
.panels { flex: 1; min-height: 32rem; position: relative; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; }
.panel { position: absolute; inset: 0; display: flex; flex-direction: column; }
.panel[hidden] { display: none; }
/* Each tab: 3D on the left over the full height, two drawings stacked on the right. */
.panel.grid {
  display: grid; gap: .5rem; padding: .5rem;
  grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
}
.panel.grid[hidden] { display: none; }
/* Templates tab: the drawings get more room than the 3D view. */
#panel-templates.grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1.35fr); }
/* Sleeves tab: sleeves on the pipes (shared 3D view) and the print parts, both full height. */
#panel-sleeves.grid { grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr); }
#panel-sleeves .cell:not(.cell-3d) { grid-row: 1 / span 2; }
.layout[data-tab="sleeves"] .pipes-only { display: none; }
.parts-list { margin: 0; padding: .3rem .6rem 0 1.6rem; font-size: .85rem; }
.parts-list:empty { display: none; }
.parts-list .swatch { display: inline-block; width: .75em; height: .75em; border-radius: 2px; margin-right: .35em; vertical-align: -.05em; }
.cell { display: flex; flex-direction: column; min-width: 0; min-height: 0; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.cell-3d { grid-row: 1 / span 2; }
.view3d-box { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.cell-title { margin: 0; font-size: .9rem; font-weight: 600; }
.cell > .cell-title { padding: .35rem .6rem 0; }
.toolbar .cell-title { margin-right: .25rem; }
.toolbar { display: flex; flex-wrap: wrap; gap: .5rem 1rem; align-items: center; padding: .4rem .6rem; border-bottom: 1px solid var(--border); font-size: .9rem; }
.canvas3d { flex: 1; min-height: 0; position: relative; touch-action: none; }
.canvas3d canvas { display: block; width: 100%; height: 100%; }
.svg-host { flex: 1; min-height: 0; padding: .5rem; }
.svg-host svg { width: 100%; height: 100%; display: block; }
.caption { margin: 0; padding: .3rem .6rem .5rem; font-size: .85rem; color: var(--muted); }
.notice { margin: 1rem; padding: .75rem 1rem; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.status { margin: 0; min-height: 1.4em; font-size: .9rem; color: var(--muted); }
.status.error { color: var(--error); }

.footer { display: flex; gap: 1.25rem; justify-content: center; padding: .5rem; border-top: 1px solid var(--border); font-size: .85rem; background: var(--panel); }
.footer a { color: var(--accent); }

/* ---- SVG drawings ---- */
svg .l-outline { stroke: var(--line); stroke-width: 1.4; fill: none; }
svg .l-edge { stroke: var(--edge); stroke-width: 1; fill: none; }
svg .l-hidden { stroke: var(--hidden); stroke-width: 1; stroke-dasharray: 5 3; fill: none; }
svg .l-cut { stroke: var(--cut); stroke-width: 2.2; fill: none; }
svg .l-cutHidden { stroke: var(--cut); stroke-width: 1; stroke-dasharray: 5 3; opacity: .6; fill: none; }
svg .l-center { stroke: var(--center); stroke-width: .9; stroke-dasharray: 12 3 2 3; fill: none; }
svg .l-break { stroke: var(--break); stroke-width: 1; fill: none; }
svg .l-ref { stroke: var(--edge); stroke-width: .8; fill: none; }
svg .l-face { stroke: var(--center); stroke-width: .8; stroke-dasharray: 4 3; fill: none; }
svg .l-dim { stroke: var(--muted); stroke-width: .8; fill: none; }
svg .f-main { fill: var(--fill-main); }
svg .f-bore { fill: var(--fill-bore); }
svg .f-branch { fill: var(--fill-branch); opacity: .9; }
svg .f-paper { fill: var(--paper); stroke: var(--border); stroke-width: 1; }
svg .f-waste { fill: var(--hidden); opacity: .18; }
svg text { fill: var(--text); font-family: inherit; }
svg text.small { fill: var(--muted); }
svg * { vector-effect: non-scaling-stroke; }

/* ---- medium screens: template options under the pipe parameters ---- */
@media (min-width: 901px) and (max-width: 1250px) {
  html:has(.layout[data-side]), body:has(.layout[data-side]) { height: auto; }
  .layout[data-side] .export-bar { position: static; margin: 0; padding: 0; border: 0; background: none; }
  .layout[data-side] { grid-template-columns: 20rem minmax(0, 1fr); grid-template-rows: auto auto minmax(0, 1fr); }
  .layout[data-side] > #params { grid-row: 2; max-height: none; overflow: visible; }
  .layout > .params-side.active { grid-column: 1; grid-row: 3; border-left: 0; border-right: 1px solid var(--border); max-height: none; overflow: visible; }
  .layout[data-side] > .views { grid-row: 2 / span 2; }
}

/* ---- narrow screens: everything stacks (tabs, parameters, template options, views) ---- */
@media (max-width: 900px) {
  html, body { height: auto; }
  .layout, .layout[data-side] { grid-template-columns: 1fr; grid-template-rows: none; align-content: start; }
  .layout > #params, .layout > .views, .layout > .params-side.active { grid-column: 1; grid-row: auto; }
  .layout > .params-side.active { border-left: 0; }
  #panel-templates.grid, #panel-sleeves.grid { grid-template-columns: minmax(0, 1fr); }
  #panel-sleeves .cell:not(.cell-3d) { grid-row: auto; }
  .params { max-height: none; overflow: visible; border-right: 0; border-bottom: 1px solid var(--border); }
  .panels { min-height: 0; }
  .panel, .panel.grid { position: static; grid-template-columns: minmax(0, 1fr); grid-template-rows: none; }
  .cell { height: max(280px, 75vw); }
  .cell-3d { grid-row: auto; height: max(340px, 95vw); }
  .tagline { display: none; }
  .mascot-peck { display: none; }
}
@media (max-width: 420px) {
  .row { grid-template-columns: 1fr 7.5rem; }
  .views { padding: .5rem; }
}

/* ---- zoomable template drawings ---- */
.cell { position: relative; }
.zoom-controls { position: absolute; top: .3rem; right: .4rem; z-index: 2; display: flex; gap: .25rem; }
.zoom-controls button { min-width: 1.9rem; padding: .1rem .45rem; line-height: 1.3; }
.svg-host.zoomable { cursor: zoom-in; touch-action: none; user-select: none; }
.svg-host.zoomable.zoomed { cursor: grab; }
.svg-host.zoomable.zoomed:active { cursor: grabbing; }

/* ---- export ---- */
.export-bar { position: sticky; bottom: -.75rem; margin: 0 -.75rem -.75rem; padding: .6rem .75rem; background: var(--panel); border-top: 1px solid var(--border); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.export-bar button { width: 100%; padding: .45rem; }
dialog { border: 1px solid var(--border); border-radius: 10px; padding: 1rem 1.25rem; max-width: min(32rem, calc(100vw - 2rem)); background: var(--panel); color: var(--text); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog h2 { margin: 0 0 .75rem; font-size: 1.15rem; }
dialog fieldset { border: 1px solid var(--border); border-radius: 8px; margin: 0 0 .75rem; padding: .4rem .75rem .6rem; display: grid; gap: .3rem; }
dialog legend { font-weight: 600; }
.export-note { font-size: .85rem; margin: .4rem 0; }
.export-error { color: var(--error); min-height: 1.2em; margin: .4rem 0; }
.export-error:empty { display: none; }
.intro p, .intro ul { margin: 0 0 .6rem; line-height: 1.45; }
.intro ul { padding-left: 1.2rem; display: grid; gap: .3rem; }
.dialog-buttons { display: flex; gap: .5rem; margin-top: .5rem; }
.dialog-legal { display: flex; gap: 1rem; font-size: .8rem; margin: .75rem 0 0; }
.dialog-legal a { color: var(--accent); }
