:root {
  --bg: #f4f3ef; --card: #ffffff; --ink: #15171a; --mute: #7b7f86; --line: #e6e4de;
  --acc: #11a866; --acc-ink: #ffffff; --acc-soft: #dff5e9; --danger: #e5484d;
  --cam: #0f1112; --cam2: #1d2023;
  --st: env(safe-area-inset-top, 0px); --sb: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--cam); color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; overscroll-behavior: none; }
button { font: inherit; color: inherit; border: 0; background: none; padding: 0; cursor: pointer; }
svg { width: 22px; height: 22px; fill: currentColor; flex: none; }
input { font: inherit; }
.hidden { display: none !important; }
.flex1 { flex: 1; }

.screen { position: fixed; inset: 0; display: none; flex-direction: column; }
.screen.active { display: flex; }

/* ---------- 相機 ---------- */
.cam { background: var(--cam); color: #fff; padding-top: var(--st); }
.cam-top { height: 48px; display: flex; align-items: center; justify-content: space-between; padding: 0 10px; flex: none; }
.brand { font-weight: 700; letter-spacing: .12em; font-size: 15px; opacity: .85; }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 50%; }
.icon-btn:active { background: rgba(255,255,255,.12); }
.icon-btn.sm { width: 40px; height: 40px; }
.icon-btn:disabled { opacity: .3; }
.viewfinder { position: relative; flex: 1; min-height: 0; overflow: hidden; background: #000; }
.viewfinder video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
.grid { position: absolute; pointer-events: none; }
.grid i { position: absolute; background: rgba(255,255,255,.45); transition: background .2s, box-shadow .2s; }
.grid i:nth-child(1), .grid i:nth-child(2) { top: 0; bottom: 0; width: 1px; }
.grid i:nth-child(1) { left: 33.333%; } .grid i:nth-child(2) { left: 66.666%; }
.grid i:nth-child(3), .grid i:nth-child(4) { left: 0; right: 0; height: 1px; }
.grid i:nth-child(3) { top: 33.333%; } .grid i:nth-child(4) { top: 66.666%; }
.grid.ok i { background: #3ddc84; box-shadow: 0 0 0 1px rgba(61,220,132,.5); }

.level-pill { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); display: flex; align-items: center; gap: 12px;
  background: rgba(30,32,34,.82); border-radius: 22px;
  padding: 9px 18px 9px 10px; min-width: 250px; color: #fff; transition: background .25s, color .25s; }
.level-pill.ok { background: #3ddc84; color: #0b2a17; }
.lv-ring { position: relative; width: 52px; height: 52px; border-radius: 50%; border: 2px solid rgba(255,255,255,.75); flex: none; }
.level-pill.ok .lv-ring { border-color: #0b2a17; }
.lv-target { position: absolute; left: 50%; top: 50%; width: 18px; height: 18px; margin: -9px; border-radius: 50%; border: 1.5px solid rgba(255,255,255,.7); }
.level-pill.ok .lv-target { border-color: #0b2a17; }
.lv-dot { position: absolute; left: 50%; top: 50%; width: 12px; height: 12px; margin: -6px; border-radius: 50%; background: #fff; }
.level-pill.ok .lv-dot { background: #0b2a17; }
.level-pill.off .lv-dot { opacity: .3; }
.lv-text { display: flex; flex-direction: column; align-items: flex-start; text-align: left; }
.lv-text small { font-size: 12px; opacity: .8; min-height: 0; }
.lv-text b { font-size: 18px; font-weight: 700; white-space: nowrap; }
.vf-bottom { position: absolute; left: 0; right: 0; bottom: 14px; display: flex; justify-content: center; }
.chip-dark { display: flex; align-items: center; gap: 8px; background: rgba(30,32,34,.82);
  border-radius: 20px; padding: 9px 16px; font-size: 15px; font-weight: 600; max-width: 80%; }
.chip-dark span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-dark svg { width: 18px; height: 18px; }
.chip-dark.empty { color: #ffd166; }
.flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.flash.go { animation: flash .35s ease-out; }
@keyframes flash { 0% { opacity: .85; } 100% { opacity: 0; } }
.cam-msg { position: absolute; inset: 0; display: grid; place-items: center; padding: 30px; text-align: center; line-height: 1.6; color: #ddd; background: #000; }

.cam-controls { flex: none; display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 16px 22px 10px; }
.thumb { width: 58px; height: 58px; border-radius: 14px; overflow: hidden; background: var(--cam2); border: 2px solid rgba(255,255,255,.2); justify-self: start; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb img:not([src]) { visibility: hidden; }
.shutter { width: 78px; height: 78px; border-radius: 50%; border: 4px solid #fff; display: grid; place-items: center; }
.shutter span { width: 62px; height: 62px; border-radius: 50%; background: #fff; transition: transform .1s, background .2s; }
.shutter:active span { transform: scale(.9); }
.shutter.ok span { background: #3ddc84; }
.shutter.busy span { background: #888; }
.lens { justify-self: end; display: flex; gap: 8px; }
.lens button { width: 46px; height: 46px; border-radius: 50%; background: var(--cam2); font-weight: 700; font-size: 14px; }
.lens button.on { background: #fff; color: #000; }
.cam-tools { flex: none; display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; padding: 6px 16px calc(12px + var(--sb)); }
.cam-tools button { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 0; border-radius: 16px; background: var(--cam2); font-size: 14px; }
.cam-tools button:active { background: #2b2f33; }

@media (orientation: landscape) {
  .cam { flex-direction: row; padding-top: 0; padding-left: env(safe-area-inset-left, 0px); padding-right: env(safe-area-inset-right, 0px); }
  .cam-top { flex-direction: column; height: auto; width: 54px; padding: 10px 0; }
  .brand { writing-mode: vertical-rl; }
  .cam-controls { grid-template-columns: none; grid-template-rows: 1fr auto 1fr; padding: 14px 10px; }
  .cam-controls .thumb { justify-self: center; align-self: end; }
  .lens { justify-self: center; align-self: start; flex-direction: column; }
  .cam-tools { grid-template-columns: none; grid-auto-rows: 1fr; padding: 10px 10px 10px 0; width: 104px; }
}

/* ---------- 共用按鈕 ---------- */
.txt-btn { font-size: 17px; padding: 8px 6px; }
.pri-btn { background: var(--acc); color: var(--acc-ink); font-weight: 700; border-radius: 22px; padding: 10px 18px; font-size: 16px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.pri-btn:active { filter: brightness(.92); }
.pri-btn:disabled { opacity: .45; }
.sec-btn { background: #ebe9e3; color: var(--ink); font-weight: 600; border-radius: 22px; padding: 10px 18px; font-size: 16px; }
.sec-btn:disabled { opacity: .45; }
.wide { width: 100%; padding: 15px; font-size: 17px; border-radius: 18px; }
.pill-btn { display: inline-flex; align-items: center; gap: 6px; background: #ebe9e3; border-radius: 18px; padding: 8px 14px; font-size: 15px; font-weight: 600; }
.pill-btn svg { width: 18px; height: 18px; }
.pill-btn.on { background: var(--acc); color: #fff; }
.pill-btn.strong { background: var(--ink); color: #fff; }
.seg { display: flex; background: #e9e7e1; border-radius: 22px; padding: 3px; }
.seg button { padding: 7px 18px; border-radius: 19px; font-weight: 600; font-size: 16px; color: #555; }
.seg button.on { background: var(--acc); color: #fff; }
.seg.small button { padding: 6px 10px; font-size: 14px; flex: 1; }

/* ---------- 修圖 ---------- */
.ed { background: var(--bg); padding-top: var(--st); }
.ed-top { display: flex; align-items: center; justify-content: space-between; padding: 6px 12px; flex: none; }
.ed-bar { display: flex; align-items: center; gap: 6px; padding: 2px 10px 8px; flex: none; }
.stage { position: relative; flex: 1; min-height: 0; background: #2a2b2d; overflow: hidden; touch-action: none; }
.canvas-wrap { position: absolute; }
.canvas-wrap canvas { width: 100%; height: 100%; display: block; }
.overlay { position: absolute; inset: -40px; width: calc(100% + 80px); height: calc(100% + 80px); overflow: visible; touch-action: none; }
.wm-preview { position: absolute; pointer-events: none; }
.badge { position: absolute; left: 50%; top: 10px; transform: translateX(-50%); background: rgba(20,20,22,.82); color: #fff; padding: 8px 16px; border-radius: 18px;
  font-size: 15px; font-weight: 600; display: flex; gap: 8px; align-items: center; white-space: nowrap; pointer-events: none; }
.badge svg { width: 18px; height: 18px; }
.badge.bottom { top: auto; bottom: 10px; font-weight: 500; font-size: 14px; }
.busy { position: absolute; inset: 0; display: flex; flex-direction: column; gap: 12px; align-items: center; justify-content: center; color: #fff; background: rgba(0,0,0,.45); font-size: 15px; }
.spin { width: 34px; height: 34px; border: 3px solid rgba(255,255,255,.3); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.panel { flex: none; background: var(--bg); max-height: 40vh; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 12px 16px 12px; border-top: 1px solid var(--line); }
.panel h4 { margin: 10px 0 4px; color: var(--mute); font-size: 14px; font-weight: 600; }
.panel h4:first-child { margin-top: 0; }
.panel p { margin: 6px 0; color: var(--mute); font-size: 14px; line-height: 1.55; }
.auto-card h3 { display: flex; align-items: center; gap: 8px; margin: 2px 0 6px; font-size: 19px; }
.auto-card .pri-btn { margin-top: 10px; }
.sl { display: grid; grid-template-columns: 72px 1fr 74px; align-items: center; gap: 8px; height: 44px; }
.sl label { font-size: 16px; }
.sl output { text-align: right; font-variant-numeric: tabular-nums; font-size: 15px; color: var(--ink); }
.sl output.zero { color: #aaa; }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 30px; background: transparent; margin: 0; touch-action: pan-y; }
input[type=range]::-webkit-slider-runnable-track { height: 5px; border-radius: 3px; background: linear-gradient(to right, var(--ink) var(--p, 50%), #dcdad4 var(--p, 50%)); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 30px; height: 26px; border-radius: 13px; background: #fff; margin-top: -10.5px; box-shadow: 0 1px 4px rgba(0,0,0,.25), 0 0 0 .5px rgba(0,0,0,.08); }
input[type=range]::-moz-range-track { height: 5px; border-radius: 3px; background: #dcdad4; }
input[type=range]::-moz-range-progress { height: 5px; border-radius: 3px; background: var(--ink); }
input[type=range]::-moz-range-thumb { width: 28px; height: 24px; border-radius: 12px; background: #fff; border: 0; box-shadow: 0 1px 4px rgba(0,0,0,.25); }
.link-danger { color: var(--danger); font-size: 16px; padding: 10px 0; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; display: inline-flex; align-items: center; gap: 6px; padding: 9px 14px; border-radius: 20px; background: #e9e7e1; font-weight: 600; font-size: 15px; }
.chip svg { width: 18px; height: 18px; }
.chip.on { background: var(--acc); color: #fff; }
.chip.v { background: #d6e8fb; color: #124a80; } .chip.v.on { background: #3b8beb; color: #fff; }
.chip.h { background: #fde3cc; color: #7a3d08; } .chip.h.on { background: #f08a2e; color: #fff; }
.chip .x { width: 16px; height: 16px; opacity: .8; margin-left: 4px; }
.row-btns { display: flex; justify-content: space-between; gap: 10px; margin-top: 10px; }
.tabs { flex: none; display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; padding: 6px 8px calc(6px + var(--sb)); background: var(--bg); border-top: 1px solid var(--line); }
.tabs button { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 8px 0; border-radius: 16px; font-size: 13px; font-weight: 600; }
.tabs button.on { background: var(--acc); color: #fff; }
.tabs.dim { opacity: .35; pointer-events: none; }
.ed:not(.manual) .panel { padding-bottom: calc(14px + var(--sb)); }

/* overlay 圖形 */
.ov-handle { fill: #1b1c1e; stroke: #fff; stroke-width: 2.5; }
.ov-handle.v { stroke: #3b8beb; } .ov-handle.h { stroke: #f08a2e; }
.ov-line { stroke-width: 3; stroke-linecap: round; }
.ov-line.v { stroke: #3b8beb; } .ov-line.h { stroke: #f08a2e; }
.ov-hit { stroke: transparent; stroke-width: 30; stroke-linecap: round; }
.ov-quad { fill: none; stroke: #fff; stroke-width: 1.6; stroke-dasharray: 7 6; }
.ov-crop { fill: none; stroke: rgba(255,255,255,.95); stroke-width: 1.5; }
.ov-dim { fill: rgba(0,0,0,.5); fill-rule: evenodd; }
.ov-poly { fill: rgba(80,200,255,.18); stroke: #fff; stroke-width: 2.5; stroke-linejoin: round; }
.ov-poly.sel { fill: rgba(80,200,255,.28); }
.ov-pt { fill: #1b1c1e; stroke: #5ec8ff; stroke-width: 2.5; }
.ov-pt.first { stroke: #3ddc84; stroke-width: 3.5; }
.ov-label { fill: #fff; font-size: 15px; font-weight: 700; paint-order: stroke; stroke: rgba(0,0,0,.6); stroke-width: 3px; }
.ov-cropcorner { stroke: #fff; stroke-width: 4; fill: none; stroke-linecap: square; }
.ov-third { stroke: rgba(255,255,255,.35); stroke-width: 1; }

/* ---------- 相簿 ---------- */
.gal { background: var(--bg); padding-top: var(--st); }
.gal-top { display: flex; align-items: center; justify-content: space-between; padding: 4px 8px; flex: none; }
.gal-title { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; max-width: 60%; }
.gal-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gal-title svg { width: 20px; height: 20px; }
.gal-grid { flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px; padding: 3px; align-content: start; }
.gal-item { position: relative; aspect-ratio: 3/4; background: #ddd; overflow: hidden; }
.gal-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.gal-item .tag { position: absolute; left: 6px; bottom: 6px; background: rgba(17,168,102,.92); color: #fff; font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 10px; }
.gal-item .sel { position: absolute; right: 6px; top: 6px; width: 24px; height: 24px; border-radius: 50%; border: 2px solid #fff; background: rgba(0,0,0,.25); display: none; }
.selecting .gal-item .sel { display: block; }
.gal-item.picked .sel { background: var(--acc); }
.gal-item.picked .sel::after { content: ''; position: absolute; left: 6px; top: 2px; width: 6px; height: 11px; border: solid #fff; border-width: 0 2.5px 2.5px 0; transform: rotate(45deg); }
.gal-item.picked img { opacity: .75; }
.gal-empty { flex: 1; display: grid; place-items: center; color: var(--mute); }
.gal-actions { flex: none; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 10px 12px calc(10px + var(--sb)); border-top: 1px solid var(--line); }
.gal-actions button { display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 14px; font-weight: 600; padding: 6px; }
.gal-actions button:disabled { opacity: .35; }
.danger { color: var(--danger); }

/* ---------- 底部面板 ---------- */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); opacity: 0; pointer-events: none; transition: opacity .25s; z-index: 20; }
.scrim.on { opacity: 1; pointer-events: auto; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; max-height: calc(100% - 30px - var(--st)); background: var(--bg); color: var(--ink); border-radius: 26px 26px 0 0;
  transform: translateY(105%); transition: transform .3s cubic-bezier(.2,.8,.2,1); z-index: 21; display: flex; flex-direction: column; }
.sheet.on { transform: none; }
.sh-head { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 16px 16px 10px; flex: none; }
.sh-head h3 { margin: 0; font-size: 19px; }
.sh-head > :last-child { justify-self: end; }
.sh-body { overflow-y: auto; padding: 4px 16px calc(20px + var(--sb)); -webkit-overflow-scrolling: touch; }
.card { background: var(--card); border-radius: 18px; margin: 10px 0; }
.card.row, .card .row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 16px; min-height: 52px; }
.card .row + .row { border-top: 1px solid var(--line); }
.card .row.col { flex-direction: column; align-items: stretch; }
.hint { color: var(--mute); font-size: 14px; line-height: 1.55; margin: 8px 4px; }
.hint.center { text-align: center; }
.ck-item { display: grid; grid-template-columns: 30px 30px 1fr; gap: 12px; align-items: start; padding: 14px 16px; width: 100%; text-align: left; }
.ck-item + .ck-item { border-top: 1px solid var(--line); }
.ck-item .box { width: 26px; height: 26px; border-radius: 50%; border: 2px solid #b9b7b0; display: grid; place-items: center; color: #fff; margin-top: 1px; }
.ck-item.done .box { background: var(--acc); border-color: var(--acc); }
.ck-item .box svg { width: 16px; height: 16px; opacity: 0; }
.ck-item.done .box svg { opacity: 1; }
.ck-item .ico { font-size: 22px; line-height: 1.2; }
.ck-item b { display: block; font-size: 17px; margin-bottom: 3px; }
.ck-item span { color: var(--mute); font-size: 14px; line-height: 1.5; }
.ck-item.done b { color: var(--mute); }
.toggle { position: relative; width: 52px; height: 32px; flex: none; }
.toggle input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; z-index: 1; }
.toggle i { position: absolute; inset: 0; border-radius: 16px; background: #d8d6d0; transition: background .2s; }
.toggle i::after { content: ''; position: absolute; left: 3px; top: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s; }
.toggle input:checked + i { background: var(--acc); }
.toggle input:checked + i::after { transform: translateX(20px); }
.toggle input:disabled + i { opacity: .4; }
.new-prop { display: flex; gap: 8px; }
.new-prop input { flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; font-size: 16px; background: #fff; -webkit-user-select: text; user-select: text; }
.prop-row { display: flex; align-items: center; gap: 12px; padding: 14px 16px; width: 100%; text-align: left; }
.prop-row + .prop-row { border-top: 1px solid var(--line); }
.prop-row .nm { flex: 1; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.prop-row .ct { color: var(--mute); font-size: 14px; }
.prop-row .ck { width: 22px; height: 22px; color: var(--acc); opacity: 0; }
.prop-row.on .ck { opacity: 1; }
.prop-row.on .nm { font-weight: 700; }
.prop-row .more { width: 34px; height: 34px; display: grid; place-items: center; color: var(--mute); font-size: 20px; border-radius: 50%; }
.wm-demo { display: grid; place-items: center; background: #e8e6e0; border-radius: 18px; padding: 10px; }
.wm-demo canvas { max-width: 100%; max-height: 38vh; border-radius: 6px; display: block; }
.ex-preview { display: grid; place-items: center; background: #e8e6e0; border-radius: 18px; padding: 10px; min-height: 120px; }
.ex-preview img { max-width: 100%; max-height: 46vh; border-radius: 6px; display: block; }
.ex-preview .ex-many { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.ex-preview .ex-many img { width: 72px; height: 96px; object-fit: cover; }
#exShare { margin-top: 6px; }
#exCamera { margin-top: 8px; }

.dialog { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 40; padding: 30px; }
.dlg-box { background: #fff; border-radius: 22px; padding: 22px 20px 16px; width: 100%; max-width: 340px; }
.dlg-box p { margin: 0 0 14px; font-size: 17px; line-height: 1.5; white-space: pre-line; }
.dlg-box input { width: 100%; border: 1px solid var(--line); border-radius: 12px; padding: 11px 12px; font-size: 16px; margin-bottom: 14px; -webkit-user-select: text; user-select: text; }
.dlg-btns { display: flex; gap: 10px; }
.dlg-btns button { flex: 1; }
.dlg-btns .pri-btn.danger-btn { background: var(--danger); }
.toast { position: fixed; left: 50%; bottom: calc(150px + var(--sb)); transform: translate(-50%, 20px); background: rgba(20,20,22,.9); color: #fff; padding: 10px 18px; border-radius: 20px;
  font-size: 15px; opacity: 0; pointer-events: none; transition: all .25s; z-index: 50; max-width: 86%; text-align: center; }
.toast.on { opacity: 1; transform: translate(-50%, 0); }

/* 效能診斷（長按「正好拍」） */
.perf { position: absolute; left: 8px; right: 8px; top: 92px; background: rgba(0,0,0,.72); color: #9effc4; font: 12px/1.5 ui-monospace, Menlo, monospace; white-space: pre-line; padding: 8px 10px; border-radius: 10px; pointer-events: none; }
