/* ============================================================
   基本リセット・変数
   ============================================================ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0a0a0a;
  --bg2: #141414;
  --bg3: #1e1e1e;
  --accent: #00c8ff;
  --accent2: #0099cc;
  --shot-color: #ff3c3c;
  --warning: #ffaa00;
  --success: #00e676;
  --text: #f0f0f0;
  --text-dim: #888;
  --border: #333;
  --btn-h: 56px;
  --radius: 8px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 15px;
  line-height: 1.5;
  overscroll-behavior: none;
}

/* ============================================================
   ヘッダー
   ============================================================ */
.app-header {
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  text-align: center;
}

.app-header h1 {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.05em;
}

/* ============================================================
   ステータスバー
   ============================================================ */
.status-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.08em;
  transition: background 0.3s, color 0.3s;
}

.status-idle      { background: #1a1a1a; color: var(--text-dim); }
.status-ready     { background: #0d2a1a; color: var(--success); }
.status-detecting { background: #0d2233; color: var(--accent); }
.status-shot      { background: #3a0000; color: var(--shot-color); animation: pulse-bg 0.5s ease; }
.status-saving    { background: #1a1a00; color: var(--warning); }
.status-cooldown  { background: #1a1200; color: var(--warning); }
.status-error     { background: #2a0000; color: var(--shot-color); }

@keyframes pulse-bg {
  0%   { background: #6a0000; }
  100% { background: #3a0000; }
}

/* ============================================================
   エラー / HTTPS 警告
   ============================================================ */
.error-msg {
  background: #3a0000;
  color: #ff8080;
  padding: 10px 16px;
  font-size: 13px;
  border-left: 3px solid var(--shot-color);
}

.https-warning {
  background: #2a1500;
  color: var(--warning);
  padding: 8px 16px;
  font-size: 13px;
  text-align: center;
}

/* ============================================================
   カメラプレビュー
   ============================================================ */
.preview-container {
  position: relative;
  width: 100%;
  background: #000;
  /* 16:9 アスペクト比 */
  aspect-ratio: 16 / 9;
  overflow: hidden;
  max-height: 56vw; /* スマホ縦向きで大きすぎないように */
}

@media (orientation: portrait) {
  .preview-container {
    max-height: 60vw;
  }
}

.video-preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.overlay-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: auto; /* ドラッグ操作を受け取る */
  touch-action: none;
}

/* SHOT DETECTED オーバーレイ */
.shot-alert {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.5);
  background: rgba(180, 0, 0, 0.85);
  color: #fff;
  font-size: clamp(20px, 6vw, 36px);
  font-weight: 900;
  letter-spacing: 0.1em;
  padding: 12px 28px;
  border-radius: 6px;
  border: 2px solid var(--shot-color);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
  white-space: nowrap;
}

.shot-alert.visible {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

/* 検知開始カウントダウン */
.countdown-overlay {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: clamp(64px, 20vw, 140px);
  font-weight: 900;
  color: #fff;
  text-shadow: 0 0 24px rgba(0, 200, 255, 0.9);
  pointer-events: none;
  z-index: 5;
}

.cooldown-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(255, 170, 0, 0.85);
  color: #000;
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 12px;
}

/* ============================================================
   Motion Score パネル
   ============================================================ */
.score-panel {
  background: var(--bg2);
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
}

.score-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.score-label {
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
  min-width: 90px;
}

.score-bar-wrap {
  flex: 1;
  height: 10px;
  background: #333;
  border-radius: 5px;
  overflow: hidden;
}

.score-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 5px;
  transition: width 0.1s, background-color 0.1s;
}

.score-num {
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  min-width: 36px;
  text-align: right;
}

.score-meta {
  display: flex;
  gap: 16px;
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 4px;
}

.score-meta strong {
  color: var(--text);
}

/* ============================================================
   操作ボタン
   ============================================================ */
.btn-panel {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
}

.btn {
  height: var(--btn-h);
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.03em;
  transition: opacity 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}

.btn:active {
  transform: scale(0.96);
}

.btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: var(--accent2);
  color: #fff;
  grid-column: 1 / 3;
}

.btn-primary.active {
  background: #cc4400;
}

.btn-secondary {
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn-detect {
  background: #003a55;
  color: var(--accent);
  border: 1px solid var(--accent2);
}

.btn-detect.active {
  background: #004422;
  color: var(--success);
  border-color: var(--success);
}

.btn-shot {
  background: #4a0000;
  color: #ff8080;
  border: 1px solid #882222;
  font-size: 14px;
}

/* ============================================================
   設定パネル
   ============================================================ */
.settings-panel {
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
}

.settings-toggle {
  padding: 12px 16px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-dim);
  list-style: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.settings-toggle::-webkit-details-marker { display: none; }

.settings-toggle:hover { color: var(--text); }

.settings-content {
  padding: 8px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.setting-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.setting-row label {
  font-size: 12px;
  color: var(--text-dim);
  font-weight: 600;
  letter-spacing: 0.05em;
}

/* オン/オフ トグル行 */
.setting-row.toggle-row {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.setting-row.toggle-row > label {
  flex: 1;
}

.switch {
  position: relative;
  display: inline-block;
  width: 46px;
  height: 26px;
  flex: none;
}
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.switch-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: #3a3a3a;
  border-radius: 26px;
  transition: background 0.2s;
}
.switch-slider::before {
  content: '';
  position: absolute;
  height: 20px;
  width: 20px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
}
.switch input:checked + .switch-slider {
  background: var(--accent);
}
.switch input:checked + .switch-slider::before {
  transform: translateX(20px);
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.slider-val {
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  min-width: 36px;
  text-align: right;
}

.setting-row.two-col {
  flex-direction: row;
  gap: 12px;
}

.setting-row.two-col > div {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.num-input {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 10px;
  border-radius: var(--radius);
  font-size: 15px;
  width: 100%;
  text-align: center;
}

.num-input:focus {
  outline: none;
  border-color: var(--accent);
}

.select-input {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius);
  font-size: 14px;
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23888' d='M0 0l6 8 6-8z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 32px;
}

.setting-hint {
  font-size: 12px;
  color: var(--text-dim);
  background: var(--bg3);
  padding: 8px 12px;
  border-radius: var(--radius);
  border-left: 3px solid var(--accent2);
  line-height: 1.6;
}

.setting-hint strong {
  color: var(--accent);
}

/* ============================================================
   保存済みクリップ
   ============================================================ */
.clips-section {
  padding: 16px 12px;
}

.clips-heading {
  font-size: 14px;
  color: var(--text-dim);
  font-weight: 600;
  letter-spacing: 0.08em;
  margin-bottom: 10px;
  text-transform: uppercase;
}

.clip-empty {
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
  padding: 24px 0;
}

.clip-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.clip-item {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.clip-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: var(--bg3);
  flex-wrap: wrap;
}

.clip-num {
  font-size: 14px;
  font-weight: 700;
  color: var(--accent);
}

.clip-time {
  font-size: 12px;
  color: var(--text-dim);
}

.clip-type {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.05em;
  padding: 2px 8px;
  border-radius: 10px;
  background: #003a55;
  color: var(--accent);
  margin-left: auto;
}

.clip-type.manual {
  background: #2a1500;
  color: var(--warning);
}

.clip-video {
  width: 100%;
  display: block;
  background: #000;
  max-height: 240px;
}

.clip-actions {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
}

.btn-download {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 40px;
  background: var(--accent2);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
  border-radius: 6px;
  -webkit-tap-highlight-color: transparent;
}

.btn-delete {
  width: 80px;
  height: 40px;
  background: #3a0000;
  color: #ff8080;
  border: 1px solid #552222;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.btn-delete:active {
  background: #550000;
}

/* ============================================================
   スマホ縦画面向け調整
   ============================================================ */
@media (max-width: 480px) {
  .app-header h1 {
    font-size: 16px;
  }

  .btn-panel {
    gap: 6px;
    padding: 8px;
  }

  .btn {
    height: 50px;
    font-size: 14px;
  }
}

/* ============================================================
   スクロールバー（デスクトップ）
   ============================================================ */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
