/* ==========================================================================
   ระบบตรวจข้อสอบ OMR — Stylesheet
   ========================================================================== */

@import url('https://fonts.googleapis.com/css2?family=Kanit:wght@500;600;700&family=Sarabun:wght@400;500;600;700&display=swap');

:root {
  --ink: #1c2333;
  --ink-soft: #4b5468;
  --ink-faint: #8891a3;
  --line: #e2e5ec;
  --line-soft: #eef0f5;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-raised: #ffffff;

  --brand: #2b4c8c;
  --brand-dark: #1e3766;
  --brand-light: #eaf0fd;
  --accent: #0f9d8f;
  --accent-light: #e3f7f4;

  --good: #16824f;
  --good-bg: #e6f6ee;
  --bad: #c0293c;
  --bad-bg: #fdecee;
  --warn: #b6791b;
  --warn-bg: #fdf3e2;

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 18px;
  --shadow-sm: 0 1px 2px rgba(28, 35, 51, 0.06);
  --shadow: 0 6px 20px rgba(28, 35, 51, 0.08);
  --shadow-lg: 0 16px 40px rgba(28, 35, 51, 0.14);

  --font-body: 'Sarabun', 'Noto Sans Thai', system-ui, sans-serif;
  --font-head: 'Kanit', 'Noto Sans Thai', system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 15.5px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 {
  font-family: var(--font-head);
  color: var(--ink);
  font-weight: 600;
  margin: 0 0 6px;
  letter-spacing: 0.01em;
}

h1 { font-size: 1.6rem; }
h2 { font-size: 1.25rem; }

p { margin: 0 0 8px; color: var(--ink-soft); }

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

small { color: var(--ink-faint); font-size: 0.82rem; }

code {
  background: var(--line-soft);
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 0.85em;
}

/* ---------- layout shell ---------- */

.topbar {
  background: linear-gradient(135deg, var(--brand-dark), var(--brand));
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 26px;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow);
}

.topbar strong {
  font-family: var(--font-head);
  font-size: 1.08rem;
  display: block;
}

.topbar > div span {
  display: block;
  font-size: 0.78rem;
  color: rgba(255, 255, 255, 0.72);
  margin-top: 1px;
}

.topbar nav {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
}

.topbar nav a {
  color: rgba(255, 255, 255, 0.88);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 0.92rem;
  font-weight: 500;
  transition: background 0.15s ease, color 0.15s ease;
}

.topbar nav a:hover {
  background: rgba(255, 255, 255, 0.16);
  text-decoration: none;
  color: #fff;
}

.container {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 24px 64px;
}

/* ---------- hero (index) ---------- */

.hero {
  background: linear-gradient(135deg, var(--brand) 0%, var(--accent) 130%);
  color: #fff;
  border-radius: var(--radius-lg);
  padding: 40px 36px;
  box-shadow: var(--shadow-lg);
  margin-bottom: 22px;
}

.hero h1 {
  color: #fff;
  font-size: 1.9rem;
  max-width: 620px;
  margin-bottom: 10px;
}

.hero p {
  color: rgba(255, 255, 255, 0.9);
  max-width: 560px;
  margin-bottom: 18px;
}

.hero .actions { margin: 0; }

.grid.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 16px;
  margin-bottom: 8px;
}

.grid.stats .card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}

.grid.stats .card b {
  display: block;
  font-family: var(--font-head);
  font-size: 1.7rem;
  color: var(--brand-dark);
}

.grid.stats .card span {
  color: var(--ink-faint);
  font-size: 0.86rem;
}

/* ---------- panels / cards ---------- */

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 26px 28px;
  box-shadow: var(--shadow-sm);
}

.panel.narrow {
  max-width: 420px;
  margin: 40px auto;
}

.panel.scanner { padding-bottom: 34px; }

.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

.page-head h1 { margin-bottom: 2px; }
.page-head p { margin: 0; color: var(--ink-faint); font-size: 0.92rem; }

