/* ==========================================================================
   TOSF — Main Stylesheet (Modern Tech Minimalist)
   ========================================================================== */

/* Design tokens are inlined (no @import) so the browser doesn't wait for a
   second CSS round trip before it can start rendering. Google Fonts are
   loaded via a <link> in each page's <head> for the same reason. */
:root {
  /* Colors — neutral foundation */
  --color-bg: #ffffff;
  --color-bg-alt: #fafafa;
  --color-bg-code: #0b0b0c;
  --color-surface: #ffffff;
  --color-border: #e5e7eb;
  --color-border-strong: #111111;
  --color-text: #111111;
  --color-text-muted: #4b5563;   /* 7.46:1 on #fff — AAA */
  --color-text-subtle: #6b7280;  /* 4.83:1 on #fff — AA */
  --color-text-inverse: #ffffff;

  /* Colors — single brand accent (Thailand saffron orange) */
  --color-accent: #e85d23;
  --color-accent-hover: #c94a15;
  --color-accent-soft: #fef0e9;
  --color-accent-text: #b53f0d;  /* 5.7:1 on #fff, 4.9:1 on #fef0e9 — AA */

  /* Status */
  --color-success: #16a34a;
  --color-warning: #d97706;
  --color-danger: #dc2626;

  /* Typography */
  --font-sans: 'Inter', 'IBM Plex Sans Thai', 'Sarabun', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'SFMono-Regular', ui-monospace, Consolas, monospace;
  --font-thai: 'IBM Plex Sans Thai', 'Sarabun', 'Noto Sans Thai', sans-serif;

  --fs-xs: 0.75rem;
  --fs-sm: 0.875rem;
  --fs-base: 1rem;
  --fs-lg: 1.125rem;
  --fs-xl: 1.25rem;
  --fs-2xl: 1.5rem;
  --fs-3xl: 2rem;
  --fs-4xl: 2.75rem;
  --fs-5xl: 4rem;

  --lh-tight: 1.15;
  --lh-snug: 1.35;
  --lh-normal: 1.6;

  --fw-regular: 400;
  --fw-medium: 500;
  --fw-semibold: 600;
  --fw-bold: 700;

  /* Spacing (4px base) */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 3rem;
  --space-8: 4rem;
  --space-9: 6rem;
  --space-10: 8rem;

  /* Layout */
  --container-max: 1200px;
  --container-narrow: 760px;
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;

  /* Motion */
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --dur-fast: 150ms;
  --dur-base: 250ms;

  /* Elevation — used sparingly in minimalist style */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.08);
}

[data-theme="dark"] {
  --color-bg: #0b0b0c;
  --color-bg-alt: #131316;
  --color-surface: #17171a;
  --color-border: #27272a;
  --color-text: #fafafa;
  --color-text-muted: #a1a1aa;
}

/* --- Reset ------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
html { scroll-behavior: smooth; -webkit-text-size-adjust: 100%; }
body, h1, h2, h3, h4, h5, h6, p, figure, blockquote, dl, dd { margin: 0; }
ul, ol { margin: 0; padding: 0; list-style: none; }
img, svg, video { display: block; max-width: 100%; height: auto; }
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; padding: 0; }
a { color: inherit; text-decoration: none; }
input, textarea, select { font: inherit; }

/* --- Base -------------------------------------------------------------- */
body {
  font-family: var(--font-sans);
  font-size: var(--fs-base);
  line-height: var(--lh-normal);
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Subtle grid background pattern — very faint */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: -1;
  background-image:
    linear-gradient(to right, rgba(0,0,0,0.02) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(0,0,0,0.02) 1px, transparent 1px);
  background-size: 40px 40px;
}

[lang="th"] body, body[lang="th"] { font-family: var(--font-thai); }

main { flex: 1; }

/* --- Typography -------------------------------------------------------- */
h1, h2, h3, h4 {
  font-weight: var(--fw-bold);
  line-height: var(--lh-tight);
  letter-spacing: -0.02em;
}
h1 { font-size: clamp(2rem, 5vw, var(--fs-5xl)); }
h2 { font-size: clamp(1.5rem, 3.5vw, var(--fs-4xl)); }
h3 { font-size: var(--fs-2xl); }
h4 { font-size: var(--fs-xl); }

