/* ============================================================
   MOBILE PATCH — load AFTER styles.css
   Phone-first overrides + new mobile components
   Drop-in: <link rel="stylesheet" href="mobile-patch.css"> after styles.css
   ============================================================ */

/* ---------- Tokens specific to mobile chrome ---------- */
:root {
  --mbn-h: 64px;
  --mbn-h-safe: calc(64px + env(safe-area-inset-bottom, 0px));
  --sticky-cta-h: 72px;
  --sticky-cta-h-safe: calc(72px + env(safe-area-inset-bottom, 0px));
  --tap-min: 48px;
  --mobile-radius: 14px;
}

/* iOS safe-area for the whole shell */
@supports (padding: env(safe-area-inset-top)) {
  body {
    padding-top: env(safe-area-inset-top);
  }
}

/* ============================================================
   GLOBAL — phone width (≤768px)
   ============================================================ */
@media (max-width: 768px) {
  html, body { font-size: 15.5px; line-height: 1.65; }

  /* Loosen tap behavior */
  button { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }

  /* Shell padding */
  .app-shell,
  .quiz-shell,
  .result-shell,
  .weak-page,
  .stats-page,
  .pricing-page,
  .account-page {
    padding-left: 16px !important;
    padding-right: 16px !important;
  }

  /* Reserve room for sticky bottom nav on home-ish pages */
  .app-shell,
  .quiz-shell,
  .result-shell,
  .weak-page,
  .stats-page {
    padding-bottom: calc(28px + var(--mbn-h-safe)) !important;
  }

  /* Tighter App Header */
  .app-header-inner {
    padding: 8px 16px !important;
    gap: 10px !important;
  }
  .app-logo-mark { font-size: 17px; padding: 5px 8px; }
  .app-logo-name { font-size: 15px; }

  /* Wrapper that Home.jsx hard-codes */
  .app-shell-wrapper {
    padding: 12px 0 !important;
  }
}

/* ============================================================
   HOME — hero, banners, action row, dashboard, categories
   ============================================================ */
@media (max-width: 768px) {
  /* Hero */
  .hero {
    padding: 14px 0 14px !important;
    margin-bottom: 22px !important;
  }
  .hero-eyebrow {
    font-size: 10.5px !important;
    letter-spacing: 0.16em !important;
    margin-bottom: 10px !important;
  }
  .hero-title {
    font-size: 30px !important;
    line-height: 1.2 !important;
    margin-bottom: 12px !important;
    letter-spacing: 0.005em !important;
  }
  /* Drop the inline <br/> that home/builder copy uses — let it wrap natively */
  .hero-lead br { display: none; }
  .hero-lead {
    font-size: 14.5px !important;
    text-wrap: pretty;
  }

  /* "信頼性アピール" inline-styled banner — reformat to a 2x2 chip grid */
  .app-shell-wrapper > .app-shell > div[style*="信頼性"], /* no-op selector; visual chips block uses inline style */
  .app-shell .hero + div[style*="flex-wrap"] {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
    padding: 12px !important;
    margin-bottom: 18px !important;
    border-radius: 12px !important;
  }
  .app-shell .hero + div[style*="flex-wrap"] > div {
    font-size: 12px !important;
    gap: 6px !important;
  }
  .app-shell .hero + div[style*="flex-wrap"] > div > span:first-child {
    font-size: 16px !important;
  }

  /* Action row: 2-cols, bigger touch target, denser type */
  .home-action-row {
    grid-template-columns: 1fr 1fr !important;
    gap: 10px !important;
    margin: 20px 0 28px !important;
  }
  .action-card {
    padding: 14px 14px 14px !important;
    border-radius: 12px !important;
    min-height: 96px;
  }
  .action-icon { font-size: 20px !important; }
  .action-name { font-size: 14.5px !important; letter-spacing: 0 !important; }
  .action-sub { font-size: 11.5px !important; }

  /* Dashboard cards — force 2 cols on mobile, denser */
  .dashboard-row {
    grid-template-columns: 1fr 1fr !important;
    gap: 10px !important;
  }
  .dashboard-row .dashboard-card {
    padding: 14px !important;
    border-radius: 12px !important;
  }
  .dashboard-row .dashboard-card > div:first-child { font-size: 22px !important; margin-bottom: 0 !important; }
  .dashboard-row .dashboard-card > div:nth-child(2) { font-size: 26px !important; }
  .dashboard-row .dashboard-card > div:nth-child(3) { font-size: 11px !important; }

  /* Section titles */
  .section-title {
    font-size: 19px !important;
    gap: 10px;
    margin-top: 6px;
  }
  .section-title .num { font-size: 11px !important; }
  .section-sub {
    font-size: 13px !important;
    margin-bottom: 14px !important;
    line-height: 1.6;
    text-wrap: pretty;
  }

  /* Category cards — denser */
  .cat-grid {
    gap: 10px !important;
    margin-bottom: 32px !important;
  }
  .cat-card {
    padding: 14px 16px 12px !important;
    border-radius: 12px !important;
  }
  .cat-card .cat-num { font-size: 10.5px !important; margin-bottom: 6px !important; }
  .cat-card .cat-name { font-size: 18px !important; margin-bottom: 4px !important; line-height: 1.3; }
  .cat-card .cat-desc { font-size: 12.5px !important; margin-bottom: 12px !important; line-height: 1.55; }
  .cat-card .cat-meta { padding-top: 10px !important; font-size: 11px !important; }

  /* Custom-quiz banner */
  .custom-banner { padding: 16px 18px !important; }
  .custom-banner-title { font-size: 20px !important; }
  .custom-banner-eyebrow { font-size: 10px !important; }
  .custom-banner-sub { font-size: 12px !important; }
  .custom-banner-arrow { font-size: 28px !important; }
}

