/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0d0d0d;
  --surface: #1a1a1a;
  --surface2: #252525;
  --border: #333;
  --text: #f0f0f0;
  --text-dim: #888;
  --rec: #e53935;
  --accent: #4fc3f7;
  --success: #66bb6a;
  --warning: #ffa726;
  --danger: #ef5350;
  --nav-h: 64px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Noto Sans JP', sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  position: relative;
  overflow: hidden;
}

/* ===== Views ===== */
.view {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-bottom: calc(var(--nav-h) + var(--safe-bottom));
}

.view.hidden { display: none; }

/* ===== Camera View ===== */
.camera-container {
  position: relative;
  flex: 1;
  background: #000;
  overflow: hidden;
}

#camera-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.orientation-indicator {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(0,0,0,0.6);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 20px;
  padding: 6px 14px;
  backdrop-filter: blur(4px);
  z-index: 10;
}

.level-track {
  width: 60px;
  height: 6px;
  background: rgba(255,255,255,0.2);
  border-radius: 3px;
  overflow: hidden;
}

.level-fill {
  height: 100%;
  width: 50%;
  background: var(--warning);
  border-radius: 3px;
  transition: width 0.1s ease, background 0.3s;
}

.orientation-indicator.level-ok .level-fill {
  background: var(--success);
}

#orientation-text {
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
}

.orientation-indicator.level-ok #orientation-text {
  color: var(--success);
}

.recording-indicator {
  position: absolute;
  top: 12px;
  right: 16px;
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(229,57,53,0.85);
  border-radius: 12px;
  padding: 4px 10px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.05em;
  z-index: 10;
}

.rec-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #fff;
  animation: blink 1s step-end infinite;
}

@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

.camera-guide {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 8px;
  text-align: center;
  background: linear-gradient(transparent, rgba(0,0,0,0.5));
  z-index: 5;
}

.guide-top {
  font-size: 12px;
  color: rgba(255,255,255,0.6);
}

.camera-controls {
  padding: 20px 0 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  background: var(--bg);
}

/* Record Button */
.btn-record {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: transparent;
  border: 4px solid rgba(255,255,255,0.5);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  position: relative;
}

.btn-record:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-record:not(:disabled) {
  border-color: rgba(255,255,255,0.8);
}

.record-icon-inner {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--rec);
  transition: border-radius 0.25s cubic-bezier(0.4, 0, 0.2, 1),
              width 0.25s cubic-bezier(0.4, 0, 0.2, 1),
              height 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

.btn-record.recording .record-icon-inner {
  border-radius: 8px;
  width: 34px;
  height: 34px;
}

.btn-record.recording {
  border-color: var(--rec);
  animation: pulse-ring 1.5s ease-out infinite;
}

@keyframes pulse-ring {
  0% { box-shadow: 0 0 0 0 rgba(229,57,53,0.5); }
  70% { box-shadow: 0 0 0 14px rgba(229,57,53,0); }
  100% { box-shadow: 0 0 0 0 rgba(229,57,53,0); }
}

.camera-hint {
  font-size: 12px;
  color: var(--text-dim);
  min-height: 18px;
  text-align: center;
}

/* ===== Processing View ===== */
.processing-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 32px;
}

.processing-content h2 {
  font-size: 20px;
  font-weight: 600;
}

.spinner {
  width: 56px;
  height: 56px;
  border: 4px solid var(--surface2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.progress-track {
  width: 240px;
  height: 6px;
  background: var(--surface2);
  border-radius: 3px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), #81d4fa);
  border-radius: 3px;
  transition: width 0.2s ease;
}

.progress-label {
  font-size: 13px;
  color: var(--text-dim);
}

/* ===== Review View ===== */
.review-canvas-wrap {
  flex: 1;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  min-height: 0;
}

#review-canvas {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
}

.playback-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px 6px;
  background: var(--bg);
}

.btn-icon {
  background: var(--surface2);
  border: none;
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

.timeline-slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--surface2);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

.timeline-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.timeline-slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  cursor: pointer;
}

