/* 山醫命相卜 共用樣式 */

:root {
  --ink: #0a0a0a;
  --ink-2: #1a1a1a;
  --paper: #f6f4ee;
  --paper-2: #efece4;
  --paper-3: #e6e2d6;
  --line: #1a1a1a;
  --line-soft: rgba(10,10,10,0.13);
  --line-mid: rgba(10,10,10,0.35);
  --accent: #8b1a1a;
}

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

html, body {
  background: var(--paper);
  color: var(--ink);
  font-family: "Noto Serif TC", "Songti TC", "STSong", serif;
  font-weight: 300;
  height: 100vh;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

body {
  background-image:
    radial-gradient(circle at 20% 0%, rgba(0,0,0,0.025), transparent 50%),
    radial-gradient(circle at 80% 100%, rgba(0,0,0,0.025), transparent 50%);
}
::selection { background: var(--ink); color: var(--paper); }

/* ===== 滾動條（極簡） ===== */
.scroll-y::-webkit-scrollbar { width: 4px; }
.scroll-y::-webkit-scrollbar-track { background: transparent; }
.scroll-y::-webkit-scrollbar-thumb { background: rgba(10,10,10,0.18); border-radius: 2px; }
.scroll-y::-webkit-scrollbar-thumb:hover { background: rgba(10,10,10,0.4); }
.scroll-y { scrollbar-width: thin; scrollbar-color: rgba(10,10,10,0.18) transparent; }

/* ===== 主容器 ===== */
.app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 28px 32px 24px;
  height: 100vh;
  display: flex;
  flex-direction: column;
  position: relative;
}

header.app-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 24px;
}
.brand {
  display: flex; align-items: baseline; gap: 14px;
  text-decoration: none; color: var(--ink);
}
.brand .mark {
  font-size: 22px; letter-spacing: 8px; font-weight: 500;
}
.brand .sub {
  font-family: "JetBrains Mono", monospace;
  font-size: 11px; letter-spacing: 4px;
  text-transform: uppercase; color: var(--line-mid);
}
.meta {
  font-family: "JetBrains Mono", monospace;
  font-size: 11px; letter-spacing: 2px; color: var(--line-mid);
}
.nav-back {
  font-family: "JetBrains Mono", monospace;
  font-size: 12px; letter-spacing: 3px; color: var(--line-mid);
  text-decoration: none; padding: 4px 8px;
  border: 1px solid var(--line-soft);
  transition: all 0.15s;
}
.nav-back:hover { border-color: var(--ink); color: var(--ink); }

