/* Phong-thuỷ ngôn ngữ lập trình — design tokens & layout.
   Palette: warm cream + accent red + gold (lunar-new-year).
   Element accents (kim/moc/thuy/hoa/tho) kept as brand. */

:root {
  --bg: #fdf6ec;
  --bg-tint: #f7ecd8;
  --fg: #2b2118;
  --muted: #5b4a38;             /* darkened for AA on cream */
  --accent: #b8312f;
  --accent-ink: #8d1f1d;
  --gold: #c79b3c;
  --card-bg: #ffffff;
  --border: rgba(43, 33, 24, 0.12);
  --border-strong: rgba(43, 33, 24, 0.22);

  --kim: #c8932a;
  --thuy: #2e6fb5;
  --moc: #2f7d3f;
  --hoa: #c0392b;
  --tho: #7a5f30;

  --shadow: 0 6px 18px rgba(58, 36, 12, 0.10);
  --shadow-sm: 0 2px 6px rgba(58, 36, 12, 0.08);
  --radius-sm: 8px;
  --radius: 12px;
  --focus-ring: 0 0 0 3px rgba(199, 155, 60, 0.45);

  --font-sans: "Be Vietnam Pro", "Inter", "Segoe UI", system-ui, -apple-system,
    "Helvetica Neue", Arial, "Noto Sans", "Noto Sans Vietnamese", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { scroll-behavior: smooth; }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

body {
  font-family: var(--font-sans);
  background: radial-gradient(1200px 600px at 50% -120px, #fff5dd 0%, transparent 60%), var(--bg);
  color: var(--fg);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--accent);
  color: #fff;
  padding: 0.5rem 0.9rem;
  border-radius: 0 0 8px 0;
  font-weight: 600;
  z-index: 100;
}
.skip-link:focus { left: 0; }

.page {
  max-width: 880px;
  margin: 0 auto;
  padding: 2.25rem 1rem 3rem;
}

/* ---------- Hero ---------- */
.hero { text-align: center; margin-bottom: 1.75rem; padding: 0 0.25rem; }
.hero h1 {
  margin: 0;
  font-size: clamp(1.55rem, 4.4vw, 2.5rem);
  line-height: 1.2; color: var(--accent); letter-spacing: 0.2px; font-weight: 700;
}
.hero .subtitle { margin: 0.5rem auto 0; max-width: 38ch; color: var(--muted); letter-spacing: 0.15px; }
.hero-ribbon {
  display: flex; justify-content: center; gap: 8px;
  margin: 1rem auto 0; width: max-content; max-width: 100%;
}
.hero-ribbon span { display: block; width: 64px; height: 6px; border-radius: 3px; }
.hero-ribbon .r-kim  { background: var(--kim); }
.hero-ribbon .r-moc  { background: var(--moc); }
.hero-ribbon .r-thuy { background: var(--thuy); }
.hero-ribbon .r-hoa  { background: var(--hoa); }
.hero-ribbon .r-tho  { background: var(--tho); }

/* ---------- Original-image disclosure ---------- */
.original-image {
  margin: 2rem 0 0; background: var(--card-bg);
  border: 1px solid var(--border); padding: 0.85rem 1rem;
  border-radius: var(--radius); box-shadow: var(--shadow-sm);
}
.original-image > summary {
  cursor: pointer; list-style: none; font-weight: 500;
  display: inline-flex; align-items: center; gap: 0.4rem;
  color: var(--accent-ink);
}
.original-image > summary::-webkit-details-marker { display: none; }
.original-image > summary::before {
  content: "▸"; display: inline-block;
  transition: transform 0.15s ease; color: var(--gold);
}
.original-image[open] > summary::before { transform: rotate(90deg); }
.original-image > summary:focus-visible {
  outline: none; box-shadow: var(--focus-ring); border-radius: 4px;
}
.original-image figure {
  margin: 0.75rem 0 0; padding: 0.85rem;
  background: var(--bg-tint); border-radius: var(--radius-sm);
}
.original-image img {
  display: block; max-width: 100%; height: auto; border-radius: var(--radius-sm);
}
.original-image figcaption {
  text-align: center; margin-top: 0.6rem; color: var(--muted); font-size: 0.9rem;
}
.original-image figcaption a {
  color: var(--accent-ink);
  text-decoration: underline;
  text-decoration-style: dotted;
  text-decoration-color: var(--accent);
  text-underline-offset: 2px;
}
.original-image figcaption a:hover { color: var(--gold); text-decoration-color: var(--gold); }

/* ---------- Section headings ---------- */
.elements h2 {
  text-align: center; color: var(--accent);
  margin: 1.75rem 0 1rem; font-weight: 700; letter-spacing: 0.3px;
}
.mode-tag {
  text-align: center; font-size: 0.85rem; font-style: italic;
  color: var(--muted); font-weight: 400; margin: -0.5rem 0 1rem;
}
.source-note {
  display: inline-block;
  margin-left: 0.4rem;
  padding: 0.05rem 0.5rem;
  border-radius: 999px;
  background: var(--bg-tint);
  color: var(--accent-ink);
  font-size: 0.7rem;
  font-style: normal;
  font-weight: 600;
  letter-spacing: 0.2px;
}

/* ---------- Card grid ---------- */
/* Grid stays auto-fit (not forced 5-col) — the Ngũ-Hành wheel reads at any
   viewport above 720px without crushing chip names. See plan 260428-1003. */
.grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  align-items: start;
}

