:root {
  --ink: #17283b;
  --muted: #657488;
  --line: #dce4ec;
  --paper: #fff;
  --bg: #f3f6f8;
  --navy: #17324d;
  --blue: #226f8f;
  --teal: #0d7c78;
  --gold: #d7a52b;
  --green: #147d50;
  --green-bg: #eaf8f0;
  --red: #b53a3f;
  --red-bg: #fff0f0;
  --shadow: 0 14px 38px rgba(25, 45, 66, .11);
  font-family: Tahoma, "Noto Sans Thai", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--bg); overflow-x: hidden; }
body { margin: 0; min-height: 100vh; min-height: 100dvh; overflow-x: hidden; }
button, input { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { touch-action: manipulation; }

.loading, .error-box {
  max-width: 560px;
  margin: 20vh auto;
  padding: 28px;
  text-align: center;
  background: white;
  border: 1px solid var(--line);
  border-radius: 18px;
}

.portal-shell, .catalog-shell {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: max(24px, env(safe-area-inset-top)) 0 max(24px, env(safe-area-inset-bottom));
}

.portal-hero {
  min-height: 210px;
  display: flex;
  align-items: center;
  gap: 26px;
  padding: 30px 34px;
  color: white;
  background: linear-gradient(125deg, #132c45, #1c566d 70%, #16807a);
  border-radius: 24px;
  box-shadow: var(--shadow);
}

.brand-mark {
  display: grid;
  place-items: center;
  flex: 0 0 92px;
  height: 92px;
  border: 1px solid rgba(255,255,255,.28);
  border-radius: 26px;
  color: #ffe393;
  background: rgba(255,255,255,.1);
  font: 700 52px Georgia, serif;
}

.eyebrow { margin: 0 0 7px; color: #dceff5; font-size: 12px; font-weight: 800; letter-spacing: .15em; }
.portal-hero h1, .catalog-hero h1 { margin: 0; font-size: clamp(28px, 4vw, 48px); line-height: 1.18; }
.portal-hero p:not(.eyebrow) { max-width: 680px; margin: 10px 0 0; color: #e4eef3; }
.portal-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; margin-top: 20px; }
.book-card {
  min-width: 0;
  padding: 24px;
  color: inherit;
  text-decoration: none;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: 0 8px 28px rgba(23,50,77,.07);
  transition: transform .18s ease, border-color .18s ease, box-shadow .18s ease;
}
.book-card:hover { transform: translateY(-2px); border-color: #9db7c5; box-shadow: var(--shadow); }
.book-index { color: var(--teal); font-size: 12px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.book-card h2 { min-height: 58px; margin: 8px 0 16px; font-size: clamp(20px, 2.2vw, 27px); line-height: 1.35; }
.book-stats { display: flex; gap: 10px; flex-wrap: wrap; }
.book-stats span { padding: 7px 10px; color: var(--muted); background: #f1f5f7; border-radius: 9px; font-size: 13px; }
.book-stats b { color: var(--ink); }
.open-book { display: flex; justify-content: space-between; margin-top: 21px; padding-top: 16px; border-top: 1px solid var(--line); color: var(--blue); font-weight: 800; }
.site-note { padding: 18px 4px 0; color: var(--muted); text-align: center; font-size: 12px; }

.catalog-hero {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 18px;
  align-items: end;
  padding: 24px 28px;
  color: white;
  background: linear-gradient(125deg, #17324d, #226f8f);
  border-radius: 20px;
}
.catalog-hero p { margin: 7px 0 0; color: #dceaf1; }
.back-link { display: inline-block; margin-bottom: 12px; color: #d8edf4; font-weight: 700; text-decoration: none; }
.catalog-summary { display: flex; gap: 18px; text-align: right; }
.catalog-summary b { display: block; font-size: 23px; }
.catalog-summary span { color: #dceaf1; font-size: 12px; }
.catalog-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 18px 0 12px; }
.catalog-toolbar h2 { margin: 0; font-size: 20px; }
.progress-note { color: var(--muted); font-size: 13px; }
.test-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.test-card {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  min-width: 0;
  min-height: 155px;
  padding: 15px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 14px;
}
.test-tag { color: var(--teal); font-size: 11px; font-weight: 900; letter-spacing: .08em; }
.test-card h3 { margin: 5px 0 8px; font-size: 16px; line-height: 1.35; }
.test-meta { color: var(--muted); font-size: 11.5px; line-height: 1.55; }
.mini-progress { height: 4px; margin: 11px 0 9px; overflow: hidden; background: #e7edf1; border-radius: 99px; }
.mini-progress i { display: block; height: 100%; background: var(--teal); }
.test-bottom { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.status { color: var(--muted); font-size: 11px; }
.status.done { color: var(--green); font-weight: 800; }
.start-button, .primary-button, .home-button, .choice, .retry-button {
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 800;
}
.start-button { padding: 8px 12px; color: white; background: var(--navy); font-size: 12px; }

.quiz-topbar {
  position: sticky;
  z-index: 20;
  top: 0;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  color: white;
  background: rgba(23,50,77,.98);
  box-shadow: 0 3px 16px rgba(18,35,50,.18);
}
.topbar-inner { width: min(1360px, 100%); margin: 0 auto; display: grid; grid-template-columns: auto minmax(0,1fr) auto; gap: 12px; align-items: center; }
.home-button { padding: 9px 12px; color: white; background: rgba(255,255,255,.12); }
.quiz-heading { min-width: 0; }
.quiz-heading b, .quiz-heading span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.quiz-heading b { font-size: 14px; }
.quiz-heading span { color: #cfe0e8; font-size: 11px; }
.score-box { padding: 8px 11px; color: #fff3bd; border: 1px solid rgba(255,255,255,.22); border-radius: 10px; font-weight: 800; font-size: 13px; }
.top-progress { grid-column: 1 / -1; height: 3px; overflow: hidden; background: rgba(255,255,255,.14); border-radius: 99px; }
.top-progress i { display: block; height: 100%; background: #ffcf5b; transition: width .2s ease; }

.quiz-layout {
  width: min(1360px, calc(100% - 20px));
  height: calc(100dvh - 75px - env(safe-area-inset-top));
  min-height: 520px;
  margin: 10px auto;
  display: grid;
  grid-template-columns: minmax(0, 1.42fr) minmax(350px, .78fr);
  gap: 10px;
}
.quiz-panel { min-width: 0; min-height: 0; background: white; border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.source-panel { display: grid; grid-template-rows: auto minmax(0,1fr); }
.answer-panel { display: grid; grid-template-rows: minmax(0, 1fr); }
.panel-bar { display: flex; align-items: center; gap: 8px; min-height: 43px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.panel-bar b { font-size: 13px; }
.panel-bar span { margin-left: auto; color: var(--muted); font-size: 11px; }
.question-scroll, .answer-scroll { overflow: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.question-scroll { padding: 10px; background: #e8edf0; text-align: center; }
.question-image { display: block; width: auto; max-width: 100%; height: auto; margin: 0 auto; background: white; box-shadow: 0 3px 15px rgba(31,45,58,.15); }
.answer-scroll { padding: 18px; }
.q-kicker { color: var(--teal); font-size: 12px; font-weight: 900; letter-spacing: .06em; }
.answer-scroll h2 { margin: 4px 0 4px; font-size: 24px; }
.answer-hint { margin: 0 0 14px; color: var(--muted); font-size: 13px; line-height: 1.6; }
.choices { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.choice { min-height: 54px; color: var(--navy); background: #f1f5f7; border: 1px solid #cdd9df; font-size: 21px; }
.choice:hover:not(:disabled) { color: white; background: var(--blue); border-color: var(--blue); }
.choice:disabled { cursor: default; opacity: 1; }
.choice.selected { outline: 3px solid #7caec1; outline-offset: 1px; }
.choice.correct { color: white; background: var(--green); border-color: var(--green); }
.choice.wrong { color: white; background: var(--red); border-color: var(--red); }
.lock-note { display: flex; gap: 8px; margin: 13px 0 0; padding: 10px; color: var(--muted); background: #f8fafb; border-radius: 9px; font-size: 11px; line-height: 1.55; }
.feedback { margin-top: 14px; padding: 14px; border-radius: 12px; }
.feedback.good { background: var(--green-bg); border: 1px solid #b9e3ca; }
.feedback.bad { background: var(--red-bg); border: 1px solid #efc2c3; }
.feedback.loading-feedback { background: #f5f7f9; border: 1px solid var(--line); }
.feedback h3 { margin: 0 0 4px; }
.feedback p { margin: 3px 0; font-size: 13px; }
.solution-head { display: flex; justify-content: space-between; gap: 8px; margin: 13px 0 7px; color: var(--muted); font-size: 11px; }
.solution-head b { color: var(--ink); }
.solution-frame { max-height: 46vh; overflow: auto; padding: 7px; background: white; border: 1px solid var(--line); border-radius: 9px; -webkit-overflow-scrolling: touch; }
.solution-image { display: block; width: 100%; height: auto; }
.primary-button { width: 100%; margin-top: 12px; padding: 13px 16px; color: white; background: var(--navy); }
.retry-button { margin-top: 9px; padding: 9px 12px; color: white; background: var(--red); }

.result-shell { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.result-card { width: min(520px, 100%); padding: 32px; text-align: center; background: white; border: 1px solid var(--line); border-radius: 22px; box-shadow: var(--shadow); }
.result-card h1 { margin: 7px 0; font-size: 25px; }
.result-score { margin: 18px 0 4px; color: var(--teal); font-size: clamp(54px, 11vw, 78px); font-weight: 900; line-height: 1; }
.result-card p { color: var(--muted); }
.result-card .home-button { margin-top: 9px; color: white; background: var(--navy); }

@media (max-width: 1000px) {
  .test-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .quiz-layout { grid-template-columns: minmax(0, 1.2fr) minmax(320px, .8fr); }
}

@media (max-width: 760px) {
  .portal-shell, .catalog-shell { width: min(100% - 20px, 620px); }
  .portal-hero { min-height: 0; padding: 23px; }
  .brand-mark { display: none; }
  .portal-grid { grid-template-columns: 1fr; }
  .book-card h2 { min-height: 0; }
  .catalog-hero { grid-template-columns: 1fr; padding: 20px; }
  .catalog-summary { justify-content: flex-start; text-align: left; }
  .test-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .quiz-layout { width: calc(100% - 12px); height: auto; min-height: 0; grid-template-columns: 1fr; }
  .source-panel { height: min(52dvh, 500px); }
  .answer-panel { min-height: 430px; }
  .solution-frame { max-height: 42dvh; }
}

@media (max-width: 470px) {
  .portal-hero h1, .catalog-hero h1 { font-size: 28px; }
  .portal-hero { border-radius: 18px; }
  .book-card { padding: 19px; }
  .test-grid { grid-template-columns: 1fr; }
  .test-card { min-height: 136px; }
  .quiz-heading span { display: none; }
  .home-button { padding: 8px 9px; font-size: 12px; }
  .score-box { padding: 7px 8px; font-size: 12px; }
  .quiz-layout { margin-top: 6px; }
  .source-panel { height: 48dvh; min-height: 330px; }
  .answer-scroll { padding: 15px; }
  .choices { gap: 6px; }
  .choice { min-height: 50px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
