:root {
  color-scheme: dark;
  --bg: #15120d;
  --bg-soft: #211b13;
  --panel: rgba(250, 239, 213, 0.08);
  --panel-strong: rgba(250, 239, 213, 0.13);
  --paper: #f4ead4;
  --paper-muted: #cdbf9e;
  --ink: #17130d;
  --line: rgba(231, 209, 156, 0.22);
  --gold: #d1aa55;
  --gold-strong: #f1cf75;
  --jade: #7bb59b;
  --jade-deep: #1f5f50;
  --error: #e58e72;
  --shadow: 0 24px 70px rgba(0, 0, 0, 0.38);
  --serif: "Noto Serif TC", "Cormorant Garamond", serif;
  --sans: "Inter", "Noto Serif TC", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(circle at 12% 12%, rgba(123, 181, 155, 0.22), transparent 26rem),
    radial-gradient(circle at 92% 10%, rgba(209, 170, 85, 0.18), transparent 28rem),
    linear-gradient(135deg, #100d09 0%, #1d1711 48%, #251d12 100%);
  color: var(--paper);
  font-family: var(--sans);
  overflow-x: hidden;
}

button,
textarea,
select {
  font: inherit;
}

button,
a,
select {
  -webkit-tap-highlight-color: transparent;
}

a {
  color: inherit;
  text-decoration: none;
}

.cosmos {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    radial-gradient(circle, rgba(244, 234, 212, 0.7) 0 1px, transparent 1.5px),
    linear-gradient(110deg, transparent 0 46%, rgba(209, 170, 85, 0.08) 47%, transparent 54%);
  background-size: 120px 120px, 100% 100%;
  mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.8), transparent 82%);
  opacity: 0.28;
}

.site-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: grid;
  grid-template-columns: minmax(220px, 1fr) auto auto;
  align-items: center;
  gap: 24px;
  padding: 18px clamp(18px, 4vw, 56px);
  border-bottom: 1px solid var(--line);
  background: rgba(17, 13, 8, 0.82);
  backdrop-filter: blur(18px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border: 1px solid rgba(209, 170, 85, 0.6);
  border-radius: 50%;
  background:
    radial-gradient(circle at 36% 28%, #f3d77c, #a77428 58%, #654417 100%);
  color: #17110a;
  font-family: var(--serif);
  font-weight: 800;
  box-shadow: inset 0 0 0 5px rgba(35, 22, 8, 0.28);
}

.brand strong {
  display: block;
  font-family: var(--serif);
  font-size: 18px;
  letter-spacing: 0;
}

.brand small,
.system-label {
  display: block;
  color: var(--gold);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.nav,
.language,
.ai-links,
.controls,
.history-actions,
.method-list {
  display: flex;
  align-items: center;
}

.nav {
  gap: 4px;
}

.nav a,
.language {
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--paper-muted);
  font-size: 14px;
  font-weight: 600;
}

.nav a {
  padding: 10px 12px;
}

.nav a:hover,
.nav a:focus-visible {
  border-color: var(--line);
  color: var(--paper);
  outline: none;
}

.language {
  gap: 8px;
  padding: 10px 12px;
  background: rgba(244, 234, 212, 0.06);
}

main {
  position: relative;
  z-index: 1;
  display: grid;
  gap: 34px;
  width: 100%;
  max-width: 1440px;
  margin: 0 auto;
  padding: 42px clamp(18px, 4vw, 56px) 72px;
}

.oracle-shell {
  display: grid;
  grid-template-columns: minmax(340px, 1.02fr) minmax(320px, 0.86fr) minmax(270px, 0.58fr);
  gap: 18px;
  align-items: stretch;
}

.intro-panel,
.hexagram-panel,
.reading-panel,
.prompt-section,
.history-section,
.info-grid {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(145deg, rgba(250, 239, 213, 0.12), rgba(250, 239, 213, 0.045)),
    rgba(18, 14, 9, 0.7);
  box-shadow: var(--shadow);
}

.intro-panel {
  padding: clamp(22px, 3vw, 38px);
}

h1,
h2 {
  margin: 0;
  color: var(--paper);
  font-family: var(--serif);
  letter-spacing: 0;
}

h1 {
  max-width: 680px;
  margin-top: 12px;
  font-size: clamp(40px, 4.7vw, 66px);
  line-height: 1.02;
  overflow-wrap: anywhere;
}

h1 span {
  display: block;
}

h2 {
  font-size: clamp(28px, 3vw, 44px);
  line-height: 1.08;
}

.lead,
.info-grid p,
.history-actions p,
#castStatus {
  color: var(--paper-muted);
  line-height: 1.75;
  overflow-wrap: anywhere;
}