.time-display {
  font-size: 12px;
  color: var(--text-dim);
  min-width: 36px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.stats-row {
  display: flex;
  gap: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.stat-item {
  flex: 1;
  padding: 10px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-item + .stat-item {
  border-left: 1px solid var(--border);
}

.stat-label {
  font-size: 11px;
  color: var(--text-dim);
}

.stat-value {
  font-size: 17px;
  font-weight: 600;
  color: var(--accent);
}

.review-actions {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  background: var(--bg);
}

.btn-primary {
  flex: 1;
  padding: 12px;
  background: var(--accent);
  color: #000;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  flex: 1;
  padding: 12px;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.btn-outline {
  flex: 1;
  padding: 12px;
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.export-status {
  text-align: center;
  font-size: 12px;
  color: var(--text-dim);
  padding: 4px 16px 8px;
}

.export-status.hidden { display: none; }

/* ===== History View ===== */
.history-header {
  padding: 20px 16px 12px;
  border-bottom: 1px solid var(--border);
}

.history-header h2 {
  font-size: 20px;
  font-weight: 700;
}

.history-sub {
  font-size: 12px;
  color: var(--text-dim);
  display: block;
  margin-top: 2px;
}

.history-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 0;
}

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  gap: 12px;
}

.history-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.history-date {
  font-size: 15px;
  font-weight: 600;
}

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

.history-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.btn-small {
  padding: 8px 16px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

.btn-small.btn-danger {
  color: var(--danger);
  border-color: var(--danger);
  background: transparent;
}

.empty-state, .error-state, .loading {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-dim);
  font-size: 14px;
}

.error-state { color: var(--danger); }

/* ===== Bottom Navigation ===== */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 100;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 11px;
  cursor: pointer;
  transition: color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.nav-item.active {
  color: var(--accent);
}

.nav-icon {
  width: 22px;
  height: 22px;
}

/* Nav icons: only show the right svg per nav item */
.nav-item[data-view="camera"] svg:first-of-type { display: none; }
.nav-item[data-view="camera"] svg:nth-of-type(2) { display: block; }
.nav-item[data-view="history"] svg { display: block; }

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: calc(var(--nav-h) + var(--safe-bottom) + 12px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface2);
  color: var(--text);
  padding: 10px 20px;
  border-radius: 24px;
  font-size: 13px;
  font-weight: 500;
  z-index: 200;
  border: 1px solid var(--border);
  white-space: nowrap;
  max-width: 90vw;
  white-space: normal;
  text-align: center;
  animation: toast-in 0.2s ease;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}

.toast.hidden {
  display: none;
}

.toast-success { border-color: var(--success); color: var(--success); }
.toast-error   { border-color: var(--danger);  color: var(--danger);  }
.toast-warning { border-color: var(--warning); color: var(--warning); }
.toast-info    { border-color: var(--accent);  color: var(--accent);  }

@keyframes toast-in {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0);   }
}

/* ===== Trim View ===== */
.trim-video-wrap {
  flex: 1;
  background: #000;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  min-height: 0;
}

#trim-video {
  max-width: 100%;
  max-height: 100%;
  display: block;
}

.trim-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.25);
  font-size: 13px;
  color: rgba(255,255,255,0.7);
  pointer-events: none;
  transition: opacity 0.3s;
}

.trim-overlay.hidden { opacity: 0; }

.trim-range-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 10px 16px 4px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.trim-point {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 70px;
}

.trim-point-label {
  font-size: 11px;
  color: var(--text-dim);
}

.trim-point-time {
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.trim-arrow {
  font-size: 18px;
  color: var(--text-dim);
}

/* Trim top row */
.trim-top-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px 4px;
  background: var(--bg);
}

/* Dual-handle trim track */
.trim-track-wrap {
  padding: 8px 20px 4px;
  background: var(--bg);
}

.trim-track {
  position: relative;
  height: 48px;
  display: flex;
  align-items: center;
  touch-action: none;
}

.trim-track-bg {
  position: absolute;
  left: 0; right: 0;
  height: 5px;
  background: var(--surface2);
  border-radius: 3px;
}

.trim-selection {
  position: absolute;
  height: 5px;
  background: var(--accent);
  opacity: 0.7;
  border-radius: 3px;
  pointer-events: none;
}

.trim-playhead {
  position: absolute;
  width: 3px;
  height: 24px;
  background: #fff;
  border-radius: 2px;
  top: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  opacity: 0.85;
}

.trim-handle {
  position: absolute;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  cursor: grab;
  touch-action: none;
  z-index: 10;
  box-shadow: 0 2px 8px rgba(0,0,0,0.5);
  border: 3px solid #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
  color: #fff;
}

.trim-handle:active { cursor: grabbing; transform: translate(-50%, -50%) scale(1.15); }

.trim-handle-start { background: #66bb6a; }
.trim-handle-end   { background: var(--danger); }

.trim-track-labels {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 4px;
}

.trim-label-start { font-size: 12px; color: #66bb6a; font-weight: 600; }
.trim-label-end   { font-size: 12px; color: var(--danger); font-weight: 600; }
.trim-label-center { font-size: 11px; color: var(--text-dim); }

.trim-actions {
  display: flex;
  gap: 10px;
  padding: 6px 16px 12px;
  background: var(--bg);
}

.trim-actions .btn-outline { flex: 1; padding: 11px; }
.trim-actions .btn-primary  { flex: 2; padding: 11px; }

/* ===== Manual Trace Bar ===== */
.manual-bar {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.manual-bar.hidden { display: none !important; }

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

.manual-hint {
  flex: 1;
  text-align: center;
  font-size: 12px;
  color: var(--accent);
}

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

.manual-count {
  flex: 1;
  font-size: 12px;
  color: var(--text-dim);
}

/* highlight canvas in manual mode */
#review-canvas.manual-mode {
  cursor: crosshair;
  outline: 2px solid var(--accent);
}

/* ===== Settings Sheet ===== */
.settings-sheet {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 300;
  display: flex;
  align-items: flex-end;
}

.settings-sheet.hidden { display: none !important; }

.settings-inner {
  width: 100%;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 24px 20px calc(20px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.settings-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 15px;
  font-weight: 600;
}

.settings-hint {
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.5;
}

/* ===== Utilities ===== */
.hidden { display: none !important; }
