/* ==========================================================================
   1. CSS Custom Properties (Theming)
   ========================================================================== */

:root {
  --bg-primary: #f5f7fa;
  --bg-surface: #ffffff;
  --bg-surface-hover: #f0f2f5;
  --bg-hero: linear-gradient(135deg, #1e3a5f 0%, #1a1a2e 100%);
  --text-primary: #1a1a2e;
  --text-secondary: #5f6368;
  --text-on-hero: #ffffff;
  --text-on-hero-muted: #7ba4cc;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-light: #eff6ff;
  --accent-surface: #dbeafe;
  --border: #e0e0e0;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
  --shadow-lg: 0 4px 24px rgba(0,0,0,0.15);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 28px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --transition: 0.2s ease;
}

[data-theme="dark"] {
  --bg-primary: #0f1219;
  --bg-surface: #1a1f2e;
  --bg-surface-hover: #242b3d;
  --bg-hero: linear-gradient(135deg, #0d1b2a 0%, #1b2838 100%);
  --text-primary: #e8eaed;
  --text-secondary: #9aa0a6;
  --text-on-hero: #e8eaed;
  --text-on-hero-muted: #5a8ab5;
  --accent: #4a9eff;
  --accent-hover: #2b7de9;
  --accent-light: #1a2744;
  --accent-surface: #1e3a5f;
  --border: #2d3548;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 4px 24px rgba(0,0,0,0.5);
}

/* ==========================================================================
   2. Base Styles
   ========================================================================== */

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

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text-primary);
  background-color: var(--bg-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  min-height: 100vh;
}

img, svg {
  display: block;
  max-width: 100%;
}

button, input, select, textarea {
  font-family: inherit;
  font-size: inherit;
}

/* ==========================================================================
   2b. Smooth theme transitions (respects reduced motion)
   ========================================================================== */

@media (prefers-reduced-motion: no-preference) {
  body,
  .top-bar,
  .hero,
  .category-pill,
  .converter-card,
  .group-summary-card,
  .compact-search,
  .search-result-row,
  .error-state {
    transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
  }
}

/* ==========================================================================
   3. Skip Link (a11y)
   ========================================================================== */

.skip-link {
  position: absolute;
  top: -100%;
  left: 16px;
  padding: 8px 16px;
  background: var(--accent);
  color: #ffffff;
  border-radius: var(--radius-sm);
  text-decoration: none;
  font-weight: 600;
  z-index: 10000;
  transition: top var(--transition);
}

.skip-link:focus {
  top: 16px;
}

/* ==========================================================================
   4. Top Bar
   ========================================================================== */

.top-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 56px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  z-index: 1000;
}

.top-bar-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.top-bar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* ==========================================================================
   5. Logo
   ========================================================================== */

.logo {
  font-size: 20px;
  font-weight: 700;
  text-decoration: none;
  color: var(--text-primary);
  letter-spacing: -0.5px;
}

.logo-accent {
  color: var(--accent);
}

/* ==========================================================================
   6. Toggle Buttons
   ========================================================================== */

.toggle-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background var(--transition), border-color var(--transition), color var(--transition);
  white-space: nowrap;
}

.toggle-btn:hover {
  background: var(--bg-surface-hover);
  border-color: var(--accent);
  color: var(--accent);
}

.toggle-label {
  pointer-events: none;
}

.theme-icon {
  font-size: 16px;
  line-height: 1;
}

.unit-count-badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  background: var(--accent-light);
  color: var(--accent);
  border-radius: var(--radius-lg);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.unit-count-badge:empty {
  display: none;
}

/* ==========================================================================
   7. Hero Section
   ========================================================================== */

.hero {
  background: var(--bg-hero);
  padding: 104px 24px 40px;
  /* 104px = 56px top-bar + 48px spacing */
  text-align: center;
}

@media (prefers-reduced-motion: no-preference) {
  .hero {
    transition: padding 0.3s ease, opacity 0.3s ease;
  }
}

.hero--hidden {
  display: none;
}

.hero-tagline {
  font-size: 32px;
  font-weight: 700;
  color: var(--text-on-hero);
  margin-bottom: 24px;
  letter-spacing: -0.5px;
}

/* ==========================================================================
   7b. Compact Search Bar (shown when hero is hidden)
   ========================================================================== */

