*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

:root {
  --accent:    #0d9488;
  --accent-dk: #0f766e;
  --danger:    #dc2626;
  --bg:        #f8fafc;
  --surface:   #ffffff;
  --border:    #e2e8f0;
  --text:      #1e293b;
  --muted:     #64748b;
  --radius:    8px;
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Header ────────────────────────────────────────────── */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  flex-shrink: 0;
}
.header-inner { max-width: 1600px; margin: 0 auto; }
header h1 { font-size: 18px; font-weight: 700; letter-spacing: -0.3px; }
header .subtitle { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* ── Main layout ────────────────────────────────────────── */
#app {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 0;
  flex: 1;
  overflow: hidden;
  max-width: 1600px;
  margin: 0 auto;
  width: 100%;
}

/* ── Canvas panel ───────────────────────────────────────── */
#canvas-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: auto;
  padding: 20px;
  background: #f1f5f9;
  border-right: 1px solid var(--border);
}

#drop-zone {
  width: 100%;
  max-width: 560px;
  border: 2px dashed #cbd5e1;
  border-radius: 12px;
  background: var(--surface);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
#drop-zone:hover, #drop-zone.drag-over {
  border-color: var(--accent);
  background: #f0fdfa;
}
.drop-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 48px 24px;
  text-align: center;
  color: var(--muted);
}
.drop-content svg { width: 40px; height: 40px; color: #94a3b8; margin-bottom: 4px; }
.browse-btn {
  display: inline-block;
  padding: 6px 16px;
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
}
.browse-btn:hover { background: var(--accent-dk); }
#file-input { display: none; }

#canvas-container {
  position: relative;
  display: inline-block;
  line-height: 0;
  max-width: 100%;
}
#image-canvas {
  display: block;
  max-width: 100%;
  height: auto;
  border-radius: var(--radius);
  box-shadow: 0 4px 20px rgba(0,0,0,0.12);
}
#overlay-canvas {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  border-radius: var(--radius);
  cursor: default;
}
#overlay-canvas.crosshair { cursor: crosshair; }

.server-note {
  margin-top: 12px;
  font-size: 11px;
  color: var(--muted);
  text-align: center;
}
.server-note code { font-family: monospace; background: #e2e8f0; padding: 1px 4px; border-radius: 3px; }

/* ── Control panel ──────────────────────────────────────── */
#control-panel {
  display: flex;
  flex-direction: column;
  gap: 0;
  overflow-y: auto;
  background: var(--surface);
}

#control-panel > * {
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}

.section-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 10px;
}

/* ── Mode buttons ───────────────────────────────────────── */
#mode-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.mode-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.mode-btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.mode-btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.mode-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.mode-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.hint { font-size: 12px; color: var(--muted); margin-top: 8px; min-height: 16px; }

/* ── Calibration badge ──────────────────────────────────── */
#calib-section { padding-bottom: 14px; }
.calib-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 99px;
  font-size: 12px;
  font-weight: 500;
}
.calib-none   { background: #fef2f2; color: #991b1b; }
.calib-single { background: #fefce8; color: #92400e; }
.calib-multi  { background: #f0fdf4; color: #166534; }

/* ── Reference dialog ───────────────────────────────────── */
#reference-dialog { background: #f8fafc; }

.swatch-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}
.swatch-item { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.swatch-label { font-size: 11px; color: var(--muted); }
.arrow { font-size: 18px; color: #94a3b8; margin-top: -14px; }

.swatch {
  width: 44px; height: 44px;
  border-radius: 6px;
  border: 1px solid rgba(0,0,0,0.15);
  background: #e2e8f0;
}
.swatch.large { width: 64px; height: 64px; border-radius: 8px; flex-shrink: 0; }
.swatch.small { width: 24px; height: 24px; border-radius: 4px; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12px; font-weight: 500; color: var(--muted); }
.field input {
  padding: 8px 10px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s;
}
.field input:focus { border-color: var(--accent); }
.field input.error { border-color: var(--danger); }
.input-error { font-size: 12px; color: var(--danger); }
.input-match { font-size: 12px; color: var(--accent); font-weight: 500; }

.dialog-buttons { display: flex; gap: 8px; margin-top: 12px; }
.primary-btn {
  flex: 1;
  padding: 9px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.primary-btn:hover:not(:disabled) { background: var(--accent-dk); }
.primary-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.secondary-btn {
  padding: 9px 16px;
  background: var(--surface);
  color: var(--muted);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  cursor: pointer;
}
.secondary-btn:hover { border-color: #94a3b8; }

/* ── Reference list ─────────────────────────────────────── */
#references-section { flex: 0 0 auto; }
#reference-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.ref-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 10px;
}
.ref-num { font-size: 11px; color: var(--muted); width: 20px; flex-shrink: 0; }
.ref-swatches { display: flex; align-items: center; gap: 5px; }
.ref-arrow { font-size: 12px; color: #94a3b8; }
.ref-info { flex: 1; min-width: 0; }
.ref-code { font-weight: 600; font-size: 13px; }
.ref-name { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ref-remove {
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 4px;
  flex-shrink: 0;
}
.ref-remove:hover { color: var(--danger); background: #fef2f2; }
.count-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px; height: 18px;
  background: var(--accent);
  color: #fff;
  border-radius: 99px;
  font-size: 10px;
  font-weight: 700;
  vertical-align: middle;
  margin-left: 4px;
}

/* ── Result card ────────────────────────────────────────── */
#result-card { flex: 0 0 auto; }
.result-body { display: flex; gap: 14px; align-items: flex-start; margin-bottom: 10px; }
#result-info { display: flex; flex-direction: column; gap: 4px; }
.result-code { font-size: 20px; font-weight: 700; letter-spacing: -0.5px; }
.result-name { font-size: 14px; color: var(--muted); }
.result-deltae { font-size: 12px; color: var(--muted); }
.result-detail { font-size: 11px; color: #94a3b8; margin-top: 4px; font-family: monospace; }

/* ── Responsive: Tablet (≤900px) ────────────────────────── */
@media (max-width: 900px) {
  #app {
    grid-template-columns: 1fr 280px;
  }
}

/* ── Responsive: Mobile (≤640px) ────────────────────────── */
@media (max-width: 640px) {
  /* Let body scroll naturally instead of clipping */
  body {
    height: auto;
    overflow-y: auto;
  }

  header {
    padding: 10px 14px;
  }
  header h1 { font-size: 16px; }
  header .subtitle { display: none; }

  /* Stack canvas above controls */
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    height: auto;
    overflow: visible;
  }

  #canvas-panel {
    min-height: 45vw;
    max-height: 60vh;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 12px;
    overflow: auto;
  }

  /* Drop zone: tighter on mobile */
  .drop-content {
    padding: 28px 16px;
    gap: 6px;
  }
  .drop-content svg { width: 28px; height: 28px; }
  /* "Drop" hint replaced with just Browse on touch */
  .drop-content > p:first-of-type { display: none; }

  /* Control panel: natural height, no internal scroll */
  #control-panel {
    overflow-y: visible;
    height: auto;
  }

  /* Mode buttons: keep side by side but shrink text */
  .mode-btn {
    padding: 8px 8px;
    font-size: 12px;
    gap: 4px;
  }

  /* Reference dialog takes full width */
  #reference-dialog {
    padding: 12px 14px;
  }

  /* Result swatch a touch smaller */
  .swatch.large { width: 52px; height: 52px; }
  .result-code { font-size: 17px; }
}
