@font-face { font-family: 'BVP'; font-weight: 400; font-display: swap; src: url(fonts/be-vietnam-pro-vietnamese-400-normal.woff2) format('woff2'); unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+0300-0301, U+0303-0304, U+0308-0309, U+0323, U+0329, U+1EA0-1EF9, U+20AB; }
@font-face { font-family: 'BVP'; font-weight: 600; font-display: swap; src: url(fonts/be-vietnam-pro-vietnamese-600-normal.woff2) format('woff2'); unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+0300-0301, U+0303-0304, U+0308-0309, U+0323, U+0329, U+1EA0-1EF9, U+20AB; }
@font-face { font-family: 'BVP'; font-weight: 800; font-display: swap; src: url(fonts/be-vietnam-pro-vietnamese-800-normal.woff2) format('woff2'); unicode-range: U+0102-0103, U+0110-0111, U+0128-0129, U+0168-0169, U+01A0-01A1, U+01AF-01B0, U+0300-0301, U+0303-0304, U+0308-0309, U+0323, U+0329, U+1EA0-1EF9, U+20AB; }
@font-face { font-family: 'BVP'; font-weight: 400; font-display: swap; src: url(fonts/be-vietnam-pro-latin-400-normal.woff2) format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; }
@font-face { font-family: 'BVP'; font-weight: 600; font-display: swap; src: url(fonts/be-vietnam-pro-latin-600-normal.woff2) format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; }
@font-face { font-family: 'BVP'; font-weight: 800; font-display: swap; src: url(fonts/be-vietnam-pro-latin-800-normal.woff2) format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; }
@font-face { font-family: 'JBM'; font-weight: 500; font-display: swap; src: url(fonts/jetbrains-mono-latin-500-normal.woff2) format('woff2'); }
@font-face { font-family: 'JBM'; font-weight: 700; font-display: swap; src: url(fonts/jetbrains-mono-latin-700-normal.woff2) format('woff2'); }

:root {
  --ink: #0d0f0e;
  --ink-2: #171a18;
  --ink-3: #242824;
  --line: rgba(243, 238, 228, .14);
  --paper: #f3eee4;
  --muted: rgba(243, 238, 228, .58);
  --amber: #ffb238;
  --amber-dim: rgba(255, 178, 56, .18);
  --go: #8bf5b4;
  --danger: #ff6b5a;
  --sans: 'BVP', -apple-system, 'Helvetica Neue', sans-serif;
  --mono: 'JBM', ui-monospace, Menlo, monospace;
  --st: env(safe-area-inset-top, 0px);
  --sb: env(safe-area-inset-bottom, 0px);
  --sl: env(safe-area-inset-left, 0px);
  --sr: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--ink); color: var(--paper); font-family: var(--sans); overflow: hidden; overscroll-behavior: none; -webkit-user-select: none; user-select: none; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
.mono { font-family: var(--mono); }
[hidden] { display: none !important; }

#app { position: fixed; inset: 0; }

/* ---------- Khung ngắm ---------- */
#stage { position: absolute; top: 0; left: 0; right: var(--tray-r, 0px); bottom: var(--tray-h, 190px); background: #000; transition: bottom .25s; }
#viewport { position: absolute; inset: 0; }
#viewport.mirror { transform: scaleX(-1); }
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; }
#flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
#flash.on { animation: flash .45s ease-out; }
@keyframes flash { 0% { opacity: .85 } 100% { opacity: 0 } }

#countdown { position: absolute; left: 50%; top: 44%; transform: translate(-50%, -50%); font: 700 min(38vw, 40vh)/1 var(--mono); color: var(--amber); text-shadow: 0 4px 40px rgba(0,0,0,.6); pointer-events: none; opacity: 0; }
#countdown.tick { animation: tick .9s cubic-bezier(.2,.7,.2,1); }
@keyframes tick { 0% { opacity: 0; transform: translate(-50%, -50%) scale(1.5) } 15% { opacity: 1; transform: translate(-50%, -50%) scale(1) } 80% { opacity: 1 } 100% { opacity: 0; transform: translate(-50%, -50%) scale(.85) } }

