/* Copyright (C) 2026 Reagen Ward */
/* SPDX-License-Identifier: GPL-2.0-or-later */

/* Sketch-A-Bike CAD in the browser (PRD FR-33 layout "workbench"): a top bar; the inputs on the left
   in collapsible groups; on the right the drawing with Side | Rear | 3D tabs, the problems
   and the frame numbers under it; the "?" guide, when open, at the far right; a footer.
   Up to 1000 px wide, the guide lies over the right of the drawing pane instead. Up to
   700 px (a phone): one column, the drawing pane first, then the numbers, then the form;
   the guide fills the screen. */

:root {
  --ink: #1d2327;
  --muted: #5f6b73;
  --line: #d5dbdf;
  --paper: #ffffff;
  --panel: #f4f6f7;
  --bar: #23303a;
  --accent: #2f6f9f;
  --bad: #b3261e;
  --bad-bg: #fdf1f0;
  --mark: #e89a00;
  --warn: #7a4a00;     /* a design warning's text, amber on --warn-bg (FR-5) */
  --warn-bg: #fff6db;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--paper);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.5rem 1rem;
  background: var(--bar);
  color: #fff;
}

.topbar h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-left: auto;
}

button {
  font: inherit;
  padding: 0.35rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
}

button:disabled {
  cursor: default;
  opacity: 0.5;
}

/* A file being made (FR-30). */
button[aria-busy="true"] { cursor: progress; }

/* The top bar's menu: Reset to defaults (FR-31). */
.menu { position: relative; }

.menu > summary {
  list-style: none;
  cursor: pointer;
  padding: 0.35rem 0.8rem;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--paper);
  color: var(--ink);
}

.menu > summary::-webkit-details-marker { display: none; }
.menu > summary::after { content: " ▾" / ""; color: var(--muted); }  /* not read aloud */

.menu-items {
  position: absolute;
  right: 0;
  top: calc(100% + 0.3rem);
  z-index: 10;
  min-width: 12rem;
  padding: 0.25rem;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 4px;
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.2);
}

.menu-items button {
  display: block;
  width: 100%;
  border: none;
  text-align: left;
}

.menu-items button:hover, .menu-items button:focus-visible { background: var(--panel); }

/* One-line notes under the top bar (FR-31: autosave). */
.note {
  margin: 0;
  padding: 0.3rem 1rem;
  border-bottom: 1px solid var(--line);
  background: #fff6db;
  font-size: 0.85rem;
}

.workbench {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(19rem, 25rem) minmax(0, 1fr) auto;  /* the guide's is empty
                                                                      while it is closed */
  grid-template-rows: minmax(0, 1fr);
  grid-template-areas: "inputs drawing guide";
  min-height: 0;
}

.inputs { grid-area: inputs; }
.drawing { grid-area: drawing; }
.guide { grid-area: guide; }

/* --- Inputs (FR-27) ------------------------------------------------------------------ */

.inputs {
  border-right: 1px solid var(--line);
  background: var(--panel);
  padding: 0.5rem 0.75rem 1rem;
  overflow: auto;
}

.group { margin-bottom: 0.25rem; }

.group > summary {
  list-style: none;
  cursor: pointer;
  padding: 0.4rem 0.1rem;
  font-weight: 600;
  border-bottom: 1px solid var(--line);
  margin-bottom: 0.3rem;
}

.group > summary::-webkit-details-marker { display: none; }
.group > summary::before { content: "▸"; display: inline-block; width: 1.1em; color: var(--muted); }
.group[open] > summary::before { content: "▾"; }

.field {
  display: grid;
  grid-template-columns: 1fr 7rem;
  align-items: center;
  gap: 0.5rem;
  padding: 0.12rem 0;
  font-size: 0.88rem;
}

.field.wide { grid-template-columns: 1fr 11rem; }

/* A choice list is as wide as its longest label, so every label shows in full (FR-15b),
   and at least as wide as the text fields. Where it does not fit beside its label, it moves
   under it, at the right. */
