:root {
  --bg: #0f172a;
  --panel: #16213e;
  --panel-2: #1e2a47;
  --line: #2b3a5c;
  --text: #e6edf7;
  --muted: #93a3bd;
  --accent: #3b82f6;
  --green: #16a34a;
  --red: #dc2626;
  --amber: #d97706;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Tahoma, "Noto Sans Thai", sans-serif;
  font-size: 14px;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: linear-gradient(90deg, #111c33, #16213e);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 14px; }
.logo {
  width: 44px; height: 44px; border-radius: 12px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  font-weight: 800; letter-spacing: .5px;
}
.topbar h1 { margin: 0; font-size: 18px; }
.topbar p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.topbar-actions { display: flex; align-items: center; gap: 12px; }
.chk { display: flex; align-items: center; gap: 6px; color: var(--text); font-size: 13px; }

/* Settings */
.settings {
  display: flex; flex-wrap: wrap; gap: 18px;
  padding: 12px 20px; background: var(--panel-2);
  border-bottom: 1px solid var(--line);
}
.settings .field { display: flex; flex-direction: column; gap: 4px; }
.settings label { font-size: 11px; color: var(--muted); }
.settings input, .settings select {
  background: #0d1730; color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 8px; width: 150px;
}

/* Layout */
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 14px;
  padding: 14px 20px 24px;
  align-items: start;
}
@media (max-width: 1250px) { .grid { grid-template-columns: 1fr 1fr; } .result-panel { grid-column: 1 / -1; } }
@media (max-width: 820px) { .grid { grid-template-columns: 1fr; } }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; border-bottom: 1px solid var(--line);
  background: #141f39;
}
.panel-head h2 { margin: 0; font-size: 14px; }
.panel-body { padding: 12px 14px; }

/* Image */
.image-wrap {
  position: relative;
  background: #0b1220;
  border: 1px solid var(--line);
  border-radius: 8px;
  min-height: 220px;
  display: grid; place-items: center;
  overflow: hidden;
}
.image-wrap img { max-width: 100%; max-height: 380px; display: none; }
.image-wrap.has img { display: block; }
.overlay { position: absolute; display: none; pointer-events: none; z-index: 3; }
.image-wrap.small { min-height: 160px; }
.image-wrap.small img { max-height: 260px; }

/* Drag & drop feedback */
.panel.drag-active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(59,130,246,.35); }
.image-wrap.drag-over { border-color: var(--accent); }
.image-wrap.drag-over::after {
  content: "วางรูปภาพที่นี่";
  position: absolute; inset: 0; z-index: 4;
  display: grid; place-items: center;
  background: rgba(59,130,246,.18);
  color: #dbeafe; font-weight: 700; font-size: 15px;
  border: 2px dashed var(--accent); border-radius: 8px;
  pointer-events: none;
}
.empty { color: var(--muted); text-align: center; font-size: 13px; line-height: 1.6; padding: 20px; }
.image-wrap.has .empty { display: none; }

