:root {
  color-scheme: light dark;
}

* { box-sizing: border-box; }

:focus-visible { outline: 2px solid #0b6bcb; outline-offset: 2px; }

button.linklike {
  font: inherit;
  color: #0b6bcb;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-decoration: underline;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: #1a2233;
  background: #f5f7fa;
  line-height: 1.5;
}

.wrap { max-width: 1280px; margin: 0 auto; padding: 24px 16px 48px; }

h1 { margin: 0 0 8px; font-size: 28px; }
.lede { color: #4a5568; margin: 0 0 20px; }

/* Three columns: controls | 3D canvas | results */
.tool {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr) 300px;
  gap: 20px;
  align-items: start;
}

/* --- left: unified control panel --- */
form.controls {
  display: flex;
  flex-direction: column;
  background: #fff;
  border: 1px solid #d7dee8;
  border-radius: 10px;
  padding: 6px 16px 10px;
  position: sticky;
  top: 16px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
}

form.controls fieldset {
  border: none;
  border-bottom: 1px solid #eef2f6;
  padding: 12px 0;
  margin: 0;
}
form.controls fieldset:last-child { border-bottom: none; }
fieldset[hidden] { display: none; }

form.controls legend {
  display: block;
  width: 100%;
  padding: 0;
  margin: 0 0 4px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: #64748b;
}

label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; margin: 8px 0; }
label[hidden] { display: none; }

input[type="number"], select {
  font: inherit;
  padding: 6px 8px;
  border: 1px solid #c3ccd8;
  border-radius: 6px;
  background: #fff;
}

label > input[type="number"] { width: 100%; }
label > select { width: 100%; }

/* unit radios inline */
.units label { flex-direction: row; align-items: center; }

.section-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px; }
.presets select { width: 100%; }

/* --- center: 3D canvas --- */
.canvas .diagram { padding: 12px; }
#beam-3d { position: relative; width: 100%; height: 560px; }
#beam-3d canvas { display: block; }

/* --- right: results --- */
.results {
  position: sticky;
  top: 16px;
  background: #fff;
  border: 1px solid #d7dee8;
  border-radius: 10px;
  padding: 14px 16px;
}

