/* ==========================================================================
   ScratchArt — 魔法刮畫工作室（Tactile Midnight Craft Desk）
   專為平板與手機觸控設計：高對比、大按鈕、防誤觸、動態視窗適應
   ========================================================================== */

:root {
  /* 溫暖深胡桃木畫桌色盤 */
  --desk-bg: #1e1a17;
  --desk-surface: #2a2420;
  --desk-elevated: #362e29;
  --desk-border: rgba(255, 241, 222, 0.14);

  /* 溫潤奶油紙藝按鈕色 */
  --cream-bg: #fff8ed;
  --cream-soft: #f5ead9;
  --cream-ink: #2b211c;
  --muted-ink: #c7b8a8;

  /* 魔法高飽和強調色 */
  --accent-gold: #f6c445;
  --accent-coral: #ff6b6b;
  --accent-mint: #2ec4b6;
  --accent-sky: #4ea8de;
  --accent-berry: #ff5ca8;

  /* 觸控目標與圓角規格 */
  --target-size: 46px;
  --radius-pill: 999px;
  --radius-card: 22px;
  --radius-btn: 16px;

  --font-display: 'Baloo 2', 'Noto Sans TC', 'PingFang TC', sans-serif;
  --font-body: 'Noto Sans TC', 'PingFang TC', 'Heiti TC', sans-serif;
}

@media (pointer: coarse) {
  :root {
    --target-size: 50px;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  background-color: var(--desk-bg);
  color: var(--cream-bg);
  font-family: var(--font-body);
  -webkit-user-select: none;
  user-select: none;
  overflow-wrap: break-word;
}

/* 細緻暖色畫桌微紋理 */
body {
  background-image:
    radial-gradient(circle at 18% 12%, rgba(246, 196, 69, 0.1) 0%, transparent 45%),
    radial-gradient(circle at 84% 88%, rgba(46, 196, 182, 0.1) 0%, transparent 48%),
    linear-gradient(160deg, #211c19 0%, #191513 100%);
}

/* 無障礙視覺隱藏（供螢幕閱讀器與檔案上傳使用） */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ==========================================================================
   主容器三段配置（動態視窗高度 100dvh＋安全邊距，嚴格禁止水平與垂直爆版）
   ========================================================================== */
.app-shell {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  padding-top: max(0.45rem, env(safe-area-inset-top, 0px));
  padding-bottom: max(0.45rem, env(safe-area-inset-bottom, 0px));
  padding-left: max(0.6rem, env(safe-area-inset-left, 0px));
  padding-right: max(0.6rem, env(safe-area-inset-right, 0px));
  gap: 0.45rem;
}

/* ==========================================================================
   頂部功能列
   ========================================================================== */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.55rem;
  min-width: 0;
  max-width: 100%;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0.25rem 0.65rem 0.25rem 0.35rem;
  border-radius: var(--radius-pill);
  background: rgba(255, 248, 237, 0.07);
  border: 1px solid var(--desk-border);
}

.brand-badge {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, #ff5ca8 0%, #f6c445 55%, #2ec4b6 100%);
  color: #1f1714;
  box-shadow: 0 2px 10px rgba(246, 196, 69, 0.35);
}

.brand-badge svg {
  width: 20px;
  height: 20px;
}

.brand-title {
  font-family: var(--font-display);
  font-size: clamp(1.1rem, 0.95rem + 0.6vw, 1.38rem);
  font-weight: 800;
  letter-spacing: 0.02em;
  color: var(--cream-bg);
}

.top-toolbar {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  scroll-snap-type: x proximity;
  padding: 2px;
  min-width: 0;
  max-width: 100%;
}

.top-toolbar::-webkit-scrollbar,
.dock-toolbar::-webkit-scrollbar {
  display: none;
}

.tool-group {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-shrink: 0;
}

.toolbar-sep {
  display: inline-block;
  width: 1px;
  height: 28px;
  border-left: 1px solid rgba(255, 248, 237, 0.2);
  flex-shrink: 0;
  margin-inline: 0.1rem;
}

/* 通用按鈕基礎與焦點樣式 */
button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
  touch-action: manipulation;
}

button:focus-visible {
  outline: 3px solid var(--accent-gold);
  outline-offset: 2px;
}

.pill-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  min-inline-size: var(--target-size);
  min-block-size: var(--target-size);
  padding: 0.4rem 0.85rem;
  border-radius: var(--radius-pill);
  background: var(--desk-surface);
  color: var(--cream-bg);
  border: 1.5px solid var(--desk-border);
  font-size: 0.92rem;
  font-weight: 700;
  white-space: nowrap;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.35);
  scroll-snap-align: start;
}

