:root {
  --paper: #ffffff;
  --chrome: rgba(255, 255, 255, 0.92);
  --border: rgba(20, 20, 30, 0.10);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06), 0 6px 20px rgba(0, 0, 0, 0.08);
  --text: #1d1d1f;
  --muted: #6e6e73;
  --hover: rgba(0, 0, 0, 0.05);
  --accent: #2f74e0;
  --accent-ink: #ffffff;
  --radius: 14px;
  --ib: 44px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft JhengHei UI", "PingFang TC", sans-serif;
  font-size: 14px;
  color: var(--text);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: var(--paper);
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  overscroll-behavior: none;
}
button, input, label { font: inherit; color: inherit; }
button { border: 0; background: none; cursor: pointer; touch-action: manipulation; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- 畫布 ---------- */
/* 尺寸由 JS 依 innerWidth/innerHeight 設定；不用 100vh（平板網址列會讓 100vh 比可見區高，筆跡就往下偏） */
#paint, #tmpl, #ink, #live, #fx { position: fixed; left: 0; top: 0; width: 100%; height: 100%; display: block; }
#paint { z-index: 1; pointer-events: none; }
/* 範本用相乘疊在著色層上：白底不洗淡顏色、線條浮在顏色上 */
#tmpl { z-index: 3; pointer-events: none; mix-blend-mode: multiply; }
#ink { z-index: 4; }
#live { z-index: 5; pointer-events: none; }
#fx { z-index: 5; pointer-events: none; }
.empty { z-index: 6; }   /* 工具列、選單等浮層的 z-index 都在 10 以上，見各自的規則 */

.layers { display: flex; gap: 2px; padding: 2px; margin-left: 4px; border-radius: 10px; background: #f2f2f5; }
.layers button {
  height: 36px; padding: 0 10px; border-radius: 8px; display: inline-flex; align-items: center; gap: 5px;
  font-weight: 600; color: var(--muted); white-space: nowrap;
}
.layers button svg { width: 18px; height: 18px; }
.layers button.on { background: #fff; color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
.layers button[data-layer="color"].on { color: #e0393e; }
/* 圖層上的眼睛：顯示／隱藏；隱藏時名稱變淡、眼睛打斜線 */
.layers .eye { display: inline-grid; place-items: center; width: 24px; height: 24px; margin: 0 -4px 0 2px; border-radius: 6px; color: #a1a1a6; }
.layers .eye:hover { background: var(--hover); color: var(--text); }
.layers .eye svg { width: 16px; height: 16px; }
.layers .eye .slash { display: none; }
.layers .hid .eye .slash { display: inline; }
.layers .hid > span, .layers .hid > svg { opacity: 0.4; }
.layers .hid .eye { color: #e0393e; }
.layers .lyr-tmpl { padding-left: 6px; }
.layers .lyr-tmpl .eye { margin: 0 2px 0 -2px; }
#ink { touch-action: none; cursor: crosshair; }
body.erasing #ink { cursor: none; }
/* 橡皮擦的範圍圈：白圈＋深色虛線，深淺底色都看得到 */
#eraseRing {
  position: fixed; left: 0; top: 0; z-index: 6; pointer-events: none; display: none;
  border-radius: 50%; border: 2px dashed #1d1d1f; box-shadow: 0 0 0 1.5px #fff, inset 0 0 0 1.5px #fff;
  background: rgba(255, 255, 255, 0.18);
}
#eraseRing.show { display: block; }

.empty {
  position: fixed; inset: 0; display: grid; place-items: center; pointer-events: none;
  color: #b4b4ba; text-align: center; line-height: 1.8; font-size: 15px;
}
.empty b { color: #8e8e93; font-weight: 600; }
.empty[hidden] { display: none; }

/* ---------- 浮動工具列 ---------- */
.bar {
  position: fixed; top: max(12px, env(safe-area-inset-top)); z-index: 10;
  display: flex; align-items: center; gap: 4px;
}
.bar-left { left: 12px; }
.bar-right { right: 12px; }
.bar-center { left: 50%; transform: translateX(-50%); }
.bar-center, .tmpl-chip, .bar-left > .ib, .bar-right > .ib {
  background: var(--chrome); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.bar-center { padding: 4px; }
.bar-left, .bar-right { gap: 8px; }

.ib {
  width: var(--ib); height: var(--ib); border-radius: 10px;
  display: inline-grid; place-items: center; color: var(--text);
}
.ib:hover { background: var(--hover); }
.ib.on { background: var(--text); color: #fff; }
.ib:disabled { opacity: 0.3; cursor: default; background: none; }
.ib.sm { width: 32px; height: 32px; border-radius: 8px; }
.ib.sm svg { width: 16px; height: 16px; }
.ib.armed { background: #e0393e; color: #fff; }

.brush-btn { width: auto; padding: 0 12px 0 10px; display: inline-flex; align-items: center; gap: 6px; font-weight: 600; white-space: nowrap; }

.brush-pop {
  position: fixed; z-index: 20; top: 68px; left: 50%; transform: translate(-50%, -6px);
  width: min(720px, calc(100vw - 24px)); padding: 8px;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px;
  max-height: calc(100vh - 160px); overflow-y: auto;
  background: var(--chrome); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  opacity: 0; pointer-events: none; transition: opacity 0.15s, transform 0.15s;
}
.brush-pop.open { opacity: 1; pointer-events: auto; transform: translate(-50%, 0); }
.brush-item {
  display: flex; flex-direction: column; align-items: stretch; gap: 4px; padding: 6px 6px 8px;
  border-radius: 10px; text-align: left;
}
.brush-item:hover { background: var(--hover); }
.brush-head { grid-column: 1 / -1; font-size: 13px; font-weight: 700; padding: 8px 6px 2px; border-top: 1px solid var(--border); margin-top: 2px; }
.brush-item.on { background: #e9f1fd; box-shadow: inset 0 0 0 1.5px var(--accent); }
.brush-item canvas { width: 100%; height: 44px; display: block; }
.brush-item span { font-size: 13px; font-weight: 600; padding-left: 4px; }
.brush-item kbd { font: inherit; font-size: 11px; color: var(--muted); margin-left: 6px; font-weight: 500; }

.sep { width: 1px; height: 24px; background: var(--border); margin: 0 4px; }

.swatches { display: flex; gap: 2px; padding: 0 2px; }
.sw {
  width: 32px; height: 32px; border-radius: 50%; position: relative;
  display: grid; place-items: center;
}
.sw::after {
  content: ""; width: 20px; height: 20px; border-radius: 50%; background: var(--c);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}
.sw.on::before {
  content: ""; position: absolute; inset: 3px; border-radius: 50%; border: 2px solid var(--c);
}
.palette-btn {
  height: 36px; padding: 0 10px 0 8px; margin-left: 2px; border-radius: 10px;
  display: inline-flex; align-items: center; gap: 6px; color: var(--text);
}
.palette-btn:hover { background: var(--hover); }
.palette-btn::after {   /* 目前顏色 */
  content: ""; width: 14px; height: 14px; border-radius: 4px; background: var(--c, #1d1d1f);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15);
}

/* 色票面板：格子大，小朋友好點 */
.color-pop {
  position: fixed; z-index: 20; top: 68px; left: 50%; transform: translate(-50%, -6px);
  width: min(480px, calc(100vw - 24px)); padding: 12px;
  background: var(--chrome); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  opacity: 0; pointer-events: none; transition: opacity 0.15s, transform 0.15s;
}
.color-pop.open { opacity: 1; pointer-events: auto; transform: translate(-50%, 0); }
.pal-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 8px; }
.pal {
  aspect-ratio: 1; border-radius: 12px; background: var(--c);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12); transition: transform 0.1s;
}
.pal:active { transform: scale(0.9); }
.pal.on { box-shadow: 0 0 0 3px #fff, 0 0 0 5px var(--accent); }
.pal-custom {
  position: relative; display: flex; align-items: center; gap: 8px; margin-top: 12px;
  height: 40px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--border); background: #fff;
  font-weight: 600; cursor: pointer;
}
.pal-custom input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.pal-rainbow { width: 20px; height: 20px; border-radius: 50%; background: conic-gradient(#e0393e, #f0c020, #2a9d5c, #2f74e0, #7a4fd6, #e0393e); }

.size { display: flex; align-items: center; gap: 8px; padding: 0 8px; }
.depth { display: flex; align-items: center; gap: 6px; padding: 0 8px 0 4px; color: var(--muted); }
.depth[hidden] { display: none; }
.depth input[type=range] { width: 90px; }
.alpha input[type=range] { width: 70px; }
.alpha.on { color: var(--accent); }
.dot { width: 22px; height: 22px; display: grid; place-items: center; }
.dot::after { content: ""; width: var(--d, 8px); height: var(--d, 8px); border-radius: 50%; background: var(--dc, #1d1d1f); }

input[type=range] { -webkit-appearance: none; appearance: none; width: 110px; height: 20px; background: none; cursor: pointer; }
input[type=range]::-webkit-slider-runnable-track { height: 4px; border-radius: 4px; background: #e5e5ea; }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 18px; height: 18px; margin-top: -7px; border-radius: 50%; background: #fff; box-shadow: 0 0 0 1px var(--border), 0 1px 4px rgba(0, 0, 0, 0.2); }
input[type=range]::-moz-range-track { height: 4px; border-radius: 4px; background: #e5e5ea; }
input[type=range]::-moz-range-thumb { width: 18px; height: 18px; border: 0; border-radius: 50%; background: #fff; box-shadow: 0 0 0 1px var(--border), 0 1px 4px rgba(0, 0, 0, 0.2); }

.tmpl-chip { display: flex; align-items: center; gap: 4px; height: 44px; padding: 0 6px 0 14px; }
.tmpl-chip[hidden] { display: none; }
.tmpl-chip span { max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; margin-right: 4px; }
.tmpl-chip input[type=range] { width: 80px; }
.tmpl-chip .ib.off { color: var(--muted); }

.btn {
  height: 40px; padding: 0 14px; border-radius: 10px; display: inline-flex; align-items: center; gap: 6px;
  background: #fff; border: 1px solid var(--border); font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: #f5f5f7; }
.btn svg { width: 18px; height: 18px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); box-shadow: var(--shadow); height: 44px; }
.btn.primary:hover { background: #2766c8; }
.btn:disabled { opacity: 0.4; cursor: default; }

/* ---------- 抽屜 ---------- */
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; z-index: 30; width: min(340px, 88vw);
  background: #fff; border-right: 1px solid var(--border); box-shadow: 8px 0 30px rgba(0, 0, 0, 0.08);
  transform: translateX(-102%); transition: transform 0.22s ease;
  display: flex; flex-direction: column;
}
.drawer.open { transform: none; }
.drawer header { display: flex; align-items: center; justify-content: space-between; padding: 16px 12px 8px 20px; }
.drawer h2 { margin: 0; font-size: 18px; }
.drawer-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 0 16px; }
.drawer-actions .btn { justify-content: center; }
.drawer-tip { margin: 8px 16px 4px; color: var(--muted); font-size: 12px; }
.tmpl-grid {
  flex: 1; overflow-y: auto; padding: 8px 16px 24px;
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px; align-content: start; touch-action: pan-y;
}
.tcard { position: relative; display: flex; flex-direction: column; gap: 6px; text-align: left; }
.tcard .thumb {
  aspect-ratio: 4 / 3; border-radius: 10px; border: 1px solid var(--border); background: #fafafa;
  display: block; overflow: hidden; padding: 8px; width: 100%;
}
.tcard .thumb img { display: block; width: 100%; height: 100%; object-fit: contain; }
.tcard.on .thumb { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.tcard .tname { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tcard .tname em { font-style: normal; font-weight: 500; font-size: 11px; color: var(--muted); background: #f2f2f5; border-radius: 6px; padding: 1px 6px; margin-left: 6px; }
.tcard .del {
  position: absolute; top: 6px; right: 6px; width: 26px; height: 26px; border-radius: 8px;
  background: rgba(255, 255, 255, 0.9); border: 1px solid var(--border); display: grid; place-items: center;
  opacity: 0; transition: opacity 0.15s;
}
.tcard .del svg { width: 14px; height: 14px; }
.tcard:hover .del, .tcard .del.armed { opacity: 1; }
.tcard .del.armed { background: #e0393e; color: #fff; border-color: #e0393e; }
@media (hover: none) { .tcard .del { opacity: 1; } }
.tmpl-grid .none { grid-column: 1 / -1; color: var(--muted); text-align: center; padding: 40px 0; line-height: 1.8; }

.scrim { position: fixed; inset: 0; z-index: 25; background: rgba(0, 0, 0, 0.12); opacity: 0; pointer-events: none; transition: opacity 0.2s; }
.scrim.show { opacity: 1; pointer-events: auto; }

/* ---------- 對話框 / 提示 ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 16px; padding: 20px; width: min(360px, 90vw);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.18);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.2); }
dialog h3 { margin: 0 0 12px; font-size: 17px; }
dialog input {
  width: 100%; height: 42px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--border);
  font-size: 15px; outline: none; user-select: text;
}
dialog input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(47, 116, 224, 0.15); }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); z-index: 40;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #1d1d1f; color: #fff; padding: 10px 16px; border-radius: 10px; font-weight: 500;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.drop-hint {
  position: fixed; inset: 12px; z-index: 50; border: 2px dashed var(--accent); border-radius: 20px;
  background: rgba(47, 116, 224, 0.06); display: none; place-items: center;
  font-size: 18px; font-weight: 600; color: var(--accent); pointer-events: none;
}
.drop-hint.show { display: grid; }

/* ---------- 分頁：描圖／光影 ---------- */
.tabs {
  display: flex; gap: 2px; padding: 3px; border-radius: var(--radius);
  background: var(--chrome); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border); box-shadow: var(--shadow);
}
.tabs button {
  height: 38px; padding: 0 12px; border-radius: 10px; display: inline-flex; align-items: center; gap: 6px;
  font-weight: 600; color: var(--muted); white-space: nowrap;
}
.tabs button svg { width: 18px; height: 18px; }
.tabs button.on { background: var(--text); color: #fff; }

/* 光影分頁開著時，描圖的畫布與工具都收起來 */
/* 重播模式（?play=）：只留畫布和一行說明 */
#playInfo { display: none; }
body.playing > :not(canvas, #playInfo, script) { display: none !important; }
body.playing #playInfo {
  display: block; position: fixed; left: 50%; bottom: 16px; transform: translateX(-50%);
  padding: 6px 14px; border-radius: 999px; background: rgba(255,255,255,.85);
  font-size: 14px; color: #555; pointer-events: none; white-space: nowrap;
}
body.studio-on :is(#paint, #tmpl, #ink, #live, #fx, #empty, #btnLib, #tmplChip, #toolbar, .bar-right, #colorPop, #brushPop) { display: none !important; }
.studio { position: fixed; inset: 0; z-index: 1; background: #e4e4e8; }
.studio canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.studio canvas.grab { cursor: grab; }
.studio canvas.sun-hover { cursor: pointer; }
.studio-tip {
  position: absolute; left: 50%; bottom: max(76px, calc(env(safe-area-inset-bottom) + 64px)); transform: translateX(-50%);
  padding: 6px 14px; border-radius: 999px; background: rgba(255, 255, 255, 0.8); color: var(--muted);
  font-size: 13px; white-space: nowrap; pointer-events: none; transition: opacity .6s;
}
.studio-tip b { color: #d98a00; }
.studio-tip.fade { opacity: 0; }
.studio-bar {
  top: auto; bottom: max(12px, env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
  padding: 4px; background: var(--chrome); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.studio-bar .tabs { box-shadow: none; border: 0; background: #f2f2f5; padding: 2px; border-radius: 10px; }
.studio-bar .tabs button { height: 36px; border-radius: 8px; }
.studio-bar .tabs button.on { background: #fff; color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
.studio-bar .ib[aria-pressed="true"] { background: var(--text); color: #fff; }
.studio-bar .btn.primary { margin-left: 4px; }

/* ---------- 平板與窄螢幕：工具列移到底部（頂端放不下範本名稱＋工具列＋右側按鈕） ---------- */
@media (max-width: 1720px) {   /* Tab S9 Ultra 橫放約 1480px，頂端放不下分頁＋範本名稱＋工具列 */
  .bar-center { top: auto; bottom: max(12px, env(safe-area-inset-bottom)); max-width: calc(100vw - 24px); overflow-x: auto; }
  .brush-pop, .color-pop { top: auto; bottom: 76px; transform: translate(-50%, 6px); }
}
/* 再窄（直立平板、手機）才把文字標籤收成只剩圖示 */
@media (max-width: 900px) {
  .palette-btn svg { display: none; }
  .layers button span { display: none; }
  #tabs button span { display: none; }
  .studio-tip { font-size: 12px; }
  .brush-btn span { display: none; }
  .size input[type=range] { width: 80px; }
  .btn.primary span { display: none; }
  .btn.primary { width: 44px; padding: 0; justify-content: center; }
}