.results h2 { margin: 0 0 10px; font-size: 18px; }
.results dl { margin: 0; }
.results dt { font-size: 12px; color: #64748b; margin-top: 8px; }
.results dd { margin: 0; font-size: 16px; font-weight: 600; }
.results dd.passes { color: #15803d; }
.results dd.exceeds { color: #b91c1c; }

/* --- deck joist span tool --- */
#schema svg { width: 100%; height: auto; display: block; }
#schema .decking { fill: #334155; }
#schema .joist, #schema .post, #schema .beam { fill: #94a3b8; transition: fill 0.2s; }
#schema[data-mode="joist"] .joist { fill: #0b6bcb; }
#schema[data-mode="beam"] .beam { fill: #0b6bcb; }
#schema .schema-label { fill: #475569; font-size: 12px; font-weight: 600; }
.results .passes { color: #15803d; font-weight: 600; }
.results .exceeds { color: #b91c1c; font-weight: 600; }
.results .note { color: #475569; font-size: 13px; }
form.controls .hint { color: #64748b; font-size: 13px; margin: 0 0 8px; }
.results .error { color: #b91c1c; }

/* --- 3D card chrome --- */
.diagram {
  background: #fff;
  border: 1px solid #d7dee8;
  border-radius: 10px;
}
.diagram h2 { margin: 0; font-size: 14px; }
.diagram-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 8px; }
.view-toggle { display: flex; gap: 6px; }
.view-toggle label { flex-direction: row; align-items: center; gap: 4px; margin: 0; font-size: 12px; }
.diagram-note { margin: 8px 0 0; font-size: 12px; color: #64748b; }

.beam-tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(17, 24, 39, 0.92);
  color: #fff;
  font-size: 12px;
  line-height: 1.4;
  padding: 6px 9px;
  border-radius: 6px;
  z-index: 10;
  white-space: nowrap;
}
.beam-tooltip[hidden] { display: none; }

/* --- content below the tool --- */
.how { max-width: 900px; margin: 24px auto 0; background: #fff; border: 1px solid #d7dee8; border-radius: 10px; padding: 16px; }
.how table { border-collapse: collapse; width: 100%; font-size: 14px; }
.how th, .how td { border: 1px solid #e2e8f0; padding: 6px 10px; text-align: left; }
.how th { background: #f1f5f9; }
.how ol, .how ul { padding-left: 20px; }
.how li { margin: 4px 0; }

.disclaimer { max-width: 900px; margin: 24px auto 0; font-size: 12px; color: #64748b; }

.legal-links { margin-top: 10px; }
.legal-links a { color: #0b6bcb; }
@media (prefers-color-scheme: dark) {
  .legal-links a { color: #7db4ec; }
}

details { margin: 8px 0; }
details summary { cursor: pointer; font-weight: 600; }

#fatal-error {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: #b91c1c;
  color: #fff;
  padding: 10px 16px;
  font-size: 14px;
  z-index: 100;
}
#fatal-error[hidden] { display: none; }
.error-banner { background: #b91c1c; color: #fff; padding: 10px 16px; }

#embed-code {
  width: 100%;
  font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
  padding: 8px;
  border: 1px solid #c3ccd8;
  border-radius: 6px;
  white-space: pre;
}

#copy-embed {
  margin-top: 8px;
  padding: 6px 14px;
  font: inherit;
  border: 1px solid #0b6bcb;
  background: #0b6bcb;
  color: #fff;
  border-radius: 6px;
  cursor: pointer;
}

#copy-link.secondary {
  margin-top: 8px;
  margin-left: 8px;
  padding: 6px 14px;
  font: inherit;
  border: 1px solid #0b6bcb;
  background: #fff;
  color: #0b6bcb;
  border-radius: 6px;
  cursor: pointer;
}

.copy-note { margin-left: 10px; font-size: 13px; color: #15803d; }

select:disabled { background: #f1f5f9; color: #64748b; cursor: not-allowed; }

/* --- results error list and hints --- */
.results .errors { margin: 8px 0 0; padding-left: 18px; color: #b91c1c; font-size: 13px; }
.results .errors li { margin: 3px 0; }
.results .hint { color: #64748b; font-size: 13px; }

/* --- 3D colour legend --- */
.beam-legend {
  display: flex;
  gap: 12px;
  align-items: center;
  font-size: 12px;
  color: #475569;
  margin: 6px 0 0;
}
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; }

/* --- embed (iframe) — form | canvas+results, so the advertised height holds --- */
.embed .wrap { max-width: 880px; }
.embed .tool {
  grid-template-columns: 320px minmax(0, 1fr);
  grid-template-areas: 'form canvas' 'form results';
}
.embed form.controls {
  grid-area: form;
  position: static;
  max-height: none;
  overflow: visible;
  padding: 4px 12px 8px;
}
.embed form.controls fieldset { padding: 8px 0; }
.embed form.controls label { margin: 5px 0; }
.embed .canvas { grid-area: canvas; }
.embed .results { grid-area: results; position: static; }
.embed #beam-3d { height: 480px; }
@media (max-width: 640px) {
  .embed .tool { grid-template-columns: 1fr; grid-template-areas: none; }
}

/* --- responsive: stack --- */
@media (max-width: 900px) {
  .tool { grid-template-columns: 1fr; }
  form.controls { position: static; }
  /* The answer right after the controls — the 3D view comes last on mobile. */
  .tool .canvas { order: 3; }
  .results { position: static; order: 2; }
  #beam-3d { height: 320px; touch-action: pan-y; }
  /* ≥16px so iOS Safari doesn't zoom on focus */
  input[type="number"], select { font-size: 16px; }
}

/* --- dark mode (prefers-color-scheme) --- */
@media (prefers-color-scheme: dark) {
  body { background: #141a24; color: #dbe2ec; }
  form.controls, .results, .diagram, .how { background: #1d2530; border-color: #2c3a4c; }
  form.controls fieldset { border-bottom-color: #2a3646; }
  form.controls legend { color: #8fa3bd; }
  .lede, .diagram-note, .results dt, .disclaimer, form.controls .hint, .results .hint, .results .note { color: #93a1b5; }
  input[type="number"], select, #embed-code {
    background: #141a24;
    color: #dbe2ec;
    border-color: #3d4c60;
  }
  select:disabled { background: #232d3b; color: #6d7b8d; }
  .how th { background: #232d3b; }
  .how th, .how td { border-color: #2c3a4c; }
  .results dd.passes, .results .passes { color: #34d399; }
  .results dd.exceeds, .results .exceeds, .results .error, .error { color: #f87171; }
  .beam-legend { color: #93a1b5; }
  #copy-link.secondary { background: #1d2530; color: #7db4ec; border-color: #7db4ec; }
  a { color: #7db4ec; }
  .how a { color: #7db4ec; }
}
