:root {
  --bg: #16110d;
  --bg-card: #fffdf8;
  --paper: #f7f0e4;
  --ink: #2a241c;
  --muted: #6b6256;
  --line: #d9cfc0;
  --accent: #8a3226;
  --accent-soft: #f0e2d6;
  --gold: #b8965a;
  --gold-dim: #8a7349;
  --ok: #2f5d3a;
  --warn: #8a5a12;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.38);
  --radius: 16px;
  --font: "Noto Serif CJK TC", "Noto Serif CJK HK", "Noto Serif TC",
    "Source Han Serif TC", "Songti TC", "PingFang HK", "PingFang TC",
    "Microsoft JhengHei", serif;
  --sans: "Noto Sans TC", "PingFang HK", "PingFang TC", "Microsoft JhengHei",
    sans-serif;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  color: var(--ink);
  background-color: var(--bg);
  background-image:
    radial-gradient(ellipse at 50% 0%, rgba(58, 44, 30, 0.55), transparent 55%),
    linear-gradient(180deg, #1c1510 0%, #16110d 45%, #0c0908 100%);
  font-family: var(--sans);
  line-height: 1.75;
  min-height: 100vh;
}

a { color: var(--accent); }

.wrap {
  width: min(720px, calc(100% - 28px));
  margin: 0 auto;
  padding: 28px 0 64px;
}

.sheet {
  position: relative;
  background: var(--paper);
  border: 1px solid rgba(184, 150, 90, 0.45);
  box-shadow: var(--shadow);
  padding: 0 0 28px;
  overflow: hidden;
}
.sheet::before,
.sheet::after { display: none; }

/* -------- Cover page (index.html) -------- */
body.is-cover {
  background: #000;
  color: #e8d5a3;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.cover {
  width: min(100vw, 100dvh);
  height: min(100vw, 100dvh);
  display: grid;
  grid-template-columns: 60% 40%;
  background: #050403;
  box-shadow: 0 0 0 1px rgba(212, 175, 80, 0.12);
}
.cover-figure {
  position: relative;
  background: #f3ead8;
  overflow: hidden;
  min-height: 0;
}
.cover-figure img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 50% 10%;
  display: block;
}
.cover-yao {
  position: absolute;
  top: max(18px, env(safe-area-inset-top));
  left: max(16px, env(safe-area-inset-left));
  z-index: 2;
  margin: 0;
  font-family: var(--font);
  color: #c9a24a;
  line-height: 0.85;
  text-shadow: 0 1px 0 rgba(255, 248, 232, 0.35);
  pointer-events: none;
  display: flex;
  align-items: baseline;
  gap: 0.06em;
}
.yao-num {
  font-size: clamp(3.4rem, 9.2vw, 5.8rem);
  font-weight: 700;
  letter-spacing: 0.01em;
}
.yao-unit {
  font-size: clamp(1.55rem, 4.2vw, 2.55rem);
  letter-spacing: 0.08em;
  font-weight: 700;
}
.cover-copy {
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: #000;
  color: #e8d5a3;
  min-height: 0;
}
.cover-light {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: left 72%;
  pointer-events: none;
  z-index: 0;
}
.cover-seal,
.cover-stage,
.cover-bottom {
  position: relative;
  z-index: 1;
}
.cover-seal {
  align-self: center;
  display: inline-block;
  border: 1px solid #c4a35a;
  color: #e8c878;
  padding: 5px 12px;
  font-size: clamp(10px, 1.15vw, 12px);
  letter-spacing: 0.42em;
  margin: 5.2% 0 0;
  font-family: var(--font);
  box-shadow: inset 0 0 0 1px rgba(196, 163, 90, 0.55);
  background: rgba(0, 0, 0, 0.25);
}
.cover-stage {
  flex: 1 1 auto;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
  gap: clamp(0.7rem, 1.6vw, 1.35rem);
  padding: 2% 7% 2% 4%;
  min-height: 0;
}
.cover-verse {
  font-family: var(--font);
  color: #ffe9b0;
  font-size: clamp(0.88rem, 1.7vw, 1.12rem);
  letter-spacing: 0.08em;
  line-height: 2.05;
  text-align: right;
  font-weight: 600;
  text-shadow:
    0 0 3px #000,
    0 1px 2px #000,
    0 0 14px rgba(0, 0, 0, 0.95),
    0 0 28px rgba(0, 0, 0, 0.8);
  max-width: 11.2em;
  align-self: flex-start;
  margin-top: 11%;
  padding: 0.55em 0.4em 0.7em 0.55em;
  background: linear-gradient(90deg, rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.42) 18%, rgba(0, 0, 0, 0.42));
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}
.cover-verse p { margin: 0 0 1.15em; }
.cover-verse p:last-child { margin-bottom: 0; }
.cover h1 {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #f3d78a;
  letter-spacing: 0;
  text-indent: 0;
  line-height: 1;
  font-size: clamp(2.2rem, 6.2vw, 3.85rem);
  font-weight: 700;
  font-family: var(--font);
}
.cover .title-text {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  font-size: 1em;
  font-weight: 700;
  letter-spacing: 0.16em;
  color: #f3d78a;
  text-shadow: 0 0 28px rgba(212, 175, 80, 0.38), 0 2px 0 rgba(0, 0, 0, 0.65);
  line-height: 1;
}
.cover .title-br {
  display: block;
  width: 1.15em;
  height: 0.48em;
  color: #f0d48a;
  margin: 0.08em 0;
}
.cover .title-br svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  filter: drop-shadow(0 0 8px rgba(212, 175, 80, 0.35));
}
.cover-bottom {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 0 6% 4.2%;
}
.cover-enter {
  display: inline-block;
  padding: 0.72em calc(1.85em - 0.16em) 0.78em 1.85em;
  border: 2px solid #f6e2a2;
  color: #1a140c;
  text-decoration: none;
  font-family: var(--font);
  font-size: clamp(1.02rem, 2vw, 1.24rem);
  font-weight: 700;
  letter-spacing: 0.16em;
  background: linear-gradient(180deg, #ffe9a8 0%, #e2b75c 55%, #c4922e 100%);
  box-shadow:
    0 0 0 1px rgba(80, 54, 12, 0.55),
    0 0 26px rgba(232, 200, 110, 0.55),
    0 6px 18px rgba(0, 0, 0, 0.45),
    inset 0 1px 0 rgba(255, 255, 255, 0.45);
}
.cover-enter:hover,
.cover-enter:focus {
  color: #100c08;
  background: linear-gradient(180deg, #fff1c0 0%, #ecc56a 55%, #d0a038 100%);
  box-shadow:
    0 0 0 1px rgba(80, 54, 12, 0.65),
    0 0 34px rgba(232, 200, 110, 0.7),
    0 6px 18px rgba(0, 0, 0, 0.45),
    inset 0 1px 0 rgba(255, 255, 255, 0.55);
  outline: none;
}
@media (min-width: 641px) {
  .cover-verse br.break-narrow { display: none; }
}
.cover-foot {
  margin: 0;
  color: #cbb679;
  font-family: var(--font);
  font-size: clamp(0.62rem, 1.15vw, 0.78rem);
  letter-spacing: 0.22em;
  text-align: center;
}

/* -------- Query page header -------- */
.query-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px 20px;
  position: relative;
  padding: 16px 44px 14px;
}
.query-top::after {
  content: "";
  position: absolute;
  left: 24px;
  right: 24px;
  bottom: 0;
  height: 1px;
  background: rgba(184, 150, 90, 0.5);
  pointer-events: none;
}
.query-top h1,
.query-top .back-link {
  font-family: var(--font);
  font-weight: 700;
  font-size: 1.05rem;
  letter-spacing: 0.16em;
  line-height: 1.2;
  margin: 0;
  text-indent: 0;
}
.query-top h1 {
  color: #1f160f;
}
.back-link {
  color: var(--accent);
  text-decoration: none;
  white-space: nowrap;
  border-bottom: 1px solid transparent;
}
.back-link:hover,
.back-link:focus { border-bottom-color: var(--accent); }

@media (max-width: 640px) {
  .query-top { padding: 12px 44px 10px; }
}

h1 {
  font-family: var(--font);
  font-weight: 700;
  font-size: clamp(2.15rem, 8vw, 3rem);
  margin: 0 0 12px;
  letter-spacing: 0.36em;
  text-indent: 0.36em;
  line-height: 1.15;
  color: #1f160f;
}
.lede {
  font-family: var(--font);
  color: var(--ink);
  margin: 0;
  font-size: 1.02rem;
  letter-spacing: 0.08em;
}
.ink-rule {
  display: block;
  width: 48px;
  height: 1px;
  margin: 18px auto;
  background: var(--gold);
  opacity: 0.7;
}
.intro {
  max-width: 28em;
  margin: 0 auto;
  color: var(--muted);
  font-size: 0.96rem;
  letter-spacing: 0.02em;
}
.essence {
  max-width: 32em;
  margin: 0 auto;
  color: var(--muted);
  font-size: 0.96rem;
  letter-spacing: 0.02em;
  text-align: left;
}
.essence p { margin: 0 0 0.75em; }
.essence p:last-child { margin-bottom: 0; }

.essence-card {
  margin: 0 24px 20px;
}
.essence-card h2 {
  text-align: center;
}
.essence-list {
  list-style: none;
  margin: 0;
  padding: 0;
  color: var(--ink);
  font-size: 0.96rem;
}
.essence-list li {
  position: relative;
  padding: 10px 0 10px 1.6em;
  border-bottom: 1px solid var(--line);
}
.essence-list li:last-child { border-bottom: 0; }
.essence-list li::before {
  content: "";
  position: absolute;
  left: 0.2em;
  top: 1.15em;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 2px rgba(138, 50, 38, 0.18);
}

.howto {
  margin: 0 0 28px;
  padding: 4px 4px 0;
}
.howto h2,
.card h2 {
  font-family: var(--font);
  font-size: 1.08rem;
  margin: 0 0 12px;
  letter-spacing: 0.14em;
  font-weight: 700;
}
.howto ol {
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--ink);
  font-size: 0.95rem;
}
.howto li {
  position: relative;
  padding: 8px 0 8px 1.7em;
  border-bottom: 1px solid var(--line);
}
.howto li:last-of-type { border-bottom: 0; }
.howto li::before {
  content: "";
  position: absolute;
  left: 0.15em;
  top: 1.05em;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.7;
}
.howto strong {
  font-family: var(--font);
  font-weight: 700;
  letter-spacing: 0.04em;
}
.howto-note {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}
.howto-note.quiet { margin-top: 8px; }

