/*
 * styles.css - plain system fonts, two colour schemes (light and dark) and no
 * decorative effects beyond subtle hover feedback (spec section 8.1).
 *
 * Every colour is a variable so the dark scheme is one block of overrides:
 *   :root                - the light palette (also the fallback)
 *   :root[data-theme="dark"] - the dark palette
 * `shared/ui/theme.js` sets `data-theme` on <html> and reads the `--canvas-*`
 * variables back for the diagram colours, so the canvas and the panels always
 * agree.
 */

:root {
  color-scheme: light;

  --bg: #f6f6f7;
  --panel: #ffffff;
  --line: #d9d9de;
  --ink: #1d1d21;
  --ink-soft: #5c5c66;
  --ink-disabled: #a8a8b0;
  --accent: #2f5fd8;
  --accent-strong: #2850bd;
  --accent-soft: #e8eeff;
  --on-accent: #ffffff;
  --warn: #b4441f;
  --warn-soft: #fdeee8;
  --ok: #1f7a5c;
  --member: #3a3a42;
  --moment: #c0392b;
  --shear: #1f7a5c;
  --axial: #7a4fb4;
  --grid: #e6e6ea;
  --grid-strong: #d2d2d8;
  --node: #1d1d21;
  --selected: #ff8a00;
  --snap: #d13b8a;

  /* Surfaces that had a hard-coded light colour before the dark scheme. */
  --chip-bg: #ececf0;
  --chip-done-bg: #dff0e6;
  --chip-done-ink: #1f7a5c;
  --hint-bg: #f4f6fb;
  --hint-border: #e2e7f2;
  --table-head: #f0f0f3;
  --table-alt: #f7f7f9;
  --floating-bg: rgba(255, 255, 255, 0.96);
  --toast-bg: #2b2b31;
  --toast-ink: #ffffff;
  --scrim: rgba(20, 20, 25, 0.32);

  /* Canvas: read back by shared/ui/theme.js#canvasPalette. */
  --canvas-bg: #fbfbfc;
  --canvas-surface: #ffffff;
  --canvas-ink: #1d1d21;
  --canvas-member: #3a3a42;
  --canvas-member-selected: #ff8a00;
  --canvas-member-hover: #2f5fd8;
  --canvas-node: #1d1d21;
  --canvas-support: #1f7a5c;
  --canvas-moment: #c0392b;
  --canvas-shear: #1f7a5c;
  --canvas-axial: #7a4fb4;
  --canvas-load: #b4441f;
  --canvas-ground: #8a8574;
  --canvas-ground-hatch: #b3ae9c;
  --canvas-grid: #e8e8ec;
  --canvas-grid-strong: #d2d2d8;
  --canvas-snap-node: #d13b8a;
  --canvas-snap-member: #7a4fb4;
  --canvas-snap-grid: #8a8a94;
  --canvas-snap-align: #2f5fd8;
  --canvas-preview: #2f5fd8;
  --canvas-moment-fill: rgba(192, 57, 43, 0.12);
  --canvas-shear-fill: rgba(31, 122, 92, 0.1);
  --canvas-select-fill: rgba(47, 95, 216, 0.1);
}

/*
 * Dark scheme: dark backgrounds, light text. The accent and the force colours
 * are lightened rather than reused, because the light palette's mid-tones are
 * too dark to read on a near-black background.
 */
:root[data-theme="dark"] {
  color-scheme: dark;

  --bg: #14161a;
  --panel: #1c1f25;
  --line: #2f333c;
  --ink: #f2f4f8;
  --ink-soft: #a8b0bf;
  --ink-disabled: #6b7280;
  --accent: #6d97ff;
  --accent-strong: #8fb0ff;
  --accent-soft: #26314a;
  --on-accent: #101420;
  --warn: #ff9a6a;
  --warn-soft: #3a241b;
  --ok: #5ad0a8;
  --member: #c9ccd6;
  --moment: #ff7b6b;
  --shear: #5ad0a8;
  --axial: #b98cf0;
  --grid: #2a2b31;
  --grid-strong: #3a3b44;
  --node: #e6e8ee;
  --selected: #ff9f2e;
  --snap: #ff7ab8;

  --chip-bg: #2a2e36;
  --chip-done-bg: #173a2b;
  --chip-done-ink: #6fd3a5;
  --hint-bg: #1a2029;
  --hint-border: #2a3240;
  --table-head: #23262d;
  --table-alt: #1f232a;
  --floating-bg: rgba(28, 31, 37, 0.96);
  --toast-bg: #eef1f6;
  --toast-ink: #14161a;
  --scrim: rgba(0, 0, 0, 0.55);

  --canvas-bg: #101318;
  --canvas-surface: #1c1f25;
  --canvas-ink: #f2f4f8;
  --canvas-member: #c9ccd6;
  --canvas-member-selected: #ff9f2e;
  --canvas-member-hover: #8fb0ff;
  --canvas-node: #e6e8ee;
  --canvas-support: #4cc39a;
  --canvas-moment: #ff7b6b;
  --canvas-shear: #5ad0a8;
  --canvas-axial: #b98cf0;
  --canvas-load: #ff9a6a;
  --canvas-ground: #8f8a76;
  --canvas-ground-hatch: #5f5b4e;
  --canvas-grid: #2a2b31;
  --canvas-grid-strong: #3a3b44;
  --canvas-snap-node: #ff7ab8;
  --canvas-snap-member: #b98cf0;
  --canvas-snap-grid: #7c8494;
  --canvas-snap-align: #8fb0ff;
  --canvas-preview: #8fb0ff;
  --canvas-moment-fill: rgba(255, 123, 107, 0.18);
  --canvas-shear-fill: rgba(90, 208, 168, 0.16);
  --canvas-select-fill: rgba(143, 176, 255, 0.18);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 13px/1.45 "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
}