/* ---------- buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 0.92rem;
  cursor: pointer;
  transition: transform 0.08s ease, box-shadow 0.15s ease, background 0.15s ease, border-color 0.15s ease;
}

.btn:hover {
  text-decoration: none;
  border-color: var(--brand);
  color: var(--brand-dark);
  box-shadow: var(--shadow-sm);
}

.btn:active { transform: translateY(1px); }

.btn.primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}

.btn.primary:hover {
  background: var(--brand-dark);
  border-color: var(--brand-dark);
  color: #fff;
}

.btn.danger {
  background: var(--bad-bg);
  border-color: rgba(192,41,60,0.3);
  color: var(--bad);
}

.btn.danger:hover:not(:disabled) {
  background: var(--bad);
  border-color: var(--bad);
  color: #fff;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn[id="autoScan"].on {
  background: var(--good-bg);
  border-color: var(--good);
  color: var(--good);
}

.actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 18px;
}

.sticky-actions {
  position: sticky;
  bottom: 0;
  background: linear-gradient(to top, var(--surface) 70%, rgba(255,255,255,0));
  padding: 16px 0 4px;
  margin-top: 10px;
}

/* ---------- forms ---------- */

.form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-top: 6px;
}

.form label,
.grid-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--ink-soft);
}

.grid-form {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px 20px;
  margin-top: 6px;
}

.grid-form .wide { grid-column: 1 / -1; }

.grid-form .check {
  flex-direction: row;
  align-items: center;
  font-weight: 600;
}

input, select, textarea, button {
  font-family: var(--font-body);
}

input[type="text"], input[type="search"], input[type="password"], input[type="number"],
input[type="file"], input:not([type]), select, textarea {
  border: 1.5px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 0.94rem;
  color: var(--ink);
  background: var(--surface);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  width: 100%;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-light);
}

input[readonly] {
  background: var(--line-soft);
  color: var(--ink-faint);
}

.form button.btn { align-self: flex-start; }

/* ---------- alerts ---------- */

.alert {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  margin-bottom: 16px;
  border: 1px solid transparent;
}

.alert.danger { background: var(--bad-bg); color: var(--bad); border-color: rgba(192,41,60,0.2); }
.alert.success { background: var(--good-bg); color: var(--good); border-color: rgba(22,130,79,0.2); }

/* ---------- tables ---------- */

.table-wrap {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  overflow: auto;
  box-shadow: var(--shadow-sm);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
  min-width: 640px;
}

thead th {
  text-align: left;
  font-family: var(--font-head);
  font-weight: 600;
  font-size: 0.8rem;
  letter-spacing: 0.03em;
  color: var(--ink-faint);
  text-transform: uppercase;
  background: var(--line-soft);
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line-soft);
  vertical-align: middle;
  color: var(--ink);
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--brand-light); }

.row-actions {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  white-space: nowrap;
}

.row-actions a {
  padding: 5px 10px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 600;
  background: var(--line-soft);
  color: var(--brand-dark);
}

.row-actions a:hover {
  background: var(--brand);
  color: #fff;
  text-decoration: none;
}

.row-actions-icons { gap: 6px; }
.row-actions-icons a.icon-btn {
  padding: 0;
  border-radius: 50%;
  background: var(--brand-light);
  color: var(--brand-dark);
}
.row-actions-icons a.icon-btn:hover {
  background: var(--brand);
  color: #fff;
}

.filter { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; }
.filter select { max-width: 260px; flex: 1 1 180px; }
.filter input[type="text"],
.filter input[type="search"] { max-width: 320px; flex: 1 1 220px; }

.search-filter {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.search-filter input[type="text"],
.search-filter input[type="search"] {
  max-width: 420px;
  flex: 1 1 260px;
}

.bulk-bar {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm) var(--radius-sm) 0 0;
  padding: 12px 16px;
  border-bottom: none;
}

.bulk-bar label.check {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--ink-soft);
  cursor: pointer;
}

.bulk-bar input[type="checkbox"] {
  width: 17px;
  height: 17px;
  accent-color: var(--brand);
  cursor: pointer;
}

