/* ==========================================================================
 * Sống Không Lo (songkhonglo.vn) — Universal 9-Tier Fluid Grid & Layout Engine
 * Roles: 006 (9-Tier Matrix), 069 (VN Typography), 085 (Container/Subgrid),
 *        091 (Nav/Thumb-Zone), 092 (Foldable/4K), 185 (Clamp Scale),
 *        326 (Tier 4-5 Tablet), 327 (Tier 6-8 Desktop/Widescreen)
 * Target Range: 320px -> 3840px (scrollWidth <= innerWidth, 0px horizontal overflow)
 * Replaces runtime Tailwind CDN for core layout, grids, and ergonomics.
 * ========================================================================== */

@layer reset, tokens, base, layout, components, utilities;

/* --------------------------------------------------------------------------
 * 1. Modern Defensive Reset & Vietnamese Overflow Protection (Role 069/092)
 * -------------------------------------------------------------------------- */
@layer reset {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    scroll-behavior: smooth;
    scroll-padding-top: calc(var(--header-height) + 1rem);
    overflow-x: clip;
    max-width: 100%;
  }

  @media (prefers-reduced-motion: reduce) {
    html {
      scroll-behavior: auto;
    }
  }

  body {
    margin: 0;
    padding: 0;
    min-height: 100dvh;
    width: 100%;
    max-width: 100%;
    overflow-x: clip;
    font-family: var(--font-sans);
    font-size: var(--text-body);
    line-height: var(--leading-normal);
    color: var(--ink-primary);
    background-color: var(--surface-canvas);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    overflow-wrap: anywhere;
  }

  img,
  picture,
  svg,
  video,
  canvas,
  iframe {
    display: block;
    max-width: 100%;
  }

  img,
  video {
    height: auto;
  }

  a {
    color: inherit;
    text-decoration: none;
  }

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

  button {
    cursor: pointer;
    background: none;
    border: none;
  }

  details > summary {
    list-style: none;
  }

  details > summary::-webkit-details-marker {
    display: none;
  }

  :focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: var(--focus-ring-offset);
  }
}

/* --------------------------------------------------------------------------
 * 2. Vietnamese Fluid Typography & Editorial Typesetting (Role 069 & 185)
 *    Extra line-height headroom prevents stacked diacritic clipping (ẫ, ệ, ồ, ử)
 * -------------------------------------------------------------------------- */
@layer base {
  h1,
  h2,
  h3,
  h4,
  h5,
  h6 {
    margin: 0;
    font-family: var(--font-sans);
    font-weight: 800;
    color: var(--ink-brand-heading);
    line-height: var(--leading-tight);
    letter-spacing: -0.015em;
    text-wrap: balance;
  }

  h1,
  .type-hero {
    font-size: var(--text-hero);
    line-height: 1.18;
  }

  h2,
  .type-h2 {
    font-size: var(--text-h2);
    line-height: 1.24;
  }

  h3,
  .type-h3 {
    font-size: var(--text-h3);
    font-weight: 700;
    line-height: 1.32;
  }

  h4,
  .type-h4 {
    font-size: var(--text-h4);
    font-weight: 700;
    line-height: 1.38;
  }

  p {
    margin: 0 0 var(--space-sm);
    text-wrap: pretty;
  }

  .prose-finance {
    max-width: var(--container-prose);
    font-size: var(--text-editorial);
    line-height: var(--leading-editorial);
    color: var(--ink-secondary);
  }

  .prose-finance h2 {
    color: var(--color-brand-forest);
    font-size: clamp(1.35rem, 1.15rem + 0.9vw, 1.75rem);
    margin-top: var(--space-xl);
    margin-bottom: var(--space-sm);
    line-height: 1.32;
  }

  .prose-finance h3 {
    color: var(--ink-primary);
    font-size: clamp(1.125rem, 1.02rem + 0.45vw, 1.35rem);
    margin-top: var(--space-lg);
    margin-bottom: var(--space-xs);
    line-height: 1.38;
  }

  .prose-finance p {
    margin-bottom: var(--space-md);
    line-height: var(--leading-editorial);
  }

  .prose-finance strong {
    color: var(--ink-primary);
    font-weight: 700;
  }

  .font-mono,
  .order-code,
  .voucher-code {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
  }
}

/* --------------------------------------------------------------------------
 * 3. Accessibility Skip-Link, Touch Targets & Safe-Area Primitives (Role 091)
 * -------------------------------------------------------------------------- */