button, select, input {
  font: inherit;
  color: inherit;
}

button {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 4px 9px;
  cursor: pointer;
}

button:hover { border-color: var(--accent); }
button:disabled { color: var(--ink-disabled); cursor: not-allowed; border-color: var(--line); }
button.active { background: var(--accent-soft); border-color: var(--accent); }

select, input[type="text"], input[type="number"] {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 3px 6px;
}

input[type="text"]:focus, input[type="number"]:focus, select:focus {
  outline: 2px solid var(--accent-soft);
  border-color: var(--accent);
}

.hidden { display: none !important; }

/* -------------------------------------------------------------- boot splash */

.boot-overlay {
  position: fixed;
  inset: 0;
  z-index: 90;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.boot-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 24px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink-soft);
  font-size: 13px;
}

.boot-spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: boot-spin 0.9s linear infinite;
}

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

/* The layout is laid out before the first frame, so keep it invisible until
   the language file and the sample project have been fetched. */
#app.booting { visibility: hidden; }

/* ------------------------------------------------------------------ layout */

#app {
  display: grid;
  grid-template-columns: 290px 1fr;
  grid-template-rows: auto 1fr auto auto;
  grid-template-areas:
    "top    top"
    "stages view"
    "stages dock"
    "status status";
  height: 100vh;
}

#topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.brand-title { font-weight: 600; }
.brand-sub { color: var(--ink-soft); margin-left: 10px; font-size: 12px; }
.topbar-actions { display: flex; gap: 6px; align-items: center; }

/* --------------------------------------------------------- mode switch */

.mode-switch { display: flex; border: 1px solid var(--line); border-radius: 5px; overflow: hidden; }
.mode-switch button {
  border: none;
  border-radius: 0;
  padding: 5px 12px;
  background: var(--panel);
  color: var(--ink-soft);
}
.mode-switch button + button { border-left: 1px solid var(--line); }
.mode-switch button.active { background: var(--accent); color: var(--on-accent); }

#stages {
  grid-area: stages;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--panel);
  border-right: 1px solid var(--line);
}

/* ---------------------------------------------------------- stage stepper */

#stageNav { display: flex; gap: 4px; padding: 8px 8px 6px; border-bottom: 1px solid var(--line); }

.stage-tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 6px 7px;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--panel);
  cursor: pointer;
  text-align: left;
}
.stage-tab .stage-index {
  width: 17px;
  height: 17px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--chip-bg);
  color: var(--ink-soft);
  font-size: 11px;
  font-weight: 600;
}
.stage-tab .stage-label { font-size: 11px; color: var(--ink-soft); line-height: 1.25; }
.stage-tab:hover { border-color: var(--accent); }
.stage-tab.done .stage-index { background: var(--chip-done-bg); color: var(--chip-done-ink); }
.stage-tab.active { background: var(--accent-soft); border-color: var(--accent); }
.stage-tab.active .stage-index { background: var(--accent); color: var(--on-accent); }
.stage-tab.active .stage-label { color: var(--ink); font-weight: 600; }

.stage-hint {
  color: var(--ink-soft);
  font-size: 11px;
  line-height: 1.5;
  background: var(--hint-bg);
  border: 1px solid var(--hint-border);
  border-radius: 5px;
  padding: 6px 8px;
  margin: 8px 0 4px;
}

.stage-body { padding: 4px 8px; overflow-y: auto; flex: 1; }