.card {
  background: var(--bg-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: none;
  padding: 22px 20px;
  margin: 0 24px 16px;
}
.cast-card {
  border-color: #d3c4b0;
}
.quiet-card {
  background: transparent;
  box-shadow: none;
  padding: 8px 4px 0;
  border: 0;
}

.rules {
  color: var(--muted);
  font-size: 0.95rem;
  margin: 0;
  padding-left: 1.1em;
}
.rules li { margin: 0.25em 0; }

label.field-label {
  display: block;
  font-size: 0.92rem;
  line-height: 1.65;
  margin-bottom: 8px;
  color: var(--muted);
}

.input-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}
#query {
  flex: 1 1 auto;
  min-width: 220px;
  width: 100%;
  height: calc(1.5em * 2 + 20px);
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 12px;
  padding: 10px 14px;
  font-size: 16px; /* avoid iOS zoom */
  line-height: 1.5;
  font-family: var(--font);
  color: var(--ink);
  outline: none;
  resize: none;
  display: block;
}
#query:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(122, 59, 46, 0.12);
}

button.primary {
  border: 0;
  background: var(--accent);
  color: #fff;
  border-radius: 12px;
  padding: 14px 22px;
  font-size: 1rem;
  letter-spacing: 0.16em;
  cursor: pointer;
  font-family: var(--sans);
}
button.primary:hover { filter: brightness(1.05); }
button.primary:disabled { opacity: 0.55; cursor: not-allowed; }