/* ============================================================
   QUIZ — header, q-card, choices, sticky bottom CTA, accordion exp
   ============================================================ */
@media (max-width: 768px) {
  /* Quiz header: stack onto two visual rows, no awkward wrap */
  .quiz-header {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 10px !important;
    padding: 12px 0 14px !important;
    margin-bottom: 14px !important;
  }
  .quiz-meta { gap: 8px !important; flex-wrap: nowrap; min-width: 0; }
  .quiz-meta > button:first-child {
    padding: 8px 10px !important;
    font-size: 12px !important;
    flex-shrink: 0;
  }
  .quiz-cat {
    font-size: 14px !important;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .quiz-cat .tag { font-size: 10px !important; margin-right: 6px !important; letter-spacing: 0.12em !important; }
  .quiz-progress {
    width: 100%;
    gap: 10px !important;
    font-size: 11.5px !important;
  }
  .quiz-progress-bar {
    width: 100% !important;
    flex: 1;
    height: 4px !important;
  }
  .quiz-progress > span { flex-shrink: 0; }

  /* Q card padding */
  .q-card {
    padding: 18px 16px 16px !important;
    border-radius: 14px !important;
    /* Reserve room for sticky CTA so content isn't covered when scrolled */
    margin-bottom: 18px;
  }
  .q-num-label { font-size: 10.5px !important; margin-bottom: 10px !important; }

  /* Prompt + passage */
  .q-prompt { font-size: 16px !important; line-height: 1.85 !important; margin-bottom: 16px !important; }
  .q-passage {
    padding: 14px 16px !important;
    font-size: 14px !important;
    line-height: 1.8 !important;
    margin-bottom: 14px !important;
  }

  /* Choices — bigger tap target, slightly less padding inside */
  .choices { gap: 8px !important; }
  .choice {
    padding: 12px 14px !important;
    font-size: 14.5px !important;
    line-height: 1.6;
    min-height: var(--tap-min);
    align-items: center;
    border-radius: 10px !important;
  }
  .choice .ch-letter {
    width: 28px; height: 28px;
    font-size: 13px;
  }

  /* Toolbar — keep tool buttons (canvas / kanji / furigana), HIDE confirm/next (moved to sticky CTA) */
  .q-toolbar {
    padding-top: 14px !important;
    margin-top: 14px !important;
    gap: 8px !important;
    align-items: center;
  }
  /* primary action buttons in toolbar are hidden on mobile — sticky bar replaces */
  .q-toolbar .btn.btn-primary,
  .q-toolbar .btn.btn-accent {
    display: none !important;
  }
  .q-toolbar .btn.btn-sm {
    padding: 10px 12px !important;
    font-size: 12.5px !important;
    border-radius: 999px;
    min-height: 40px;
  }
  .q-toolbar .toolbar-spacer { display: none; }
  .q-toolbar .furigana-toggle { margin-left: auto; }

  /* Furigana toggle slightly larger */
  .toggle-switch { width: 36px; height: 22px; }
  .toggle-switch::after { width: 18px; height: 18px; }
  .toggle-switch.on::after { transform: translateX(14px); }

  /* Explanation — accordion behavior */
  .explanation { border-radius: 12px !important; margin-top: 18px !important; }
  .exp-tabs {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .exp-tabs::-webkit-scrollbar { display: none; }
  .exp-tab {
    flex: none !important;
    min-width: 33%;
    padding: 10px 14px !important;
    white-space: nowrap;
  }
  .exp-tab .exp-tab-jp { font-size: 13px !important; }
  .exp-tab .exp-tab-tag { font-size: 8px !important; }
  .exp-body {
    padding: 16px 18px !important;
    font-size: 14px !important;
    line-height: 1.85 !important;
  }
  .exp-body .formula { padding: 8px 12px; font-size: 13px; }
  .exp-body .step { gap: 10px; margin-bottom: 12px; }
  .exp-body .step-num { width: 22px; height: 22px; font-size: 12px; }
  .exp-body .trick-box { padding: 12px 14px; }
  .exp-body .trick-title { font-size: 14px; }

  /* When sticky CTA is visible we add extra room so the content isn't hidden */
  body.has-sticky-cta .quiz-shell {
    padding-bottom: calc(28px + var(--sticky-cta-h-safe)) !important;
  }

  /* Inline canvas-collapsible — REPLACED by bottom sheet on mobile */
  .canvas-collapsible { display: none !important; }
}

/* ============================================================
   STICKY QUIZ CTA — fixed bottom bar with 解答する / 次の問題
   ============================================================ */
.sticky-quiz-cta {
  display: none;
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  z-index: 80;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));
  background: color-mix(in srgb, var(--paper) 94%, transparent);
  -webkit-backdrop-filter: saturate(140%) blur(12px);
  backdrop-filter: saturate(140%) blur(12px);
  border-top: 1px solid var(--paper-line);
  box-shadow: 0 -6px 20px -10px rgba(29,26,22,0.15);
}
.sticky-quiz-cta-inner {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  gap: 10px;
  align-items: center;
}
.sticky-quiz-cta .btn {
  flex: 1;
  min-height: var(--tap-min);
  padding: 14px 18px;
  font-size: 15px;
  font-weight: 600;
  border-radius: 12px;
  letter-spacing: 0.04em;
  justify-content: center;
}
.sticky-quiz-cta .btn-icon {
  flex: none;
  width: var(--tap-min);
  min-height: var(--tap-min);
  padding: 0;
  border-radius: 12px;
  font-size: 18px;
  background: var(--card-bg);
  border: 1px solid var(--paper-line);
  color: var(--ink-2);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.sticky-quiz-cta .btn-icon.active {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}
.sticky-quiz-cta .progress-mini {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-soft);
  white-space: nowrap;
}