.stage-nav-buttons {
  display: flex;
  gap: 6px;
  padding: 8px;
  border-top: 1px solid var(--line);
}
.stage-nav-buttons button { flex: 1; padding: 6px 8px; }
.stage-nav-buttons .nav-next.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}
.stage-nav-buttons .nav-next.primary:hover { background: var(--accent-strong); }

#viewport {
  grid-area: view;
  position: relative;
  overflow: hidden;
  background: var(--canvas-bg);
}

#canvas { display: block; width: 100%; height: 100%; cursor: crosshair; }

#processDock {
  grid-area: dock;
  background: var(--panel);
  border-top: 1px solid var(--line);
  max-height: 32vh;
  display: flex;
  flex-direction: column;
}

.dock-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 10px;
  border-bottom: 1px solid var(--line);
  font-weight: 600;
}
.dock-body { overflow: auto; padding: 8px 10px; }

#statusbar {
  grid-area: status;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 4px 12px;
  background: var(--panel);
  border-top: 1px solid var(--line);
  font-size: 12px;
  color: var(--ink-soft);
}
#statusbar .spacer { flex: 1; }
#statusState { font-weight: 600; }
#statusState.failed { color: var(--warn); }

/* --------------------------------------------------------------- floating */

.floating {
  position: absolute;
  background: var(--floating-bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
}

#resultSelector {
  top: 10px;
  right: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 210px;
}
#resultSelector label { display: flex; flex-direction: column; gap: 2px; font-size: 12px; }
#resultSelector select, #resultSelector input { width: 100%; }
/* The load toggle is part of the same stack but reads as a single line. */
#resultSelector .check-row { flex-direction: row; align-items: center; gap: 6px; }
#resultSelector .check-row input { width: auto; }
.check-row { display: flex; align-items: center; gap: 6px; }
.check-row input { width: auto; }

#inputBar {
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
}
#inputBar input { width: 110px; }
#inputBar .unit { color: var(--ink-soft); }
#inputBar .hint { color: var(--ink-soft); font-size: 11px; }

#toast {
  left: 50%;
  top: 12px;
  transform: translateX(-50%);
  background: var(--toast-bg);
  color: var(--toast-ink);
  border: none;
  padding: 6px 12px;
  border-radius: 14px;
  font-size: 12px;
}

/* ---------------------------------------------------------------- overlays */

.overlay {
  position: fixed;
  inset: 0;
  background: var(--scrim);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
}

.overlay-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  min-width: 320px;
  max-width: 90vw;
  max-height: 84vh;
  display: flex;
  flex-direction: column;
}
.overlay-panel.wide { min-width: 620px; }

.overlay-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  font-weight: 600;
}
.overlay-body { padding: 10px 12px; overflow: auto; }

.context-menu {
  position: fixed;
  z-index: 60;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12);
  min-width: 170px;
}
.context-menu button {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: none;
  padding: 5px 8px;
  border-radius: 4px;
}
.context-menu button:hover { background: var(--accent-soft); }

/* ------------------------------------------------------- simple mode wizard */

/* One decision per screen, with controls that are easy to hit with a thumb. */
.stepper { display: flex; align-items: center; gap: 4px; }
.stepper .step { width: 38px; font-size: 16px; padding: 4px 0; text-align: center; }
.stepper .span-count-input { width: 56px; text-align: center; }

.chip-row { display: flex; flex-wrap: wrap; gap: 4px; margin: 4px 0; }
.chip {
  padding: 6px 10px;
  min-width: 38px;
  border-radius: 16px;
  background: var(--panel);
}
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

.length-rows { display: flex; flex-direction: column; gap: 4px; }
.length-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.length-label { color: var(--ink-soft); }
.length-input { width: 96px; text-align: right; }

.support-rows { display: flex; flex-direction: column; gap: 6px; }
.support-row { display: flex; align-items: center; gap: 6px; }
.support-row .support-label { flex: 1; color: var(--ink-soft); font-size: 12px; }
.support-row select { flex: 0 0 96px; }
.support-row .remove, .support-row .restore { flex: 0 0 32px; padding: 4px 0; text-align: center; }
.support-row .restore { border-color: var(--accent); color: var(--accent); }

.load-label { flex: 1; }

button.wide { flex: 1; padding: 8px 10px; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
button.primary:hover { background: var(--accent-strong); }
.nav-restart { flex: 1; padding: 6px 8px; }

/* ------------------------------------------------------------ panel parts */

.tool-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px; margin-bottom: 8px; }
.tool-grid button { text-align: left; }
.tool-grid button .key { color: var(--ink-soft); font-size: 11px; }