.compact-search {
  display: none;
  position: fixed;
  top: 56px;
  left: 0;
  right: 0;
  z-index: 999;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 24px;
  box-shadow: var(--shadow-sm);
}

.compact-search--visible {
  display: block;
}

.compact-search-inner {
  max-width: 600px;
  margin: 0 auto;
}

.compact-search-form {
  display: flex;
  align-items: center;
  gap: 8px;
}

.compact-search-input {
  flex: 1;
  min-width: 0;
  height: 40px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  transition: border-color var(--transition);
}

.compact-search-input:focus {
  border-color: var(--accent);
}

.compact-search-input::placeholder {
  color: var(--text-secondary);
  opacity: 0.7;
}

.compact-search-submit {
  flex-shrink: 0;
  height: 40px;
  padding: 0 20px;
  background: var(--accent);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-lg);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition);
  white-space: nowrap;
}

.compact-search-submit:hover {
  background: var(--accent-hover);
}

/* ==========================================================================
   8. Search Bar
   ========================================================================== */

.search-form {
  max-width: 640px;
  margin: 0 auto;
}

.search-bar {
  display: flex;
  align-items: center;
  max-width: 640px;
  margin: 0 auto;
  background: var(--bg-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 4px 4px 4px 16px;
  transition: box-shadow var(--transition);
}

.search-bar:focus-within {
  box-shadow: var(--shadow-lg), 0 0 0 2px var(--accent);
}

.search-icon {
  font-size: 18px;
  color: var(--text-secondary);
  flex-shrink: 0;
  margin-right: 8px;
  line-height: 1;
}

.search-input {
  flex: 1;
  min-width: 0;
  border: none;
  outline: none;
  padding: 12px 8px;
  font-size: 16px;
  color: var(--text-primary);
  background: transparent;
}

.search-input::placeholder {
  color: var(--text-secondary);
  opacity: 0.7;
}

.search-submit {
  flex-shrink: 0;
  padding: 10px 24px;
  background: var(--accent);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-lg);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition);
  white-space: nowrap;
}

.search-submit:hover {
  background: var(--accent-hover);
}

/* ==========================================================================
   9. Example Queries
   ========================================================================== */

.example-queries {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
}

.example-label {
  font-size: 13px;
  color: var(--text-on-hero-muted);
  font-weight: 500;
}

.example-query {
  background: transparent;
  border: none;
  color: var(--text-on-hero-muted);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: color var(--transition), text-decoration var(--transition);
  text-decoration: none;
}

.example-query:hover {
  color: var(--text-on-hero);
  text-decoration: underline;
}

/* ==========================================================================
   10. Category Pills
   ========================================================================== */

.category-nav {
  padding: 16px 24px;
  max-width: 1200px;
  margin: 0 auto;
}

.category-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.category-pill {
  display: inline-flex;
  align-items: center;
  padding: 8px 16px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--transition), color var(--transition), border-color var(--transition);
  white-space: nowrap;
}

.category-pill:hover {
  background: var(--bg-surface-hover);
  border-color: var(--accent);
  color: var(--accent);
}

.category-pill.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

/* ==========================================================================
   10b. Group Summary Grid (shown when "All" is active)
   ========================================================================== */

.group-summary-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px 40px;
}

.group-summary-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  text-align: left;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 20px;
  cursor: pointer;
  transition: box-shadow var(--transition), border-color var(--transition), transform var(--transition);
}

.group-summary-card:hover {
  box-shadow: var(--shadow-md);
  border-color: var(--accent);
}

@media (prefers-reduced-motion: no-preference) {
  .group-summary-card:hover {
    transform: translateY(-2px);
  }
}

.group-summary-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 4px;
}

.group-summary-meta {
  font-size: 13px;
  font-weight: 500;
  color: var(--accent);
  margin-bottom: 8px;
}

.group-summary-cats {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.4;
}

/* ==========================================================================
   11. Converter Grid
   ========================================================================== */

.converter-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px 40px;
}

/* ==========================================================================
   12. Converter Cards
   ========================================================================== */

.converter-card {
  background: var(--bg-surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  padding: 20px;
  transition: box-shadow var(--transition);
}

.converter-card:hover {
  box-shadow: var(--shadow-md);
}

.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 4px;
}

