:root {
  --bg: #090405;
  --panel: rgba(20, 9, 10, 0.72);
  --panel-strong: rgba(12, 7, 8, 0.86);
  --line: rgba(216, 184, 119, 0.28);
  --gold: #d8b877;
  --red: #b43a31;
  --teal: #4fd6c0;
  --text: #f4ede4;
  --muted: rgba(244, 237, 228, 0.66);
  --shadow: rgba(0, 0, 0, 0.42);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family:
    "Noto Sans SC", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-width: 320px;
  min-height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

button,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.stage {
  position: relative;
  min-height: 100dvh;
  overflow-x: hidden;
  background:
    radial-gradient(ellipse at 50% 15%, rgba(180, 58, 49, 0.24), transparent 42%),
    radial-gradient(ellipse at 75% 72%, rgba(79, 214, 192, 0.08), transparent 34%),
    linear-gradient(180deg, #120608, #070304 66%);
}

#camera-video {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1);
  opacity: 0;
  filter: saturate(0.72) contrast(1.08) brightness(0.72);
  transition: opacity 0.35s ease;
}

.camera-mode #camera-video {
  opacity: 1;
}

#trail-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  transform: scaleX(-1);
  opacity: 0;
}

.camera-mode #trail-canvas {
  opacity: 1;
}

.vignette {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(ellipse at center, transparent 35%, rgba(7, 3, 4, 0.78) 100%),
    linear-gradient(to bottom, rgba(7, 3, 4, 0.76), transparent 20%, transparent 72%, rgba(7, 3, 4, 0.88));
}

.topbar {
  position: relative;
  z-index: 8;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: calc(18px + var(--safe-top)) 24px 12px;
}

.back-link {
  color: var(--gold);
  text-decoration: none;
  font-size: 13px;
  letter-spacing: 0.16em;
}

.top-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

.mode-switch {
  display: flex;
  gap: 8px;
  padding: 5px;
  border: 1px solid var(--line);
  background: rgba(7, 3, 4, 0.46);
  backdrop-filter: blur(12px);
}

.mode-button {
  min-height: 34px;
  padding: 0 14px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  letter-spacing: 0.08em;
}

.mode-button.is-active {
  color: var(--text);
  border-color: rgba(216, 184, 119, 0.52);
  background: rgba(216, 184, 119, 0.1);
}

.sound-button {
  min-height: 46px;
  padding: 0 14px;
  border: 1px solid rgba(216, 184, 119, 0.28);
  background: rgba(7, 3, 4, 0.46);
  color: var(--gold);
  letter-spacing: 0.1em;
  backdrop-filter: blur(12px);
}

.sound-button[aria-pressed="false"] {
  color: rgba(244, 237, 228, 0.46);
  border-color: rgba(244, 237, 228, 0.16);
}

.ritual-layout {
  position: relative;
  z-index: 2;
  width: min(1320px, 100%);
  min-height: calc(100dvh - 70px);
  margin: 0 auto;
  padding: 22px 24px calc(32px + var(--safe-bottom));
  display: grid;
  grid-template-columns: minmax(260px, 0.9fr) minmax(340px, 1.15fr) minmax(300px, 0.95fr);
  gap: 18px;
  align-items: stretch;
}

.copy-panel,
.ritual-stage,
.result-panel {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 28px 70px var(--shadow);
  backdrop-filter: blur(12px);
}

.copy-panel,
.result-panel {
  padding: 20px;
}

.kicker {
  margin: 0;
  color: var(--teal);
  font-size: 11px;
  letter-spacing: 0.28em;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  font-family: "Noto Serif SC", serif;
}

h1 {
  margin-top: 10px;
  font-size: clamp(44px, 6vw, 82px);
  line-height: 1.04;
  letter-spacing: 0.12em;
}

.lead {
  margin: 18px 0 0;
  color: var(--gold);
  line-height: 1.85;
  font-size: 15px;
}

.question-box {
  display: grid;
  gap: 8px;
  margin-top: 22px;
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0.08em;
}

.question-box textarea {
  width: 100%;
  resize: vertical;
  min-height: 88px;
  border: 1px solid rgba(216, 184, 119, 0.22);
  border-radius: 6px;
  background: rgba(5, 3, 4, 0.46);
  color: var(--text);
  padding: 12px;
  outline: none;
  line-height: 1.65;
}

.question-box textarea:focus {
  border-color: rgba(216, 184, 119, 0.72);
  box-shadow: 0 0 0 3px rgba(216, 184, 119, 0.1);
}

.guide-card {
  margin-top: 16px;
  display: grid;
  gap: 8px;
  padding-top: 16px;
  border-top: 1px solid rgba(216, 184, 119, 0.18);
}

.guide-card strong {
  color: var(--text);
  font-family: "Noto Serif SC", serif;
  font-size: 17px;
  letter-spacing: 0.12em;
}

.guide-card span {
  color: var(--muted);
  line-height: 1.7;
  font-size: 13px;
}

