/* ============================================
   HypeCalculator – Main Stylesheet
   Fonts: Syne (headings) + Outfit (body) + Space Mono (numbers)
   ============================================ */

:root {
  --bg:           #f8f7f4;
  --surface:      #ffffff;
  --surface-2:    #f1f0ec;
  --border:       #e2e0d8;
  --text:         #1c1917;
  --text-muted:   #78716c;
  --primary:      #1d4ed8;
  --primary-dark: #1e3a8a;
  --primary-light:#eff6ff;
  --accent:       #f59e0b;
  --success:      #16a34a;
  --result-bg:    #0f172a;
  --result-text:  #f0fdf4;
  --radius:       10px;
  --radius-lg:    16px;
  --shadow:       0 1px 3px rgba(0,0,0,.08), 0 4px 16px rgba(0,0,0,.06);
  --shadow-lg:    0 8px 32px rgba(0,0,0,.12);
  --font-head:    'Syne', sans-serif;
  --font-body:    'Outfit', sans-serif;
  --font-mono:    'Space Mono', monospace;
  --max-width:    1140px;
  --calc-width:   640px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { scroll-behavior: smooth; }

body {
  /* PERF: System font fallback closely matches Outfit's metrics.
     Text renders immediately in the fallback, then swaps with minimal reflow
     once Outfit loads — reduces Speed Index by shrinking the invisible-text window. */
  font-family: 'Outfit', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 17px;
  line-height: 1.7;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Layout ── */
.container { max-width: var(--max-width); margin: 0 auto; padding: 0 24px; width: 100%; box-sizing: border-box; }

/* ── Header ── */
.site-header {
  background: var(--result-bg);
  position: sticky; top: 0; z-index: 100;
  border-bottom: 1px solid rgba(255,255,255,.08);
  /* PERF: Promotes sticky header to its own compositor layer.
     Prevents the browser repainting the entire page on every scroll tick. */
  will-change: transform;
}
.header-inner {
  display: flex; align-items: center; gap: 32px;
  height: 62px;
}
.logo {
  display: flex; align-items: center; gap: 10px;
  font-family: var(--font-head); font-size: 1.3rem; font-weight: 800;
  color: #fff; text-decoration: none; flex-shrink: 0;
}
.logo-icon { font-size: 1.5rem; color: var(--accent); }
.logo:hover { text-decoration: none; opacity: .9; }

.main-nav { display: flex; gap: 4px; margin-left: auto; }
.main-nav a {
  color: rgba(255,255,255,.75); font-size: .92rem; font-weight: 500;
  /* FIX #11: Minimum 44px touch target height */
  padding: 11px 14px; border-radius: 6px; transition: background .18s, color .18s;
  display: inline-flex; align-items: center; min-height: 44px;
}
.main-nav a:hover { color: #fff; background: rgba(255,255,255,.1); text-decoration: none; }

/* FIX #1: Hamburger hidden by default; revealed at ≤768px, not just ≤640px */
.nav-toggle { display: none; background: none; border: none; color: #fff; font-size: 1.4rem; cursor: pointer; margin-left: auto; min-width: 44px; min-height: 44px; }

/* ── Breadcrumb ── */
.breadcrumb-bar { background: var(--surface-2); border-bottom: 1px solid var(--border); padding: 8px 0; font-size: .85rem; }
.breadcrumb { list-style: none; display: flex; gap: 6px; align-items: center; }
.breadcrumb li + li::before { content: '›'; color: var(--text-muted); margin-right: 6px; }
.breadcrumb a { color: var(--text-muted); }
.breadcrumb a:hover { color: var(--primary); }
.breadcrumb li:last-child { color: var(--text-muted); font-weight: 500; }

/* ── Main ── */
.main-content { min-height: 70vh; }

/* ── Hero (homepage) ── */
.hero {
  background: var(--result-bg);
  color: #fff;
  padding: 64px 0 56px;
  text-align: center;
  position: relative; overflow: hidden;
  /* PERF: Reserves above-the-fold space before web fonts load.
     Without this, the hero reflows when Syne swaps in, pushing content
     down and inflating the Speed Index score. */
  min-height: 320px;
}
.hero::before {
  content: '';
  position: absolute; inset: 0;
  background: radial-gradient(ellipse 70% 60% at 50% 0%, rgba(29,78,216,.45), transparent);
  pointer-events: none;
}
.hero-label {
  display: inline-block;
  font-family: var(--font-mono); font-size: .75rem; letter-spacing: .1em; text-transform: uppercase;
  color: var(--accent); background: rgba(245,158,11,.12);
  border: 1px solid rgba(245,158,11,.3);
  padding: 4px 14px; border-radius: 100px; margin-bottom: 20px;
}
.hero h1 {
  font-family: var(--font-head); font-size: clamp(2.2rem, 5vw, 3.4rem); font-weight: 800;
  line-height: 1.15; margin-bottom: 16px;
}
.hero p { font-size: 1.1rem; color: rgba(255,255,255,.65); max-width: 500px; margin: 0 auto 32px; }
.hero-search { display: flex; gap: 10px; max-width: 420px; margin: 0 auto; }
.hero-search input {
  flex: 1; padding: 12px 18px; border-radius: var(--radius); border: 1px solid rgba(255,255,255,.15);
  background: rgba(255,255,255,.08); color: #fff; font-family: var(--font-body); font-size: .95rem;
  outline: none;
}
.hero-search input::placeholder { color: rgba(255,255,255,.4); }
.hero-search input:focus { border-color: var(--accent); background: rgba(255,255,255,.12); }
.hero-search button {
  padding: 12px 20px; border-radius: var(--radius); border: none;
  background: var(--accent); color: #1c1917; font-weight: 700; cursor: pointer;
  font-family: var(--font-body); font-size: .95rem; transition: opacity .18s;
  white-space: nowrap;
}
.hero-search button:hover { opacity: .88; }

/* ── Category Grid ── */
.categories-section { padding: 56px 0; }
.section-heading {
  font-family: var(--font-head); font-size: 1.6rem; font-weight: 700;
  margin-bottom: 28px;
}
.section-heading span { color: var(--primary); }
.category-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 18px; margin-bottom: 48px; }
.category-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 24px;
  transition: box-shadow .2s, transform .2s, border-color .2s;
  cursor: pointer;
}
.category-card:hover {
  box-shadow: var(--shadow-lg); transform: translateY(-2px);
  border-color: var(--primary); text-decoration: none;
}
.category-card .cat-icon { font-size: 2rem; margin-bottom: 12px; display: block; }
.category-card h3 { font-family: var(--font-head); font-size: 1rem; font-weight: 700; margin-bottom: 6px; color: var(--text); }
.category-card p  { font-size: .85rem; color: var(--text-muted); line-height: 1.5; }

/* Calculator list */
.calc-list { list-style: none; display: flex; flex-direction: column; gap: 2px; }
.calc-list a {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); font-size: .95rem; color: var(--text);
  transition: all .15s;
}
.calc-list a:hover { background: var(--primary-light); border-color: var(--primary); color: var(--primary); text-decoration: none; }
.calc-list a::after { content: '→'; color: var(--text-muted); }
.calc-list a:hover::after { color: var(--primary); }

/* ── Calculator Page ── */
.calc-page { padding: 40px 0 64px; }
.calc-page-inner { display: grid; grid-template-columns: 1fr 300px; gap: 40px; align-items: start; }

.calc-header { margin-bottom: 28px; }
.calc-header .cat-badge {
  display: inline-block; font-size: .78rem; font-weight: 600; text-transform: uppercase; letter-spacing: .06em;
  color: var(--primary); background: var(--primary-light); padding: 3px 10px; border-radius: 100px; margin-bottom: 12px;
}
.calc-header h1 { font-family: var(--font-head); font-size: clamp(1.8rem, 4vw, 2.4rem); font-weight: 800; line-height: 1.2; margin-bottom: 10px; }
.calc-header .intro { color: var(--text-muted); font-size: 1rem; max-width: 560px; }

/* Calculator widget */
.calc-widget {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); overflow: hidden;
  box-shadow: var(--shadow); margin-bottom: 40px;
  width: 100%; max-width: 100%; box-sizing: border-box;
}
.calc-widget-header {
  background: var(--result-bg); padding: 10px 16px;
  display: flex; align-items: center; gap: 8px;
  overflow: hidden; min-width: 0;
}
.calc-widget-header span:last-child {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1;
}
.calc-widget-header span { font-family: var(--font-mono); font-size: .8rem; color: rgba(255,255,255,.5); }
.dot { width: 10px; height: 10px; border-radius: 50%; }
.dot-r { background: #ef4444; }
.dot-y { background: var(--accent); }
.dot-g { background: #22c55e; }

.calc-widget-body { padding: 28px; box-sizing: border-box; width: 100%; overflow: hidden; }

/* Form elements */
.form-group { margin-bottom: 20px; }
.form-label {
  display: block; font-size: .88rem; font-weight: 600; margin-bottom: 7px;
  color: var(--text);
}
.form-hint { font-size: .8rem; color: var(--text-muted); margin-left: 6px; font-weight: 400; }
.form-input, .form-select {
  width: 100%; padding: 11px 16px;
  border: 1.5px solid var(--border); border-radius: var(--radius);
  font-family: var(--font-body); font-size: 1rem; color: var(--text);
  background: var(--bg); outline: none; transition: border-color .18s, box-shadow .18s;
}
.form-input:focus, .form-select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(29,78,216,.12);
}
.input-prefix-wrap { position: relative; }
.input-prefix {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  font-size: .95rem; color: var(--text-muted); pointer-events: none;
}
.input-prefix-wrap .form-input { padding-left: 28px; }

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* Result box */
.result-box {
  background: var(--result-bg); color: var(--result-text);
  border-radius: var(--radius); padding: 22px 24px; margin-top: 8px;
  display: none;
}
.result-box.show { display: block; animation: fadeSlideUp .25s ease; }
.result-label { font-size: .8rem; color: rgba(255,255,255,.5); text-transform: uppercase; letter-spacing: .08em; margin-bottom: 6px; }
.result-value { font-family: var(--font-mono); font-size: 2rem; font-weight: 700; color: #4ade80; }
.result-breakdown { margin-top: 14px; padding-top: 14px; border-top: 1px solid rgba(255,255,255,.1); }
.result-row { display: flex; justify-content: space-between; font-size: .9rem; padding: 3px 0; }
.result-row .rk { color: rgba(255,255,255,.55); }
.result-row .rv { font-family: var(--font-mono); font-weight: 700; color: rgba(255,255,255,.9); }

/* Button */
.btn-calc {
  width: 100%; padding: 14px; margin-top: 6px;
  background: var(--primary); color: #fff;
  border: none; border-radius: var(--radius); cursor: pointer;
  font-family: var(--font-head); font-size: 1rem; font-weight: 700; letter-spacing: .02em;
  transition: background .18s, transform .12s;
}
.btn-calc:hover  { background: var(--primary-dark); }
.btn-calc:active { transform: scale(.98); }

.btn-reset {
  background: none; border: 1.5px solid var(--border); color: var(--text-muted);
  border-radius: var(--radius); padding: 9px 18px; cursor: pointer;
  font-family: var(--font-body); font-size: .88rem; margin-top: 10px;
  transition: all .15s;
}
.btn-reset:hover { border-color: var(--text-muted); color: var(--text); }

/* ── Content sections ── */
.content-section { margin-bottom: 36px; }
.content-section h2 {
  font-family: var(--font-head); font-size: 1.25rem; font-weight: 700;
  margin-bottom: 12px; padding-bottom: 10px; border-bottom: 2px solid var(--border);
}
.content-section p { color: var(--text-muted); line-height: 1.8; margin-bottom: 12px; }
.formula-box {
  background: var(--result-bg); color: #e0f2fe;
  font-family: var(--font-mono); font-size: .88rem;
  padding: 16px 20px; border-radius: var(--radius); margin: 14px 0;
  border-left: 3px solid var(--accent);
  white-space: pre-wrap;
  word-break: break-word;
  overflow-x: auto;
  line-height: 1.7;
}

/* FAQ */
.faq-list { list-style: none; display: flex; flex-direction: column; gap: 12px; }
.faq-item {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
}
.faq-q {
  width: 100%; background: none; border: none; padding: 16px 20px;
  text-align: left; font-family: var(--font-body); font-size: .97rem; font-weight: 600;
  color: var(--text); cursor: pointer; display: flex; justify-content: space-between; align-items: center;
}
.faq-q::after { content: '+'; font-size: 1.3rem; color: var(--primary); transition: transform .2s; }
.faq-item.open .faq-q::after { transform: rotate(45deg); }
.faq-a { display: none; padding: 0 20px 16px; color: var(--text-muted); font-size: .93rem; line-height: 1.75; }
.faq-item.open .faq-a { display: block; }

/* Related calculators sidebar */
.sidebar-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 22px; margin-bottom: 20px;
}
.sidebar-card h3 { font-family: var(--font-head); font-size: 1rem; font-weight: 700; margin-bottom: 14px; }
.related-list { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.related-list a {
  display: flex; align-items: center; gap: 10px;
  color: var(--text); font-size: .9rem; padding: 8px 12px;
  border-radius: 8px; background: var(--bg); transition: all .15s;
}
.related-list a:hover { background: var(--primary-light); color: var(--primary); text-decoration: none; }
.related-list a .rel-icon { font-size: 1rem; flex-shrink: 0; }

/* ── Animations ── */
@keyframes fadeSlideUp {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Footer ── */
.site-footer {
  background: var(--result-bg); color: rgba(255,255,255,.65);
  padding: 52px 0 0; margin-top: 80px;
  /* PERF: Skip rendering cost for off-screen footer until user scrolls near it */
  content-visibility: auto;
  contain-intrinsic-size: 0 300px;
}
.footer-inner { display: grid; grid-template-columns: 260px 1fr; gap: 48px; margin-bottom: 40px; }
.footer-brand .logo { color: #fff; margin-bottom: 12px; display: flex; }
.footer-brand p { font-size: .88rem; color: rgba(255,255,255,.45); line-height: 1.6; }
.footer-links { display: grid; grid-template-columns: repeat(4, 1fr); gap: 24px; }
.footer-col h4, .footer-col-title { font-family: var(--font-head); font-size: .8rem; text-transform: uppercase; letter-spacing: .1em; color: rgba(255,255,255,.4); margin-bottom: 12px; }
.footer-col a { display: block; color: rgba(255,255,255,.65); font-size: .88rem; margin-bottom: 8px; transition: color .15s; }
.footer-col a:hover { color: #fff; text-decoration: none; }
.footer-bottom { border-top: 1px solid rgba(255,255,255,.08); padding: 18px 0; font-size: .82rem; color: rgba(255,255,255,.3); }


/* ═══════════════════════════════════════════
   RESPONSIVE BREAKPOINTS
   ═══════════════════════════════════════════ */

/* ── FIX #4: 1100px — Narrow sidebar before full collapse ── */
/* Prevents the main column getting cramped at mid-range viewport widths */
@media (max-width: 1100px) {
  .calc-page-inner { grid-template-columns: 1fr 240px; gap: 32px; }
}

/* ── 900px — Collapse sidebar + fix footer + enable table scroll ── */
/* FIX #5: Merged the two separate 900px blocks into one */
@media (max-width: 900px) {
  .calc-page-inner { grid-template-columns: 1fr; gap: 24px; }
  .footer-inner { grid-template-columns: 1fr; gap: 32px; }
  .footer-links { grid-template-columns: repeat(2, 1fr); }
  /* Table scrolling moved here so it applies to tablet as well as mobile */
  .content-section { overflow-x: hidden; }
  .content-section > table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
}

/* ── FIX #1 & #2: 768px — Hamburger nav (covers tablet gap 641–899px) ── */
/* Was previously only at ≤640px, leaving tablet nav broken */
@media (max-width: 768px) {
  /* MOBILE PERF: Remove GPU compositor layer — mobile devices have limited GPU memory.
     will-change:transform on the sticky header helps desktop but hurts low-end phones. */
  .site-header { will-change: auto; }

  .main-nav {
    display: none;
    flex-direction: column;
    position: fixed;
    top: 62px; left: 0; right: 0; bottom: 0;
    background: var(--result-bg);
    padding: 16px;
    border-top: 1px solid rgba(255,255,255,.1);
    overflow-y: auto;
    z-index: 150;
  }
  .main-nav.open { display: flex; }
  .nav-toggle { display: block; }
  /* FIX #11: Proper 44px touch targets in expanded mobile menu */
  .main-nav a {
    padding: 0 16px;
    font-size: 1rem;
    color: rgba(255,255,255,.8);
    border-radius: 8px;
    min-height: 44px;
    display: flex;
    align-items: center;
  }

  /* Also stack hero search at this breakpoint (was only at 640px, cramped at ~500–640px) */
  .hero-search { flex-direction: column; max-width: 360px; }
  .hero-search input,
  .hero-search button { width: 100%; }
}

/* ── 640px — Mobile layout ── */
@media (max-width: 640px) {
  /* Layout */
  .form-row { grid-template-columns: 1fr; }
  .category-grid { grid-template-columns: 1fr 1fr; gap: 12px; }

  /* Calculator widget */
  .calc-widget-body { padding: 16px; }
  .calc-widget-header { padding: 8px 16px; font-size: .78rem; }

  /* FIX #3: Use overflow-x: auto instead of hidden so result content can scroll, not clip */
  .result-box { overflow-x: auto; }
  .formula-box { font-size: .8rem; padding: 12px 14px; overflow-x: auto; }

  /* FIX #2 (table): Removed the `thead, tbody, tfoot { display: table }` override.
     Setting children back to display:table while the parent is display:block causes
     cross-browser rendering inconsistencies. display:block + overflow-x:auto is sufficient. */
  table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; width: 100%; }
  th, td { white-space: nowrap; }

  /* Result rows — stack */
  .result-row { flex-direction: column; gap: 2px; }
  .result-row .rk { font-size: .8rem; }
  .result-row .rv { text-align: left; max-width: 100%; font-size: .85rem; }

  /* Result value */
  .result-value { font-size: 1.4rem; word-break: break-all; }

  /* FIX #6: Replaced fragile inline-style attribute selectors with a proper class.
     Add class="tab-group" to tab button container elements in your HTML.
     The old selectors `div[style*="display:flex"][style*="gap:4px"]` break if the
     inline style string varies even slightly (spacing, order, quotes). */
  .tab-group { flex-wrap: wrap; }
  /* Catch inline-styled flex tab containers */
  .calc-widget-body > div { flex-wrap: wrap; }

  /* Inputs — prevent iOS zoom on focus */
  .form-input, .form-select { font-size: 16px; }

  /* Buttons */
  .btn-calc, .btn-reset { width: 100%; margin-right: 0; }

  /* Sidebar */
  .sidebar-card { margin-bottom: 12px; }

  /* Hero */
  .hero { padding: 40px 0 36px; }
  .hero h1 { font-size: clamp(1.6rem, 6vw, 2.4rem); }
  .hero p { font-size: .95rem; }

  /* Category cards */
  .category-card { padding: 20px 16px; }

  /* Content sections */
  .content-section h2 { font-size: 1.1rem; }
  .intro { font-size: .9rem; }

  /* Container padding — FIX: removed redundant max-width:100vw (global rule covers it) */
  .container { padding: 0 14px; }
}

/* ── Global overflow guard ── */
/* FIX #7: Removed max-width:100vw (causes scroll-jump with position:fixed elements).
   overflow-x:hidden on html/body is sufficient. */
html, body { overflow-x: hidden; }

/* Every layout element must stay within viewport */
.container, .calc-widget, .calc-widget-body,
.result-box, .content-section, aside,
.sidebar-card, .calc-header {
  max-width: 100%;
  box-sizing: border-box;
}

/* FIX #6: Removed the brittle .calc-widget-body > div[style*="display:flex"] selector.
   Add class="tab-group" to any flex tab container in HTML and use the rule above instead.
   If you cannot modify the HTML, use the broader: .calc-widget-body > div { flex-wrap: wrap; } */

/* ── Very small phones (< 380px) ── */
@media (max-width: 380px) {
  .category-grid { grid-template-columns: 1fr; }
  .calc-widget-body { padding: 12px; }
  .result-value { font-size: 1.2rem; }
  .btn-calc, .btn-reset { font-size: .85rem; padding: 11px 16px; }
  th, td { font-size: .78rem; padding: 6px 8px !important; }
  .formula-box { font-size: .75rem; }
  /* FIX #9: Scoped bare h1 selector to avoid conflicting with clamp()-based
     .hero h1 and .calc-header h1 rules (which have higher specificity anyway,
     but a bare h1 is confusing and can affect other page h1s unexpectedly). */
  .main-content > h1 { font-size: 1.5rem; }
}

/* ── FIX #8: Respect user's reduced-motion preference ── */
/* Disables fadeSlideUp, hover transforms, and all CSS transitions for users
   who have enabled "Reduce Motion" in their OS accessibility settings. */
@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;
  }
}