.field.choice {
  display: flex;
  flex-wrap: wrap;
  gap: 0.15rem 0.5rem;
}

.field.choice label { flex: 1 1 auto; }

.field input, .field select {
  font: inherit;
  width: 100%;
  padding: 0.2rem 0.35rem;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: var(--paper);
  color: var(--ink);
}

.field.choice select {
  width: auto;
  min-width: 11rem;
  max-width: 100%;
  margin-left: auto;
}

.field.number input {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.field input:focus, .field select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.form-failed, .load-failed { color: var(--bad); font-weight: 600; }

/* --- Drawing pane (FR-28) ------------------------------------------------------------ */

.drawing {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-width: 0;
  padding: 0.6rem 0.75rem 0.5rem;
  overflow: auto;
}

.view-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.tabs { display: flex; gap: 0.25rem; }

.tabs [role="tab"] {
  border-radius: 4px 4px 0 0;
  border-bottom-color: transparent;
  background: var(--panel);
  color: var(--muted);
}

.tabs [role="tab"][aria-selected="true"] {
  background: var(--paper);
  color: var(--ink);
  border-color: var(--accent);
  border-bottom-color: transparent;
  box-shadow: inset 0 2px 0 var(--accent);
  font-weight: 600;
}

.view-hint {
  margin-left: auto;
  color: var(--muted);
  font-size: 0.8rem;
}

.views {
  position: relative;
  flex: 1 1 auto;
  min-height: 16rem;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--paper);
  overflow: hidden;
}

.view { position: absolute; inset: 0; }

.svg-view {
  touch-action: none;  /* the view zooms and pans itself */
  cursor: grab;
  user-select: none;
}

.svg-view:active { cursor: grabbing; }

.svg-view > svg {
  display: block;
  width: 100%;
  height: 100%;
}

/* Lines keep their screen width at any zoom (app.js sets the widths). */
.svg-view svg * { vector-effect: non-scaling-stroke; }

/* The 3D view (FR-29): three.js's canvas fills it; it turns, zooms and pans itself. */
.model-view > canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}

.model-view > canvas:active { cursor: grabbing; }

/* Said in place of the model when the browser has no WebGL 2. */
.model-message {
  margin: 2rem;
  color: var(--muted);
}

/* Shown while the 3D view's STL is made or drawn. */
.spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2.5rem;
  height: 2.5rem;
  margin: -1.25rem 0 0 -1.25rem;
  border: 0.25rem solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 3s; }
}

/* While the input has problems, the last valid drawing, model and numbers stay, greyed. */
.drawing.stale .svg-view > svg,
.drawing.stale .model-view > canvas,
.drawing.stale .numbers {
  opacity: 0.3;
}

.problems, .warnings, .internal-error {
  margin: 0;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--bad);
  border-radius: 4px;
  background: var(--bad-bg);
  color: var(--bad);
}

.problems, .warnings {
  font: inherit;
  font-size: 0.9rem;
  white-space: pre-wrap;
}

/* A design warning (FR-5) is not a problem: amber, and the drawing stays live. */
.warnings {
  border-color: var(--mark);
  background: var(--warn-bg);
  color: var(--warn);
}

.internal-error p { margin: 0; font-weight: 600; }

.internal-error pre, #engine-error {
  margin: 0.4rem 0 0;
  max-height: 16rem;
  overflow: auto;
  padding: 0.5rem;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--ink);
  font-size: 0.8rem;
  white-space: pre-wrap;
}

.numbers {
  margin: 0;
  font: inherit;
  font-size: 0.88rem;
  line-height: 1.45;
  font-variant-numeric: tabular-nums;
  white-space: pre-wrap;  /* a line longer than its column (a narrow pane) wraps, not clips */
  columns: 3 15rem;
  column-gap: 2rem;
}

