/* ============================================
   App shell — reset, layout, header, sidebar,
   main, mobile tabbar, safe-area handling.
   ============================================ */

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

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

body {
  margin: 0;
  font-family: var(--font-body);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--color-text);
  background: var(--color-surface);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

a {
  color: var(--color-primary);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

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

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

input,
textarea,
select {
  font: inherit;
  font-size: 16px; /* iOS no-zoom */
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ── App grid ── */
.app {
  position: relative; /* anchors .sidebar-resizer handle */
  min-height: 100dvh;
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "tabbar tabbar";
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-rows: auto 1fr auto;
  padding-top: var(--safe-top);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
}

@media (max-width: 1023px) {
  .app {
    grid-template-areas:
      "header"
      "main"
      "tabbar";
    grid-template-columns: 1fr;
  }
}

/* ── Header ── */
.app-header {
  grid-area: header;
  position: sticky;
  top: var(--safe-top);
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  height: var(--header-height);
  padding: 0 clamp(var(--space-4), 2vw, var(--space-6));
  background: color-mix(in oklch, var(--color-surface) 78%, transparent);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-bottom: 1px solid color-mix(in oklch, var(--color-border) 80%, transparent);
  box-shadow: 0 1px 0 color-mix(in oklch, var(--color-surface-raised) 40%, transparent);
}

.app-header__left,
.app-header__right {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.app-header__logo {
  display: inline-flex;
  align-items: center;
  gap: var(--space-3);
  cursor: pointer;
  user-select: none;
  border-radius: var(--radius-md);
  padding: var(--space-1) var(--space-2);
  transition: background var(--duration-fast) ease;
}

.app-header__logo:hover {
  background: var(--color-surface-sunken);
}

.app-header__logo-icon {
  position: relative;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  background: var(--gradient-brand);
  color: #ffffff;
  border-radius: 10px;
  font-weight: var(--weight-black);
  font-size: 1.15rem;
  letter-spacing: -0.04em;
  box-shadow: var(--shadow-brand),
              inset 0 1px 0 oklch(100% 0 0 / 0.45),
              inset 0 -8px 14px oklch(0% 0 0 / 0.12);
  overflow: hidden;
  transition: transform var(--duration-normal) var(--ease-out-expo),
              box-shadow var(--duration-normal) var(--ease-out-expo);
}

/* Glass sheen */
.app-header__logo-icon::before {
  content: "";
  position: absolute;
  inset: 1px;
  border-radius: 9px;
  background: linear-gradient(180deg,
    oklch(100% 0 0 / 0.28) 0%,
    oklch(100% 0 0 / 0) 50%);
  pointer-events: none;
}

.app-header__logo-icon > span {
  position: relative;
  z-index: 1;
}

.app-header__logo:hover .app-header__logo-icon {
  transform: translateY(-1px) rotate(-2deg);
}

.app-header__logo-text {
  font-weight: var(--weight-bold);
  font-size: var(--text-lg);
  letter-spacing: 0.02em;
  background: var(--gradient-brand);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

@media (max-width: 420px) {
  .app-header__logo-text {
    display: none;
  }
}

.menu-toggle {
  width: 44px;
  height: 44px;
  border: 1px solid var(--color-border);
  background: var(--color-surface-raised);
  border-radius: var(--radius-md);
  font-size: 1.2rem;
  display: none;
}

@media (max-width: 1023px) {
  body[data-route="lesson"] .menu-toggle {
    display: inline-grid;
    place-items: center;
  }
}

/* ── Header progress ── */
.app-header__progress {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.app-header__progress-bar {
  width: 100px;
  height: 6px;
  background: var(--color-surface-sunken);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.app-header__progress-fill {
  height: 100%;
  background: var(--gradient-brand);
  transition: width var(--duration-normal) var(--ease-out-quart);
  box-shadow: 0 0 12px oklch(54% 0.23 275 / 0.4);
}

@media (max-width: 600px) {
  .app-header__progress {
    display: none;
  }
}

/* ── Sidebar ── */
.sidebar {
  grid-area: sidebar;
  border-right: 1px solid var(--color-border);
  background: var(--color-surface);
  padding: var(--space-6) var(--space-4);
  overflow-y: auto;
  position: sticky;
  top: calc(var(--header-height) + var(--safe-top));
  align-self: start;
  max-height: calc(100dvh - var(--header-height) - var(--safe-top));
}

.sidebar[hidden] {
  display: none;
}

.sidebar__section-title {
  margin: 0 0 var(--space-3);
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: var(--weight-bold);
  color: var(--color-text-muted);
}

.sidebar__nav {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.sidebar__item {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3);
  min-height: 44px;
  background: transparent;
  border: none;
  border-radius: var(--radius-md);
  text-align: left;
  cursor: pointer;
  width: 100%;
  transition: background var(--duration-fast) ease;
}

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

.sidebar__item[aria-current="true"] {
  background: var(--color-primary-light);
  color: var(--color-primary-dark);
  font-weight: var(--weight-medium);
  position: relative;
}

.sidebar__item[aria-current="true"]::before {
  content: "";
  position: absolute;
  left: -4px;
  top: 8px;
  bottom: 8px;
  width: 3px;
  border-radius: 2px;
  background: var(--gradient-brand);
}

.sidebar__item-number {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--color-surface-sunken);
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  color: var(--color-text-muted);
}

.sidebar__item--done .sidebar__item-number {
  background: var(--gradient-brand);
  color: var(--color-text-inverse);
  box-shadow: 0 2px 6px oklch(54% 0.23 275 / 0.3);
}

.sidebar__item-title {
  font-size: var(--text-sm);
  line-height: var(--leading-tight);
}

/* ── Locked (drip) lessons ── */
.sidebar__item--locked {
  opacity: 0.72;
}
.sidebar__item--locked:hover {
  background: var(--color-surface-sunken);
}
.sidebar__item--locked .sidebar__item-number {
  background: var(--color-surface-sunken);
  color: var(--color-text-muted);
}
.sidebar__item-lock {
  display: block;
  margin-top: 2px;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
  font-weight: var(--weight-regular);
}

/* Locked lesson body card */
.lesson-lock {
  display: grid;
  place-items: center;
  gap: var(--space-3);
  padding: clamp(2rem, 6vw, 4rem) var(--space-4);
  margin: 0 auto;
  max-width: 32rem;
  text-align: center;
  border: 1px dashed var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-surface-sunken);
}
.lesson-lock__icon {
  font-size: 3rem;
}
.lesson-lock__title {
  margin: 0;
  font-size: var(--text-xl);
  font-weight: var(--weight-bold);
  color: var(--color-text-muted);
}
.lesson-lock__msg {
  margin: 0;
  color: var(--color-text-muted);
}
.lesson-lock__countdown {
  margin: 0;
  padding: var(--space-2) var(--space-4);
  border-radius: 999px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  font-weight: var(--weight-medium);
  color: var(--color-text);
}
.lesson-lock__hint {
  margin: 0;
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

/* ── Sidebar as drawer (mobile + tablet) ── */
@media (max-width: 1023px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(86vw, 20rem);
    max-height: 100dvh;
    z-index: 70;
    padding-top: calc(var(--header-height) + var(--safe-top) + var(--space-3));
    padding-bottom: calc(var(--tabbar-height) + var(--safe-bottom) + var(--space-6));
    transform: translateX(-100%);
    transition: transform var(--duration-normal) ease;
    box-shadow: 0 20px 40px oklch(0% 0 0 / 0.2);
  }

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

.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: oklch(0% 0 0 / 0.4);
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-normal) ease;
}

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

/* ── Main ── */
.main {
  grid-area: main;
  min-width: 0;
  padding: clamp(var(--space-5), 2vw + 1rem, var(--space-10));
  padding-bottom: calc(var(--tabbar-height) + var(--safe-bottom) + var(--space-8));
}

@media (max-width: 600px) {
  .main {
    padding: var(--space-4);
    padding-bottom: calc(var(--tabbar-height) + var(--safe-bottom) + var(--space-6));
  }
}

.view-mount:empty {
  display: none;
}

/* ── Mobile tabbar ── */
.mobile-tabbar {
  grid-area: tabbar;
  display: none;
  position: sticky;
  bottom: 0;
  z-index: 40;
  background: color-mix(in oklch, var(--color-surface) 92%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--color-border);
  padding-bottom: var(--safe-bottom);
}

.mobile-tabbar[hidden] {
  display: none;
}

.mobile-tabbar__list {
  list-style: none;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  margin: 0;
  padding: 0;
  height: var(--tabbar-height);
}

.mobile-tabbar__item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  padding: var(--space-2);
  min-height: 44px;
  color: var(--color-text-muted);
  font-size: var(--text-xs);
  font-weight: var(--weight-medium);
}

.mobile-tabbar__item {
  position: relative;
  transition: color var(--duration-fast) ease,
              transform var(--duration-fast) ease;
}

.mobile-tabbar__item:active {
  transform: scale(0.94);
}

.mobile-tabbar__item[aria-current="true"] {
  color: var(--color-primary);
  font-weight: var(--weight-bold);
}

.mobile-tabbar__item[aria-current="true"]::before {
  content: "";
  position: absolute;
  top: 4px;
  left: 50%;
  transform: translateX(-50%);
  width: 28px;
  height: 3px;
  border-radius: 2px;
  background: var(--gradient-brand);
}

.mobile-tabbar__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  line-height: 1;
  color: var(--color-text-muted, oklch(45% 0.02 280));
  transition: color 180ms ease, transform 180ms ease;
}

.mobile-tabbar__icon svg {
  width: 100%;
  height: 100%;
  display: block;
}

.mobile-tabbar__item:hover .mobile-tabbar__icon,
.mobile-tabbar__item:focus-visible .mobile-tabbar__icon {
  color: var(--color-text, oklch(20% 0.02 280));
}

.mobile-tabbar__item[aria-current="true"] .mobile-tabbar__icon {
  color: var(--color-primary, #4f46e5);
  transform: translateY(-1px);
}

.mobile-tabbar__item[aria-current="true"] .mobile-tabbar__icon svg {
  stroke-width: 2.1;
}

@media (prefers-color-scheme: dark) {
  .mobile-tabbar__icon {
    color: oklch(70% 0.01 280);
  }
  .mobile-tabbar__item:hover .mobile-tabbar__icon,
  .mobile-tabbar__item:focus-visible .mobile-tabbar__icon {
    color: oklch(95% 0.005 280);
  }
}

@media (max-width: 1023px) {
  body[data-auth="true"]:not([data-route="lesson"]) .mobile-tabbar {
    display: block;
  }
}

/* ── Reduced motion ── */
@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;
  }
}