.selected-count {
  font-size: 0.85rem;
  color: var(--ink-faint);
  flex: 1;
}

.bulk-bar + .table-wrap {
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
}

.col-check { width: 40px; }
.col-check input[type="checkbox"] {
  width: 17px;
  height: 17px;
  accent-color: var(--brand);
  cursor: pointer;
}

.row-check:disabled { opacity: 0.25; }

.empty-row {
  text-align: center;
  color: var(--ink-faint);
  padding: 32px 16px !important;
}

/* ---------- answer key grid ---------- */

.key-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 10px;
  margin: 18px 0;
}

.key-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--line-soft);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
}

.key-item b {
  width: 26px;
  text-align: right;
  color: var(--ink-faint);
  font-weight: 600;
  font-size: 0.85rem;
}

.key-item select { padding: 6px 8px; }

/* ---------- scanner ---------- */

/* ---------- scan stage: idle / camera / review ---------- */

#scanStage .camera-wrap,
#scanStage .canvas-wrap,
#scanStage #scanSummary,
#scanStage #saveForm { display: none; }

#scanStage[data-state="idle"] .scan-controls-bar { display: flex; }

#scanStage[data-state="camera"] .scan-controls-bar { display: none; }
#scanStage[data-state="camera"] .camera-wrap { display: block; }

#scanStage[data-state="review"] .scan-controls-bar { display: none; }
#scanStage[data-state="review"] .canvas-wrap { display: block; }
#scanStage[data-state="review"] #scanSummary { display: block; }
#scanStage[data-state="review"] #saveForm { display: block; }

.scan-controls-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
  margin-bottom: 16px;
}

.scan-controls-bar label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--ink-soft);
}

.scan-controls-bar select, .scan-controls-bar input[type="file"] { min-width: 220px; }

.scan-controls-bar label.check-inline {
  flex-direction: row;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}
.scan-controls-bar label.check-inline input { width: 16px; height: 16px; accent-color: var(--brand); }

.idle-note {
  font-size: 0.82rem;
  color: var(--ink-faint);
  max-width: 640px;
  margin: -4px 0 16px;
}

.camera-wrap {
  position: relative;
  width: 100%;
  max-width: 420px;
  aspect-ratio: 3 / 4;
  margin: 0 auto 14px;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
}

.camera-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  background: #000;
}

.camera-video.hidden { display: none; }
.camera-video.hidden ~ .live-overlay,
.camera-video.hidden ~ .frame-guide,
.camera-video.hidden ~ .cam-toolbar { display: none; }

.live-overlay {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none;
}

.frame-guide {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.frame-guide::before {
  content: "";
  /* สัดส่วนเดียวกับกระดาษ A5 แนวตั้ง (148 x 210 มม.) ให้วางกระดาษในกรอบนี้พอดี */
  aspect-ratio: 148 / 210;
  height: 86%;
  max-width: 78%;
  border: 2.5px dashed rgba(255, 255, 255, 0.92);
  border-radius: 10px;
  box-shadow: 0 0 0 2000px rgba(0, 0, 0, 0.32);
}

.cam-toolbar {
  position: absolute;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  z-index: 5;
}

.cam-toolbar-top {
  top: 0;
  justify-content: space-between;
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0));
}

.cam-toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.cam-toolbar-bottom {
  bottom: 0;
  flex-direction: column;
  gap: 12px;
  padding-bottom: 16px;
  background: linear-gradient(to top, rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0));
}

.cam-btn {
  border: none;
  border-radius: 999px;
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 0.82rem;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.92);
  color: var(--ink);
  padding: 8px 16px;
}