@media (prefers-reduced-motion: no-preference) {
  .pill-btn,
  .dock-btn,
  .picker-card {
    transition: transform 0.14s ease, background-color 0.16s ease, border-color 0.16s ease, box-shadow 0.16s ease;
  }
}

@media (hover: hover) {
  .pill-btn:hover:not([aria-disabled="true"]) {
    background: var(--desk-elevated);
    border-color: rgba(255, 248, 237, 0.32);
  }
}

.pill-btn:active:not([aria-disabled="true"]) {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
}

/* 以 ARIA 屬性驅動切換狀態，確保視覺與無障礙狀態一致 */
.pill-btn[aria-pressed="true"] {
  background: var(--cream-bg);
  color: var(--cream-ink);
  border-color: var(--accent-gold);
  box-shadow: 0 0 0 2px rgba(246, 196, 69, 0.35), 0 3px 0 rgba(0, 0, 0, 0.4);
}

.pill-btn[aria-disabled="true"] {
  opacity: 0.42;
  cursor: not-allowed;
}

.save-btn {
  background: linear-gradient(135deg, #2ec4b6 0%, #22a699 100%);
  color: #0d2220;
  border-color: rgba(255, 255, 255, 0.35);
}

@media (hover: hover) {
  .save-btn:hover {
    background: linear-gradient(135deg, #3bd6c7 0%, #27b8aa 100%);
  }
}

.danger-btn {
  background: rgba(255, 107, 107, 0.16);
  border-color: rgba(255, 107, 107, 0.45);
  color: #ffd3d3;
  overflow: hidden;
}

.danger-btn.is-holding {
  background: rgba(255, 107, 107, 0.34);
  border-color: #ff6b6b;
  color: #ffffff;
  transform: scale(0.97);
}

/* 長按 2 秒滿版掃描進度條（適配各種按鈕寬度，兒童一眼看懂） */
.hold-fill {
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, rgba(255, 107, 107, 0.72) 0%, rgba(246, 196, 69, 0.85) 100%);
  transform-origin: left center;
  transform: scaleX(var(--hold-progress, 0));
  pointer-events: none;
  z-index: 0;
}

.danger-btn > .btn-icon,
.danger-btn > .btn-text {
  position: relative;
  z-index: 1;
}

.btn-icon {
  font-size: 1.15rem;
  line-height: 1;
}

/* ==========================================================================
   中央刮畫紙舞台（核心視覺焦點）
   ========================================================================== */
.stage-area {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 0;
  min-width: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  padding: 0.1rem 0.15rem;
}

/* 紙藝畫框外框（溫潤象牙白卡紙墊框） */
.canvas-mat {
  position: relative;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  max-width: 1220px;
  padding: clamp(5px, 0.9vw, 10px);
  border-radius: var(--radius-card);
  background: linear-gradient(145deg, #fffaf0 0%, #efe4d2 100%);
  box-shadow:
    0 14px 34px rgba(0, 0, 0, 0.52),
    0 2px 0 rgba(255, 255, 255, 0.7) inset;
  display: flex;
}

.canvas-frame {
  position: relative;
  flex: 1;
  width: 100%;
  height: 100%;
  border-radius: calc(var(--radius-card) - 8px);
  overflow: hidden;
  background: #141317;
  /* 關鍵防誤觸：完全鎖定畫布區的捲動、雙指縮放與系統手勢 */
  touch-action: none;
  box-shadow: 0 0 0 1.5px rgba(20, 19, 23, 0.35) inset;
}

.canvas-frame canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#scratch-canvas {
  cursor: crosshair;
  touch-action: none;
}

#stencil-canvas,
#particle-canvas {
  pointer-events: none;
}

/* 初次進入中央柔和提示 */
.canvas-hint {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  pointer-events: none;
  text-align: center;
  padding: 1.5rem;
  color: rgba(255, 248, 237, 0.78);
  font-size: clamp(1rem, 0.85rem + 0.9vw, 1.35rem);
  font-weight: 700;
  letter-spacing: 0.03em;
  opacity: 1;
  transition: opacity 0.45s ease;
}

.canvas-hint.is-hidden {
  opacity: 0;
}

.hint-spark {
  font-size: 2rem;
}

/* 浮動提示氣泡（Toast） */
.toast-banner {
  position: absolute;
  top: 1.15rem;
  left: 50%;
  transform: translate(-50%, -14px);
  padding: 0.55rem 1.15rem;
  border-radius: var(--radius-pill);
  background: rgba(255, 248, 237, 0.96);
  color: var(--cream-ink);
  font-size: 0.95rem;
  font-weight: 700;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.42);
  border: 2px solid var(--accent-gold);
  pointer-events: none;
  opacity: 0;
  z-index: 20;
  max-width: min(92vw, 440px);
  text-align: center;
}