p { line-height: var(--lh-normal); color: var(--color-text); }
p + p { margin-top: var(--space-4); }

.text-muted { color: var(--color-text-muted); }
.text-mono { font-family: var(--font-mono); }
.text-sm { font-size: var(--fs-sm); }

.eyebrow {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  color: var(--color-accent-text);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: var(--fw-medium);
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}
.eyebrow::before {
  content: '';
  width: 24px;
  height: 1px;
  background: var(--color-accent-text);
}

a.link {
  color: var(--color-text);
  border-bottom: 1px solid var(--color-border-strong);
  transition: color var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease);
}
a.link:hover { color: var(--color-accent); border-color: var(--color-accent); }

/* --- Layout ------------------------------------------------------------ */
.container {
  width: 100%;
  max-width: var(--container-max);
  margin: 0 auto;
  padding: 0 var(--space-5);
}
.container-narrow { max-width: var(--container-narrow); margin: 0 auto; padding: 0 var(--space-5); }
.section { padding: var(--space-9) 0; }
.section-sm { padding: var(--space-7) 0; }

.grid { display: grid; gap: var(--space-5); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }

@media (max-width: 900px) {
  .grid-3, .grid-4 { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 600px) {
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}

/* --- Header / Nav ------------------------------------------------------ */
.site-header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--color-border);
}
.nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 68px;
}
.brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  font-family: var(--font-mono);
  font-weight: var(--fw-bold);
  font-size: var(--fs-base);
  letter-spacing: -0.02em;
}
.brand-mark {
  height: 32px;
  width: auto;
  display: block;
  object-fit: contain;
}
.nav-links {
  display: flex;
  align-items: center;
  gap: var(--space-6);
}
.nav-links a {
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  color: var(--color-text-muted);
  transition: color var(--dur-fast) var(--ease);
}
.nav-links a:hover, .nav-links a.active { color: var(--color-text); }
.nav-right { display: flex; align-items: center; gap: var(--space-4); }

.lang-toggle {
  display: inline-flex;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
}
.lang-toggle button {
  padding: var(--space-2) var(--space-3);
  color: var(--color-text-muted);
  transition: all var(--dur-fast) var(--ease);
}
.lang-toggle button.active {
  background: var(--color-border-strong);
  color: var(--color-text-inverse);
}

.mobile-toggle { display: none; width: 40px; height: 40px; align-items: center; justify-content: center; }
.mobile-toggle span {
  display: block;
  width: 20px;
  height: 2px;
  background: var(--color-text);
  position: relative;
}
.mobile-toggle span::before, .mobile-toggle span::after {
  content: ''; position: absolute; left: 0; width: 20px; height: 2px; background: var(--color-text);
}
.mobile-toggle span::before { top: -6px; }
.mobile-toggle span::after { top: 6px; }

@media (max-width: 820px) {
  .nav-links {
    position: fixed;
    top: 68px; left: 0; right: 0;
    background: var(--color-bg);
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    padding: var(--space-4);
    border-bottom: 1px solid var(--color-border);
    transform: translateY(-120%);
    transition: transform var(--dur-base) var(--ease);
  }
  .nav-links.open { transform: translateY(0); }
  .nav-links a { padding: var(--space-3) 0; border-bottom: 1px solid var(--color-border); font-size: var(--fs-base); }
  .mobile-toggle { display: inline-flex; }
}

