:root {
  --bg: #121212;
  --surface: #1e1e1e;
  --surface-2: #262626;
  --text: #f2f2f2;
  --text-dim: #9a9a9a;
  --accent: #4fa88a;
  --accent-dim: #2c5c4c;
  --border: #333;
  --flash: #4fa88a;
  --flash-opacity: 0.14;
  --flash-opacity-accent: 0.24;
  --wood-light: #a86b3f;
  --wood-mid: #7a4a28;
  --wood-dark: #4a2c17;
  --wood-plate: #2c1a10;
  --brass: #d8b46a;
  --brass-dark: #8a6a30;
  /* リッチ表現用の追加トーン */
  --wood-hi: #c68a52;
  --wood-shadow: #2a180c;
  --brass-hi: #f4e0ae;
  --brass-shade: #5e4620;
  --grain-alpha: 0.10;
  --sheen-alpha: 0.14;
  --case-glow: rgba(0, 0, 0, 0.55);
  --plate-face: #241408;
  --plate-deep: #140b05;
  color-scheme: dark;
}

:root.light {
  --bg: #f7f7f7;
  --surface: #ffffff;
  --surface-2: #ececec;
  --text: #1a1a1a;
  --text-dim: #666;
  --accent: #3d8c72;
  --accent-dim: #cfe8de;
  --border: #ddd;
  --flash: #3d8c72;
  /* ライト背景(#f7f7f7)は暗色フラッシュとの輝度差がダークテーマよりずっと大きく、
     WCAG 2.3.1のフラッシュ閾値(輝度差10%未満)に抵触しうるため不透明度を抑える。
     TODO: 全画面フラッシュ自体を周辺フレーム点灯など面積の小さい表現に置き換える案を検討中。 */
  --flash-opacity: 0.04;
  --flash-opacity-accent: 0.07;
  --wood-light: #c98a52;
  --wood-mid: #a3673a;
  --wood-dark: #6b4223;
  --wood-plate: #3a2415;
  --brass: #e8c88a;
  --brass-dark: #a3823f;
  --wood-hi: #e3ab72;
  --wood-shadow: #472b16;
  --brass-hi: #fbf0d2;
  --brass-shade: #7a5c2c;
  --grain-alpha: 0.07;
  --sheen-alpha: 0.20;
  --case-glow: rgba(80, 50, 25, 0.30);
  --plate-face: #35200f;
  --plate-deep: #1e1208;
  color-scheme: light;
}

* { box-sizing: border-box; }

@media (prefers-reduced-motion: reduce) {
  /* 振り子はJS側でrAFループ自体を起動しない（visualBeat.js）。
     念のためCSSアニメーションも無効化しておく。 */
  .pendulum {
    animation: none !important;
  }
  #flash-overlay {
    display: none;
  }
  .beat-number,
  .beat-dots .dot,
  .play-btn {
    transition: none;
  }
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overscroll-behavior: none;
  touch-action: manipulation;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

#flash-overlay {
  position: fixed;
  inset: 0;
  background: var(--flash);
  opacity: 0;
  pointer-events: none;
  z-index: 999;
  transition: opacity 80ms ease-out;
}
#flash-overlay.on { opacity: var(--flash-opacity); transition: none; }
#flash-overlay.accent.on { opacity: var(--flash-opacity-accent); }

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) 20px calc(48px + env(safe-area-inset-bottom));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

@media (max-width: 360px) {
  .beat-number { font-size: 3.2rem; }
  .bpm-input { font-size: 2.4rem; }
  .play-btn { width: 76px; height: 76px; font-size: 2rem; }
}

@media (min-width: 480px) {
  .app { padding-top: 40px; }
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.app-header h1 {
  font-size: 1.3rem;
  margin: 0;
  letter-spacing: 0.02em;
}

.icon-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  font-size: 1.1rem;
  cursor: pointer;
}

.metronome-body {
  display: flex;
  justify-content: center;
  perspective: 900px;
}

/* --- 本体ケース（木製ボディ） --- */
.metronome-case {
  position: relative;
  width: min(100%, 288px);
  aspect-ratio: 3 / 4;
  clip-path: polygon(29% 0%, 71% 0%, 97% 100%, 3% 100%);
  background:
    /* 側面の落ち込み */
    linear-gradient(90deg,
      rgba(0, 0, 0, 0.38) 0%,
      rgba(0, 0, 0, 0.10) 14%,
      transparent 34%,
      transparent 62%,
      rgba(0, 0, 0, 0.16) 84%,
      rgba(0, 0, 0, 0.45) 100%),
    /* 上から下への深度 */
    linear-gradient(178deg,
      var(--wood-hi) 0%,
      var(--wood-light) 18%,
      var(--wood-mid) 55%,
      var(--wood-dark) 88%,
      var(--wood-shadow) 100%);
  filter: drop-shadow(0 14px 26px var(--case-glow));
}

