/* ============================================================
   v2-responsive.css — Mobile-first responsive system
   ------------------------------------------------------------
   Standard breakpoints:
     xs:   0    – 479   (small phone, 1-handed)
     sm:   480  – 639   (large phone)
     md:   640  – 899   (small tablet)
     lg:   900  – 1199  (large tablet / small laptop)
     xl:   1200 – 1599  (desktop)
     2xl:  1600+        (large desktop)

   Strategy:
     - Mobile-first base styles
     - Progressive enhancement at min-width breakpoints
     - Touch targets ≥ 44px on coarse pointers
     - Fluid typography via clamp()
     - Container-aware grid (auto-fit minmax)
   ============================================================ */

/* ──────────────────────────────────────────────────────────
   Layout containers
   ────────────────────────────────────────────────────────── */
.app {
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  min-height: 100vh; /* fallback */
}

/* Main content padding scales with viewport */
.main {
  padding: clamp(12px, 3vw, 32px) !important;
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
  box-sizing: border-box;
}

@media (min-width: 1600px) {
  .main {
    max-width: 1440px;
  }
}

/* Body bottom padding for mobile tabbar (phones + tablets) */
@media (max-width: 1023px) {
  body[data-auth="true"]:not([data-route="lesson"]) .main {
    padding-bottom: calc(env(safe-area-inset-bottom, 0) + 92px) !important;
  }
}

/* ──────────────────────────────────────────────────────────
   Header — responsive sizing & layout
   ────────────────────────────────────────────────────────── */
.app-header {
  height: auto;
  min-height: 56px;
  padding: 0 clamp(12px, 3vw, 24px);
  padding-top: max(env(safe-area-inset-top, 0), 4px);
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: nowrap;
}

.app-header__left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex-shrink: 1;
}

.app-header__right {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
  flex-shrink: 0;
}