/* ---------- HUD ---------- */
#hud { position: absolute; top: 0; left: 0; right: 0; padding: calc(var(--st) + 10px) calc(var(--sr) + 12px) 0 calc(var(--sl) + 12px); display: flex; gap: 10px; align-items: flex-start; pointer-events: none; }
#hint-wrap { flex: 1; min-width: 0; }
#hint { margin: 0; display: inline-block; max-width: 100%; padding: 9px 14px 10px; border-radius: 14px; background: rgba(13,15,14,.62); -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px); border: 1px solid var(--line); font-weight: 600; font-size: 17px; line-height: 1.3; letter-spacing: -.01em; transition: color .25s, border-color .25s; }
#hint.good { color: var(--go); border-color: rgba(139,245,180,.4); }
#hint.warn { color: var(--amber); }
#meter { position: relative; width: 58px; height: 58px; flex: none; border-radius: 50%; background: rgba(13,15,14,.62); -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px); }
#meter svg { position: absolute; inset: 4px; transform: rotate(-90deg); }
#meter .track { fill: none; stroke: var(--line); stroke-width: 3; }
#meter .bar { fill: none; stroke: var(--amber); stroke-width: 3.5; stroke-linecap: round; stroke-dasharray: 119.4; stroke-dashoffset: 119.4; transition: stroke-dashoffset .2s, stroke .25s; }
#meter.good .bar { stroke: var(--go); }
#meter-num { position: absolute; inset: 0; display: grid; place-items: center; font: 700 17px var(--mono); letter-spacing: -.04em; }
#meter.small-text #meter-num { font-size: 13px; }

.chip { display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 13px; border-radius: 17px; background: rgba(13,15,14,.6); -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px); border: 1px solid var(--line); font-size: 13.5px; font-weight: 600; white-space: nowrap; }
.chip.icon { width: 34px; padding: 0; justify-content: center; }
.chip svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.chip.ghost { pointer-events: none; }
#status-chip { position: absolute; left: 50%; transform: translateX(-50%); top: calc(var(--st) + 78px); opacity: 0; transition: opacity .3s; font-weight: 400; color: var(--muted); }
#status-chip.show { opacity: 1; }
#mode-btn i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
#mode-btn[data-mode="auto"] i { background: var(--go); box-shadow: 0 0 8px var(--go); }
#mode-btn[data-mode="prompt"] i { background: var(--amber); box-shadow: 0 0 8px var(--amber); }

/* ---------- Khay ---------- */
#tray { position: absolute; left: 0; right: 0; bottom: 0; padding: 0 var(--sr) calc(var(--sb) + 10px) var(--sl); background: var(--ink); }
.tray-handle { display: block; margin: 0 auto; width: 64px; height: 22px; display: grid; place-items: center; }
.tray-handle span { width: 36px; height: 4px; border-radius: 2px; background: rgba(243,238,228,.35); }
.tray-body { position: absolute; left: 0; right: 0; bottom: 100%; padding-top: 34px; background: linear-gradient(to top, rgba(13,15,14,.96) 0%, rgba(13,15,14,.88) 72%, rgba(13,15,14,0) 100%); transition: opacity .25s, transform .25s; }
.tray-handle { position: relative; z-index: 2; }
#tray.collapsed .tray-body { opacity: 0; transform: translateY(16px); pointer-events: none; }
#tray.collapsed .tray-handle span { background: var(--amber); }

#tpl-info { padding: 0 16px 8px; display: flex; flex-direction: column; gap: 2px; }
#tpl-name { font-weight: 800; font-size: 16px; letter-spacing: -.015em; }
#tpl-tip { font-size: 12.5px; color: var(--muted); line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

.scroller { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; padding: 0 16px; scroll-snap-type: x proximity; }
.scroller::-webkit-scrollbar { display: none; }
#cats { padding-bottom: 10px; }
#cats button { flex: none; height: 30px; padding: 0 12px; border-radius: 15px; font-size: 13px; font-weight: 600; color: var(--muted); border: 1px solid transparent; }
#cats button.on { color: var(--ink); background: var(--paper); }

#templates { padding-bottom: 12px; }
.tpl { flex: none; scroll-snap-align: start; width: 62px; display: flex; flex-direction: column; align-items: center; gap: 5px; }
.tpl .pic { width: 62px; height: 84px; border-radius: 12px; background: var(--ink-3); border: 1.5px solid transparent; overflow: hidden; display: grid; place-items: center; transition: border-color .2s, transform .2s; }
.tpl canvas { width: 62px; height: 84px; display: block; }
.tpl span { font-size: 10.5px; color: var(--muted); line-height: 1.2; text-align: center; height: 26px; overflow: hidden; }
.tpl.on .pic { border-color: var(--amber); transform: translateY(-2px); }
.tpl.on span { color: var(--paper); }