/* 木目：年輪風の縦ストライプを2枚重ねる */
.case-grain {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.9;
  background:
    repeating-linear-gradient(94deg,
      rgba(0, 0, 0, var(--grain-alpha)) 0px,
      rgba(0, 0, 0, var(--grain-alpha)) 1px,
      transparent 1px,
      transparent 7px),
    repeating-linear-gradient(88deg,
      rgba(255, 255, 255, 0.05) 0px,
      rgba(255, 255, 255, 0.05) 1px,
      transparent 1px,
      transparent 17px),
    repeating-linear-gradient(91deg,
      rgba(0, 0, 0, 0.05) 0px,
      rgba(0, 0, 0, 0.05) 3px,
      transparent 3px,
      transparent 29px);
}

/* ニス塗装のツヤ：斜めの光沢帯 */
.case-sheen {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(112deg,
      transparent 0%,
      rgba(255, 255, 255, var(--sheen-alpha)) 22%,
      rgba(255, 255, 255, 0.02) 40%,
      transparent 55%),
    radial-gradient(120% 60% at 50% -8%,
      rgba(255, 255, 255, 0.22), transparent 60%),
    radial-gradient(140% 70% at 50% 108%,
      rgba(0, 0, 0, 0.42), transparent 62%);
  mix-blend-mode: screen;
}
:root.light .case-sheen { mix-blend-mode: soft-light; }

/* 縁の面取り（clip-pathで枠線が使えないためinsetシャドウで代替） */
.metronome-case::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  box-shadow:
    inset 0 2px 1px rgba(255, 255, 255, 0.28),
    inset 0 -3px 6px rgba(0, 0, 0, 0.45),
    inset 0 0 40px rgba(0, 0, 0, 0.30);
}

/* 底部の台座ライン */
.case-base {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 9%;
  pointer-events: none;
  background:
    linear-gradient(180deg,
      rgba(0, 0, 0, 0.30) 0%,
      rgba(0, 0, 0, 0.05) 22%,
      rgba(0, 0, 0, 0.35) 78%,
      rgba(0, 0, 0, 0.6) 100%);
  border-top: 1px solid rgba(255, 255, 255, 0.12);
}

