/* Visual Part Finder — minimal, no framework, no CDN. */

:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #d4d7dc;
  --text: #1f2330;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --danger: #dc2626;
  --warn: #d97706;
  --warn-bg: #fef3c7;
  --ok: #16a34a;
  --highlight: #dbeafe;
  --highlight-border: #2563eb;
  --radius: 6px;
  --toolbar-h: 52px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 14px;
}
/* Full-height column layout so the page itself never scrolls; only inner
   panes (image viewport / table) scroll. */
body { display: flex; flex-direction: column; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}
.hidden { display: none !important; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.muted { color: var(--muted); }

/* ---------- Toolbar ---------- */
#toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 50;
  flex-shrink: 0;
}
.app-title {
  font-size: 16px;
  margin: 0;
  white-space: nowrap;
  font-weight: 600;
}

/* search + drawing selector + prev/next group */
.toolbar-nav {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 1 auto;
  min-width: 0;
}
.toolbar-nav input[type="text"] {
  width: 200px;
  min-width: 120px;
}
.toolbar-nav select {
  min-width: 180px;
  max-width: 340px;
}

/* import + refresh group (pushed right) */
.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
}

.inline-label { font-size: 13px; color: var(--muted); }

select, button, input[type="text"] {
  font: inherit;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
}
button {
  cursor: pointer;
  background: #f9fafb;
}
button:hover { background: #eef0f3; }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button.primary:hover { background: var(--accent-dark); }
button.danger { color: var(--danger); border-color: #fecaca; }
button.danger:hover { background: #fee2e2; }
button:disabled { opacity: .5; cursor: not-allowed; }
select { min-width: 110px; }

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: #1f2330;
  color: #fff;
  padding: 8px 16px;
  border-radius: var(--radius);
  z-index: 600;
  box-shadow: 0 4px 12px rgba(0,0,0,.2);
  transition: opacity .25s;
  max-width: 80vw;
}
#toast.error { background: var(--danger); }
#toast.ok { background: var(--ok); }

/* ---------- Loading overlay ---------- */
#loadingOverlay {
  position: fixed;
  inset: 0;
  background: rgba(255,255,255,.7);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  z-index: 700;
}
.spinner {
  width: 36px; height: 36px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { color: var(--muted); font-size: 14px; }

/* ---------- Main: fills viewport below toolbar ---------- */
#mainView {
  flex: 1 1 auto;
  min-height: 0;
  padding: 12px;
  display: flex;
  flex-direction: column;
}

/* ---------- Home view: scrollable drawing grid ---------- */
#homeView {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}
.home-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
  padding: 16px;
}
.home-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0;
  cursor: pointer;
  overflow: hidden;
  font: inherit;
  color: var(--text);
  text-align: left;
  width: 100%;
  transition: box-shadow 0.15s, transform 0.15s;
}
.home-card:hover {
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  transform: translateY(-2px);
}
.home-card:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.home-card-thumb {
  aspect-ratio: 4 / 3;
  background: #fff;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.home-card-thumb img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}
.home-card-name {
  font-weight: 600;
  padding: 8px 12px 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.home-card-meta {
  font-size: 12px;
  color: var(--muted);
  padding: 2px 12px 10px;
}
.home-empty {
  color: var(--muted);
  text-align: center;
  padding: 56px 16px;
  background: var(--panel);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  margin: auto;
  max-width: 640px;
}

/* ---------- Card (fills available height) ---------- */
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: #fafbfc;
  flex-shrink: 0;
}
.card-title-wrap { min-width: 0; }
.card-name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.card-actions { display: flex; gap: 6px; flex-shrink: 0; }

/* per-card no-match flash */
.card-flash {
  color: var(--warn);
  background: var(--warn-bg);
  border-bottom: 1px solid #fde68a;
  padding: 4px 14px;
  font-size: 12px;
  opacity: 0;
  transition: opacity .15s;
  flex-shrink: 0;
}
.card-flash.show { opacity: 1; }

.card-body {
  display: flex;
  gap: 14px;
  padding: 14px;
  align-items: stretch;
  flex: 1 1 auto;
  min-height: 0;
}
.card-left {
  flex: 1 1 64%;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.card-right {
  flex: 1 1 36%;
  min-width: 260px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* Selected-row panel: compact label→row info */
.selected-row-panel {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 10px;
  margin-bottom: 8px;
  flex-shrink: 0;
  font-size: 13px;
  background: #fafbfc;
  min-height: 36px;
}
.selected-row-panel .srp-header {
  font-weight: 600;
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.selected-row-panel .srp-value {
  font-weight: 400;
  color: var(--text);
}
.selected-row-panel .srp-placeholder {
  color: var(--muted);
  font-style: italic;
}
.selected-row-panel .srp-row-title {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  margin-bottom: 4px;
}
.selected-row-panel .srp-kv {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  line-height: 1.4;
}
.selected-row-panel .srp-kv-item {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.selected-row-panel .srp-kv-key {
  color: var(--muted);
  font-size: 11px;
}
.selected-row-panel .srp-kv-val {
  font-size: 12px;
}

/* ---------- Drawing viewport + zoom ---------- */
.image-viewport {
  position: relative;
  width: 100%;
  height: 100%;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.image-viewport:active { cursor: grabbing; }

.image-container {
  position: relative;
  width: 100%;
  line-height: 0;
  /* height comes from aspect-ratio; transform applied via JS */
  transform-origin: 0 0;
  will-change: transform;
}
.image-container img.base-img {
  width: 100%;
  height: auto;
  display: block;
  user-select: none;
  pointer-events: none;
}

/* zoom controls */
.zoom-controls {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  align-items: center;
  gap: 4px;
  background: rgba(255,255,255,.9);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px;
  z-index: 10;
}
.zoom-btn {
  padding: 2px 8px;
  min-width: 28px;
  font-size: 13px;
  background: #fff;
}
.zoom-label {
  font-size: 12px;
  color: var(--muted);
  padding: 0 4px;
  min-width: 38px;
  text-align: center;
}

/* ---------- Circle overlays: solid filled disc, number centered ---------- */
.circle-overlay {
  position: absolute;
  border-radius: 50%;
  container-type: size;
  transform: translate(-50%, -50%);
  background: var(--accent);
  border: 1px solid rgba(37, 99, 235, 0.9);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  z-index: 2;
  box-shadow: 0 0 0 1px rgba(255,255,255,.4) inset;
}
.circle-overlay:hover { background: var(--accent-dark); }
.circle-overlay.selected {
  border-color: var(--danger);
  background: var(--danger);
  box-shadow: 0 0 0 2px rgba(220,38,38,.5), 0 0 12px rgba(220,38,38,.55);
  z-index: 5;
}
.circle-label {
  color: #fff;
  font-weight: 700;
  font-size: 11px;  /* fallback if container-query units unsupported */
  font-size: clamp(8px, 55cqw, 18px);
  text-shadow: 0 1px 2px rgba(0,0,0,.6);
  pointer-events: none;
  white-space: nowrap;
  line-height: 1;
}
.circle-overlay.null-text {
  border-color: var(--warn);
  background: var(--warn);
}
.circle-overlay.null-text .circle-label { color: #fff; }
.circle-overlay.selected .circle-label { color: #fff; }

/* ---------- Right pane: table / placeholder ---------- */
.placeholder {
  color: var(--muted);
  padding: 32px 14px;
  text-align: center;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  margin: auto;
}
.table-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  flex-shrink: 0;
}
.match-column-select { min-width: 160px; }
.table-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-height: calc(100vh - 220px);
}
.data-table {
  border-collapse: collapse;
  width: 100%;
  font-size: 13px;
}
.data-table th, .data-table td {
  border: 1px solid var(--border);
  padding: 4px 8px;
  text-align: left;
  white-space: nowrap;
}
.data-table th {
  background: #f3f4f6;
  position: sticky;
  top: 0;
  z-index: 1;
}
.data-table tr.row-matched td {
  background: var(--highlight);
  outline: 2px solid var(--highlight-border);
  outline-offset: -2px;
}
.data-table tr.row-clickable { cursor: pointer; }
.data-table tr.row-clickable:hover td { background: #f0f4ff; }

/* ---------- Modal (shared by Import + OCR) ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.45);
}
.modal-card {
  position: relative;
  background: var(--panel);
  border-radius: 10px;
  box-shadow: 0 10px 40px rgba(0,0,0,.3);
  width: min(560px, 92vw);
  max-height: 90vh;
  overflow: auto;
  z-index: 1;
  display: flex;
  flex-direction: column;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.modal-header h2 { margin: 0; font-size: 16px; }
.icon-btn {
  border: none;
  background: none;
  font-size: 22px;
  line-height: 1;
  padding: 0 6px;
}
.modal-body {
  padding: 16px;
  display: flex;
  gap: 16px;
  align-items: flex-start;
}
.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

/* import modal specifics */
.import-body { flex-direction: column; gap: 12px; }
.import-field { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.import-file-list { font-size: 12px; word-break: break-word; flex-basis: 100%; }
.import-hint { font-size: 11px; color: var(--muted); margin: 0; }

/* OCR modal specifics */
.crop-wrap {
  flex: 0 0 200px;
  background: #f3f4f6;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
}
#cropImg {
  width: 100%;
  height: auto;
  image-rendering: pixelated;
}
.ocr-fields { flex: 1 1 auto; display: flex; flex-direction: column; gap: 10px; }
.ocr-current { font-size: 13px; color: var(--muted); }
#ocrInput { width: 100%; padding: 8px; font-size: 16px; }
.ocr-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.ocr-hint { font-size: 11px; color: var(--muted); margin: 4px 0 0; }

/* ---------- Import progress bar (inside loading overlay) ---------- */
.import-progress {
  width: min(420px, 86vw);
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.import-progress-bar {
  width: 100%;
  height: 10px;
  background: #e5e7eb;
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.import-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width .15s ease;
}
.import-progress-text {
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 820px) {
  .card-body { flex-direction: column; }
  .card-left, .card-right { flex: 1 1 100%; }
  .image-viewport { height: 50vh; }
  .modal-body { flex-direction: column; }
  .crop-wrap { flex: 0 0 auto; width: 100%; }
}
