:root {
  color-scheme: dark;
  --bg: #07110f;
  --panel: #10251d;
  --panel-2: #163325;
  --text: #f4f7ee;
  --muted: #a8b8ad;
  --line: rgba(255, 255, 255, 0.12);
  --accent: #ffcf56;
  --accent-2: #f25f4c;
  --green: #2fb36d;
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.36);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100svh;
  background:
    radial-gradient(circle at 50% 0%, rgba(47, 179, 109, 0.18), transparent 36rem),
    linear-gradient(180deg, #07110f 0%, #0b1714 45%, #111717 100%);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: min(100%, 520px);
  min-height: 100svh;
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) 14px max(160px, env(safe-area-inset-bottom));
  display: grid;
  gap: 12px;
}

.scoreboard,
.camera-panel,
.control-panel,
.stats-panel,
.history-panel {
  border: 1px solid var(--line);
  background: rgba(16, 37, 29, 0.88);
  box-shadow: var(--shadow);
}

.scoreboard {
  overflow: hidden;
  border-radius: 8px;
  padding: 18px;
}

.brand-row,
.section-heading,
.action-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.section-heading h2 {
  min-width: 0;
  flex: 1 1 auto;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.3rem, 8vw, 2.25rem);
  line-height: 1.05;
}

h2 {
  font-size: 1rem;
}

.icon-button {
  width: 44px;
  height: 44px;
  flex: 0 0 44px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
  font-size: 1.1rem;
}

.icon-button.off {
  color: var(--muted);
}

.speed-readout {
  display: flex;
  align-items: baseline;
  justify-content: center;
  margin: 18px 0 6px;
  font-variant-numeric: tabular-nums;
}

#speedValue {
  font-size: clamp(4.8rem, 28vw, 8rem);
  font-weight: 900;
  line-height: 0.9;
}

.unit {
  margin-left: 8px;
  color: var(--muted);
  font-size: 1.1rem;
  font-weight: 800;
}

.status {
  min-height: 44px;
  color: var(--muted);
  line-height: 1.45;
  text-align: center;
}

.field-visual {
  position: relative;
  height: 112px;
  margin-top: 16px;
  border-radius: 8px;
  overflow: hidden;
  background:
    linear-gradient(150deg, rgba(255, 255, 255, 0.08) 0 1px, transparent 1px 32px),
    linear-gradient(180deg, #23613c 0%, #194b32 100%);
}

.lane {
  position: absolute;
  left: 22px;
  right: 22px;
  top: 54px;
  height: 20px;
  border-top: 2px dashed rgba(255, 255, 255, 0.55);
}

.mound,
.plate {
  position: absolute;
  top: -13px;
  width: 34px;
  height: 26px;
  background: #d49c55;
}

.mound {
  left: 0;
  border-radius: 50%;
}

.plate {
  right: 0;
  clip-path: polygon(50% 100%, 0 45%, 12% 0, 88% 0, 100% 45%);
  background: #f4f7ee;
}

.baseball {
  position: absolute;
  left: 18px;
  top: 20px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #f8f4e8;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.28);
  transition: transform 420ms cubic-bezier(0.18, 0.88, 0.3, 1.15);
}

.baseball.flying {
  transform: translateX(calc(min(100vw, 520px) - 98px)) rotate(520deg);
}

.baseball span,
.baseball::before,
.baseball::after {
  position: absolute;
  content: "";
}

.baseball::before,
.baseball::after {
  top: 6px;
  width: 10px;
  height: 22px;
  border: 2px solid #d4453b;
  border-color: transparent #d4453b transparent transparent;
  border-radius: 50%;
}

.baseball::before {
  left: 5px;
}

.baseball::after {
  right: 5px;
  transform: rotate(180deg);
}

.ball-trail {
  position: absolute;
  left: 42px;
  right: 42px;
  top: -3px;
  height: 4px;
  transform: scaleX(0);
  transform-origin: left;
  background: linear-gradient(90deg, var(--accent), transparent);
}

.ball-trail.active {
  animation: trail 420ms ease-out;
}

@keyframes trail {
  0% { transform: scaleX(0); opacity: 0; }
  18% { opacity: 1; }
  100% { transform: scaleX(1); opacity: 0; }
}

.camera-panel,
.control-panel,
.stats-panel,
.history-panel {
  border-radius: 8px;
  padding: 14px;
}

.collapsible-section {
  overflow: hidden;
}

.heading-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex: 0 0 auto;
  gap: 8px;
  margin-left: auto;
}

.card-collapse-button {
  position: relative;
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.09);
  color: var(--text);
  padding: 0;
  transition: background 160ms ease;
}

.card-collapse-button::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 8px;
  height: 8px;
  border-left: 2px solid currentColor;
  border-top: 2px solid currentColor;
  transform: translate(-50%, -36%) rotate(45deg);
}

