:root {
  --bg: #0e0f12;
  --stage: #121317;
  --surface: #191a1f;
  --surface-2: #22242a;
  --line: rgba(255, 255, 255, 0.08);
  --line-2: rgba(255, 255, 255, 0.14);
  --text: #f3f3f5;
  --muted: #9b9ea7;
  --accent: #0a84ff;
  --accent-text: #fff;
  --thumb: #f3f3f5;
  --track: rgba(255, 255, 255, 0.16);
  --danger: #ff6b6b;
  --radius: 14px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f2f2f5;
    --stage: #e9e9ee;
    --surface: #ffffff;
    --surface-2: #f0f0f3;
    --line: rgba(0, 0, 0, 0.08);
    --line-2: rgba(0, 0, 0, 0.14);
    --text: #16171a;
    --muted: #6c6f78;
    --thumb: #ffffff;
    --track: rgba(0, 0, 0, 0.14);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: default; opacity: 0.38; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.app { height: 100dvh; display: flex; flex-direction: column; }

/* ---------- top bar ---------- */
.topbar {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  gap: 8px; padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--bg); z-index: 5;
}
.tb-side { display: flex; align-items: center; gap: 6px; min-width: 0; }
.tb-side.right { justify-content: flex-end; }
.brand { font-weight: 700; letter-spacing: 0.01em; font-size: 16px; }
.chip {
  display: inline-flex; align-items: center; gap: 8px; height: 36px; padding: 0 12px 0 4px;
  border-radius: 999px; border: 1px solid var(--line); background: var(--surface); min-width: 0;
}
.chip:hover { background: var(--surface-2); }
.lib-thumb { width: 26px; height: 28px; border-radius: 7px; background: var(--surface-2) center/cover no-repeat; flex: none; margin-left: 2px; }
.lib-label { white-space: nowrap; }
.icon-btn {
  width: 36px; height: 36px; display: inline-grid; place-items: center; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface);
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); }
.export-btn {
  height: 36px; padding: 0 18px; border-radius: 999px; border: 0; background: var(--accent); color: var(--accent-text);
  font-weight: 600; margin-left: 4px; white-space: nowrap; flex: none;
}
.export-btn:hover:not(:disabled) { filter: brightness(1.08); }

/* ---------- workspace ---------- */
.workspace { flex: 1; min-height: 0; display: grid; grid-template-columns: 1fr 380px; }
.stage { position: relative; background: var(--stage); display: grid; place-items: center; min-height: 0; overflow: hidden; padding: 20px; }
.frame {
  position: relative; aspect-ratio: 9 / 16; height: 100%; max-width: 100%; max-height: 100%;
  border-radius: 18px; overflow: hidden; outline: none; touch-action: none;
  background: var(--surface); box-shadow: 0 0 0 1px var(--line), 0 18px 50px rgba(0, 0, 0, 0.35);
}
.frame.has-image { background: transparent; box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35); cursor: grab; }
.frame.focus-mode { box-shadow: 0 0 0 1px var(--line-2); }
.frame.dragging { cursor: grabbing; }
.frame.over-sticker { cursor: move; }
#view { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.guide { position: absolute; background: rgba(255, 255, 255, 0.9); box-shadow: 0 0 0 0.5px rgba(0, 0, 0, 0.35); pointer-events: none; display: none; }
.guide-v { top: 0; bottom: 0; width: 1px; }
.guide-h { left: 0; right: 0; height: 1px; }
.guide.on { display: block; }

.empty { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 24px; text-align: center; }
.frame.has-image .empty { display: none; }
.empty-add {
  display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 26px 34px;
  border-radius: 20px; border: 1px dashed var(--line-2); background: var(--surface-2); color: var(--text); font-weight: 600;
}
.empty-add:hover { border-color: var(--muted); }
.empty-alt { display: flex; gap: 8px; color: var(--muted); }
.link-btn { border: 0; background: none; color: var(--text); text-decoration: underline; text-underline-offset: 3px; padding: 4px; }
.empty-hint { color: var(--muted); font-size: 13px; max-width: 260px; margin: 0; }
.frame.drop { box-shadow: 0 0 0 2px var(--accent); }