button.ghost {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  border-radius: 12px;
  padding: 10px 14px;
  cursor: pointer;
  font-family: var(--sans);
}

.hint {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.error {
  margin-top: 12px;
  padding: 12px 14px;
  border-radius: 12px;
  background: #fff1ea;
  color: #7a2e1c;
  border: 1px solid #f0c7b8;
  display: none;
}
.error.show { display: block; }

#result { display: none; }
#result.show { display: block; }

.lot-head {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  align-items: baseline;
  margin-bottom: 8px;
}
.lot-num {
  font-family: var(--font);
  font-size: 1.6rem;
  letter-spacing: 0.08em;
}
.pill {
  display: inline-block;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 0.85rem;
}
.lot-title {
  font-family: var(--font);
  font-size: 1.35rem;
  margin: 4px 0 12px;
}

.poem {
  font-family: var(--font);
  font-size: 1.15rem;
  white-space: pre-wrap;
  background: #faf6ef;
  border-left: 3px solid var(--accent);
  padding: 12px 14px;
  border-radius: 0 12px 12px 0;
  margin: 0 0 16px;
}

.section { margin: 14px 0; }
.section h3 {
  margin: 0 0 6px;
  font-size: 0.98rem;
  color: var(--accent);
  letter-spacing: 0.08em;
  font-family: var(--font);
}
.section p, .section pre {
  margin: 0;
  white-space: pre-wrap;
  color: var(--ink);
  font-family: var(--sans);
  font-size: 0.96rem;
}

.steps {
  font-size: 0.92rem;
  color: var(--muted);
  background: #faf7f2;
  border-radius: 12px;
  padding: 12px 14px;
}
.steps table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 8px;
  font-size: 0.9rem;
}
.steps th, .steps td {
  border-bottom: 1px solid var(--line);
  padding: 6px 4px;
  text-align: left;
  word-break: break-word;
}
.steps ol { margin: 8px 0 0; padding-left: 1.2em; }

