/* 萬用計算機 — 手機優先樣式
   重點:輸入框字級 ≥16px(避免 iOS 聚焦時自動放大)、觸控目標 ≥44px、
   安全區域(瀏海/Home 指示條)、深色模式、離線可用(無外部資源) */

:root {
  color-scheme: light dark;
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface2: #f6f8fb;
  --field: #ffffff;
  --text: #16202c;
  --muted: #566274;
  --line: #d6dde8;
  --primary: #0b5cad;
  --primary-text: #0b5cad;
  --on-primary: #ffffff;
  --seg-bg: #e6ebf3;
  --bar: #0b4a8f;
  --ok: #0d7a43;
  --warn: #8a5a00;
  --bad: #b3261e;
  --focus: rgba(11, 92, 173, 0.28);
  --backdrop: rgba(10, 15, 25, 0.5);
  --shadow: 0 8px 28px rgba(10, 20, 40, 0.18);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --surface: #161b22;
    --surface2: #1b222b;
    --field: #0f141b;
    --text: #e6edf3;
    --muted: #9aa7b6;
    --line: #2b3441;
    --primary: #1f6fc2;
    --primary-text: #7ab8ff;
    --seg-bg: #232b36;
    --bar: #0a2a4d;
    --ok: #4cd08a;
    --warn: #f0b95a;
    --bad: #ff8a80;
    --focus: rgba(122, 184, 255, 0.35);
    --backdrop: rgba(0, 0, 0, 0.62);
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--bg);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

body.modal-open { overflow: hidden; }

[hidden] { display: none !important; }

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }

.sr {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.ico {
  width: 24px; height: 24px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- 上方列 ---------- */
.appbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--bar);
  color: #fff;
  padding: var(--safe-t) var(--safe-r) 0 var(--safe-l);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
}
.appbar-in {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 56px;
  padding: 0 6px 0 4px;
  max-width: 640px;
  margin: 0 auto;
}
.bar-switch {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  height: 48px;
  padding: 0 10px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: inherit;
  text-align: left;
}
.bar-switch:active { background: rgba(255, 255, 255, 0.16); }
.bar-switch .chev { width: 20px; height: 20px; margin-left: auto; opacity: 0.85; }
.bar-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.2; }
.bar-app { font-size: 12px; opacity: 0.8; }
.bar-name { font-size: 17px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.icon-btn {
  width: 44px; height: 44px;
  flex: none;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: inherit;
}
.icon-btn:active { background: rgba(255, 255, 255, 0.16); }
.appbar .icon-btn[aria-pressed="false"] { opacity: 0.55; }
.icon-btn.small { width: 44px; height: 44px; color: var(--muted); }
.icon-btn.small:active { background: var(--seg-bg); }

button:focus-visible, select:focus-visible, summary:focus-visible, a:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}
.appbar button:focus-visible { outline-color: rgba(255, 255, 255, 0.7); }

/* 迷你結果列:結果卡片不在畫面內時(例如鍵盤打開),固定顯示目前的結果 */
.mini {
  /* 用 fixed(不佔版面):出現/消失時不會把下方內容推動,否則點擊瞬間版面位移會造成點不到按鈕 */
  position: fixed;
  left: 0;
  right: 0;
  top: calc(56px + var(--safe-t));
  z-index: 25;
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-height: 40px;
  padding: 6px max(16px, var(--safe-r)) 6px max(16px, var(--safe-l));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 3px 10px rgba(10, 20, 40, 0.1);
}
.mini-label { flex: none; color: var(--muted); font-size: 13.5px; font-weight: 650; white-space: nowrap; }
.mini-val {
  min-width: 0;
  color: var(--ok);
  font-size: 20px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mini-unit { flex: none; color: var(--muted); font-size: 15px; font-weight: 700; white-space: nowrap; }
@media (max-height: 460px) and (orientation: landscape) { .mini { top: calc(48px + var(--safe-t)); } }
.field { scroll-margin-top: calc(var(--safe-t) + 130px); }

/* ---------- 主要內容 ---------- */
.main {
  max-width: 640px;
  margin: 0 auto;
  padding: 14px max(16px, var(--safe-r)) calc(32px + var(--safe-b)) max(16px, var(--safe-l));
}
body.kb .main { padding-bottom: 45vh; }

.tip {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 8px 12px 14px;
  margin-bottom: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 4px solid var(--primary);
  border-radius: 12px;
  font-size: 14.5px;
}
.tip p { margin: 0; flex: 1; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 16px;
  margin: 0 0 14px;
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 0 0 12px;
}
.card-head h2 {
  margin: 0;
  font-size: 18px;
  line-height: 1.35;
  color: var(--primary-text);
}
.ghost-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  min-height: 44px;
  padding: 0 10px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-size: 14.5px;
}
.ghost-btn:active { background: var(--seg-bg); }
.ghost-btn .ico { width: 18px; height: 18px; }