.zoom-btn {
  position: absolute; right: 10px; bottom: 10px; width: 34px; height: 34px; border-radius: 999px; border: 0;
  display: none; place-items: center; color: #fff; background: rgba(20, 20, 24, 0.45);
  -webkit-backdrop-filter: blur(14px) saturate(1.4); backdrop-filter: blur(14px) saturate(1.4);
}
.frame.has-image .zoom-btn { display: grid; }
.frame.focus-mode .zoom-btn { display: none; }
.focus-bar {
  position: absolute; left: 50%; bottom: 12px; transform: translateX(-50%); display: none; gap: 6px; padding: 5px;
  border-radius: 999px; background: rgba(20, 20, 24, 0.5); -webkit-backdrop-filter: blur(16px) saturate(1.4); backdrop-filter: blur(16px) saturate(1.4);
}
.frame.focus-mode .focus-bar { display: flex; }
.focus-bar button { border: 0; border-radius: 999px; padding: 6px 12px; background: rgba(255, 255, 255, 0.14); color: #fff; font-size: 13px; white-space: nowrap; }
.focus-bar button.on { background: #fff; color: #111; }

/* ---------- panel ---------- */
.panel { border-left: 1px solid var(--line); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin: 14px 16px 6px; padding: 4px; border-radius: 12px; background: var(--surface-2); }
.tab { border: 0; background: none; height: 34px; border-radius: 9px; color: var(--muted); font-weight: 600; }
.tab[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); }
.tab-body { display: none; flex: 1; overflow: auto; padding: 10px 16px 28px; overscroll-behavior: contain; }
.tab-body.on { display: block; }

.field-label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 6px; }
textarea#titleInput {
  width: 100%; resize: none; border-radius: 12px; border: 1px solid var(--line-2); background: var(--surface-2); color: var(--text);
  font: 600 17px/1.4 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", sans-serif; padding: 10px 12px; overflow-y: auto;
}
textarea#titleInput:focus { outline: none; border-color: var(--muted); }

.slider { margin: 16px 0 4px; }
.slider .row { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.slider label { font-size: 14px; }
.slider output { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.slider .track { display: flex; align-items: center; gap: 8px; }
.slider .end { color: var(--muted); font-size: 12px; width: 14px; text-align: center; flex: none; }
.slider.disabled { opacity: 0.4; }
input[type="range"] { -webkit-appearance: none; appearance: none; width: 100%; height: 28px; background: transparent; margin: 0; }
input[type="range"]::-webkit-slider-runnable-track { height: 4px; border-radius: 4px; background: linear-gradient(to right, var(--text) var(--p, 50%), var(--track) var(--p, 50%)); }
input[type="range"]::-moz-range-track { height: 4px; border-radius: 4px; background: var(--track); }
input[type="range"]::-moz-range-progress { height: 4px; border-radius: 4px; background: var(--text); }
input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 22px; height: 22px; margin-top: -9px; border-radius: 50%; background: var(--thumb); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35), 0 0 0 0.5px rgba(0, 0, 0, 0.2); }
input[type="range"]::-moz-range-thumb { width: 22px; height: 22px; border: 0; border-radius: 50%; background: var(--thumb); box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35); }

.btn-row { display: flex; gap: 8px; margin: 10px 0; }
.btn-row.center { justify-content: center; }
.btn-row .btn { flex: 1; }
.btn {
  min-height: 40px; padding: 0 14px; border-radius: 11px; border: 1px solid var(--line-2); background: var(--surface-2); font-weight: 500;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn.subtle { background: none; width: 100%; margin-top: 14px; color: var(--muted); }
.btn.primary { background: var(--accent); border-color: transparent; color: #fff; font-weight: 600; }
.hint { color: var(--muted); font-size: 13px; margin: 14px 0 0; }
.hint.tight { margin-top: 2px; font-size: 12px; }

.adv { margin-top: 18px; border-top: 1px solid var(--line); }
.adv summary {
  list-style: none; display: flex; justify-content: space-between; align-items: center; cursor: pointer;
  padding: 14px 2px; font-weight: 600;
}
.adv summary::-webkit-details-marker { display: none; }
.adv summary::after { content: ""; width: 8px; height: 8px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg); margin-right: 4px; transition: transform 0.2s; }
.adv[open] summary::after { transform: rotate(-135deg); }

.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; border-radius: 12px; background: var(--surface-2); }
.segmented button { border: 0; background: none; height: 32px; border-radius: 9px; color: var(--muted); font-weight: 600; }
.segmented button[aria-checked="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25); }

.styles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 14px; }
.style-card { border: 0; background: none; padding: 0; display: flex; flex-direction: column; gap: 6px; align-items: center; color: var(--muted); font-size: 13px; }
.style-card canvas { width: 100%; aspect-ratio: 3 / 2; border-radius: 10px; background: var(--surface-2); box-shadow: 0 0 0 1px var(--line); }
.style-card.on { color: var(--text); font-weight: 600; }
.style-card.on canvas { box-shadow: 0 0 0 2px var(--text); }

/* ---------- dialogs ---------- */
dialog.sheet {
  border: 1px solid var(--line-2); border-radius: 18px; background: var(--surface); color: var(--text);
  padding: 0 18px 18px; width: min(560px, calc(100vw - 32px)); max-height: calc(100dvh - 48px);
}
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.5); }
.sheet-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 0 8px; position: sticky; top: 0; background: var(--surface); z-index: 1; }
.sheet-head h2 { font-size: 17px; margin: 0; }
.lib-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); gap: 12px; }
.lib-item { position: relative; display: flex; flex-direction: column; gap: 6px; }
.lib-item .open { border: 0; padding: 0; background: var(--surface-2); border-radius: 12px; aspect-ratio: 9 / 16; overflow: hidden; box-shadow: 0 0 0 1px var(--line); }
.lib-item.current .open { box-shadow: 0 0 0 2px var(--text); }
.lib-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.lib-item .meta { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; align-items: center; gap: 4px; }
.lib-item .meta span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-item .del { border: 0; background: none; color: var(--danger); font-size: 12px; padding: 2px 0; flex: none; }
.lib-empty { color: var(--muted); grid-column: 1 / -1; padding: 24px 0; text-align: center; }