@layer components {
  .skip-link {
    position: fixed;
    top: -100%;
    left: max(1rem, env(safe-area-inset-left));
    z-index: var(--z-skip-link);
    display: inline-flex;
    align-items: center;
    min-height: var(--touch-target-min);
    padding: 0.75rem 1.25rem;
    border-radius: var(--radius-md);
    background-color: var(--color-brand-forest);
    color: var(--ink-on-brand);
    font-weight: 700;
    font-size: var(--text-sm);
    box-shadow: var(--shadow-cockpit);
    border: 2px solid var(--color-brand-mint);
  }

  .skip-link:focus,
  .skip-link:focus-visible {
    top: max(0.75rem, env(safe-area-inset-top));
  }

  .touch-target,
  .btn-cockpit,
  .lms-item-btn,
  .nav-link-item {
    min-height: var(--touch-target-min);
    min-width: var(--touch-target-min);
  }

  .btn-primary-cta {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    min-height: var(--cta-target-height);
    padding: 0.75rem 1.375rem;
    border-radius: var(--radius-md);
    background-color: var(--color-brand-emerald);
    color: #ffffff;
    font-weight: 700;
    font-size: var(--text-body);
    line-height: 1.25;
    box-shadow: var(--shadow-sm);
    transition: transform var(--duration-fast) var(--ease-standard),
      background-color var(--duration-fast) var(--ease-standard);
  }

  .btn-primary-cta:hover {
    background-color: var(--color-brand-emerald-ink);
    transform: translateY(-1px);
  }

  .btn-secondary-cta {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    min-height: var(--touch-target-min);
    padding: 0.625rem 1.125rem;
    border-radius: var(--radius-md);
    border: 1.5px solid var(--border-strong);
    background-color: var(--surface-primary);
    color: var(--ink-primary);
    font-weight: 600;
    font-size: var(--text-sm);
  }

  /* Sticky Single-Row Cockpit Header + Logo Lock */
  .site-header {
    position: sticky;
    top: 0;
    z-index: var(--z-header);
    width: 100%;
    min-height: var(--header-height);
    padding-top: env(safe-area-inset-top);
    padding-inline: max(var(--container-gutter), env(safe-area-inset-left))
      max(var(--container-gutter), env(safe-area-inset-right));
    background-color: var(--surface-glass);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border-subtle);
  }

  .site-header-inner {
    max-width: var(--container-cockpit);
    margin-inline: auto;
    min-height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-sm);
  }

  .site-header-logo-box {
    width: 48px;
    height: 48px;
    min-width: 48px;
    min-height: 48px;
    max-width: 48px;
    max-height: 48px;
    flex-shrink: 0;
  }

  @media (min-width: 640px) {
    .site-header-logo-box {
      width: 56px;
      height: 56px;
      min-width: 56px;
      min-height: 56px;
      max-width: 56px;
      max-height: 56px;
    }
  }

  /* Breadcrumb Trail (Canonical Route Compatible) */
  .breadcrumb-nav {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    overflow-x: auto;
    scrollbar-width: none;
    white-space: nowrap;
    font-size: var(--text-xs);
    color: var(--ink-muted);
    padding-block: 0.375rem;
  }

  .breadcrumb-nav::-webkit-scrollbar {
    display: none;
  }

  .breadcrumb-nav a {
    display: inline-flex;
    align-items: center;
    min-height: 36px;
    padding-inline: 0.25rem;
    border-radius: var(--radius-sm);
  }

  .breadcrumb-nav a:hover {
    color: var(--ink-accent-emerald);
  }

  /* Sticky Thumb-Zone Mobile Action Bar (Tiers 1-4: <768px) */
  .mobile-thumb-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: var(--z-thumb-bar);
    display: grid;
    grid-template-columns: 1fr 1.35fr;
    gap: 0.625rem;
    padding: 0.625rem max(1rem, env(safe-area-inset-right))
      calc(0.625rem + env(safe-area-inset-bottom))
      max(1rem, env(safe-area-inset-left));
    background-color: var(--surface-glass);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-top: 1px solid var(--border-subtle);
    box-shadow: 0 -10px 25px -10px rgba(5, 46, 22, 0.14);
  }

  @media (min-width: 768px) {
    .mobile-thumb-bar {
      display: none;
    }
  }

  /* Reserve bottom safe space on mobile when thumb bar is active */
  @media (max-width: 767.98px) {
    body.has-thumb-bar {
      padding-bottom: calc(4.75rem + env(safe-area-inset-bottom));
    }
  }

  /* Comparison Table Overflow-Safe Wrapper */
  .comparison-table-wrap {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--radius-card);
    border: 1px solid var(--border-subtle);
    background-color: var(--surface-primary);
  }

  .comparison-table {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--text-sm);
  }

  .comparison-table th,
  .comparison-table td {
    padding: 0.75rem 1rem;
    text-align: left;
    border-bottom: 1px solid var(--border-subtle);
  }

  .comparison-table th {
    background-color: var(--surface-subtle);
    color: var(--ink-brand-heading);
    font-weight: 700;
  }

  /* Neutralize Legacy .stacking-cover Desktop Trap (AUD / Principle 3) */
  .stacking-cover {
    position: relative;
    top: auto;
    min-height: auto;
    overflow: visible;
    width: 100%;
    max-width: 100%;
    border-top: 1px solid var(--border-subtle);
  }

  /* Reading Progress Bar */
  #reading-progress,
  #scroll-progress-bar {
    position: fixed;
    top: 0;
    left: 0;
    height: 3px;
    width: 0%;
    background: var(--gradient-reading-progress);
    z-index: var(--z-progress-bar);
    pointer-events: none;
  }
}