.app-header__logo-text {
  font-size: clamp(13px, 1.6vw, 15px);
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Top menu: タブレットサイズまでは非表示。SVG タブバーが優先。
   1024px 以上のラップトップ・デスクトップでだけ上部メニューを出す。 */
.app-menu {
  display: none !important;
}

@media (min-width: 1024px) {
  .app-menu {
    display: flex !important;
    align-items: center;
    gap: 4px;
    margin: 0 8px;
    flex-wrap: nowrap;
  }

  /* Body bottom padding restored on large viewports */
  body[data-auth="true"]:not([data-route="lesson"]) .main {
    padding-bottom: clamp(12px, 3vw, 32px) !important;
  }
}

/* Progress meter: hide on phone, show on tablet+ */
.app-header__progress {
  display: none !important;
}

@media (min-width: 768px) {
  .app-header__progress:not([hidden]) {
    display: flex !important;
  }
}

/* Chip badge: hide text on tiny phones */
@media (max-width: 380px) {
  .app-chip {
    display: none !important;
  }
  .app-header__update {
    width: 32px;
    height: 32px;
  }
}

/* Update button: keep visible everywhere, scale on mobile */
.app-header__update {
  width: clamp(32px, 4vw, 36px);
  height: clamp(32px, 4vw, 36px);
}

/* ──────────────────────────────────────────────────────────
   Sidebar — responsive
   既存 .app grid (col 2) に頼りつつ、モバイルではドロワー化
   ────────────────────────────────────────────────────────── */

/* Drawer mode (mobile / tablet portrait) */
@media (max-width: 1023px) {
  .sidebar:not([hidden]) {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(280px, 80vw);
    background: var(--color-surface-raised);
    z-index: 95;
    transform: translateX(-100%);
    transition: transform 280ms var(--ease-out-expo);
    box-shadow: 4px 0 24px oklch(0% 0 0 / 0.15);
    padding: calc(env(safe-area-inset-top, 0) + 16px) 16px 16px;
    overflow-y: auto;
    overscroll-behavior: contain;
    display: block;
  }

  .sidebar[data-open="true"] {
    transform: translateX(0);
  }

  .sidebar-overlay {
    position: fixed;
    inset: 0;
    background: oklch(0% 0 0 / 0.4);
    z-index: 90;
    opacity: 0;
    pointer-events: none;
    transition: opacity 280ms ease;
  }

  .sidebar-overlay[data-open="true"] {
    opacity: 1;
    pointer-events: auto;
  }

  .menu-toggle:not([hidden]) {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: transparent;
    border: 1px solid var(--color-border);
    font-size: 18px;
    cursor: pointer;
    color: var(--color-text);
  }

  .menu-toggle:hover {
    background: var(--color-surface-sunken);
  }
}

/* Persistent sidebar mode (desktop with lesson view)
   ※ 既存 .app の grid-template-areas が sidebar 配置を担当する前提。
   .app は上部の `.app { display:flex }`（モバイル基準）で上書きされて
   グリッドが死んでいたため、ここで desktop の lesson 表示だけグリッドを
   復活させる。これで --sidebar-width が効き、メニュー幅のドラッグ調整が機能する。
   （body[data-route="lesson"] .app は `.app` より詳細度が高いので確実に勝つ） */
@media (min-width: 1024px) {
  body[data-route="lesson"] .app {
    display: grid;
    grid-template-areas:
      "header header"
      "sidebar main"
      "tabbar tabbar";
    grid-template-columns: var(--sidebar-width) 1fr;
    grid-template-rows: auto 1fr auto;
  }

  body[data-route="lesson"] .sidebar {
    display: block;
    position: sticky;
    top: var(--header-height, 60px);
    align-self: start;
    height: calc(100dvh - var(--header-height, 60px));
    overflow-y: auto;
    overscroll-behavior: contain;
    border-right: 1px solid var(--color-border);
    background: var(--color-surface);
    padding: 20px 14px;
    transform: none !important; /* drawer の transform を打ち消す */
    box-shadow: none;
  }

  body[data-route="lesson"] .lesson-content {
    padding: 32px clamp(20px, 4vw, 48px);
    max-width: 760px;
    margin: 0 auto;
  }

  .menu-toggle {
    display: none !important;
  }

  .sidebar-overlay {
    display: none !important;
  }
}

/* ──────────────────────────────────────────────────────────
   Mobile tabbar — phones AND tablets (up to 1023px)
   ────────────────────────────────────────────────────────── */
.mobile-tabbar {
  display: none !important;
}

@media (max-width: 1023px) {
  body[data-auth="true"]:not([data-route="lesson"]) .mobile-tabbar:not([hidden]) {
    display: block !important;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 60;
  }
}

/* タブレットサイズ専用：タブバーのアイコン・テキストを少し大きく */
@media (min-width: 600px) and (max-width: 1023px) {
  .mobile-tabbar__list {
    max-width: 720px;
    margin: 0 auto !important;
    padding: 6px 12px 8px !important;
  }
  .mobile-tabbar__item {
    padding: 8px 6px !important;
  }
  .mobile-tabbar__icon {
    width: 28px !important;
    height: 28px !important;
  }
  .mobile-tabbar__item span:last-child {
    font-size: 11.5px !important;
  }
}

/* ──────────────────────────────────────────────────────────
   Hero — fluid scaling
   ────────────────────────────────────────────────────────── */
.dashboard__hero {
  padding: clamp(20px, 5vw, 56px) clamp(20px, 4vw, 40px) !important;
  border-radius: clamp(16px, 3vw, 28px) !important;
}

.dashboard__hero-title {
  font-size: clamp(20px, 4.5vw, 36px) !important;
  line-height: 1.35 !important;
}

.dashboard__hero-sub {
  font-size: clamp(13px, 1.6vw, 15px) !important;
}

@media (max-width: 479px) {
  .dashboard__hero-title br {
    display: none;
  }
}

/* ──────────────────────────────────────────────────────────
   Course grid — fluid columns
   ────────────────────────────────────────────────────────── */
.course-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

@media (min-width: 480px) {
  .course-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 14px;
  }
}

@media (min-width: 768px) {
  .course-grid {
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 16px;
  }
}

@media (min-width: 1280px) {
  .course-grid {
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 20px;
  }
}

.course-card {
  min-height: clamp(180px, 22vw, 240px) !important;
}

/* ──────────────────────────────────────────────────────────
   Section headers — vertical layout on mobile
   ────────────────────────────────────────────────────────── */
.dashboard__section-head {
  flex-direction: row;
  align-items: baseline;
  gap: 8px;
}

@media (max-width: 479px) {
  .dashboard__section-head {
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
  }
}

/* ──────────────────────────────────────────────────────────
   Forms — touch-friendly sizes
   ────────────────────────────────────────────────────────── */
.form__input,
.form__textarea,
.form__select {
  min-height: 44px;
  font-size: 16px; /* prevents iOS zoom on focus */
  padding: 12px 14px !important;
}

.form__textarea {
  min-height: 100px;
}

@media (min-width: 1024px) {
  .form__input,
  .form__select {
    font-size: 15px;
  }
  .form__textarea {
    font-size: 15px;
  }
}

/* ──────────────────────────────────────────────────────────
   Buttons — touch-friendly + responsive widths
   ────────────────────────────────────────────────────────── */
.btn {
  min-height: 44px;
  padding: 10px 18px;
  font-size: clamp(13px, 1.5vw, 14px);
}