@media (max-width: 768px) {
  .sticky-quiz-cta { display: block; }
}

/* ============================================================
   MOBILE BOTTOM NAV
   ============================================================ */
.mobile-bottom-nav {
  display: none;
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  z-index: 70;
  padding: 6px 8px env(safe-area-inset-bottom, 0px);
  background: color-mix(in srgb, var(--paper) 96%, transparent);
  -webkit-backdrop-filter: saturate(160%) blur(14px);
  backdrop-filter: saturate(160%) blur(14px);
  border-top: 1px solid var(--paper-line);
}
.mobile-bottom-nav-inner {
  max-width: 560px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
}
.mbn-item {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 8px 4px 6px;
  background: none;
  border: none;
  color: var(--ink-soft);
  font-family: var(--sans);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  border-radius: 10px;
  min-height: 54px;
  transition: color 0.15s ease, background 0.15s ease;
  cursor: pointer;
}
.mbn-item:active { background: var(--paper-2); }
.mbn-item.active {
  color: var(--accent);
}
.mbn-item.active .mbn-icon {
  color: var(--accent);
}
.mbn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 22px;
  color: var(--ink-2);
  transition: color 0.15s ease, transform 0.15s ease;
}
.mbn-item.active .mbn-icon { transform: translateY(-1px); }
.mbn-label { line-height: 1; }
.mbn-badge {
  position: absolute;
  top: 4px;
  right: calc(50% - 18px);
  background: var(--accent);
  color: #fff;
  font-family: var(--mono);
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0;
  padding: 1px 5px;
  border-radius: 999px;
  line-height: 1.4;
  border: 1.5px solid var(--paper);
  min-width: 16px;
  text-align: center;
}