/* --------------------------------------------------------------------------
 * 4. Core Fluid Containers, Bento & Cockpit Grids (Roles 006, 085, 185)
 * -------------------------------------------------------------------------- */
@layer layout {
  .container-cockpit,
  .max-w-7xl {
    width: 100%;
    max-width: min(var(--container-cockpit), 100%);
    margin-inline: auto;
    padding-inline: max(var(--container-gutter), env(safe-area-inset-left));
  }

  .container-editorial,
  .max-w-4xl {
    width: 100%;
    max-width: min(var(--container-reading), 100%);
    margin-inline: auto;
    padding-inline: max(var(--container-gutter), env(safe-area-inset-left));
  }

  .max-w-5xl {
    width: 100%;
    max-width: min(64rem, 100%);
    margin-inline: auto;
  }

  .max-w-3xl {
    width: 100%;
    max-width: min(48rem, 100%);
    margin-inline: auto;
  }

  .max-w-2xl {
    width: 100%;
    max-width: min(42rem, 100%);
    margin-inline: auto;
  }

  .section-fluid {
    padding-block: var(--space-2xl);
    padding-inline: env(safe-area-inset-left) env(safe-area-inset-right);
  }

  /* Hero First-Fold SVH Stability (never clipped by mobile browser chrome) */
  .hero-svh-shell {
    min-height: clamp(32rem, 84svh, 52rem);
    display: grid;
    align-items: center;
    padding-block: var(--space-xl);
  }

  /* Grid Primitives */
  .grid-12,
  .bento-7-5,
  .bento-8-4,
  .grid-pots-5,
  .grid-value-ladder,
  .grid-auto-cards,
  .editorial-split-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-md);
  }

  .grid-auto-cards {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 17.5rem), 1fr));
  }

  /* Named Container Contexts (Role 085 — Intrinsic Component Adaptation) */
  .cockpit-shell,
  .bento-card-wrap,
  .course-grid-wrap,
  .blog-grid-wrap,
  .pot-grid-wrap,
  .lms-layout-wrap {
    container-type: inline-size;
  }

  .cockpit-shell {
    container-name: cockpit;
  }

  .bento-card-wrap {
    container-name: bentocard;
  }

  .pot-grid-wrap {
    container-name: pots;
  }

  .course-grid-wrap {
    container-name: courses;
  }

  .blog-grid-wrap {
    container-name: blogs;
  }

  .lms-layout-wrap {
    container-name: lms;
  }

  /* Container Query Rules */
  @container cockpit (min-width: 44rem) {
    .cockpit-split {
      display: grid;
      grid-template-columns: minmax(0, 1fr) minmax(0, 1.05fr);
      gap: var(--space-lg);
      align-items: start;
    }
  }

  @container pots (min-width: 34rem) {
    .grid-pots-5 {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }
  }

  @container pots (min-width: 56rem) {
    .grid-pots-5 {
      grid-template-columns: repeat(5, minmax(0, 1fr));
    }
  }

  @container courses (min-width: 38rem) {
    .grid-value-ladder {
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }
  }

  @container courses (min-width: 68rem) {
    .grid-value-ladder {
      grid-template-columns: repeat(4, minmax(0, 1fr));
    }
  }

  @container blogs (min-width: 38rem) {
    .grid-blog-archive {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: var(--space-md);
    }
  }

  @container blogs (min-width: 60rem) {
    .grid-blog-archive {
      grid-template-columns: repeat(3, minmax(0, 1fr));
    }
  }

  /* CSS Subgrid for Aligned Card Rows (Role 085) */
  @supports (grid-template-rows: subgrid) {
    .bento-subgrid-cards {
      display: grid;
      gap: var(--space-md);
    }

    .bento-subgrid-card {
      display: grid;
      grid-template-rows: subgrid;
      grid-row: span 4; /* [1] badge/title, [2] metric/price, [3] body/features, [4] footer CTA */
      gap: var(--space-xs);
    }
  }

  /* Native Dialog & Modal Dynamic Viewport Constraints */
  dialog.skl-dialog,
  .skl-modal-panel {
    width: min(94vw, 48rem);
    max-height: 92dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    margin: auto;
    border-radius: var(--radius-cockpit-panel);
    padding: var(--space-lg);
    padding-bottom: calc(var(--space-lg) + env(safe-area-inset-bottom));
  }

  dialog.skl-dialog-wide {
    width: min(96vw, 76rem);
    max-height: 92dvh;
  }
}