.export-body { display: grid; grid-template-columns: 180px 1fr; gap: 18px; align-items: start; }
.export-preview { position: relative; aspect-ratio: 9 / 16; border-radius: 12px; overflow: hidden; background: var(--surface-2); }
.export-preview img { width: 100%; height: 100%; display: block; object-fit: cover; }
.export-actions { display: flex; flex-direction: column; gap: 8px; }
.export-actions .btn.subtle { margin-top: 4px; }
.export-meta { color: var(--muted); margin: 0 0 4px; font-size: 13px; }
.spinner { position: absolute; inset: 0; display: none; place-items: center; }
.spinner.on { display: grid; }
.spinner.on::after { content: ""; width: 26px; height: 26px; border-radius: 50%; border: 3px solid var(--line-2); border-top-color: var(--text); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#cameraVideo { width: 100%; border-radius: 12px; background: #000; max-height: 60dvh; }

.viewer { position: fixed; inset: 0; background: #000; z-index: 50; display: grid; place-items: center; overflow: hidden; touch-action: none; }
.viewer[hidden] { display: none; }
.viewer img { max-width: 100%; max-height: 100%; transform-origin: 0 0; user-select: none; -webkit-user-drag: none; }
.viewer-hint { position: absolute; bottom: max(16px, env(safe-area-inset-bottom)); left: 0; right: 0; text-align: center; color: rgba(255, 255, 255, 0.55); font-size: 13px; margin: 0; pointer-events: none; }

.toast {
  position: fixed; left: 50%; top: 70px; transform: translate(-50%, -12px); opacity: 0; pointer-events: none;
  padding: 10px 16px; border-radius: 999px; background: rgba(30, 31, 36, 0.92); color: #fff; font-size: 14px; z-index: 60;
  transition: opacity 0.2s, transform 0.2s; box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3); white-space: nowrap;
}
.toast.on { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; cursor: default; }
.toast.clickable { cursor: pointer; }

.sec-title { display: none; font-size: 12px; font-weight: 700; letter-spacing: 0.08em; color: var(--muted); margin: 6px 0 4px; }
.shortcuts { display: none; margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); }
.shortcuts div { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin: 6px 0; }
.shortcuts span { margin-right: 8px; }
kbd { font: 600 11px/1 -apple-system, BlinkMacSystemFont, sans-serif; padding: 4px 6px; border-radius: 6px; background: var(--surface-2); border: 1px solid var(--line-2); color: var(--text); }

/* ---------- medium window: one side panel, every section visible ---------- */
@media (min-width: 861px) {
  .tabs { display: none; }
  .panel { overflow-y: auto; }
  .tab-body { display: block; flex: none; overflow: visible; padding-bottom: 8px; }
  .tab-body + .tab-body { border-top: 1px solid var(--line); padding-top: 14px; }
  .sec-title { display: block; }
  .shortcuts { display: block; }
}

/* ---------- landscape desktop: controls left, cover centre, glass right ---------- */
@media (min-width: 1180px) {
  .workspace {
    grid-template-columns: minmax(300px, 340px) 1fr minmax(300px, 340px);
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas: "image stage glass" "text stage glass";
  }
  .stage { grid-area: stage; padding: 28px; }
  .panel { display: contents; }
  .tab-body { background: var(--surface); padding: 14px 20px 20px; overflow-y: auto; }
  .tab-body + .tab-body { border-top: 0; padding-top: 14px; }
  .tab-body[data-body="image"] { grid-area: image; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); }
  .tab-body[data-body="text"] { grid-area: text; border-right: 1px solid var(--line); min-height: 0; }
  .tab-body[data-body="glass"] { grid-area: glass; border-left: 1px solid var(--line); min-height: 0; }
  .tab-body[data-body="image"] .slider { margin-top: 10px; }
  .tab-body[data-body="image"] .hint { margin-top: 8px; }
  .styles { gap: 10px; }
}

/* ---------- narrow (phone / small window) ---------- */
@media (max-width: 860px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; }
  .stage { padding: 12px; }
  .panel { border-left: 0; border-top: 1px solid var(--line); border-radius: 18px 18px 0 0; max-height: 46dvh; padding-bottom: env(safe-area-inset-bottom); }
  .lib-label, .brand { display: none; }
  .topbar { display: flex; justify-content: space-between; }
  .chip { padding-right: 8px; }
  .topbar { padding-left: 12px; padding-right: 12px; }
  .export-body { grid-template-columns: 1fr; }
  .export-preview { width: 160px; margin: 0 auto; }
}
@media (prefers-reduced-motion: reduce) {
  .toast, .adv summary::after { transition: none; }
}
