/* ==========================================================================
   Krizo Foundation (system.css)
   --------------------------------------------------------------------------
   Phase 1: shared responsive foundation for HTML/Jinja templates.

   Design contract:
     - ADDITIVE ONLY. Does not rewrite existing components.
     - Utility classes are namespaced with `k-` to avoid collisions with
       existing `.btn`, `.container`, etc. across glass.css / admin.css.
     - Tokens live on :root (safe to coexist with existing variables).
     - Global safety rules are kept conservative; iOS zoom-prevention is
       scoped to mobile so desktop input typography is untouched.

   Breakpoints (use ONLY these, globally):
     - phone:         @media (max-width: 480px)
     - large phone:   @media (max-width: 640px)
     - tablet:        @media (max-width: 980px)
     - small laptop:  @media (max-width: 1200px)
     - desktop:       @media (min-width: 1201px)
   ========================================================================== */


/* --------------------------------------------------------------------------
   1. Design tokens
   -------------------------------------------------------------------------- */
:root {
  /* Spacing */
  --k-space-2xs: 4px;
  --k-space-xs:  8px;
  --k-space-sm:  12px;
  --k-space-md:  16px;
  --k-space-lg:  24px;
  --k-space-xl:  32px;
  --k-space-2xl: 48px;
  --k-space-3xl: 64px;

  /* Radius */
  --k-radius-sm: 8px;
  --k-radius-md: 12px;
  --k-radius-lg: 14px;
  --k-radius-xl: 20px;
  --k-radius-pill: 999px;

  /* Layout */
  --k-container-max: 1200px;
  --k-container-pad: clamp(16px, 4vw, 32px);

  /* Touch target */
  --k-touch-min: 44px;

  /* Motion */
  --k-ease: cubic-bezier(0.4, 0, 0.2, 1);
  --k-dur-fast: 160ms;
  --k-dur-med:  240ms;
  --k-dur-slow: 360ms;

  /* Z-index scale */
  --k-z-base:    1;
  --k-z-sticky:  100;
  --k-z-drawer:  900;
  --k-z-modal:   1000;
  --k-z-toast:   1100;

  /* ── Font stack ─────────────────────────────────────────────────────────── */
  --k-font-sans: Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  --k-font-mono: "JetBrains Mono", SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;

  /* ── Typography scale ────────────────────────────────────────────────────── */
  --k-page-title-size:         26px;
  --k-page-title-size-tablet:  30px;
  --k-page-title-size-desktop: 32px;
  --k-page-title-weight:       800;
  --k-page-title-lh:           1.2;
  --k-section-title-size:         16px;
  --k-section-title-size-tablet:  16px;
  --k-section-title-size-desktop: 16px;
  --k-section-title-weight:       800;
  --k-section-title-lh:           1.3;
  --k-mini-title-size:         13px;
  --k-mini-title-size-tablet:  13px;
  --k-mini-title-size-desktop: 13px;
  --k-mini-title-weight:       700;
  --k-mini-title-lh:           1.3;
  --k-mini-title-tracking:     0.02em;
  --k-mini-title-transform:    none;
  --k-body-size:         11px;
  --k-body-size-tablet:  11px;
  --k-body-size-desktop: 11px;
  --k-body-weight:       400;
  --k-body-lh:           1.7;

  /* ── Text colors ─────────────────────────────────────────────────────────── */
  --k-text-primary:   #ffffff;
  --k-text-secondary: rgba(255, 255, 255, 0.90);
  --k-text-tertiary:  rgba(255, 255, 255, 0.75);
  --k-text-meta:      rgba(255, 255, 255, 0.50);

  /* ── Semantic colors ─────────────────────────────────────────────────────── */
  /* Action — primary CTA, buttons, links */
  --k-action:        #3b82f6;
  --k-action-hover:  #2563eb;
  /* Solid-fill buttons use -hover as their RESTING background, not --k-action
     directly: white text on #3b82f6 measures 3.68:1, below WCAG AA's 4.5:1
     for normal text. #2563eb (5.17:1) and this active shade (6.7:1) both
     pass; --k-action itself is untouched for links/icons/borders/glows,
     which aren't a text-on-fill contrast case. */
  --k-action-active: #1d4ed8;
  --k-action-soft:   rgba(59, 130, 246, 0.12);
  --k-action-border: rgba(59, 130, 246, 0.18);
  --k-action-text:   #93c5fd;

  /* Learning — study, coach, AI content */
  --k-learning:        #8b5cf6;
  --k-learning-hover:  #7c3aed;
  --k-learning-soft:   rgba(139, 92, 246, 0.12);
  --k-learning-border: rgba(139, 92, 246, 0.18);
  --k-learning-text:   #c4b5fd;

  /* Success — completed, verified, done */
  --k-success:        #10b981;
  --k-success-hover:  #059669;
  --k-success-soft:   rgba(16, 185, 129, 0.12);
  --k-success-border: rgba(16, 185, 129, 0.20);
  --k-success-text:   #34d399;

  /* Warning — pending, review, caution */
  --k-warning:        #f59e0b;
  --k-warning-hover:  #d97706;
  --k-warning-soft:   rgba(245, 158, 11, 0.12);
  --k-warning-border: rgba(245, 158, 11, 0.20);
  --k-warning-text:   #fcd34d;

  /* Danger — error, destructive, failed */
  --k-danger:        #ef4444;
  --k-danger-hover:  #dc2626;
  --k-danger-soft:   rgba(239, 68, 68, 0.12);
  --k-danger-border: rgba(239, 68, 68, 0.20);
  --k-danger-text:   #fca5a5;

  /* Status — neutral info, in-progress */
  --k-status:        #38bdf8;
  --k-status-hover:  #0ea5e9;
  --k-status-soft:   rgba(56, 189, 248, 0.10);
  --k-status-border: rgba(56, 189, 248, 0.18);
  --k-status-text:   #7dd3fc;

  /* ── Button tokens ───────────────────────────────────────────────────────── */
  --k-btn-height:       44px;
  --k-btn-height-sm:    36px;
  --k-btn-height-lg:    52px;
  --k-btn-padding-x:    24px;
  --k-btn-padding-x-sm: 16px;
  --k-btn-padding-x-lg: 32px;
  --k-btn-radius:       var(--k-radius-lg);
  --k-btn-size:         13px;
  --k-btn-size-sm:      12px;
  --k-btn-size-lg:      15px;
  --k-btn-weight:       700;
  --k-btn-tracking:     0.04em;

  /* ── Shadow tokens ───────────────────────────────────────────────────────── */
  --k-shadow-sm:      0 2px 8px rgba(0, 0, 0, 0.25);
  --k-shadow-md:      0 8px 24px rgba(0, 0, 0, 0.35);
  --k-shadow-lg:      0 16px 48px rgba(0, 0, 0, 0.50);
  --k-shadow-action:  0 4px 16px rgba(59, 130, 246, 0.35);
  --k-shadow-success: 0 4px 16px rgba(16, 185, 129, 0.30);
  --k-shadow-warning: 0 4px 16px rgba(245, 158, 11, 0.30);
  --k-shadow-danger:  0 4px 16px rgba(239, 68, 68, 0.30);
  --k-shadow-learning: 0 4px 16px rgba(139, 92, 246, 0.30);

  /* ── Surface tokens ──────────────────────────────────────────────────────── */
  --k-surface:              rgba(11, 15, 25, 0.96);
  --k-surface-raised:       rgba(17, 22, 34, 0.96);
  --k-surface-border:       rgba(255, 255, 255, 0.07);
  --k-surface-border-hover: rgba(255, 255, 255, 0.14);
  --k-card-radius:          20px;
  --k-card-padding:         24px;
  --k-card-padding-sm:      16px;

  /* ── Badge tokens ────────────────────────────────────────────────────────── */
  --k-badge-size:     11px;
  --k-badge-weight:   700;
  --k-badge-tracking: 0.10em;
  --k-badge-pad-y:    3px;
  --k-badge-pad-x:    9px;

  /* ── Input tokens ────────────────────────────────────────────────────────── */
  --k-input-height:       44px;
  --k-input-radius:       var(--k-radius-md);
  --k-input-bg:           rgba(0, 0, 0, 0.20);
  --k-input-border:       rgba(255, 255, 255, 0.10);
  --k-input-border-focus: rgba(59, 130, 246, 0.50);
  --k-input-size:         15px;
}

