* { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; box-sizing: border-box; }
[hidden] { display: none !important; }

:root {
  --bg: #f7f5f2;
  --surface: #ffffff;
  --text: #2f2c28;
  --muted: #77716a;
  --line: #e4dfd8;
  --line-strong: #cfc8be;
  --btn: #4b4842;
  --btn-hover: #3a3833;
  --on-btn: #ffffff;
  --soft: #efebe5;
  --mark: #8a5a2b;        /* 動爻、世應等標示 */
  --warn-bg: #fbf3e4;
  --warn-line: #e6cf9f;
  --error: #b3261e;
  --radius: 10px;
  --font: "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", system-ui, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "Consolas", "PingFang TC", "Microsoft JhengHei", monospace;
}

html { touch-action: pan-x pan-y; overflow-x: clip; overscroll-behavior-x: none; }
body {
  margin: 0; background: var(--bg); color: var(--text); font-family: var(--font);
  font-size: 15px; line-height: 1.65; overflow-x: clip; max-width: 100%;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: inherit; }
h1, h2, h3 { line-height: 1.35; margin: 0; }
h1 { font-size: 22px; font-weight: 600; }
h2 { font-size: 17px; font-weight: 600; }
h3 { font-size: 15px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ───── 頂部 ───── */
.topbar {
  position: sticky; top: 0; z-index: 30; display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px; background: rgba(247,245,242,.94);
  border-bottom: 1px solid var(--line); backdrop-filter: blur(8px);
}
.brand { font-weight: 700; font-size: 18px; letter-spacing: .04em; text-decoration: none; }
.nav { display: flex; gap: 4px; margin-left: auto; }
.nav a { text-decoration: none; padding: 10px 12px; border-radius: 8px; color: var(--muted); min-height: 44px; display: flex; align-items: center; }
.nav a[aria-current="page"] { color: var(--text); background: var(--soft); }
.menu-btn { display: none; }
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.28); z-index: 40; }

@media (max-width: 720px) {
  .menu-btn {
    display: flex !important; align-items: center; justify-content: center; width: 44px; height: 44px;
    border: 0; background: transparent; color: var(--text); border-radius: 8px; margin-left: -8px;
  }
  .nav {
    position: fixed; top: 0; left: 0; bottom: 0; width: 240px; z-index: 50; flex-direction: column;
    background: var(--surface); padding: calc(env(safe-area-inset-top) + 16px) 12px 16px; margin: 0;
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: 0 0 24px rgba(0,0,0,.12);
  }
  .nav.open { transform: none; }
}

/* ───── 版面 ───── */
.page { max-width: 880px; margin: 0 auto; padding: 20px 16px 40px; }
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.section { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.section > h2 { margin-bottom: 12px; }
.footer { max-width: 880px; margin: 0 auto; padding: 16px 16px 28px; display: flex; justify-content: space-between; gap: 12px; color: var(--muted); font-size: 12px; }

/* ───── 表單 ───── */
.field { margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field > label, .field > .label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.row { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr)); gap: 12px; }
input[type="text"], input[type="number"], input[type="datetime-local"], select, textarea {
  width: 100%; font: inherit; font-size: 16px; color: var(--text); background: var(--surface);
  border: 1px solid var(--line-strong); border-radius: 8px; padding: 10px 12px; min-height: 44px;
}
select {
  appearance: none; -webkit-appearance: none; padding-right: 36px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2 4l4 4 4-4' fill='none' stroke='%2377716a' stroke-width='1.6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid #b9ad9d; outline-offset: 1px; }
.counter { text-align: right; font-size: 12px; color: var(--muted); margin-top: 4px; }
.error-text { color: var(--error); font-size: 13px; margin-top: 6px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 44px; padding: 10px 18px;
  border: 1px solid var(--btn); border-radius: 8px; background: var(--btn); color: var(--on-btn);
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
.btn:hover { background: var(--btn-hover); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.secondary { background: var(--surface); color: var(--text); border-color: var(--line-strong); }
.btn.secondary:hover { background: var(--soft); }
.btn.block { width: 100%; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* 分頁籤 */
.tabs { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; background: var(--soft); padding: 4px; border-radius: 10px; margin-bottom: 14px; }
.tabs button { border: 0; background: transparent; font: inherit; min-height: 44px; border-radius: 8px; color: var(--muted); cursor: pointer; padding: 6px; }
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.06); }

/* 逐爻輸入 */
.yao-list { display: flex; flex-direction: column; gap: 6px; }
.yao-row { display: grid; grid-template-columns: 44px minmax(0, 1fr); align-items: center; gap: 8px; }
.yao-row .pos { font-size: 13px; color: var(--muted); }
.seg { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 4px; }
.seg button {
  min-height: 44px; border: 1px solid var(--line-strong); background: var(--surface); border-radius: 8px;
  font: inherit; font-size: 13px; line-height: 1.2; cursor: pointer; color: var(--text); padding: 4px 2px;
}
.seg button[aria-pressed="true"] { background: var(--btn); border-color: var(--btn); color: var(--on-btn); }
.seg small { display: block; font-size: 11px; opacity: .75; }

.coin-result { font-size: 15px; }
.coin-row { display: grid; grid-template-columns: 44px 96px 40px 64px 20px; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.coin-row:last-child { border-bottom: 0; }
.coin-row .pos { color: var(--muted); font-size: 13px; }
.coin-row .mv { color: var(--mark); font-weight: 700; }
.hint { font-size: 13px; color: var(--muted); margin: 8px 0 0; }

details.advanced > summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; font-weight: 600; }
details.advanced .field { margin-top: 12px; }

/* ───── 盤面 ───── */
.notice { background: var(--warn-bg); border: 1px solid var(--warn-line); border-radius: 8px; padding: 10px 12px; font-size: 14px; margin-bottom: 14px; }
.summary-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr)); gap: 8px 16px; font-size: 14px; }
.summary-grid div span { color: var(--muted); margin-right: 6px; }
.gua-title { font-size: 20px; font-weight: 600; margin-bottom: 4px; }