@media (prefers-reduced-motion: no-preference) {
  .toast-banner {
    transition: opacity 0.22s ease, transform 0.22s ease;
  }
}

.toast-banner.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ==========================================================================
   底部觸控筆刷與底圖工具列
   ========================================================================== */
.bottom-dock {
  display: flex;
  justify-content: center;
  width: 100%;
  min-width: 0;
  max-width: 100%;
}

.dock-toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.42rem 0.6rem;
  border-radius: 22px;
  background: rgba(42, 36, 32, 0.94);
  border: 1.5px solid var(--desk-border);
  box-shadow: 0 10px 26px rgba(0, 0, 0, 0.42);
  overflow-x: auto;
  scrollbar-width: none;
  scroll-snap-type: x proximity;
  min-width: 0;
  max-width: 100%;
}

.dock-group {
  display: flex;
  align-items: center;
  gap: 0.42rem;
  flex-shrink: 0;
}

.dock-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.18rem;
  min-inline-size: 56px;
  min-block-size: 56px;
  padding: 0.35rem 0.5rem;
  border-radius: var(--radius-btn);
  background: var(--desk-elevated);
  color: var(--cream-bg);
  border: 2px solid transparent;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.38);
  scroll-snap-align: center;
}

@media (hover: hover) {
  .dock-btn:hover {
    background: #443a33;
    border-color: rgba(255, 248, 237, 0.25);
  }
}

.dock-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.38);
}

.dock-btn[aria-pressed="true"] {
  background: var(--cream-bg);
  color: var(--cream-ink);
  border-color: var(--accent-gold);
  transform: translateY(-3px);
  box-shadow:
    0 6px 14px rgba(0, 0, 0, 0.42),
    0 0 0 2px rgba(246, 196, 69, 0.4);
}

.dock-label {
  font-size: 0.78rem;
  font-weight: 700;
  line-height: 1.1;
  white-space: nowrap;
}

.dock-emoji {
  font-size: 1.3rem;
  line-height: 1;
}

/* 筆刷粗細圓點視覺提示 */
.brush-dot {
  display: block;
  border-radius: 50%;
  background: linear-gradient(135deg, #ff5ca8 0%, #f6c445 52%, #2ec4b6 100%);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
}

.dot-fine {
  width: 9px;
  height: 9px;
  margin: 6px 0;
}

.dot-medium {
  width: 16px;
  height: 16px;
  margin: 2.5px 0;
}

.dot-thick {
  width: 23px;
  height: 23px;
}

/* 底色縮圖圓徽章 */
.theme-swatch-mini {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: linear-gradient(135deg, #ff3b6b, #ffe64d, #2ec4b6, #9b5de5);
  border: 2px solid rgba(255, 255, 255, 0.85);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
}

.feature-btn {
  background: linear-gradient(180deg, #3e342e 0%, #312924 100%);
  border-color: rgba(255, 248, 237, 0.14);
}

.photo-btn.is-active {
  border-color: var(--accent-mint);
  background: rgba(46, 196, 182, 0.22);
}

/* ==========================================================================
   原生 <dialog> 挑選與存檔視窗
   ========================================================================== */
.craft-dialog {
  width: min(92vw, 540px);
  max-height: 84dvh;
  padding: 1.25rem;
  border-radius: 26px;
  border: 2px solid rgba(246, 196, 69, 0.45);
  background: #27211d;
  color: var(--cream-bg);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.68);
  overflow-y: auto;
}

.craft-dialog::backdrop {
  background: rgba(15, 12, 10, 0.68);
  backdrop-filter: blur(5px);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.dialog-header h2 {
  margin: 0;
  font-size: clamp(1.15rem, 1rem + 0.6vw, 1.4rem);
  font-weight: 800;
  text-wrap: balance;
}

.dialog-sub {
  margin: 0.35rem 0 1rem;
  color: var(--muted-ink);
  font-size: 0.92rem;
  text-wrap: pretty;
}

.dialog-close {
  display: grid;
  place-items: center;
  min-inline-size: 44px;
  min-block-size: 44px;
  border-radius: 50%;
  background: rgba(255, 248, 237, 0.1);
  color: var(--cream-bg);
  font-size: 1.1rem;
  font-weight: 800;
}

@media (hover: hover) {
  .dialog-close:hover {
    background: rgba(255, 248, 237, 0.22);
  }
}

.picker-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 0.75rem;
}

.picker-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 0.6rem;
  border-radius: 18px;
  background: var(--desk-elevated);
  border: 2px solid transparent;
  color: var(--cream-bg);
  font-weight: 700;
  font-size: 0.95rem;
  min-block-size: 98px;
}

.picker-card[aria-pressed="true"] {
  border-color: var(--accent-gold);
  background: #463a31;
  box-shadow: 0 0 0 2px rgba(246, 196, 69, 0.35);
}

.theme-swatch-big {
  width: 100%;
  height: 58px;
  border-radius: 12px;
  border: 1.5px solid rgba(255, 255, 255, 0.25);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.32);
}

