:root {
  --bg: #000000;
  --surface: #0a0a0a;
  --surface-2: #141414;
  --text: #f0f0f0;
  --muted: #555;
  --line: #1a1a1a;
  --accent: #ffffff;
  --red: #ff3b30;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Kaku Gothic ProN",
    "Yu Gothic", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

button {
  cursor: pointer;
  font-family: inherit;
}

/* --------------------------------
   App Shell
   -------------------------------- */

.app-shell {
  max-width: 430px;
  margin: 0 auto;
  min-height: 100dvh;
  padding: max(env(safe-area-inset-top, 0px), 12px) 20px
    calc(76px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
}

/* --------------------------------
   Header
   -------------------------------- */

.app-header {
  padding: 8px 0 24px;
}

.brand {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.status {
  font-size: 11px;
  color: var(--muted);
  margin-top: 3px;
  font-family: "SF Mono", ui-monospace, "Menlo", "Monaco", monospace;
  letter-spacing: 0.02em;
}

/* --------------------------------
   Views
   -------------------------------- */

.view {
  display: none;
  flex: 1;
}

.view.is-active {
  display: flex;
  flex-direction: column;
}

/* --------------------------------
   Stages
   -------------------------------- */

.stage {
  display: none;
}

.stage.is-active {
  display: flex;
  flex-direction: column;
  flex: 1;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.stage-hint {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 12px;
}

/* --------------------------------
   Idle Hero
   -------------------------------- */

.idle-hero {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  padding: 40px 0 60px;
}

.idle-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 14px;
}

.idle-hint {
  font-size: 20px;
  font-weight: 300;
  line-height: 1.7;
  color: var(--text);
  letter-spacing: 0.04em;
}

.idle-cta {
  margin-top: 40px;
  width: 100%;
  max-width: 260px;
}

/* --------------------------------
   Capture Area
   -------------------------------- */

.capture-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  border-radius: 16px;
  overflow: hidden;
  background: var(--surface);
}

#cameraVideo,
#previewCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* --------------------------------
   Recording Countdown
   -------------------------------- */

.record-progress {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: rgba(255, 255, 255, 0.12);
  opacity: 0;
  transition: opacity 0.2s ease;
}

.record-progress.is-recording {
  opacity: 1;
}

.record-progress-bar {
  height: 100%;
  background: var(--red);
  transform-origin: left;
  transform: scaleX(1);
  transition: transform 0.1s linear;
}

.countdown-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  background: rgba(255, 59, 48, 0.88);
  color: #fff;
  border-radius: 7px;
  padding: 3px 8px;
  font-family: "SF Mono", ui-monospace, "Menlo", "Monaco", monospace;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.04em;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.countdown-badge.is-recording {
  opacity: 1;
}

/* --------------------------------
   Camera Controls
   -------------------------------- */

.camera-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px 4px 8px;
}

.controls-spacer {
  width: 56px;
}

.btn-text {
  background: none;
  border: none;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  padding: 8px 12px;
  width: 56px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.6;
  transition: opacity 0.15s ease;
}

.btn-text svg {
  width: 22px;
  height: 22px;
  display: block;
}

.btn-text:active {
  opacity: 0.3;
}

.btn-text:disabled {
  opacity: 0.15;
  pointer-events: none;
}

/* --------------------------------
   Record Button
   -------------------------------- */

.record-btn {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.85);
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: transform 0.15s ease;
}

.record-btn:active {
  transform: scale(0.92);
}

.record-btn:disabled {
  opacity: 0.25;
  pointer-events: none;
}

.record-btn-inner {
  display: block;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--red);
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.record-btn.is-recording .record-btn-inner {
  width: 26px;
  height: 26px;
  border-radius: 6px;
  background: var(--red);
  animation: pulse 1.2s ease-in-out infinite;
}

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

/* --------------------------------
   Action Row
   -------------------------------- */

.action-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 20px 0 8px;
}

.confirm-actions {
  grid-template-columns: 48px 1fr;
}

/* --------------------------------
   Icon Button
   -------------------------------- */

.btn-icon {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  border: none;
  background: var(--surface-2);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.15s ease, transform 0.1s ease;
  flex-shrink: 0;
}

.btn-icon svg {
  width: 20px;
  height: 20px;
  display: block;
}

.btn-icon:active {
  opacity: 0.6;
  transform: scale(0.94);
}

.btn-icon:disabled {
  opacity: 0.25;
  pointer-events: none;
}

/* --------------------------------
   Buttons
   -------------------------------- */

.btn {
  width: 100%;
  border: none;
  border-radius: 12px;
  min-height: 48px;
  padding: 13px 16px;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.01em;
  transition: opacity 0.15s ease, transform 0.1s ease;
}

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

.btn:disabled {
  opacity: 0.25;
  pointer-events: none;
}

.btn-primary {
  background: var(--accent);
  color: #000;
}

.btn-ghost {
  background: var(--surface-2);
  color: var(--text);
}

/* --------------------------------
   Form
   -------------------------------- */

.name-form {
  padding: 8px 0;
}

label {
  display: block;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 8px;
}

input {
  width: 100%;
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  padding: 13px 14px;
  font-size: 16px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s ease;
}

input::placeholder {
  color: #333;
}

input:focus {
  border-color: #333;
}

/* --------------------------------
   Bottom Tab Bar
   -------------------------------- */