/* ── Standalone PWA: remove browser chrome padding ── */
@media (display-mode: standalone) {
  body {
    overscroll-behavior: none;
  }
}

/* ── Coarse pointer: larger tap targets ── */
@media (pointer: coarse) {
  .sidebar__item,
  .mobile-tabbar__item {
    min-height: 48px;
  }
}

/* Skip link for keyboard users */
.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
}

.skip-link:focus {
  left: var(--space-4);
  top: var(--space-4);
  z-index: 100;
  background: var(--color-primary);
  color: var(--color-text-inverse);
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-md);
}

/* ── Resizable sidebar handle ──────────────────────────────
   メニュー（カリキュラム）とコンテンツの境界をドラッグして
   サイドバー幅を調整する。デスクトップの lesson 表示中のみ表示。 */
.sidebar-resizer {
  position: absolute;
  top: calc(var(--header-height) + var(--safe-top, 0px));
  bottom: 0;
  left: calc(var(--safe-left, 0px) + var(--sidebar-width));
  width: 16px;
  transform: translateX(-50%);
  z-index: 45;
  display: none;
  cursor: col-resize;
  background: transparent;
  border: 0;
  padding: 0;
  touch-action: none;
}
/* 常時うっすら見える境界線（ドラッグできることを示す） */
.sidebar-resizer::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 2px;
  transform: translateX(-50%);
  background: var(--color-border);
  transition: width var(--duration-fast, 150ms) ease,
    background-color var(--duration-fast, 150ms) ease;
}
/* 常時見えるグリップ（中央のつまみ）。ドラッグ可能だと一目で分かる */
.sidebar-resizer::after {
  content: "⋮";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 16px;
  height: 40px;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  box-shadow: 0 1px 4px rgba(16, 24, 40, 0.12);
  color: var(--color-text-muted);
  font-size: 14px;
  line-height: 1;
  opacity: 0.85;
  transition: opacity var(--duration-fast, 150ms) ease,
    background-color var(--duration-fast, 150ms) ease,
    border-color var(--duration-fast, 150ms) ease,
    color var(--duration-fast, 150ms) ease;
}
.sidebar-resizer:hover::before,
.sidebar-resizer:focus-visible::before,
.sidebar-resizer[data-dragging="true"]::before {
  width: 3px;
  background: var(--color-primary);
}
.sidebar-resizer:hover::after,
.sidebar-resizer:focus-visible::after,
.sidebar-resizer[data-dragging="true"]::after {
  opacity: 1;
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: var(--color-text-inverse, #fff);
}
.sidebar-resizer:focus-visible {
  outline: none;
}
@media (min-width: 1024px) {
  body[data-route="lesson"] .sidebar-resizer {
    display: block;
  }
}

/* ── Offline表示インジケータ ───────────────────────────────
   cloud 読み取り失敗時に同梱コンテンツへフォールバックしたことを
   控えめに伝える帯。db.js が dispatch する nva:offline-mode で表示。 */
.offline-banner {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
  transform: translate(-50%, 24px);
  z-index: 90;
  display: none;
  align-items: center;
  gap: var(--space-2, 8px);
  max-width: min(92vw, 560px);
  padding: 10px 12px 10px 14px;
  border-radius: 999px;
  background: color-mix(in oklch, var(--color-surface) 88%, #000 4%);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  box-shadow: 0 10px 30px rgba(16, 24, 40, 0.18);
  backdrop-filter: saturate(160%) blur(12px);
  -webkit-backdrop-filter: saturate(160%) blur(12px);
  font-size: var(--text-sm, 0.875rem);
  opacity: 0;
  transition: opacity var(--duration-normal, 300ms) ease,
    transform var(--duration-normal, 300ms) ease;
}
.offline-banner[data-show="true"]:not([data-dismissed="true"]) {
  display: flex;
  opacity: 1;
  transform: translate(-50%, 0);
}
.offline-banner__dot {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #f59e0b;
  box-shadow: 0 0 0 4px color-mix(in oklch, #f59e0b 22%, transparent);
}
.offline-banner__msg {
  line-height: 1.4;
}
.offline-banner__close {
  flex: none;
  border: 0;
  background: transparent;
  color: var(--color-text-muted);
  font-size: 1.1rem;
  line-height: 1;
  padding: 2px 6px;
  border-radius: 8px;
  cursor: pointer;
}
.offline-banner__close:hover {
  background: var(--color-surface-sunken);
  color: var(--color-text);
}
@media (prefers-reduced-motion: reduce) {
  .offline-banner {
    transition: none;
  }
}
