/* ==========================================================================
   TOPICS FORM PAGE — 3-step wizard (mobile-first)
   ========================================================================== */

body:has(.topics-page) {
  background: var(--sl-cream);
  overflow-anchor: none;
}

.topics-page {
  font-family: var(--font-sans);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem 1.25rem 0;
  position: relative;
  /* clip, not hidden: hidden creates a scroll container and breaks the
     sticky footer CTA */
  overflow: clip;
}

/* Decorative blob — calm, behind everything */
.topics-page::before {
  content: '';
  position: absolute;
  top: -35%;
  right: -25%;
  width: 70%;
  height: 130%;
  background: var(--sl-teal-light);
  border-radius: 50% 30% 40% 50%;
  z-index: 0;
  opacity: 0.4;
  pointer-events: none;
}

.topics-form {
  width: 100%;
  max-width: 640px;
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  flex: 1;
}

/* ============================================
   TOP BAR — back chevron, progress
   ============================================ */

.tw-topbar {
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  gap: 0.75rem;
  padding: 0.25rem 0 1.25rem;
}

.tw-back {
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--sl-charcoal);
  border-radius: var(--radius-full);
  transition: background var(--transition-fast);
}

.tw-back i {
  font-size: 1.25rem;
}

.tw-back:hover {
  background: rgba(1, 129, 141, 0.08);
}

/* 3-segment progress bar — pinned to the middle column so it stays
   centered when the side cells are empty */
.tw-progress {
  grid-column: 2;
  display: flex;
  gap: 6px;
  max-width: 220px;
  width: 100%;
  margin: 0 auto;
}

.tw-progress-seg {
  flex: 1;
  height: 4px;
  border-radius: var(--radius-full);
  background: rgba(1, 129, 141, 0.15);
  transition: background var(--transition-normal);
}

.tw-progress-seg--filled {
  background: var(--sl-teal);
}

/* ============================================
   STEP PANELS
   ============================================ */

.tw-step {
  animation: tw-step-in 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes tw-step-in {
  from { opacity: 0; transform: translateX(16px); }
  to { opacity: 1; transform: translateX(0); }
}

.topics-headline {
  font-family: var(--font-serif);
  font-size: clamp(1.75rem, 4vw, 2.375rem);
  line-height: 1.2;
  color: var(--sl-charcoal);
  margin-bottom: 0.5rem;
  font-weight: 400;
  letter-spacing: -0.02em;
  max-width: 18ch;
  outline: none;
}

/* Signature: the user's own topic echoed back under the headline */
.tw-topic-echo {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 1.125rem;
  color: var(--sl-teal);
  margin-bottom: 0.375rem;
  overflow-wrap: anywhere;
}

.tw-subcopy {
  font-size: 0.9375rem;
  color: var(--sl-gray);
  margin-bottom: var(--space-md);
  line-height: 1.5;
}

/* ============================================
   WHITE CARD
   ============================================ */

.topics-card {
  background: white;
  border-radius: var(--radius-lg);
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.06), 0 1px 3px rgba(0, 0, 0, 0.04);
  padding: clamp(1.25rem, 4vw, 2rem);
  margin-top: var(--space-sm);
}

/* ============================================
   STEP 1 — topic textarea + popular chips
   ============================================ */

.tw-topic-input {
  width: 100%;
  border: none;
  border-bottom: 2px solid rgba(1, 129, 141, 0.18);
  border-radius: 0;
  background: transparent;
  font-family: var(--font-sans);
  font-size: 1.125rem;
  font-weight: 500;
  color: var(--sl-charcoal);
  line-height: 1.5;
  padding: 0.25rem 0.125rem 0.25rem;
  resize: none;
  overflow: hidden;
  outline: none;
  transition: border-color 0.25s ease;
}

.tw-topic-input::placeholder {
  color: var(--sl-gray);
  opacity: 0.5;
  font-style: italic;
  font-weight: 400;
}

.tw-topic-input:focus {
  border-color: var(--sl-teal);
}