.ritual-stage {
  position: relative;
  min-height: 560px;
  display: grid;
  place-items: center;
  overflow: hidden;
  background:
    radial-gradient(ellipse at 50% 55%, rgba(216, 184, 119, 0.14), transparent 52%),
    rgba(7, 3, 4, 0.42);
}

.camera-hint {
  position: absolute;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 14px;
  border: 1px solid rgba(216, 184, 119, 0.26);
  border-radius: 999px;
  background: rgba(7, 3, 4, 0.58);
  color: var(--gold);
  font-size: 12px;
  letter-spacing: 0.08em;
  opacity: 0;
  transition: opacity 0.25s ease;
}

.camera-mode .camera-hint {
  opacity: 1;
}

.tube-wrap {
  position: relative;
  width: min(70vw, 330px);
  height: 430px;
  display: grid;
  place-items: end center;
  outline: none;
  touch-action: none;
  transform: translateY(0) rotate(0deg);
  transition: transform 0.12s ease;
}

.qian-tube-asset {
  position: absolute;
  left: 50%;
  bottom: 0;
  width: min(100%, 330px);
  height: auto;
  pointer-events: none;
  user-select: none;
  filter:
    drop-shadow(0 24px 38px rgba(0, 0, 0, 0.42))
    drop-shadow(0 0 24px rgba(216, 184, 119, 0.1));
  transform: translateX(-50%);
}

.tube-wrap.is-shaking {
  animation: tube-shake 0.26s linear infinite;
}

@keyframes tube-shake {
  0%, 100% { transform: translateY(0) rotate(0deg); }
  25% { transform: translateY(-10px) rotate(-2deg); }
  50% { transform: translateY(7px) rotate(2deg); }
  75% { transform: translateY(-6px) rotate(-1deg); }
}

.stick-cluster {
  position: absolute;
  left: 50%;
  bottom: 142px;
  width: 210px;
  height: 220px;
  transform: translateX(-50%);
}

.stick-cluster span {
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 13px;
  height: 205px;
  border-radius: 8px 8px 2px 2px;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.18), transparent 22%),
    linear-gradient(180deg, #e6c98a, #9f6f36);
  box-shadow: 0 12px 20px rgba(0, 0, 0, 0.28);
  transform-origin: bottom center;
}

.stick-cluster span:nth-child(1) { transform: translateX(-50%) rotate(-18deg); }
.stick-cluster span:nth-child(2) { transform: translateX(-50%) rotate(-11deg); height: 218px; }
.stick-cluster span:nth-child(3) { transform: translateX(-50%) rotate(-4deg); height: 230px; }
.stick-cluster span:nth-child(4) { transform: translateX(-50%) rotate(5deg); height: 224px; }
.stick-cluster span:nth-child(5) { transform: translateX(-50%) rotate(13deg); height: 214px; }
.stick-cluster span:nth-child(6) { transform: translateX(-50%) rotate(20deg); height: 202px; }

.qian-tube {
  position: relative;
  width: 220px;
  height: 250px;
  z-index: 2;
}

.tube-rim {
  position: absolute;
  top: 0;
  left: 50%;
  width: 222px;
  height: 44px;
  border: 2px solid rgba(216, 184, 119, 0.72);
  border-radius: 50%;
  background:
    radial-gradient(ellipse at center, rgba(5,3,4,0.78) 0 44%, transparent 45%),
    linear-gradient(180deg, #6c251f, #2a0f0f);
  transform: translateX(-50%);
  z-index: 4;
}

.tube-body {
  position: absolute;
  left: 16px;
  right: 16px;
  top: 18px;
  bottom: 18px;
  border: 1px solid rgba(216, 184, 119, 0.38);
  border-radius: 16px 16px 28px 28px;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.12), transparent 18%, rgba(0,0,0,0.22) 78%),
    repeating-linear-gradient(90deg, rgba(216,184,119,0.14) 0 1px, transparent 1px 22px),
    linear-gradient(180deg, #7e241e, #34100f);
  box-shadow:
    inset 0 14px 28px rgba(255,255,255,0.06),
    inset 0 -22px 42px rgba(0,0,0,0.32),
    0 24px 50px rgba(0,0,0,0.34);
}

.carving {
  position: absolute;
  left: 50%;
  top: 36%;
  transform: translate(-50%, -50%);
  font-family: "Noto Serif SC", serif;
  color: rgba(216, 184, 119, 0.82);
  font-size: 70px;
  line-height: 1;
}

.carving.small {
  top: 64%;
  font-size: 24px;
  letter-spacing: 0.28em;
}

.tube-base {
  position: absolute;
  left: 50%;
  bottom: 0;
  width: 170px;
  height: 34px;
  border-radius: 50%;
  background: linear-gradient(180deg, #8f3127, #2c0d0d);
  border: 1px solid rgba(216, 184, 119, 0.32);
  transform: translateX(-50%);
  z-index: 3;
}

.tube-shadow {
  position: absolute;
  left: 50%;
  bottom: -6px;
  width: 230px;
  height: 30px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.42);
  transform: translateX(-50%);
}

