:root {
  --bg: #0f1115;
  --panel: #1a1e27;
  --text: #f2f4f8;
  --muted: #9aa3b2;
  --accent: #4da3ff;
  --danger: #ff5c6c;
  --ok: #3dd68c;
  --line: #2a3140;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  overflow: hidden;
  user-select: none;
}

.hidden { display: none !important; }

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 720px;
  margin: 0 auto;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

header h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

#countBadge {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
  min-width: 3.5em;
  text-align: right;
}

#previewWrap {
  position: relative;
  flex: 1;
  background: #000;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#video, #overlayCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

#video.rot90 {
  transform: rotate(90deg);
  width: 100%;
  height: 100%;
  object-fit: contain;
}

#overlayCanvas {
  pointer-events: none;
}

#flash {
  position: absolute;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
  z-index: 4;
  transition: opacity 0.05s;
}
#flash.show {
  opacity: 0.85;
}

#lastThumbs {
  position: absolute;
  left: 8px;
  bottom: 8px;
  display: flex;
  gap: 6px;
  z-index: 4;
  pointer-events: none;
}
#lastThumbs img {
  width: 56px;
  height: 72px;
  object-fit: cover;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: #111;
}

#startOverlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(0,0,0,0.72);
  z-index: 5;
  padding: 24px;
  text-align: center;
}

#startOverlay p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
}

.meta-line {
  font-size: 12px !important;
  color: var(--muted) !important;
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 14px 18px;
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  background: var(--accent);
  cursor: pointer;
}

.btn:active { opacity: 0.85; transform: scale(0.98); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn.secondary { background: #2c3444; }
.btn.danger { background: var(--danger); }
.btn.ok { background: var(--ok); color: #0b1a12; }
.btn.ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
}
.btn.large { min-width: 200px; padding: 16px 24px; font-size: 18px; }

#toolbar {
  display: grid;
  grid-template-columns: 1.1fr 1fr 1.2fr 1fr;
  gap: 8px;
  padding: 10px 10px calc(10px + var(--safe-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
  flex-shrink: 0;
}

#toolbar.share-ready {
  grid-template-columns: 1fr 1fr 1fr 1fr 1.2fr;
}

#toolbar .btn {
  padding: 16px 8px;
  font-size: 14px;
}

#shutterBtn {
  background: var(--ok);
  color: #0b1a12;
}

#settings {
  background: var(--panel);
  border-top: 1px solid var(--line);
  padding: 10px 14px calc(8px + var(--safe-bottom));
  flex-shrink: 0;
  max-height: 42vh;
  overflow-y: auto;
}

#settings.collapsed .settings-body { display: none; }

.settings-toggle {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 13px;
  padding: 4px 0 8px;
}

.settings-body {
  display: grid;
  gap: 12px;
}

.row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  font-size: 13px;
}

.row label { color: var(--muted); }
.row output { color: var(--text); font-variant-numeric: tabular-nums; min-width: 3.5em; text-align: right; }

input[type="range"] { width: 100%; grid-column: 1 / -1; }

select {
  grid-column: 1 / -1;
  background: #12151c;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
}

#statusLine {
  font-size: 12px;
  color: var(--muted);
  min-height: 1.2em;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(110px + var(--safe-bottom));
  transform: translateX(-50%) translateY(20px);
  background: rgba(20,24,32,0.94);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 20;
  border: 1px solid var(--line);
  max-width: 90%;
  text-align: center;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

#progress {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 30;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 12px;
}
#progress.show { display: flex; }
#progress .bar {
  width: min(280px, 70vw);
  height: 8px;
  background: #2a3140;
  border-radius: 4px;
  overflow: hidden;
}
#progress .bar > span {
  display: block;
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.15s;
}

/* Landscape: header on top, preview left, controls right, settings at the bottom.
   2026-11-06 fix: flex-wrap left the preview at width 0 (everything pushed off-screen),
   so the layout is now an explicit grid. */
@media (orientation: landscape) {
  #app {
    max-width: none;
    height: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 112px;
    grid-template-rows: auto minmax(0, 1fr) auto;
    flex-wrap: nowrap;
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  header {
    grid-column: 1 / -1;
    width: auto;
    order: 0;
  }
  #previewWrap {
    grid-column: 1;
    grid-row: 2;
    order: 1;
    height: auto;
    min-height: 0;
    min-width: 0;
  }
  #toolbar {
    grid-column: 2;
    grid-row: 2;
    order: 2;
    width: auto;
    height: auto;
    min-height: 0;
    grid-template-columns: 1fr;
    gap: 6px;
    padding: 6px;
    border-top: none;
    border-left: 1px solid var(--line);
    align-content: start;
    overflow-y: auto;
  }
  #toolbar.share-ready {
    grid-template-columns: 1fr;
  }
  #toolbar .btn {
    padding: 9px 4px;
    font-size: 13px;
  }
  #settings {
    grid-column: 1 / -1;
    grid-row: 3;
    order: 3;
    width: auto;
    max-height: 30vh;
    padding-top: 4px;
  }
  .settings-toggle {
    padding: 2px 0 4px;
  }
}