.card-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.card-category-badge {
  display: inline-block;
  padding: 2px 8px;
  background: var(--accent-light);
  color: var(--accent);
  border-radius: var(--radius-sm);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.card-units-subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 16px;
}

.input-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.input-row input,
.input-row select {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  color: var(--text-primary);
  font-size: 14px;
  transition: border-color var(--transition);
}

.input-row input:focus,
.input-row select:focus {
  border-color: var(--accent);
  outline: none;
}

.to-row {
  margin-bottom: 12px;
}

.to-row select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  color: var(--text-primary);
  font-size: 14px;
  transition: border-color var(--transition);
}

.to-row select:focus {
  border-color: var(--accent);
  outline: none;
}

.result-display {
  background: var(--accent-light);
  border-radius: var(--radius-sm);
  padding: 16px;
  text-align: center;
  margin-bottom: 8px;
}

.result-value {
  font-size: 24px;
  font-weight: 700;
  color: var(--accent);
  word-break: break-all;
}

.result-unit {
  font-size: 16px;
  font-weight: 400;
  color: var(--accent);
  opacity: 0.8;
}

.swap-btn {
  display: block;
  margin: 0 auto;
  background: transparent;
  border: none;
  color: var(--accent);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  padding: 4px 12px;
  transition: color var(--transition);
}

.swap-btn:hover {
  color: var(--accent-hover);
}

/* ==========================================================================
   13. Search Result (Specific Target)
   ========================================================================== */

/* When compact search is visible, push content below the fixed bars */
.has-compact-search {
  padding-top: 120px;
  /* 56px top-bar + 56px compact-search + 8px gap */
}

.search-result-specific {
  max-width: 640px;
  margin: 24px auto;
  padding: 0 24px;
  text-align: center;
}

.search-result-subtitle {
  font-size: 16px;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.search-result-big {
  font-size: 48px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 16px;
  word-break: break-all;
}

.search-result-big .result-unit {
  font-size: 28px;
  font-weight: 400;
}

.search-result-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-bottom: 16px;
}

.search-result-actions .swap-btn,
.search-result-actions .copy-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg-surface);
  color: var(--accent);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}

.search-result-actions .swap-btn:hover,
.search-result-actions .copy-btn:hover {
  background: var(--bg-surface-hover);
  border-color: var(--accent);
}

.search-result-also {
  font-size: 14px;
  color: var(--text-secondary);
  margin-top: 16px;
}

/* ==========================================================================
   14. Search Result (All Conversions)
   ========================================================================== */

.search-result-list {
  max-width: 640px;
  margin: 16px auto;
  padding: 0 24px;
}

.search-result-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--bg-surface);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
  margin-bottom: 8px;
  transition: box-shadow var(--transition);
}

.search-result-row:hover {
  box-shadow: var(--shadow-md);
}

.search-result-row-value {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
}

.search-result-row-unit {
  font-size: 14px;
  color: var(--text-secondary);
  margin-left: 4px;
}

.search-result-row-name {
  font-size: 13px;
  color: var(--text-secondary);
  flex: 1;
  margin-left: 12px;
}

/* ==========================================================================
   15. Error State
   ========================================================================== */

.error-state {
  text-align: center;
  padding: 40px 24px;
  max-width: 480px;
  margin: 0 auto;
}

.error-message {
  font-size: 18px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 16px;
}

.error-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.error-suggestion-btn {
  padding: 8px 16px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--accent);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}

.error-suggestion-btn:hover {
  background: var(--accent-light);
  border-color: var(--accent);
}

/* ==========================================================================
   16. Swap Button (standalone)
   ========================================================================== */

/* Already defined in section 12 (.swap-btn) */

/* ==========================================================================
   17. Copy Button
   ========================================================================== */

.copy-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
  position: relative;
}

.copy-btn:hover {
  background: var(--bg-surface-hover);
  color: var(--accent);
}

.copy-btn .tooltip {
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  padding: 4px 10px;
  background: var(--text-primary);
  color: var(--bg-surface);
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity var(--transition);
}

.copy-btn .tooltip.show {
  opacity: 1;
}

/* ==========================================================================
   18. Result Value (general)
   ========================================================================== */