.calc-process {
  margin-top: 8px;
}
.calc-process summary {
  cursor: pointer;
  color: var(--accent);
  font-family: var(--font);
  letter-spacing: 0.08em;
  list-style: none;
  font-size: 0.92rem;
}
.calc-process summary::-webkit-details-marker { display: none; }
.calc-process summary::before {
  content: "▸ ";
  font-family: var(--sans);
}
.calc-process[open] summary::before { content: "▾ "; }

details.radicals {
  margin-top: 8px;
}
details.radicals summary {
  cursor: pointer;
  color: var(--accent);
  font-family: var(--font);
  letter-spacing: 0.06em;
}
.radicals table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.86rem;
  margin-top: 10px;
}
.radicals th, .radicals td {
  border: 1px solid var(--line);
  padding: 6px 8px;
  text-align: left;
}
.radicals th { background: #f4ebe1; }

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


.lot-title:empty { display: none; }

.follow-gate {
  text-align: center;
  background: #faf6ef;
  border: 1px solid var(--line);
  padding: 18px 14px;
  border-radius: 12px;
  margin: 0 0 16px;
}
.follow-gate[hidden],
#lot-body[hidden] {
  display: none !important;
}
.follow-gate p {
  margin: 0 0 14px;
  color: var(--ink);
  font-size: 1.02rem;
  letter-spacing: 0.04em;
  line-height: 1.75;
  font-family: var(--font);
}
.follow-gate .nobr { white-space: nowrap; }
.follow-gate a {
  display: inline-block;
  border: 1px solid var(--accent);
  color: var(--accent);
  text-decoration: none;
  border-radius: 12px;
  padding: 10px 22px;
  letter-spacing: 0.16em;
  font-family: var(--sans);
}
.follow-gate a:hover,
.follow-gate a:focus {
  background: var(--accent-soft);
}

.follow-x {
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
  text-align: center;
}
.follow-x p {
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 0.92rem;
  letter-spacing: 0.08em;
  font-family: var(--font);
}
.follow-x a {
  display: inline-block;
  border: 1px solid var(--accent);
  color: var(--accent);
  text-decoration: none;
  border-radius: 12px;
  padding: 10px 22px;
  letter-spacing: 0.16em;
  font-family: var(--sans);
}
.follow-x a:hover,
.follow-x a:focus {
  background: var(--accent-soft);
}

footer {
  margin-top: 28px;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
  line-height: 1.7;
  padding: 0 24px 8px;
}

.loading {
  color: var(--muted);
  text-align: center;
  padding: 28px 24px;
}

.ai-box {
  margin-top: 1.25rem;
  padding: 1rem 1.1rem;
  border-radius: 12px;
  background: #f7f1e6;
  border: 1px solid var(--line);
}
.ai-box h3 {
  margin: 0 0 0.6rem;
  font-size: 1rem;
  letter-spacing: 0.08em;
  color: var(--accent);
  font-family: var(--font);
}
.ai-box p {
  margin: 0;
  line-height: 1.75;
  white-space: pre-wrap;
}