.case-inner {
  position: absolute;
  inset: 0;
  padding: 15px 0 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* --- 目盛りスケール（振り子の背板） --- */
.metronome-scale {
  position: relative;
  height: 62%;
  width: 62px;
  display: flex;
  justify-content: center;
  background:
    linear-gradient(90deg,
      rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.05) 30%,
      rgba(0, 0, 0, 0.05) 70%, rgba(0, 0, 0, 0.35)),
    linear-gradient(180deg, var(--wood-shadow), #120a04);
  border-radius: 4px;
  box-shadow:
    inset 0 0 0 1px rgba(0, 0, 0, 0.5),
    inset 0 3px 8px rgba(0, 0, 0, 0.7),
    0 1px 0 rgba(255, 255, 255, 0.10);
}

/* 目盛りの刻み（左右に短い罫線） */
.scale-ticks {
  position: absolute;
  inset: 14% 4px 7%;
  pointer-events: none;
  opacity: 0.6;
  background:
    repeating-linear-gradient(180deg,
      rgba(216, 180, 106, 0.8) 0px,
      rgba(216, 180, 106, 0.8) 1px,
      transparent 1px,
      transparent 12px);
  -webkit-mask-image: linear-gradient(90deg, #000 0 9px, transparent 9px calc(100% - 9px), #000 calc(100% - 9px) 100%);
  mask-image: linear-gradient(90deg, #000 0 9px, transparent 9px calc(100% - 9px), #000 calc(100% - 9px) 100%);
}

/* 中心マーク：振り子がここを通過する瞬間が目視の基準点になる */
.scale-center-mark {
  position: absolute;
  top: 12%;
  bottom: 6%;
  left: 50%;
  width: 1px;
  transform: translateX(-50%);
  background: linear-gradient(180deg,
    transparent 0%,
    rgba(216, 180, 106, 0.55) 10%,
    rgba(216, 180, 106, 0.55) 90%,
    transparent 100%);
  z-index: 1;
  pointer-events: none;
}
.scale-center-mark::before {
  content: "";
  position: absolute;
  top: -3px;
  left: 50%;
  width: 5px;
  height: 5px;
  transform: translateX(-50%) rotate(45deg);
  background: var(--brass);
  box-shadow: 0 0 4px rgba(216, 180, 106, 0.6);
}

/* 支点キャップ（真鍮のネジ頭）: 実物同様、土台側=下端が固定支点 */
.scale-pivot {
  position: absolute;
  bottom: -8px;
  left: 50%;
  width: 18px;
  height: 18px;
  transform: translateX(-50%);
  border-radius: 50%;
  z-index: 3;
  background:
    radial-gradient(circle at 34% 28%,
      var(--brass-hi) 0%,
      var(--brass) 38%,
      var(--brass-dark) 74%,
      var(--brass-shade) 100%);
  box-shadow:
    0 2px 4px rgba(0, 0, 0, 0.6),
    inset 0 -1px 2px rgba(0, 0, 0, 0.45);
}
.scale-pivot::after {
  content: "";
  position: absolute;
  inset: 40% 22%;
  border-radius: 1px;
  background: linear-gradient(180deg, var(--brass-shade), rgba(0, 0, 0, 0.5));
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.22);
}

/* --- 振り子 --- */
/* 支点は下端（土台側）。棒の上部が左右に振れる */
.pendulum {
  position: absolute;
  bottom: 0;
  left: 50%;
  height: 92%;
  width: 5px;
  margin-left: -2.5px;
  transform-origin: bottom center;
  transform: rotate(0deg);
  z-index: 2;
  filter: drop-shadow(2px 4px 5px rgba(0, 0, 0, 0.55));
}
/* 振り子の角度はJS(requestAnimationFrame)がaudioClockに同期して直接指定する。
   CSSアニメーション/transitionは同期を乱すため使わない。 */

.pendulum-rod {
  width: 100%;
  height: 100%;
  border-radius: 2.5px;
  background: linear-gradient(90deg,
    var(--brass-shade) 0%,
    var(--brass-dark) 22%,
    var(--brass-hi) 46%,
    var(--brass) 60%,
    var(--brass-dark) 84%,
    var(--brass-shade) 100%);
}

/* 重りは支点(下端)から離れた上寄りに置く（実物はここをスライドさせてテンポ調整）
   --weight-pos: BPMに応じてJSが設定する重り位置(%, 0=支点付近/速い ~ 1=先端/遅い) */
.pendulum-weight {
  position: absolute;
  top: calc(72% - var(--weight-pos, 0.55) * 52%);
  left: 50%;
  width: 30px;
  height: 18px;
  transform: translateX(-50%);
  transition: top 240ms ease-out;
  border-radius: 4px / 5px;
  cursor: grab;
  touch-action: none;
  background:
    linear-gradient(180deg,
      rgba(255, 255, 255, 0.35) 0%,
      transparent 26%,
      transparent 62%,
      rgba(0, 0, 0, 0.35) 100%),
    linear-gradient(90deg,
      var(--brass-shade) 0%,
      var(--brass-dark) 16%,
      var(--brass-hi) 42%,
      var(--brass) 58%,
      var(--brass-dark) 86%,
      var(--brass-shade) 100%);
  box-shadow:
    0 3px 6px rgba(0, 0, 0, 0.55),
    inset 0 1px 1px rgba(255, 255, 255, 0.45),
    inset 0 -1px 2px rgba(0, 0, 0, 0.4);
}
/* タップ/ドラッグの当たり判定を見た目より広げる */
.pendulum-weight::after {
  content: "";
  position: absolute;
  inset: -14px -10px;
}
.pendulum-weight.dragging,
.pendulum-weight:active {
  cursor: grabbing;
  transition: none;
  box-shadow:
    0 3px 6px rgba(0, 0, 0, 0.55),
    inset 0 1px 1px rgba(255, 255, 255, 0.45),
    inset 0 -1px 2px rgba(0, 0, 0, 0.4),
    0 0 0 2px var(--accent);
}
/* 重りの中央の刻み線 */
.weight-notch {
  position: absolute;
  left: 14%;
  right: 14%;
  top: 50%;
  height: 2px;
  transform: translateY(-50%);
  background: linear-gradient(180deg,
    rgba(0, 0, 0, 0.42), rgba(255, 255, 255, 0.30));
  border-radius: 1px;
}


/* --- 拍表示プレート --- */
.metronome-plate {
  position: relative;
  z-index: 4;
  margin-top: auto;
  width: 68%;
  border-radius: 12px;
  padding: 12px 10px 13px;
  text-align: center;
  overflow: hidden;
  background:
    linear-gradient(180deg, var(--plate-face) 0%, var(--plate-deep) 100%);
  box-shadow:
    inset 0 2px 10px rgba(0, 0, 0, 0.8),
    inset 0 0 0 1px rgba(216, 180, 106, 0.28),
    0 1px 0 rgba(255, 255, 255, 0.14),
    0 4px 10px rgba(0, 0, 0, 0.45);
}
/* ガラスの反射 */
.plate-glass {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(158deg,
      rgba(255, 255, 255, 0.16) 0%,
      rgba(255, 255, 255, 0.04) 30%,
      transparent 48%);
}

.beat-number {
  position: relative;
  font-size: 3rem;
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  color: var(--brass);
  text-shadow:
    0 1px 0 rgba(0, 0, 0, 0.7),
    0 0 12px rgba(216, 180, 106, 0.35);
  transition: color 220ms ease-out, text-shadow 220ms ease-out;
}
.beat-number.pulse {
  color: var(--brass-hi);
  text-shadow:
    0 1px 0 rgba(0, 0, 0, 0.7),
    0 0 18px rgba(216, 180, 106, 0.75);
  transition: none;
}
.beat-number.pulse-accent {
  color: var(--accent);
  text-shadow:
    0 1px 0 rgba(0, 0, 0, 0.7),
    0 0 20px color-mix(in srgb, var(--accent) 70%, transparent);
  transition: none;
}

.beat-dots {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 9px;
  margin-top: 12px;
  min-height: 14px;
}
.beat-dots .dot {
  position: relative;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 34% 30%,
      rgba(216, 180, 106, 0.30), rgba(216, 180, 106, 0.06) 70%);
  box-shadow:
    inset 0 1px 2px rgba(0, 0, 0, 0.7),
    inset 0 0 0 1px rgba(216, 180, 106, 0.35);
  transition: background 240ms ease-out, box-shadow 240ms ease-out;
}
.beat-dots .dot.accent {
  width: 13px;
  height: 13px;
}
.beat-dots .dot.active {
  background:
    radial-gradient(circle at 34% 30%,
      color-mix(in srgb, var(--accent) 55%, #fff),
      var(--accent) 68%);
  box-shadow:
    inset 0 0 0 1px color-mix(in srgb, var(--accent) 70%, #fff),
    0 0 8px color-mix(in srgb, var(--accent) 75%, transparent),
    0 0 16px color-mix(in srgb, var(--accent) 40%, transparent);
  transition: none;
}

.bpm-section {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
}
.bpm-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1.8rem;
  cursor: pointer;
}
.bpm-btn:active { background: var(--surface-2); }

.bpm-value-wrap {
  text-align: center;
  min-width: 140px;
}
.bpm-input {
  width: 100%;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 3rem;
  font-weight: 700;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.bpm-input::-webkit-inner-spin-button { -webkit-appearance: none; }
.bpm-label {
  display: block;
  color: var(--text-dim);
  font-size: 0.85rem;
  letter-spacing: 0.1em;
}

.bpm-slider {
  width: 100%;
  accent-color: var(--accent);
}

.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
}
.play-btn {
  width: 88px;
  height: 88px;
  border-radius: 50%;
  border: none;
  background:
    radial-gradient(circle at 38% 26%,
      color-mix(in srgb, var(--accent) 62%, #fff),
      var(--accent) 58%,
      color-mix(in srgb, var(--accent) 72%, #000) 100%);
  color: #fff;
  font-size: 2.2rem;
  cursor: pointer;
  box-shadow:
    inset 0 1px 1px rgba(255, 255, 255, 0.4),
    inset 0 -2px 4px rgba(0, 0, 0, 0.25),
    0 6px 16px color-mix(in srgb, var(--accent) 35%, transparent),
    0 2px 4px rgba(0, 0, 0, 0.3);
  transition: transform 120ms ease-out, box-shadow 200ms ease-out;
}
.play-btn:active { transform: scale(0.96); }
.play-btn.playing {
  background:
    radial-gradient(circle at 38% 26%, var(--surface-2), var(--surface));
  color: var(--accent);
  box-shadow:
    inset 0 0 0 2px var(--accent),
    inset 0 2px 6px rgba(0, 0, 0, 0.35),
    0 0 18px color-mix(in srgb, var(--accent) 28%, transparent);
}

.secondary-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  padding: 14px 20px;
  font-size: 1rem;
  cursor: pointer;
}

.quick-slots-section h2,
.settings-section h2 {
  font-size: 0.95rem;
  color: var(--text-dim);
  margin: 0 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.quick-slots {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.quick-slot-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 4px;
  cursor: pointer;
  color: var(--text);
  font-size: 1rem;
  font-weight: 600;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}
.quick-slot-btn:active {
  border-color: var(--accent);
}
.quick-slot-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.quick-slots-hint {
  font-size: 0.78rem;
  color: var(--text-dim);
  margin: 6px 0 0;
}

.settings-section details {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
  margin-bottom: 10px;
}
.settings-section summary {
  cursor: pointer;
  font-weight: 600;
  padding: 4px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.summary-hint {
  font-weight: 400;
  font-size: 0.8rem;
  color: var(--text-dim);
}
details[open] .summary-hint {
  display: none;
}

.settings-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 10px;
}
.settings-grid label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.85rem;
  color: var(--text-dim);
}
.settings-grid select,
.settings-grid input[type="number"] {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px;
  font-size: 1rem;
}
.switch-row {
  flex-direction: row !important;
  align-items: center;
  justify-content: space-between;
}
.switch-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}