/* Hide bottom nav on quiz / mock / result via body class */
body.bottom-nav-hidden .mobile-bottom-nav { display: none !important; }

@media (max-width: 768px) {
  .mobile-bottom-nav { display: block; }
}

/* ============================================================
   BOTTOM SHEET
   ============================================================ */
.bottom-sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(29,26,22,0.45);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  animation: bsFadeIn 0.18s ease;
}
@keyframes bsFadeIn { from { opacity: 0; } to { opacity: 1; } }
.bottom-sheet {
  width: 100%;
  max-width: 720px;
  background: var(--paper);
  border-top-left-radius: 18px;
  border-top-right-radius: 18px;
  box-shadow: 0 -12px 40px rgba(29,26,22,0.25);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: bsSlideUp 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
@keyframes bsSlideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
.bottom-sheet-handle {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 22px;
  padding-top: 8px;
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.bottom-sheet-handle:active { cursor: grabbing; }
.bottom-sheet-grip {
  width: 40px;
  height: 4px;
  border-radius: 999px;
  background: var(--paper-line);
}
.bottom-sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 18px 12px;
  border-bottom: 1px solid var(--paper-line);
  font-family: var(--serif);
  font-weight: 700;
  font-size: 16px;
  color: var(--ink);
}
.bottom-sheet-close {
  width: 32px; height: 32px;
  border-radius: 50%;
  border: 1px solid var(--paper-line);
  background: var(--card-bg);
  color: var(--ink-2);
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.bottom-sheet-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 14px 16px 22px;
}
.bottom-sheet-body .canvas-pad {
  position: static;
  max-height: none;
  border: none;
  box-shadow: none;
  background: transparent;
}
.bottom-sheet-body .canvas-wrap {
  min-height: 340px;
  border-radius: 12px;
  border: 1px solid var(--paper-line);
}

/* ============================================================
   PULL-TO-REFRESH
   ============================================================ */
.ptr-host {
  position: relative;
  /* allow translate of contents on mobile only */
}
.ptr-indicator {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%) translateY(-100%);
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--card-bg);
  border: 1px solid var(--paper-line);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
  z-index: 5;
  pointer-events: none;
  transition: transform 0.15s ease, opacity 0.15s ease;
  opacity: 0;
}
.ptr-indicator.visible { opacity: 1; }
.ptr-indicator .ptr-arrow {
  font-size: 18px;
  color: var(--accent);
  transition: transform 0.2s ease;
}
.ptr-indicator.armed .ptr-arrow { transform: rotate(180deg); }
.ptr-indicator.refreshing .ptr-spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--paper-line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: ptrSpin 0.7s linear infinite;
}
@keyframes ptrSpin { to { transform: rotate(360deg); } }

/* ============================================================
   CUSTOM QUIZ BUILDER — mobile
   ============================================================ */
@media (max-width: 768px) {
  .cb-hero { margin: 8px 0 22px !important; }
  .cb-section { margin-bottom: 24px !important; }
  .cb-section-head {
    margin-bottom: 4px !important;
    gap: 8px !important;
  }
  .cb-section-head .btn.btn-sm {
    padding: 8px 12px !important;
    min-height: 36px;
  }

  .cb-list { gap: 6px !important; }
  .cb-row {
    padding: 12px 12px !important;
    gap: 10px !important;
    border-radius: 10px !important;
    min-height: var(--tap-min);
  }
  .cb-box { width: 20px; height: 20px; }
  .cb-name { font-size: 14.5px !important; }
  .cb-desc { font-size: 11.5px !important; line-height: 1.5 !important; }
  .cb-meta { gap: 0 !important; }
  .cb-count { font-size: 11px !important; }
  .cb-rate { font-size: 10px !important; }

  /* Count picker — 3 columns even on tiny screens with smaller numbers */
  .cb-count-row {
    grid-template-columns: 1fr 1fr 1fr !important;
    gap: 8px !important;
  }
  .cb-count-btn {
    padding: 14px 6px !important;
    border-radius: 12px !important;
  }
  .cb-count-num { font-size: 28px !important; }
  .cb-count-unit { font-size: 12px !important; }
  .cb-count-time { font-size: 10px !important; }

  /* Footer summary + start CTA — stack as compact card on mobile */
  .cb-footer {
    padding: 16px !important;
    gap: 12px !important;
    flex-direction: column !important;
    align-items: stretch !important;
    margin-top: 18px !important;
  }
  .cb-summary { width: 100%; }
  .cb-summary-line { font-size: 12.5px !important; }
  .cb-summary-label { min-width: 72px !important; }
  .cb-footer .btn.btn-lg {
    width: 100%;
    padding: 14px 20px !important;
    min-height: var(--tap-min);
    border-radius: 12px;
    justify-content: center;
  }

  /* Custom-builder topbar back button */
  .topbar { padding: 14px 0 10px !important; margin-bottom: 18px !important; }
}