.board { width: 100%; border-collapse: collapse; font-size: 14px; }
.board th { font-weight: 500; color: var(--muted); font-size: 12px; text-align: left; padding: 0 4px 6px; border-bottom: 1px solid var(--line); }
.board td { padding: 9px 4px; border-bottom: 1px solid var(--line); vertical-align: middle; white-space: nowrap; }
.board tr:last-child td { border-bottom: 0; }
.board .fu { display: block; font-size: 11px; color: var(--muted); }
.board .mv { color: var(--mark); font-weight: 700; }
.board .sy { color: var(--mark); font-weight: 600; }
.board .tags { display: block; font-size: 11px; color: var(--muted); white-space: normal; }
.yao-sym { display: inline-flex; gap: 6px; width: 64px; }
.yao-sym i { display: block; height: 8px; background: var(--text); border-radius: 1px; flex: 1; }
.yao-sym.yin i { flex: 0 0 29px; }
.yao-sym.yin { justify-content: space-between; }

.facts { margin: 0; padding: 0; list-style: none; font-size: 14px; }
.facts li { padding: 8px 0; border-bottom: 1px solid var(--line); }
.facts li:last-child { border-bottom: 0; }
.facts .sub { display: block; color: var(--muted); font-size: 13px; }

details.block { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 14px; }
details.block > summary { cursor: pointer; padding: 14px 16px; font-weight: 600; min-height: 44px; }
details.block > .inner { padding: 0 16px 14px; }

.text-out { font-family: var(--mono); font-size: 12.5px; line-height: 1.6; white-space: pre-wrap; word-break: break-word; background: #fbfaf8; border: 1px solid var(--line); border-radius: 8px; padding: 12px; max-height: 420px; overflow: auto; margin: 0 0 12px; }
.check { display: flex; align-items: center; gap: 8px; min-height: 44px; font-size: 14px; }
.check input { width: 20px; height: 20px; }

.yaoci li { padding: 8px 0; border-bottom: 1px solid var(--line); }
.yaoci li:last-child { border-bottom: 0; }
.yaoci b { font-weight: 600; margin-right: 6px; }

/* 規則頁 */
.rule-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.rule-table th, .rule-table td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.rule-table th { color: var(--muted); font-weight: 500; font-size: 12px; }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 60;
  background: var(--btn); color: var(--on-btn); padding: 10px 16px; border-radius: 8px; font-size: 14px; max-width: calc(100% - 32px);
}
.toast.error { background: var(--error); }