.group-title {
  margin: 22px 2px 4px;
  font-size: 18px;
  line-height: 1.4;
  color: var(--primary-text);
}
.group-title:first-child { margin-top: 4px; }
.formula {
  margin: 0 2px 12px;
  color: var(--muted);
  font-size: 13.5px;
}
.formula span { display: block; }

.note, .hint {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13.5px;
  line-height: 1.55;
  white-space: pre-line;
}
body.no-hints .hint { display: none; }

/* ---------- 欄位 ---------- */
.fields { display: block; }
.field { margin: 0 0 16px; }
.field-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 0 0 6px;
  min-height: 24px;
}
.field-head label { font-weight: 650; font-size: 15.5px; }
.link-btn {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  min-height: 44px;
  margin: -10px -8px -10px 0;
  padding: 0 8px 0 10px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--primary-text);
  font-size: 14.5px;
  font-weight: 600;
}
.link-btn .ico { width: 16px; height: 16px; }
.link-btn:active { background: var(--seg-bg); }

.num-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: stretch; }
.inp {
  flex: 1 1 112px;
  min-width: 0;
  display: flex;
  align-items: center;
  min-height: 50px;
  padding: 0 14px;
  background: var(--field);
  border: 1.5px solid var(--line);
  border-radius: 12px;
}
.inp:focus-within { border-color: var(--primary); box-shadow: 0 0 0 4px var(--focus); }
.inp input {
  flex: 1;
  min-width: 0;
  width: 100%;
  padding: 12px 0;
  border: 0;
  outline: 0;
  background: transparent;
  font-size: 18px;
  line-height: 1.3;
  font-variant-numeric: tabular-nums;
  -webkit-appearance: none;
  appearance: none;
  border-radius: 0;
}
.inp input::placeholder { color: var(--muted); opacity: 0.7; }
.unit { margin-left: 8px; color: var(--muted); font-size: 15px; white-space: nowrap; }

.sign-btn {
  flex: none;
  min-width: 50px;
  min-height: 50px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: var(--surface2);
  color: var(--primary-text);
  font-size: 22px;
  font-weight: 700;
}
.sign-btn:active { background: var(--seg-bg); }

