/* === CSS Reset === */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* === 全域基礎樣式 === */
html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  background-color: var(--color-background);
  font-family: var(--font-text);
  color: var(--color-text);
  line-height: 1.6;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

img,
picture,
video,
svg {
  display: block;
  max-width: 100%;
}

/* === 標題字體 === */
h1,
h2,
h3 {
  font-family: var(--font-display);
  font-weight: 700;
  line-height: 1.2;
}

h1 { font-size: 2rem; }
h2 { font-size: 1.5rem; }
h3 { font-size: 1.25rem; }

/* === Apple 風格按鈕 === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  padding: 12px 24px;
  border-radius: var(--radius-md);
  font-family: var(--font-text);
  font-size: 15px;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  text-decoration: none;
}

.btn:active {
  transform: scale(0.97);
}

.btn:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
}

.btn-primary {
  background: var(--color-primary);
  color: var(--color-white);
}

.btn-primary:hover {
  opacity: 0.9;
}

.btn-secondary {
  background: var(--color-surface);
  color: var(--color-text);
}

.btn-secondary:hover {
  background: #ececee;
}

.btn-cta {
  background: var(--color-cta);
  color: var(--color-white);
}

.btn-cta:hover {
  opacity: 0.9;
}

/* === Bento Grid === */
.bento-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.bento-item {
  border-radius: var(--radius-lg);
  padding: 18px 16px;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s;
  border: none;
  text-align: left;
  font-family: var(--font-text);
  background: var(--color-surface);
}

.bento-item:hover {
  transform: scale(0.98);
}

.bento-item:active {
  transform: scale(0.96);
}

.bento-item:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
}

.bento-item.wide {
  grid-column: span 2;
}

.bento-item.tall {
  grid-row: span 2;
}

.bento-icon {
  font-size: 28px;
  margin-bottom: 6px;
  line-height: 1;
}

.bento-label {
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text);
  line-height: 1.3;
}

.bento-sub {
  font-size: 13px;
  color: rgba(0, 0, 0, 0.45);
  margin-top: 2px;
  line-height: 1.3;
}

.bento-watermark {
  position: absolute;
  bottom: -6px;
  right: 10px;
  font-size: 42px;
  font-weight: 900;
  color: rgba(0, 0, 0, 0.06);
  letter-spacing: -0.03em;
  pointer-events: none;
  line-height: 1;
}

/* 漸層背景 */
.bento-pink { background: var(--gradient-pink); }
.bento-green { background: var(--gradient-green); }
.bento-yellow { background: var(--gradient-yellow); }
.bento-purple { background: var(--gradient-purple); }
.bento-blue { background: var(--gradient-blue); }
.bento-orange { background: var(--gradient-orange); }
.bento-gray { background: var(--color-surface); }

/* === Fade-in 動畫（純 CSS） === */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.fade-in {
  animation: fadeIn 300ms ease-out both;
}

.fade-in:nth-child(2) { animation-delay: 60ms; }
.fade-in:nth-child(3) { animation-delay: 120ms; }
.fade-in:nth-child(4) { animation-delay: 180ms; }
.fade-in:nth-child(5) { animation-delay: 240ms; }
.fade-in:nth-child(6) { animation-delay: 300ms; }
.fade-in:nth-child(7) { animation-delay: 360ms; }
.fade-in:nth-child(8) { animation-delay: 420ms; }
.fade-in:nth-child(9) { animation-delay: 480ms; }

/* === 基本 Utilities === */
.text-center {
  text-align: center;
}

.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.gap-md {
  gap: var(--space-md);
}

/* === 頁面容器 === */
.page-container {
  min-height: 100dvh;
  padding: var(--space-lg);
  max-width: 820px;
  margin: 0 auto;
}

/* === 首頁 === */
.home-container {
  min-height: 100dvh;
  padding: var(--space-lg);
  max-width: 820px;
  margin: 0 auto;
}

.home-title {
  font-family: var(--font-display);
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--color-text);
  margin-bottom: 16px;
}

.home-title span {
  color: var(--color-primary);
}

/* === 上次列印捷徑（Bento 版） === */
.last-printed-bento {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
}

.last-printed-label {
  font-size: 15px;
  color: var(--color-text);
  display: flex;
  align-items: center;
  gap: var(--space-xs);
}

.last-printed-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.last-printed-btn {
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  background: rgba(0, 0, 0, 0.06);
  border: none;
  border-radius: 50px;
  cursor: pointer;
  color: var(--color-text);
  transition: all 0.2s;
}

.last-printed-btn:hover {
  background: rgba(0, 0, 0, 0.1);
}

/* === 頁面標題列 === */
.page-header {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  margin-bottom: var(--space-xl);
}

.page-title {
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 700;
  color: var(--color-text);
  letter-spacing: -0.02em;
}

/* === 返回按鈕 === */
.back-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  background: var(--color-surface);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
  color: var(--color-text);
  flex-shrink: 0;
}

.back-btn:hover {
  background: #ececee;
}

.back-btn:active {
  transform: scale(0.95);
}

.back-btn:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
}

/* === 配置頁 Layout === */
.config-layout {
  display: flex;
  gap: var(--space-xl);
  align-items: flex-start;
}

.config-panel {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-xl);
}

.config-section {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.config-label {
  font-family: var(--font-display);
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text);
}

/* === 主題選擇器（pill 按鈕） === */
.theme-selector {
  display: flex;
  gap: var(--space-sm);
  flex-wrap: wrap;
}

.theme-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 16px;
  background: var(--color-surface);
  border: none;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.2s;
  font-family: var(--font-text);
  font-weight: 500;
}