/* Already defined in section 12 (.result-value, .result-unit) */

/* ==========================================================================
   19. Focus Indicators (a11y)
   ========================================================================== */

a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ==========================================================================
   20. Responsive
   ========================================================================== */

@media (max-width: 768px) {
  .category-pills {
    flex-wrap: nowrap;
    overflow-x: auto;
    justify-content: flex-start;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .category-pills::-webkit-scrollbar {
    display: none;
  }

  /* Ensure touch targets are at least 44px */
  .category-pill {
    min-height: 44px;
    padding: 10px 16px;
  }

  .converter-grid {
    grid-template-columns: 1fr;
  }

  .group-summary-grid {
    grid-template-columns: 1fr;
  }

  .group-summary-card {
    min-height: 44px;
  }

  .hero {
    padding: 88px 16px 24px;
    /* 88px = 56px top-bar + 32px spacing */
  }

  .search-bar {
    max-width: 100%;
  }

  .hero-tagline {
    font-size: 20px;
  }

  .search-result-big {
    font-size: 32px;
  }

  .search-result-big .result-unit {
    font-size: 20px;
  }

  .top-bar-content {
    padding: 0 16px;
  }

  .toggle-label {
    display: none;
  }

  .toggle-btn {
    padding: 6px 10px;
    min-height: 44px;
  }

  /* Compact search mobile */
  .compact-search {
    padding: 8px 16px;
  }

  .compact-search-input {
    font-size: 16px; /* Prevents iOS zoom on focus */
  }

  /* Search result spacing for mobile */
  .search-result-specific {
    padding: 0 16px;
  }

  .search-result-list {
    padding: 0 16px;
  }

  .error-state {
    padding: 40px 16px;
  }
}

@media (min-width: 768px) and (max-width: 1024px) {
  .converter-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .group-summary-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ==========================================================================
   21. Reduced Motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}

/* ==========================================================================
   22. Scrollbar Styling (Dark Mode)
   ========================================================================== */

[data-theme="dark"] {
  scrollbar-color: #2d3548 #0f1219;
}

[data-theme="dark"] ::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

[data-theme="dark"] ::-webkit-scrollbar-track {
  background: #0f1219;
}

[data-theme="dark"] ::-webkit-scrollbar-thumb {
  background: #2d3548;
  border-radius: 4px;
}

[data-theme="dark"] ::-webkit-scrollbar-thumb:hover {
  background: #3d4a62;
}

/* ==========================================================================
   23. Category Nav Hidden State
   ========================================================================== */

.category-nav--hidden {
  display: none;
}

/* ==========================================================================
   24. Popular Section (Landing Page)
   ========================================================================== */

.popular-section {
  text-align: center;
  padding: 24px 16px 32px;
  max-width: 640px;
  margin: 0 auto;
}

.popular-label {
  font-size: 11px;
  color: var(--text-secondary);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
}

.popular-links {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.popular-link {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--text-primary);
  cursor: pointer;
  font-family: var(--font);
  transition: border-color var(--transition), background var(--transition);
}

.popular-link:hover {
  border-color: var(--accent);
  background: var(--accent-light);
}

.browse-link {
  display: inline-block;
  margin-top: 20px;
  color: var(--accent);
  font-size: 13px;
  text-decoration: none;
  cursor: pointer;
  border: none;
  background: none;
  font-family: var(--font);
  padding: 4px 8px;
}

.browse-link:hover {
  text-decoration: underline;
}

/* ==========================================================================
   25. Browse Top Link (Top Bar)
   ========================================================================== */

.browse-top-link {
  display: inline-flex;
  align-items: center;
  padding: 6px 14px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  font-family: var(--font);
  transition: background var(--transition), border-color var(--transition), color var(--transition);
  white-space: nowrap;
}

.browse-top-link:hover {
  background: var(--bg-surface-hover);
  border-color: var(--accent);
  color: var(--accent);
}

/* ==========================================================================
   26. Single Converter Grid (from popular link)
   ========================================================================== */

.converter-grid--single {
  max-width: 480px;
  grid-template-columns: 1fr;
  margin: 0 auto;
  padding: 16px 24px 40px;
}

@media (max-width: 768px) {
  .browse-top-link .browse-label {
    display: none;
  }

  .popular-link {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
  }
}