.sel {
  position: relative;
  min-width: 0;
  background: var(--field);
  border: 1.5px solid var(--line);
  border-radius: 12px;
}
.sel:focus-within { border-color: var(--primary); box-shadow: 0 0 0 4px var(--focus); }
.sel::after {
  content: "";
  position: absolute;
  right: 18px;
  top: 50%;
  width: 8px; height: 8px;
  margin-top: -7px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg);
  pointer-events: none;
}
.sel select {
  display: block;
  width: 100%;
  min-height: 50px;
  padding: 10px 40px 10px 14px;
  border: 0;
  outline: 0;
  border-radius: 12px;
  background: transparent;
  font-size: 17px;
  line-height: 1.35;
  text-overflow: ellipsis;
  -webkit-appearance: none;
  appearance: none;
}
.sel select option { color: #16202c; background: #fff; }
@media (prefers-color-scheme: dark) { .sel select option { color: #e6edf3; background: #161b22; } }

.seg {
  display: inline-flex;
  flex: none;
  gap: 2px;
  padding: 3px;
  background: var(--seg-bg);
  border-radius: 12px;
}
.seg-btn {
  min-width: 44px;
  min-height: 44px;
  padding: 0 12px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  font-size: 15px;
  font-weight: 650;
  color: var(--text);
  white-space: nowrap;
}
.seg-btn[aria-checked="true"] { background: var(--primary); color: var(--on-primary); }

/* ---------- 按鈕 ---------- */
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 52px;
  padding: 0 16px;
  border: 0;
  border-radius: 14px;
  font-size: 17px;
  font-weight: 700;
}
.btn.primary { background: var(--primary); color: var(--on-primary); }
.btn.secondary { background: var(--surface2); color: var(--primary-text); border: 1.5px solid var(--line); min-height: 48px; font-size: 16px; }
.btn:active { transform: scale(0.985); filter: brightness(0.94); }

.err {
  margin: 10px 0 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(179, 38, 30, 0.1);
  color: var(--bad);
  font-size: 15px;
  font-weight: 600;
}

/* ---------- 結果 ---------- */
.result {
  margin-top: 16px;
  padding: 14px 16px 16px;
  background: var(--surface2);
  border: 1px solid var(--line);
  border-radius: 14px;
  scroll-margin-top: 76px;
  scroll-margin-bottom: 24px;
}
.res-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 28px; }
.res-head .icon-btn { margin: -8px -8px -8px 0; }
.res-label { color: var(--muted); font-size: 14px; font-weight: 650; }
.res-main { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; margin-top: 2px; }
.res-main .seg { margin-left: auto; max-width: 100%; }
@media (max-width: 350px) { .seg-btn { padding: 0 9px; font-size: 14.5px; } }
.res-value {
  flex: 0 1 auto;
  min-width: 0;
  font-size: 38px;
  line-height: 1.2;
  font-weight: 800;
  letter-spacing: -0.5px;
  color: var(--ok);
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}
.res-value.m { font-size: 32px; }
.res-value.s { font-size: 26px; }
.res-value.xs { font-size: 21px; }
.res-value.empty { color: var(--muted); font-weight: 600; }
.res-unit { font-size: 20px; font-weight: 700; color: var(--muted); }
.res-detail { margin: 8px 0 0; color: var(--muted); font-size: 14px; line-height: 1.6; }
.res-detail span { display: block; }
.verdict { margin: 10px 0 0; padding: 10px 12px; border-radius: 10px; font-size: 14.5px; font-weight: 600; background: var(--seg-bg); }
.verdict.ok { color: var(--ok); }
.verdict.warn { color: var(--warn); }
.verdict.bad { color: var(--bad); }

.rows { margin: 8px 0 0; }
.rows .row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 0;
  border-top: 1px solid var(--line);
}
.rows .row:first-child { border-top: 0; }
.rows dt { color: var(--muted); font-size: 14.5px; }
.rows dd {
  margin: 0;
  min-width: 0;
  text-align: right;
  font-size: 19px;
  font-weight: 800;
  color: var(--ok);
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}
.rows dd.empty { color: var(--muted); font-weight: 600; }

/* ---------- 內嵌輔助區、折疊區 ---------- */
.helper {
  margin: 0 0 4px;
  padding: 14px;
  background: var(--surface2);
  border: 1px dashed var(--line);
  border-radius: 14px;
}
.helper h3 { margin: 0; font-size: 15.5px; line-height: 1.45; color: var(--primary-text); }
.helper .fields { margin-top: 12px; }
.helper .field { margin-bottom: 12px; }

.acc { padding: 0; overflow: hidden; }
.acc summary {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding: 12px 16px;
  list-style: none;
  cursor: pointer;
  font-size: 16.5px;
  font-weight: 700;
  color: var(--primary-text);
}
.acc summary::-webkit-details-marker { display: none; }
.acc summary .ico { margin-left: auto; transition: transform 0.2s; }
.acc[open] summary .ico { transform: rotate(180deg); }
.acc-body { padding: 0 16px 16px; border-top: 1px solid var(--line); padding-top: 14px; }

/* ---------- 單位換算 ---------- */
.pair { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; align-items: end; }
.pair .pc label { display: block; margin: 0 0 6px; font-weight: 650; font-size: 15.5px; }
.swap-btn {
  width: 48px; height: 50px;
  display: grid; place-items: center;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: var(--surface2);
  color: var(--primary-text);
}
.swap-btn:active { background: var(--seg-bg); }
.pair.stack { grid-template-columns: 1fr; gap: 10px; }
.pair.stack .swap-btn { width: 100%; height: 44px; }
.pair.stack .swap-btn .ico { transform: rotate(90deg); }

/* 整列寬度的分段選擇(例如氣缸的「推出／拉回」) */
.seg.block { display: flex; width: 100%; }
.seg.block .seg-btn { flex: 1; }

