:root {
  color-scheme: dark;
  --bg: #071019;
  --bg-radial: rgba(53, 208, 186, 0.13);
  --surface: #0c1823;
  --surface-2: #112231;
  --surface-3: #172b3a;
  --border: #233847;
  --text: #eef7f6;
  --muted: #96aaa9;
  --teal: #35d0ba;
  --teal-dark: #143f3c;
  --red: #ff5b64;
  --amber: #f4be5b;
  --shadow: 0 18px 45px rgba(0, 0, 0, 0.25);

  --header-bg: rgba(7, 16, 25, 0.86);
  --divider: rgba(255, 255, 255, 0.07);
  --divider-strong: rgba(255, 255, 255, 0.08);

  --button-grad-1: #142635;
  --button-grad-2: #0f1f2b;
  --button-hover-border: #456173;
  --button-hover-bg: #172b3b;
  --button-shadow-inset: rgba(255, 255, 255, 0.04);

  --button-primary-text: #041211;
  --button-primary-grad-1: #45dec8;
  --button-primary-grad-2: #2bbba7;
  --button-primary-border: #56e4cf;
  --button-primary-hover-bg: #52dfcb;
  --button-primary-hover-border: #75f2df;

  --panel-grad-1: rgba(17, 34, 49, 0.92);
  --panel-grad-2: rgba(10, 23, 33, 0.94);
  --panel-border: rgba(255, 255, 255, 0.08);

  --input-bg: #09151f;

  --result-secondary-text: #dce7e6;
  --result-row-text: #adbfbd;

  --accuracy-border: #5b4824;
  --accuracy-bg: rgba(244, 190, 91, 0.07);
  --accuracy-strong: #f2cc7c;
  --accuracy-text: #bdad8a;

  --footer-text: #5e7476;

  --dialog-bg: #0a1722;
  --dialog-shadow: rgba(0, 0, 0, 0.65);
  --dialog-backdrop: rgba(2, 8, 12, 0.84);
  --icon-button-bg: #122331;

  --toast-border: #3b5968;
  --toast-bg: #112532;

  --stage-bg: #dce3e5;
  --stage-checker: #d4dcde;
  --stage-empty-text: #18303b;
  --stage-empty-icon-border: #62808d;
  --stage-empty-icon-text: #315360;
  --stage-empty-p: #4f6974;
  --stage-empty-actions: #506a73;
  --kbd-bg: #f1f5f5;
  --kbd-border: #acbcc2;
  --kbd-shadow: #869ca4;
  --kbd-text: #263f48;

  --status-success-text: var(--teal);
  --status-success-border: #236e63;
  --status-success-bg: #0b2626;
  --status-error-text: #ff8f96;
  --status-error-border: #74353b;
  --status-error-bg: #2a1218;

  font-family: Inter, "Segoe UI Variable", "Segoe UI", system-ui, sans-serif;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f3f6f7;
  --bg-radial: rgba(15, 156, 134, 0.1);
  --surface: #ffffff;
  --surface-2: #eef3f4;
  --surface-3: #e2eaec;
  --border: #ccd8db;
  --text: #10262d;
  --muted: #5b7178;
  --teal: #0f9c86;
  --teal-dark: #d3f2ec;
  --red: #d9313d;
  --amber: #9c6a12;
  --shadow: 0 14px 34px rgba(15, 40, 48, 0.12);

  --header-bg: rgba(255, 255, 255, 0.86);
  --divider: rgba(10, 30, 35, 0.08);
  --divider-strong: rgba(10, 30, 35, 0.1);

  --button-grad-1: #ffffff;
  --button-grad-2: #eef3f4;
  --button-hover-border: #9fb4b9;
  --button-hover-bg: #e6edee;
  --button-shadow-inset: rgba(255, 255, 255, 0.6);

  --button-primary-text: #04211c;
  --button-primary-grad-1: #26c3a9;
  --button-primary-grad-2: #129485;
  --button-primary-border: #0e8577;
  --button-primary-hover-bg: #22b39c;
  --button-primary-hover-border: #0c7669;

  --panel-grad-1: #ffffff;
  --panel-grad-2: #f7fafa;
  --panel-border: rgba(10, 30, 35, 0.09);

  --input-bg: #f5f8f9;

  --result-secondary-text: #294048;
  --result-row-text: #55696f;

  --accuracy-border: #e3c078;
  --accuracy-bg: rgba(185, 121, 15, 0.08);
  --accuracy-strong: #8a5a06;
  --accuracy-text: #6c5427;

  --footer-text: #728488;

  --dialog-bg: #ffffff;
  --dialog-shadow: rgba(15, 40, 48, 0.28);
  --dialog-backdrop: rgba(20, 35, 40, 0.55);
  --icon-button-bg: #eef3f4;

  --toast-border: #c9d3d6;
  --toast-bg: #ffffff;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }

body {
  background:
    radial-gradient(circle at 70% -20%, var(--bg-radial), transparent 32rem),
    var(--bg);
  overflow-x: hidden;
  transition: background 160ms ease, color 160ms ease;
}

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

.app-shell { min-height: 100vh; display: flex; flex-direction: column; }

.app-header {
  min-height: 76px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--divider);
  background: var(--header-bg);
  backdrop-filter: blur(14px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 230px; }
.brand img { filter: drop-shadow(0 8px 18px rgba(53, 208, 186, 0.15)); }
.brand h1 { margin: 0; font-size: 21px; letter-spacing: -0.03em; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }

.header-actions, .canvas-actions, .empty-actions, .dialog-actions {
  display: flex;
  align-items: center;
  gap: 9px;
  flex-wrap: wrap;
}

.button {
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: linear-gradient(180deg, var(--button-grad-1), var(--button-grad-2));
  box-shadow: 0 1px 0 var(--button-shadow-inset) inset;
  cursor: pointer;
  transition: border-color 120ms ease, transform 120ms ease, background 120ms ease;
}

.button:hover:not(:disabled) { border-color: var(--button-hover-border); background: var(--button-hover-bg); }
.button:active:not(:disabled) { transform: translateY(1px); }
.button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }
.button:disabled { opacity: 0.42; cursor: not-allowed; }
.button.primary { color: var(--button-primary-text); background: linear-gradient(180deg, var(--button-primary-grad-1), var(--button-primary-grad-2)); border-color: var(--button-primary-border); font-weight: 700; }
.button.primary:hover:not(:disabled) { background: var(--button-primary-hover-bg); border-color: var(--button-primary-hover-border); }
.button.subtle { background: transparent; }
.button.compact { min-height: 34px; padding: 0 11px; font-size: 12px; border-radius: 8px; }
.button.wide { width: 100%; justify-content: center; }
.button.is-picking { border-color: var(--amber); color: var(--amber); }
.button.icon-toggle { min-height: 40px; width: 40px; padding: 0; display: grid; place-items: center; font-size: 17px; }