.collapsible-section.section-collapsed .card-collapse-button::before {
  transform: translate(-50%, -64%) rotate(225deg);
}

.card-collapse-button:active {
  transform: scale(0.96);
}

.collapsible-section.section-collapsed .collapsible-body {
  display: none;
}

.collapsible-section.section-collapsed {
  padding-bottom: 14px;
}

.camera-panel {
  overflow: hidden;
}

.camera-badge {
  min-width: 58px;
  padding: 5px 9px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
  text-align: center;
}

.camera-badge.active {
  background: rgba(47, 179, 109, 0.2);
  color: #a7f1c7;
}

.mode-toggle {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px;
  margin-top: 12px;
  padding: 4px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.22);
}

.mode-button {
  min-height: 38px;
  border-radius: 6px;
  color: var(--muted);
  background: transparent;
  font-weight: 900;
}

.mode-button.active {
  color: #241806;
  background: var(--accent);
}

.camera-frame {
  position: relative;
  min-height: 238px;
  margin-top: 12px;
  overflow: hidden;
  border-radius: 8px;
  background: #050807;
  border: 1px solid var(--line);
}

.camera-frame video {
  width: 100%;
  height: 100%;
  min-height: 238px;
  display: block;
  object-fit: cover;
  background: #050807;
}

#analysisCanvas {
  display: none;
}

.camera-placeholder,
.camera-tap-layer,
.camera-guide,
.camera-speed-overlay,
.auto-lines {
  position: absolute;
  inset: 0;
}

.camera-speed-overlay {
  z-index: 2;
  pointer-events: none;
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 12px;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.58), transparent 42%);
}

.overlay-speed-readout {
  display: flex;
  align-items: baseline;
  justify-content: center;
  width: 100%;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.84);
}

#overlaySpeedValue {
  font-size: clamp(3.4rem, 19vw, 5.8rem);
  font-weight: 950;
  line-height: 0.9;
}

.overlay-unit {
  margin-left: 6px;
  color: rgba(255, 255, 255, 0.86);
  font-size: 0.92rem;
  font-weight: 900;
}

#overlayStatusText {
  position: absolute;
  left: 12px;
  right: 12px;
  top: clamp(72px, 22vw, 102px);
  color: rgba(255, 255, 255, 0.88);
  font-size: 0.8rem;
  font-weight: 800;
  line-height: 1.35;
  text-align: center;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.84);
}

.camera-placeholder {
  display: grid;
  place-content: center;
  gap: 8px;
  padding: 24px;
  color: var(--muted);
  text-align: center;
  background:
    linear-gradient(150deg, rgba(255, 255, 255, 0.08) 0 1px, transparent 1px 28px),
    #0b1411;
}

.camera-placeholder strong {
  color: var(--text);
  font-size: 1.1rem;
}

.camera-placeholder.hidden {
  display: none;
}

.camera-tap-layer {
  z-index: 3;
  width: 100%;
  height: 100%;
  display: grid;
  place-items: end center;
  padding: 14px;
  background: transparent;
  color: var(--text);
}

.camera-tap-layer span {
  max-width: 100%;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.52);
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.82rem;
  font-weight: 800;
}

.camera-guide {
  z-index: 2;
  pointer-events: none;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  color: rgba(255, 255, 255, 0.72);
  font-size: 0.74rem;
  font-weight: 800;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

.camera-guide i {
  height: 2px;
  background: linear-gradient(90deg, transparent, rgba(255, 207, 86, 0.92), transparent);
}

.auto-lines {
  z-index: 2;
  pointer-events: none;
  display: none;
}

.camera-panel.auto-mode .auto-lines {
  display: block;
}

.measure-line {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 3px;
  transform: translateX(-50%);
  background: rgba(255, 207, 86, 0.92);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.45), 0 0 18px rgba(255, 207, 86, 0.4);
}

.measure-line::before {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  padding: 3px 7px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.58);
  color: #fff;
  font-size: 0.68rem;
  font-weight: 900;
  white-space: nowrap;
}

.measure-line.start {
  left: 18%;
}

.measure-line.start::before {
  content: "START";
}

.measure-line.finish {
  left: 82%;
  background: rgba(242, 95, 76, 0.95);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.45), 0 0 18px rgba(242, 95, 76, 0.4);
}

.measure-line.finish::before {
  content: "FINISH";
}

.motion-dot {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 16px;
  height: 16px;
  display: none;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--accent-2);
  box-shadow: 0 0 18px rgba(242, 95, 76, 0.75);
  transform: translate(-50%, -50%);
}

.motion-dot.visible {
  display: block;
}

.camera-actions .secondary-button {
  margin-top: 0;
}

.auto-panel {
  margin-top: 12px;
  display: grid;
  gap: 10px;
}

.auto-panel.hidden {
  display: none;
}

.auto-status {
  min-height: 40px;
  padding: 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.2);
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.45;
}

