/**
 * Clay 兒童首頁 — 僅 .home-* + 文件化 shared .clay-*
 * 色彩／陰影／動效時長一律走 clay tokens（禁裸 hex）
 */

/* ── 頁面殼 ─────────────────────────────────────── */
.home-page {
  position: relative;
  isolation: isolate;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  padding:
    max(16px, env(safe-area-inset-top))
    max(20px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom))
    max(20px, env(safe-area-inset-left));
  color: var(--clay-text-brown);
  /* 首屏避免請求 Open Huninn TTF；僅限首頁，不動 --clay-font 全域 */
  font-family: var(--font-display);
  overflow: hidden;
  background: linear-gradient(
    180deg,
    var(--clay-sky-top) 0%,
    var(--clay-sky-bottom) 100%
  );
}

/* ── 裝飾層（不可攔截點擊） ─────────────────────── */
.home-deco {
  position: absolute;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
}

.home-sky,
.home-hills {
  position: absolute;
  display: block;
  max-width: none;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

.home-sky {
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center top;
}

.home-hills {
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: auto;
  max-height: 42%;
  object-fit: cover;
  object-position: center bottom;
}

/* ── Header：吉祥物+wordmark / 齒輪 ─────────────── */
.home-header {
  position: relative;
  /* theme popover 必須浮在後續 .home-main 卡片上方。 */
  z-index: 2;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex: 0 0 auto;
  min-height: 72px;
  margin-bottom: 8px;
}

.home-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  pointer-events: none;
}

.home-mascot {
  width: clamp(64px, 9vw, 96px);
  height: auto;
  flex: 0 0 auto;
  object-fit: contain;
}

/* B4：wordmark + version 穩定垂直置中，不綁背景雲朵 crop */
.home-brand-mark {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 0 1 auto;
}

.home-wordmark {
  width: clamp(140px, 22vw, 240px);
  height: auto;
  flex: 0 1 auto;
  object-fit: contain;
  object-position: left center;
}

/* 家長除錯用版號：wordmark 右側垂直置中（B4／T-A06） */
.home-app-version {
  flex: 0 0 auto;
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 13px;
  font-weight: 600;
  line-height: 1;
  letter-spacing: 0.02em;
  color: rgba(92, 64, 51, 0.55);
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
  white-space: nowrap;
}

/* 右上：貼紙簿 + 主題 + 家長齒輪（長按） */
.home-header-actions {
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  gap: 12px;
  flex: 0 0 auto;
}

/* 版本更新：只在 remote strict-semver 較新時由 JS 顯示，並非清除快取操作。 */
.home-update-notice {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: fit-content;
  max-width: min(100%, 540px);
  min-height: 52px;
  margin: 0 auto 12px;
  padding: 8px 10px 8px 16px;
  border-radius: var(--radius-xl);
  background: var(--clay-capsule-cream);
  box-shadow: var(--clay-shadow-card);
  color: var(--clay-text-brown);
}

.home-update-notice[hidden] {
  display: none;
}

.home-update-copy {
  font-size: 0.95rem;
  font-weight: 700;
  white-space: nowrap;
}

.home-update-button {
  min-width: 132px;
  min-height: 44px;
  padding: 8px 14px;
  border: 0;
  border-radius: var(--radius-lg);
  background: var(--clay-card-blue);
  box-shadow: var(--clay-shadow-card);
  color: var(--clay-text-brown);
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}

.home-update-button:active:not(:disabled) {
  transform: scale(var(--clay-press-scale));
  box-shadow: var(--clay-shadow-pressed);
}

.home-update-button:focus-visible {
  outline: 3px solid var(--clay-sky-top);
  outline-offset: 3px;
}

.home-update-button:disabled {
  cursor: wait;
  opacity: 0.72;
}

/* 弱點練習入口：≥100px、圖像 only、無文字標籤（S2-2） */
.home-weak-practice {
  position: relative;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  width: 100px;
  height: 100px;
  min-width: 100px;
  min-height: 100px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-xl);
  background: var(--clay-capsule-cream);
  box-shadow: var(--clay-shadow-card);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform var(--clay-motion-squish) ease,
    box-shadow var(--clay-motion-squish) ease;
}

.home-weak-practice:active {
  transform: scale(var(--clay-press-scale));
  box-shadow: var(--clay-shadow-pressed);
}

.home-weak-practice:focus-visible {
  outline: 3px solid var(--clay-sky-top);
  outline-offset: 3px;
}

.home-weak-practice-icon {
  display: block;
  width: 56px;
  height: 56px;
  pointer-events: none;
}

