:root {
  --bg: #000;
  --bar: #111;
  --panel: #1c1c1e;
  --panel-2: #2c2c2e;
  --text: #f2f2f2;
  --muted: #9a9aa0;
  --accent: #ffcc00;
  --danger: #ff5a52;
  --line: #3a3a3c;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
body { font-family: system-ui, "Noto Sans KR", "Malgun Gothic", sans-serif; font-size: 16px; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

#app { position: fixed; inset: 0; display: flex; flex-direction: column; }

/* 상단 바 */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 6px) 12px 6px; background: var(--bar);
}
.icon-btn {
  width: 44px; height: 44px; border: 0; border-radius: 12px; background: transparent;
  display: grid; place-items: center; cursor: pointer;
}
.icon-btn svg { width: 26px; height: 26px; fill: var(--text); }
.icon-btn:disabled svg { fill: #555; }
.icon-btn:active:not(:disabled) { background: var(--panel-2); }
.flash-btn { position: relative; }
.flash-btn[data-mode="shot"] svg, .flash-btn[data-mode="on"] svg { fill: var(--accent); }
.flash-btn:disabled svg { fill: #444; }
.flash-label {
  position: absolute; bottom: 0; left: 0; right: 0; font-size: 10px; line-height: 1; color: var(--accent); pointer-events: none;
}
.flash-btn[data-mode="off"] .flash-label { color: var(--muted); }
/* 같은 번호 버튼: 켜지면 노란 바탕 */
.same-btn { display: flex; flex-direction: column; justify-content: center; gap: 0; line-height: 1.1; font-size: 12px; color: var(--muted); border: 1px solid var(--line); }
.same-btn[aria-pressed="true"] { background: var(--accent); color: #000; border-color: var(--accent); font-weight: 700; }
.same-btn[aria-pressed="true"]:active { background: var(--accent); }
.flash-btn[data-mode="off"]:not(:disabled) svg { fill: var(--text); }
.icon-btn.round { border-radius: 50%; background: var(--panel-2); width: 52px; height: 52px; }
.next {
  flex: 1; min-width: 0; border: 0; background: var(--panel); border-radius: 12px;
  padding: 4px 12px; display: flex; align-items: baseline; justify-content: center; gap: 8px; cursor: pointer;
}
.next-label { color: var(--muted); font-size: 13px; }
.next-text { font-size: 22px; font-weight: 700; color: var(--accent); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* 미리보기 */
.stage { position: relative; flex: 1; min-height: 0; overflow: hidden; background: #000; }
.stage { touch-action: none; }
.stage video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; transform: scale(var(--dz, 1)); }
.stage video.mirror { transform: scale(calc(-1 * var(--dz, 1)), var(--dz, 1)); }
.overlay { position: absolute; pointer-events: none; }
.flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.flash.on { animation: flash .25s ease-out; }
@keyframes flash { from { opacity: .8; } to { opacity: 0; } }
.fallback {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; padding: 24px; text-align: center; background: #000;
}
.fallback-msg { color: var(--muted); line-height: 1.5; max-width: 360px; margin: 0 0 8px; }
.fallback .btn { width: 100%; max-width: 300px; }

/* 하단 바 */
.bottombar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px calc(env(safe-area-inset-bottom) + 14px); background: var(--bar);
}
.thumb-btn {
  position: relative; width: 52px; height: 52px; border-radius: 10px; border: 2px solid var(--line);
  background: var(--panel); padding: 0; overflow: visible; cursor: pointer;
}
.thumb-btn img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; display: block; }
.thumb-btn img:not([src]) { visibility: hidden; }
.badge {
  position: absolute; top: -8px; right: -8px; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px;
  background: var(--accent); color: #000; font-size: 12px; font-weight: 700; line-height: 22px; text-align: center;
}
.shutter {
  width: 76px; height: 76px; border-radius: 50%; border: 4px solid #fff; background: transparent; padding: 4px; cursor: pointer;
}
.shutter span { display: block; width: 100%; height: 100%; border-radius: 50%; background: #fff; transition: transform .1s; }
.shutter:active span { transform: scale(.88); }
.shutter:disabled { opacity: .4; }

/* 공통 버튼 */
.btn {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 10px; padding: 12px 16px;
  cursor: pointer; font-size: 15px;
}
.btn.small { padding: 8px 12px; font-size: 14px; }
.btn.primary { background: var(--accent); color: #000; border-color: var(--accent); font-weight: 700; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; }

/* 패널 */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; max-height: 72%; display: flex; flex-direction: column;
  background: var(--panel); border-radius: 16px 16px 0 0; box-shadow: 0 -8px 30px rgba(0,0,0,.5);
  animation: up .2s ease-out; z-index: 10;
}
.sheet.full { top: 0; max-height: none; border-radius: 0; }
@keyframes up { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.sheet-head {
  display: flex; align-items: center; justify-content: space-between; padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line);
}
.sheet-head h2 { margin: 0; font-size: 18px; }
.sheet-head small { color: var(--muted); font-weight: normal; font-size: 13px; }
.sheet-body { overflow-y: auto; overflow-x: hidden; padding: 8px 16px calc(env(safe-area-inset-bottom) + 20px); }
fieldset { border: 0; border-bottom: 1px solid var(--line); margin: 0; padding: 10px 0 8px; min-width: 0; }
legend { padding: 0; font-weight: 700; color: var(--accent); font-size: 14px; margin-bottom: 2px; }
/* 제목 오른쪽에 켜기/끄기 */
legend.sec-head { display: flex; align-items: center; justify-content: space-between; width: 100%; }
legend.sec-head label.check { flex: 0 0 auto; margin: 0; font-weight: normal; font-size: 14px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); margin: 6px 0; flex: 1; min-width: 0; }
/* 두 칸짜리 줄: 폭이 모자라면(좁은 화면·큰 글자) 위아래로 내려간다 */
.row { display: flex; flex-wrap: wrap; gap: 0 12px; align-items: flex-end; }
.row > label, .row > .cell, .row > .stack { flex: 1 1 11em; min-width: 0; }
.cell, .stack { font-size: 13px; } /* 11em 너비 기준을 label(13px)과 맞춘다 */
.row.mid { align-items: center; }
/* 한 칸 안에 체크박스+색처럼 작은 것 둘 */
.cell { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 6px 0; min-height: 44px; }
.cell.end { justify-content: flex-end; }
.cell > label { flex: 0 1 auto; margin: 0; }
.stack > label { margin: 4px 0; }
.inline { display: flex; gap: 6px; }
.inline input { flex: 1; min-width: 0; }
.inline .btn { flex: 0 0 auto; white-space: nowrap; padding: 8px 10px; }
input[type=number], input[type=text], select {
  width: 100%; font: inherit; font-size: 16px; color: var(--text); background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 8px; padding: 9px 10px;
}
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 15px; white-space: nowrap; }
label.check input { width: 20px; height: 20px; accent-color: var(--accent); }
label.color { flex-direction: row; align-items: center; justify-content: flex-end; gap: 8px; white-space: nowrap; }
input[type=color] { width: 44px; height: 32px; border: 1px solid var(--line); border-radius: 6px; background: none; padding: 2px; }
label.range { color: var(--text); font-size: 14px; display: block; }
label.range output { color: var(--accent); float: right; }
label.range input { width: 100%; accent-color: var(--accent); margin: 6px 0 0; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0; line-height: 1.5; word-break: break-all; }
.hint b { color: var(--text); font-weight: normal; }
.center { text-align: center; }
.grid9 {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; width: 100%; max-width: 150px; aspect-ratio: 4 / 3;
  padding: 4px; background: var(--panel-2); border-radius: 8px;
}
.grid9 button { border: 1px solid var(--line); background: #3a3a3c; border-radius: 4px; cursor: pointer; padding: 0; }
.grid9 button.on { background: var(--accent); border-color: var(--accent); }

/* 목록 */
.list-actions { display: flex; gap: 8px; padding: 10px 16px; flex-wrap: wrap; }
.list-grid {
  flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 6px; padding: 0 16px calc(env(safe-area-inset-bottom) + 16px); align-content: start;
}
.list-item { position: relative; border: 0; padding: 0; background: var(--panel-2); border-radius: 6px; overflow: hidden; aspect-ratio: 1; cursor: pointer; }
.list-item img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
/* 길게 누를 때 안드로이드 이미지 메뉴·글자 선택이 뜨지 않게 */
.list-item { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; touch-action: manipulation; }
/* 선택 모드: 오른쪽 위 동그라미, 고른 사진은 노란 테두리 + 체크 */
.list-grid.selecting .list-item::after {
  content: ''; position: absolute; top: 6px; right: 6px; width: 24px; height: 24px; border-radius: 50%;
  border: 2px solid #fff; background: rgba(0,0,0,.35);
}
.list-grid.selecting .list-item.sel { outline: 3px solid var(--accent); outline-offset: -3px; }
.list-grid.selecting .list-item.sel img { opacity: .7; }
.list-grid.selecting .list-item.sel::after {
  content: '✓'; background: var(--accent); border-color: var(--accent); color: #000;
  font-size: 14px; font-weight: 700; line-height: 20px; text-align: center;
}
.sel-bar { flex-direction: column; flex-wrap: nowrap; }
.sel-top, .sel-ops { display: flex; gap: 8px; align-items: center; }
.sel-top b { flex: 1; color: var(--accent); font-size: 15px; }
.sel-ops .btn { flex: 1; min-width: 0; padding-left: 4px; padding-right: 4px; }
#listTip { margin: -4px 16px 8px; }
.list-item span {
  position: absolute; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,.65); font-size: 11px; padding: 3px 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: left;
}

/* 사진 보기 */
.viewer { position: fixed; inset: 0; background: #000; z-index: 20; display: flex; flex-direction: column; }
.viewer img { flex: 1; min-height: 0; width: 100%; object-fit: contain; }
.viewer-bar {
  display: flex; flex-direction: column; gap: 8px; padding: 10px 12px calc(env(safe-area-inset-bottom) + 10px);
  background: var(--bar); font-size: 13px; color: var(--muted);
}
.viewer-bar div { display: flex; gap: 8px; flex-wrap: wrap; }

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 170px); transform: translateX(-50%);
  max-width: calc(100% - 32px); background: rgba(40,40,40,.95); color: #fff; padding: 10px 16px; border-radius: 20px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 30; text-align: center;
}
.toast.show { opacity: 1; }
.overlay.cropped { box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .55); outline: 1px solid rgba(255, 255, 255, .35); }