.range-control {
  display: grid;
  grid-template-columns: 88px 1fr;
  align-items: center;
  gap: 10px;
}

.range-control span {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
}

.range-control input {
  width: 100%;
  accent-color: var(--accent);
}

.auto-arm-button {
  min-height: 50px;
  border-radius: 8px;
  background: rgba(47, 179, 109, 0.2);
  color: #a7f1c7;
  font-weight: 950;
}

.auto-arm-button.armed {
  background: linear-gradient(180deg, #ffcf56 0%, #f25f4c 100%);
  color: #241806;
}

.distance-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.distance-option {
  min-width: 0;
  padding: 10px 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
}

.distance-option span,
.stat span,
.history-meta,
.custom-distance span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
}

.distance-option strong {
  display: block;
  margin-top: 2px;
  font-size: clamp(0.82rem, 3.7vw, 1rem);
}

.distance-option.active {
  border-color: rgba(255, 207, 86, 0.88);
  background: rgba(255, 207, 86, 0.18);
}

.custom-distance {
  margin-top: 10px;
  display: grid;
  grid-template-columns: 1fr 96px auto;
  align-items: center;
  gap: 8px;
}

.custom-distance input {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.22);
  color: var(--text);
  padding: 8px 10px;
  text-align: right;
}

.tap-button {
  width: 100%;
  min-height: 176px;
  margin-top: 14px;
  border-radius: 8px;
  background:
    radial-gradient(circle at 50% 32%, rgba(255, 255, 255, 0.24), transparent 22%),
    linear-gradient(180deg, #ffcf56 0%, #f25f4c 100%);
  color: #23170b;
  box-shadow: inset 0 -8px 0 rgba(0, 0, 0, 0.18), 0 16px 34px rgba(242, 95, 76, 0.25);
}

.tap-button:active {
  transform: translateY(2px);
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.18), 0 10px 22px rgba(242, 95, 76, 0.2);
}

.tap-button span {
  display: block;
  font-size: clamp(2.2rem, 14vw, 4rem);
  font-weight: 950;
  line-height: 1;
}

.tap-button small {
  display: block;
  margin-top: 8px;
  font-weight: 800;
}

.secondary-button,
.text-button {
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
}

.secondary-button {
  width: 100%;
  min-height: 48px;
  margin-top: 10px;
  border-radius: 8px;
}

.text-button {
  min-height: 34px;
  padding: 0 10px;
  border-radius: 8px;
  color: var(--muted);
}

.stats-panel {
  display: block;
  background: rgba(16, 37, 29, 0.88);
}

.stats-heading {
  margin-bottom: 10px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1px;
  overflow: hidden;
  border-radius: 8px;
  background: var(--line);
}

.stat {
  padding: 12px;
  background: rgba(16, 37, 29, 0.96);
  text-align: center;
}

.stat strong {
  display: block;
  margin-top: 2px;
  font-size: 1.45rem;
  font-variant-numeric: tabular-nums;
}

.history-list {
  min-height: 54px;
  margin: 10px 0 0;
  padding: 0;
  list-style: none;
}

.history-list:empty::before {
  content: "まだ計測がありません";
  display: block;
  padding: 14px 0;
  color: var(--muted);
  text-align: center;
}

.history-item {
  display: grid;
  grid-template-columns: 40px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-top: 1px solid var(--line);
}

.history-rank {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  color: var(--muted);
  font-size: 0.82rem;
}

.history-speed {
  font-size: 1.2rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

@media (orientation: portrait) and (max-width: 720px) {
  .scoreboard .speed-readout,
  .scoreboard .status {
    display: none;
  }

  .scoreboard {
    padding-bottom: 14px;
  }

  .camera-speed-overlay {
    display: flex;
  }

  .camera-frame {
    min-height: min(78svh, 620px);
  }

  .camera-frame video {
    min-height: min(78svh, 620px);
  }
}

@media (orientation: landscape) and (min-width: 720px) {
  .app-shell {
    width: 100%;
    max-width: 1180px;
    grid-template-columns: minmax(260px, 360px) minmax(420px, 1fr);
    align-items: start;
    padding: max(14px, env(safe-area-inset-top)) 16px max(90px, env(safe-area-inset-bottom));
  }

  .scoreboard {
    grid-column: 1;
    position: sticky;
    top: 16px;
  }

  .camera-panel {
    grid-column: 2;
    grid-row: span 3;
  }

  .control-panel,
  .stats-panel,
  .history-panel {
    grid-column: 1;
  }

  .camera-speed-overlay {
    display: none;
  }

  .camera-frame {
    min-height: calc(100svh - 150px);
  }

  .camera-frame video {
    min-height: calc(100svh - 150px);
  }

  #speedValue {
    font-size: clamp(4rem, 9vw, 6.2rem);
  }
}

@media (min-width: 720px) and (orientation: portrait) {
  .app-shell {
    padding-top: 24px;
  }
}
