:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #22c55e;
  --accent-2: #16a34a;
  --warn: #f59e0b;
  --err: #f87171;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 16px;
  line-height: 1.5;
}

body { min-height: 100dvh; padding-bottom: env(safe-area-inset-bottom); }

[hidden] { display: none !important; }

.topbar {
  display: flex; align-items: center; gap: .6rem;
  padding: calc(env(safe-area-inset-top) + .7rem) 1rem .7rem;
  background: #0b1222; border-bottom: 1px solid #1f2a3d;
  position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 1.05rem; margin: 0; letter-spacing: .02em; }
.badge { font-size: .7rem; padding: .1rem .5rem; border-radius: 99px; background: #134e2a; color: #86efac; }

main { max-width: 720px; margin: 0 auto; padding: 1rem; }

.lead { color: var(--muted); text-align: center; margin: .6rem 0 1.2rem; }
.note { color: var(--muted); font-size: .85rem; }
.note.center { text-align: center; margin: .4rem 0 .8rem; }
.hint { color: var(--muted); font-size: .88rem; text-align: center; margin: 0 0 .6rem; }
.spacer { flex: 1; }

.home-buttons { display: grid; gap: 1rem; }

.big-btn {
  display: flex; align-items: center; justify-content: center; gap: .8rem;
  min-height: 56px; padding: .9rem 1.2rem;
  border: 1px solid #334155; border-radius: var(--radius);
  background: var(--panel); color: var(--text);
  font-size: 1.05rem; font-weight: 600; cursor: pointer;
  touch-action: manipulation; font-family: inherit;
}
.big-btn:active { transform: scale(.98); }
.big-btn:disabled { opacity: .45; }
.big-btn.primary { background: var(--accent-2); border-color: var(--accent); color: #fff; }
.big-btn.danger { color: var(--err); }
.big-btn.wide { width: 100%; }
.home-buttons .big-btn { justify-content: flex-start; min-height: 88px; text-align: left; }
.home-buttons .icon { font-size: 2rem; }
.home-buttons small { display: block; font-weight: 400; font-size: .85rem; opacity: .85; }
.file-btn input { display: none; }

.back-btn { margin-top: 1.2rem; background: none; border: none; color: var(--muted); font-size: 1rem; padding: .6rem 0; }

.howto, .settings { margin-top: 1.2rem; background: var(--panel); border-radius: var(--radius); padding: .8rem 1rem; }
.howto summary, .settings summary { cursor: pointer; font-weight: 600; }
.howto ol { padding-left: 1.2rem; }
.settings .options { margin-top: .8rem; }

.card { background: var(--panel); border-radius: var(--radius); padding: 1rem; margin: 1rem 0; }
.options { display: grid; gap: .9rem; }
.options label { display: grid; gap: .35rem; font-size: .92rem; color: var(--muted); }
.options label.check { display: flex; align-items: center; gap: .6rem; color: var(--text); }
.options input[type=checkbox] { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }
select, input[type=text] {
  width: 100%; font-size: 1rem; padding: .6rem; border-radius: 10px;
  background: var(--panel-2); color: var(--text); border: 1px solid #334155; font-family: inherit;
}

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; margin-bottom: 1rem;
  background: var(--panel); border-radius: var(--radius); }
.seg.filters { grid-template-columns: repeat(4, 1fr); }
.seg-btn { min-height: 48px; border: 0; border-radius: 10px; background: transparent; color: var(--muted);
  font-size: 1rem; font-weight: 600; cursor: pointer; touch-action: manipulation; font-family: inherit; padding: .4rem; }
.seg-btn small { display: block; font-size: .75rem; font-weight: 400; }
.seg-btn.on { background: var(--panel-2); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.35); }
.seg.filters .seg-btn.on { background: var(--accent-2); color: #fff; }

.chip {
  min-height: 44px; padding: .4rem .9rem; border-radius: 99px;
  border: 1px solid #334155; background: var(--panel-2); color: var(--text);
  font-size: .95rem; cursor: pointer; touch-action: manipulation; font-family: inherit;
}
.chip.on { background: #854d0e; border-color: var(--warn); }
.chip.done { background: var(--accent-2); border-color: var(--accent); color: #fff; font-weight: 700; }

.row { display: flex; gap: .5rem; flex-wrap: wrap; justify-content: center; margin: .8rem 0; }
.row.actions { display: grid; grid-template-columns: 1fr 1.6fr; }
.stack { display: grid; gap: .6rem; margin-top: .8rem; }

/* ---- カメラ ---- */
.cam-top { display: flex; align-items: center; gap: .5rem; margin-bottom: .6rem; }
.mode-chip { font-size: .8rem; padding: .2rem .7rem; border-radius: 99px; background: #1e3a5f; color: #bfdbfe; }
.cam-stage {
  position: relative; width: 100%; aspect-ratio: 3 / 4; max-height: calc(100dvh - 230px);
  margin: 0 auto; background: #000; border-radius: var(--radius); overflow: hidden;
}
.cam-stage video, .cam-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.cam-stage video { object-fit: contain; }
.cam-stage canvas { pointer-events: none; }
.cam-placeholder {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 1rem;
  text-align: center; background: radial-gradient(circle at center, #1e293b, #020617);
}
.cam-hint {
  position: absolute; left: 50%; bottom: .8rem; transform: translateX(-50%);
  background: rgba(15, 23, 42, .78); padding: .35rem .9rem; border-radius: 99px; font-size: .9rem; white-space: nowrap;
}
.cam-hint.ok { background: rgba(21, 128, 61, .88); }
.cam-flash { position: absolute; inset: 0; background: #fff; animation: flash .35s ease-out forwards; }
@keyframes flash { from { opacity: .85; } to { opacity: 0; } }
.cam-bar { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 1rem; margin-top: .9rem; }
.shutter {
  grid-column: 2; width: 78px; height: 78px; border-radius: 50%; border: 5px solid #fff; background: transparent;
  padding: 5px; cursor: pointer; touch-action: manipulation;
}
.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; }
.shutter.auto span { background: var(--accent); }
.cam-thumb {
  grid-column: 1; justify-self: start; position: relative; width: 56px; height: 56px; padding: 0;
  border: 2px solid #fff; border-radius: 10px; overflow: hidden; background: #334155; cursor: pointer;
}
.cam-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cam-thumb span {
  position: absolute; right: 2px; bottom: 2px; min-width: 22px; padding: 0 5px; border-radius: 99px;
  background: var(--accent-2); color: #fff; font-size: .8rem; font-weight: 700;
}
.cam-thumb.pop { animation: pop .35s ease-out; }
.cam-bar .done { grid-column: 3; justify-self: end; }
@keyframes pop { 0% { transform: scale(.8); } 60% { transform: scale(1.12); } 100% { transform: scale(1); } }

/* ---- 四隅の調整 ---- */
.edit-wrap { display: flex; justify-content: center; }
.edit-stage { position: relative; touch-action: none; user-select: none; -webkit-user-select: none; }
#edit-img { display: block; background: #000; }
#edit-overlay { position: absolute; touch-action: none; cursor: crosshair; }
.loupe {
  position: absolute; width: 140px; height: 140px; border-radius: 50%; border: 3px solid #fff;
  box-shadow: 0 6px 24px rgba(0,0,0,.6); pointer-events: none; background: #000; z-index: 3;
}

/* ---- 結果 ---- */
.result-img-wrap { display: flex; justify-content: center; background: #0b1222; border-radius: var(--radius); padding: .6rem; }
#result-img { max-width: 100%; max-height: 52dvh; display: block; box-shadow: 0 4px 16px rgba(0,0,0,.5); background: #fff; }

/* ---- ページ一覧 ---- */
.pages-head { display: flex; align-items: center; gap: .5rem; }
.pages-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: .8rem; margin-top: .6rem; }
.page-card { background: var(--panel); border-radius: 12px; padding: .5rem; display: grid; gap: .4rem; }
.page-card img { width: 100%; aspect-ratio: 3 / 4; object-fit: contain; background: #0b1222; border-radius: 6px; cursor: pointer; display: block; }
.page-card .page-row { display: flex; align-items: center; gap: .25rem; }
.page-card .page-row b { flex: 1; font-size: .9rem; }
.page-card .mini { min-height: 34px; min-width: 34px; padding: 0 .4rem; border-radius: 8px; border: 1px solid #334155;
  background: var(--panel-2); color: var(--text); font-size: .9rem; cursor: pointer; }
.page-card .mini:disabled { opacity: .3; }
.page-empty { color: var(--muted); text-align: center; padding: 2rem 0; grid-column: 1 / -1; }

.busy {
  position: fixed; inset: 0; z-index: 40; background: rgba(2, 6, 23, .6);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .8rem; font-weight: 600;
}
.spinner { width: 44px; height: 44px; border-radius: 50%; border: 4px solid #334155; border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 1.2rem); transform: translateX(-50%);
  background: #111827; color: #fff; padding: .7rem 1.1rem; border-radius: 12px; z-index: 50;
  max-width: 90vw; box-shadow: 0 8px 30px rgba(0,0,0,.4); font-size: .92rem;
}
.toast.err { background: #7f1d1d; }
.note.license { text-align: center; margin-top: 1.5rem; }
.note.license a { color: inherit; }

@media (min-width: 720px) {
  .home-buttons { grid-template-columns: 1fr 1fr; }
}