.stencil-icon-big {
  font-size: 2.15rem;
  line-height: 1.1;
}

/* 存檔預覽對話框 */
.save-preview-wrap {
  margin: 0.85rem 0;
  padding: 0.5rem;
  border-radius: 18px;
  background: #fff8ed;
  display: flex;
  justify-content: center;
}

.save-preview-wrap img {
  max-inline-size: 100%;
  max-block-size: 46dvh;
  block-size: auto;
  border-radius: 12px;
  display: block;
}

.save-tip {
  margin: 0 0 1rem;
  font-size: 0.9rem;
  color: var(--muted-ink);
  line-height: 1.5;
  text-wrap: pretty;
}

.save-actions {
  display: flex;
  gap: 0.65rem;
  flex-wrap: wrap;
}

.primary-cta,
.secondary-cta {
  flex: 1;
  min-inline-size: 150px;
  min-block-size: 50px;
  padding: 0.7rem 1.1rem;
  border-radius: var(--radius-pill);
  font-size: 1rem;
  font-weight: 800;
}

.primary-cta {
  background: linear-gradient(135deg, #2ec4b6 0%, #22a699 100%);
  color: #0d2220;
}

.secondary-cta {
  background: var(--cream-bg);
  color: var(--cream-ink);
}

/* ==========================================================================
   手機與窄螢幕響應式配置（320px–720px 全按鈕完整顯示、零水平捲動、零截斷）
   ========================================================================== */
@media (max-width: 720px) {
  .app-shell {
    gap: 0.35rem;
    padding-left: max(0.45rem, env(safe-area-inset-left, 0px));
    padding-right: max(0.45rem, env(safe-area-inset-right, 0px));
  }

  .top-bar {
    width: 100%;
  }

  .brand {
    display: none;
  }

  /* 頂部 6 顆按鈕採整齊 3x2 網格，每顆皆清晰好按且完全不超出螢幕 */
  .top-toolbar {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
    gap: 0.32rem;
    overflow: visible;
    padding: 0;
  }

  .top-toolbar .tool-group {
    display: contents;
  }

  .top-toolbar .toolbar-sep,
  .dock-toolbar .toolbar-sep {
    display: none;
  }

  .pill-btn {
    width: 100%;
    min-inline-size: 0;
    min-block-size: 44px;
    padding: 0.3rem 0.4rem;
    font-size: 0.82rem;
    gap: 0.22rem;
  }

  .btn-icon {
    font-size: 1rem;
  }

  /* 底部工具列分兩排：上排 6 種筆刷、下排 3 大底圖模式，零遮擋零溢位 */
  .dock-toolbar {
    display: flex;
    flex-direction: column;
    width: 100%;
    gap: 0.35rem;
    padding: 0.38rem;
    border-radius: 18px;
    overflow: visible;
  }

  .brush-group {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    width: 100%;
    gap: 0.25rem;
  }

  .mode-group {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    width: 100%;
    gap: 0.32rem;
  }

  .dock-btn {
    width: 100%;
    min-inline-size: 0;
    min-block-size: 48px;
    padding: 0.26rem 0.2rem;
    border-radius: 13px;
  }

  .mode-group .dock-btn {
    flex-direction: row;
    gap: 0.35rem;
    min-block-size: 44px;
    padding: 0.28rem 0.45rem;
  }

  .dock-emoji {
    font-size: 1.12rem;
  }

  .dock-label {
    font-size: 0.74rem;
  }

  .mode-group .dock-label {
    font-size: 0.84rem;
  }

  .picker-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 360px) {
  .pill-btn {
    font-size: 0.74rem;
    padding: 0.25rem 0.2rem;
  }

  .dock-label {
    font-size: 0.68rem;
  }
}

/* 高對比強制色彩模式相容 */
@media (forced-colors: active) {
  .pill-btn,
  .dock-btn,
  .picker-card {
    border: 2px solid ButtonText;
  }
  .pill-btn[aria-pressed="true"],
  .dock-btn[aria-pressed="true"],
  .picker-card[aria-pressed="true"] {
    outline: 3px solid Highlight;
  }
}
