/* ---------------------------------------------------------------------
   ใบงานคัดลายมือ — Thai Worksheet Library
   Design direction: "school supply drawer" — kraft/manila paper ground,
   chalkboard-navy ink, red-pen accent for the primary action (nods to a
   teacher's grading pen, not a generic brand red). Distinct from the
   sibling flashcards app's cream/jade palette while sharing the portal's
   type voice (Fraunces headings, Work Sans UI, Sarabun Thai glyphs).
   ------------------------------------------------------------------- */

:root {
  --kraft: #ece1c8;
  --kraft-raised: #faf4e6;
  --ink: #1e2a3a;
  --ink-soft: #4d5b6d;
  --ink-faint: #556173;
  --rule: #d9caa4;
  --rule-strong: #c6b384;

  --navy: #23405c;
  --navy-ink: #14263a;
  --navy-soft: #dde6ee;

  --redpen: #a4291f;
  --redpen-ink: #7c1e16;
  --redpen-soft: #f6ddd8;

  --gold: #7a5714;
  --gold-soft: #f1e3c2;

  --focus-ring: #14263a;
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;

  --shadow-card: 0 1px 2px rgba(30, 42, 58, 0.08), 0 12px 28px -18px rgba(30, 42, 58, 0.4);
  --shadow-pop: 0 24px 60px -20px rgba(20, 38, 58, 0.5);

  color-scheme: light;
}

/* ---------- Dark theme ----------
   Toggled via [data-theme="dark"] on <html> (set by the anti-FOUC inline
   script in index.html). Same token names as the light palette above are
   redefined here — every rule in this file already reads var(--kraft)/
   var(--ink)/etc., so the whole page repaints without touching component
   styles. Navy/redpen/gold keep the tool's own "school supply drawer"
   identity but get dark-safe counterparts (Spec: "Kategorie-/Akzentfarben...
   für Dark Mode ebenfalls beide Varianten pflegen"). */
html[data-theme="dark"] {
  --kraft: #211d16;
  --kraft-raised: #2b271d;
  --ink: #e9e3d3;
  --ink-soft: #b3ac97;
  --ink-faint: #8f8870;
  --rule: #3d3524;
  --rule-strong: #574d34;

  --navy: #6f96c0;
  --navy-ink: #c3d8ee;
  --navy-soft: #263646;

  --redpen: #e08072;
  --redpen-ink: #f4c3ba;
  --redpen-soft: #402420;

  --gold: #d9b360;
  --gold-soft: #3a2f18;

  --focus-ring: #e9e3d3;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

/* An author-stylesheet `display` declaration always wins over the browser's
   built-in `[hidden] { display: none }` rule, regardless of selector
   specificity — origin beats specificity in the cascade. Several elements
   here (.modal, .btn used as a toggled link, etc.) declare their own
   `display`, which would otherwise silently defeat `el.hidden = true` in
   script.js. This single global override guarantees `hidden` always means
   hidden, everywhere in this app. */
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--kraft);
  background-image:
    linear-gradient(rgba(30, 42, 58, 0.025) 1px, transparent 1px);
  background-size: 100% 2.1rem;
  color: var(--ink);
  font-family: "Work Sans", -apple-system, BlinkMacSystemFont, sans-serif;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  /* Defense-in-depth for 320-500px viewports (verified via CDP: no
     overflow found today, but nothing on this page needs horizontal
     scroll, so guard against future regressions at the document level). */
  overflow-x: hidden;
}

[lang="th"] {
  font-family: var(--font-thai), "Work Sans", sans-serif;
}

h1, h2 {
  font-family: "Fraunces", Georgia, serif;
  font-weight: 600;
  margin: 0;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--ink);
  color: var(--kraft-raised);
  padding: 0.75rem 1.25rem;
  border-radius: 0 0 var(--radius-sm) 0;
  z-index: 200;
}

.skip-link:focus {
  left: 0;
}

.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;
}

:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

button {
  font-family: inherit;
}

a {
  color: inherit;
}

