:root {
  --ink: #12355f;
  --text: #1b2a3c;
  --muted: #56667b;
  --line: #d3dbe6;
  --paper: #f8f9fb;
  --grid: rgba(18, 53, 95, 0.05);
  --grid-major: rgba(18, 53, 95, 0.085);
  --accent: #1f63d6;
  --accent-dark: #174fae;
  --ok: #2b8a3e;
  --error: #c92a2a;
  --sheet-font: Bahnschrift, "DIN Alternate", "Segoe UI", system-ui, sans-serif;
  color-scheme: light;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { display: flex; flex-direction: column; min-height: 100vh; color: var(--text); background: #eef1f5; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Header */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 20px; background: #fff; border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand img { flex: none; border-radius: 9px; }
.brand-head { display: flex; align-items: baseline; flex-wrap: wrap; gap: 4px 10px; }
.brand h1 { margin: 0; font-size: 18px; color: var(--ink); }
.brand p { margin: 2px 0 0; font-size: 13px; color: var(--muted); }
.studio-back {
  display: inline-flex; align-items: center; gap: 4px;
  font: 500 12px/1 var(--sheet-font); color: var(--muted); text-decoration: none;
  padding: 3px 7px; border: 1px solid var(--line); border-radius: 5px; background: #fafbfc;
}
.studio-back:hover { color: var(--ink); border-color: var(--accent); background: #eef3fb; }
.status {
  display: flex; align-items: center; gap: 8px; flex: none; padding: 6px 12px;
  font-size: 13px; color: var(--muted); white-space: nowrap;
  background: #fafbfc; border: 1px solid var(--line); border-radius: 999px;
}
.status .dot { width: 9px; height: 9px; border-radius: 50%; background: #adb5bd; }
/* How much of the engine has arrived: fixed-width figures, so the pill holds still. */
.status .progress { min-width: 4ch; text-align: right; font-variant-numeric: tabular-nums; }
.status[data-state="ok"] .dot { background: var(--ok); }
.status[data-state="busy"] .dot, .status[data-state="loading"] .dot { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
.status[data-state="error"] { color: var(--error); background: #fff5f5; border-color: #f1c0c0; }
.status[data-state="error"] .dot { background: var(--error); }
@keyframes pulse { 50% { opacity: 0.25; } }

/* Drawing sheet */
.layout { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) 330px; min-height: 0; }
.viewer {
  position: relative; min-height: 460px; overflow: hidden;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid-major) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-major) 1px, transparent 1px),
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 100px 100px, 100px 100px, 20px 20px, 20px 20px;
}
.viewer::after {
  content: ""; position: absolute; inset: 10px; pointer-events: none;
  border: 1px solid rgba(18, 53, 95, 0.28); border-radius: 2px;
}
/* A file dragged over the page: the sheet says it will open it. */
.viewer.dropping::before {
  content: "Drop a saved parameters file to open it"; position: absolute; inset: 10px; z-index: 30; display: grid; place-items: center;
  font: 600 15px var(--sheet-font); color: var(--accent); background: rgba(248, 249, 251, 0.9);
  border: 2px dashed var(--accent); border-radius: 2px; pointer-events: none;
}
.viewer .gl { position: absolute; inset: 0; display: block; touch-action: none; }
.label-layer { position: absolute; inset: 0; z-index: 4; pointer-events: none; }
.label-layer[hidden] { display: none; }
.view-tools { position: absolute; top: 20px; left: 20px; z-index: 5; display: flex; flex-wrap: wrap; gap: 6px; }
.view-tools label, .view-tools button {
  display: flex; align-items: center; gap: 6px; padding: 5px 10px; font-size: 13px; color: var(--text);
  background: #fff; border: 1px solid var(--line); border-radius: 7px; cursor: pointer;
}
.view-tools label:hover, .view-tools button:hover { border-color: var(--accent); }
.view-tools label.disabled { color: #8794a6; background: #f1f3f6; cursor: not-allowed; }
.view-tools label.disabled:hover { border-color: var(--line); }
.view-tools input:disabled { cursor: not-allowed; }
.view-tools input[type="checkbox"] { margin: 0; }
/* The units switch and Reset wrap together on narrow screens; Reset is then just its icon. */
.view-tools-end { display: flex; gap: 6px; }
.reset-icon { display: none; }
/* Units: a two-segment switch; the selected unit is filled with the drawing's ink. */
.view-tools .units { display: flex; overflow: hidden; background: #fff; border: 1px solid var(--line); border-radius: 7px; }
.view-tools .units label { position: relative; border: 0; border-radius: 0; }
.view-tools .units label + label { border-left: 1px solid var(--line); }
.units input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.units label:hover { background: #eef3fb; }
.units label:has(input:checked) { color: #fff; background: var(--ink); }
.units label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: -4px; }
.view-hint { position: absolute; left: 20px; bottom: 18px; z-index: 5; margin: 0; font-size: 12px; color: var(--muted); pointer-events: none; }
.hint-touch { display: none; }
@media (pointer: coarse) {
  .hint-mouse { display: none; }
  .hint-touch { display: inline; }
}

/* Title block: the part's figures, in the drawing's lower right */
.title-block {
  font: 12px/1.35 var(--sheet-font); color: var(--ink); background: #fff;
  border: 1px solid rgba(18, 53, 95, 0.45); border-radius: 2px;
}
.viewer .title-block { position: absolute; right: 20px; bottom: 20px; z-index: 5; width: 290px; }
.tb-head {
  display: flex; justify-content: space-between; align-items: baseline; gap: 8px;
  padding: 6px 10px; border-bottom: 1px solid rgba(18, 53, 95, 0.45);
}
.tb-head strong { font-size: 13px; }
.tb-head span { color: var(--muted); }
.tb-fields { display: grid; grid-template-columns: auto 1fr; margin: 0; }
.tb-fields dt, .tb-fields dd { margin: 0; padding: 4px 10px; border-bottom: 1px solid rgba(18, 53, 95, 0.12); }
.tb-fields dt { color: var(--muted); border-right: 1px solid rgba(18, 53, 95, 0.12); white-space: nowrap; }
.tb-fields dd { font-variant-numeric: tabular-nums; }
.tb-fields dt:nth-last-of-type(1), .tb-fields dd:nth-last-of-type(1) { border-bottom: 0; }

/* Dimension value labels: hairline boxes beside the drawing, joined by leaders */
.leaders { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.leaders polyline { fill: none; stroke: var(--ink); stroke-opacity: 0.55; stroke-width: 1; }
.leaders circle { fill: var(--ink); fill-opacity: 0.7; }
.leaders .active { stroke: var(--accent); stroke-opacity: 1; }
.leaders .invalid { stroke: var(--error); stroke-opacity: 1; }
.leaders polyline.active, .leaders polyline.invalid { stroke-width: 2; }
.leaders circle.active { fill: var(--accent); fill-opacity: 1; }
.leaders circle.invalid { fill: var(--error); fill-opacity: 1; }
.dim {
  position: absolute; left: 0; top: 0; pointer-events: auto; max-width: 250px;
  padding: 3px 7px; font: 12px/1.3 var(--sheet-font); color: var(--muted);
  background: #fff; border: 1px solid rgba(18, 53, 95, 0.55); border-radius: 2px;
}
.dim[hidden], .dim-note[hidden] { display: none; }
.dim-row { display: flex; align-items: baseline; gap: 4px; white-space: nowrap; cursor: text; }
.dim-cap, .dim-unit { font-size: 11px; }
.dim-unit.deg { margin-left: -3px; }
.dim input[type="text"] {
  min-width: 2.4ch; margin: 0; padding: 0; border: 0; outline: none; background: transparent; text-align: right;
  font: 600 14px/1.2 var(--sheet-font); font-variant-numeric: tabular-nums; color: var(--accent);
}
.dim-choice { display: inline-flex; align-self: center; margin: 0 1px; border: 1px solid rgba(18, 53, 95, 0.35); border-radius: 2px; overflow: hidden; cursor: default; }
.dim-choice label { position: relative; display: grid; place-items: center; width: 19px; height: 17px; color: var(--ink); cursor: pointer; }
.dim-choice label + label { border-left: 1px solid rgba(18, 53, 95, 0.2); }
.dim-choice input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; }
.dim-choice svg { width: 13px; height: 13px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }
.dim-choice label:hover { background: #eef3fb; }
.dim-choice label:has(input:checked) { color: #fff; background: var(--ink); }
.dim-choice label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: -2px; }
.dim.no-size input[type="text"], .dim.no-size .dim-unit { display: none; }
.dim-note { margin: 4px 0 1px; font-size: 12px; line-height: 1.35; color: var(--error); white-space: normal; }
.dim:hover { border-color: var(--accent); }
.dim.active, .dim:focus-within { z-index: 10; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(31, 99, 214, 0.18); }
.dim.pending { opacity: 0.6; }
.dim.invalid { background: #fff5f5; border-color: var(--error); }
.dim.invalid input[type="text"] { color: var(--error); }
.dim.updated { animation: updated 1.2s cubic-bezier(0.16, 1, 0.3, 1); }
@keyframes updated { from { background: #dff0e3; border-color: var(--ok); } }

/* Side panel */
.panel { padding: 6px 18px 18px; overflow-y: auto; background: #fff; border-left: 1px solid var(--line); }
.panel section { padding: 14px 0; border-bottom: 1px solid #edf0f4; }
.panel section:last-child { border-bottom: 0; }
.panel h2 { margin: 2px 0 10px; font-size: 15px; font-weight: 600; color: var(--ink); }
.messages { margin: 0; padding: 0; list-style: none; font-size: 13px; line-height: 1.45; }
.messages li + li { margin-top: 8px; }
.messages .clear { color: var(--muted); }
.messages .warning, .messages .error { padding: 8px 10px; border-radius: 7px; }
.messages .warning { color: #6f4700; background: #fff8e6; border: 1px solid #f0d28c; }
.messages .error { color: var(--error); background: #fff5f5; border: 1px solid #f1c0c0; }
.help { margin: 4px 0 10px; font-size: 13px; line-height: 1.45; color: var(--muted); }
.buttons { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 4px; }
button {
  font: inherit; font-size: 14px; color: var(--text); padding: 8px 12px; cursor: pointer;
  background: #fff; border: 1px solid var(--line); border-radius: 8px;
}
button:hover { border-color: var(--accent); }
button.primary { color: #fff; font-weight: 600; background: var(--accent); border-color: var(--accent); }
button.primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
button:disabled, button:disabled:hover {
  cursor: not-allowed; color: #8794a6; background: #f1f3f6; border-color: var(--line);
}
/* Download STL, with the other downloads in a menu behind its arrow. */
.split { display: inline-flex; }
.split > button:first-child { border-right: 0; border-top-right-radius: 0; border-bottom-right-radius: 0; }
.split > button + button {
  display: grid; place-items: center; width: 36px; padding: 0;
  border-top-left-radius: 0; border-bottom-left-radius: 0;
}
.split > button + button:enabled, .split > button + button:enabled:hover { border-left-color: rgba(255, 255, 255, 0.45); }
.split > button[aria-expanded="true"] { background: var(--accent-dark); border-color: var(--accent-dark); border-left-color: rgba(255, 255, 255, 0.45); }
.split > button:focus-visible { position: relative; z-index: 1; }
.split svg { width: 12px; height: 12px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.menu {
  position: fixed; inset: auto; margin: 0; padding: 4px; min-width: 250px; color: var(--text);
  background: #fff; border: 1px solid var(--line); border-radius: 8px;
}
.menu button { display: grid; gap: 1px; width: 100%; padding: 7px 10px; text-align: left; border: 0; border-radius: 6px; background: none; }
.menu button:hover, .menu button:focus-visible { background: #eef3fb; }
.menu button:focus-visible { outline-offset: -2px; }
.menu-help { font-size: 12px; color: var(--muted); }
button.copied svg { width: 12px; height: 12px; margin-right: 6px; vertical-align: -1px; fill: none; stroke: var(--ok); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.link-fallback { margin: 12px 0 12px; }
.link-fallback label { display: block; margin-bottom: 6px; font-size: 13px; line-height: 1.45; color: var(--muted); }
.link-fallback input {
  width: 100%; padding: 6px 8px; font: inherit; font-size: 13px; color: var(--text);
  background: #fff; border: 1px solid var(--line); border-radius: 7px;
}
.about p { margin: 0 0 8px; font-size: 13px; line-height: 1.5; }
.panel .title-block { margin: 14px 0 4px; }

@media (max-width: 900px) {
  .layout { display: block; }
  .viewer { height: min(72vh, 125vw); min-height: 400px; }
  .panel { display: flex; flex-direction: column; overflow: visible; border-left: 0; border-top: 1px solid var(--line); }
  .panel > .title-block { order: -1; }
  .panel > .has-errors { order: -2; }
  .topbar { flex-wrap: wrap; gap: 10px; padding: 10px 14px; }
  .brand { flex: 1 1 220px; }
  .brand p { display: none; }
  .status { flex: 1 1 auto; min-width: 0; white-space: normal; }
}

@media (max-width: 559px) {
  .dim-note { display: none; }
}

@media (max-width: 600px) {
  .view-tools { top: 16px; left: 12px; right: 12px; gap: 4px; }
  .view-tools label, .view-tools button { padding: 5px 6px; gap: 4px; font-size: 12px; }
  .view-tools-end { gap: 4px; }
  .reset-icon { display: block; }
  .reset-text { display: none; }
  .view-hint { left: 16px; bottom: 14px; }
  .dim { min-height: 26px; padding: 3px 6px; }
  .dim-cap { font-size: 11px; }
  .dim input[type="text"] { font-size: 13px; }
  .dim-unit.len { display: none; }
  .dim:has(.dim-choice) .dim-cap { display: none; }
  .dim-choice label { width: 17px; }
}

@media (prefers-reduced-motion: reduce) {
  .dim.updated, .status .dot { animation: none !important; }
}