.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  background: rgba(0, 0, 0, 0.82);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--line);
  padding: 6px 0 max(env(safe-area-inset-bottom, 4px), 6px);
  z-index: 100;
}

.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 10px;
  font-weight: 500;
  padding: 4px 0;
  letter-spacing: 0.02em;
  transition: color 0.2s ease;
}

.tab.is-active {
  color: var(--accent);
}

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

/* --------------------------------
   Browse Toggle
   -------------------------------- */

.browse-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  background: var(--surface);
  border-radius: 10px;
  padding: 3px;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.toggle-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  padding: 7px;
  border-radius: 7px;
  letter-spacing: 0.02em;
  transition: background 0.15s ease, color 0.15s ease;
}

.toggle-btn.is-active {
  background: var(--surface-2);
  color: var(--text);
}

/* --------------------------------
   Gallery
   -------------------------------- */

.gallery {
  list-style: none;
  display: grid;
  gap: 0;
}

.empty {
  text-align: center;
  color: var(--muted);
  padding: 80px 20px;
  font-size: 13px;
  line-height: 1.7;
}

.item {
  display: grid;
  grid-template-columns: 52px 1fr auto;
  gap: 14px;
  align-items: center;
  padding: 16px 0;
  border-bottom: 1px solid var(--line);
}

.item:last-child {
  border-bottom: none;
}

.thumb {
  width: 52px;
  height: 68px;
  border-radius: 8px;
  object-fit: cover;
  background: var(--surface-2);
}

.item-body {
  min-width: 0;
}

.item-title {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-meta {
  margin-top: 3px;
  color: var(--muted);
  font-size: 11px;
  font-family: "SF Mono", ui-monospace, "Menlo", "Monaco", monospace;
  letter-spacing: 0.01em;
}

.user-tag {
  color: var(--muted);
  font-size: 10px;
  letter-spacing: 0.05em;
}

.empty.is-loading {
  animation: pulse 1.5s ease-in-out infinite;
}

.item-action {
  background: none;
  border: none;
  color: var(--muted);
  padding: 10px;
  border-radius: 8px;
  transition: background 0.15s ease, color 0.15s ease;
}

.item-action:active {
  background: var(--surface-2);
  color: var(--text);
}

.item-action svg {
  width: 18px;
  height: 18px;
  display: block;
}

/* --------------------------------
   Utilities
   -------------------------------- */

::-webkit-scrollbar {
  display: none;
}

::selection {
  background: rgba(255, 255, 255, 0.12);
}

/* --------------------------------
   Gallery item — clickable
   -------------------------------- */

.item {
  cursor: pointer;
}

/* --------------------------------
   Preview Modal
   -------------------------------- */

.preview-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-modal[hidden] {
  display: none;
}

.modal-close {
  position: absolute;
  top: max(env(safe-area-inset-top, 0px), 12px);
  right: 16px;
  z-index: 10;
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: none;
  border-radius: 50%;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
}

.modal-close:active {
  background: rgba(255, 255, 255, 0.24);
}

.modal-close svg {
  width: 18px;
  height: 18px;
}

.modal-canvas {
  display: block;
}

.modal-info {
  position: absolute;
  bottom: max(env(safe-area-inset-bottom, 0px), 16px);
  left: 20px;
  right: 20px;
  pointer-events: none;
}

.modal-title-text {
  font-size: 15px;
  font-weight: 600;
  color: #fff;
}

.modal-meta-text {
  margin-top: 3px;
  font-size: 11px;
  color: rgba(255, 255, 255, 0.45);
  font-family: "SF Mono", ui-monospace, "Menlo", "Monaco", monospace;
  letter-spacing: 0.01em;
}

/* --------------------------------
   PC Layout (≥ 700px)
   -------------------------------- */

@media (min-width: 700px) {
  /* フルワイド */
  .app-shell {
    max-width: 100%;
    padding-left: 40px;
    padding-right: 40px;
  }

  /* カメラ / 確認ステージ: capture-wrap が縦方向に伸びる */
  #stageCamera .capture-wrap,
  #stageConfirm .capture-wrap {
    flex: 1;
    aspect-ratio: unset;
    min-height: 320px;
  }

  /* PC ウェブカメラは左右反転（ミラー表示） */
  #cameraVideo {
    transform: scaleX(-1);
  }

  /* 名前入力フォームは幅を抑える */
  .name-form {
    max-width: 520px;
    margin: 0 auto;
    width: 100%;
  }

  /* ギャラリー: グリッドカード */
  .gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 16px;
  }

  .gallery .empty,
  .gallery .empty.is-loading {
    grid-column: 1 / -1;
  }

  /* カードレイアウト */
  .item {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: 200px 1fr;
    border-bottom: none;
    border-radius: 12px;
    overflow: hidden;
    background: var(--surface);
    padding: 0;
    gap: 0;
    align-items: stretch;
    transition: opacity 0.15s ease, transform 0.12s ease;
  }

  .item:hover {
    opacity: 0.88;
  }

  .item:active {
    transform: scale(0.97);
    opacity: 1;
  }

  .thumb {
    grid-column: 1 / 3;
    grid-row: 1;
    width: 100%;
    height: 200px;
    border-radius: 0;
  }

  .item-body {
    grid-column: 1;
    grid-row: 2;
    padding: 10px 6px 12px 12px;
  }

  .item-action {
    grid-column: 2;
    grid-row: 2;
    align-self: end;
    padding: 8px;
  }
}