/* -------------------- page + masthead --------------------
   Standardized 2-line header, same class names as the portal/web-countdown
   (see assets/css/portal.css) — kept in sync so this block can be
   copy-pasted into the next tool during rollout. Colors reuse this tool's
   own navy/ink tokens so it keeps its "school supply drawer" identity
   instead of inheriting the portal's neutral rust accent. */

.page {
  max-width: 1080px;
  margin: 0 auto;
}

.masthead {
  max-width: 920px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 1.15rem;
}

.masthead-rule {
  border: none;
  border-top: 2px solid var(--ink);
  margin: 0 1.25rem 0;
}

@media (min-width: 970px) {
  .masthead-rule {
    max-width: 920px;
    margin: 0 auto;
  }
}

.masthead-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.masthead-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.home-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.4rem;
  height: 2.4rem;
  flex-shrink: 0;
  border-radius: 50%;
  color: var(--ink-soft);
  text-decoration: none;
  transition: background-color 0.12s ease, color 0.12s ease;
}

.home-icon:hover {
  background: var(--navy-soft);
  color: var(--navy-ink);
}

.home-icon:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .home-icon {
    transition: none;
  }
}

.brand-tile {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: var(--radius-sm);
  background: var(--navy);
  color: var(--kraft-raised);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.brand-tile img {
  display: block;
  width: 1.25rem;
  height: 1.25rem;
  /* External <img> SVGs do not inherit currentColor. Brand-tile is ink-on-paper
     inverted (dark tile / paper glyph). Force monochrome paint to match. */
  filter: brightness(0) invert(1);
}
html[data-theme="dark"] .brand-tile img {
  /* Dark theme: --ink tile is light; keep icon dark. */
  filter: brightness(0);
}


.brand-tile-icon {
  width: 1.25rem;
  height: 1.25rem;
}

.brand-title {
  font-size: 1.3rem;
  color: var(--ink);
  /* Complex-script rendering (Kai.md): generous line-height + visible
     overflow so Thai tone marks/vowel signs above the consonant are never
     clipped, and no truncation since this title is always short. */
  line-height: 1.8;
  overflow: visible;
  white-space: normal;
  min-width: 0;
}

.brand-title-sub {
  font-family: "Work Sans", sans-serif;
  font-weight: 500;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

.masthead-actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-shrink: 0;
}

.theme-toggle {
  position: relative;
  width: 2.3rem;
  height: 2.3rem;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--rule-strong);
  border-radius: 50%;
  background: var(--kraft-raised);
  color: var(--ink-soft);
  cursor: pointer;
  transition: border-color 0.12s ease, color 0.12s ease, transform 0.1s ease;
}

.theme-toggle:hover {
  border-color: var(--navy);
  color: var(--navy-ink);
}

.theme-toggle:active {
  transform: scale(0.94);
}

.theme-toggle:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.theme-icon {
  position: absolute;
  width: 1.1rem;
  height: 1.1rem;
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.theme-icon--sun {
  opacity: 1;
  transform: scale(1) rotate(0deg);
}

.theme-icon--moon {
  opacity: 0;
  transform: scale(0.5) rotate(-40deg);
}

html[data-theme="dark"] .theme-icon--sun {
  opacity: 0;
  transform: scale(0.5) rotate(40deg);
}

html[data-theme="dark"] .theme-icon--moon {
  opacity: 1;
  transform: scale(1) rotate(0deg);
}

@media (prefers-reduced-motion: reduce) {
  .theme-toggle,
  .theme-icon {
    transition: none;
  }
}

.masthead-sub {
  margin: 0.4rem 0 0;
  font-size: 0.78rem;
  color: var(--ink-soft);
}

/* -------------------- workspace -------------------- */

.workspace {
  max-width: 920px;
  margin: 0 auto;
  padding: 1.75rem 1.25rem 3rem;
}

.intro {
  background: var(--navy-soft);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  padding: 1rem 1.25rem;
  margin-bottom: 1.5rem;
}

.intro p {
  margin: 0;
  font-size: 0.92rem;
  color: var(--navy-ink);
  line-height: 1.55;
}

/* -------------------- filters -------------------- */

.filter-bar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.75rem;
}

.filter-heading {
  font-family: "Work Sans", sans-serif;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-faint);
  margin: 0 0 0.5rem;
}