.tw-popular-label {
  margin: var(--space-md) 0 0.625rem;
  font-size: 0.75rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-weight: 600;
  color: var(--sl-teal);
}

/* Chips: wrapped rows, all visible — no horizontal scrolling */
.tw-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4375rem;
}

.tw-chip {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 0.375rem 0.8125rem;
  background: var(--sl-teal-light);
  color: var(--sl-teal-dark);
  border: 1px solid transparent;
  border-radius: var(--radius-full);
  font-family: var(--font-sans);
  font-size: 0.8125rem;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  transition: border-color var(--transition-fast), background var(--transition-fast);
}

.tw-chip:hover {
  border-color: var(--sl-teal);
}

.tw-chip:active {
  background: rgba(1, 129, 141, 0.18);
}

/* ============================================
   STEPS 2-3 — radio option cards
   (visual pattern shared with .exp-statement-card)
   ============================================ */

.tw-options {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
}

.tw-option-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  min-height: 44px;
  padding: 0.875rem 1rem;
  background: white;
  border: 1.5px solid rgba(0, 0, 0, 0.08);
  border-radius: var(--radius-md);
  cursor: pointer;
  position: relative;
  margin-bottom: 0;
  transition: border-color var(--transition-fast), background var(--transition-fast),
              box-shadow var(--transition-fast);
  user-select: none;
}

.tw-option-card:hover {
  border-color: var(--sl-teal);
  box-shadow: 0 2px 8px rgba(1, 129, 141, 0.1);
}

.tw-option-card:has(input:checked) {
  background: var(--sl-teal-light);
  border-color: var(--sl-teal);
  border-width: 2px;
}

.tw-option-card:has(input:focus-visible) {
  outline: 2px solid var(--sl-teal);
  outline-offset: 3px;
}

.tw-option-text {
  font-size: 0.9375rem;
  color: var(--sl-charcoal);
  line-height: 1.5;
  flex: 1;
}

.tw-option-check {
  color: var(--sl-teal);
  font-size: 1rem;
  flex-shrink: 0;
  opacity: 0;
  transform: scale(0.6);
  transition: opacity var(--transition-fast), transform var(--transition-fast);
}

.tw-option-card:has(input:checked) .tw-option-check {
  opacity: 1;
  transform: scale(1);
}

/* Hidden radio — covers the whole card so Capybara can always click it */
.tw-option-radio {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0.001;
  cursor: pointer;
  z-index: 2;
  margin: 0;
}

/* "Something else" free-text reveal */
.tw-other-wrap {
  padding: 0.25rem 0.25rem 0;
}

.tw-other-input {
  width: 100%;
  border: none;
  border-bottom: 2px solid rgba(1, 129, 141, 0.18);
  border-radius: 0;
  background: transparent;
  font-family: var(--font-sans);
  font-size: 1rem;
  color: var(--sl-charcoal);
  line-height: 1.5;
  padding: 0.25rem 0.125rem 0.25rem;
  resize: none;
  outline: none;
  transition: border-color 0.25s ease;
}

.tw-other-input::placeholder {
  color: var(--sl-gray);
  opacity: 0.5;
  font-style: italic;
}

.tw-other-input:focus {
  border-color: var(--sl-teal);
}

/* ============================================
   STICKY FOOTER CTA
   ============================================ */

.tw-footer {
  position: sticky;
  bottom: 0;
  margin-top: auto;
  padding: var(--space-sm) 0 calc(1rem + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--sl-cream) 70%, transparent);
}

/* Native shells lay the WebView out above the system/tab bars and pad for
   them natively, but Android 15 edge-to-edge still reports a non-zero
   env(safe-area-inset-bottom) — adding it here double-compensates. */
body[data-turbo-native="true"] .tw-footer {
  padding-bottom: 1rem;
}

.tw-footer-row {
  display: flex;
  align-items: stretch;
  gap: 0.625rem;
}

.tw-footer-row .topics-submit {
  flex: 1;
}

