:root {
  --bg: #f6f8fb;
  --card: #ffffff;
  --text: #172033;
  --muted: #637083;
  --border: #d7deea;
  --accent: #208450;
  --accent-soft: #eef2ff;
  --success: #059669;
  --warning: #d97706;
  --danger: #dc2626;
  --shadow: 0 12px 32px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 13px;
}
button, input, select { font: inherit; }
.app-shell { width: min(1440px, calc(100% - 32px)); margin: 24px auto; }
.hero {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: flex-end;
  margin-bottom: 20px;
}
.eyebrow { color: var(--accent); text-transform: uppercase; letter-spacing: .12em; font-size: .75rem; font-weight: 800; margin: 0 0 6px; }
h1 { margin: 0; font-size: clamp(1.75rem, 4vw, 3.35rem); line-height: 1; }
h2, h3, p { margin-top: 0; }
.subtitle { max-width: 760px; color: var(--muted); margin-bottom: 0; }
.hero-actions, .field-tools, .export-row { display: flex; gap: 10px; flex-wrap: wrap; }
.layout { display: grid; grid-template-columns: minmax(0, 1.15fr) minmax(360px, .85fr); gap: 20px; align-items: start; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: 22px; padding: 20px; box-shadow: var(--shadow); }
.section-title-row { display: flex; justify-content: space-between; gap: 16px; align-items: start; margin-bottom: 14px; }
.section-title-row p { color: var(--muted); margin-bottom: 0; }
.btn { border: 1px solid var(--border); background: #fff; color: var(--text); padding: 7px 11px; border-radius: 10px; cursor: pointer; font-weight: 750; }
.btn:hover { transform: translateY(-1px); }
.btn.primary { background: var(--accent); color: white; border-color: var(--accent); }
.btn.success { background: var(--success); color: white; border-color: var(--success); }
.btn.ghost { color: var(--muted); background: transparent; }
.hidden { display: none !important; }
.sample-size-control { display: grid; gap: 6px; color: var(--muted); font-weight: 700; font-size: .85rem; }
.sample-size-control select { border: 1px solid var(--border); border-radius: 10px; padding: 8px; background: #fff; }
.sampler-box {
  position: relative;
  width: 100%;
  height: clamp(360px, 62vh, 760px);
  border: 1px solid var(--border);
  border-radius: 18px;
  background: #e8edf4;
  overflow: auto;
  display: block;
  overscroll-behavior: contain;
}
#srcCanvas {
  display: block;
  width: 100%;
  height: auto;
  max-width: none;
  max-height: none;
  cursor: crosshair;
}
#videoStream { width: 100%; height: 100%; display: none; object-fit: contain; background: #111827; }
.sample-reticle { position: absolute; width: 24px; height: 24px; border: 2px solid white; outline: 2px solid rgba(79, 70, 229, .85); border-radius: 999px; transform: translate(-50%, -50%); pointer-events: none; display: none; }
.field-tools { margin-top: 14px; }
.advanced-panel { margin-top: 14px; border: 1px solid var(--border); border-radius: 16px; padding: 12px 14px; background: #fbfdff; }
.advanced-panel summary { cursor: pointer; font-weight: 850; }
.slider-grid { margin-top: 14px; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.slider-grid h3 { grid-column: 1 / -1; border-bottom: 1px solid var(--border); padding-bottom: 8px; margin-bottom: 0; }
.slider-grid label { display: grid; gap: 6px; font-weight: 700; color: var(--text); }
.slider-grid span { color: var(--accent); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; justify-self: end; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.results-card { display: grid; gap: 16px; }
.preview-stack { display: grid; grid-template-columns: 1fr 180px; gap: 14px; align-items: stretch; }
.color-preview { min-height: 180px; border-radius: 18px; border: 1px solid var(--border); display: grid; place-items: center; color: white; text-shadow: 0 2px 8px rgba(0,0,0,.55); font-weight: 900; background: #9a6b45; }
#magnifierCanvas { width: 180px; height: 180px; border: 1px solid var(--border); border-radius: 18px; image-rendering: pixelated; background: #f1f5f9; }
.result-main { padding: 16px; border-radius: 18px; background: var(--accent-soft); border: 1px solid #dfe4ff; }
.metric-label { color: var(--muted); text-transform: uppercase; letter-spacing: .08em; font-size: .75rem; font-weight: 900; margin-bottom: 6px; }
.color-code { font-size: clamp(1.75rem, 4vw, 2.75rem); font-weight: 950; line-height: 1; color: var(--accent); margin-bottom: 12px; }
.confidence-row { display: flex; gap: 10px; align-items: center; }
.confidence-meter { flex: 1; height: 12px; border-radius: 999px; overflow: hidden; background: #d8defc; }
.confidence-meter span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.quality-note { color: var(--muted); margin: 10px 0 0; }
.metric-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.metric-tile { border: 1px solid var(--border); background: #fbfdff; border-radius: 14px; padding: 12px; }
.metric-tile span { display: block; color: var(--muted); font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; font-weight: 850; }
.metric-tile strong { display: block; margin-top: 4px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; overflow-wrap: anywhere; }
#wheelCanvas { width: 100%; max-width: 360px; justify-self: center; }
.neighbors h3 { margin-bottom: 10px; }
.neighbor-list { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.neighbor-chip { border: 1px solid var(--border); border-radius: 12px; padding: 10px; display: grid; gap: 8px; font-size: .8rem; font-weight: 800; }
.swatch { height: 32px; border-radius: 8px; border: 1px solid rgba(0,0,0,.15); }
.history-card { margin-top: 20px; }
.table-wrap { overflow: auto; border: 1px solid var(--border); border-radius: 14px; }
table { width: 100%; border-collapse: collapse; font-size: .84rem; }
th, td { padding: 12px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
th { background: #f8fafc; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; font-size: .72rem; }
tr:last-child td { border-bottom: 0; }
dialog { border: 0; border-radius: 22px; padding: 0; box-shadow: var(--shadow); width: min(460px, calc(100% - 32px)); }
dialog::backdrop { background: rgba(15, 23, 42, .35); }
.dialog-card { padding: 22px; display: grid; gap: 14px; }
.dialog-card select { padding: 10px; border-radius: 12px; border: 1px solid var(--border); }
.dialog-card menu { display: flex; justify-content: flex-end; gap: 10px; padding: 0; margin: 0; }
@media (max-width: 980px) {
  .hero, .layout { grid-template-columns: 1fr; display: grid; }
  .preview-stack { grid-template-columns: 1fr; }
  #magnifierCanvas { width: 100%; height: auto; justify-self: center; max-width: 220px; }
}
@media (max-width: 640px) {
  .app-shell { width: min(100% - 20px, 1440px); margin: 10px auto; }
  .sampler-box { height: clamp(320px, 58vh, 620px); }
  .slider-grid, .metric-grid, .neighbor-list { grid-template-columns: 1fr; }
  .section-title-row { display: grid; }
}

.calibration-box {
  margin-top: 14px;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 14px;
  background: #fbfdff;
}
.calibration-header {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 12px;
}
.calibration-header h3 { margin: 0 0 4px; font-size: 0.98rem; }
.calibration-header p { margin: 0; color: var(--muted); font-size: 0.88rem; }
.btn.small { padding: 7px 10px; font-size: 0.82rem; }
.reference-list { display: grid; gap: 8px; margin-top: 12px; }
.reference-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #fff;
}
.reference-item strong { display: block; font-size: 0.9rem; }
.reference-item span { display: block; color: var(--muted); font-size: 0.78rem; }
.reference-item button {
  border: 0;
  background: transparent;
  color: var(--danger);
  cursor: pointer;
  font-weight: 800;
  padding: 4px 6px;
}
.calibration-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}
.fit-readout {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 12px;
}
.fit-readout span {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 7px 9px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 850;
  text-align: center;
}
@media (max-width: 640px) {
  .fit-readout { grid-template-columns: 1fr; }
}

.dialog-card input {
  padding: 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
}
