:root {
  color-scheme: dark;
  --ink: #fff6ee;
  --muted: #e4cfc0;
  --panel: rgba(28, 12, 16, 0.9);
  --line: rgba(255, 220, 190, 0.16);
  --leaf: #c4473a;
  --leaf-deep: #6e2430;
  --control: #3a1820;
  --chroma-green: #00ff00;
  --chroma-magenta: #ff00ff;
  --chroma-blue: #0000ff;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: transparent;
  overflow: hidden;
  font-family: "Hiragino Maru Gothic ProN", "Yu Gothic UI", "Hiragino Kaku Gothic ProN", Meiryo, sans-serif;
}
html[data-bg="green"], html[data-bg="green"] body { background: var(--chroma-green); }
html[data-bg="blue"], html[data-bg="blue"] body { background: var(--chroma-blue); }
html[data-bg="magenta"], html[data-bg="magenta"] body { background: var(--chroma-magenta); }
html[data-bg="dark"], html[data-bg="dark"] body { background: #14171c; }
html[data-bg="light"], html[data-bg="light"] body { background: #f6f3ee; }
html[data-bg="sky"], html[data-bg="sky"] body { background: #bfe8ff; }
html[data-bg="transparent"], html[data-bg="transparent"] body { background: transparent; }

#stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
}
#stage.dragging { cursor: grabbing; }

#panel {
  position: fixed;
  z-index: 5;
  top: 14px;
  left: 14px;
  width: min(332px, calc(100vw - 28px));
  max-height: calc(100vh - 28px);
  overflow: auto;
  padding: 14px 14px 12px;
  border-radius: 16px;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(10px);
}
#panel h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0.04em;
}
#panel .sub {
  margin: 2px 0 10px;
  color: var(--muted);
  font-size: 12px;
}
.row, .field {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 8px 0;
  font-size: 13px;
}
.row { flex-wrap: wrap; }
.field { flex-direction: column; align-items: stretch; }
.field span, .row span { color: var(--muted); }
label.chk {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}
select, input[type="range"] { width: 100%; accent-color: var(--leaf); }
select {
  background: var(--control);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 8px;
}
button, .btn {
  border: 0;
  border-radius: 9px;
  background: var(--leaf-deep);
  color: white;
  font: inherit;
  font-weight: 700;
  padding: 8px 10px;
  cursor: pointer;
}
button:hover, .btn:hover { filter: brightness(1.12); }
button:focus-visible, select:focus-visible, input:focus-visible {
  outline: 3px solid #e9c774;
  outline-offset: 2px;
}
button.stop { background: #8d3a3a; }
button[aria-pressed="true"] { background: var(--leaf); }
.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.actions { display: flex; gap: 6px; margin-top: 8px; }
.actions button { flex: 1; font-size: 12px; line-height: 1.3; padding: 8px 6px; }
.accents, .vowels {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 10px;
}
.vowels button { min-width: 40px; }
#status {
  min-height: 2.6em;
  margin: 8px 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  color: #d5e6d6;
  font-size: 12px;
  line-height: 1.5;
}
.hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.5;
}
#manual { margin-top: 8px; }

#videoWrap {
  position: fixed;
  z-index: 4;
  top: 14px;
  right: 14px;
  width: 220px;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #000;
}
#cam {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1);
}
html[data-mirror="0"] #cam { transform: none; }
#faceBadge,
#trackFps {
  position: absolute;
  bottom: 6px;
  padding: 2px 6px;
  border-radius: 999px;
  font-size: 10px;
  line-height: 1.4;
  color: white;
  pointer-events: none;
}
#faceBadge:not([hidden]) {
  display: inline-block;
  left: 6px;
  background: var(--leaf-deep);
}
#trackFps {
  right: 6px;
  background: rgba(0, 0, 0, 0.62);
}

#toast {
  position: fixed;
  z-index: 6;
  left: 50%;
  top: 18px;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--panel);
  color: white;
  font-size: 13px;
  pointer-events: none;
}

html[data-clean="1"] #panel,
html[data-clean="1"] #videoWrap,
html[data-clean="1"] #engine-badge { display: none !important; }
#panel .charrow { display: flex; gap: 8px; align-items: center; margin: 4px 0 6px; }
#panel .charrow select { flex: 1; font-weight: 700; }
#panel .brand { font-size: 10px; letter-spacing: 0.12em; color: var(--muted); margin: 0 0 2px; }
#panel, select, button { transition: background-color 0.25s ease, border-color 0.25s ease; }

.panel-fold {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 44px;
  margin: -2px 0 8px;
  padding: 8px 10px;
  border: 0;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.07);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.02em;
  cursor: pointer;
}
.panel-fold:hover { filter: none; background: rgba(255, 255, 255, 0.14); }
.panel-fold-bar {
  display: block;
  width: 36px;
  height: 4px;
  border-radius: 999px;
  background: var(--muted);
  box-shadow: 0 5px 0 var(--muted);
  margin-top: -4px;
}
.panel-open {
  position: fixed;
  z-index: 7;
  right: max(10px, env(safe-area-inset-right));
  bottom: max(10px, env(safe-area-inset-bottom));
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  display: none;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: rgba(28, 12, 16, 0.55);
  color: var(--ink);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.28);
  backdrop-filter: blur(8px);
  opacity: 0.72;
  cursor: pointer;
}
.panel-open:hover { filter: none; opacity: 0.95; background: rgba(28, 12, 16, 0.78); }
.panel-open-icon {
  display: block;
  width: 16px;
  height: 10px;
  border: 2px solid var(--ink);
  border-radius: 3px;
  position: relative;
}
.panel-open-icon::before {
  content: "";
  position: absolute;
  left: 2px;
  right: 2px;
  top: 2px;
  height: 2px;
  background: var(--ink);
  box-shadow: 0 3px 0 var(--ink);
  opacity: 0.9;
}
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

#panel {
  transition:
    background-color 0.25s ease,
    border-color 0.25s ease,
    transform 0.28s ease,
    opacity 0.28s ease,
    visibility 0.28s ease;
}
html[data-panel="0"] #panel {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateX(calc(-100% - 24px));
}
html[data-panel="0"] #videoWrap { display: none; }
html[data-panel="0"] #panelOpen:not([hidden]) { display: flex; }
html[data-clean="1"] #panelOpen { display: none !important; }

@media (max-width: 720px) {
  /* スマホ: パネルは下のシートにして、上半分にアバターの顔が見えるようにする */
  #panel { top: auto; bottom: 10px; left: 10px; width: calc(100vw - 20px); max-height: 44vh; }
  #videoWrap { width: 140px; top: 10px; bottom: auto; }
  html[data-panel="0"] #panel { transform: translateY(110%); }
}

@media (prefers-reduced-motion: reduce) {
  #panel, .panel-open, .panel-fold {
    transition: none;
  }
  html[data-panel="0"] #panel { transform: none; }
}

#panel .charlabel { font-size: 12px; color: var(--muted); white-space: nowrap; }
