:root {
  color-scheme: light;
  font-family: "Noto Sans Thai", "Leelawadee UI", system-ui, sans-serif;
  color: var(--text);
  background: #FFFFFF;
  --canvas: #FFFFFF;
  --surface: #FFFFFF;
  --line: #d5dee7;
  --text: #202e3c;
  --muted: #4d6175;
  --chrome: #f3f6f9;
  --selected: #e4effa;
  --accent: #145a9c;
  --accent-on: #FFFFFF;
  --warning: #E5587A;
}
* { box-sizing: border-box; }
body { margin: 0; min-width: 320px; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; color: inherit; }
button:disabled { cursor: default; opacity: .42; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

.workspace {
  display: grid;
  grid-template: auto auto minmax(0, 1fr) auto / 192px minmax(0, 1fr) auto;
  min-height: 550px;
  height: 100dvh;
}
.viewport { grid-area: 3 / 2; position: relative; min-height: 0; min-width: 0; background: var(--canvas); overflow: hidden; }
canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
canvas:focus-visible { outline-offset: -3px; }
canvas.pan { cursor: grab; }
canvas.panning { cursor: grabbing; }

.top { grid-area: 1 / 1 / 2 / -1; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; line-height: 1.35; }
.brand div { min-width: 0; }
.brand-icon { position: relative; display: inline-block; width: 25px; height: 23px; margin-top: 2px; flex-shrink: 0; border: 1px solid var(--accent); }
.brand-icon::before, .brand-icon::after { position: absolute; content: ""; background: var(--accent); }
.brand-icon::before { width: 33px; height: 1px; bottom: 3px; left: -5px; }
.brand-icon::after { width: 1px; height: 31px; top: -5px; right: 4px; }
h1 { margin: 0; font-size: 18px; font-weight: 720; }
#source-name { display: block; max-width: min(60vw, 540px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 12px; }
.top-actions { display: flex; justify-content: end; gap: 8px; }
.view-tabs { display: flex; gap: 2px; padding: 3px; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; }
.view-tabs button { background: transparent; border: 0; border-radius: 4px; padding: 4px 9px; min-height: 32px; font-size: 13px; white-space: nowrap; }
.view-tabs button:hover:not(:disabled), .view-tabs button[aria-pressed=true] { background: var(--selected); color: var(--accent); }
.view-tabs button[aria-pressed=true] { font-weight: 720; }
.button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 33px; padding: 5px 12px; border: 1px solid transparent; border-radius: 7px; font-size: 12px; font-weight: 660; white-space: nowrap; }
.primary { background: var(--accent); color: var(--accent-on); border-color: var(--accent); }
.primary:hover { background: #104b82; }
.secondary { background: var(--surface); border-color: var(--line); }
.secondary:hover { background: var(--selected); border-color: var(--accent); }

.layers { grid-area: 3 / 1; padding: 13px 12px; border-right: 1px solid var(--line); background: var(--chrome); overflow: auto; }
.layer-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.layer-heading h2 { margin: 0; font-size: 15px; }
.layer-heading span, #source-note { color: var(--muted); font-size: 12px; }
#source-note { margin: 5px 0 10px; overflow-wrap: anywhere; }
.layer-controls { display: grid; gap: 3px; margin-block: 12px; }
.opacity-row { display: grid; grid-template-columns: 1fr auto; gap: 4px; align-items: center; margin-bottom: 12px; font-size: 12px; }
.opacity-row input { grid-column: 1 / -1; width: 100%; margin: 0; accent-color: var(--accent); }
.opacity-row output { font-variant-numeric: tabular-nums; }
.layer-row { display: flex; min-height: 34px; align-items: stretch; justify-content: space-between; font-size: 13px; border-radius: 4px; }
.layer-row:hover { background: var(--selected); }
.layer-row label { position: relative; color: var(--muted); display: flex; flex: 1; align-items: center; gap: 6px; padding: 4px; cursor: pointer; }
.layer-row label:has(input:checked) { opacity: 1; }
.layer-row input { position: absolute; left: 3px; margin: 0; width: 14px; height: 16px; opacity: 0; }
.layer-row label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: -1px; border-radius: 4px; }
.layer-row i { position: relative; width: 14px; height: 10px; pointer-events: none; border: 1px solid currentColor; border-radius: 70% 45%; margin-right: 2px; }
.layer-row input:checked + i::after { content: ""; position: absolute; width: 4px; height: 4px; border: 1px solid currentColor; border-radius: 50%; left: 4px; top: 2px; }
.layer-row button { border: 0; background: transparent; padding: 1px 5px; font-size: 19px; line-height: 1; }
.count { display: inline-block; padding: 0 5px; border-radius: 4px; color: #813049; background: #fcecf0; font-size: 11px; font-weight: 760; font-variant-numeric: tabular-nums; }

.plan-footer { grid-area: 2 / 1 / 3 / -1; display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; padding: 6px 12px; background: var(--chrome); border-bottom: 1px solid var(--line); }
.plan-footer .tool-row { order: 1; }
.plan-footer .status-summary { order: 2; }
.plan-footer .view-actions { order: 3; margin-inline-start: auto; }
.status-summary, .view-actions { display: flex; align-items: center; gap: 9px; }
.stats { display: grid; gap: 4px; margin: 0; font-size: 13px; }
.stats div { display: flex; justify-content: space-between; gap: 5px; }
.stats dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }
#snap-toggle, #snap-midpoint { background: transparent; border: 1px solid #0064F6; border-radius: 4px; font-size: 11px; padding: 3px 5px; white-space: nowrap; }
.tool-row { display: flex; align-items: center; gap: 4px; padding: 4px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
.tool-group { display: flex; align-items: center; gap: 4px; }
.history-tools { border-left: 1px solid var(--line); margin-left: 2px; padding-left: 3px; }
.tool { color: var(--accent); display: grid; place-items: center; width: 40px; height: 39px; padding: 1px; border: 1px solid transparent; border-radius: 7px; background: transparent; font-size: 22px; }
.tool:hover:not(:disabled) { border-color: var(--accent); }
.tool.active { color: var(--accent); background: var(--selected); }
.tool-icon { position: relative; display: grid; place-items: center; width: 20px; height: 18px; color: currentColor; }
.tool-icon::before, .tool-icon::after { position: absolute; content: ""; }
.icon-select::before { width: 13px; height: 16px; left: 4px; top: 0; background: currentColor; clip-path: polygon(0 0, 95% 62%, 59% 65%, 71% 96%, 55% 100%, 42% 72%, 17% 100%); }
.icon-pan { font-size: 21px; line-height: 18px; }
.icon-beam::before { top: 8px; left: 1px; width: 18px; border-top: 1.5px solid currentColor; transform: rotate(-38deg); }
.icon-column::before { top: 3px; left: 4px; width: 10px; height: 10px; border: 1.5px solid currentColor; border-radius: 1px; }
.icon-quick::before { left: 1px; right: 1px; top: 8px; border-top: 2px dashed currentColor; }
.icon-quick::after { inset: 4px 0; border-inline: 1.5px solid currentColor; }
.icon-reference::before { width: 18px; height: 7px; border: 1px solid currentColor; transform: rotate(-42deg); }
.icon-reference::after { width: 1px; height: 3px; left: 8px; top: 7px; background: currentColor; transform: rotate(-42deg); }
.icon-align::before { inset: 3px 4px; border: 1px solid currentColor; }
.icon-align::after { inset: 7px 8px; border: 1px solid currentColor; }

.view-actions { justify-content: end; }
.plan-unit { display: inline-flex; align-items: center; gap: 5px; padding: 2px 5px 2px 8px; min-height: 33px; white-space: nowrap; color: #000000; border: 1px solid #0064F6; border-radius: 7px; background: #FFFFFF; font-size: 12px; }
.plan-unit select { background: #FFFFFF; color: #000000; border: 0; min-height: 24px; cursor: pointer; }
.zoom { display: flex; min-height: 33px; align-items: stretch; background: #FFFFFF; border: 1px solid #0064F6; border-radius: 7px; }
.zoom button { border: 0; padding: 3px 8px; background: transparent; font-size: 16px; }
.zoom button:hover { background: #FFFFFF; }
#zoom-fit { min-width: 52px; font-size: 12px; font-variant-numeric: tabular-nums; }
.expand { padding: 3px 8px; font-size: 18px; }
.review-button { width: 100%; justify-content: space-between; background: var(--surface); border-color: var(--line); }
.review-button:hover { border-color: var(--accent); background: var(--selected); }
.plan-meta { grid-area: 4 / 1 / 5 / -1; display: flex; flex-wrap: wrap; gap: 4px 12px; padding: 5px 12px; border-top: 1px solid var(--line); background: var(--chrome); color: var(--muted); font-size: 12px; }
#coords { margin-left: auto; font-variant-numeric: tabular-nums; }
#feedback { order: 1; }
#feedback:empty, #import-progress:empty, .edit-error:empty { display: none; }
#feedback.error, .edit-error, .form-error {  border-left: 3px solid var(--warning); padding-left: 5px; }

.canvas-message { position: absolute; z-index: 1; top: 10px; left: 50%; width: max-content; max-width: calc(100% - 24px); transform: translateX(-50%); padding: 5px 10px; border: 1px solid var(--line); background: var(--surface); border-radius: 5px; font-size: 12px; text-align: center; pointer-events: none; }
.selection-card, .beam-context { position: absolute; z-index: 2; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.selection-card { width: 218px; padding: 10px; font-size: 12px; line-height: 1.25; }
.selection-card::after { content: ""; width: 9px; height: 9px; position: absolute; left: var(--anchor-x, 25px); bottom: -6px; transform: rotate(45deg); border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); background: var(--surface); }
.selection-card[data-side="below"]::after { top: -6px; bottom: auto; transform: rotate(225deg); }
.selection-card header { display: flex; align-items: center; gap: 6px; }
.selection-card header i { display: inline-block; width: 9px; height: 9px; flex: none; background: var(--accent); }
.selection-card header code { margin-left: auto; white-space: nowrap; }
.selection-card header strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.selection-card p { margin: 5px 0 0; color: var(--text); overflow-wrap: anywhere; }
.selection-card .readout { display: flex; align-items: center; gap: 7px; margin: 4px 0; font-variant-numeric: tabular-nums; }
.selection-card .readout > strong { font-size: 19px; white-space: nowrap; }
.selection-card .card-source { border-top: 1px solid var(--line); padding-top: 4px; font-size: 11px; }
.selection-card .card-actions { display: flex; gap: 6px; margin-top: 6px; }
.selection-card .button { flex: 1; min-height: 28px; padding: 3px 4px; font-size: 11px; }
.beam-context { z-index: 5; min-width: 172px; padding: 5px; border-radius: 6px; }
.context-title { padding: 5px 8px; color: var(--text); border-bottom: 1px solid var(--line); font-size: 11px; }
.beam-context button { display: block; width: 100%; border: 0; border-radius: 4px; padding: 6px 8px; background: transparent; text-align: left; font-size: 13px; }
.beam-context button:hover, .beam-context button:focus-visible { background: var(--selected); }

.sidebar { grid-area: 3 / 3; width: clamp(300px, 27vw, 380px); display: flex; flex-direction: column; min-height: 0; background: var(--chrome); border-left: 1px solid var(--line); }
.tabs { display: flex; min-height: 40px; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; min-width: 0; padding: 5px; background: transparent; border: 0; border-bottom: 2px solid transparent; font-size: 12px; white-space: nowrap; }
.tabs button.active { border-color: var(--accent); font-weight: 700; }
#close-panel { flex: 0 0 35px; font-size: 17px; }
#close-panel:hover { background: #FFFFFF; }
#review-count {  font-variant-numeric: tabular-nums; }
.panel { overflow-y: auto; flex: 1; padding: 15px; }
.panel h2, dialog h2 { margin: 0 0 6px; font-size: 19px; }
.panel h3 { margin: 0; font-size: 14px; }
.panel p, dialog p { margin: 4px 0 10px; color: var(--text); font-size: 12px; line-height: 1.55; }
.panel .beam-name { margin: 0; font-size: 26px; }
.panel .beam-location { margin: 4px 0 7px; }
.span-title { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.property-caption { display: flex; gap: 6px; color: var(--text); font-size: 12px; }
.property-section { margin-top: 12px; padding-top: 11px; border-top: 1px solid var(--line); }
.dimension-value { display: flex; align-items: baseline; flex-wrap: wrap; gap: 6px; font-size: 23px; font-weight: 730; }
.dimension-value span { color: var(--text); font-size: 11px; font-weight: 500; }
.code { font: 11px ui-monospace, monospace; }
.chip { display: inline-block; background: var(--surface); padding: 2px 5px; border: 1px solid var(--warning); border-radius: 4px; font-size: 10px; font-weight: 650; white-space: nowrap; }
.chip.ok { background: var(--surface); border-color: var(--accent); }
.block, .edit-fields { margin: 10px 0 0; border: 1px solid var(--line); border-radius: 5px; padding: 9px; background: var(--surface); }
.fields, .endpoints, .list { display: grid; gap: 6px; }
.endpoints { margin-top: 7px; }
.field { min-width: 0; display: flex; flex-direction: column; gap: 4px; font-size: 12px; font-weight: 650; }
.field input, .field select { min-width: 0; width: 100%; min-height: 34px; border: 1px solid #8192a3; border-radius: 4px; padding: 4px 7px; background: var(--surface); }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.inline { display: flex; align-items: end; flex-wrap: wrap; gap: 6px; }
.inline .field { flex: 1; }
.edit-fields .inline { justify-content: end; }
.endpoint, .list-item { width: 100%; display: flex; justify-content: space-between; align-items: center; gap: 7px; text-align: left; background: var(--surface); border: 1px solid var(--line); border-radius: 4px; font-size: 12px; padding: 6px 8px; }
.endpoint:hover, .endpoint.active, .list-item:hover, .list-item.active { background: var(--selected); border-color: var(--accent); }
.endpoint strong, .endpoint small, .list-item strong, .list-item small { display: block; }
.endpoint small, .list-item small { margin-top: 2px; font-size: 11px; }
.help { color: var(--text); font-size: 11px; }
.search { margin: 8px 0; }
.filters { display: flex; flex-wrap: wrap; gap: 4px; margin: 8px 0; }
.review-notice { margin: 8px 0 12px; padding: 9px; border: 1px solid var(--line); border-radius: 5px; background: var(--surface); }
.filter { padding: 4px 5px; background: #FFFFFF; border: 1px solid #0064F6; border-radius: 4px; font-size: 11px; }
.filter[aria-pressed=true] { box-shadow: inset 0 -2px var(--accent); font-weight: 700; }

dialog { color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 20px; width: min(520px, calc(100% - 28px)); max-height: calc(100dvh - 28px); }
dialog::backdrop { background: #122538; opacity: .48; }
dialog form { display: grid; gap: 8px; }
dialog .check { display: flex; align-items: center; gap: 6px; font-size: 13px; }
#cad-settings { margin: 2px 0; min-width: 0; padding: 8px 10px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--chrome); }
#cad-settings legend { padding: 0 5px; font-weight: 700; }
#cad-settings p { margin: 0 0 4px; }
.measurement-evidence { margin: 6px 0; padding-left: 20px; font-size: 12px; }
input::placeholder { color: var(--muted); opacity: 1; }
dialog input[type=checkbox] { accent-color: var(--accent); margin: 0; }
.dialog-actions { display: flex; justify-content: end; gap: 7px; }

@media (max-width: 1050px) {
  .workspace { grid-template-columns: 156px minmax(0, 1fr) auto; }
  .tool-group { gap: 1px; }
  .tool { width: 36px; }
}
@media (max-width: 780px) {
  .workspace { height: auto; grid-template: auto auto auto minmax(440px, 57dvh) auto auto / minmax(0, 1fr); }
  .top { flex-wrap: wrap; }
  .layers { grid-area: 3 / 1; display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; border-right: 0; border-bottom: 1px solid var(--line); padding: 6px 10px; }
  .layer-heading { gap: 6px; }
  .layer-controls, .stats { display: flex; flex-wrap: wrap; gap: 2px 8px; margin: 0; }
  .layers .review-button { width: auto; }
  #source-note { margin: 0; }
  .viewport { grid-area: 4 / 1; }
  .sidebar { grid-area: 5 / 1; width: 100%; max-height: 54dvh; border-left: 0; border-top: 1px solid var(--line); }
  .plan-meta { grid-area: 6 / 1; }
  .plan-footer .view-actions { margin-inline-start: 0; }
}
@media (max-width: 460px) {
  .top, .plan-footer { padding-inline: 8px; }
  .top-actions { flex-wrap: wrap; justify-content: start; }
  .plan-footer .tool-row { order: 0; }
  .plan-footer .view-actions { flex-wrap: wrap; }
  .tool { width: 32px; }
}

#snap-midpoint { border-color: #000000; }
#snap-midpoint[aria-pressed="true"] { background: #FFFFFF; border-color: #0064F6; box-shadow: inset 0 0 0 1px #0064F6; }
 #snap-midpoint[aria-pressed="true"] span { color: #0064F6; }