.fortune-stick {
  position: absolute;
  left: 50%;
  bottom: 250px;
  z-index: 6;
  width: 34px;
  height: 236px;
  border-radius: 12px 12px 3px 3px;
  display: grid;
  place-items: center;
  padding: 12px 0;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.2), transparent 26%),
    linear-gradient(180deg, #f2d79b, #a87436);
  color: #38150d;
  box-shadow: 0 20px 34px rgba(0, 0, 0, 0.38);
  opacity: 0;
  transform: translate(-50%, 130px) rotate(0deg);
  transition: opacity 0.2s ease, transform 0.7s cubic-bezier(.22,1,.36,1);
}

.fortune-stick span {
  writing-mode: vertical-rl;
  font-family: "Noto Serif SC", serif;
  font-size: 13px;
  letter-spacing: 0.1em;
}

.stage.has-result .fortune-stick {
  opacity: 1;
  transform: translate(-50%, -118px) rotate(-8deg);
}

.progress-ring {
  position: absolute;
  right: 22px;
  bottom: 22px;
  width: 92px;
  height: 92px;
  display: grid;
  place-items: center;
}

.progress-ring svg {
  position: absolute;
  inset: 0;
  transform: rotate(-90deg);
}

.progress-ring circle {
  fill: none;
  stroke-width: 7;
}

.progress-ring .track {
  stroke: rgba(216, 184, 119, 0.16);
}

.progress-ring .fill {
  stroke: var(--teal);
  stroke-linecap: round;
  stroke-dasharray: 327;
  stroke-dashoffset: 327;
  transition: stroke-dashoffset 0.12s ease;
}

#progress-label {
  font-family: "Noto Serif SC", serif;
  color: var(--gold);
  font-size: 22px;
}

.action-row {
  position: absolute;
  left: 22px;
  bottom: 24px;
  display: flex;
  gap: 10px;
}

.primary,
.secondary {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 18px;
  border-radius: 4px;
  text-decoration: none;
  letter-spacing: 0.1em;
  font-size: 14px;
}

.primary {
  border: 1px solid var(--gold);
  background: rgba(216, 184, 119, 0.14);
  color: var(--text);
}

.secondary {
  border: 1px solid rgba(216, 184, 119, 0.28);
  background: rgba(5, 3, 4, 0.24);
  color: var(--muted);
}

.primary:disabled,
.secondary:disabled {
  opacity: 0.42;
  cursor: default;
}

.result-panel {
  align-self: stretch;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.result-head {
  display: grid;
  gap: 8px;
  padding-bottom: 16px;
  border-bottom: 1px solid rgba(216, 184, 119, 0.18);
}

.qian-number {
  margin: 0;
  color: var(--teal);
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 11px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
}

.result-head h2 {
  font-size: clamp(28px, 4vw, 42px);
  line-height: 1.16;
  letter-spacing: 0.08em;
}

.qian-level {
  width: fit-content;
  padding: 5px 10px;
  border: 1px solid rgba(216, 184, 119, 0.42);
  color: var(--gold);
  font-size: 12px;
  letter-spacing: 0.12em;
}

.qian-poem {
  margin: 20px 0 0;
  color: var(--text);
  font-family: "Noto Serif SC", serif;
  font-size: 18px;
  line-height: 2;
  white-space: pre-line;
}

.ai-text {
  min-height: 7.5em;
  margin: 18px 0 0;
  color: var(--muted);
  line-height: 1.9;
  font-size: 14px;
}

.ai-text.is-loading {
  color: rgba(216, 184, 119, 0.68);
}

.result-actions {
  margin-top: 22px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

@media (max-width: 1080px) {
  .ritual-layout {
    grid-template-columns: minmax(280px, 0.85fr) minmax(360px, 1.15fr);
  }

  .result-panel {
    grid-column: 1 / -1;
  }
}

@media (max-width: 760px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
    padding-inline: 16px;
  }

  .top-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .mode-switch {
    width: 100%;
  }

  .mode-button {
    flex: 1;
  }

  .ritual-layout {
    grid-template-columns: 1fr;
    padding: 16px 16px calc(24px + var(--safe-bottom));
  }

  .ritual-stage {
    min-height: 510px;
  }

  .copy-panel,
  .result-panel {
    padding: 18px;
  }

  .lead {
    font-size: 14px;
  }

  .action-row {
    left: 50%;
    transform: translateX(-50%);
    width: calc(100% - 36px);
  }

  .action-row button {
    flex: 1;
  }

  .progress-ring {
    right: 16px;
    width: 76px;
    height: 76px;
  }
}

@media (max-width: 430px) {
  .ritual-stage {
    min-height: 470px;
  }

  .tube-wrap {
    width: 280px;
    height: 390px;
  }

  .stage.has-result .fortune-stick {
    transform: translate(-50%, -96px) rotate(-8deg);
  }
}