/* 貼紙簿入口：≥100px、圖像 only、無文字標籤 */
.home-collection {
  position: relative;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  width: 100px;
  height: 100px;
  min-width: 100px;
  min-height: 100px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-xl);
  background: var(--clay-capsule-cream);
  box-shadow: var(--clay-shadow-card);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform var(--clay-motion-squish) ease,
    box-shadow var(--clay-motion-squish) ease;
}

.home-collection:active {
  transform: scale(var(--clay-press-scale));
  box-shadow: var(--clay-shadow-pressed);
}

.home-collection:focus-visible {
  outline: 3px solid var(--clay-sky-top);
  outline-offset: 3px;
}

.home-collection-icon {
  display: block;
  width: 56px;
  height: 56px;
  object-fit: contain;
  pointer-events: none;
}

/* H2：核准 icon 的入口維持 collection / gear 同一個 100×100 click target。 */
.home-theme-picker {
  position: relative;
  flex: 0 0 auto;
}

.home-theme-trigger {
  position: relative;
  display: inline-grid;
  place-items: center;
  width: 100px;
  height: 100px;
  min-width: 100px;
  min-height: 100px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-xl);
  background: var(--clay-capsule-cream);
  box-shadow: var(--clay-shadow-card);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform var(--clay-motion-squish) ease,
    box-shadow var(--clay-motion-squish) ease;
}

.home-theme-trigger:active,
.home-theme-picker:has(.home-theme-popover:not([hidden])) .home-theme-trigger {
  transform: scale(var(--clay-press-scale));
  box-shadow: var(--clay-shadow-pressed);
}

.home-theme-trigger:focus-visible,
.home-theme-option:focus-visible {
  outline: 3px solid var(--clay-sky-top);
  outline-offset: 3px;
}

.home-theme-icon {
  display: block;
  width: 56px;
  height: 56px;
  object-fit: contain;
  pointer-events: none;
}

/* 小 hero 只回饋目前已選主題；首頁背景與其他 UI token 不會隨此改色。 */
.home-current-theme-hero {
  position: absolute;
  right: 7px;
  bottom: 7px;
  display: block;
  width: 28px;
  height: 28px;
  padding: 2px;
  border-radius: 50%;
  background: var(--clay-capsule-cream);
  box-shadow: var(--clay-shadow-card);
  object-fit: contain;
  pointer-events: none;
}

.home-theme-popover {
  position: absolute;
  z-index: 12;
  top: calc(100% + 12px);
  right: 0;
  box-sizing: border-box;
  /* 5×100 + 4×10 gap + 24 padding = 564；不能用 548 截斷內容。 */
  width: 564px;
  max-width: calc(100vw - 40px);
  padding: 12px;
  border-radius: var(--radius-xl);
  background: var(--clay-capsule-cream);
  box-shadow: var(--clay-shadow-card);
}

.home-theme-options {
  display: grid;
  grid-template-columns: repeat(5, 100px);
  gap: 10px;
}

.home-theme-option.clay-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100px;
  min-width: 100px;
  height: 100px;
  min-height: 100px;
  padding: 8px;
  border: 0;
  border-radius: var(--clay-radius-card);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition:
    transform var(--clay-motion-squish) ease,
    box-shadow var(--clay-motion-squish) ease;
}

.home-theme-option.clay-card:active {
  transform: scale(var(--clay-press-scale));
  box-shadow: var(--clay-shadow-pressed);
}

.home-theme-option.is-selected {
  box-shadow:
    0 0 0 4px var(--clay-text-brown),
    0 10px 20px rgba(122, 74, 45, 0.28);
  transform: translateY(-2px);
}

.home-theme-option-hero {
  display: block;
  width: 72px;
  height: 72px;
  object-fit: contain;
  pointer-events: none;
}

/* C3：設定與收藏同尺寸 — 外框 100×100、內圖 56×56；長按 ring 同步 100 */
.home-gear {
  /* 長按進度 0–1（JS 寫入；CSS 驅動 ring） */
  --home-gear-progress: 0;
  position: relative;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  width: 100px;
  height: 100px;
  min-width: 100px;
  min-height: 100px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-xl);
  background: var(--clay-capsule-cream);
  box-shadow: var(--clay-shadow-card);
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  transition:
    transform var(--clay-motion-squish) ease,
    box-shadow var(--clay-motion-squish) ease;
}

.home-gear:active,
.home-gear.is-holding {
  transform: scale(var(--clay-press-scale));
  box-shadow: var(--clay-shadow-pressed);
}

.home-gear:focus-visible {
  outline: 3px solid var(--clay-sky-top);
  outline-offset: 3px;
}

.home-gear-ring {
  position: absolute;
  inset: 0;
  width: 100px;
  height: 100px;
  pointer-events: none;
  transform: rotate(-90deg);
}