/* --- Engine status (FR-32): what shows follows <body data-engine> --------------------- */

.engine-status {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  color: var(--muted);
  background: rgb(255 255 255 / 0.85);
}

[data-engine="ready"] .engine-status { display: none; }

.when-loading, .when-failed, .when-ready { display: none; }
[data-engine="loading"] .when-loading,
[data-engine="failed"] .when-failed { display: block; }
[data-engine="ready"] .when-ready { display: block; }

.when-loading, .when-failed {
  width: min(28rem, 100%);
}

.when-loading p { margin: 0 0 0.4rem; }

#engine-progress {
  width: 100%;
  height: 0.6rem;
  accent-color: var(--accent);
}

.engine-failed {
  margin: 0 0 0.5rem;
  color: var(--bad);
  font-weight: 600;
}

.when-failed details { margin-top: 0.6rem; }

/* --- Guide (FR-33): a panel at the right, beside the inputs and the drawing; over the
   drawing pane on a narrow desktop, and over the whole screen on a phone (below) ------- */

.guide {
  width: 21rem;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-left: 1px solid var(--line);
  background: var(--paper);
}

.guide:focus { outline: none; }

.guide-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.35rem 0.5rem 0.35rem 0.9rem;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.guide-bar h2 { margin: 0; font-size: 1rem; }

#close-guide {
  padding: 0.1rem 0.55rem;
  font-size: 1.2rem;
  line-height: 1;
}

.guide-body {
  overflow: auto;
  overscroll-behavior: contain;  /* scrolling the guide never scrolls the page under it */
  padding: 0.6rem 0.9rem 1rem;
  font-size: 0.9rem;
  line-height: 1.45;
}

.guide-intro { margin: 0 0 0.6rem; color: var(--muted); }

.steps { margin: 0; padding-left: 1.4rem; }

.step { margin-bottom: 1rem; }
.step::marker { font-weight: 600; }
.step h3 { margin: 0 0 0.2rem; font-size: 0.95rem; }
.step p { margin: 0 0 0.4rem; }

.show-me {
  padding: 0.15rem 0.6rem;
  font-size: 0.85rem;
  border-color: var(--accent);
  color: var(--accent);
}

/* What a "Show me" points at, for 3 s (app.js). */
.highlight {
  outline: 3px solid var(--mark);
  outline-offset: 2px;
  box-shadow: 0 0 0 6px rgb(232 154 0 / 0.3);
  scroll-margin: 0.75rem;
}

/* --- Footer (FR-33): the versions and the links -------------------------------------- */

.footer {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.15rem 0.45rem;
  padding: 0.3rem 0.75rem;
  border-top: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  font-size: 0.78rem;
}

.footer a { color: var(--accent); }

/* --- Narrow desktops (FR-33; app.js's NARROW and PHONE name the same widths): the guide
   lies over the right of the drawing pane, which keeps its width ----------------------- */

@media (700px < width <= 1000px) {
  .guide {
    grid-area: drawing;
    justify-self: end;
    z-index: 5;
    width: min(21rem, 100%);
    box-shadow: -6px 0 16px rgb(0 0 0 / 0.18);
  }
}

/* --- Phones (FR-33) --------------------------------------------------------------------- */

@media (width <= 700px) {
  body { height: auto; min-height: 100vh; }

  .topbar { flex-wrap: wrap; gap: 0.4rem 1rem; padding: 0.5rem 0.75rem; }
  .actions { margin-left: 0; }

  .workbench {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: none;
    grid-template-areas: "drawing" "inputs";
  }

  .drawing { overflow: visible; }
  .view-bar { flex-wrap: wrap; row-gap: 0.25rem; }
  .views { flex: none; height: 50vh; }

  .inputs {
    overflow: visible;
    border-right: none;
    border-top: 1px solid var(--line);
  }

  .guide {
    position: fixed;
    inset: 0;
    z-index: 20;
    width: auto;
    border-left: none;
  }
}
