/* ==========================================================================
   Base: reset, typography, layout และ component ที่ใช้ร่วมกันทุกหน้า
   ต้องโหลดหลัง tokens.css
   ========================================================================== */

*,
*::before,
*::after { box-sizing: border-box; }

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background-color: var(--graphite-900);
  /* ลาย "node field": จุดเล็กๆ แทนโหนดในเครือข่าย จางลงด้านล่าง */
  background-image:
    radial-gradient(1200px 520px at 85% -10%, rgba(142, 113, 211, 0.13), transparent 70%),
    radial-gradient(circle at 1px 1px, rgba(198, 179, 239, 0.07) 1px, transparent 0);
  background-size: auto, 28px 28px;
  background-attachment: fixed, fixed;
  color: var(--text);
  font-family: var(--font-body);
  font-size: var(--fs-md);
  line-height: var(--lh-body);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

img, svg { max-width: 100%; }

/* attribute hidden ต้องชนะ display ของ component เสมอ (เช่น .quiz-result ที่เป็น grid) */
[hidden] { display: none !important; }

a { color: var(--iris-300); text-underline-offset: 3px; }
a:hover { color: var(--iris-200); }

button, input, select, textarea { font: inherit; color: inherit; }

h1, h2, h3, h4 {
  font-family: var(--font-display);
  line-height: var(--lh-tight);
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 0;
}

p { margin: 0 0 var(--space-3); }
p:last-child { margin-bottom: 0; }

code, kbd, .mono {
  font-family: var(--font-mono);
  font-size: 0.92em;
}

:focus-visible {
  outline: 2px solid var(--iris-400);
  outline-offset: 3px;
  border-radius: 4px;
}

::selection { background: var(--iris-600); color: #fff; }

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute;
  left: var(--space-4);
  top: -60px;
  z-index: 100;
  padding: var(--space-2) var(--space-4);
  background: var(--iris-400);
  color: var(--text-on-accent);
  border-radius: var(--radius-sm);
  font-weight: 600;
  text-decoration: none;
}
.skip-link:focus { top: var(--space-3); color: var(--text-on-accent); }

/* ---- Layout ---- */
.wrap {
  max-width: var(--wrap);
  margin-inline: auto;
  padding-inline: clamp(16px, 4vw, 28px);
}

/* ---- Site header ---- */
.site-header {
  padding-block: var(--space-5) var(--space-2);
}
.site-header .wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
}
.wordmark {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: var(--fs-lg);
  color: var(--text);
  text-decoration: none;
}
.wordmark:hover { color: var(--text); }
.wordmark-mark {
  width: 26px;
  height: 26px;
  flex: none;
}
.wordmark span { color: var(--iris-300); }

.crumb {
  font-size: var(--fs-sm);
  color: var(--text-muted);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border);
  transition: color var(--dur) var(--ease-out), border-color var(--dur) var(--ease-out),
    background-color var(--dur) var(--ease-out);
}
.crumb:hover {
  color: var(--text);
  border-color: var(--iris-600);
  background: var(--iris-900);
}
.crumb .arrow { transition: transform var(--dur) var(--ease-out); }
.crumb:hover .arrow { transform: translateX(-3px); }

/* ---- Eyebrow / chips ---- */
.eyebrow {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--iris-300);
}
.eyebrow::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 2px;
  background: var(--iris-400);
  box-shadow: 0 0 0 3px rgba(171, 146, 230, 0.18);
}

.chip {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  line-height: 1.4;
  padding: 3px 9px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--border);
  background: var(--graphite-800);
  color: var(--text-muted);
  white-space: nowrap;
}
.chip--accent {
  border-color: var(--iris-800);
  background: var(--iris-900);
  color: var(--iris-200);
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 10px 20px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  font-weight: 600;
  font-size: var(--fs-md);
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  transition: background-color var(--dur) var(--ease-out), border-color var(--dur) var(--ease-out),
    color var(--dur) var(--ease-out), transform var(--dur-fast) var(--ease-out),
    box-shadow var(--dur) var(--ease-out);
}
.btn:active { transform: scale(0.98); }
.btn:disabled { cursor: not-allowed; opacity: 0.5; transform: none; }

.btn--primary {
  background: var(--iris-400);
  color: var(--text-on-accent);
}
.btn--primary:hover:not(:disabled) {
  background: var(--iris-300);
  color: var(--text-on-accent);
  box-shadow: 0 8px 24px -10px rgba(171, 146, 230, 0.6);
}

.btn--ghost {
  background: transparent;
  border-color: var(--border-strong);
  color: var(--text);
}
.btn--ghost:hover:not(:disabled) {
  border-color: var(--iris-500);
  background: var(--iris-900);
  color: var(--text);
}

/* ---- Card ---- */
.card {
  background: var(--graphite-850);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
}

/* ---- Tables (ห่อด้วย .table-scroll เสมอเพื่อไม่ให้หน้าเลื่อนแนวนอนบนมือถือ) ---- */
.table-scroll {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-block: var(--space-3) var(--space-4);
}
.table-scroll table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
  min-width: 480px;
}
.table-scroll th,
.table-scroll td {
  text-align: left;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border-subtle);
  vertical-align: top;
}
.table-scroll th {
  background: var(--graphite-800);
  color: var(--iris-200);
  font-weight: 600;
  white-space: nowrap;
}
.table-scroll tr:last-child td { border-bottom: 0; }
.table-scroll tbody tr { transition: background-color var(--dur-fast) linear; }
.table-scroll tbody tr:hover { background: rgba(142, 113, 211, 0.06); }

/* ---- Formula block ---- */
.formula {
  display: block;
  font-family: var(--font-mono);
  font-size: 0.95rem;
  color: var(--iris-200);
  background: var(--graphite-950);
  border: 1px solid var(--border);
  border-left: 3px solid var(--iris-500);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  margin-block: var(--space-2) var(--space-3);
  overflow-x: auto;
  white-space: nowrap;
}

/* ---- Footer ---- */
.site-footer {
  margin-top: var(--space-8);
  padding-block: var(--space-6) var(--space-7);
  border-top: 1px solid var(--border-subtle);
  background: linear-gradient(to bottom, transparent, rgba(18, 16, 21, 0.8));
}
.site-footer p {
  margin: 0;
  font-size: var(--fs-sm);
  color: var(--text-faint);
}

@media print {
  body { background: #fff; color: #111; }
  .site-header, .site-footer, .skip-link { display: none; }
}