/* 화면 보조: 격자선·수평 (4:3 저장 영역 위, 사진에는 안 찍힘) */
.guides { position: absolute; pointer-events: none; }
.guides.grid {
  background:
    linear-gradient(to right, transparent calc(33.333% - .5px), rgba(255,255,255,.45) calc(33.333% - .5px), rgba(255,255,255,.45) calc(33.333% + .5px), transparent calc(33.333% + .5px),
      transparent calc(66.667% - .5px), rgba(255,255,255,.45) calc(66.667% - .5px), rgba(255,255,255,.45) calc(66.667% + .5px), transparent calc(66.667% + .5px)),
    linear-gradient(to bottom, transparent calc(33.333% - .5px), rgba(255,255,255,.45) calc(33.333% - .5px), rgba(255,255,255,.45) calc(33.333% + .5px), transparent calc(33.333% + .5px),
      transparent calc(66.667% - .5px), rgba(255,255,255,.45) calc(66.667% - .5px), rgba(255,255,255,.45) calc(66.667% + .5px), transparent calc(66.667% + .5px));
}
.level-line {
  position: absolute; left: 30%; right: 30%; top: 50%; height: 0; border-top: 2px solid rgba(255,255,255,.85);
  transform: rotate(var(--roll, 0deg)); box-shadow: 0 0 2px rgba(0,0,0,.8);
}
.level-line.ok { border-color: var(--accent); }
.level-bubble {
  position: absolute; left: 50%; top: 50%; width: 56px; height: 56px; margin: -28px 0 0 -28px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.7); box-shadow: 0 0 2px rgba(0,0,0,.8);
}
.level-bubble span {
  position: absolute; left: 50%; top: 50%; width: 14px; height: 14px; margin: -7px 0 0 -7px; border-radius: 50%;
  background: rgba(255,255,255,.9); transform: translate(var(--bx, 0px), var(--by, 0px));
}
.level-bubble.ok { border-color: var(--accent); }
.level-bubble.ok span { background: var(--accent); }