/* ===== 階段 ===== */
.stage { display: none; flex: 1; min-height: 0; animation: fadeIn 0.5s ease; }
.stage.active { display: flex; flex-direction: column; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
.stage-body {
  flex: 1; min-height: 0; overflow: hidden;
  display: flex; flex-direction: column;
}
.stage-foot { flex-shrink: 0; padding-top: 14px; }

/* ===== 文字 ===== */
h1 { font-size: 34px; font-weight: 400; letter-spacing: 6px; margin-bottom: 8px; flex-shrink: 0; }
h2 {
  font-size: 18px; font-weight: 400; letter-spacing: 4px;
  margin-bottom: 14px; padding-bottom: 8px;
  border-bottom: 1px solid var(--line-soft);
  flex-shrink: 0;
}
.lead {
  font-size: 14px; line-height: 1.85;
  color: rgba(10,10,10,0.7);
  margin: 10px 0 18px; max-width: 640px;
}
.label {
  font-family: "JetBrains Mono", monospace;
  font-size: 10px; letter-spacing: 3px;
  text-transform: uppercase; color: var(--line-mid);
  margin-bottom: 6px; display: block;
}

/* ===== 按鈕 ===== */
.btn {
  display: inline-flex; align-items: center; gap: 10px;
  padding: 12px 24px;
  background: var(--ink); color: var(--paper);
  border: 1px solid var(--ink);
  font-family: "Noto Serif TC", serif;
  font-size: 13px; letter-spacing: 4px;
  cursor: pointer; transition: all 0.2s ease;
  text-decoration: none;
}
.btn:hover { background: var(--paper); color: var(--ink); }
.btn.ghost { background: transparent; color: var(--ink); }
.btn.ghost:hover { background: var(--ink); color: var(--paper); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-arrow { font-family: "JetBrains Mono", monospace; font-size: 16px; }

.action-row {
  display: flex; gap: 16px; margin-top: 24px;
  align-items: center; flex-wrap: wrap; flex-shrink: 0;
}

/* ===== 輸入（統一高度 44px） ===== */
textarea, .text-input {
  width: 100%;
  background: rgba(255,255,255,0.5);
  border: 1px solid var(--line);
  font-family: inherit;
  color: var(--ink);
  resize: none; outline: none;
  box-sizing: border-box;
}

/* 單行輸入：統一 44px 高、14.5px 字、水平 16px padding */
.text-input {
  height: 44px;
  padding: 0 16px;
  font-size: 14.5px;
  line-height: 42px;          /* 高度 - 上下 border = 42 */
  letter-spacing: 0.5px;
}

/* date/time/number 把瀏覽器原生小圖示對齊到右側 */
input.text-input[type="date"],
input.text-input[type="time"] {
  padding-right: 12px;
  font-family: "JetBrains Mono", "Noto Serif TC", monospace;
  font-size: 13.5px; letter-spacing: 1px;
}
input.text-input[type="number"] {
  font-family: "JetBrains Mono", monospace;
  font-size: 13.5px; letter-spacing: 1px;
}
/* 移除 number 旋轉箭頭（更乾淨） */
input.text-input[type="number"]::-webkit-outer-spin-button,
input.text-input[type="number"]::-webkit-inner-spin-button {
  -webkit-appearance: none; margin: 0;
}
input.text-input[type="number"] { -moz-appearance: textfield; }

/* 多行 textarea 用獨立規則（可變高） */
textarea {
  padding: 12px 16px;
  font-size: 14.5px; line-height: 1.7;
  min-height: 90px; max-height: 130px;
}

textarea:focus, .text-input:focus { background: #fff; border-color: var(--ink); }

select.text-input {
  height: 44px;
  padding: 0 36px 0 16px;
  line-height: 42px;
  appearance: none;
  background: rgba(255,255,255,0.5)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%230a0a0a' fill-opacity='0.5'/%3E%3C/svg%3E")
    no-repeat right 16px center;
  cursor: pointer;
}

/* ===== 通用 grid ===== */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
@media (max-width: 880px) {
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}

/* ===== 加載 / 錯誤 ===== */
.spinner {
  display: inline-block; width: 14px; height: 14px;
  border: 1.5px solid var(--line-soft);
  border-top-color: var(--ink);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle; margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.err {
  padding: 12px 16px;
  border: 1px solid var(--accent);
  color: var(--accent);
  background: rgba(139, 26, 26, 0.05);
  margin: 16px 0; font-size: 14px;
}

.typing-cursor {
  display: inline-block;
  width: 8px; height: 16px;
  background: var(--ink);
  vertical-align: middle;
  animation: blink 1s infinite;
}
@keyframes blink { 0%, 50% { opacity: 1; } 51%, 100% { opacity: 0; } }

/* ===== Loading 卡片（共用） ===== */
.loading-state {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 28px 16px;
  gap: 16px;
  text-align: center;
}
.loading-state .lt-title {
  font-size: 15px; letter-spacing: 4px; color: var(--ink);
}
.loading-state .lt-sub {
  font-family: "JetBrains Mono", monospace;
  font-size: 11px; line-height: 1.8; letter-spacing: 1px;
  color: var(--line-mid);
  max-width: 360px;
}
.loading-state .lt-elapsed {
  font-family: "JetBrains Mono", monospace;
  font-size: 13px; letter-spacing: 3px; color: var(--ink);
  padding: 6px 16px;
  border: 1px solid var(--line-soft);
  background: rgba(255,255,255,0.5);
  margin-top: 4px;
}
.loading-dots {
  display: flex; gap: 8px;
}
.loading-dots span {
  width: 6px; height: 6px;
  background: var(--ink);
  border-radius: 50%;
  animation: dotpulse 1.4s ease-in-out infinite;
}
.loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.loading-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes dotpulse {
  0%, 60%, 100% { opacity: 0.2; transform: scale(0.8); }
  30% { opacity: 1; transform: scale(1); }
}

/* ===== 掐指算載入器（手掌 + 大拇指尖蛇形遊走 12 指節） ===== */
.palm-loader {
  display: inline-block;
  width: 92px; height: 86px;
  color: var(--ink);
  user-select: none;
}
.palm-loader svg { width: 100%; height: 100%; display: block; overflow: visible; }

/* 12 段位置（每指 3 段：tip / mid / base）—— 與 SVG 內 line 對齊
   pinky(x=22)  tip(y=24) / mid(y=36) / base(y=48)
   ring(x=40)   tip(y=15) / mid(y=29) / base(y=43)
   middle(x=58) tip(y=13) / mid(y=27) / base(y=41)
   index(x=76)  tip(y=22) / mid(y=34) / base(y=46)

   蛇形路徑：index_tip → index_mid → index_base
   → middle_base → middle_mid → middle_tip
   → ring_tip → ring_mid → ring_base
   → pinky_base → pinky_mid → pinky_tip
   alternate 反向回掃；2.4s × 2 = 4.8s 一個完整來回 */
.palm-loader .thumb-tip {
  animation: thumb-walk 2.4s ease-in-out infinite alternate;
  transform-box: fill-box;
  transform-origin: center;
}
@keyframes thumb-walk {
  0%   { transform: translate(76px, 22px); }
  9%   { transform: translate(76px, 34px); }
  18%  { transform: translate(76px, 46px); }
  27%  { transform: translate(58px, 41px); }
  36%  { transform: translate(58px, 27px); }
  45%  { transform: translate(58px, 13px); }
  54%  { transform: translate(40px, 15px); }
  63%  { transform: translate(40px, 29px); }
  72%  { transform: translate(40px, 43px); }
  81%  { transform: translate(22px, 48px); }
  90%  { transform: translate(22px, 36px); }
  100% { transform: translate(22px, 24px); }
}

/* 指尖落點淡入淡出的「叩擊」漣漪（與 thumb-tip 同位置） */
.palm-loader .thumb-ripple {
  animation: thumb-walk 2.4s ease-in-out infinite alternate,
             thumb-pulse 0.4s ease-out infinite;
  transform-box: fill-box;
  transform-origin: center;
  opacity: 0;
}
@keyframes thumb-pulse {
  0%   { opacity: 0.55; }
  100% { opacity: 0; }
}

/* 進度條（占位動效） */
.loading-bar {
  width: 200px; height: 2px;
  background: var(--line-soft);
  position: relative; overflow: hidden;
}
.loading-bar::after {
  content: ""; position: absolute;
  width: 40%; height: 100%;
  background: var(--ink);
  animation: bar-slide 1.6s ease-in-out infinite;
}
@keyframes bar-slide {
  0% { left: -40%; }
  100% { left: 100%; }
}

.ornament {
  text-align: center;
  margin: 6px 0 6px;
  font-family: "JetBrains Mono", monospace;
  font-size: 10px; letter-spacing: 6px;
  color: var(--line-mid);
}
.ornament::before, .ornament::after {
  content: ""; display: inline-block;
  width: 40px; height: 1px;
  background: var(--line-mid);
  vertical-align: middle; margin: 0 12px;
}

/* ===== 觀盤 / 解 雙欄通用 ===== */
.split-2 {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 28px;
  overflow: hidden;
}
.split-left {
  min-height: 0; overflow: hidden;
  display: flex; flex-direction: column; gap: 12px;
}
.split-right {
  min-height: 0; overflow-y: auto;
  padding-right: 12px;
}
.split-right::-webkit-scrollbar { width: 4px; }
.split-right::-webkit-scrollbar-track { background: transparent; }
.split-right::-webkit-scrollbar-thumb { background: rgba(10,10,10,0.18); border-radius: 2px; }
.split-right::-webkit-scrollbar-thumb:hover { background: rgba(10,10,10,0.4); }
.split-right { scrollbar-width: thin; scrollbar-color: rgba(10,10,10,0.18) transparent; }
@media (max-width: 880px) {
  .split-2 { grid-template-columns: 1fr; gap: 16px; }
}

/* ===== 解讀卡片（共用） ===== */
.reading {
  margin-top: 8px;
  padding: 28px 32px 32px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,0.5);
  position: relative;
}
.reading::before {
  content: "解";
  position: absolute;
  top: -12px; left: 22px;
  background: var(--paper); padding: 0 10px;
  font-size: 15px; letter-spacing: 4px;
}
.reading-content {
  font-size: 14px; line-height: 1.95; letter-spacing: 0.3px;
}
.reading-content h2 {
  font-size: 17px; font-weight: 500; letter-spacing: 4px;
  margin: 26px 0 12px; padding-bottom: 6px;
  border-bottom: 1px solid var(--line-soft);
}
.reading-content h2:first-child { margin-top: 0; }
.reading-content h3 {
  font-size: 15px; font-weight: 500; letter-spacing: 2px;
  margin: 20px 0 8px;
}
.reading-content p { margin: 14px 0; }
.reading-content ul, .reading-content ol { margin: 12px 0; padding-left: 24px; }
.reading-content li { margin: 4px 0; }
.reading-content strong { font-weight: 500; }
.reading-content em { font-style: normal; border-bottom: 1px solid var(--line-mid); }
.reading-content code {
  font-family: "JetBrains Mono", monospace;
  font-size: 0.9em; background: var(--paper-2); padding: 1px 6px;
}

/* ===== 資訊行（chart-info 共用） ===== */
.info-table {
  font-family: "JetBrains Mono", monospace;
  font-size: 11px; line-height: 1.65; letter-spacing: 0.5px;
  flex-shrink: 0;
}
.info-table .row {
  display: flex; justify-content: space-between;
  border-bottom: 1px dashed var(--line-soft);
  padding: 2px 0;
}
.info-table .row .k { color: var(--line-mid); margin-right: 12px; white-space: nowrap; }
.info-table .row .v { text-align: right; }

/* ===================================================================
   移動端自適應（≤ 768px = 平板直 + 手機；≤ 480px = 純手機）
   核心策略：解除桌面端的「視窗鎖定 + 內部滾動」改成自然頁面流；
   所有雙欄/三欄/四欄轉單欄；字體與間距按比例縮減；觸控目標 ≥ 44px。
   =================================================================== */

@media (max-width: 768px) {
  /* 解除頁面鎖定，允許整頁滾動 */
  html, body {
    height: auto;
    min-height: 100vh;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-text-size-adjust: 100%;
  }
  /* iOS safe area 支援 */
  .app {
    padding: 16px 16px calc(20px + env(safe-area-inset-bottom, 0px));
    height: auto;
    min-height: 100vh;
  }

  /* === Header === */
  header.app-header {
    margin-bottom: 16px;
    padding-bottom: 10px;
    flex-wrap: wrap;
    gap: 8px;
  }
  .brand .mark { font-size: 18px; letter-spacing: 4px; }
  .brand .sub { font-size: 9px; letter-spacing: 2px; }
  .meta { display: none; }   /* 行動裝置隱藏時鐘，讓出空間 */
  .nav-back { font-size: 11px; padding: 6px 10px; }

  /* === Headings === */
  h1 { font-size: 24px; letter-spacing: 4px; }
  h2 { font-size: 15px; letter-spacing: 3px; margin-bottom: 12px; padding-bottom: 6px; }
  .lead { font-size: 13px; line-height: 1.75; margin: 8px 0 14px; }
  .label { font-size: 10px; letter-spacing: 2px; }

  /* === Stage 容器：取消內部 overflow 限制，讓頁面自然滾 === */
  .stage.active { display: flex; flex-direction: column; }
  .stage-body {
    overflow: visible;
    flex: 0 0 auto;
    min-height: 0;
  }
  .stage-foot { padding-top: 12px; }

  /* === Buttons：觸控目標放大 === */
  .btn {
    padding: 14px 22px;
    font-size: 13px;
    letter-spacing: 3px;
    min-height: 48px;
    width: 100%;
    justify-content: center;
  }
  .btn-arrow { font-size: 14px; }
  .action-row {
    flex-direction: column-reverse;
    align-items: stretch;
    gap: 10px;
    margin-top: 18px;
  }
  .action-row .btn { width: 100%; }

  /* === Inputs：高度提升至 48px、字稍大避免 iOS 自動放大 === */
  .text-input {
    height: 48px;
    line-height: 46px;
    font-size: 16px;       /* iOS 16px 以下會自動 zoom */
    padding: 0 14px;
  }
  input.text-input[type="date"],
  input.text-input[type="time"] { font-size: 14px; }
  select.text-input { padding: 0 36px 0 14px; line-height: 46px; }
  textarea {
    font-size: 16px; padding: 12px 14px;
    min-height: 100px; max-height: 180px;
  }

  /* === 通用 grid → 全部單欄 === */
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; gap: 12px; }
  .form-grid { grid-template-columns: 1fr !important; gap: 12px !important; }
  .form-grid.two { grid-template-columns: 1fr !important; }

  /* === 雙欄佈局 → 單欄縱向 === */
  .split-2 {
    grid-template-columns: 1fr;
    gap: 14px;
    overflow: visible;
  }
  .split-left,
  .split-right {
    overflow: visible;
    min-height: 0;
    padding-right: 0;
  }

  /* === 解讀卡片 === */
  .reading {
    padding: 22px 16px 24px;
    margin-top: 10px;
  }
  .reading::before { left: 14px; font-size: 13px; letter-spacing: 3px; }
  .reading-content { font-size: 14px; line-height: 1.85; }
  .reading-content h2 { font-size: 15px; letter-spacing: 3px; margin: 20px 0 10px; }
  .reading-content h3 { font-size: 14px; letter-spacing: 1px; margin: 16px 0 6px; }
  .reading-content p { margin: 11px 0; }

  /* === 資訊表 === */
  .info-table { font-size: 11px; }
  .info-table .row { padding: 4px 0; flex-wrap: wrap; gap: 4px 12px; }
  .info-table .row .v { text-align: left; word-break: break-all; }

  /* === Loading === */
  .loading-state { padding: 22px 12px; gap: 14px; }
  .loading-state .lt-title { font-size: 14px; letter-spacing: 3px; }
  .loading-state .lt-sub { font-size: 11px; max-width: 320px; }
  .loading-bar { width: 160px; }
  .palm-loader { width: 78px; height: 72px; }

  /* === Ornament 上下虛線 === */
  .ornament::before, .ornament::after { width: 28px; margin: 0 8px; }

  /* === 卜·奇門 八卦盤 SVG === */
  .chart-svg-wrap svg { width: 100%; height: auto; max-width: none; }

  /* === 命·四柱柱表 === */
  .bazi-board .pillar { padding: 10px 6px; }
  .bazi-board .pillar .gan,
  .bazi-board .pillar .zhi { font-size: 24px; }
  .bazi-board .pillar .label,
  .bazi-board .pillar .shishen,
  .bazi-board .pillar .hidden,
  .bazi-board .pillar .nayin { font-size: 9px; letter-spacing: 0.5px; }

  /* === 紫微 12 宮盤：每格內字體再壓縮一點，化字角標也縮 === */
  .ziwei-board { max-height: none; aspect-ratio: 1 / 1; }
  .ziwei-board .zw-cell {
    padding: 4px 5px 3px;
    font-size: 9px;
  }
  .ziwei-board .zw-cell .zw-name { font-size: 10px; letter-spacing: 0; }
  .ziwei-board .zw-cell .zw-gz { font-size: 8px; }
  .ziwei-board .zw-cell .zw-stars { gap: 1px 3px; }
  .ziwei-board .zw-star .zw-star-name { font-size: 9.5px; line-height: 1.1; }
  .ziwei-board .zw-star .zw-star-hua { font-size: 7px; }
  .ziwei-board .zw-cell .zw-foot { font-size: 7px; }
  .zw-center { padding: 8px 8px; font-size: 9.5px; }
  .zw-center .zc-title { font-size: 11px; letter-spacing: 2px; padding-bottom: 4px; margin-bottom: 5px; }
  .zw-center .zc-row { padding: 1.5px 0; }
  .zw-center .zc-k, .zw-center .zc-v { font-size: 9px; }
  .zw-legend { font-size: 9.5px; gap: 4px 10px; }

  /* === 大運表 === */
  .dayun-table { padding: 8px 10px; }
  .dayun-grid { grid-template-columns: 1fr 1fr !important; gap: 4px 10px; }
  .dayun-grid .step .gz { font-size: 12px; }

  /* === 圖片預覽（山/相） === */
  #image-display img { max-width: 100% !important; max-height: 220px !important; }

}

@media (max-width: 480px) {
  .app { padding: 12px 12px calc(16px + env(safe-area-inset-bottom, 0px)); }
  header.app-header { margin-bottom: 12px; }
  .brand .mark { font-size: 16px; letter-spacing: 3px; }
  .brand .sub { display: none; }   /* sub 一行字在最小屏隱藏 */
  h1 { font-size: 20px; }
  h2 { font-size: 14px; letter-spacing: 2px; }

  /* 主控按鈕字稍小 */
  .btn { font-size: 12px; letter-spacing: 2px; }

  /* 紫微在 < 480 時 cells 會非常小，把整盤上下尺寸放開 */
  .ziwei-board { aspect-ratio: 1 / 1.05; }
  .ziwei-board .zw-cell { padding: 3px 3px 2px; }
  .ziwei-board .zw-cell .zw-name { font-size: 9.5px; }
  .ziwei-board .zw-cell .zw-gz { font-size: 7.5px; }
  .ziwei-board .zw-star .zw-star-name { font-size: 8.5px; }
  .ziwei-board .zw-star .zw-star-hua { font-size: 6.5px; }
  .zw-center { padding: 6px 6px; }
  .zw-center .zc-title { font-size: 10px; letter-spacing: 1px; }
  .zw-center .zc-k, .zw-center .zc-v { font-size: 8.5px; }
}