/* --------------------------------------------------------------------------
 * 5. Universal 9-Tier Responsive Breakpoint Matrix (320px -> 3840px)
 *    Tier 1: 320-374px   | Tier 2: 375-479px   | Tier 3: 480-639px
 *    Tier 4: 640-767px   | Tier 5: 768-1023px  | Tier 6: 1024-1279px
 *    Tier 7: 1280-1919px | Tier 8: 1920-2559px | Tier 9: 2560-3840px+
 * -------------------------------------------------------------------------- */
@layer layout {
  /* TIER 1 (320px - 374px): Cover screens & compact phones */
  @media (max-width: 374.98px) {
    :root {
      --container-gutter: 1rem;
    }

    .cta-group-mobile-full > a,
    .cta-group-mobile-full > button {
      width: 100%;
    }
  }

  /* TIER 2 (375px - 479px): Standard smartphones (iPhone Pro, Galaxy S) */
  @media (min-width: 375px) {
    .grid-metrics-compact {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
      gap: var(--space-xs);
    }
  }

  /* TIER 3 (480px - 639px): Large flagship phones (Pro Max, Ultra) */
  @media (min-width: 480px) {
    .hero-svh-shell {
      min-height: clamp(34rem, 86svh, 54rem);
    }
  }

  /* TIER 4 (640px - 767px): Phablets & Unfolded Foldables (Role 326 & 092) */
  @media (min-width: 640px) {
    .sm\:grid-cols-2 {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .bento-7-5,
    .bento-8-4 {
      gap: var(--space-lg);
    }
  }

  /* Foldable Dual-Screen Hinge Safety (Galaxy Z Fold / Surface Duo) */
  @media (horizontal-viewport-segments: 2) {
    .foldable-hinge-grid {
      display: grid;
      grid-template-columns: env(viewport-segment-width 0 0) env(viewport-segment-width 1 0);
      column-gap: calc(env(viewport-segment-left 1 0) - env(viewport-segment-right 0 0));
    }
  }

  /* TIER 5 (768px - 1023px): Tablets Portrait & Landscape (Role 326) */
  @media (min-width: 768px) {
    .md\:grid-cols-2 {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .md\:grid-cols-3 {
      display: grid;
      grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .grid-12 {
      grid-template-columns: repeat(12, minmax(0, 1fr));
    }

    .bento-7-5 {
      grid-template-columns: minmax(0, 7fr) minmax(0, 5fr);
      align-items: stretch;
    }

    .bento-8-4 {
      grid-template-columns: minmax(0, 8fr) minmax(0, 4fr);
      align-items: start;
    }

    /* LMS Portal Sidebar + Player Split */
    .lms-split-shell {
      display: flex;
      align-items: stretch;
      min-height: min(82dvh, 52rem);
    }

    #lms-sidebar {
      display: flex;
      flex-direction: column;
      width: 22rem;
      max-width: 24rem;
      flex-shrink: 0;
    }

    #lms-main {
      display: flex;
      flex-direction: column;
      flex: 1 1 0%;
      min-width: 0;
    }
  }

  /* TIER 6 (1024px - 1279px): Laptops & Small Desktops (Role 326/327) */
  @media (min-width: 1024px) {
    .lg\:grid-cols-2 {
      display: grid;
      grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .lg\:grid-cols-3 {
      display: grid;
      grid-template-columns: repeat(3, minmax(0, 1fr));
    }

    .lg\:grid-cols-4 {
      display: grid;
      grid-template-columns: repeat(4, minmax(0, 1fr));
    }

    .editorial-split-layout {
      grid-template-columns: minmax(0, 1fr) 18.5rem;
      gap: var(--space-xl);
      align-items: start;
    }

    .editorial-toc-sticky {
      position: sticky;
      top: calc(var(--header-height) + 1.5rem);
      max-height: calc(100dvh - var(--header-height) - 3rem);
      overflow-y: auto;
    }
  }

  /* TIER 7 (1280px - 1919px): Standard Desktops (Role 327) */
  @media (min-width: 1280px) {
    :root {
      --container-gutter: 2rem;
    }

    .xl\:grid-cols-4 {
      display: grid;
      grid-template-columns: repeat(4, minmax(0, 1fr));
    }

    .editorial-split-layout {
      grid-template-columns: minmax(0, 1fr) 20rem;
    }
  }

  /* TIER 8 (1920px - 2559px): Full HD & Large Studio Monitors (Role 327) */
  @media (min-width: 1920px) {
    :root {
      --container-cockpit: 84rem;
      --container-reading: 58rem;
    }
  }

  /* TIER 9 (2560px - 3840px+): 4K & Ultrawide Hard Rail Lock (Role 092) */
  @media (min-width: 2560px) {
    :root {
      --container-cockpit: var(--container-ultrawide); /* Hard 90rem / 1440px cap */
      --container-reading: 60rem;
    }

    body {
      background-image: radial-gradient(
        circle at 50% 0%,
        rgba(5, 150, 105, 0.04) 0%,
        transparent 55%
      );
    }
  }
}

/* --------------------------------------------------------------------------
 * 6. Essential Static Layout & Utility Primitives (Zero Tailwind CDN)
 * -------------------------------------------------------------------------- */
@layer utilities {
  .sr-only {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0, 0, 0, 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
  }

  .hidden {
    display: none;
  }

  .block { display: block; }
  .inline-block { display: inline-block; }
  .flex { display: flex; }
  .inline-flex { display: inline-flex; }
  .grid { display: grid; }
  .flex-col { flex-direction: column; }
  .flex-wrap { flex-wrap: wrap; }
  .items-start { align-items: flex-start; }
  .items-center { align-items: center; }
  .items-stretch { align-items: stretch; }
  .justify-between { justify-content: space-between; }
  .justify-center { justify-content: center; }
  .shrink-0 { flex-shrink: 0; }
  .flex-1 { flex: 1 1 0%; min-width: 0; }

  .w-full { width: 100%; }
  .h-full { height: 100%; }
  .mx-auto { margin-inline: auto; }
  .relative { position: relative; }
  .overflow-hidden { overflow: hidden; }
  .text-left { text-align: left; }
  .text-center { text-align: center; }
  .truncate {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .gap-1 { gap: 0.25rem; }
  .gap-2 { gap: 0.5rem; }
  .gap-3 { gap: 0.75rem; }
  .gap-4 { gap: 1rem; }
  .gap-6 { gap: 1.5rem; }
  .gap-8 { gap: 2rem; }

  .p-3 { padding: 0.75rem; }
  .p-4 { padding: 1rem; }
  .p-5 { padding: 1.25rem; }
  .p-6 { padding: 1.5rem; }
  .px-3 { padding-inline: 0.75rem; }
  .px-4 { padding-inline: 1rem; }
  .py-1 { padding-block: 0.25rem; }
  .py-2 { padding-block: 0.5rem; }
  .py-3 { padding-block: 0.75rem; }

  .rounded-lg { border-radius: var(--radius-sm); }
  .rounded-xl { border-radius: var(--radius-md); }
  .rounded-2xl { border-radius: var(--radius-card); }
  .rounded-3xl { border-radius: var(--radius-cockpit-panel); }
  .rounded-full { border-radius: var(--radius-pill); }

  .hide-mobile {
    display: none;
  }

  @media (min-width: 1024px) {
    .hide-mobile {
      display: flex;
    }

    .hide-desktop {
      display: none !important;
    }
  }
}
