* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --bg: #1a1208;
  --card: #2c1810;
  --accent: #c4956a;
  --gold: #d4a853;
  --text: #e8d5b5;
  --dim: #8a7560;
  --yang: #d4a853;
  --yin: #6b8cae;
  --change: #c05050;
  --kong: #666;
  --fushen: #7a6a5a;
  --andong: #b87333;
}

body {
  font-family: -apple-system, "PingFang SC", "Noto Serif SC", serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  padding: env(safe-area-inset-top) 16px env(safe-area-inset-bottom);
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* ==================== View Container ==================== */
.view {
  width: 100%;
  max-width: 840px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.view.hidden { display: none; }

/* ==================== Headings ==================== */
h1 {
  font-size: 28px;
  font-weight: 400;
  letter-spacing: 6px;
  color: var(--gold);
  margin: 24px 0 4px;
}

.subtitle {
  font-size: 13px;
  color: var(--dim);
  margin-bottom: 18px;
  letter-spacing: 2px;
}

/* ==================== Question Input ==================== */
.question-input {
  width: 100%;
  padding: 10px 14px;
  background: var(--card);
  border: 1px solid rgba(196,149,106,0.3);
  border-radius: 6px;
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  margin-bottom: 16px;
  outline: none;
  -webkit-appearance: none;
}
.question-input::placeholder { color: var(--dim); }
.question-input:focus { border-color: var(--gold); }

.question-display {
  width: 100%;
  text-align: center;
  font-size: 14px;
  color: var(--accent);
  margin-bottom: 6px;
  display: none;
}
.question-display.show { display: block; }

/* ==================== Mode Tabs ==================== */
.mode-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 18px;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid rgba(196,149,106,0.3);
}
.mode-tab {
  padding: 8px 26px;
  background: var(--card);
  color: var(--dim);
  border: none;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: all 0.2s;
}
.mode-tab.active {
  background: var(--accent);
  color: var(--bg);
}
.mode-panel { display: flex; flex-direction: column; align-items: center; width: 100%; }
.mode-panel.hidden { display: none; }

/* ==================== Shake Button ==================== */
.shake-btn {
  width: 110px;
  height: 110px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  background: radial-gradient(circle at 40% 35%, #3d2518, #1a1208);
  color: var(--gold);
  font-size: 24px;
  letter-spacing: 4px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  margin-bottom: 14px;
  transition: all 0.2s;
}
.shake-btn:active {
  transform: scale(0.95);
  border-color: var(--gold);
  box-shadow: 0 0 20px rgba(212,168,83,0.3);
}
.shake-btn.shaking { animation: shake 0.4s ease-in-out; }
@keyframes shake {
  0%,100% { transform: rotate(0); }
  25% { transform: rotate(-8deg) scale(0.97); }
  75% { transform: rotate(8deg) scale(0.97); }
}

/* ==================== Coins ==================== */
.coins-row {
  display: flex;
  gap: 10px;
  margin-bottom: 14px;
  height: 40px;
  align-items: center;
}
.coin {
  width: 34px; height: 34px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: bold;
  border: 2px solid var(--accent);
  opacity: 0; transition: all 0.2s;
}
.coin.heads { background: var(--gold); color: var(--bg); opacity: 1; }
.coin.tails { background: var(--card); color: var(--dim); opacity: 1; }

/* ==================== Manual Input ==================== */
.manual-grid {
  width: 100%;
  max-width: 420px;
  margin-bottom: 14px;
}
.manual-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.manual-label {
  width: 40px;
  font-size: 13px;
  color: var(--dim);
  text-align: right;
  flex-shrink: 0;
}
.manual-btns {
  display: flex;
  gap: 6px;
  flex: 1;
}
.manual-btn {
  flex: 1;
  padding: 8px 4px;
  background: var(--card);
  border: 1px solid rgba(196,149,106,0.2);
  border-radius: 4px;
  color: var(--dim);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: all 0.15s;
}
.manual-btn.selected {
  border-color: var(--gold);
  color: var(--gold);
  background: rgba(212,168,83,0.15);
}
.manual-btn.sel-change {
  border-color: var(--change);
  color: var(--change);
  background: rgba(192,80,80,0.15);
}
.manual-submit {
  width: 100%;
  max-width: 420px;
  padding: 12px;
  background: var(--accent);
  border: none;
  border-radius: 6px;
  color: var(--bg);
  font-size: 16px;
  font-family: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  margin-bottom: 14px;
}
.manual-submit:active { opacity: 0.8; }
.manual-submit:disabled { opacity: 0.4; cursor: not-allowed; }

/* ==================== Result Header ==================== */
.result-header {
  width: 100%;
  display: flex;
  align-items: center;
  padding: 14px 0 10px;
}
.new-gua-btn {
  padding: 8px 20px;
  background: var(--card);
  border: 1px solid rgba(196,149,106,0.3);
  border-radius: 6px;
  color: var(--accent);
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: all 0.15s;
}
.new-gua-btn:active { opacity: 0.7; }

/* ==================== 干支 Info Bar ==================== */
.gz-bar {
  width: 100%;
  padding: 10px 14px;
  background: var(--card);
  border-radius: 6px;
  margin-bottom: 10px;
  display: none;
  font-size: 14px;
  line-height: 1.8;
}
.gz-bar.show { display: block; }
.gz-row {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
}
.gz-item { color: var(--text); }
.gz-label { color: var(--dim); }
.gz-value { color: var(--gold); }
.kong-wang { color: var(--change); }

/* ==================== Gua Header ==================== */
.gua-header {
  width: 100%;
  display: none;
  align-items: baseline;
  justify-content: center;
  gap: 10px;
  margin: 8px 0 10px;
  flex-wrap: wrap;
}
.gua-header.show { display: flex; }
.gua-name { font-size: 18px; color: var(--gold); letter-spacing: 1px; }
.gua-arrow { font-size: 14px; color: var(--dim); display: none; }
.gua-arrow.show { display: inline; }

/* ==================== 排盘 Table ==================== */
.paipan {
  width: 100%;
  display: none;
  margin-bottom: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.paipan.show { display: block; }

.paipan-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.paipan-table th {
  padding: 6px 6px;
  color: var(--dim);
  font-weight: normal;
  font-size: 13px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  text-align: center;
}

.paipan-table td {
  padding: 8px 6px;
  text-align: center;
  border-bottom: 1px solid rgba(255,255,255,0.04);
  white-space: nowrap;
}

.paipan-table tr:last-child td { border-bottom: none; }

/* Yao line rendering */
.yao-line {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 12px;
  width: 48px;
}
.yao-line .bar {
  height: 6px;
  border-radius: 1px;
}
.yao-line.yang .bar { width: 100%; background: var(--yang); }
.yao-line.yin .bar { width: 40%; background: var(--yin); }
.yao-line.old-yang .bar { width: 100%; background: var(--change); }
.yao-line.old-yin .bar { width: 40%; background: var(--change); }

/* Cell colors */
.c-gold { color: var(--gold); }
.c-dim { color: var(--dim); }
.c-text { color: var(--text); }
.c-change { color: var(--change); }
.c-kong { color: var(--kong); }
.c-andong { color: var(--andong); }
.c-fushen { color: var(--fushen); font-size: 13px; }
.c-shi { color: var(--gold); font-weight: bold; }

.tag {
  display: inline-block;
  font-size: 10px;
  padding: 1px 4px;
  border-radius: 2px;
  margin-left: 2px;
  vertical-align: middle;
}
.tag-kong { background: rgba(102,102,102,0.3); color: var(--kong); }
.tag-andong { background: rgba(184,115,51,0.2); color: var(--andong); }
.tag-chong { background: rgba(192,80,80,0.2); color: var(--change); }
.tag-he { background: rgba(100,180,100,0.2); color: #7cb87c; }
.tag-cs { background: rgba(100,100,180,0.2); color: #8a8ac0; }

/* ==================== Analysis Section ==================== */
.analysis {
  width: 100%;
  padding: 12px 14px;
  background: var(--card);
  border-radius: 6px;
  margin-bottom: 10px;
  display: none;
  font-size: 14px;
  line-height: 1.8;
}
.analysis.show { display: block; }
.analysis-title {
  color: var(--gold);
  font-size: 15px;
  margin-bottom: 4px;
  letter-spacing: 1px;
}
.analysis-row {
  color: var(--dim);
}
.analysis-row span { color: var(--text); }

/* ==================== History ==================== */
.history-toggle {
  margin-top: 18px;
  font-size: 13px;
  color: var(--dim);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  letter-spacing: 1px;
}
.history-list {
  width: 100%;
  margin-top: 8px;
  display: none;
}
.history-list.show { display: block; }
.history-item {
  padding: 10px 14px;
  background: var(--card);
  border-radius: 6px;
  margin-bottom: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.history-item .h-name { color: var(--gold); }
.history-item .h-detail { color: var(--dim); font-size: 11px; }
.history-item .h-time { color: var(--dim); font-size: 12px; }
.history-item .h-right { text-align: right; }

.footer {
  margin-top: auto;
  padding: 18px 0;
  font-size: 11px;
  color: rgba(138,117,96,0.4);
  letter-spacing: 1px;
}

/* Animation */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
.fade-in { animation: fadeIn 0.3s ease-out forwards; }

/* ==================== Responsive: Tablet (iPad portrait, iPhone landscape) ==================== */
@media (min-width: 600px) {
  body { padding-left: 28px; padding-right: 28px; }
  h1 { font-size: 34px; margin: 32px 0 8px; }
  .subtitle { font-size: 15px; }
  .question-input { font-size: 16px; padding: 12px 16px; }
  .question-display { font-size: 16px; }
  .mode-tab { padding: 10px 34px; font-size: 16px; }
  .shake-btn { width: 130px; height: 130px; font-size: 28px; }
  .coin { width: 40px; height: 40px; font-size: 16px; }
  .manual-grid { max-width: 500px; }
  .manual-label { font-size: 15px; width: 48px; }
  .manual-btn { font-size: 14px; padding: 10px 6px; }
  .manual-submit { max-width: 500px; font-size: 17px; padding: 14px; }
  .new-gua-btn { font-size: 16px; padding: 10px 24px; }
  .gz-bar { font-size: 16px; padding: 12px 18px; }
  .gua-name { font-size: 22px; }
  .gua-arrow { font-size: 16px; }
  .paipan-table { font-size: 16px; }
  .paipan-table th { font-size: 14px; padding: 8px 8px; }
  .paipan-table td { padding: 10px 8px; }
  .yao-line { width: 56px; height: 14px; }
  .yao-line .bar { height: 7px; }
  .c-fushen { font-size: 15px; }
  .tag { font-size: 11px; padding: 2px 5px; }
  .analysis { font-size: 16px; padding: 14px 18px; }
  .analysis-title { font-size: 17px; }
  .history-toggle { font-size: 14px; }
  .history-item { font-size: 15px; padding: 12px 16px; }
  .history-item .h-detail { font-size: 12px; }
  .history-item .h-time { font-size: 13px; }
  .footer { font-size: 12px; }
}

/* ==================== Large screens (iPad landscape, desktop) ==================== */
@media (min-width: 1024px) {
  h1 { font-size: 36px; }
  .gua-name { font-size: 24px; }
  .gua-arrow { font-size: 17px; }
  .paipan-table { font-size: 17px; }
  .paipan-table th { font-size: 15px; padding: 10px 10px; }
  .paipan-table td { padding: 11px 10px; }
  .yao-line { width: 64px; height: 16px; gap: 5px; }
  .yao-line .bar { height: 8px; border-radius: 2px; }
  .c-fushen { font-size: 16px; }
  .tag { font-size: 12px; padding: 2px 6px; }
  .analysis { font-size: 17px; }
  .analysis-title { font-size: 18px; }
}