@media (max-width: 640px) {
  body.is-cover {
    display: block;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
  }
  .cover {
    display: grid;
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto minmax(0, 1fr) auto;
    grid-template-areas:
      "seal"
      "title"
      "verse"
      "figure"
      "bottom";
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    min-height: 0;
    overflow: hidden;
    background: #050403;
  }
  .cover-copy,
  .cover-stage {
    display: contents;
  }
  .cover-light {
    position: relative;
    inset: auto;
    grid-area: 1 / 1 / -1 / -1;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center 42%;
    z-index: 0;
    pointer-events: none;
    opacity: 0.92;
  }
  .cover-seal {
    grid-area: seal;
    z-index: 1;
    margin: max(12px, env(safe-area-inset-top)) 0 0;
    padding: 5px 12px;
    font-size: 10px;
    letter-spacing: 0.42em;
    justify-self: center;
  }
  .cover h1 {
    grid-area: title;
    z-index: 1;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: clamp(1.72rem, 8.2vw, 2.2rem);
    padding: 10px 16px 2px;
    gap: 0;
  }
  .cover .title-text {
    writing-mode: horizontal-tb;
    text-orientation: mixed;
    letter-spacing: 0.16em;
    white-space: nowrap;
    text-shadow: 0 0 22px rgba(212, 175, 80, 0.42), 0 2px 0 rgba(0, 0, 0, 0.75), 0 0 10px #000;
  }
  .cover .title-br {
    width: 2.5em;
    height: 0.44em;
    margin: 0.08em 0;
    flex: 0 0 auto;
    transform: none;
  }
  .cover-verse {
    grid-area: verse;
    z-index: 1;
    justify-self: center;
    align-self: start;
    text-align: center;
    font-size: clamp(0.82rem, 3.5vw, 0.98rem);
    letter-spacing: 0.08em;
    line-height: 2;
    max-width: 16em;
    width: auto;
    margin: 6px 24px 4px;
    padding: 0;
    background: none;
    text-shadow:
      0 0 3px #000,
      0 1px 2px #000,
      0 0 14px rgba(0, 0, 0, 0.95),
      0 0 28px rgba(0, 0, 0, 0.8);
  }
  .cover-verse p { margin-bottom: 0.8em; }
  .cover-figure {
    grid-area: figure;
    z-index: 1;
    justify-self: center;
    align-self: center;
    width: min(70%, calc(100% - 80px), calc(42dvh * 3 / 4));
    height: auto;
    max-height: min(42dvh, 100%);
    min-height: 0;
    aspect-ratio: 3 / 4;
    background: #f3ead8;
    box-shadow:
      0 0 0 1px rgba(212, 175, 80, 0.38),
      0 10px 28px rgba(0, 0, 0, 0.45);
  }
  .cover-figure img {
    object-fit: cover;
    object-position: 50% 50%;
  }
  .cover-yao { top: 8px; left: 8px; }
  .yao-num { font-size: clamp(1.7rem, 7vw, 2.3rem); }
  .yao-unit { font-size: clamp(0.9rem, 3.4vw, 1.15rem); }
  .cover-bottom {
    grid-area: bottom;
    z-index: 1;
    gap: 8px;
    padding: 10px 16px max(14px, env(safe-area-inset-bottom));
  }
  .cover-enter {
    font-size: clamp(0.92rem, 4vw, 1.05rem);
    padding: 0.58em calc(1.7em - 0.16em) 0.62em 1.7em;
    letter-spacing: 0.16em;
    min-height: 44px;
    min-width: 0;
    text-align: center;
    box-sizing: border-box;
  }
  .cover-foot {
    font-size: 0.66rem;
    letter-spacing: 0.18em;
  }
}

@media (max-width: 480px) {
  .wrap { width: min(100% - 16px, 720px); padding: 12px 0 40px; }
  .sheet { padding: 0 0 20px; }
  .query-top { padding: 12px 28px 10px; }
  .query-top::after { left: 14px; right: 14px; }
  .card, .essence-card { margin-left: 14px; margin-right: 14px; padding: 16px 14px; }
  .quiet-card { padding: 8px 0 0; }
  footer { padding: 0 16px 8px; }
  button.primary { width: 100%; }
  .follow-x a, .follow-gate a { width: 100%; box-sizing: border-box; }
  .input-row { flex-direction: column; }
  #query {
    flex: none;
    min-width: 0;
    width: 100%;
    height: calc(1.5em * 2 + 20px);
  }
  .steps table { font-size: 0.82rem; }
  .howto li, .essence-list li { padding-left: 1.5em; }
  .cover h1 { font-size: clamp(1.62rem, 8vw, 1.95rem); padding: 8px 12px 0; }
  .cover-verse {
    font-size: clamp(0.8rem, 3.6vw, 0.92rem);
    line-height: 1.95;
    max-width: 16em;
    margin: 4px 20px 2px;
  }
  .cover-figure {
    width: min(70%, calc(100% - 72px), calc(42dvh * 3 / 4));
  }
  .cover-bottom { padding: 8px 14px max(12px, env(safe-area-inset-bottom)); }
}