.workspace {
  width: 100%;
  padding: 16px;
  display: grid;
  grid-template-columns: minmax(248px, 292px) minmax(420px, 1fr) minmax(244px, 286px);
  gap: 14px;
  flex: 1;
  min-height: 0;
}

.panel, .canvas-panel {
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  background: linear-gradient(145deg, var(--panel-grad-1), var(--panel-grad-2));
  box-shadow: var(--shadow);
  min-width: 0;
}

.panel { overflow: auto; }
.panel-section { padding: 18px; border-bottom: 1px solid var(--divider); }
.panel-section:last-child { border-bottom: 0; }

.section-heading { display: flex; gap: 11px; align-items: flex-start; margin-bottom: 18px; }
.section-heading.small { margin-bottom: 14px; }
.section-heading h2 { margin: 0; font-size: 15px; }
.section-heading p { margin: 4px 0 0; color: var(--muted); font-size: 12px; line-height: 1.45; }
.step-number { width: 25px; height: 25px; display: grid; place-items: center; flex: 0 0 auto; border-radius: 8px; background: var(--teal-dark); color: var(--teal); font-weight: 800; font-size: 12px; }

.field { display: grid; gap: 7px; margin: 0 0 15px; color: var(--result-row-text); font-size: 12px; font-weight: 650; }
.field > span { display: flex; justify-content: space-between; }
select, input[type="number"] {
  min-height: 40px;
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 9px;
  color: var(--text);
  background: var(--input-bg);
  padding: 0 10px;
}
select { cursor: pointer; }
output { color: var(--teal); font-variant-numeric: tabular-nums; }

input[type="range"] { width: 100%; accent-color: var(--teal); cursor: pointer; }
.range-field { transition: opacity 120ms ease; }
.range-field.disabled { opacity: 0.36; }

