/* ค้นฎีกา — ดีไซน์แบบที่ 2 (deka-variant2-reference) เขียวเด่น + ครีมอ่อน + เงาพรีเมียม
   ขนาดตัวอักษรเป็น rem ทั้งหมด เพื่อให้ปรับขนาดตัวอักษร 3 ระดับได้จาก --scale บน html */
:root {
  --bg: #f4f2ea;
  --brand: #798763;
  --brand-top: #85936e;
  --btn-top: #82906b;
  --card: #ffffff;
  --ink: #2c3125;
  --ink-2: #64694f;
  --ink-3: #9a9a82;
  --body: #3a3d2c;
  --soft: #4c4f3b;
  --head: #556141;
  --chip-bg: #e9ede0;
  --chip-ink: #4f5b39;
  --line: #f0eee3;
  --ok: #4e7a3f;
  --ok-ink: #3f7233;
  --warn-bg: #eef1e6;
  --warn-ink: #5e6149;
  --tint: #f2f4ea;
  --tint-2: #f7f5ec;
  --nav-off: #a2a08c;
  --icon-soft: #a6ab8a;
  --err-bg: #f8ece6;
  --err-ink: #8a3b22;
  --shadow-card: 0 10px 26px rgba(96, 107, 74, .12), 0 1px 3px rgba(60, 66, 45, .05);
  --shadow-soft: 0 8px 22px rgba(70, 80, 55, .10), 0 1px 3px rgba(60, 66, 45, .05);
  --shadow-btn: 0 10px 24px rgba(107, 122, 84, .42);
  --shadow-head: 0 8px 22px rgba(70, 80, 55, .24);
  --grad-head: linear-gradient(180deg, var(--brand-top) 0%, var(--brand) 100%);
  --grad-btn: linear-gradient(180deg, var(--btn-top) 0%, var(--brand) 100%);
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
  --scale: 16px;
  color-scheme: light;
}
html[data-size="small"] { --scale: 14.5px; }
html[data-size="large"] { --scale: 18px; }