/* Responsive typography (one place) — re-point base size tokens per breakpoint
   so any element using var(--k-*-size) scales automatically. */
@media (min-width: 768px) {
  :root {
    --k-page-title-size:    var(--k-page-title-size-tablet);
    --k-section-title-size: var(--k-section-title-size-tablet);
    --k-mini-title-size:    var(--k-mini-title-size-tablet);
    --k-body-size:          var(--k-body-size-tablet);
  }
}
@media (min-width: 1024px) {
  :root {
    --k-page-title-size:    var(--k-page-title-size-desktop);
    --k-section-title-size: var(--k-section-title-size-desktop);
    --k-mini-title-size:    var(--k-mini-title-size-desktop);
    --k-body-size:          var(--k-body-size-desktop);
  }
}


/* --------------------------------------------------------------------------
   2. Global safety rules
   --------------------------------------------------------------------------
   These are intentionally conservative. They mirror what modern CSS resets
   already provide and are safe to layer on top of glass.css / admin.css.
   -------------------------------------------------------------------------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}

img,
svg,
video,
canvas {
  max-width: 100%;
  height: auto;
}

/* Prevent iOS Safari from zooming form fields on focus.
   Scoped to mobile so we don't bump desktop input typography. */
@media (max-width: 640px) {
  input,
  textarea,
  select {
    font-size: 16px;
  }
}


/* --------------------------------------------------------------------------
   3. Container system  (opt-in via `.k-container`)
   -------------------------------------------------------------------------- */
.k-container {
  width: min(var(--k-container-max), 100%);
  margin-inline: auto;
  padding-inline: var(--k-container-pad);
}

.k-container-narrow {
  width: min(880px, 100%);
  margin-inline: auto;
  padding-inline: var(--k-container-pad);
}