.home-gear-ring-track,
.home-gear-ring-progress {
  fill: none;
  stroke-width: 4;
  stroke-linecap: round;
}

.home-gear-ring-track {
  stroke: var(--clay-card-blue);
  opacity: 0.55;
}

.home-gear-ring-progress {
  stroke: var(--clay-sky-top);
  stroke-dasharray: 100;
  stroke-dashoffset: 100;
  opacity: 0;
  transition: opacity var(--clay-motion-squish) ease;
}

.home-gear.is-holding .home-gear-ring-progress,
.home-gear.is-complete .home-gear-ring-progress {
  opacity: 1;
}

.home-gear-icon {
  position: relative;
  z-index: 1;
  display: block;
  width: 56px;
  height: 56px;
  object-fit: contain;
  pointer-events: none;
}

/* ── 主區 + Grid ────────────────────────────────── */
.home-main {
  position: relative;
  z-index: 1;
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
}

.home-grid {
  display: grid;
  /* 孩子端 exact 10 卡＝ 5×2；column flow → DOM 成對上下相鄰（數數／數字筆順…） */
  grid-template-columns: repeat(5, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  grid-auto-flow: column;
  gap: clamp(10px, 1.2vw, 16px);
  width: 100%;
  min-height: 0;
  align-content: stretch;
}

/* ── 模組卡 ─────────────────────────────────────── */
.home-card.clay-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 0;
  width: 100%;
  min-width: 0;
  min-height: 200px;
  height: 100%;
  padding: 12px 10px 14px;
  margin: 0;
  border: 0;
  border-radius: var(--clay-radius-card);
  box-shadow: var(--clay-shadow-card);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
  color: var(--clay-text-brown);
  font-family: var(--font-display);
  animation: home-card-bounce 0.55s cubic-bezier(0.34, 1.4, 0.64, 1) both;
  animation-delay: calc(var(--home-stagger, 0) * 45ms);
  transition:
    transform var(--clay-motion-squish) ease,
    box-shadow var(--clay-motion-squish) ease;
}

.home-card.clay-card:active {
  transform: scale(var(--clay-press-scale));
  box-shadow: var(--clay-shadow-pressed);
}

.home-card.clay-card:focus-visible {
  outline: 3px solid var(--clay-sky-top);
  outline-offset: 3px;
  box-shadow: var(--clay-shadow-halo);
}

/* 圖形區（B4：移除星列後回收高度給 icon） */
.home-card-visual {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 64%;
  width: 100%;
  min-height: 0;
  height: 64%;
  max-height: 68%;
}

.home-card-icon {
  width: auto;
  height: 100%;
  max-width: 92%;
  max-height: 100%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  -webkit-user-drag: none;
}

/* 識字卡 DOM 浮雕字形（疊在 blank container 素材上） */
.home-card-chars {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 6%;
  /* ~38–42px at iPad 1194×834 / 768×1024；三字不溢出 blank */
  font-size: clamp(1.5rem, 5.1vmin, 2.625rem);
  font-weight: 900;
  line-height: 1;
  letter-spacing: 0.04em;
  white-space: nowrap;
  pointer-events: none;
  text-align: center;
}

/* 覆寫 .clay-text-emboss 的 --clay-font，避免首屏載入 Open Huninn TTF */
.home-page .home-card.clay-text-emboss,
.home-page .home-card .clay-text-emboss {
  font-family: var(--font-display);
}

/* 標籤區 20–24% 卡高 */
.home-card-label {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 20%;
  width: 100%;
  min-height: 0;
  max-height: 24%;
  margin-top: auto;
  padding: 6px 10px;
  border-radius: var(--clay-radius-capsule);
  background: var(--clay-capsule-cream);
  box-shadow: var(--clay-shadow-inner-top);
  color: var(--clay-text-brown);
  font-family: var(--font-display);
  font-size: clamp(0.95rem, 1.5vw, 1.2rem);
  font-weight: 700;
  line-height: 1.15;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}