/* ---------- 底部彈出面板(參考表、切換計算機) ---------- */
.sheet-root {
  position: fixed;
  left: 0; right: 0; top: 0; bottom: 0;
  z-index: 60;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet-backdrop { position: absolute; left: 0; right: 0; top: 0; bottom: 0; background: var(--backdrop); touch-action: none; animation: fade 0.18s ease-out; }
.sheet {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 640px;
  max-height: 88vh;
  max-height: 88dvh;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  box-shadow: var(--shadow);
  animation: rise 0.22s ease-out;
}
.sheet-grab { width: 40px; height: 4px; margin: 8px auto 0; border-radius: 4px; background: var(--line); flex: none; }
.sheet-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px 8px 20px;
  border-bottom: 1px solid var(--line);
}
.sheet-head h2 { flex: 1; margin: 0; font-size: 18px; line-height: 1.35; color: var(--primary-text); }
.sheet-head .icon-btn { color: var(--muted); }
.sheet-head .icon-btn:active { background: var(--seg-bg); }
.sheet-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 14px 20px calc(24px + var(--safe-b));
}
@keyframes rise { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .sheet, .sheet-backdrop { animation: none; }
  .acc summary .ico { transition: none; }
}

.menu-list { list-style: none; margin: 0; padding: 0; }
.menu-item {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  min-height: 64px;
  padding: 10px 8px;
  border: 0;
  border-radius: 14px;
  background: transparent;
  text-align: left;
}
.menu-item:active { background: var(--seg-bg); }
.menu-item[aria-current="true"] { background: var(--seg-bg); }
.menu-ico {
  flex: none;
  display: grid;
  place-items: center;
  width: 44px; height: 44px;
  border-radius: 12px;
  background: var(--primary);
  color: var(--on-primary);
}
.menu-txt { display: flex; flex-direction: column; min-width: 0; line-height: 1.35; }
.menu-txt strong { font-size: 16.5px; }
.menu-txt span { color: var(--muted); font-size: 13.5px; }
.menu-item .check { margin-left: auto; color: var(--primary-text); }

.ref-table { width: 100%; border-collapse: collapse; margin-top: 12px; font-size: 15px; }
.ref-table th, .ref-table td { padding: 4px 6px; border-bottom: 1px solid var(--line); text-align: center; }
.ref-table thead th { color: var(--muted); font-size: 13.5px; font-weight: 600; background: var(--surface2); padding: 8px 4px; }
.ref-table tbody th { font-weight: 650; }
.ref-table td.dash { color: var(--muted); }
.pick {
  min-width: 64px;
  min-height: 44px;
  padding: 0 8px;
  border: 1.5px solid var(--line);
  border-radius: 10px;
  background: var(--field);
  color: var(--primary-text);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.pick:active { background: var(--seg-bg); }

.rod-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 12px; }
.pick.rod { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-height: 64px; }
.rod-bore { font-size: 16.5px; }
.rod-rod { font-size: 13.5px; color: var(--muted); font-weight: 600; }

.opt-list { display: grid; gap: 10px; margin-top: 12px; }
.opt {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  min-height: 64px;
  padding: 10px 14px;
  border: 1.5px solid var(--line);
  border-radius: 12px;
  background: var(--surface2);
  text-align: left;
}
.opt:active { background: var(--seg-bg); }
.opt span { color: var(--muted); font-size: 14px; }

/* ---------- 提示訊息 ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-b));
  z-index: 80;
  max-width: min(92vw, 520px);
  padding: 12px 18px;
  border-radius: 14px;
  background: rgba(22, 28, 38, 0.94);
  color: #fff;
  font-size: 15px;
  line-height: 1.45;
  text-align: center;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translate(-50%, 12px);
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (prefers-reduced-motion: reduce) { .toast { transition: none; } }

/* ---------- 較大螢幕(平板、橫放)---------- */
@media (min-width: 700px) {
  .main { padding-top: 22px; }
  .card { padding: 22px; }
  .res-value { font-size: 44px; }
  .sheet { border-radius: 22px; margin-bottom: 4vh; }
  .sheet-root { align-items: center; }
}
@media (max-height: 460px) and (orientation: landscape) {
  .appbar-in { height: 48px; }
  .sheet { max-height: 94vh; max-height: 94dvh; }
}
@media print {
  .appbar, .toast, .tip { display: none; }
}