.filter-heading-note {
  font-weight: 500;
  letter-spacing: 0;
  text-transform: none;
  opacity: 0.9;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.chip {
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  border: 1px solid var(--rule-strong);
  background: var(--kraft-raised);
  color: var(--ink-soft);
  font-weight: 600;
  font-size: 0.82rem;
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.chip:hover {
  border-color: var(--navy);
  color: var(--navy-ink);
}

.chip:active {
  transform: scale(0.97);
}

.chip.is-active {
  background: var(--navy);
  border-color: var(--navy);
  color: var(--kraft-raised);
}

.filter-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding-top: 0.25rem;
  border-top: 1px dashed var(--rule);
}

#result-count {
  margin: 0;
  font-size: 0.8rem;
  color: var(--ink-soft);
  font-weight: 600;
}

.reset-btn {
  background: none;
  border: none;
  color: var(--redpen);
  font-weight: 700;
  font-size: 0.8rem;
  cursor: pointer;
  padding: 0.2rem 0.3rem;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.reset-btn:hover {
  color: var(--redpen-ink);
}

/* -------------------- worksheet grid -------------------- */

.grid-section {
  min-height: 12rem;
}

.worksheet-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr));
  gap: 1.1rem;
}

.empty-state {
  text-align: center;
  color: var(--ink-soft);
  font-size: 0.9rem;
  padding: 2.5rem 1rem;
}

.card {
  display: flex;
  flex-direction: column;
  text-align: left;
  background: var(--kraft-raised);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  overflow: hidden;
  cursor: pointer;
  padding: 0;
  font-family: inherit;
  color: inherit;
  animation: card-in 0.25s ease both;
  /* Stagger Reveal (uimotion.fyi #22): --i set per-card in script.js so a
     filter change reads as an intentional, ordered reflow of the grid
     rather than an instant hard cut/reflow. Capped in JS to keep the
     tail of a large result set snappy. */
  animation-delay: calc(var(--i, 0) * 35ms);
  transition: transform 0.15s ease, box-shadow 0.15s ease, border-color 0.15s ease;
}

@keyframes card-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

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

.card:hover {
  border-color: var(--navy);
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(30, 42, 58, 0.1), 0 20px 36px -20px rgba(30, 42, 58, 0.45);
}

.card:active {
  transform: translateY(0);
}

.card-thumb {
  width: 100%;
  aspect-ratio: 4 / 3;
  background: var(--kraft);
  border-bottom: 1px solid var(--rule);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}

.card-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: top center;
}

.card-thumb-fallback {
  color: var(--ink-faint);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.card-thumb-fallback svg {
  width: 2.1rem;
  height: 2.1rem;
}

.card-hosting-badge {
  position: absolute;
  top: 0.55rem;
  right: 0.55rem;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  background: var(--kraft-raised);
  border: 1px solid var(--rule-strong);
  color: var(--ink-soft);
}

.card-hosting-badge.is-linkout {
  background: var(--gold-soft);
  border-color: var(--gold);
  color: var(--gold);
}

.card-body {
  padding: 0.85rem 1rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  flex: 1;
}

.card-title {
  margin: 0;
  font-family: var(--font-thai), sans-serif;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--ink);
  line-height: 1.3;
}

.card-title-en {
  margin: 0;
  font-size: 0.76rem;
  color: var(--ink-soft);
  font-weight: 500;
}

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.15rem;
}

.tag {
  font-size: 0.68rem;
  font-weight: 700;
  padding: 0.2rem 0.5rem;
  border-radius: 999px;
  white-space: nowrap;
}

.tag--grade {
  background: var(--navy-soft);
  color: var(--navy-ink);
}

.tag--pedagogical {
  background: transparent;
  color: var(--ink-soft);
  border: 1px solid var(--rule);
  font-weight: 600;
}

.tag--skill {
  background: var(--gold-soft);
  color: var(--gold);
}

.card-source {
  margin: 0.25rem 0 0;
  font-size: 0.72rem;
  color: var(--ink-faint);
  font-weight: 600;
}

/* -------------------- footer -------------------- */

.site-footer {
  text-align: center;
  padding: 1.5rem 1rem 2.5rem;
  color: var(--ink-faint);
  font-size: 0.78rem;
  max-width: 42rem;
  margin: 0 auto;
}