.tw-footer-back {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  min-width: 44px;
  min-height: 44px;
  padding: 0.9375rem 1.25rem;
  background: white;
  color: var(--sl-charcoal);
  font-size: 1rem;
  font-weight: 500;
  font-family: var(--font-sans);
  border: 1.5px solid rgba(0, 0, 0, 0.12);
  border-radius: var(--radius-full);
  cursor: pointer;
  flex-shrink: 0;
  transition: border-color var(--transition-fast), background var(--transition-fast);
}

.tw-footer-back:hover {
  border-color: var(--sl-teal);
  color: var(--sl-teal);
}

.topics-submit {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.9375rem 2rem;
  background: linear-gradient(135deg, var(--sl-coral) 0%, var(--sl-coral-dark) 100%);
  color: white;
  font-size: 1rem;
  font-weight: 600;
  font-family: var(--font-sans);
  border: none;
  border-radius: var(--radius-full);
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(224, 122, 95, 0.3);
  transition: all var(--transition-normal);
  position: relative;
  overflow: hidden;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.topics-submit::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, rgba(255,255,255,0.2) 0%, transparent 50%);
  pointer-events: none;
}

.topics-submit:hover:not(:disabled) {
  background: linear-gradient(135deg, var(--sl-coral-dark) 0%, #B55D47 100%);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(224, 122, 95, 0.4);
}

.topics-submit:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
  box-shadow: 0 2px 8px rgba(224, 122, 95, 0.15);
}

.topics-submit-hint {
  margin-top: var(--space-xs);
  margin-bottom: 0;
  font-size: 0.8125rem;
  color: var(--sl-gray);
  text-align: center;
  line-height: 1.5;
  opacity: 0.8;
}

/* ============================================
   DESKTOP
   ============================================ */

@media (min-width: 641px) {
  .topics-page {
    padding-top: clamp(1.5rem, 4vw, 3rem);
  }

  .topics-headline,
  .tw-topic-echo,
  .tw-subcopy {
    text-align: center;
    margin-left: auto;
    margin-right: auto;
  }

  /* Roomier chips — vertical space is not scarce on desktop */
  .tw-chips {
    gap: 0.5rem;
  }

  .tw-chip {
    min-height: 44px;
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
  }

  /* Footer stays sticky on desktop too — the CTA must never sit
     below the fold. */
  .tw-footer {
    padding-bottom: var(--space-lg);
  }
}

/* Narrow phones: tighter card + smaller chips so pairs of chips
   still share a row and the list stays 4 rows tall */
@media (max-width: 380px) {
  .topics-card {
    padding-left: 0.875rem;
    padding-right: 0.875rem;
  }

  .tw-chip {
    min-height: 32px;
    padding: 0.3125rem 0.6875rem;
    font-size: 0.75rem;
  }
}

/* ============================================
   SHORT VIEWPORTS
   Tighten vertical rhythm so all chips clear
   the sticky CTA without scrolling.
   ============================================ */

@media (max-height: 700px) {
  .topics-page {
    padding-top: 0.5rem;
  }

  .tw-topbar {
    padding-bottom: 0.625rem;
  }

  .topics-headline {
    font-size: clamp(1.375rem, 3.5vw, 2.375rem);
    margin-bottom: 0.25rem;
  }

  .tw-subcopy {
    margin-bottom: 0.75rem;
  }

  .topics-card {
    padding-top: 1rem;
    padding-bottom: 1rem;
    margin-top: 0;
  }

  .tw-popular-label {
    margin-top: 0.875rem;
  }
}

/* ============================================
   ACCESSIBILITY
   ============================================ */

.topics-page *:focus-visible {
  outline: 2px solid var(--sl-teal);
  outline-offset: 3px;
}

.topics-page .tw-topic-input:focus-visible,
.topics-page .tw-other-input:focus-visible,
.topics-page .tw-option-radio:focus-visible,
.topics-page .topics-headline:focus-visible {
  /* headline focus is programmatic (step change, for screen readers) —
     no visible ring on a non-interactive element */
  outline: none;
}

/* ============================================
   REDUCED MOTION
   ============================================ */

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

  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
