* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

:root {
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --bg: #f5f6fa;
  --card: #ffffff;
  --text: #1f2430;
  --muted: #6b7280;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --border: #d7dae2;
}

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

#appHeader {
  background: var(--primary);
  color: #fff;
  padding: 14px 16px;
  position: sticky; top: 0; z-index: 10;
}
#appHeader h1 { font-size: 1.15rem; font-weight: 600; }

#main { padding: 16px; padding-bottom: 84px; max-width: 560px; margin: 0 auto; }

.tab { display: none; }
.tab.active { display: block; }

/* ---------- buttons ---------- */
.btn {
  display: block; width: 100%;
  border: none; border-radius: 12px;
  padding: 12px 16px; margin-top: 12px;
  font-size: 1rem; font-family: inherit;
  cursor: pointer; text-align: center;
}
.btn-big { padding: 16px; font-size: 1.1rem; font-weight: 600; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }
.btn-secondary { background: #fff; color: var(--primary); border: 1.5px solid var(--primary); }
.btn-ghost { background: transparent; color: var(--muted); width: auto; }
.btn:disabled { opacity: .5; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }

/* ---------- fields ---------- */
.field { margin-top: 14px; }
.field label { display: block; font-weight: 600; margin-bottom: 6px; font-size: .95rem; }
.field input, .field select {
  width: 100%; padding: 12px; font-size: 1rem; font-family: inherit;
  border: 1.5px solid var(--border); border-radius: 10px; background: #fff;
}
.hint { font-size: .82rem; color: var(--muted); margin-top: 6px; line-height: 1.5; }
.hint.success { color: var(--green); }
.notice {
  background: #fef3c7; border: 1px solid #fcd34d; color: #92400e;
  padding: 12px; border-radius: 10px; margin-top: 14px; font-size: .92rem;
}

.hidden { display: none !important; }

/* ---------- answer key cards ---------- */
.key-card {
  background: var(--card); border-radius: 14px; padding: 14px;
  margin-bottom: 12px; box-shadow: 0 1px 4px rgba(0,0,0,.07);
  display: flex; align-items: center; gap: 12px;
}
.key-card .key-info { flex: 1; min-width: 0; }
.key-card .key-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.key-card .key-meta { font-size: .8rem; color: var(--muted); margin-top: 2px; }
.key-card .key-thumb {
  font-size: .6rem; background: var(--bg); border-radius: 8px; padding: 4px 8px;
  font-weight: 700; color: var(--primary); white-space: nowrap;
}
.key-card button { background: none; border: none; font-size: 1.2rem; cursor: pointer; padding: 8px; }
.empty-msg { text-align: center; color: var(--muted); padding: 32px 12px; }

/* ---------- overlay ---------- */
#overlay {
  position: fixed; inset: 0; background: var(--bg); z-index: 50;
  overflow-y: auto; padding-bottom: 24px;
}
#overlayHeader {
  display: flex; align-items: center; gap: 8px;
  background: #fff; padding: 8px 12px; position: sticky; top: 0; z-index: 5;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
#overlayHeader .btn { margin: 0; padding: 8px 10px; }
#overlayTitle { font-weight: 600; }
.step { padding: 16px; max-width: 560px; margin: 0 auto; }
.step-hint { text-align: center; color: var(--muted); margin: 10px 0 16px; line-height: 1.6; }

/* ---------- crop ---------- */
#cropWrap { display: flex; justify-content: center; }
#cropCanvas { max-width: 100%; border-radius: 10px; touch-action: none; display: block; }

/* ---------- spinner ---------- */
.spinner {
  width: 52px; height: 52px; margin: 48px auto 20px;
  border: 5px solid var(--border); border-top-color: var(--primary);
  border-radius: 50%; animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- grid table ---------- */
.sgrid { border-collapse: collapse; margin: 0 auto; background: #fff; }
.sgrid td {
  border: 1px solid #b9bdc9; text-align: center; vertical-align: middle;
  padding: 0; position: relative;
}
.sgrid td.blockR { border-right: 2.5px solid #333; }
.sgrid td.blockB { border-bottom: 2.5px solid #333; }
.sgrid tr:first-child td { border-top: 2.5px solid #333; }
.sgrid td:first-child { border-left: 2.5px solid #333; }
.sgrid tr:last-child td { border-bottom: 2.5px solid #333; }
.sgrid td:last-child { border-right: 2.5px solid #333; }
.sgrid input {
  width: 100%; height: 100%; border: none; outline: none; background: transparent;
  text-align: center; font-size: 1.15rem; font-family: inherit; padding: 0;
}
.sgrid input:focus { background: #eef2ff; }

.sgrid.result td { cursor: pointer; }
.sgrid td.ok { background: var(--green-bg); }
.sgrid td.bad { background: var(--red-bg); color: var(--red); font-weight: 700; }
.sgrid td .correct-val {
  display: block; font-size: .55em; color: var(--green); font-weight: 600; line-height: 1;
}
.sgrid td .cell-main { line-height: 1.2; }

/* preview grid (read-only, small) */
.sgrid.preview td { font-size: .8rem; }

#resultScore {
  text-align: center; margin: 8px 0 16px;
}
#resultScore .score-big { font-size: 2.2rem; font-weight: 700; }
#resultScore .score-perfect { color: var(--green); }
#resultScore .score-partial { color: var(--red); }
#resultScore .score-label { color: var(--muted); }

/* ---------- bottom nav ---------- */
#bottomNav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex; background: #fff; border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.nav-btn {
  flex: 1; background: none; border: none; padding: 8px 0 10px;
  font-size: 1.3rem; color: var(--muted); cursor: pointer; font-family: inherit;
}
.nav-btn span { display: block; font-size: .72rem; margin-top: 2px; }
.nav-btn.active { color: var(--primary); font-weight: 600; }

/* ---------- toast ---------- */
#toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%);
  background: rgba(30,30,40,.92); color: #fff; padding: 12px 18px;
  border-radius: 12px; font-size: .92rem; z-index: 100; max-width: 90vw;
  text-align: center;
}