.app-version {
  margin-top: 0.35rem;
  opacity: 0.7;
}

.app-version:empty {
  display: none;
}

/* -------------------- modal -------------------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(20, 26, 34, 0.5);
  animation: fade-in 0.2s ease both;
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-panel {
  position: relative;
  width: 100%;
  max-width: 38rem;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--kraft-raised);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-pop);
  animation: pop-in 0.2s ease both;
}

@keyframes pop-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@media (prefers-reduced-motion: reduce) {
  .modal-overlay,
  .modal-panel {
    animation: none;
  }
}

.modal-head {
  display: flex;
  justify-content: flex-end;
  padding: 0.75rem 0.75rem 0;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--ink-soft);
  cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.icon-btn:hover {
  color: var(--navy-ink);
  background: var(--navy-soft);
}

.icon {
  width: 1.2rem;
  height: 1.2rem;
  flex-shrink: 0;
}

.modal-body {
  padding: 0 1.5rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

@media (min-width: 560px) {
  .modal-body {
    flex-direction: row;
    align-items: flex-start;
  }
}

.preview-thumb-wrap {
  flex: 0 0 auto;
  width: 100%;
  max-width: 14rem;
  aspect-ratio: 4 / 3;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 1px solid var(--rule);
  background: var(--kraft);
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: top center;
}

.preview-thumb-fallback {
  color: var(--ink-faint);
}

.fallback-icon {
  width: 3rem;
  height: 3rem;
}

.preview-info {
  flex: 1;
  min-width: 0;
}

.preview-eyebrow {
  margin: 0 0 0.2rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--navy);
}

.preview-title {
  font-size: 1.3rem;
  color: var(--ink);
  line-height: 1.25;
}

.preview-title-en {
  margin: 0.15rem 0 0;
  font-size: 0.85rem;
  color: var(--ink-soft);
  font-weight: 500;
}

.tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.65rem 0;
}

.preview-description {
  margin: 0.5rem 0 0;
  font-size: 0.92rem;
  color: var(--ink-soft);
  line-height: 1.55;
}

.license-box {
  margin-top: 0.9rem;
  padding: 0.75rem 0.9rem;
  border-radius: var(--radius-sm);
  background: var(--redpen-soft);
  border: 1px solid var(--redpen);
}

.license-attribution {
  margin: 0;
  font-size: 0.78rem;
  color: var(--redpen-ink);
  font-weight: 600;
  line-height: 1.45;
}

.license-condition {
  margin: 0.3rem 0 0;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--redpen-ink);
}

.license-note {
  margin: 0.5rem 0 0;
  font-size: 0.74rem;
  color: var(--ink-soft);
  font-style: italic;
  line-height: 1.45;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  padding: 0.5rem 1.5rem 1.5rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.7rem 1.2rem;
  border-radius: var(--radius-sm);
  font-weight: 700;
  font-size: 0.88rem;
  cursor: pointer;
  border: 1px solid transparent;
  text-decoration: none;
}

.btn--ghost {
  background: transparent;
  color: var(--ink-soft);
  border-color: var(--rule-strong);
}

.btn--ghost:hover {
  background: var(--kraft);
}

.btn--primary {
  background: var(--redpen);
  color: var(--kraft-raised);
}

.btn--primary:hover {
  background: var(--redpen-ink);
}

/* -------------------- responsive -------------------- */

@media (max-width: 420px) {
  .worksheet-grid {
    grid-template-columns: 1fr;
  }
}

/* Verified via CDP at 320/375/414/500px: no horizontal overflow found on
   this page. This block only lifts the masthead's two interactive
   controls to the 44x44px minimum touch target. */
@media (max-width: 500px) {
  .home-icon,
  .theme-toggle {
    width: 2.75rem;
    height: 2.75rem;
  }

  /* When the title wraps and pushes .masthead-actions to its own line,
     margin-left: auto keeps it pinned to the right instead of collapsing
     to flex-start (space-between has nothing to distribute for a lone
     item on a wrapped line). */
  .masthead-actions {
    margin-left: auto;
  }
}