/* 눌러서 초점 표시 */
.focus-ring {
  position: absolute; width: 72px; height: 72px; margin: -36px 0 0 -36px; border: 2px solid var(--accent); border-radius: 50%;
  pointer-events: none; animation: focus .6s ease-out forwards;
}
.focus-ring.fail { border-color: #fff; border-style: dashed; }
@keyframes focus { from { transform: scale(1.4); opacity: 1; } 70% { transform: scale(1); opacity: 1; } to { transform: scale(1); opacity: .0; } }

/* 메모 칩 (촬영 화면 위쪽 가운데) */
.memo-chip {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%); z-index: 2; max-width: calc(100% - 24px);
  display: flex; align-items: center; gap: 8px; border: 1px solid rgba(255,255,255,.25); border-radius: 18px;
  background: rgba(0,0,0,.55); padding: 6px 12px; font-size: 14px; cursor: pointer;
}
.memo-ico { color: var(--muted); font-size: 12px; flex: 0 0 auto; }
.memo-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; color: var(--muted); }
.memo-chip.has .memo-text { color: var(--accent); font-weight: 700; }

/* 줌 (촬영 화면 아래쪽 가운데) */
.zoom-bar {
  position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%); z-index: 2;
  display: flex; gap: 6px; padding: 4px; border-radius: 22px; background: rgba(0,0,0,.45);
}
.zoom-bar button {
  min-width: 40px; height: 36px; border: 0; border-radius: 18px; background: transparent; color: #fff;
  font-size: 13px; font-weight: 700; cursor: pointer; padding: 0 8px;
}
.zoom-bar button.on { background: rgba(255,255,255,.18); color: var(--accent); }