@media print {
  .topbar, .footer, .no-print, .toast { display: none !important; }
  body { background: #fff; }
  .section, details.block { border: 0; padding: 0; }
  details.block > .inner { display: block; }
}

/* ───── 五行顏色（只用於區分五行，不代表吉凶） ───── */
.el-wood { color: #2f7d3a; }
.el-fire { color: #c0392b; }
.el-earth { color: #95662b; }
.el-metal { color: #b7861f; }
.el-water { color: #1f4f9c; }

/* ───── 盤面頁：頂部資訊 ───── */
.head-card .question { margin: 0 0 12px; }
.head-card .question .muted { margin-right: 8px; font-size: 13px; }
.head-grid { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 16px; align-items: center; }
.pillars { display: grid; grid-template-columns: repeat(4, 44px); gap: 4px; text-align: center; }
.pillar-k { font-size: 12px; color: var(--muted); background: var(--soft); border-radius: 4px; }
.pillar.day .pillar-k { color: var(--text); font-weight: 600; }
.pillar-s, .pillar-b { font-size: 26px; line-height: 1.25; font-weight: 600; }
.date-box { font-size: 14px; line-height: 1.7; }
.date-box .date-main { font-weight: 600; }
.wx-bar { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); margin-top: 14px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.wx-bar div { text-align: center; padding: 6px 0; font-size: 15px; font-weight: 600; }
.wx-bar div + div { border-left: 1px solid var(--line); }
.wx-bar span { margin-left: 6px; color: var(--text); font-weight: 500; }
@media (max-width: 520px) {
  .head-grid { grid-template-columns: minmax(0, 1fr); }
  .pillars { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

.chart-tabs { grid-template-columns: repeat(4, minmax(0, 1fr)); position: sticky; top: calc(env(safe-area-inset-top) + 61px); z-index: 20; }

/* ───── 六爻盤 ───── */
.pan-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; font-size: 17px; }
.pan-table { width: 100%; border-collapse: collapse; table-layout: fixed; }
.pan-table th { font-size: 12px; font-weight: 500; color: var(--muted); padding: 0 2px 6px; border-bottom: 1px solid var(--line-strong); text-align: center; }
.pan-table td { padding: 10px 2px; border-bottom: 1px solid var(--line); text-align: center; vertical-align: middle; font-size: 15px; }
.pan-table tr:last-child td { border-bottom: 0; }
.pan-table th:nth-child(1), .pan-table td:nth-child(1) { width: 17%; }
.pan-table th:nth-child(2), .pan-table td:nth-child(2) { width: 14%; }
.pan-table th:nth-child(3), .pan-table td:nth-child(3) { width: 14%; }
.pan-table th:nth-child(4), .pan-table td:nth-child(4) { width: 20%; }
.pan-table th:nth-child(5), .pan-table td:nth-child(5) { width: 18%; }
.pan-table th:nth-child(6), .pan-table td:nth-child(6) { width: 17%; }
.pan-table tr.is-moving td { background: #fbf6ee; }
.c-fu { font-size: 13px !important; line-height: 1.35; }
.c-fu .fu-q { display: block; color: var(--muted); }
.c-yao { white-space: nowrap; }
.c-yao .yao-sym { width: 44px; vertical-align: middle; }
.c-yao .yao-sym.yin i { flex: 0 0 19px; }
.c-yao .mv { display: inline-block; width: 14px; color: var(--mark); font-weight: 700; margin-left: 2px; }
.c-yao .sy { display: block; font-size: 13px; color: var(--mark); font-weight: 600; min-height: 1em; line-height: 1.2; margin-top: 2px; }
.c-na .na-b, .c-bian .na-b { display: block; font-weight: 600; }
.c-bian .bian-q { display: block; font-size: 13px; color: var(--muted); }
.c-qin .ym { display: block; font-size: 11px; color: var(--mark); }
.badge { display: inline-block; font-size: 11px; line-height: 1.5; padding: 0 5px; margin: 2px 1px 0; border-radius: 4px; background: var(--warn-bg); border: 1px solid var(--warn-line); color: #6b4a1a; font-weight: 500; }
.kong-line { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--line); font-size: 14px; }
.kong-line .kong-day { font-weight: 600; }

.info-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); gap: 6px 16px; font-size: 14px; }
.info-grid span, .shensha span { color: var(--muted); margin-right: 8px; }
.shensha span { display: inline-block; width: 4.5em; }
.shensha { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 230px), 1fr)); gap: 6px 16px; font-size: 14px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.shensha em { font-style: normal; font-size: 12px; color: var(--muted); margin-left: 6px; }

/* ───── 卦象 ───── */
.gua-cards { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.gua-card { border: 1px solid var(--line); border-radius: 8px; padding: 10px 4px; text-align: center; }
.gua-label { font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.gua-body { display: flex; justify-content: center; align-items: center; gap: 8px; }
.gua-tris { display: grid; grid-template-rows: 1fr 1fr; height: 132px; }
.tri-name { display: flex; flex-direction: column; justify-content: center; font-size: 20px; font-weight: 600; line-height: 1.2; }
.tri-name small { font-size: 13px; font-weight: 500; }
.tri-name .ty { font-style: normal; font-size: 12px; color: var(--mark); margin-top: 2px; }
.gua-name { margin-top: 8px; font-size: 17px; font-weight: 600; }
.hex-sym { display: inline-flex; flex-direction: column; gap: 9px; padding: 4px 0; }
.hex-line { display: flex; align-items: center; height: 13px; position: relative; }
.hex-line .yao-sym { width: 56px; }
.hex-line .yao-sym i { height: 10px; }
.hex-line .yao-sym.yin i { flex: 0 0 24px; }
@media (max-width: 420px) { .hex-line .yao-sym { width: 44px; } .hex-line .yao-sym.yin i { flex: 0 0 19px; } .tri-name { font-size: 17px; } .gua-name { font-size: 15px; } }
.hex-line.moving::after { content: ''; position: absolute; right: -7px; width: 4px; height: 4px; border-radius: 50%; background: var(--mark); }

/* ───── 經文 ───── */
.subtabs { display: grid; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); border: 1px solid var(--btn); border-radius: 8px; overflow: hidden; margin-bottom: 14px; }
.subtabs button { border: 0; background: var(--surface); color: var(--text); min-height: 44px; font: inherit; cursor: pointer; }
.subtabs button + button { border-left: 1px solid var(--btn); }
.subtabs button[aria-selected="true"] { background: var(--btn); color: var(--on-btn); font-weight: 600; }
.jing-head { display: flex; align-items: center; gap: 20px; }
.jing-head .hex-line .yao-sym { width: 60px; }
.jing-head .hex-line .yao-sym.yin i { flex: 0 0 25px; }
.jing-title { font-size: 18px; font-weight: 600; }
.jing-row { display: grid; grid-template-columns: 52px minmax(0, 1fr); gap: 8px; padding: 10px 0; border-bottom: 1px solid var(--line); line-height: 1.8; }
.jing-row:last-child { border-bottom: 0; }
.jing-row b { color: var(--muted); font-weight: 600; font-size: 14px; }
.jing-row.head span { font-weight: 600; }
.jing-yao { border-bottom: 1px solid var(--line); padding: 10px 0; line-height: 1.8; }
.jing-yao:last-of-type { border-bottom: 0; }
.jing-yao .yao-text b { margin-right: 8px; }
.jing-yao .yao-xiang { color: var(--muted); font-size: 14px; display: grid; grid-template-columns: 40px minmax(0, 1fr); }
.jing-yao .yao-xiang b { font-weight: 500; }
.jing-yao.moving .yao-text { color: var(--mark); font-weight: 600; }
.jing-yao.moving { background: #fbf6ee; margin: 0 -16px; padding-left: 16px; padding-right: 16px; }
.text-out.tall { max-height: 60vh; }

/* ───── 教學 ───── */
.toc { margin: 0; padding-left: 0; list-style: none; counter-reset: none; }
.toc li { padding: 12px 0; border-bottom: 1px solid var(--line); }
.toc li:last-child { border-bottom: 0; }
.toc a { font-weight: 600; text-decoration: none; display: block; min-height: 24px; }
.toc span { display: block; font-size: 13px; color: var(--muted); margin-top: 2px; }
.sec-nav { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.sec-nav a { font-size: 13px; text-decoration: none; padding: 6px 10px; border: 1px solid var(--line-strong); border-radius: 999px; background: var(--surface); min-height: 32px; display: inline-flex; align-items: center; }
.learn { scroll-margin-top: 76px; line-height: 1.85; }
.learn p { margin: 0 0 10px; }
.learn ul, .learn ol { padding-left: 1.4em; margin: 0 0 10px; }
.learn li { margin-bottom: 4px; }
.learn .tbl-wrap { overflow-x: auto; margin: 6px 0 12px; }
.learn .rule-table td, .learn .rule-table th { line-height: 1.6; }
.school { background: #f3f0f8; border-left: 3px solid #7a6a9a; border-radius: 6px; padding: 10px 12px; margin: 10px 0; font-size: 14px; }
.school b { display: block; color: #5b4d78; font-size: 13px; margin-bottom: 2px; }
.tip { background: var(--soft); border-radius: 6px; padding: 10px 12px; margin: 10px 0; font-size: 14px; color: var(--text); }
.ex-link { font-size: 13px; color: var(--mark); white-space: nowrap; }
.yao-inline { display: inline-flex; gap: 5px; width: 48px; vertical-align: middle; }
.yao-inline i { display: block; height: 7px; background: var(--text); flex: 1; }
.yao-inline.yin i { flex: 0 0 21px; }
.yao-inline.yin { justify-content: space-between; }
.pager { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.pager .btn { font-weight: 500; }
.learn .rule-table td, .learn .rule-table th { min-width: 3.6em; }