.card {
  position: relative; background: var(--card-bg);
  border: 1px solid var(--border); border-top: 4px solid var(--gold);
  border-radius: var(--radius); padding: 0.95rem 1rem 1.05rem;
  box-shadow: var(--shadow-sm);
  transition: transform 0.18s ease, box-shadow 0.18s ease;
}
.card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }

@media (prefers-reduced-motion: reduce) {
  .card { transition: none; }
  .card:hover { transform: none; }
}

.card h3 {
  margin: 0 0 0.5rem;
  font-size: 1.2rem;
  letter-spacing: 0.4px;
  font-weight: 700;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid color-mix(in oklch, var(--gold) 60%, transparent);
}
.card p { margin: 0; color: var(--fg); }

.card.kim  { border-top-color: var(--kim);  background: color-mix(in oklch, var(--kim)  6%, var(--card-bg)); }
.card.kim  h3, .card.kim  .card-count { color: var(--kim);  }
.card.thuy { border-top-color: var(--thuy); background: color-mix(in oklch, var(--thuy) 6%, var(--card-bg)); }
.card.thuy h3, .card.thuy .card-count { color: var(--thuy); }
.card.moc  { border-top-color: var(--moc);  background: color-mix(in oklch, var(--moc)  6%, var(--card-bg)); }
.card.moc  h3, .card.moc  .card-count { color: var(--moc);  }
.card.hoa  { border-top-color: var(--hoa);  background: color-mix(in oklch, var(--hoa)  6%, var(--card-bg)); }
.card.hoa  h3, .card.hoa  .card-count { color: var(--hoa);  }
.card.tho  { border-top-color: var(--tho);  background: color-mix(in oklch, var(--tho)  6%, var(--card-bg)); }
.card.tho  h3, .card.tho  .card-count { color: var(--tho);  }

.disclaimer { text-align: center; color: var(--muted); font-size: 0.85rem; margin-top: 1rem; }

/* ---------- Debug panel disclosure ---------- */
#debug-panel > summary {
  cursor: pointer; list-style: none; font-weight: 500;
  display: inline-flex; align-items: center; gap: 0.4rem;
  color: var(--muted);
}
#debug-panel > summary::-webkit-details-marker { display: none; }
#debug-panel > summary::before {
  content: "▸"; display: inline-block;
  transition: transform 0.15s ease; color: var(--gold);
}
#debug-panel[open] > summary::before { transform: rotate(90deg); }
#debug-panel > summary:focus-visible {
  outline: none; box-shadow: var(--focus-ring); border-radius: 4px;
}

/* ---------- Chip grid (modern panel) ---------- */
.card-count {
  display: block; margin: 0 0 0.6rem;
  color: var(--muted); font-size: 0.85rem; font-weight: 600;
  font-variant-numeric: tabular-nums; letter-spacing: 0.1px;
}

.chips { display: flex; flex-wrap: wrap; gap: 0.4rem 0.45rem; }
.elements.show-all .chips {
  gap: 0.45rem 0.5rem; padding-top: 0.25rem;
  max-height: 60vh; overflow-y: auto;
  scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent;
}
.elements.show-all .chips::-webkit-scrollbar { width: 6px; }
.elements.show-all .chips::-webkit-scrollbar-thumb {
  background: var(--border-strong); border-radius: 3px;
}