.cam-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.cam-count {
  font-size: 0.78rem;
  font-weight: 700;
  color: #fff;
  background: rgba(0, 0, 0, 0.4);
  border-radius: 999px;
  padding: 8px 14px;
}
.cam-count b { font-size: 0.9rem; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--brand-light);
  color: var(--brand-dark);
  border: none;
  text-decoration: none;
  transition: background 0.15s ease, color 0.15s ease, transform 0.08s ease;
}
.icon-btn:hover { background: var(--brand); color: #fff; text-decoration: none; }
.icon-btn:active { transform: translateY(1px); }
.icon-btn svg { width: 16px; height: 16px; }

.icon-row { display: flex; align-items: center; gap: 6px; }

.icon-btn-disabled {
  background: var(--line-soft);
  color: var(--ink-faint);
  opacity: 0.6;
  cursor: not-allowed;
}
.icon-btn-disabled:hover { background: var(--line-soft); color: var(--ink-faint); }
.cam-btn-round {
  width: 38px;
  height: 38px;
  padding: 0;
  font-size: 1.3rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.4);
  color: #fff;
}

.cam-btn-shutter {
  width: 62px;
  height: 62px;
  padding: 0;
  border-radius: 50%;
  background: #fff;
  border: 4px solid rgba(255, 255, 255, 0.5);
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.35);
}
.cam-btn-shutter:active { background: #e2e5ec; }

.cam-btn-light {
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
}

.recent-panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  z-index: 8;
  background: rgba(15, 18, 26, 0.92);
  display: flex;
  flex-direction: column;
}
.recent-panel.hidden { display: none; }

.recent-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  color: #fff;
  font-weight: 700;
  font-size: 0.92rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
}

.recent-close {
  background: rgba(255, 255, 255, 0.15);
  border: none;
  color: #fff;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 0.85rem;
}

.recent-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 10px;
}

.recent-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: rgba(255, 255, 255, 0.06);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  margin-bottom: 6px;
  color: #fff;
}

.recent-item .recent-info {
  font-size: 0.82rem;
  line-height: 1.4;
  color: #fff;
  text-decoration: none;
  display: block;
  flex: 1;
  min-width: 0;
}
.recent-item .recent-info:hover { text-decoration: underline; }
.recent-item .recent-info b { font-size: 0.9rem; }
.recent-item .recent-info small { color: rgba(255, 255, 255, 0.6); }
.recent-item.dup .recent-info small { color: #ffce7a; }

.recent-item .recent-undo {
  background: rgba(192, 41, 60, 0.85);
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.76rem;
  font-weight: 700;
  cursor: pointer;
  flex: 0 0 auto;
}
.recent-item .recent-undo:disabled { opacity: 0.5; cursor: not-allowed; }

.recent-empty {
  color: rgba(255, 255, 255, 0.55);
  text-align: center;
  padding: 24px 10px;
  font-size: 0.85rem;
}

.canvas-wrap {
  position: relative;
  text-align: center;
  margin-bottom: 16px;
}

.canvas-toolbar {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  justify-content: space-between;
  z-index: 5;
}

.auto-status {
  text-align: center;
  font-weight: 600;
  font-size: 0.82rem;
  color: #fff;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 999px;
  padding: 8px 16px;
  max-width: 88%;
}

.canvas-wrap canvas {
  max-width: 100%;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  cursor: crosshair;
  background: #eee;
}

.scan-summary {
  max-width: 560px;
  margin: 0 auto 16px;
}

.scan-summary .summary-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
}

.scan-summary .summary-card h3 { margin-bottom: 10px; }

.scan-summary .summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(90px, 1fr));
  gap: 10px;
  margin-top: 10px;
}

.scan-summary .summary-grid div {
  background: var(--line-soft);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  text-align: center;
}

.scan-summary .summary-grid b {
  display: block;
  font-family: var(--font-head);
  font-size: 1.2rem;
  color: var(--brand-dark);
}

.scan-summary .summary-grid span {
  font-size: 0.74rem;
  color: var(--ink-faint);
}

.saveForm.hidden, .hidden { display: none !important; }

/* ---------- misc ---------- */

@media (max-width: 900px) {
  .grid-form { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 640px) {
  .topbar { flex-direction: column; align-items: flex-start; }
  .container { padding: 18px 14px 48px; }
  .grid-form { grid-template-columns: 1fr; }
  .panel { padding: 20px; }
  .page-head { flex-direction: column; }
}