/* ============================================================
   RESULT — mobile
   ============================================================ */
@media (max-width: 768px) {
  .result-shell { padding: 30px 16px 60px !important; }
  .result-score { font-size: 78px !important; }
  .result-score .total { font-size: 32px !important; }
  .result-msg { font-size: 17px !important; margin-bottom: 24px !important; }
  .result-eyebrow { margin-bottom: 12px !important; }

  .review-list { margin-top: 24px; }
  .review-item {
    padding: 14px 16px !important;
    border-radius: 12px !important;
    margin-bottom: 10px !important;
  }
  .review-item .review-num { font-size: 10.5px !important; margin-bottom: 6px !important; }
  .review-item .review-q { font-size: 14.5px !important; line-height: 1.6 !important; }
  .review-item .review-ans { font-size: 12px !important; }

  /* Result CTAs — full-width stack */
  .result-shell .gap-row {
    flex-direction: column !important;
    gap: 8px !important;
    align-items: stretch !important;
  }
  .result-shell .gap-row .btn {
    width: 100%;
    padding: 14px 18px !important;
    min-height: var(--tap-min);
    border-radius: 12px;
    justify-content: center;
  }

  /* Share buttons full width */
  .share-section {
    padding: 16px !important;
  }
  .share-buttons {
    flex-direction: column !important;
    gap: 8px !important;
  }
  .share-btn {
    width: 100%;
    min-height: var(--tap-min);
  }
}

/* ============================================================
   WEAK / STATS — mobile
   ============================================================ */
@media (max-width: 768px) {
  .page-head h1 { font-size: 22px !important; }
  .page-sub { font-size: 13px !important; }
  .weak-section { padding: 14px 14px !important; border-radius: 12px !important; margin-bottom: 12px !important; }
  .weak-section h3 { font-size: 15px !important; margin-bottom: 10px !important; }

  /* Weak rows already collapse to 1 col under 720px; tighten further */
  .weak-row {
    gap: 8px !important;
    padding: 12px !important;
    border-radius: 10px !important;
  }
  .weak-cat-name { font-size: 14px !important; }
  .weak-row .btn {
    width: 100%;
    min-height: 40px;
    justify-content: center;
  }

  .stats-overview {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
  }
  .stat-card-big { padding: 12px 14px !important; border-radius: 10px !important; }
  .stat-card-num { font-size: 26px !important; }
  .stat-card-label { font-size: 11px !important; }

  .cat-bar-row {
    grid-template-columns: 1fr !important;
    gap: 4px !important;
  }
  .cat-bar-label { font-size: 12.5px; }
  .cat-bar-count { text-align: left !important; }
}

/* ============================================================
   Smaller phone tweaks (≤380px — iPhone SE, narrow Android)
   ============================================================ */
@media (max-width: 380px) {
  .hero-title { font-size: 26px !important; }
  .home-action-row { gap: 8px !important; }
  .action-card { padding: 12px !important; min-height: 86px; }
  .action-name { font-size: 13.5px !important; }
  .action-sub { font-size: 11px !important; }
  .cat-card { padding: 12px 14px 10px !important; }
  .cat-card .cat-name { font-size: 16.5px !important; }

  .q-card { padding: 14px 14px 14px !important; }
  .q-prompt { font-size: 15.5px !important; }
  .choice { padding: 11px 12px !important; font-size: 14px !important; }
}

/* ============================================================
   Desktop: still benefit from a few tightening tweaks
   ============================================================ */
@media (min-width: 769px) {
  /* Pull-to-refresh hidden on desktop */
  .ptr-indicator { display: none !important; }

  /* Result CTAs gain consistent min-height */
  .result-shell .gap-row .btn { min-height: 44px; }
}