.field { display: flex; align-items: center; justify-content: space-between; gap: 6px; margin: 3px 0; }
.field > span { color: var(--ink-soft); }
.field input, .field select { width: 118px; }
.field input[type="checkbox"] { width: auto; }

.group-box { border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; margin-bottom: 8px; }
.group-box h3 { font-size: 12px; margin: 0 0 5px; text-transform: uppercase; color: var(--ink-soft); letter-spacing: 0.03em; }

.row-actions { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 5px; }

.hint-text { color: var(--ink-soft); font-size: 11px; margin: 4px 0; }
.empty-text { color: var(--ink-soft); font-style: italic; }

.object-list { list-style: none; margin: 4px 0 0; padding: 0; max-height: 150px; overflow: auto; }
.object-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  padding: 2px 4px;
  border-radius: 3px;
  cursor: pointer;
}
.object-list li:hover { background: var(--accent-soft); }
.object-list li.selected { background: var(--accent-soft); font-weight: 600; }
.object-list li .remove { color: var(--ink-soft); border: none; background: none; cursor: pointer; }

.gate-note { color: var(--warn); font-size: 11px; margin: 2px 0 0; }

/* ---------------------------------------------------------------- results */

table.process-table {
  border-collapse: collapse;
  font-size: 12px;
  margin-bottom: 14px;
  width: 100%;
}
table.process-table caption {
  text-align: left;
  font-weight: 600;
  padding: 4px 0;
}
table.process-table th, table.process-table td {
  border: 1px solid var(--line);
  padding: 2px 6px;
  text-align: right;
  white-space: nowrap;
}
table.process-table th { background: var(--table-head); text-align: center; font-weight: 600; }
table.process-table td:first-child, table.process-table th:first-child { text-align: left; }
table.process-table tr.final td { font-weight: 600; background: var(--table-alt); }

.check-list { list-style: none; padding: 0; margin: 6px 0 0; }
.check-list li { padding: 1px 0; }
.check-list li.ok::before { content: "✓ "; color: var(--ok); }
.check-list li.failed::before { content: "✗ "; color: var(--warn); }

.warning-list { list-style: none; padding: 0; margin: 6px 0 0; }
.warning-list li {
  background: var(--warn-soft);
  border-left: 3px solid var(--warn);
  padding: 4px 8px;
  margin-bottom: 3px;
  color: var(--warn);
}

.symbolic-note { color: var(--accent); font-size: 11px; }

/* ------------------------------------------------------------------- help */

.help-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.help-grid h3 { font-size: 12px; text-transform: uppercase; color: var(--ink-soft); margin: 0 0 6px; }
.help-grid ul { margin: 0; padding-left: 18px; }
.help-grid li { margin-bottom: 3px; }
.help-priority { counter-reset: snap; list-style: none; padding: 0; }

/* ------------------------------------------------------------------ phones */

/*
 * Below this width the sidebar would eat the screen, so the canvas moves to
 * the top and the panel becomes a bottom sheet. This is what simple mode was
 * built for, but the full editor benefits from it too.
 */
@media (max-width: 820px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(38vh, 1fr) auto auto;
    grid-template-areas:
      "top"
      "view"
      "stages"
      "status";
    height: 100dvh;
  }

  #topbar { padding: 6px 10px; gap: 8px; }
  .brand-sub { display: none; }
  .brand-title { font-size: 12px; }
  .mode-switch button { padding: 5px 9px; font-size: 12px; }
  /* Project files are a desktop concern; the wizard never needs them. */
  #buttonNew, #buttonOpen, #buttonSave { display: none; }

  #stages {
    border-right: none;
    border-top: 1px solid var(--line);
    max-height: 54vh;
    min-height: 30vh;
  }
  .stage-body { min-height: 0; padding: 4px 10px 10px; }
  #processDock { max-height: 26vh; }

  #statusbar { gap: 10px; font-size: 11px; white-space: nowrap; overflow: hidden; }
  #statusUnits { display: none; }

  /* Thumb-sized targets and no iOS zoom-on-focus (inputs need >= 16px). */
  button { padding: 7px 11px; }
  .stage-nav-buttons button { padding: 10px 8px; }
  .stage-tab { padding: 6px 5px; }
  .stage-tab .stage-label { font-size: 10px; }
  select, input[type="text"], input[type="number"] { font-size: 16px; padding: 6px 8px; }
  .field input, .field select { width: 132px; }
  #canvas { touch-action: none; }
  .overlay-panel.wide { min-width: 0; width: 96vw; }
  .help-grid { grid-template-columns: 1fr; }
}

/* Dragging the canvas must never scroll the page - on a touch laptop too. */
@media (pointer: coarse) {
  body.mode-simple #canvas { touch-action: none; }
}