/* Chip text contrast policy: best-effort via WCAG luminance picker
   (render-elements.js pickTextColor). Worst post-fix case ~3.5:1
   (Swift, MATLAB) is acceptable for decorative chips. */
.chip {
  display: inline-flex; align-items: center;
  padding: 0.22rem 0.6rem; border-radius: 999px;
  font-size: 0.78rem; line-height: 1.4;
  border: 1px solid rgba(0, 0, 0, 0.22);
  background: var(--bg); color: var(--fg);
  max-width: 100%;
  white-space: normal;
  overflow-wrap: anywhere;
}
/* Subtle outline boost for chips with a colored background — helps borderline
   contrast cases (e.g. Swift #F05138) without changing chip color. */
.chip[style*="background"] {
  text-shadow: 0 0 1px rgba(0, 0, 0, 0.45), 0 0 1px rgba(0, 0, 0, 0.45);
}
.chip-tiobe {
  border-color: rgba(0, 0, 0, 0.45); font-weight: 600;
  box-shadow: inset 0 0 0 1.5px rgba(255, 255, 255, 0.5), 0 0 0 1px rgba(0, 0, 0, 0.18);
}

.elements .chip-other { display: none; }
.elements.show-all .chip-other { display: inline-flex; }

.modern-controls {
  display: flex; flex-wrap: wrap; justify-content: center; align-items: stretch;
  gap: 1rem 1.25rem; margin: 0 0 1rem;
}
.control-group {
  display: inline-flex; flex-direction: column; align-items: center; gap: 0.3rem;
}
.control-label {
  font-size: 0.78rem; color: var(--muted); letter-spacing: 0.3px; text-transform: uppercase;
}
.modern-controls #source-toggle,
.modern-controls #view-toggle,
.modern-controls #sort-toggle { display: flex; margin: 0; }

/* ---------- Segmented control (source / view / sort) ---------- */
.segmented {
  display: inline-flex; border: 1px solid var(--border-strong);
  border-radius: 999px; padding: 2px; background: var(--card-bg);
}
.segmented-btn {
  font: inherit; font-size: 0.85rem; padding: 0.4rem 0.9rem;
  min-height: 36px;
  border-radius: 999px; border: none; background: transparent;
  color: var(--fg); cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}
.segmented-btn[aria-checked="true"] { background: var(--accent); color: #fff; }
.segmented-btn:hover:not([aria-checked="true"]) { background: var(--bg-tint); }
.segmented-btn:focus-visible { outline: none; box-shadow: var(--focus-ring); }

.legend {
  text-align: center; font-style: italic;
  color: var(--muted); font-size: 0.85rem;
  margin: 1rem auto 0; max-width: 60ch;
}

.render-error {
  text-align: center; color: var(--accent-ink);
  padding: 0.9rem 1rem; border: 1px dashed var(--accent);
  border-radius: var(--radius-sm); background: #fff7f6;
}

/* ---------- Debug panel ---------- */
#debug-panel {
  margin-top: 1rem;
  background: var(--bg-tint);
  border: 1px dashed var(--border-strong);
  padding: 0.75rem 1rem;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
}
#debug-panel[open] { background: var(--card-bg); }
#debug-panel h4 { margin: 0.85rem 0 0.4rem; font-size: 0.9rem; color: var(--fg); }

/* ---------- Mobile ---------- */
@media (max-width: 720px) {
  .grid { grid-template-columns: 1fr; }
}

@media (max-width: 480px) {
  .modern-controls { flex-direction: column; align-items: stretch; gap: 0.85rem; }
  .modern-controls .control-group { flex-direction: row; justify-content: space-between; gap: 0.6rem; }
  .modern-controls .control-label { align-self: center; flex-shrink: 0; }
  .segmented-btn { min-height: 44px; padding: 0.5rem 0.9rem; }
  .hero-ribbon span { width: clamp(40px, 14vw, 64px); }
}

@media (max-width: 420px) {
  .page { padding: 1.75rem 0.85rem 2.5rem; }
  .chip { font-size: 0.74rem; padding: 0.22rem 0.55rem; min-height: 28px; }
}