/* --- Buttons ----------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-5);
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  border-radius: var(--radius-sm);
  transition: all var(--dur-fast) var(--ease);
  cursor: pointer;
  white-space: nowrap;
  border: 1px solid transparent;
}
.btn-primary {
  background: var(--color-border-strong);
  color: var(--color-text-inverse);
}
.btn-primary:hover { background: var(--color-accent); transform: translateY(-1px); }
.btn-outline {
  border-color: var(--color-border-strong);
  color: var(--color-text);
  background: transparent;
}
.btn-outline:hover { background: var(--color-border-strong); color: var(--color-text-inverse); }
.btn-ghost { color: var(--color-text); }
.btn-ghost:hover { color: var(--color-accent); }
.btn-sm { padding: var(--space-2) var(--space-3); font-size: var(--fs-xs); }

/* --- Hero -------------------------------------------------------------- */
.hero {
  padding: var(--space-10) 0 var(--space-9);
  position: relative;
  overflow: hidden;
}
.hero h1 {
  max-width: 14ch;
  margin-top: var(--space-4);
}
.hero h1 .accent { color: var(--color-accent-text); }
.hero p.lead {
  margin-top: var(--space-5);
  font-size: var(--fs-xl);
  color: var(--color-text-muted);
  max-width: 52ch;
  line-height: var(--lh-snug);
}
.hero-actions { margin-top: var(--space-6); display: flex; gap: var(--space-3); flex-wrap: wrap; }
.hero-meta {
  margin-top: var(--space-7);
  padding-top: var(--space-5);
  border-top: 1px solid var(--color-border);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-5);
}
.hero-meta > div .num {
  font-family: var(--font-mono);
  font-size: var(--fs-3xl);
  font-weight: var(--fw-bold);
  display: block;
}
.hero-meta > div .label {
  font-size: var(--fs-sm);
  color: var(--color-text-muted);
}
@media (max-width: 600px) { .hero-meta { grid-template-columns: 1fr; } }

/* --- Cards ------------------------------------------------------------- */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-5);
  transition: all var(--dur-base) var(--ease);
  display: flex;
  flex-direction: column;
  height: 100%;
}
.card:hover {
  border-color: var(--color-border-strong);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}
.card h3 { font-size: var(--fs-xl); margin-bottom: var(--space-2); }
.card p { color: var(--color-text-muted); font-size: var(--fs-sm); flex: 1; }

.team-group-title {
  font-family: var(--font-mono);
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
  padding-bottom: var(--space-3);
  margin-bottom: var(--space-5);
  border-bottom: 1px solid var(--color-border);
}
.card-meta {
  margin-top: var(--space-4);
  padding-top: var(--space-4);
  border-top: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  color: var(--color-text-muted);
}

.card-image {
  margin: calc(-1 * var(--space-5)) calc(-1 * var(--space-5)) var(--space-4);
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border-radius: var(--radius-md) var(--radius-md) 0 0;
  background: var(--color-bg-alt);
}
.card-image img { width: 100%; height: 100%; object-fit: cover; }

.tag {
  display: inline-block;
  padding: 2px var(--space-2);
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  background: var(--color-accent-soft);
  color: var(--color-accent-text);
  border-radius: var(--radius-sm);
}

/* --- Sections ---------------------------------------------------------- */
.section-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  margin-bottom: var(--space-6);
  gap: var(--space-5);
  flex-wrap: wrap;
}
.section-header h2 { max-width: 20ch; }
.section-header p { color: var(--color-text-muted); max-width: 48ch; }

/* --- Footer ------------------------------------------------------------ */
.site-footer {
  background: var(--color-bg-code);
  color: var(--color-text-inverse);
  padding: var(--space-8) 0 var(--space-5);
  margin-top: var(--space-9);
}
.site-footer a { color: #d4d4d8; }
.site-footer a:hover { color: var(--color-accent); }
.footer-grid {
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 1fr;
  gap: var(--space-6);
}
.footer-grid .footer-heading {
  font-size: var(--fs-sm);
  font-family: var(--font-mono);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #a1a1aa;
  margin-bottom: var(--space-3);
  font-weight: var(--fw-medium);
}
.footer-grid ul li { margin-bottom: var(--space-2); font-size: var(--fs-sm); }
.footer-bottom {
  margin-top: var(--space-7);
  padding-top: var(--space-4);
  border-top: 1px solid #27272a;
  display: flex;
  justify-content: space-between;
  font-size: var(--fs-xs);
  color: #a1a1aa;
  font-family: var(--font-mono);
}
@media (max-width: 820px) {
  .footer-grid { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 500px) {
  .footer-grid { grid-template-columns: 1fr; }
  .footer-bottom { flex-direction: column; gap: var(--space-2); }
}

/* --- Forms ------------------------------------------------------------- */
.form-field { margin-bottom: var(--space-4); }
.form-field label {
  display: block;
  font-size: var(--fs-sm);
  font-weight: var(--fw-medium);
  margin-bottom: var(--space-2);
}
.form-field input,
.form-field textarea,
.form-field select {
  width: 100%;
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-bg);
  color: var(--color-text);
  transition: border-color var(--dur-fast) var(--ease);
}
.form-field input:focus,
.form-field textarea:focus,
.form-field select:focus {
  outline: none;
  border-color: var(--color-accent);
}
.form-field textarea { min-height: 140px; resize: vertical; font-family: inherit; }

/* --- Utility ----------------------------------------------------------- */
.divider { height: 1px; background: var(--color-border); margin: var(--space-6) 0; }
.skeleton {
  position: relative;
  background: var(--color-bg-alt);
  border-radius: var(--radius-sm);
  overflow: hidden;
  isolation: isolate;
}
.skeleton::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, #f0f0f0, transparent);
  transform: translate3d(-100%, 0, 0);
  will-change: transform;
  animation: shimmer 1.4s infinite;
}
@keyframes shimmer {
  0%   { transform: translate3d(-100%, 0, 0); }
  100% { transform: translate3d(100%, 0, 0); }
}