.background-row { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; }
.color-swatch { width: 25px; height: 25px; border-radius: 7px; border: 2px solid rgba(255,255,255,0.55); background: #fff; flex: 0 0 auto; }
.microcopy, .hint { color: var(--muted); font-size: 11px; line-height: 1.45; }
.hint { margin: 3px 0 0; }

.check-row { display: flex; gap: 9px; align-items: flex-start; margin: 11px 0; color: var(--result-row-text); font-size: 12px; line-height: 1.4; cursor: pointer; }
.check-row.disabled { opacity: 0.4; cursor: default; }
.check-row input { margin: 2px 0 0; accent-color: var(--teal); }
.dimension-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.unit-input { display: flex; align-items: center; border: 1px solid var(--border); border-radius: 9px; overflow: hidden; background: var(--input-bg); }
.unit-input input { border: 0; border-radius: 0; min-width: 0; }
.unit-input span { padding-right: 9px; color: var(--muted); font-weight: 500; }

.canvas-panel { display: flex; flex-direction: column; overflow: hidden; }
.canvas-toolbar { min-height: 62px; padding: 11px 14px; display: flex; align-items: center; justify-content: space-between; gap: 12px; border-bottom: 1px solid var(--divider); }
.canvas-toolbar strong, .canvas-toolbar span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.canvas-toolbar strong { max-width: 330px; font-size: 13px; }
.canvas-toolbar > div > span { margin-top: 3px; max-width: 330px; color: var(--muted); font-size: 11px; }
.status-pill { padding: 6px 9px; border: 1px solid var(--border); border-radius: 999px; color: var(--muted) !important; background: var(--input-bg); font-size: 10px !important; font-weight: 750; text-transform: uppercase; letter-spacing: 0.05em; }
.status-pill.success { color: var(--status-success-text) !important; border-color: var(--status-success-border); background: var(--status-success-bg); }
.status-pill.error { color: var(--status-error-text) !important; border-color: var(--status-error-border); background: var(--status-error-bg); }

.canvas-stage {
  position: relative;
  flex: 1;
  min-height: 470px;
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: 20px;
  background-color: var(--stage-bg);
  background-image:
    linear-gradient(45deg, var(--stage-checker) 25%, transparent 25%),
    linear-gradient(-45deg, var(--stage-checker) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--stage-checker) 75%),
    linear-gradient(-45deg, transparent 75%, var(--stage-checker) 75%);
  background-size: 24px 24px;
  background-position: 0 0, 0 12px, 12px -12px, -12px 0;
}
.canvas-stage.drag-over { box-shadow: 0 0 0 4px var(--teal) inset; }
.canvas-stage.picking { cursor: crosshair; }
#displayCanvas { display: none; max-width: 100%; max-height: calc(100vh - 230px); object-fit: contain; box-shadow: 0 14px 35px rgba(0,0,0,0.22); }
#displayCanvas.visible { display: block; }

.empty-state { text-align: center; color: var(--stage-empty-text); user-select: none; }
.empty-state.hidden { display: none; }
.drop-icon { width: 66px; height: 66px; display: grid; place-items: center; margin: 0 auto 15px; border: 2px dashed var(--stage-empty-icon-border); border-radius: 20px; color: var(--stage-empty-icon-text); font-size: 34px; }
.empty-state h2 { margin: 0; font-size: 19px; }
.empty-state p { margin: 7px 0 18px; color: var(--stage-empty-p); font-size: 12px; }
.empty-actions { justify-content: center; color: var(--stage-empty-actions); font-size: 11px; }
kbd { padding: 2px 5px; border-radius: 5px; background: var(--kbd-bg); border: 1px solid var(--kbd-border); box-shadow: 0 1px 0 var(--kbd-shadow); color: var(--kbd-text); }
.pick-banner { position: absolute; top: 14px; left: 50%; transform: translateX(-50%); padding: 8px 13px; border-radius: 999px; color: #271b05; background: var(--amber); box-shadow: 0 8px 24px rgba(0,0,0,.22); font-size: 12px; font-weight: 800; }

.legend { min-height: 46px; padding: 9px 14px; display: flex; align-items: center; gap: 18px; color: var(--muted); font-size: 10px; border-top: 1px solid var(--divider); }
.legend > span { display: flex; align-items: center; gap: 6px; }
.legend-chip { width: 11px; height: 11px; border-radius: 3px; display: inline-block; }
.legend-chip.material { background: rgba(53,208,186,.65); }
.legend-chip.point { background: var(--red); border-radius: 50%; }
.camera-tip { margin-left: auto; text-align: right; }

.results-panel { display: flex; flex-direction: column; }
.point-toggle-section .check-row { margin: 9px 0; }
.result-cards { display: grid; gap: 10px; padding: 14px 17px 4px; overflow-y: auto; }
.point-card { border: 1px solid var(--divider-strong); border-radius: 12px; padding: 12px; background: var(--surface-2); }
.point-card-head { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 8px; }
.point-card-dot { width: 12px; height: 12px; margin-top: 3px; border-radius: 50%; background: var(--red); box-shadow: 0 0 10px rgba(255,91,100,.35); flex: 0 0 auto; }
.point-card-head strong { display: block; font-size: 12px; }
.point-card-head p { margin: 3px 0 0; color: var(--muted); font-size: 10.5px; line-height: 1.4; }
.point-card .result-row { padding: 8px 0; font-size: 11px; }
.point-card .result-row:last-child { border-bottom: 0; }
.results-empty { margin: 22px 17px; color: var(--muted); font-size: 12px; line-height: 1.5; text-align: center; }
.result-row { display: flex; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--divider); font-size: 11px; }
.result-row span { color: var(--result-row-text); }
.result-row strong { text-align: right; color: var(--teal); font-size: 13px; font-variant-numeric: tabular-nums; }
.result-row.secondary strong { color: var(--result-secondary-text); font-size: 11px; }
.area-row { padding: 10px 17px; }
.area-row strong { color: var(--result-secondary-text); font-size: 11px; }
.result-actions { display: grid; gap: 8px; padding: 12px 17px; }
.copy-image-group { display: grid; gap: 6px; }
.copy-image-label { color: var(--muted); font-size: 11px; font-weight: 650; }
.copy-image-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.copy-image-buttons .button { width: 100%; justify-content: center; }
.accuracy-note { margin: auto 17px 17px; padding: 12px; border: 1px solid var(--accuracy-border); border-radius: 10px; background: var(--accuracy-bg); }
.accuracy-note strong { color: var(--accuracy-strong); font-size: 11px; }
.accuracy-note p { margin: 5px 0 0; color: var(--accuracy-text); font-size: 10px; line-height: 1.45; }