* { box-sizing: border-box; }
html { font-size: var(--scale); -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: #e7e4d9; color: var(--ink);
  font-family: 'IBM Plex Sans Thai', system-ui, sans-serif;
}
a { color: inherit; text-decoration: none; }
button, input, select { font-family: inherit; color: inherit; }
button { cursor: pointer; }
svg { display: block; }
.sr { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.mono { font-family: var(--mono); }
[hidden] { display: none !important; }

/* กรอบแอป: เต็มจอบนมือถือ กว้างสุด 480px บนจอใหญ่ */
.app {
  max-width: 480px; margin: 0 auto; min-height: 100dvh; background: var(--bg);
  display: flex; flex-direction: column; position: relative;
}
@media (min-width: 520px) { .app { box-shadow: 0 0 40px rgba(40, 44, 30, .18); } }
.view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
main { flex: 1; padding: 20px 20px 24px; }

/* แถบบน */
.head {
  background: var(--grad-head); color: #fff; border-radius: 0 0 26px 26px; box-shadow: var(--shadow-head);
  padding: 16px 14px 18px; display: flex; align-items: center; gap: 6px;
  position: sticky; top: 0; z-index: 5;
}
.head.home { padding: 24px 20px 22px; justify-content: space-between; }
.head .title { flex: 1; font-size: 1rem; font-weight: 600; }
.brand-name { font-size: 1.5625rem; font-weight: 700; letter-spacing: -.3px; }
.brand-sub { font-size: .75rem; color: rgba(255, 255, 255, .85); margin-top: 2px; }
.icon-btn {
  width: 40px; height: 40px; flex: none; border: 0; background: transparent; color: #fff;
  display: flex; align-items: center; justify-content: center; border-radius: 12px;
}
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn.boxed { width: 44px; height: 44px; background: rgba(255, 255, 255, .18); }
.icon-btn.boxed svg, .head .actions svg { width: 20px; height: 20px; }
.head .actions { display: flex; gap: 2px; margin-left: auto; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; font-size: .72rem; font-weight: 500;
  background: rgba(255, 255, 255, .2); border-radius: 999px; padding: 6px 11px; max-width: 55%;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.dot { width: 7px; height: 7px; border-radius: 50%; background: #d6ecc7; flex: none; }

/* ช่องค้น */
.searchbox {
  display: flex; align-items: center; gap: 10px; height: 54px; padding: 0 15px; border-radius: 15px;
  background: var(--card); box-shadow: var(--shadow-soft);
}
.searchbox svg { width: 20px; height: 20px; color: var(--brand); flex: none; }
.searchbox input { flex: 1; min-width: 0; border: 0; outline: 0; font-size: .94rem; background: transparent; }
.searchbox .go {
  flex: none; display: inline-flex; align-items: center; gap: 5px; height: 38px; padding: 0 13px; border: 0;
  border-radius: 11px; background: var(--grad-btn); color: #fff; font-size: .8125rem; font-weight: 600;
  box-shadow: 0 6px 14px rgba(107, 122, 84, .35);
}
.searchbox .go svg { width: 15px; height: 15px; color: #fff; }
.head .searchbox .go { height: 32px; padding: 0 10px; border-radius: 9px; font-size: .75rem; }
.head .searchbox { flex: 1; height: 46px; padding: 0 13px; border-radius: 13px; box-shadow: 0 6px 16px rgba(45, 52, 32, .18); color: var(--ink); }
.head .searchbox svg { width: 18px; height: 18px; }
.head .searchbox input { font-size: .875rem; }
.hints { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.hint {
  display: inline-flex; align-items: center; gap: 6px; font-size: .75rem; font-weight: 500;
  color: var(--chip-ink); background: var(--chip-bg); border-radius: 999px; padding: 7px 12px; border: 0;
}
.hint svg { width: 13px; height: 13px; color: var(--brand); }
.hint.on { background: var(--brand); color: #fff; }
.hint.on svg { color: #fff; }
.hint-note { margin: 10px 4px 0; font-size: .72rem; color: var(--ink-3); line-height: 1.5; }

/* ปุ่มค้นด้วย AI */
.ai-cta {
  display: flex; align-items: center; gap: 14px; margin-top: 18px; padding: 16px; border-radius: 18px;
  background: var(--grad-btn); color: #fff; box-shadow: 0 12px 26px rgba(107, 122, 84, .40);
}
.ai-cta .badge { width: 42px; height: 42px; flex: none; display: flex; align-items: center; justify-content: center; border-radius: 13px; background: rgba(255, 255, 255, .2); }
.ai-cta .badge svg { width: 21px; height: 21px; }
.ai-cta b { display: block; font-size: .94rem; }
.ai-cta small { display: block; font-size: .75rem; color: rgba(255, 255, 255, .85); margin-top: 2px; }
.ai-cta > svg { width: 18px; height: 18px; opacity: .8; }

/* หัวข้อย่อย + รายการ */
.section { margin: 26px 4px 10px; font-size: .8125rem; font-weight: 600; color: var(--head); display: flex; justify-content: space-between; align-items: center; }
.section button, .section .see-all { border: 0; background: transparent; font-size: .75rem; color: var(--ink-3); padding: 0; font-weight: 500; }
.grow { flex: 1; min-width: 0; }
.list { background: var(--card); border-radius: 16px; overflow: hidden; box-shadow: var(--shadow-card); }
.list a { display: flex; align-items: center; gap: 12px; padding: 14px 15px; border-bottom: 1px solid var(--line); }
.list a:last-child { border-bottom: 0; }
.list svg { width: 17px; height: 17px; color: var(--icon-soft); flex: none; }
.list .q { flex: 1; font-size: .875rem; color: var(--body); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .n { font-size: .75rem; color: var(--ink-3); }
.empty { font-size: .8125rem; color: var(--ink-3); padding: 4px 4px 0; line-height: 1.6; }

/* การ์ดฎีกา */
.cards { display: flex; flex-direction: column; gap: 12px; }
.card { display: block; background: var(--card); border-radius: 17px; padding: 16px; box-shadow: var(--shadow-card); }
.card.saved { border-radius: 15px; padding: 15px; }
.card-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.card-top .left { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; min-width: 0; }
.no { font-size: .875rem; font-weight: 600; font-family: var(--mono); color: var(--ink); }
.tag { font-size: .69rem; color: var(--chip-ink); background: var(--chip-bg); border-radius: 6px; padding: 2px 7px; }
.mark { border: 0; background: transparent; padding: 0; color: #b8b596; width: 28px; height: 28px; display: flex; align-items: center; justify-content: center; flex: none; margin: -4px -4px 0 0; }
.mark.on { color: var(--brand); }
.mark svg { width: 18px; height: 18px; }
.head .mark { color: #fff; width: 40px; height: 40px; margin: 0; }
.head .mark svg { width: 20px; height: 20px; }
.card p { margin: 10px 0 0; font-size: .84rem; line-height: 1.62; color: var(--body); }
.card.saved p { font-size: .8125rem; color: var(--soft); margin-top: 6px; line-height: 1.5; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.chip { font-size: .72rem; color: var(--chip-ink); background: var(--chip-bg); border-radius: 7px; padding: 4px 9px; font-family: var(--mono); }
.card-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.card-foot .meta { font-size: .75rem; color: var(--ink-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.more { display: inline-flex; align-items: center; gap: 4px; font-size: .78rem; font-weight: 600; color: var(--head); flex: none; }
.more svg { width: 14px; height: 14px; }

/* แถบนับผล + เรียง + กรอง */
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 16px 18px 6px; }
.toolbar .count { font-size: .8125rem; color: var(--ink-2); }
.toolbar .count b { color: var(--ink); }
.toolbar .btns { display: flex; gap: 8px; }
.tbtn {
  display: inline-flex; align-items: center; gap: 6px; background: var(--card); border: 0; border-radius: 999px;
  padding: 8px 13px; font-size: .75rem; font-weight: 500; color: var(--head); box-shadow: 0 4px 12px rgba(60, 66, 45, .08);
}
.tbtn svg { width: 14px; height: 14px; color: var(--brand); }
.tbtn.active { background: var(--chip-bg); }
.results main { padding: 8px 16px 20px; }

/* รายละเอียดฎีกา */
.sheet { background: var(--card); border-radius: 20px; padding: 20px; box-shadow: 0 12px 30px rgba(96, 107, 74, .14), 0 1px 3px rgba(60, 66, 45, .05); }
.sheet h1 { margin: 0; font-size: 1.25rem; font-weight: 600; line-height: 1.4; }
.label { margin-top: 16px; font-size: .75rem; font-weight: 600; color: var(--brand); }
.sheet .chips .chip { font-size: .75rem; border-radius: 8px; padding: 5px 10px; }
.abstract { margin-top: 18px; padding: 15px; background: var(--tint); border-radius: 14px; }
.abstract .label { margin: 0 0 8px; color: var(--head); }
.abstract p { margin: 0; font-size: .875rem; line-height: 1.72; }
.sheet h2 { margin: 20px 0 0; font-size: .8125rem; font-weight: 600; }
.long p { margin: 14px 0 0; font-size: .875rem; line-height: 1.85; color: var(--body); white-space: pre-wrap; }
.long p:first-child { margin-top: 10px; }
.fine { margin-top: 18px; font-size: .72rem; color: var(--ink-3); line-height: 1.6; }

/* ค้นด้วย AI */
.ask main { padding: 18px 16px 20px; }
.bubble {
  margin-left: auto; max-width: 82%; width: fit-content; background: var(--grad-btn); color: #fff; font-size: .875rem; line-height: 1.55;
  border-radius: 16px 16px 4px 16px; padding: 11px 14px; box-shadow: 0 8px 18px rgba(107, 122, 84, .30); margin-bottom: 18px; white-space: pre-wrap;
}
.answer { background: var(--card); border-radius: 18px; padding: 16px; box-shadow: 0 12px 30px rgba(96, 107, 74, .14), 0 1px 3px rgba(60, 66, 45, .05); }
.answer-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.answer-head .av { width: 30px; height: 30px; flex: none; border-radius: 9px; background: var(--grad-btn); display: flex; align-items: center; justify-content: center; color: #fff; box-shadow: 0 4px 10px rgba(107, 122, 84, .35); }
.answer-head .av svg { width: 16px; height: 16px; }
.answer-head span { font-size: .8125rem; font-weight: 600; color: var(--head); }
.answer p { margin: 0 0 12px; font-size: .875rem; line-height: 1.72; white-space: pre-wrap; }
.answer p:last-of-type { margin-bottom: 0; }
.cite { display: flex; align-items: center; gap: 10px; margin-top: 8px; padding: 12px; background: var(--tint-2); border-radius: 12px; }
.cite .mono { font-size: .78rem; font-weight: 600; white-space: nowrap; }
.cite .t { flex: 1; font-size: .78rem; color: var(--soft); line-height: 1.4; }
.cite svg { width: 16px; height: 16px; color: var(--brand); flex: none; }
.progress .label { color: var(--head); }
.progress .cards { margin-top: 8px; }
.progress .card { padding: 12px 14px; }
.progress .card p { font-size: .8rem; margin-top: 6px; }
.chip.wrap { white-space: normal; font-family: inherit; }
.verdict { background: var(--tint); border-radius: 12px; padding: 12px 14px; margin-bottom: 4px; }
.verdict p { margin: 0 0 8px; font-weight: 500; }
.verdict p:last-child { margin-bottom: 0; }
.step { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line); }
.step h3 { display: flex; align-items: center; gap: 8px; margin: 0 0 8px; font-size: .9rem; font-weight: 600; color: var(--head); }
.step-no {
  width: 20px; height: 20px; flex: none; border-radius: 50%; background: var(--brand); color: #fff;
  display: inline-flex; align-items: center; justify-content: center; font-size: .69rem; font-weight: 700;
}
.step .rule { margin: 0 0 8px; font-size: .84rem; line-height: 1.65; }
.step .apply { margin: 10px 0 0; font-size: .84rem; line-height: 1.65; background: var(--tint-2); border-radius: 10px; padding: 10px 12px; }
.tag.ok { background: #e2ecdb; color: var(--ok-ink); }
.cite.apart { background: var(--err-bg); }
.cite.apart .line { color: var(--err-ink); }
.cite.ruling { display: block; }
.cite-head { display: flex; align-items: center; gap: 8px; }
.cite-head .mono { font-size: .8125rem; font-weight: 600; }
.cite-head svg { width: 15px; height: 15px; color: var(--brand); margin-left: auto; flex: none; }
.cite .line { margin: 6px 0 0; font-size: .78rem; line-height: 1.5; color: var(--soft); }
.cite .line.dim { color: var(--ink-3); }
.cite blockquote {
  margin: 8px 0 0; padding: 8px 10px; border-left: 3px solid var(--brand); background: var(--card);
  border-radius: 0 8px 8px 0; font-size: .78rem; line-height: 1.6; color: var(--body);
}
.cautions { margin: 8px 0 0; padding-left: 18px; }
.cautions li { font-size: .8125rem; line-height: 1.6; color: var(--soft); margin-bottom: 6px; }
.queries { margin-top: 14px; font-size: .72rem; color: var(--ink-3); line-height: 1.6; }
.queries a { color: var(--head); font-weight: 500; }
.note.warn { background: var(--err-bg); color: var(--err-ink); }
.note.warn svg { color: var(--err-ink); }
.note { display: flex; align-items: flex-start; gap: 8px; margin-top: 14px; padding: 11px 12px; background: var(--warn-bg); border-radius: 10px; font-size: .72rem; color: var(--warn-ink); line-height: 1.55; }
.note svg { width: 15px; height: 15px; flex: none; margin-top: 1px; color: var(--brand); }
.answer-btns { display: flex; gap: 8px; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.answer-btns .tbtn { flex: 1; justify-content: center; background: var(--tint); box-shadow: none; padding: 10px; font-size: .8125rem; }
.card.saved .card-top .q { font-size: .875rem; font-weight: 600; color: var(--ink); line-height: 1.45; }
.card.saved .card-top .n { font-size: .72rem; color: var(--ink-3); white-space: nowrap; flex: none; }
.composer { background: var(--card); padding: 12px 16px calc(16px + env(safe-area-inset-bottom)); box-shadow: 0 -3px 16px rgba(60, 66, 45, .07); position: sticky; bottom: 0; }
.composer form { display: flex; align-items: center; gap: 10px; }
.composer .field { flex: 1; display: flex; align-items: center; min-height: 46px; padding: 0 16px; border-radius: 23px; background: var(--bg); }
.composer textarea { flex: 1; border: 0; outline: 0; font: inherit; font-size: .875rem; background: transparent; resize: none; padding: 12px 0; max-height: 120px; }
.send { width: 46px; height: 46px; flex: none; border: 0; border-radius: 50%; background: var(--grad-btn); display: flex; align-items: center; justify-content: center; color: #fff; box-shadow: 0 8px 18px rgba(107, 122, 84, .40); }
.send svg { width: 20px; height: 20px; }
.send:disabled { opacity: .5; }

/* ตั้งค่า */
.settings main { padding: 20px 18px 28px; }
.group { font-size: .75rem; font-weight: 600; color: var(--head); letter-spacing: .3px; margin: 22px 4px 10px; display: block; }
.group:first-child { margin-top: 0; }
.panel { background: var(--card); border-radius: 18px; padding: 18px; box-shadow: var(--shadow-card); }
.panel-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.panel-row label { font-size: .875rem; font-weight: 600; }
.status { display: inline-flex; align-items: center; gap: 5px; font-size: .72rem; color: var(--ink-3); }
.status .dot { background: var(--ink-3); }
.status.ok { color: var(--ok-ink); }
.status.ok .dot { background: var(--ok); }
.status.bad { color: var(--err-ink); }
.status.bad .dot { background: var(--err-ink); }
.secret { display: flex; align-items: center; gap: 8px; height: 48px; margin-top: 10px; padding: 0 6px 0 14px; border-radius: 12px; background: var(--bg); }
.secret input { flex: 1; min-width: 0; border: 0; outline: 0; font-size: .875rem; letter-spacing: .5px; background: transparent; font-family: var(--mono); }
.secret button { width: 40px; height: 40px; flex: none; border: 0; background: transparent; display: flex; align-items: center; justify-content: center; color: var(--brand); }
.secret button svg { width: 18px; height: 18px; }
.help { display: flex; align-items: flex-start; gap: 7px; margin-top: 10px; font-size: .72rem; color: var(--warn-ink); line-height: 1.5; }
.help svg { width: 14px; height: 14px; flex: none; margin-top: 2px; color: var(--brand); }
.link { display: inline-flex; align-items: center; gap: 5px; margin-top: 14px; font-size: .8125rem; font-weight: 600; color: var(--head); border-bottom: 1px solid var(--head); padding-bottom: 1px; }
.link svg { width: 13px; height: 13px; }
.rows { background: var(--card); border-radius: 18px; overflow: hidden; box-shadow: var(--shadow-card); }
.rows > div { display: flex; align-items: center; justify-content: space-between; padding: 16px; border-bottom: 1px solid var(--line); font-size: .875rem; }
.rows > div:last-child { border-bottom: 0; }
.seg { display: inline-flex; background: var(--bg); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: transparent; border-radius: 8px; padding: 6px 10px; font-size: .75rem; color: var(--ink-2); }
.seg button.on { background: var(--card); color: var(--head); font-weight: 600; box-shadow: 0 2px 6px rgba(60, 66, 45, .10); }
.primary {
  width: 100%; height: 52px; margin-top: 22px; border: 0; border-radius: 15px; background: var(--grad-btn);
  color: #fff; font-size: .9rem; font-weight: 700; box-shadow: var(--shadow-btn);
}
.primary.secondary { background: var(--card); color: var(--head); box-shadow: var(--shadow-card); margin-top: 12px; }

/* แถบเมนูล่าง */
.tabs { display: flex; background: var(--card); padding: 8px 8px calc(12px + env(safe-area-inset-bottom)); box-shadow: 0 -3px 16px rgba(60, 66, 45, .07); position: sticky; bottom: 0; z-index: 5; }
.tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px; color: var(--nav-off); font-size: .69rem; }
.tabs a svg { width: 22px; height: 22px; }
.tabs a.on { color: var(--head); font-weight: 600; }

/* สถานะ: กำลังโหลด, ผิดพลาด, แจ้งเตือนสั้น */
.skel { background: var(--card); border-radius: 17px; padding: 16px; box-shadow: var(--shadow-card); }
.skel i { display: block; height: 12px; border-radius: 6px; background: linear-gradient(90deg, #eeece2 0%, #f7f6f0 50%, #eeece2 100%); background-size: 200% 100%; animation: shimmer 1.2s infinite; margin-top: 10px; }
.skel i:first-child { width: 40%; height: 14px; margin-top: 0; }
.skel i:last-child { width: 70%; }
@keyframes shimmer { to { background-position: -200% 0; } }
.wait { text-align: center; font-size: .78rem; color: var(--ink-3); margin: 4px 0 12px; }
.error { background: var(--err-bg); color: var(--err-ink); border-radius: 14px; padding: 14px 15px; font-size: .84rem; line-height: 1.6; }
.error a, .error button { display: inline-block; margin-top: 8px; font-weight: 600; color: var(--err-ink); border: 0; background: transparent; padding: 0; text-decoration: underline; font-size: .84rem; }
.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--ink); color: #fff; font-size: .8125rem; padding: 10px 16px; border-radius: 999px; z-index: 30; box-shadow: 0 8px 20px rgba(0, 0, 0, .2); max-width: 90vw; text-align: center; }

/* แผ่นตัวกรอง */
.backdrop { position: fixed; inset: 0; background: rgba(44, 49, 37, .35); z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.bottom-sheet { width: 100%; max-width: 480px; background: var(--bg); border-radius: 22px 22px 0 0; padding: 18px 18px calc(20px + env(safe-area-inset-bottom)); box-shadow: 0 -10px 30px rgba(40, 44, 30, .25); }
.bottom-sheet h3 { margin: 0 0 4px; font-size: 1rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.fld { display: block; margin-top: 12px; }
.fld span { display: block; font-size: .75rem; font-weight: 600; color: var(--head); margin: 0 2px 6px; }
.fld input { width: 100%; height: 46px; border: 0; border-radius: 12px; background: var(--card); padding: 0 12px; font-size: .875rem; box-shadow: var(--shadow-soft); outline: 0; }
.fld small { display: block; font-size: .7rem; color: var(--ink-3); margin: 6px 2px 0; }
.sheet-btns { display: flex; gap: 10px; margin-top: 18px; }
.sheet-btns .primary { margin: 0; }
.sheet-btns .primary.secondary { flex: 0 0 38%; }
:focus-visible { outline: 2px solid var(--head); outline-offset: 2px; }

.related-list { margin-top: 8px; }
.related-list .sublabel { margin-top: 12px; font-size: .72rem; font-weight: 600; color: var(--soft); }
.cite.related { display: block; }
.cite.related .cite-head { flex-wrap: wrap; row-gap: 4px; }