.k-container-wide {
  width: min(1440px, 100%);
  margin-inline: auto;
  padding-inline: var(--k-container-pad);
}


/* --------------------------------------------------------------------------
   4. Fluid typography  (opt-in via `.k-*` classes)
   -------------------------------------------------------------------------- */
.k-hero-title {
  font-size: clamp(36px, 6vw, 72px);
  line-height: 1.05;
  letter-spacing: -0.02em;
  font-weight: 800;
}

.k-section-title {
  font-size: clamp(24px, 4vw, 42px);
  line-height: 1.15;
  letter-spacing: -0.01em;
  font-weight: 700;
}

.k-subsection-title {
  font-size: clamp(20px, 2.4vw, 28px);
  line-height: 1.25;
  font-weight: 600;
}

.k-body-text {
  font-size: clamp(14px, 1.6vw, 18px);
  line-height: 1.6;
}

.k-small-text {
  font-size: clamp(12px, 1.2vw, 14px);
  line-height: 1.5;
}


/* --------------------------------------------------------------------------
   5. Button system  (opt-in via `.k-btn`)
   --------------------------------------------------------------------------
   This sits alongside existing `.btn`, `.btn-primary` etc. and does NOT
   override them. Use `.k-btn` when building NEW components on the
   foundation.
   -------------------------------------------------------------------------- */
.k-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--k-space-xs);
  min-height: var(--k-touch-min);
  padding: 0 var(--k-space-lg);
  border-radius: var(--k-radius-lg);
  border: 1px solid transparent;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  text-decoration: none;
  transition:
    background-color var(--k-dur-fast) var(--k-ease),
    border-color var(--k-dur-fast) var(--k-ease),
    color var(--k-dur-fast) var(--k-ease),
    transform var(--k-dur-fast) var(--k-ease);
  -webkit-tap-highlight-color: transparent;
}

.k-btn:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 2px;
}

.k-btn:disabled,
.k-btn[aria-disabled="true"] {
  opacity: 0.55;
  cursor: not-allowed;
}

.k-btn-block {
  width: 100%;
}


/* --------------------------------------------------------------------------
   6. Grid system  (opt-in)
   -------------------------------------------------------------------------- */
.k-grid-auto {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: var(--k-space-lg);
}

.k-grid-auto-sm {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: var(--k-space-md);
}

.k-grid-auto-lg {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: var(--k-space-xl);
}

.k-grid-split {
  display: grid;
  grid-template-columns: 1.15fr 0.85fr;
  gap: var(--k-space-xl);
}

.k-grid-halves {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--k-space-xl);
}

.k-grid-thirds {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--k-space-lg);
}

@media (max-width: 980px) {
  .k-grid-split,
  .k-grid-halves {
    grid-template-columns: 1fr;
  }
  .k-grid-thirds {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 640px) {
  .k-grid-thirds {
    grid-template-columns: 1fr;
  }
}


/* --------------------------------------------------------------------------
   7. Stack / row helpers
   -------------------------------------------------------------------------- */
.k-stack    { display: flex; flex-direction: column; gap: var(--k-space-md); }
.k-stack-sm { display: flex; flex-direction: column; gap: var(--k-space-sm); }
.k-stack-lg { display: flex; flex-direction: column; gap: var(--k-space-lg); }

.k-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--k-space-sm);
  align-items: center;
}

.k-row-between {
  display: flex;
  flex-wrap: wrap;
  gap: var(--k-space-sm);
  align-items: center;
  justify-content: space-between;
}


/* --------------------------------------------------------------------------
   8. Toolbars (wrap on small screens, per Rule 6)
   -------------------------------------------------------------------------- */
.k-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--k-space-sm);
  align-items: center;
}


/* --------------------------------------------------------------------------
   9. Responsive visibility helpers
   -------------------------------------------------------------------------- */
.k-hide-mobile  { }
.k-show-mobile  { display: none; }
.k-hide-tablet  { }
.k-show-tablet  { display: none; }

@media (max-width: 980px) {
  .k-hide-tablet  { display: none !important; }
  .k-show-tablet  { display: initial; }
}

@media (max-width: 640px) {
  .k-hide-mobile  { display: none !important; }
  .k-show-mobile  { display: initial; }
}


/* --------------------------------------------------------------------------
   10. Table responsiveness helper  (opt-in)
   --------------------------------------------------------------------------
   Wrap any wide table in `<div class="k-table-scroll">` to get safe
   horizontal scroll on small screens without layout overflow.
   -------------------------------------------------------------------------- */
.k-table-scroll {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}


/* --------------------------------------------------------------------------
   11. Modal / drawer mobile pattern  (opt-in)
   --------------------------------------------------------------------------
   Add `.k-modal-mobile-fullscreen` to a modal/dialog wrapper to make it
   take the full viewport on small phones, per Rule 5.
   -------------------------------------------------------------------------- */
@media (max-width: 640px) {
  .k-modal-mobile-fullscreen {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    max-width: none;
    max-height: none;
    border-radius: 0;
    margin: 0;
  }
}


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