:root {
  /* 介面：暖色系 */
  --ui-bg: #f3ebe0;
  --ui-panel: #e8dccb;
  --ui-line: #d6c6b0;
  --ui-ink: #4a3626;
  --ui-ink-soft: #7a6450;
  --ui-accent: #b5653a;
  --ui-accent-ink: #ffffff;
  /* 拼圖桌面：預設深灰 */
  --board-bg: #3a3c40;
  --toolbar-size: 84px;
  --tray-cell: 86px;   /* 零件盤每格大小；盤子固定兩排 */
  --tray-w: calc(var(--tray-cell) * 2 + 1px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--ui-bg);
  color: var(--ui-ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

[hidden] { display: none !important; }

button { font: inherit; color: inherit; }

.btn {
  border: none;
  border-radius: 14px;
  background: var(--ui-accent);
  color: var(--ui-accent-ink);
  font-size: 20px;
  padding: 14px 28px;
  cursor: pointer;
}
.btn:active { transform: scale(0.97); }

.btn.ghost { background: transparent; color: inherit; border: 1px solid currentColor; }
.btn.danger { background: #a8432f; }
.visually-hidden { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- 首頁 ---------- */
.home, .page {
  min-height: 100%;
  padding: max(28px, env(safe-area-inset-top)) max(32px, env(safe-area-inset-right)) 48px max(32px, env(safe-area-inset-left));
}
.home { display: flex; flex-direction: column; align-items: center; }
.title {
  font-size: 52px;
  font-weight: 600;
  margin: 12px 0 28px;
  letter-spacing: 0.15em;
  padding: 0 24px;
  transition: color 0.3s;
}
.title.pressing { color: var(--ui-accent); transition: color 3s linear; }
.home-actions { display: flex; gap: 20px; margin-bottom: 40px; }
.big-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  border: 1px solid var(--ui-line);
  background: var(--ui-panel);
  border-radius: 20px;
  padding: 22px 36px;
  font-size: 24px;
  cursor: pointer;
}
.big-btn svg { width: 32px; height: 32px; }
.big-btn.primary { background: var(--ui-accent); border-color: var(--ui-accent); color: var(--ui-accent-ink); }
.big-btn:active { transform: scale(0.97); }
.section-title { align-self: stretch; max-width: 900px; width: 100%; margin: 0 auto 12px; font-size: 22px; font-weight: 500; color: var(--ui-ink-soft); }
.empty-hint { color: var(--ui-ink-soft); font-size: 18px; text-align: center; margin: 24px 0; }
.playing-list { width: 100%; max-width: 900px; display: grid; grid-template-columns: repeat(auto-fill, minmax(380px, 1fr)); gap: 14px; }
.playing-item {
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--ui-panel);
  border: 1px solid var(--ui-line);
  border-radius: 16px;
  padding: 12px;
  cursor: pointer;
  text-align: left;
}
.playing-item:active { transform: scale(0.99); }
.playing-item img { width: 120px; height: 90px; object-fit: contain; border-radius: 8px; flex: none; }
.playing-info { flex: 1; min-width: 0; }
.playing-info b { display: block; font-size: 20px; font-weight: 500; margin-bottom: 4px; }
.playing-info span { display: block; font-size: 15px; color: var(--ui-ink-soft); line-height: 1.5; }
.give-up { border: none; background: transparent; color: var(--ui-ink-soft); font-size: 15px; padding: 10px 12px; border-radius: 10px; cursor: pointer; flex: none; }
.give-up:active { background: var(--ui-line); }

/* ---------- 相簿、管理 ---------- */
.bar { display: flex; align-items: center; gap: 16px; margin-bottom: 24px; }
.bar h2 { margin: 0; font-size: 26px; font-weight: 500; }
.back { display: flex; align-items: center; gap: 4px; border: none; background: var(--ui-panel); border-radius: 12px; padding: 10px 16px 10px 8px; font-size: 18px; cursor: pointer; }
.back svg { width: 24px; height: 24px; }
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.photo-grid.small { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
.photo-card { border: none; padding: 0; background: transparent; cursor: pointer; text-align: left; position: relative; }
.photo-card img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 12px; display: block; background: var(--ui-panel); }
.photo-card span { display: block; font-size: 15px; color: var(--ui-ink-soft); margin-top: 6px; }
.photo-card .dot { position: absolute; top: 8px; right: 8px; width: 14px; height: 14px; border-radius: 50%; border: 2px solid #fff; }
.dot.new { background: #4f9a5b; }
.dot.playing { background: var(--ui-accent); }
.dot.done { background: #8d8d8d; }
.admin-stats { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 20px; }
.stat { background: var(--ui-panel); border-radius: 14px; padding: 12px 20px; min-width: 110px; }
.stat small { display: block; color: var(--ui-ink-soft); font-size: 14px; }
.stat b { font-size: 28px; font-weight: 500; }
.admin-actions { display: flex; align-items: center; gap: 16px; margin-bottom: 8px; }
.admin-hint { color: var(--ui-ink-soft); margin: 8px 0 16px; }

/* ---------- 全螢幕看照片 ---------- */
.viewer { position: fixed; inset: 0; z-index: 50; background: #14100c; display: flex; align-items: center; justify-content: center; touch-action: none; }
.viewer img { max-width: 100%; max-height: calc(100% - 90px); object-fit: contain; }
.viewer-bar { position: absolute; left: 0; right: 0; bottom: 0; display: flex; align-items: center; justify-content: center; gap: 20px; padding: 16px 24px max(16px, env(safe-area-inset-bottom)); color: #fff; font-size: 18px; }
.viewer-bar #viewer-info { margin-right: auto; color: #d8cfc4; }
.viewer-nav { position: absolute; top: 50%; transform: translateY(-50%); width: 64px; height: 96px; border: none; background: rgba(0, 0, 0, 0.25); color: #fff; border-radius: 14px; cursor: pointer; }
.viewer-nav svg { width: 40px; height: 40px; }
.viewer-nav.prev { left: 12px; }
.viewer-nav.next { right: 12px; }

/* ---------- 對話框 ---------- */
.dialog { position: fixed; inset: 0; z-index: 60; background: rgba(30, 22, 14, 0.5); display: flex; align-items: center; justify-content: center; padding: 24px; }
.dialog-box { background: var(--ui-bg); border-radius: 22px; padding: 28px; width: min(560px, 100%); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25); }
.dialog-box h3 { margin: 0 0 8px; font-size: 24px; font-weight: 500; }
.dialog-box p { margin: 0 0 20px; color: var(--ui-ink-soft); font-size: 17px; line-height: 1.5; }
.dialog-box p:empty { display: none; }
.dialog-options { display: flex; flex-wrap: wrap; gap: 12px; justify-content: flex-end; }
.dialog-options.grid { display: grid; grid-template-columns: repeat(4, 1fr); }
.dialog-options.grid .btn { padding: 18px 0; font-size: 22px; background: var(--ui-panel); color: var(--ui-ink); }
.dialog-options.grid .btn.cancel { grid-column: 1 / -1; background: transparent; border: 1px solid var(--ui-line); font-size: 18px; padding: 12px; }

/* ---------- 設定面板 ---------- */
.settings-box { width: min(600px, 100%); }
.settings-box h3 { margin-bottom: 20px; }
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 0; border-top: 1px solid var(--ui-line); }
.set-row.column { flex-direction: column; align-items: stretch; }
.set-label { font-size: 19px; }
.seg { display: flex; background: var(--ui-panel); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { border: none; background: transparent; border-radius: 9px; padding: 10px 22px; font-size: 18px; cursor: pointer; min-width: 76px; }
.seg button.on { background: var(--ui-accent); color: var(--ui-accent-ink); }
.swatches { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px; }
.swatch { border: 2px solid transparent; background: var(--ui-panel); border-radius: 14px; padding: 8px 4px 6px; display: flex; flex-direction: column; align-items: center; gap: 6px; cursor: pointer; font-size: 15px; }
.swatch i { width: 100%; max-width: 72px; height: 44px; border-radius: 8px; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15); }
.swatch.on { border-color: var(--ui-accent); }
.settings-box .dialog-options { margin-top: 12px; }

/* ---------- 遊戲畫面 ---------- */
.game {
  position: fixed;
  inset: 0;
  display: grid;
  /* 橫放：工具列｜拼圖區｜零件盤 */
  grid-template-columns: var(--toolbar-size) minmax(0, 1fr) var(--tray-w);
  grid-template-rows: minmax(0, 1fr);
  grid-template-areas: "tool board tray";
  touch-action: none;
}
.game.swap { grid-template-columns: var(--tray-w) minmax(0, 1fr) var(--toolbar-size); grid-template-areas: "tray board tool"; }
/* 收起零件盤：拼圖區變寬，接近 4:3 */
.game.tray-hidden { grid-template-columns: var(--toolbar-size) minmax(0, 1fr); grid-template-areas: "tool board"; }
.game.swap.tray-hidden { grid-template-columns: minmax(0, 1fr) var(--toolbar-size); grid-template-areas: "board tool"; }
.game.tray-hidden .tray-wrap { display: none; }
/* 零件盤在左邊時：分隔線換邊，「收起盤」圖示左右相反 */
.game.swap .toolbar { border-right: none; border-left: 1px solid var(--ui-line); }
.game.swap .tray-wrap { border-left: none; border-right: 1px solid var(--ui-line); }
.game.swap .tool[data-act="tray"] svg { transform: scaleX(-1); }
.game.tray-hidden .tool[data-act="edge"],
.game.tray-hidden .tool[data-act="inner"] { opacity: 0.35; }

/* 只支援橫放：觸控裝置直拿時蓋上提示（網頁 App 無法強制鎖定方向） */
.rotate-hint {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--ui-bg);
  color: var(--ui-ink);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 24px;
  font-size: 26px;
  text-align: center;
  padding: 24px;
}
.rotate-hint svg { width: 120px; height: 120px; fill: none; stroke: var(--ui-accent); stroke-width: 1.4; stroke-linecap: round; stroke-linejoin: round; }
@media (orientation: portrait) and (pointer: coarse) {
  .rotate-hint { display: flex; }
}

.toolbar {
  grid-area: tool;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: max(10px, env(safe-area-inset-top)) 0 10px;
  background: var(--ui-panel);
  border-right: 1px solid var(--ui-line);
  z-index: 2;
}
.tool {
  width: 72px;
  height: 72px;
  border: none;
  border-radius: 14px;
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  cursor: pointer;
  color: var(--ui-ink);
  font-size: 13px;
}
.tool svg { width: 30px; height: 30px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tool:active { background: var(--ui-line); }
.tool.on { background: var(--ui-accent); color: var(--ui-accent-ink); }

.board-wrap, .tray-wrap { position: relative; overflow: hidden; min-width: 0; min-height: 0; }
.board-wrap { grid-area: board; background: var(--board-bg); }
.tray-wrap { grid-area: tray; background: var(--ui-panel); border-left: 1px solid var(--ui-line); }
.board-wrap canvas, .tray-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

.overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 3; }

/* ---------- 完成畫面 ---------- */
.complete {
  position: absolute;
  inset: 0;
  z-index: 10;
  background: rgba(20, 16, 12, 0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 24px;
  animation: fadein 0.8s ease;
}
.complete-photo { max-width: 100%; max-height: calc(100% - 100px); border-radius: 6px; }
.complete-bar { display: flex; align-items: center; gap: 24px; color: #fff; font-size: 26px; }
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