.lead {
  max-width: 690px;
  margin: 18px 0 24px;
  font-size: 16px;
  word-break: break-all;
}

.field-label {
  display: block;
  margin: 18px 0 9px;
  color: var(--gold-strong);
  font-size: 13px;
  font-weight: 700;
}

textarea,
select {
  width: 100%;
  border: 1px solid rgba(231, 209, 156, 0.28);
  border-radius: 8px;
  background: rgba(10, 8, 5, 0.54);
  color: var(--paper);
  outline: none;
}

textarea {
  min-height: 104px;
  resize: vertical;
  padding: 16px;
  line-height: 1.6;
}

textarea:focus,
select:focus {
  border-color: var(--gold);
  box-shadow: 0 0 0 4px rgba(209, 170, 85, 0.13);
}

textarea::placeholder {
  color: rgba(205, 191, 158, 0.58);
}

.input-footer {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  color: rgba(205, 191, 158, 0.75);
  font-size: 12px;
}

.text-button {
  border: 0;
  background: transparent;
  color: var(--jade);
  cursor: pointer;
  font-weight: 700;
}

.coin-stage {
  margin: 22px 0;
  padding: 16px;
  border: 1px solid rgba(123, 181, 155, 0.22);
  border-radius: 8px;
  background: rgba(19, 50, 43, 0.22);
}

.coin-tray {
  display: flex;
  gap: 12px;
  align-items: center;
  min-height: 76px;
}

.coin {
  display: grid;
  width: 64px;
  height: 64px;
  place-items: center;
  border: 1px solid rgba(248, 215, 127, 0.72);
  border-radius: 50%;
  background:
    radial-gradient(circle at 34% 28%, #ffe8a0, #c39138 54%, #694515 100%);
  color: #1c1308;
  cursor: default;
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 800;
  box-shadow:
    inset 0 0 0 8px rgba(53, 32, 8, 0.22),
    0 12px 26px rgba(0, 0, 0, 0.28);
  transform-style: preserve-3d;
}

.coin[data-face="背"] {
  background:
    radial-gradient(circle at 34% 28%, #c6f2d4, #679977 52%, #244e3e 100%);
  color: #071711;
}

.coin.is-tossing {
  animation: toss 0.62s ease-in-out;
}

@keyframes toss {
  0% {
    transform: translateY(0) rotateY(0) rotateX(0);
  }
  45% {
    transform: translateY(-20px) rotateY(440deg) rotateX(180deg);
  }
  100% {
    transform: translateY(0) rotateY(720deg) rotateX(360deg);
  }
}

.controls {
  flex-wrap: wrap;
  gap: 10px;
}

.gesture-panel {
  display: grid;
  gap: 14px;
  scroll-margin-top: 112px;
  margin-top: 18px;
  padding: 16px;
  border: 1px solid rgba(123, 181, 155, 0.26);
  border-radius: 8px;
  background: linear-gradient(145deg, rgba(12, 39, 34, 0.52), rgba(10, 8, 5, 0.28));
}

.gesture-copy strong {
  display: block;
  margin-top: 6px;
  color: var(--paper);
  font-family: var(--serif);
  font-size: 22px;
  line-height: 1.18;
}

.gesture-copy p,
.gesture-status {
  margin: 8px 0 0;
  color: var(--paper-muted);
  font-size: 13px;
  line-height: 1.65;
}

.camera-frame {
  position: relative;
  display: grid;
  min-height: 180px;
  overflow: hidden;
  place-items: center;
  border: 1px solid rgba(231, 209, 156, 0.18);
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(244, 234, 212, 0.05), transparent),
    rgba(5, 8, 6, 0.74);
}

.camera-frame video,
.camera-frame canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scaleX(-1);
}

.camera-frame:not(.is-active) video,
.camera-frame:not(.is-active) canvas {
  display: none;
}

.camera-frame span {
  position: relative;
  z-index: 2;
  padding: 9px 12px;
  border: 1px solid rgba(231, 209, 156, 0.2);
  border-radius: 999px;
  background: rgba(17, 13, 8, 0.72);
  color: var(--paper-muted);
  font-size: 12px;
  font-weight: 800;
}

.camera-frame.is-active span {
  position: absolute;
  right: 10px;
  bottom: 10px;
  color: #d1f1e3;
}

.camera-frame.is-triggered {
  border-color: rgba(241, 207, 117, 0.72);
  box-shadow: 0 0 0 4px rgba(209, 170, 85, 0.12);
}

.camera-frame.is-audio-hot span {
  border-color: rgba(241, 207, 117, 0.54);
  color: var(--gold-strong);
}

