:root {
  --bg: #05070f;
  --ink: #e8ecf6;
  --dim: #8d96ad;
  --line: rgba(255, 255, 255, 0.12);
  --panel: rgba(10, 13, 24, 0.82);
  --accent: #ffb257;
  --accent2: #6ea8ff;
  --radius: 12px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP",
               system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

#view {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
}

/* ---------- HUD ---------- */
#hud {
  position: fixed;
  top: calc(12px + var(--safe-t));
  left: calc(14px + var(--safe-l));
  pointer-events: none;
  z-index: 5;
  transition: opacity .25s;
}
#hud h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: .06em;
  text-shadow: 0 1px 12px rgba(0, 0, 0, .9);
}
.hud-line {
  margin-top: 3px;
  font-size: 11px;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 10px rgba(0, 0, 0, .9);
}

#readout {
  position: fixed;
  left: calc(14px + var(--safe-l));
  bottom: calc(14px + var(--safe-b));
  z-index: 5;
  max-width: min(340px, calc(100vw - 100px));
  padding: 10px 12px;
  border-radius: var(--radius);
  background: var(--panel);
  border: 1px solid var(--line);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  font-size: 11.5px;
  line-height: 1.55;
  pointer-events: none;
  transition: opacity .25s;
}
.ro-title {
  font-weight: 600;
  font-size: 12.5px;
  margin-bottom: 6px;
  color: var(--accent);
  letter-spacing: .03em;
}
.ro-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 14px;
  font-variant-numeric: tabular-nums;
}
.ro-grid span { color: var(--dim); }
.ro-grid b { font-weight: 600; text-align: right; }
.dim { color: var(--dim); }

#toast {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(.96);
  padding: 12px 22px;
  border-radius: 999px;
  background: rgba(10, 13, 24, .85);
  border: 1px solid var(--line);
  backdrop-filter: blur(10px);
  font-size: 14px;
  letter-spacing: .08em;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s, transform .3s;
  z-index: 20;
}
#toast.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }

body.hide-ui #hud,
body.hide-ui #readout,
body.hide-ui #panel { opacity: 0; pointer-events: none; }

/* ---------- パネル ---------- */
#panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 320px;
  z-index: 10;
  transition: opacity .25s, transform .3s cubic-bezier(.2, .8, .2, 1);
  transform: translateX(calc(100% - 0px));
}
#panel.open { transform: translateX(0); }

#panel-toggle {
  position: absolute;
  left: -52px;
  top: calc(12px + var(--safe-t));
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  color: var(--ink);
  cursor: pointer;
  transition: transform .3s, background .2s;
}
#panel-toggle:hover { background: rgba(30, 36, 56, .9); }
#panel.open #panel-toggle { transform: rotate(90deg); }

#panel-scroll {
  height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--panel);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-left: 1px solid var(--line);
  padding: calc(14px + var(--safe-t)) 14px calc(20px + var(--safe-b));
  scrollbar-width: thin;
}
#panel-scroll::-webkit-scrollbar { width: 8px; }
#panel-scroll::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, .14);
  border-radius: 4px;
}

#panel-head { margin-bottom: 12px; }
#panel-head strong { font-size: 13px; letter-spacing: .1em; }
#panel-head .dim { display: block; margin-top: 4px; font-size: 10.5px; line-height: 1.5; }

.pwa-btn {
  display: block;
  width: 100%;
  margin-top: 9px;
  padding: 8px 10px;
  border-radius: 9px;
  border: 1px solid rgba(110, 168, 255, .45);
  background: rgba(110, 168, 255, .12);
  color: var(--ink);
  font: inherit;
  font-size: 11.5px;
  cursor: pointer;
  transition: background .15s;
}
.pwa-btn:hover { background: rgba(110, 168, 255, .22); }
.pwa-btn[hidden] { display: none; }
.pwa-btn.update {
  border-color: rgba(255, 178, 87, .5);
  background: rgba(255, 178, 87, .14);
  color: var(--accent);
  font-weight: 600;
}
.pwa-btn.update:hover { background: rgba(255, 178, 87, .24); }

.group { border-top: 1px solid var(--line); }
.group-head {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 11px 2px;
  background: none;
  border: 0;
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .08em;
  cursor: pointer;
}
.group-head .chev { color: var(--dim); transition: transform .2s; }
.group.collapsed .chev { transform: rotate(-90deg); }
.group-body { padding-bottom: 10px; }
.group.collapsed .group-body { display: none; }

.ctrl { display: block; margin-bottom: 11px; }
.ctrl-label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  font-size: 11px;
  color: var(--dim);
  margin-bottom: 5px;
}
.ctrl-value {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  font-size: 11px;
}