footer { min-height: 34px; display: flex; justify-content: space-between; align-items: center; padding: 0 20px; color: var(--footer-text); font-size: 9px; }

.camera-dialog { width: min(860px, calc(100vw - 30px)); padding: 0; border: 1px solid var(--border); border-radius: 16px; background: var(--dialog-bg); color: var(--text); box-shadow: 0 30px 100px var(--dialog-shadow); }
.camera-dialog::backdrop { background: var(--dialog-backdrop); backdrop-filter: blur(5px); }
.dialog-card { padding: 16px; }
.dialog-header { display: flex; justify-content: space-between; gap: 15px; margin-bottom: 14px; }
.dialog-header h2 { margin: 0; font-size: 18px; }
.dialog-header p { margin: 5px 0 0; color: var(--muted); font-size: 11px; }
.icon-button { width: 38px; height: 38px; border: 1px solid var(--border); border-radius: 10px; background: var(--icon-button-bg); color: var(--text); font-size: 25px; cursor: pointer; }
.video-wrap { position: relative; overflow: hidden; min-height: 320px; display: grid; place-items: center; border-radius: 12px; background: #020608; }
#cameraVideo { display: block; width: 100%; max-height: 62vh; object-fit: contain; }
.camera-guide { position: absolute; inset: 8%; border: 2px dashed rgba(255,255,255,.42); border-radius: 12px; pointer-events: none; }
.dialog-actions { justify-content: space-between; margin-top: 13px; }
.dialog-actions select { width: min(430px, 65%); }
.camera-error { color: #ff959b; }

.toast { position: fixed; z-index: 80; left: 50%; bottom: 24px; transform: translate(-50%, 20px); padding: 10px 15px; border: 1px solid var(--toast-border); border-radius: 10px; background: var(--toast-bg); color: var(--text); box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity 160ms ease, transform 160ms ease; font-size: 12px; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.hidden { display: none !important; }

@media (max-width: 1120px) {
  .workspace { grid-template-columns: 245px minmax(420px, 1fr); }
  .results-panel { grid-column: 1 / -1; }
  .result-cards { max-height: 340px; }
}

@media (max-width: 760px) {
  .app-header { position: static; align-items: flex-start; flex-direction: column; }
  .header-actions { width: 100%; }
  .header-actions .button { flex: 1; }
  .header-actions .button.icon-toggle { flex: 0 0 auto; }
  .workspace { grid-template-columns: 1fr; padding: 10px; }
  .controls-panel { order: 2; }
  .canvas-panel { order: 1; min-height: 570px; }
  .results-panel { order: 3; grid-column: auto; display: flex; }
  .canvas-stage { min-height: 430px; padding: 10px; }
  #displayCanvas { max-height: 65vh; }
  .camera-tip { display: none !important; }
  footer { padding-bottom: env(safe-area-inset-bottom); }
}

@media (max-width: 450px) {
  .header-actions .button:not(.icon-toggle) { flex-basis: 30%; padding: 0 8px; }
  .canvas-toolbar { align-items: flex-start; flex-direction: column; }
  .canvas-actions { width: 100%; justify-content: space-between; }
  .legend { flex-wrap: wrap; }
  .dimension-grid { grid-template-columns: 1fr; }
  .dialog-actions { align-items: stretch; flex-direction: column; }
  .dialog-actions select { width: 100%; }
}