/* 메모 입력 창 */
.memo-form { display: flex; gap: 8px; margin: 10px 0 6px; }
.memo-form input { flex: 1; min-width: 0; }
.memo-ops { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 6px 0 10px; }
.memo-ops label.check { flex: 0 0 auto; margin: 0; }
.memo-recent { display: flex; flex-wrap: wrap; gap: 8px; padding-bottom: 8px; }
.memo-recent .chip {
  display: inline-flex; align-items: center; max-width: 100%; border: 1px solid var(--line); border-radius: 18px;
  background: var(--panel-2); overflow: hidden;
}
.memo-recent .chip button { border: 0; background: transparent; cursor: pointer; font-size: 15px; padding: 8px 4px 8px 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.memo-recent .chip button.x { color: var(--muted); padding: 8px 12px 8px 8px; flex: 0 0 auto; }
.memo-recent .chip.cur { border-color: var(--accent); }
.legend-note { color: var(--muted); font-weight: normal; font-size: 12px; margin-left: 6px; }
.row > p.hint.cell { display: block; min-height: 0; margin: 6px 0; }

/* 좁은 세로 화면: 위 막대 버튼을 줄여 다음 번호 칸을 넓힌다 */
@media (max-width: 360px) and (orientation: portrait) {
  .topbar { gap: 4px; padding-left: 6px; padding-right: 6px; }
  .topbar .icon-btn { width: 38px; }
  .next { padding: 4px 6px; gap: 6px; }
  .next-text { font-size: 20px; }
}

/* 가로 화면: 위·아래 막대를 왼쪽·오른쪽 세로 막대로 (미리보기를 최대한 크게) */
@media (orientation: landscape) {
  #app { flex-direction: row; }
  .topbar {
    flex-direction: column; width: 76px; padding: 8px 6px; padding-left: calc(env(safe-area-inset-left) + 6px);
    box-sizing: content-box;
  }
  .next { flex-direction: column; align-items: center; gap: 0; padding: 6px 2px; width: 100%; }
  .next-text { font-size: 17px; max-width: 100%; }
  .bottombar {
    flex-direction: column-reverse; width: 92px; padding: 12px 8px; padding-right: calc(env(safe-area-inset-right) + 8px);
    box-sizing: content-box;
  }
  .sheet { max-height: none; top: 0; left: 50%; transform: translateX(-50%); width: min(100%, 640px); border-radius: 0; }
  @keyframes up { from { opacity: 0; } to { opacity: 1; } }
  .sheet.full { width: 100%; left: 0; transform: none; }
  .viewer { flex-direction: row; }
  .viewer img { width: auto; min-width: 0; flex: 1; height: 100%; }
  .viewer-bar { width: 200px; justify-content: center; }
  .viewer-bar div { flex-direction: column; }
  .toast { bottom: 70px; }
  .same-btn { flex: 0 0 auto; }
}