/* ── 進場 bounce ────────────────────────────────── */
@keyframes home-card-bounce {
  0% {
    opacity: 0;
    transform: translateY(22px) scale(0.9);
  }
  55% {
    opacity: 1;
    transform: translateY(-6px) scale(1.03);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes home-card-fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* ── 橫向大屏：5×2、盡量 ≥200×280、無文件捲軸 ─── */
@media (orientation: landscape) and (min-width: 1000px) {
  .home-page {
    height: 100vh;
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
  }

  .home-header {
    min-height: 80px;
    margin-bottom: 10px;
  }

  .home-main {
    flex: 1 1 auto;
    min-height: 0;
  }

  .home-grid {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    grid-template-rows: repeat(2, minmax(0, 1fr));
    grid-auto-flow: column;
    gap: clamp(12px, 1.1vw, 18px);
  }

  .home-card.clay-card {
    min-height: 280px;
  }
}

/* 較小橫向（仍 5×2，卡可低於 280 但仍盡量填滿） */
@media (orientation: landscape) and (max-width: 999px) {
  .home-page {
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  .home-card.clay-card {
    min-height: 184px;
    padding: 10px 8px 12px;
  }

  .home-card-chars {
    font-size: clamp(1.5rem, 5.1vmin, 2.5rem);
  }
}

/* ── 直向：2×5、卡寬 ≥148、允許垂直捲動 ────────── */
@media (orientation: portrait), (max-width: 820px) and (orientation: portrait) {
  .home-page {
    height: auto;
    min-height: 100dvh;
    overflow-x: hidden;
    overflow-y: auto;
  }

  .home-header {
    min-height: 64px;
  }

  .home-mascot {
    width: clamp(56px, 14vw, 80px);
  }

  .home-wordmark {
    width: clamp(120px, 36vw, 200px);
  }

  .home-main {
    flex: 0 0 auto;
  }

  .home-grid {
    /* portrait 2×5 row flow → 同一 DOM 序左右相鄰成對 */
    grid-template-columns: repeat(2, minmax(148px, 1fr));
    grid-template-rows: repeat(5, auto);
    grid-auto-flow: row;
    gap: 14px;
    align-content: start;
  }

  .home-card.clay-card {
    min-width: 148px;
    min-height: 200px;
    height: auto;
    aspect-ratio: 5 / 7;
    animation-delay: calc(var(--home-stagger, 0) * 40ms);
  }

  .home-card-visual {
    flex: 0 0 62%;
    height: 62%;
    max-height: 66%;
  }

  .home-card-label {
    flex: 0 0 20%;
  }

  .home-card-chars {
    font-size: clamp(1.5rem, 5.1vmin, 2.625rem);
  }

  .home-theme-options {
    grid-template-columns: repeat(2, 100px);
  }

  .home-theme-popover {
    /* 2×100 + 1×10 gap + 24 padding = 234。 */
    width: 234px;
  }

  .home-theme-option:last-child {
    grid-column: 1 / -1;
    justify-self: center;
  }
}

/* 窄橫向也是三欄，避免 5×100 的 popover 在 1000px 以下被截斷。 */
@media (orientation: landscape) and (max-width: 1000px) {
  .home-theme-popover {
    /* 3×100 + 2×10 gap + 24 padding = 344。 */
    width: 344px;
  }

  .home-theme-options {
    grid-template-columns: repeat(3, 100px);
  }
}

/* 明確對齊 768 直向規格 */
@media (orientation: portrait) and (min-width: 700px) and (max-width: 820px) {
  .home-grid {
    grid-template-columns: repeat(2, minmax(148px, 1fr));
  }

  .home-card.clay-card {
    min-width: 148px;
  }

  .home-theme-options {
    grid-template-columns: repeat(3, 100px);
  }

  .home-theme-popover {
    /* 3×100 + 2×10 gap + 24 padding = 344。 */
    width: 344px;
  }

  .home-theme-option:last-child {
    grid-column: auto;
    justify-self: stretch;
  }
}

/* 窄手機仍保留三顆 100px touch target，但 header 允許它們換列，不能水平溢出。 */
@media (max-width: 560px) {
  .home-header {
    flex-wrap: wrap;
  }

  .home-header-actions {
    width: 100%;
    max-width: 100%;
    flex-wrap: wrap;
    justify-content: flex-end;
  }
}

/* ── reduced-motion：停用 transform / bounce ────── */
@media (prefers-reduced-motion: reduce) {
  .home-card.clay-card {
    animation: home-card-fade var(--clay-motion-reduced-fade) ease both;
    animation-delay: calc(var(--home-stagger, 0) * 20ms);
    transition: opacity var(--clay-motion-reduced-fade) ease;
  }

  .home-card.clay-card:active,
  .home-gear:active,
  .home-gear.is-holding,
  .home-collection:active,
  .home-theme-trigger:active,
  .home-theme-picker:has(.home-theme-popover:not([hidden])) .home-theme-trigger,
  .home-theme-option.clay-card:active,
  .home-weak-practice:active {
    transform: none;
  }

  .home-update-button:active {
    transform: none;
  }

  .home-gear,
  .home-collection,
  .home-theme-trigger,
  .home-theme-option.clay-card,
  .home-weak-practice {
    transition: opacity var(--clay-motion-reduced-fade) ease;
  }
}