input[type=range] {
  width: 100%;
  height: 18px;
  appearance: none;
  -webkit-appearance: none;
  background: none;
  cursor: pointer;
}
input[type=range]::-webkit-slider-runnable-track {
  height: 3px;
  border-radius: 2px;
  background: rgba(255, 255, 255, .16);
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 13px;
  height: 13px;
  margin-top: -5px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px rgba(255, 178, 87, .55);
}
input[type=range]::-moz-range-track {
  height: 3px;
  border-radius: 2px;
  background: rgba(255, 255, 255, .16);
}
input[type=range]::-moz-range-thumb {
  width: 13px;
  height: 13px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px rgba(255, 178, 87, .55);
}

.ctrl-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11.5px;
  cursor: pointer;
  padding: 3px 0;
}
input[type=checkbox] {
  appearance: none;
  -webkit-appearance: none;
  width: 36px;
  height: 20px;
  border-radius: 999px;
  background: rgba(255, 255, 255, .14);
  position: relative;
  cursor: pointer;
  transition: background .2s;
  flex: none;
}
input[type=checkbox]::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  transition: transform .2s;
}
input[type=checkbox]:checked { background: var(--accent); }
input[type=checkbox]:checked::after { transform: translateX(16px); }

select {
  width: 100%;
  padding: 7px 9px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, .06);
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}
select option { background: #10141f; color: var(--ink); }

.btn-row { display: flex; gap: 7px; flex-wrap: wrap; }
.btn {
  flex: 1 1 auto;
  padding: 9px 10px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, .06);
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  transition: background .15s, transform .1s;
  white-space: nowrap;
}
.btn:hover { background: rgba(255, 255, 255, .13); }
.btn:active { transform: scale(.97); }
.btn.primary {
  background: linear-gradient(180deg, #ffc274, #ff9a3c);
  border-color: transparent;
  color: #21160a;
  font-weight: 700;
}

.note {
  margin: 8px 0 2px;
  font-size: 10.5px;
  line-height: 1.6;
  color: var(--dim);
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, .04);
  border-left: 2px solid rgba(255, 178, 87, .5);
}

#panel-foot {
  margin-top: 16px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
  font-size: 11px;
  color: var(--dim);
  line-height: 1.7;
}
#panel-foot summary { cursor: pointer; font-size: 11.5px; color: var(--ink); }
#panel-foot details + details { margin-top: 10px; }
#panel-foot .lead { margin: 9px 0 6px; font-size: 10.5px; line-height: 1.65; }

.sounds {
  list-style: none;
  counter-reset: snd;
  padding: 0;
  margin: 8px 0 0;
}
.sounds li {
  counter-increment: snd;
  position: relative;
  padding: 7px 0 7px 20px;
  border-top: 1px solid var(--line);
  font-size: 10.5px;
  line-height: 1.6;
}
.sounds li::before {
  content: counter(snd);
  position: absolute;
  left: 0;
  top: 7px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: rgba(255, 178, 87, .18);
  color: var(--accent);
  font-size: 9px;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.sounds b { display: inline; color: var(--ink); }
.sounds .when {
  display: block;
  color: var(--accent);
  font-size: 9.5px;
  letter-spacing: .04em;
  margin: 1px 0 3px;
}

.timeline {
  margin: 6px 0 0;
  padding: 9px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, .04);
  border-left: 2px solid rgba(110, 168, 255, .5);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 9.5px;
  line-height: 1.75;
  color: var(--dim);
  overflow-x: auto;
  white-space: pre;
}
#panel-foot ul { padding-left: 18px; margin: 10px 0 0; }
#panel-foot li { margin-bottom: 7px; }
#panel-foot b { color: var(--ink); }

/* ---------- 起動 / エラー ---------- */
#loading, #fatal {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: var(--bg);
  text-align: center;
  font-size: 13px;
  color: var(--dim);
}
#fatal { display: none; }
#fatal h2 { color: var(--ink); font-size: 17px; margin: 0 0 10px; }
#fatal p { margin: 5px 0; max-width: 34ch; }
.spark {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 30px 6px rgba(255, 178, 87, .8);
  animation: pulse 1.1s ease-in-out infinite;
  margin: 0 auto 14px;
}
@keyframes pulse {
  0%, 100% { transform: scale(.6); opacity: .5; }
  50% { transform: scale(1.4); opacity: 1; }
}
#loading p { margin: 0; }
#loading > div, #loading > p { grid-area: 1 / 1; }
#loading { grid-auto-flow: row; align-content: center; }

@media (max-width: 720px) {
  #panel { width: min(300px, 88vw); }
  #readout { max-width: calc(100vw - 90px); font-size: 11px; }
  #hud h1 { font-size: 13px; }
}
