:root {
  --theme-header: #082f28;
  --theme-header-soft: #0d5c49;
  --theme-accent: #0f8a63;
  --theme-accent-strong: #0b6d53;
  --theme-accent-soft: #e2f6eb;
  --theme-bg: #f1f8f3;
  --theme-bg-soft: #e2f4e8;
  --theme-shadow: rgba(8, 63, 46, 0.18);
  --theme-action: #c6f36a;
  --theme-action-text: #0d2c24;
  --ink: #163129;
  --muted: #5f746b;
  --green: var(--theme-accent);
  --line: color-mix(in srgb, var(--theme-accent) 22%, #fff);
  --paper: #fff;
  --shadow: 0 18px 55px var(--theme-shadow);
}

body[data-theme="carmine"] {
  --theme-header: #7a1020; --theme-header-soft: #a30f24; --theme-accent: #a30f24;
  --theme-accent-strong: #7a1020; --theme-accent-soft: #fde8ec; --theme-bg: #fff6f8;
  --theme-shadow: rgba(163, 15, 36, 0.2); --theme-action: #f4c430; --theme-action-text: #3b0a12;
}

body[data-theme="blue"] {
  --theme-header: #163b73; --theme-header-soft: #2563eb; --theme-accent: #2563eb;
  --theme-accent-strong: #1d4ed8; --theme-accent-soft: #e7efff; --theme-bg: #f4f8ff;
  --theme-shadow: rgba(37, 99, 235, 0.2); --theme-action: #7dd3fc; --theme-action-text: #0f2544;
}

body[data-theme="maroon"] {
  --theme-header: #4a1014; --theme-header-soft: #7f1d1d; --theme-accent: #7f1d1d;
  --theme-accent-strong: #5f1518; --theme-accent-soft: #f7e6e6; --theme-bg: #fbf4f4;
  --theme-shadow: rgba(127, 29, 29, 0.2); --theme-action: #f2c94c; --theme-action-text: #3d0c10;
}

body[data-theme="green"] {
  --theme-header: #14532d; --theme-header-soft: #15803d; --theme-accent: #15803d;
  --theme-accent-strong: #166534; --theme-accent-soft: #e8f7ee; --theme-bg: #f3fbf6;
  --theme-shadow: rgba(21, 128, 61, 0.2); --theme-action: #bbf7d0; --theme-action-text: #0f3d22;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background:
    linear-gradient(rgba(126, 151, 196, 0.12) 1px, transparent 1px),
    linear-gradient(90deg, rgba(126, 151, 196, 0.12) 1px, transparent 1px),
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.92), color-mix(in srgb, var(--theme-bg) 72%, #fff) 42%, var(--theme-bg) 100%);
  background-size: 28px 28px, 28px 28px, auto;
  color: var(--ink);
  font-family: "Kanit", sans-serif;
  font-weight: 400;
}

.topbar {
  min-height: 78px;
  padding: 12px clamp(20px, 4vw, 56px);
  background: linear-gradient(135deg, var(--theme-header), var(--theme-header-soft) 68%, var(--theme-header));
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  box-shadow: 0 16px 36px var(--theme-shadow);
}

.brand { display: flex; align-items: center; gap: 13px; color: inherit; text-decoration: none; }
.brand img { width: 52px; height: 52px; }
.brand span { display: grid; }
.brand strong { font-size: 1.18rem; }
.brand small { opacity: 0.78; }
.topbar nav { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.topbar nav a { padding: 10px 14px; border-radius: 999px; color: #fff; text-decoration: none; border: 1px solid rgba(255, 255, 255, 0.18); }
.topbar nav a.active { background: var(--theme-action); color: var(--theme-action-text); border-color: var(--theme-action); }

.theme-switcher {
  display: grid;
  grid-template-columns: auto minmax(120px, 1fr);
  align-items: center;
  gap: 8px;
  min-height: 46px;
  padding: 5px 6px 5px 12px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 999px;
  color: #fff;
  font-size: 0.82rem;
  font-weight: 600;
}

.theme-switcher select {
  width: 100%; height: 34px; padding: 0 30px 0 12px; border: 0; border-radius: 999px;
  background: rgba(255, 255, 255, 0.94); color: var(--theme-header);
}

main { width: min(1460px, calc(100% - 40px)); margin: 30px auto 70px; }
.hero {
  min-height: 380px;
  padding: 40px clamp(28px, 5vw, 68px);
  border-radius: 30px;
  background:
    radial-gradient(circle at 85% 25%, color-mix(in srgb, var(--theme-action) 24%, transparent), transparent 32%),
    linear-gradient(100deg, rgba(6, 34, 28, 0.94) 0%, rgba(8, 47, 40, 0.8) 32%, rgba(8, 47, 40, 0.4) 62%, rgba(8, 47, 40, 0.12) 100%),
    url("dsd-hero-bg.png");
  background-color: var(--theme-header);
  background-position: center, center, center 60%;
  background-size: auto, auto, cover;
  background-repeat: no-repeat;
  color: #fff;
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 30px;
  box-shadow: var(--shadow);
}
.hero h1 { font-size: clamp(2.2rem, 5vw, 4.25rem); line-height: 1.05; margin: 8px 0 14px; }
.hero p { max-width: 870px; margin: 0; font-size: 1.08rem; opacity: 0.9; }
.eyebrow { text-transform: uppercase; letter-spacing: 0.13em; color: var(--theme-accent); font-size: 0.78rem; font-weight: 700; margin: 0; }
.hero .eyebrow { color: var(--theme-action); }
.source-note { max-width: 330px; padding: 17px 20px; border-radius: 18px; background: rgba(255, 255, 255, 0.12); border: 1px solid rgba(255, 255, 255, 0.25); font-size: 0.88rem; }
.search-panel { position: relative; margin: -20px 24px 24px; padding: 22px; border-radius: 22px; background: rgba(255, 255, 255, 0.96); box-shadow: var(--shadow); border: 1px solid var(--line); }
form { display: grid; grid-template-columns: minmax(300px, 2fr) repeat(3, minmax(150px, 1fr)) auto auto; gap: 12px; align-items: end; }
label { display: grid; gap: 7px; font-size: 0.83rem; color: var(--muted); }
input, select, button { font: inherit; }
input, select { width: 100%; height: 48px; border: 1px solid var(--line); border-radius: 12px; background: #fff; padding: 0 13px; color: var(--ink); }
.search-field div { position: relative; }
.search-field svg { position: absolute; width: 20px; left: 14px; top: 14px; fill: none; stroke: var(--muted); stroke-width: 2; }
.search-field input { padding-left: 44px; }
button { height: 48px; border: 0; border-radius: 12px; padding: 0 20px; background: var(--theme-accent); color: #fff; cursor: pointer; font-weight: 600; }
button:hover { filter: brightness(0.95); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
.secondary { background: var(--theme-accent-soft); color: var(--theme-accent-strong); border: 1px solid var(--line); }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin: 26px 0; }
.stats article { padding: 22px; border-radius: 18px; background: var(--paper); border: 1px solid var(--line); box-shadow: 0 12px 30px color-mix(in srgb, var(--theme-shadow) 55%, transparent); }
.stats strong { display: block; color: var(--theme-accent); font-size: 2rem; }
.stats span { color: var(--muted); }
.results { background: var(--paper); border: 1px solid var(--line); border-radius: 24px; overflow: hidden; }
.results > header { padding: 24px; display: flex; align-items: end; justify-content: space-between; border-bottom: 1px solid var(--line); }
.results h2 { margin: 3px 0 0; font-size: 1.55rem; }
.results > header > strong { color: var(--theme-accent); }
.status { text-align: center; padding: 40px; color: var(--muted); }
.status.hidden { display: none; }
.result-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; padding: 20px; }
.result-card { display: flex; flex-direction: column; min-height: 270px; padding: 21px; border: 1px solid var(--line); border-radius: 18px; background: linear-gradient(160deg, #fff, color-mix(in srgb, var(--theme-accent-soft) 42%, #fff)); }
.result-card:hover { border-color: color-mix(in srgb, var(--theme-accent) 58%, #fff); box-shadow: 0 12px 28px color-mix(in srgb, var(--theme-shadow) 55%, transparent); }
.result-card h3 { font-size: 1.15rem; margin: 10px 0 3px; }
.result-card .english { color: var(--muted); font-size: 0.9rem; min-height: 24px; }
.group { font-size: 0.85rem; color: var(--muted); margin: 13px 0; }
.badges { display: flex; gap: 6px; flex-wrap: wrap; }
.badge { padding: 5px 9px; border-radius: 999px; background: var(--theme-accent-soft); color: var(--theme-accent-strong); font-size: 0.75rem; }
.badge.warn { background: #fff3d3; color: #805c00; }
.card-foot { margin-top: auto; padding-top: 16px; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.card-foot small { color: var(--muted); }
.pagination { padding: 16px 20px 22px; display: flex; justify-content: center; align-items: center; gap: 14px; }
.pagination button { height: 42px; background: #fff; color: var(--theme-accent); border: 1px solid var(--line); }
dialog { width: min(1120px, calc(100% - 30px)); height: min(88vh, 920px); padding: 0; border: 0; border-radius: 26px; box-shadow: 0 30px 100px rgba(0, 30, 23, 0.35); }
dialog::backdrop { background: color-mix(in srgb, var(--theme-header) 78%, transparent); backdrop-filter: blur(5px); }
.dialog-shell { height: 100%; display: grid; grid-template-rows: auto 1fr; }
.dialog-head { padding: 24px 28px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; gap: 20px; }
.dialog-head h2 { margin: 4px 0; font-size: 1.7rem; }
.dialog-head p { margin: 0; color: var(--muted); }
.dialog-head button { font-size: 2rem; width: 45px; padding: 0; background: var(--theme-accent-soft); color: var(--ink); }
.dialog-body { overflow: auto; padding: 24px 28px; }
.source-links { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 20px; }
.source-links a, .doc-link { display: inline-flex; padding: 9px 13px; border-radius: 10px; background: var(--theme-accent-soft); color: var(--theme-accent-strong); text-decoration: none; border: 1px solid var(--line); }
.level-section { border: 1px solid var(--line); border-radius: 16px; margin: 12px 0; overflow: hidden; }
.level-section summary { cursor: pointer; padding: 17px 19px; font-weight: 600; background: color-mix(in srgb, var(--theme-accent-soft) 62%, #fff); }
.level-content { padding: 8px 19px 20px; }
.detail-block { border-top: 1px solid var(--line); padding: 15px 0; }
.detail-block:first-child { border: 0; }
.detail-block h4 { margin: 0 0 8px; color: var(--theme-accent); }
.detail-text { white-space: pre-wrap; line-height: 1.7; color: var(--ink); }
.missing { padding: 11px 13px; border-radius: 10px; background: #fff6dc; color: #765a12; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); }
footer { text-align: center; color: var(--muted); padding: 22px; }

@media (max-width: 1100px) {
  form { grid-template-columns: repeat(2, 1fr); }
  .search-field { grid-column: 1 / -1; }
  .result-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 760px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .topbar nav { width: 100%; overflow: auto; flex-wrap: nowrap; padding-bottom: 3px; }
  .topbar nav a { white-space: nowrap; }
  .theme-switcher { min-width: 230px; flex: 0 0 auto; }
  .hero { padding: 30px 24px; align-items: start; flex-direction: column; }
  .search-panel { margin: -12px 8px 18px; }
  form, .stats, .result-grid { grid-template-columns: 1fr; }
  .search-field { grid-column: auto; }
  .result-grid { padding: 13px; }
  main { width: min(100% - 20px, 1460px); margin-top: 14px; }
  .dialog-head, .dialog-body { padding: 19px; }
  .stats { grid-template-columns: repeat(2, 1fr); }
}

.detail-section {
  margin-top: 18px;
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 16px;
}
.detail-section h3 {
  margin: 0 0 11px;
  color: var(--theme-accent);
  font-size: 1.08rem;
}
.badge.neutral {
  background: var(--theme-bg-soft);
  color: var(--muted);
}
.course-table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 13px;
}
.course-table {
  width: 100%;
  min-width: 900px;
  border-collapse: collapse;
}
.course-table th,
.course-table td {
  padding: 12px 13px;
  text-align: left;
  border-bottom: 1px solid var(--line);
  font-size: 0.82rem;
  vertical-align: top;
}
.course-table th {
  background: var(--theme-bg-soft);
  color: var(--muted);
  font-weight: 500;
}
.course-table tr:last-child td {
  border-bottom: 0;
}
.course-link,
.course-name-link {
  color: var(--green);
  font-weight: 600;
  text-decoration: none;
}
.course-link:hover,
.course-name-link:hover {
  text-decoration: underline;
}
.reference-text {
  max-width: 360px;
  color: var(--muted);
  line-height: 1.55;
}
.review-note {
  color: #a96600;
  font-size: 0.75rem;
}
.course-action-cell {
  width: 190px;
}
.create-plan-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 40px;
  padding: 8px 12px;
  border-radius: 11px;
  background: var(--green);
  color: #fff;
  text-decoration: none;
  font-weight: 600;
  white-space: nowrap;
  box-shadow: 0 7px 18px var(--theme-shadow);
}
.create-plan-link:hover {
  background: var(--theme-accent-strong);
}
.create-plan-link svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.load-more {
  display: block;
  margin: 15px auto 0;
  height: 42px;
  padding: 0 18px;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: #fff;
  color: var(--green);
  font-weight: 600;
  cursor: pointer;
}
.load-more[hidden] {
  display: none;
}
@media (max-width: 800px) {
  .reference-text { max-width: 220px; }
}