.gesture-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.gesture-diagnostics {
  display: grid;
  gap: 10px;
  padding-top: 2px;
}

.signal-row {
  display: grid;
  grid-template-columns: 86px minmax(90px, 1fr) 88px;
  align-items: center;
  gap: 10px;
  color: var(--paper-muted);
  font-size: 12px;
  font-weight: 800;
}

.signal-track {
  height: 9px;
  overflow: hidden;
  border: 1px solid rgba(231, 209, 156, 0.16);
  border-radius: 999px;
  background: rgba(7, 6, 4, 0.44);
}

.signal-track i {
  display: block;
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--jade), var(--gold-strong));
  transition: width 0.08s linear;
}

.signal-row strong {
  color: var(--paper);
  font-size: 11px;
  text-align: right;
}

.primary-button,
.ghost-button,
.primary-link {
  min-height: 46px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 800;
}

.primary-button,
.primary-link {
  border: 1px solid rgba(247, 219, 140, 0.78);
  background: linear-gradient(135deg, #f2d57f, #b87a28);
  color: #191107;
  box-shadow: 0 14px 34px rgba(169, 109, 34, 0.26);
}

.primary-button,
.ghost-button {
  padding: 0 18px;
}

.primary-button:disabled,
.ghost-button:disabled {
  cursor: not-allowed;
  filter: grayscale(0.7);
  opacity: 0.45;
}

.ghost-button {
  border: 1px solid rgba(231, 209, 156, 0.28);
  background: rgba(244, 234, 212, 0.06);
  color: var(--paper);
}

.ghost-button:hover:not(:disabled),
.primary-button:hover:not(:disabled),
.primary-link:hover {
  transform: translateY(-1px);
}

.hexagram-panel,
.reading-panel {
  padding: 22px;
}

.panel-heading {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  color: var(--paper-muted);
  font-weight: 700;
}

.panel-heading strong {
  color: var(--gold-strong);
}

.line-stack {
  display: grid;
  gap: 14px;
  min-height: 438px;
  padding: 12px 4px;
}

.line-row {
  display: grid;
  grid-template-columns: 74px 1fr 74px;
  align-items: center;
  gap: 12px;
  min-height: 58px;
}

.line-row small,
.line-row .line-type {
  color: rgba(205, 191, 158, 0.72);
  font-size: 12px;
  font-weight: 700;
}

.line-row .line-type {
  text-align: right;
}

.yao {
  position: relative;
  height: 18px;
}

.yao::before,
.yao::after {
  position: absolute;
  top: 0;
  height: 18px;
  border-radius: 2px;
  content: "";
}

.yao.empty::before {
  right: 0;
  left: 0;
  border: 1px dashed rgba(205, 191, 158, 0.24);
  background: transparent;
}

.yao.yang::before {
  right: 0;
  left: 0;
  background: linear-gradient(90deg, #efe0ac, #bd8f3e);
}

.yao.yin::before,
.yao.yin::after {
  width: 43%;
  background: linear-gradient(90deg, #efe0ac, #bd8f3e);
}

.yao.yin::before {
  left: 0;
}

.yao.yin::after {
  right: 0;
}

.yao.moving {
  filter: drop-shadow(0 0 12px rgba(123, 181, 155, 0.44));
}

.yao.moving span {
  position: absolute;
  top: 50%;
  left: 50%;
  display: grid;
  width: 22px;
  height: 22px;
  place-items: center;
  border-radius: 50%;
  background: var(--jade);
  color: #081711;
  font-size: 12px;
  font-weight: 900;
  transform: translate(-50%, -50%);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  color: var(--paper-muted);
  font-size: 12px;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.legend i {
  display: inline-block;
  width: 24px;
  height: 8px;
  border-radius: 2px;
  background: var(--gold);
}

.legend-yin {
  background: linear-gradient(90deg, var(--gold) 0 42%, transparent 42% 58%, var(--gold) 58% 100%) !important;
}

.legend-moving {
  background: var(--jade) !important;
}

.reading-panel {
  display: grid;
  align-content: start;
  gap: 12px;
}

.result-card {
  padding: 16px;
  border: 1px solid rgba(231, 209, 156, 0.2);
  border-radius: 8px;
  background: rgba(7, 6, 4, 0.3);
}

.result-card span,
.result-card small {
  display: block;
  color: var(--paper-muted);
}

.result-card span {
  font-size: 12px;
  font-weight: 800;
}

.result-card strong {
  display: block;
  margin: 8px 0 6px;
  color: var(--paper);
  font-family: var(--serif);
  font-size: 24px;
  line-height: 1.15;
}

.result-card small {
  font-size: 12px;
  line-height: 1.45;
}

select {
  height: 44px;
  padding: 0 12px;
}

.ai-links {
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 4px;
}

.ai-links a {
  padding: 9px 10px;
  border: 1px solid rgba(123, 181, 155, 0.34);
  border-radius: 8px;
  color: #bde8d5;
  font-size: 13px;
  font-weight: 800;
}

.prompt-section,
.history-section,
.info-grid {
  padding: clamp(22px, 3vw, 34px);
}

.section-heading {
  margin-bottom: 18px;
}

.section-heading .system-label {
  margin-bottom: 8px;
}

#promptOutput {
  min-height: 330px;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 13px;
  white-space: pre-wrap;
}

.history-actions {
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.history-actions p {
  margin: 0;
}

.history-list {
  display: grid;
  gap: 10px;
}

.history-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 14px;
  align-items: center;
  padding: 14px;
  border: 1px solid rgba(231, 209, 156, 0.16);
  border-radius: 8px;
  background: rgba(244, 234, 212, 0.045);
}

.history-item strong {
  display: block;
  margin-bottom: 4px;
  font-family: var(--serif);
  font-size: 18px;
}

.history-item span {
  display: block;
  color: var(--paper-muted);
  font-size: 13px;
  line-height: 1.45;
}

.history-item button {
  width: 40px;
  height: 40px;
  border: 1px solid rgba(231, 209, 156, 0.18);
  border-radius: 8px;
  background: rgba(244, 234, 212, 0.06);
  color: var(--paper);
  cursor: pointer;
  font-weight: 800;
}

.history-empty {
  padding: 24px;
  border: 1px dashed rgba(231, 209, 156, 0.24);
  border-radius: 8px;
  color: var(--paper-muted);
  text-align: center;
}

.info-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 24px;
  align-items: center;
}

.info-grid p {
  max-width: 760px;
  margin: 14px 0 0;
}

.primary-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 18px;
  white-space: nowrap;
}

.method-list {
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.method-list span {
  padding: 10px 12px;
  border: 1px solid rgba(123, 181, 155, 0.32);
  border-radius: 8px;
  color: #cbeedd;
  font-size: 13px;
  font-weight: 800;
}

.toast {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 40;
  max-width: min(380px, calc(100vw - 48px));
  padding: 13px 16px;
  border: 1px solid rgba(231, 209, 156, 0.24);
  border-radius: 8px;
  background: rgba(17, 13, 8, 0.92);
  color: var(--paper);
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(14px);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 1180px) {
  .oracle-shell {
    grid-template-columns: minmax(0, 1fr) minmax(280px, 0.72fr);
  }

  .reading-panel {
    grid-column: 1 / -1;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .reading-panel .field-label,
  .reading-panel select,
  .ai-links {
    grid-column: 1 / -1;
  }
}

@media (max-width: 820px) {
  .site-header {
    grid-template-columns: 1fr auto;
    gap: 12px;
  }

  .nav {
    grid-column: 1 / -1;
    order: 3;
    overflow-x: auto;
  }

  .oracle-shell {
    grid-template-columns: 1fr;
  }

  .reading-panel {
    grid-template-columns: 1fr;
  }

  .line-stack {
    min-height: 392px;
  }

  .line-row {
    grid-template-columns: 58px 1fr 62px;
    gap: 8px;
  }

  .info-grid,
  .history-item {
    grid-template-columns: 1fr;
  }

  .method-list {
    justify-content: flex-start;
  }
}

@media (max-width: 560px) {
  main {
    padding: 24px 14px 56px;
  }

  .site-header {
    padding: 14px;
  }

  .brand small,
  .language {
    display: none;
  }

  h1 {
    font-size: 36px;
    line-height: 1.08;
  }

  .intro-panel,
  .hexagram-panel,
  .reading-panel,
  .prompt-section,
  .history-section,
  .info-grid {
    padding: 18px;
  }

  .coin-tray {
    display: grid;
    grid-template-columns: repeat(3, 58px);
    justify-content: start;
    gap: 20px;
  }

  .coin {
    width: 58px;
    height: 58px;
  }

  .controls {
    display: grid;
    grid-template-columns: 1fr;
  }

  .gesture-controls {
    display: grid;
    grid-template-columns: 1fr;
  }

  .signal-row {
    grid-template-columns: 1fr;
    gap: 7px;
  }

  .signal-row strong {
    text-align: left;
  }

  .primary-button,
  .ghost-button {
    width: 100%;
  }

  .camera-frame {
    min-height: 170px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