/* Controls */
.row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.input {
  flex: 1; min-width: 120px;
  background: #0d1730; color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px;
}
.btn {
  background: #24334f; color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 12px; cursor: pointer; font-size: 13px;
  transition: background .15s;
}
.btn:hover { background: #2c3d5f; }
.btn.primary { background: var(--accent); border-color: var(--accent); }
.btn.primary:hover { background: #2563eb; }
.btn.ghost { background: transparent; }
.btn.danger { background: #3a1c22; border-color: #6b1d25; color: #fca5a5; }
.btn.danger:hover { background: #4a1f27; }
.btn.verify {
  flex: 1; font-size: 15px; font-weight: 700; padding: 12px;
  background: linear-gradient(135deg, #2563eb, #16a34a);
  border: none;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* OCR */
details.ocr { margin-top: 10px; }
details.ocr summary { cursor: pointer; color: var(--muted); font-size: 12px; }
details.ocr pre {
  margin: 8px 0 0; padding: 10px; background: #0b1220; border: 1px solid var(--line);
  border-radius: 8px; max-height: 200px; overflow: auto; white-space: pre-wrap;
  font-family: Consolas, monospace; font-size: 12px; color: #cbd5e1;
}
textarea.ocr-text {
  margin: 8px 0 0; padding: 10px; width: 100%; min-height: 150px; resize: vertical;
  background: #0b1220; border: 1px solid var(--line); border-radius: 8px;
  font-family: Consolas, monospace; font-size: 12px; color: #cbd5e1; line-height: 1.5;
}
textarea.ocr-text:focus { outline: none; border-color: var(--accent); }

/* Pills / badges */
.pill { font-size: 11px; padding: 3px 9px; border-radius: 999px; }
.pill.muted { background: #24334f; color: var(--muted); }
.pill.ok { background: rgba(22,163,74,.2); color: #4ade80; }
.pill.bad { background: rgba(220,38,38,.2); color: #f87171; }

.badge {
  font-size: 16px; font-weight: 800; letter-spacing: 1px;
  padding: 6px 16px; border-radius: 8px;
}
.badge.idle { background: #24334f; color: var(--muted); }
.badge.pass { background: var(--green); color: #fff; box-shadow: 0 0 20px rgba(22,163,74,.5); }
.badge.fail { background: var(--red); color: #fff; box-shadow: 0 0 20px rgba(220,38,38,.5); }

/* Scores */
.scores { display: flex; gap: 10px; margin-top: 12px; }
.score {
  flex: 1; background: #0d1730; border: 1px solid var(--line);
  border-radius: 10px; padding: 10px; text-align: center;
}
.score-label { display: block; font-size: 11px; color: var(--muted); }
.score-value { display: block; font-size: 20px; font-weight: 800; margin-top: 4px; }
.score-value.ok { color: #4ade80; }
.score-value.bad { color: #f87171; }

/* Legend + diff */
.legend { display: flex; gap: 16px; margin: 12px 0; font-size: 11px; color: var(--muted); flex-wrap: wrap; }
.legend .sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.sw.red { background: var(--red); }
.sw.amber { background: var(--amber); }
.sw.blue { background: var(--accent); }

.diff-block { margin-top: 14px; }
.diff-block h3 { margin: 0 0 8px; font-size: 13px; color: var(--muted); font-weight: 600; }
.diff-list { display: flex; flex-wrap: wrap; gap: 6px; font-family: Consolas, monospace; font-size: 12px; }
.diff-list .none { color: #4ade80; }
.tok { padding: 3px 7px; border-radius: 6px; }
.tok.miss { background: rgba(220,38,38,.22); color: #fca5a5; border: 1px solid #7f1d1d; }
.tok.extra { background: rgba(59,130,246,.22); color: #93c5fd; border: 1px solid #1e3a8a; }
.tok.close { background: rgba(217,119,6,.22); color: #fcd34d; border: 1px solid #7c2d12; }
.tok.ok { background: rgba(22,163,74,.15); color: #86efac; }

/* Toast + busy */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: #1e2a47; border: 1px solid var(--line); color: var(--text);
  padding: 10px 18px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.4);
  z-index: 50; font-size: 13px;
}
.toast.error { border-color: #7f1d1d; color: #fca5a5; }
.toast.ok { border-color: #14532d; color: #86efac; }
.busy {
  position: fixed; inset: 0; background: rgba(6,10,20,.7);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  z-index: 60; color: var(--text);
}
.spinner {
  width: 46px; height: 46px; border-radius: 50%;
  border: 4px solid #2b3a5c; border-top-color: var(--accent);
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.hidden { display: none !important; }

/* ---------------- Mobile / PWA refinements ---------------- */
body { padding-bottom: env(safe-area-inset-bottom); }

@media (max-width: 820px) {
  html, body { font-size: 15px; }
  .topbar { padding: 10px 12px; flex-wrap: wrap; gap: 10px; }
  .brand { gap: 10px; }
  .brand .logo { width: 38px; height: 38px; border-radius: 10px; font-size: 15px; }
  .topbar h1 { font-size: 16px; }
  .topbar p { font-size: 11px; }
  .topbar-actions { width: 100%; justify-content: space-between; flex-wrap: wrap; gap: 8px; }
  .grid { padding: 10px 10px calc(20px + env(safe-area-inset-bottom)); gap: 12px; }
  .panel-body { padding: 10px; }
  .image-wrap { min-height: 180px; }
  .image-wrap img { max-height: 320px; width: 100%; object-fit: contain; }
  .row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .row .btn, .row .input { width: 100%; min-width: 0; }
  .row .btn { white-space: normal; text-align: center; }
  .btn { padding: 11px 14px; font-size: 14px; -webkit-tap-highlight-color: transparent; }
  .input, .settings input, .settings select, textarea.ocr-text { font-size: 16px; }
  .settings { padding: 10px 12px; gap: 12px; }
  .settings .field { flex: 1 1 140px; }
  .settings input, .settings select { width: 100%; }
  .scores { flex-direction: column; }
  .toast {
    left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom));
    transform: none; text-align: center;
  }
  textarea.ocr-text { min-height: 120px; }
}

@media (max-width: 480px) {
  .topbar .brand p { display: none; }
}

/* Crop dialog */
.crop-modal {
  position: fixed; inset: 0; z-index: 70; background: rgba(4,8,16,.88);
  display: flex; align-items: center; justify-content: center;
  padding: calc(10px + env(safe-area-inset-top)) 10px calc(10px + env(safe-area-inset-bottom));
  overscroll-behavior: contain;
}
.crop-panel {
  background: var(--panel, #131b2e); border: 1px solid var(--line, #26324b);
  border-radius: 14px; padding: 12px; width: 100%; max-width: 720px;
  max-height: 100%; display: flex; flex-direction: column; gap: 10px;
}
.crop-title { font-size: 15px; font-weight: 600; }
.crop-stage {
  display: flex; align-items: center; justify-content: center;
  overflow: auto; max-height: 58vh; background: #0a1120; border-radius: 10px;
}
.crop-frame { position: relative; line-height: 0; touch-action: none; }
.crop-img { display: block; max-width: 100%; max-height: 56vh; user-select: none; -webkit-user-drag: none; }
.crop-box {
  position: absolute; border: 2px solid #38bdf8; border-radius: 4px;
  box-shadow: 0 0 0 9999px rgba(4,8,16,.55); cursor: move; touch-action: none;
}
.crop-handle {
  position: absolute; width: 28px; height: 28px; background: #38bdf8;
  border: 2px solid #0a1120; border-radius: 50%;
}
.crop-handle.nw { left: -14px; top: -14px; }
.crop-handle.ne { right: -14px; top: -14px; }
.crop-handle.sw { left: -14px; bottom: -14px; }
.crop-handle.se { right: -14px; bottom: -14px; }
.crop-help { font-size: 12px; color: var(--muted, #94a3b8); }
.crop-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.crop-actions .btn { width: 100%; padding: 12px 10px; font-size: 14px; white-space: normal; }
.crop-actions .btn.ghost { grid-column: 1 / -1; }