.theme-btn:hover {
  background: #ececee;
}

.theme-btn.active {
  background: var(--color-primary);
  color: var(--color-white);
}

.theme-btn:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
}

.theme-btn-icon {
  font-size: 20px;
  line-height: 1;
}

.theme-btn-name {
  font-size: 14px;
  color: inherit;
}

/* === 難度選擇器（pill 按鈕） === */
.difficulty-selector {
  display: flex;
  gap: var(--space-sm);
}

.difficulty-btn {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: 10px 16px;
  background: var(--color-surface);
  border: none;
  border-radius: 50px;
  cursor: pointer;
  transition: all 0.2s;
  font-family: var(--font-text);
  font-weight: 500;
}

.difficulty-btn:hover {
  background: #ececee;
}

.difficulty-btn.active {
  background: var(--diff-color);
  color: var(--color-white);
}

.difficulty-btn:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
}

.difficulty-btn-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--diff-color);
}

.difficulty-btn.active .difficulty-btn-dot {
  background: var(--color-white);
}

.difficulty-btn-name {
  font-size: 15px;
  color: inherit;
}

/* === 預覽框 === */
.preview-frame {
  flex: 0 0 280px;
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: var(--color-surface);
  border: 1px solid rgba(0, 0, 0, 0.06);
}

.preview-frame .ws-preview-container {
  padding: 0;
  min-height: auto;
  background: none;
}

.preview-frame .worksheet {
  transform-origin: top left;
  transform: scale(0.353);
  width: 793px;
  height: 1122px;
  border-radius: 0;
}

.preview-frame {
  height: 396px;
}

/* === 列印按鈕 === */
.print-btn {
  background: var(--color-cta);
  color: var(--color-white);
  font-size: 16px;
  font-weight: 600;
  padding: var(--space-md) var(--space-xl);
  gap: var(--space-sm);
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.print-btn:hover {
  opacity: 0.9;
}

.print-btn:active {
  transform: scale(0.97);
}

.print-btn:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
}

.print-btn svg {
  width: 24px;
  height: 24px;
}

/* === 列印容器 === */
#print-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 9999;
}

#print-container .worksheet {
  border-radius: 0;
  box-shadow: none;
  width: 100%;
  height: auto;
}

/* === 響應式：小螢幕 === */
@media screen and (max-width: 640px) {
  .home-title {
    font-size: 24px;
  }

  .bento-grid {
    gap: 8px;
  }

  .bento-item {
    padding: 14px 12px;
  }

  .bento-watermark {
    font-size: 32px;
  }

  /* 配置頁改為上下排列 */
  .config-layout {
    flex-direction: column;
  }

  .preview-frame {
    flex: none;
    width: 100%;
    max-width: 320px;
    align-self: center;
    height: auto;
    aspect-ratio: 793 / 1122;
  }

  .preview-frame .worksheet {
    transform: scale(calc(min(320px, 100vw - 48px) / 793));
  }
}

/* === 互動模式容器 === */
.play-mode {
  position: relative;
  width: 100%;
  min-height: 100dvh;
}

.play-mode > * {
  pointer-events: auto;
}

/* === 兒童鎖 === */
.parent-lock {
  position: fixed;
  top: 12px;
  right: 12px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.05);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  overflow: hidden;
  cursor: pointer;
  border: none;
  -webkit-tap-highlight-color: transparent;
}

.parent-lock svg {
  width: 20px;
  height: 20px;
  opacity: 0.4;
}

.lock-progress {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 3px;
  width: 0%;
  background: var(--color-cta);
}

/* === 提示光暈 === */
.hint-glow {
  box-shadow: 0 0 20px 8px rgba(236, 72, 153, 0.6) !important;
  animation: hintPulse 500ms ease-in-out 3;
}

@keyframes hintPulse {
  0%, 100% { box-shadow: 0 0 20px 8px rgba(236, 72, 153, 0.6); }
  50% { box-shadow: 0 0 30px 12px rgba(236, 72, 153, 0.8); }
}

/* === 離線畫面 === */
.offline-splash {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  gap: var(--space-lg);
  text-align: center;
  padding: var(--space-xl);
}

.offline-splash h2 {
  color: var(--color-text);
  font-family: var(--font-display);
}

.offline-splash p {
  color: var(--color-text-secondary);
}

.offline-splash svg {
  max-width: 200px;
}

/* === 更新 banner === */
.update-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: var(--color-primary);
  color: white;
  padding: 12px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 200;
  font-family: var(--font-text);
  animation: fadeIn 300ms ease-out;
}

.update-banner .btn-secondary {
  padding: var(--space-xs) var(--space-md);
  font-size: 14px;
  min-height: 36px;
}

.update-banner .dismiss-btn {
  margin-left: auto;
  background: none;
  border: none;
  color: white;
  font-size: 18px;
  cursor: pointer;
  padding: 4px 8px;
  -webkit-tap-highlight-color: transparent;
}

/* === 列印確認浮動卡片 === */
.print-confirm {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 150;
  animation: fadeIn 300ms ease-out;
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  padding: var(--space-lg);
  background: var(--color-white);
  border-radius: var(--radius-lg);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.12);
}

.print-confirm p {
  font-family: var(--font-display);
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text);
}

/* === 無障礙：減少動態偏好 === */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  .hint-glow {
    animation: none !important;
  }
}

/* === 列印時隱藏非列印元素 === */
@media print {
  .parent-lock,
  .update-banner,
  .print-confirm {
    display: none !important;
  }
}