/* Full-width buttons on tiny screens */
@media (max-width: 379px) {
  .btn-row {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .btn-row .btn {
    width: 100%;
  }
}

/* ──────────────────────────────────────────────────────────
   Profile — centered with max width
   ────────────────────────────────────────────────────────── */
.profile {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.profile__header {
  display: flex;
  align-items: center;
  gap: clamp(12px, 2.5vw, 20px);
  padding: 8px 4px;
  flex-wrap: wrap;
}

.profile__avatar {
  width: clamp(56px, 10vw, 80px) !important;
  height: clamp(56px, 10vw, 80px) !important;
}

.profile__name {
  font-size: clamp(18px, 2.5vw, 24px) !important;
}

@media (max-width: 379px) {
  .profile {
    gap: 12px;
  }
  .card {
    padding: 18px;
    border-radius: 16px;
  }
}

/* ──────────────────────────────────────────────────────────
   Lesson content — reading-optimized widths
   ────────────────────────────────────────────────────────── */
.lesson-content {
  max-width: 100%;
  padding: clamp(16px, 4vw, 32px);
  font-size: clamp(15px, 1.6vw, 17px);
  line-height: 1.8;
}

@media (min-width: 768px) {
  .lesson-content {
    max-width: 720px;
    margin: 0 auto;
  }
}

@media (min-width: 1024px) {
  .lesson-content {
    max-width: 760px;
  }
}

/* Lesson sections: heading sizes scale */
.lesson-content h1 {
  font-size: clamp(22px, 4vw, 32px);
}

.lesson-content h2,
.lesson-content .sec-h {
  font-size: clamp(18px, 2.6vw, 22px);
}

.lesson-content h3 {
  font-size: clamp(15px, 2vw, 18px);
}

/* ──────────────────────────────────────────────────────────
   Cards — adaptive padding
   ────────────────────────────────────────────────────────── */
.card {
  padding: clamp(16px, 3vw, 24px) !important;
  border-radius: clamp(14px, 2.5vw, 20px) !important;
}

.card__title {
  font-size: clamp(14px, 1.8vw, 17px) !important;
}

/* ──────────────────────────────────────────────────────────
   Modals (PWA install + others) — adaptive
   ────────────────────────────────────────────────────────── */
@media (max-width: 599px) {
  .pwa-install-modal {
    align-items: flex-end !important;
  }
  .pwa-install-modal__sheet {
    border-radius: 22px 22px 0 0 !important;
    max-height: 85vh !important;
  }
}

@media (min-width: 600px) {
  .pwa-install-modal {
    align-items: center !important;
    padding: 32px !important;
  }
  .pwa-install-modal__sheet {
    border-radius: 24px !important;
    max-height: 88vh !important;
    max-width: 480px !important;
  }
}

/* ──────────────────────────────────────────────────────────
   Toast — adaptive bottom position
   ────────────────────────────────────────────────────────── */
.toast {
  font-size: clamp(13px, 1.6vw, 14px);
  padding: 10px 16px !important;
}

@media (max-width: 1023px) {
  .toast {
    bottom: calc(env(safe-area-inset-bottom, 0) + 96px) !important;
  }
}

@media (min-width: 1024px) {
  .toast {
    bottom: 32px !important;
  }
}

/* ──────────────────────────────────────────────────────────
   Touch target safety — coarse pointers
   ────────────────────────────────────────────────────────── */
@media (pointer: coarse) {
  /* All interactive elements get 44px minimum */
  button,
  a.btn,
  .btn,
  .mobile-tabbar__item,
  .sidebar__item,
  .app-menu__item {
    min-height: 44px;
  }

  /* Add tap padding to small icon buttons */
  .app-header__update,
  .menu-toggle {
    width: 40px;
    height: 40px;
  }
}

/* ──────────────────────────────────────────────────────────
   Auth screen — center & responsive
   ────────────────────────────────────────────────────────── */
.auth-screen {
  max-width: 440px;
  margin: 0 auto;
  padding: clamp(20px, 5vw, 40px);
  width: 100%;
  box-sizing: border-box;
}

@media (min-height: 700px) and (min-width: 600px) {
  .auth-screen {
    padding-top: 8vh;
  }
}

/* ──────────────────────────────────────────────────────────
   Quizzes — adaptive
   ────────────────────────────────────────────────────────── */
.quiz {
  padding: clamp(14px, 2.5vw, 22px);
}

.quiz__options {
  display: grid;
  gap: 8px;
}

@media (min-width: 600px) {
  .quiz__options {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  }
}

.quiz__option {
  min-height: 44px;
  padding: 10px 14px;
  text-align: left;
  white-space: normal;
  word-break: break-word;
}

/* ──────────────────────────────────────────────────────────
   Admin screens — wider layouts
   ────────────────────────────────────────────────────────── */
.admin-screen,
.admin {
  max-width: 1400px;
  margin: 0 auto;
}

@media (min-width: 1600px) {
  body[data-route="admin"] .main {
    max-width: 1600px;
  }
}

/* ──────────────────────────────────────────────────────────
   Comms / Q&A — responsive list
   ────────────────────────────────────────────────────────── */
.comms-list,
.qa-list {
  display: grid;
  gap: 10px;
}

@media (min-width: 768px) {
  .comms-list,
  .qa-list {
    gap: 12px;
  }
}

/* ──────────────────────────────────────────────────────────
   Image / video / iframe responsive
   ────────────────────────────────────────────────────────── */
.lesson-content img,
.lesson-content video,
.lesson-content iframe {
  max-width: 100%;
  height: auto;
  border-radius: 12px;
}

.lesson-content figure {
  margin: 1em 0;
}

/* ──────────────────────────────────────────────────────────
   Tables — horizontal scroll on small viewports
   ────────────────────────────────────────────────────────── */
.lesson-content table,
.card table {
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  width: 100%;
  max-width: 100%;
}

@media (min-width: 600px) {
  .lesson-content table,
  .card table {
    display: table;
  }
}

/* ──────────────────────────────────────────────────────────
   Print styles
   ────────────────────────────────────────────────────────── */
@media print {
  .app-header,
  .mobile-tabbar,
  .sidebar,
  .sidebar-overlay,
  .toast,
  .pwa-install-modal,
  .pwa-update-banner,
  .app-header__update {
    display: none !important;
  }

  body {
    background: white !important;
    color: black !important;
  }

  .lesson-content {
    max-width: 100% !important;
    padding: 0 !important;
  }

  .course-card {
    break-inside: avoid;
    box-shadow: none !important;
    border: 1px solid #ccc !important;
  }
}

/* ──────────────────────────────────────────────────────────
   Landscape phone handling
   ────────────────────────────────────────────────────────── */
@media (max-height: 480px) and (orientation: landscape) {
  .dashboard__hero {
    padding: 16px 20px !important;
  }
  .dashboard__hero-title {
    font-size: 18px !important;
  }
  .dashboard__hero-progress {
    margin-top: 12px;
  }
}

/* ──────────────────────────────────────────────────────────
   Tabbar — show item labels conditionally
   ────────────────────────────────────────────────────────── */
@media (max-width: 379px) {
  .mobile-tabbar__item span:last-child {
    font-size: 9.5px !important;
  }
  .mobile-tabbar__icon {
    width: 22px !important;
    height: 22px !important;
  }
}

/* ──────────────────────────────────────────────────────────
   Ultra-wide / 2xl support
   ────────────────────────────────────────────────────────── */
@media (min-width: 1600px) {
  :root {
    --content-max: 88rem;
    --reading-max: 44rem;
  }

  .dashboard__hero {
    padding: 60px 48px !important;
  }

  .dashboard__hero-title {
    font-size: 40px !important;
  }
}

/* ──────────────────────────────────────────────────────────
   Foldable / dual-screen (CSS spanning)
   ────────────────────────────────────────────────────────── */
@media (horizontal-viewport-segments: 2) {
  .main {
    /* Avoid placing content on the fold */
    padding-left: env(viewport-segment-left 1 0, 24px);
  }
}

/* ──────────────────────────────────────────────────────────
   High contrast / forced colors mode
   ────────────────────────────────────────────────────────── */
@media (forced-colors: active) {
  .course-card {
    border: 1px solid CanvasText !important;
  }
  .btn--primary {
    background: Highlight !important;
    color: HighlightText !important;
  }
  .app-header__logo-icon {
    border: 1px solid CanvasText !important;
  }
}

/* ──────────────────────────────────────────────────────────
   Dynamic viewport height fix (mobile address bar)
   ────────────────────────────────────────────────────────── */
@supports (height: 100dvh) {
  body, html {
    min-height: 100dvh;
  }
}

/* ──────────────────────────────────────────────────────────
   Horizontal overflow hard guard
   `overflow-x: hidden` を body に与えると、CSS 仕様で
   overflow-y も auto 扱いになって body 自身が scroll container
   化し、結果として「ページ全体がスクロールできない」現象が
   発生する（特に PWA standalone）。 `clip` を使えば scroll
   container を作らず、横はみ出しだけ抑止できる。
   ────────────────────────────────────────────────────────── */
html, body {
  overflow-x: clip;
  max-width: 100vw;
}

/* Safe-area aware app boundaries */
.app {
  padding-left: env(safe-area-inset-left, 0);
  padding-right: env(safe-area-inset-right, 0);
}