/* Shimmer for image containers — applied via JS, removed when img loads */
.img-skeleton {
  position: relative;
  overflow: hidden;
  isolation: isolate;
}
.img-skeleton::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
  transform: translate3d(-100%, 0, 0);
  will-change: transform;
  animation: shimmer 1.4s infinite;
  pointer-events: none;
}

/* Blog post cover image */
.post-cover {
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border-radius: var(--radius-md);
  margin-top: 2rem;
  background: var(--color-bg-alt);
}
.post-cover img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* Body images — grey placeholder until loaded, prevents layout shift */
.post-body img { display: block; min-height: 120px; background: var(--color-bg-alt); border-radius: var(--radius-sm); transition: background 0.3s; }
.post-body img.img-loaded { min-height: unset; background: none; }

/* i18n visibility */
[data-i18n-lang] { display: none; }
html[lang="en"] [data-i18n-lang="en"],
html[lang="th"] [data-i18n-lang="th"] { display: initial; }

.hidden { display: none !important; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

/* Keyboard focus — visible focus ring for a11y (WCAG 2.4.7) */
:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
  border-radius: 2px;
}
a:focus-visible,
button:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible { outline-offset: 3px; }

/* Skip-to-main link — hidden until focused */
.skip-link {
  position: absolute;
  top: -40px;
  left: var(--space-4);
  background: var(--color-text);
  color: var(--color-text-inverse);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-sm);
  font-size: var(--fs-sm);
  font-weight: var(--fw-semibold);
  z-index: 100;
  transition: top var(--dur-fast) var(--ease);
}
.skip-link:focus { top: var(--space-3); }

/* --- Page loader ------------------------------------------------------- */
/* Full-viewport overlay shown while main.js boots (partials + i18n).
   Dismissed by adding .is-hidden once the app is ready to paint content. */
#page-loader {
  position: fixed;
  inset: 0;
  background: var(--color-bg);
  display: grid;
  place-items: center;
  gap: var(--space-3);
  z-index: 9999;
  transition: opacity 0.25s var(--ease), visibility 0.25s var(--ease);
}
#page-loader.is-hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}
.page-loader__mark {
  display: grid;
  place-items: center;
  gap: var(--space-3);
}
.page-loader__logo {
  animation: page-loader-pulse 1.4s ease-in-out infinite;
}
.page-loader__ring {
  width: 28px;
  height: 28px;
  border: 2px solid var(--color-border);
  border-top-color: var(--color-accent);
  border-radius: 50%;
  animation: page-loader-spin 0.9s linear infinite;
}
.page-loader__sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }
@keyframes page-loader-pulse {
  0%, 100% { opacity: 0.45; transform: scale(0.97); }
  50%      { opacity: 1;    transform: scale(1); }
}
@keyframes page-loader-spin {
  to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
  .page-loader__logo, .page-loader__ring { animation: none; opacity: 1; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