.quick { display: flex; gap: 8px; justify-content: center; padding: 2px 12px 10px; }
.controls { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 0 26px; }
#shutter { width: 76px; height: 76px; border-radius: 50%; border: 3px solid var(--paper); display: grid; place-items: center; transition: transform .1s; }
#shutter span { width: 62px; height: 62px; border-radius: 50%; background: var(--paper); transition: background .2s, transform .15s; }
#shutter:active span { transform: scale(.9); }
#shutter.ready { border-color: var(--go); }
#shutter.ready span { background: var(--go); animation: breathe 1.1s ease-in-out infinite; }
#shutter.busy span { background: var(--amber); transform: scale(.55); border-radius: 12px; }
@keyframes breathe { 50% { transform: scale(.9) } }
.thumb-btn { justify-self: start; position: relative; width: 48px; height: 48px; border-radius: 12px; background: var(--ink-3); border: 1.5px solid var(--line); overflow: visible; }
.thumb-btn img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; display: block; }
.thumb-btn img:not([src]) { visibility: hidden; }
#gallery-count { position: absolute; top: -6px; right: -6px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px; background: var(--amber); color: var(--ink); font: 700 11px/20px var(--mono); text-align: center; }
#gallery-count:empty { display: none; }
.thumb-btn.pop { animation: pop .4s ease; }
@keyframes pop { 40% { transform: scale(1.18) } }
.round-btn { justify-self: end; width: 48px; height: 48px; border-radius: 50%; background: rgba(243,238,228,.1); display: grid; place-items: center; }
.round-btn svg { width: 24px; height: 24px; fill: none; stroke: var(--paper); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Màn hình bắt đầu ---------- */
.screen { position: absolute; inset: 0; z-index: 20; }
#start { background: radial-gradient(120% 70% at 50% 0%, #2a2418 0%, var(--ink) 60%); display: grid; place-items: center; padding: calc(var(--st) + 24px) 26px calc(var(--sb) + 24px); overflow-y: auto; }
.start-inner { width: 100%; max-width: 420px; }
.brand { margin-bottom: 34px; }
.mark { position: relative; width: 58px; height: 74px; margin-bottom: 22px; }
.mark span { position: absolute; width: 16px; height: 16px; border-color: var(--amber); border-style: solid; border-width: 0; }
.mark span:nth-child(1) { top: 0; left: 0; border-top-width: 3px; border-left-width: 3px; }
.mark span:nth-child(2) { top: 0; right: 0; border-top-width: 3px; border-right-width: 3px; }
.mark span:nth-child(3) { bottom: 0; left: 0; border-bottom-width: 3px; border-left-width: 3px; }
.mark span:nth-child(4) { bottom: 0; right: 0; border-bottom-width: 3px; border-right-width: 3px; }
.mark::after { content: ''; position: absolute; left: 50%; top: 14px; width: 14px; height: 14px; margin-left: -7px; border-radius: 50%; background: var(--paper); box-shadow: 0 20px 0 -1px var(--paper), 0 30px 0 0 var(--paper); }
h1 { font-weight: 800; font-size: 46px; letter-spacing: -.045em; margin: 0; line-height: 1; }
.sub { margin: 10px 0 0; font: 500 12px var(--mono); letter-spacing: .14em; text-transform: uppercase; color: var(--amber); }
.steps { list-style: none; padding: 0; margin: 0 0 30px; counter-reset: s; display: grid; gap: 16px; }
.steps li { counter-increment: s; display: grid; grid-template-columns: 30px 1fr; font-size: 15px; line-height: 1.5; color: var(--muted); }
.steps li::before { content: '0' counter(s); font: 700 12px/24px var(--mono); color: var(--amber); }
.steps b { color: var(--paper); font-weight: 600; }
.primary { display: block; width: 100%; height: 56px; border-radius: 16px; background: var(--amber); color: var(--ink); font-weight: 800; font-size: 17px; letter-spacing: -.01em; }
.primary:active { filter: brightness(.9); }
.primary.small { width: auto; height: 40px; padding: 0 18px; border-radius: 12px; font-size: 15px; }
.primary:disabled { opacity: .5; }
.fine { font-size: 12.5px; color: var(--muted); line-height: 1.5; margin: 16px 0 0; }
.fine b { color: var(--paper); font-weight: 600; }
.warn { background: rgba(255,107,90,.12); border: 1px solid rgba(255,107,90,.4); color: #ffc2b9; padding: 12px 14px; border-radius: 12px; font-size: 14px; line-height: 1.45; margin: 0 0 16px; }

/* ---------- Sheet ---------- */
.sheet { position: absolute; inset: 0; z-index: 30; background: var(--ink); display: flex; flex-direction: column; padding-top: var(--st); animation: up .28s cubic-bezier(.2,.8,.2,1); }
@keyframes up { from { transform: translateY(30px); opacity: 0 } }
.sheet-head { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; height: 54px; padding: 0 16px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { margin: 0; font-size: 16px; font-weight: 800; }
.sheet-head > :last-child { justify-self: end; }
.link { color: var(--amber); font-weight: 600; font-size: 15px; padding: 8px 0; }
.link.danger { color: var(--danger); }
#gallery-grid { flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; align-content: start; padding: 2px; padding-bottom: calc(var(--sb) + 80px); }
.g-item { position: relative; aspect-ratio: 3/4; background: var(--ink-3); overflow: hidden; }
.g-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.g-item .check { position: absolute; right: 6px; bottom: 6px; width: 24px; height: 24px; border-radius: 50%; border: 2px solid #fff; background: rgba(0,0,0,.3); display: none; }
#gallery.selecting .g-item .check { display: block; }
.g-item.sel .check { background: var(--amber); border-color: var(--amber); }
.g-item.sel .check::after { content: ''; position: absolute; left: 6px; top: 2px; width: 6px; height: 11px; border: solid var(--ink); border-width: 0 2.5px 2.5px 0; transform: rotate(45deg); }
.g-item.sel img { opacity: .75; }
.empty { text-align: center; color: var(--muted); margin-top: 30vh; }
.sheet-foot { position: absolute; left: 0; right: 0; bottom: 0; display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 16px calc(var(--sb) + 12px); background: rgba(13,15,14,.92); -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px); border-top: 1px solid var(--line); }
#gallery-selcount { font: 500 13px var(--mono); color: var(--muted); }

#viewer { background: #000; display: grid; place-items: center; }
#viewer-img { max-width: 100%; max-height: 100%; object-fit: contain; -webkit-user-select: auto; user-select: auto; -webkit-touch-callout: default; }
.viewer-bar { position: absolute; top: 0; left: 0; right: 0; display: flex; justify-content: space-between; align-items: center; padding: calc(var(--st) + 6px) 16px 10px; background: linear-gradient(rgba(0,0,0,.7), transparent); }
#viewer-meta { font-size: 12px; color: var(--muted); }
.viewer-foot { position: absolute; left: 16px; right: 16px; bottom: calc(var(--sb) + 16px); }

.settings-body { flex: 1; overflow-y: auto; padding: 6px 16px calc(var(--sb) + 30px); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 14px; min-height: 58px; border-bottom: 1px solid var(--line); font-size: 15px; }
.row > span:first-child { display: flex; flex-direction: column; gap: 2px; }
.row small { font-size: 12px; color: var(--muted); }
.row .val { display: flex; align-items: center; gap: 10px; }
.row output { font: 700 13px var(--mono); color: var(--amber); min-width: 34px; text-align: right; }
.row select { font: 600 14px var(--sans); color: var(--paper); background: var(--ink-3); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; -webkit-appearance: none; appearance: none; }
input[type=range] { width: 130px; accent-color: var(--amber); }
.switch { -webkit-appearance: none; appearance: none; width: 50px; height: 30px; border-radius: 15px; background: var(--ink-3); position: relative; transition: background .2s; border: 1px solid var(--line); margin: 0; flex: none; }
.switch::after { content: ''; position: absolute; top: 2px; left: 2px; width: 24px; height: 24px; border-radius: 50%; background: var(--paper); transition: transform .2s; }
.switch:checked { background: var(--amber); }
.switch:checked::after { transform: translateX(20px); }
#remote-led { width: 14px; height: 14px; border-radius: 50%; background: var(--ink-3); border: 1px solid var(--line); }
#remote-led.on { background: var(--go); box-shadow: 0 0 12px var(--go); }

#toast { position: absolute; left: 50%; bottom: calc(var(--sb) + 210px); transform: translate(-50%, 10px); z-index: 50; padding: 10px 16px; border-radius: 12px; background: var(--paper); color: var(--ink); font-weight: 600; font-size: 14px; opacity: 0; transition: opacity .25s, transform .25s; pointer-events: none; max-width: 86vw; text-align: center; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Xoay ngang: khay gọn lại, đưa sang phải */
@media (orientation: landscape) and (max-height: 500px) {
  #tray { left: auto; top: 0; width: min(300px, 42vw); padding: calc(var(--st) + 8px) calc(var(--sr) + 8px) calc(var(--sb) + 8px) 0; display: flex; flex-direction: column; justify-content: flex-end; }
  .tray-body { position: static; padding-top: 0; background: none; }
  #tray.collapsed .tray-body { opacity: 1; transform: none; pointer-events: auto; }
  .tray-handle { display: none; }
  #tpl-info { display: none; }
  .controls { padding: 0 10px; }
  #hud { right: min(300px, 42vw); }
  #toast { bottom: calc(var(--sb) + 30px); }
}
