/* ═══ Tokens ═══ */
:root {
  --bg: #f5f1ea;
  --surface: #ffffff;
  --surface-2: #faf7f2;
  --text: #2a2420;
  --muted: #7a6f66;
  --border: #e3dbcf;
  --border-strong: #cfc4b4;

  --tw: #a85a24;        /* 台制：木色 */
  --tw-hover: #8f4c1e;
  --tw-soft: #f6e6d8;
  --us: #2d5d8c;        /* 英制：藍 */
  --us-hover: #244c73;
  --us-soft: #e1eaf4;

  --danger: #b83a2e;
  --danger-soft: #fbe9e7;
  --warn: #9a6a12;
  --warn-soft: #fbf1dc;

  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(60,40,20,.06), 0 4px 16px rgba(60,40,20,.06);
  --shadow-lg: 0 -8px 32px rgba(40,25,10,.18);
  --tap: 48px;
  --font: "Noto Sans TC", -apple-system, BlinkMacSystemFont, "PingFang TC", "Microsoft JhengHei", sans-serif;
}

/* 目前制度色（由 body[data-sys] 決定） */
body[data-sys="tw"] { --sys: var(--tw); --sys-hover: var(--tw-hover); --sys-soft: var(--tw-soft); }
body[data-sys="us"] { --sys: var(--us); --sys-hover: var(--us-hover); --sys-soft: var(--us-soft); }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, h3 { margin: 0; }
button { font: inherit; color: inherit; }
.muted { color: var(--muted); font-weight: 400; }
.small { font-size: 13px; }
[hidden] { display: none !important; }

/* ═══ Layout ═══ */
.layout { max-width: 1080px; margin: 0 auto; }
.calc { padding: 12px 16px 0; max-width: 560px; margin: 0 auto; }
.bottom-spacer { height: calc(88px + env(safe-area-inset-bottom)); }

.topbar { display: flex; align-items: center; justify-content: space-between; padding: 4px 0 12px; }
.topbar h1 { font-size: 20px; font-weight: 700; letter-spacing: .02em; }

/* ═══ Buttons ═══ */
.btn-primary, .btn-secondary, .btn-danger, .btn-ghost, .btn-icon, .link {
  cursor: pointer; border: 0; transition: background .15s, color .15s, border-color .15s, transform .05s, opacity .15s;
}
.btn-primary {
  width: 100%; min-height: 56px; margin-top: 14px;
  background: var(--sys); color: #fff; border-radius: var(--radius);
  font-size: 18px; font-weight: 700; letter-spacing: .04em;
  box-shadow: var(--shadow);
}
.btn-primary:hover:not(:disabled) { background: var(--sys-hover); }
.btn-primary:active:not(:disabled) { transform: scale(.985); }
.btn-primary:disabled { background: var(--border-strong); color: #fff; opacity: .7; cursor: not-allowed; box-shadow: none; }

.btn-secondary { min-height: var(--tap); padding: 0 20px; border-radius: var(--radius-sm); background: var(--surface-2); border: 1px solid var(--border); font-weight: 500; }
.btn-secondary:hover { background: #f1ebe2; }
.btn-secondary:active { transform: scale(.98); }

.btn-danger { min-height: var(--tap); padding: 0 20px; border-radius: var(--radius-sm); background: var(--danger); color: #fff; font-weight: 700; }
.btn-danger:hover { background: #9c2f25; }
.btn-danger:active { transform: scale(.98); }

.btn-ghost { min-height: 40px; padding: 0 12px; border-radius: 999px; background: transparent; border: 1px solid var(--border); color: var(--muted); font-size: 14px; }
.btn-ghost:hover:not(:disabled) { background: var(--surface); color: var(--text); border-color: var(--border-strong); }
.btn-ghost:active:not(:disabled) { transform: scale(.97); }
.btn-ghost:disabled { opacity: .4; cursor: not-allowed; }
.btn-ghost.danger:hover:not(:disabled) { color: var(--danger); border-color: var(--danger); background: var(--danger-soft); }

.btn-icon { width: 44px; height: 44px; border-radius: 50%; background: transparent; color: var(--muted); font-size: 18px; display: inline-flex; align-items: center; justify-content: center; }
.btn-icon:hover { background: var(--surface-2); color: var(--text); }
.btn-icon:active { transform: scale(.94); }

.link { background: none; padding: 8px 0; color: var(--sys); font-size: 14px; font-weight: 500; }
.link:hover { text-decoration: underline; }
.link:active { opacity: .7; }
.link:disabled { color: var(--border-strong); cursor: not-allowed; text-decoration: none; }

/* ═══ 制度切換 ═══ */
.seg {
  display: grid; grid-template-columns: 1fr 1fr; gap: 6px;
  padding: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  box-shadow: var(--shadow);
}
.seg-btn {
  min-height: 60px; border: 2px solid transparent; border-radius: 12px; background: transparent; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0;
  transition: background .15s, color .15s, border-color .15s;
  color: var(--muted);
}
.seg-name { font-size: 18px; font-weight: 700; line-height: 1.2; }
.seg-unit { font-size: 13px; }
.seg-btn:hover { background: var(--surface-2); color: var(--text); }
.seg-btn:active { transform: scale(.98); }
.seg-btn[data-sys="tw"].on { background: var(--tw); color: #fff; }
.seg-btn[data-sys="us"].on { background: var(--us); color: #fff; }
.seg-btn.on:hover { filter: brightness(.95); }

/* ═══ Card ═══ */
.card { margin-top: 14px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card-hd { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.card-hd h2 { font-size: 16px; font-weight: 700; }
.card-hd h2 .muted { font-size: 13px; margin-left: 4px; }

/* Switch */
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; min-height: 44px; user-select: none; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch-track { width: 42px; height: 24px; border-radius: 999px; background: var(--border-strong); position: relative; transition: background .15s; }
.switch-thumb { position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.2); transition: transform .15s; }
.switch input:checked + .switch-track { background: var(--sys); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(18px); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--sys); outline-offset: 2px; }
.switch:hover .switch-track { filter: brightness(.95); }
.switch-label { font-size: 14px; color: var(--muted); }

/* Fields */
.dims { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 14px; }
.field label { display: block; font-size: 13px; font-weight: 500; color: var(--muted); margin-bottom: 4px; white-space: nowrap; }
.field input {
  width: 100%; min-height: 52px; padding: 0 12px;
  border: 1.5px solid var(--border); border-radius: var(--radius-sm); background: var(--surface-2);
  font: inherit; font-size: 20px; font-weight: 500; color: var(--text);
  transition: border-color .15s, background .15s, box-shadow .15s;
  font-variant-numeric: tabular-nums;
}
.field input::placeholder { color: #bdb2a5; font-weight: 400; font-size: 16px; }
.field input:hover { border-color: var(--border-strong); }
.field input:focus { outline: none; border-color: var(--sys); background: var(--surface); box-shadow: 0 0 0 3px var(--sys-soft); }
.field.err input { border-color: var(--danger); background: var(--danger-soft); }
.field.err input:focus { box-shadow: 0 0 0 3px var(--danger-soft); }
.field.warn input { border-color: var(--warn); }
.cm-eq { margin: 4px 0 0; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; min-height: 0; }
.cm-eq:empty { display: none; }
.msg { margin: 4px 0 0; min-height: 0; font-size: 12px; line-height: 1.35; }
.field.err .msg { color: var(--danger); }
.field.warn .msg { color: var(--warn); }

/* ═══ Result ═══ */
.result { border-color: var(--sys-soft); border-width: 2px; }
.result-main { display: flex; align-items: baseline; gap: 6px; margin-top: 2px; }
.vol { font-size: 44px; font-weight: 700; color: var(--sys); line-height: 1.1; font-variant-numeric: tabular-nums; word-break: break-all; }
.vol-unit { font-size: 20px; font-weight: 700; color: var(--sys); }
.calc-step + .calc-step { margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--border); }
.step-expr { font-size: 15px; color: var(--muted); font-variant-numeric: tabular-nums; word-break: break-word; }
.amount { margin-top: 2px; font-size: 40px; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; word-break: break-all; }
.result-ft { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 10px; padding-top: 8px; border-top: 1px dashed var(--border); }

/* ═══ Tags ═══ */
.tag { display: inline-block; font-size: 12px; font-weight: 700; padding: 1px 8px; border-radius: 999px; }
.tag.tw { background: var(--tw-soft); color: var(--tw); }
.tag.us { background: var(--us-soft); color: var(--us); }

/* ═══ Total bar（mobile） ═══ */
.totalbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  min-height: 64px; padding: 10px 20px calc(10px + env(safe-area-inset-bottom));
  background: var(--text); color: #fff; border: 0; cursor: pointer;
  box-shadow: var(--shadow-lg); font-size: 16px;
  transition: background .15s;
}
.totalbar:hover { background: #3a322c; }
.totalbar:active { background: #1f1a17; }
.tb-right { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.tb-arrow { font-size: 12px; margin-left: 4px; opacity: .7; }

/* ═══ List panel ═══ */
.list-panel {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  max-height: 85vh; overflow-y: auto;
  background: var(--surface); border-radius: 20px 20px 0 0; box-shadow: var(--shadow-lg);
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
  transform: translateY(105%); transition: transform .25s ease;
}
.list-panel.open { transform: translateY(0); }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--border-strong); margin: 4px auto 10px; }
.list-hd { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.list-hd h2 { font-size: 18px; }
.list-hd h2 .muted { font-size: 14px; margin-left: 4px; }
.list-actions { display: flex; align-items: center; gap: 4px; }

.items { list-style: none; margin: 0; padding: 0; }
.item { padding: 12px 0 10px; border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums; }
.it-top { display: flex; align-items: center; gap: 8px; min-width: 0; }
.tag-solid { flex: none; font-size: 13px; font-weight: 700; color: #fff; padding: 2px 10px; border-radius: 999px; }
.tag-solid.us { background: var(--us); }
.tag-solid.tw { background: var(--tw); }
.it-dims { color: var(--muted); font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.it-view { position: relative; flex: none; }
.it-view-lbl { display: inline-flex; align-items: center; min-height: 36px; padding: 0 6px; border-radius: 6px; font-size: 14px; font-weight: 500; color: var(--text); white-space: nowrap; transition: background .15s; }
.it-view select { position: absolute; inset: 0; width: 100%; opacity: 0; cursor: pointer; font-size: 16px; }
.it-view:hover .it-view-lbl { background: var(--surface-2); }
.it-view:active .it-view-lbl { background: var(--border); }
.it-view select:focus-visible + .it-view-lbl, .it-view:focus-within .it-view-lbl { outline: 2px solid var(--sys); }
.it-row { display: grid; grid-template-columns: minmax(0, 1fr) 52px 60px 76px 40px; align-items: center; gap: 6px; margin-top: 6px; }
.it-vol b { font-size: 22px; font-weight: 600; }
.it-vol .u { font-size: 14px; color: var(--muted); }
.it-qty {
  width: 100%; height: 40px; text-align: center; font: inherit; font-size: 17px;
  border: 1.5px solid var(--border); border-radius: 8px; background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
}
.it-qty:hover { border-color: var(--border-strong); }
.it-qty:focus { outline: none; border-color: var(--sys); box-shadow: 0 0 0 3px var(--sys-soft); }
.it-price { text-align: right; font-size: 16px; }
.it-sub { text-align: right; font-size: 17px; font-weight: 700; }
.item-del { width: 40px; height: 40px; border: 0; border-radius: 50%; background: transparent; color: var(--muted); cursor: pointer; font-size: 16px; transition: background .15s, color .15s; }
.item-del:hover { background: var(--danger-soft); color: var(--danger); }
.item-del:active { transform: scale(.92); }
.list-empty { text-align: center; color: var(--muted); padding: 32px 0; }

.totals { margin-top: 12px; padding: 14px; background: var(--surface-2); border-radius: var(--radius-sm); font-variant-numeric: tabular-nums; }
.totals .tr { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; padding: 3px 0; }
.totals .tr.sep { border-top: 1px dashed var(--border-strong); margin-top: 6px; padding-top: 9px; }
.totals .tr.grand { font-size: 20px; font-weight: 700; margin-top: 6px; padding-top: 10px; border-top: 1px solid var(--border-strong); }

/* ═══ Overlay / Formula panel ═══ */
.overlay { position: fixed; inset: 0; z-index: 30; background: rgba(30,20,10,.38); opacity: 0; pointer-events: none; transition: opacity .2s; }
.overlay.on { opacity: 1; pointer-events: auto; }

.formula-panel {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  max-height: 90vh; overflow-y: auto;
  background: var(--surface); border-radius: 20px 20px 0 0; box-shadow: var(--shadow-lg);
  padding: 8px 20px calc(24px + env(safe-area-inset-bottom));
  transform: translateY(105%); transition: transform .25s ease;
}
.formula-panel.open { transform: translateY(0); }
.fp-hd { display: flex; align-items: center; justify-content: space-between; }
.fp-hd h2 { font-size: 18px; }
.fp-tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 10px 0 4px; }
.fp-tab { min-height: 44px; border-radius: 10px; border: 1.5px solid var(--border); background: var(--surface-2); cursor: pointer; font-weight: 700; color: var(--muted); transition: background .15s, color .15s, border-color .15s; }
.fp-tab:hover { color: var(--text); border-color: var(--border-strong); }
.fp-tab:active { transform: scale(.98); }
.fp-tab[data-tab="tw"].on { background: var(--tw-soft); border-color: var(--tw); color: var(--tw); }
.fp-tab[data-tab="us"].on { background: var(--us-soft); border-color: var(--us); color: var(--us); }
.fp-body { display: none; }
.fp-body.on { display: block; }
.fp-body h3, .fp-common h3 { font-size: 14px; color: var(--muted); font-weight: 700; margin: 18px 0 6px; letter-spacing: .04em; }
.fp-body p, .fp-common p { margin: 4px 0; }
.fp-body ul { margin: 4px 0; padding-left: 20px; }
.big-formula { font-size: 18px; font-weight: 700; }
.fp-body[data-body="tw"] .big-formula { color: var(--tw); }
.fp-body[data-body="us"] .big-formula { color: var(--us); }
.example { background: var(--surface-2); padding: 8px 10px; border-radius: 8px; }
.note { margin-top: 12px; padding: 10px 12px; background: var(--warn-soft); border-radius: 8px; font-size: 14px; }
.conv { width: 100%; border-collapse: collapse; font-size: 14px; }
.conv td { padding: 7px 0; border-bottom: 1px solid var(--border); vertical-align: top; }
.conv td:first-child { color: var(--muted); width: 38%; padding-right: 8px; }
.fp-common { margin-top: 8px; }
.fp-common .small { margin-top: 12px; }

/* ═══ Dialog ═══ */
.dialog-wrap { position: fixed; inset: 0; z-index: 70; background: rgba(30,20,10,.45); display: flex; align-items: center; justify-content: center; padding: 16px; }
.dialog { width: 100%; max-width: 360px; background: var(--surface); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow-lg); }
.dialog h3 { font-size: 18px; margin-bottom: 6px; }
.dialog-btns { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

/* ═══ Toast ═══ */
.toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); z-index: 60;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  display: flex; align-items: center; gap: 14px;
  max-width: calc(100vw - 32px); padding: 12px 16px; border-radius: 12px;
  background: #2a2420; color: #fff; font-size: 14px; box-shadow: var(--shadow-lg);
  transition: opacity .2s, transform .2s;
}
.toast.on { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast-undo { border: 0; background: transparent; color: #f4c27a; font-weight: 700; cursor: pointer; padding: 6px 4px; min-height: 36px; }
.toast-undo:hover { text-decoration: underline; }
.toast-undo:active { opacity: .7; }

/* ═══ Desktop：P1 + P2 並排 ═══ */
@media (min-width: 900px) {
  .layout { display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 24px; padding: 24px; align-items: start; }
  .calc { padding: 0; margin: 0; max-width: none; }
  .bottom-spacer { height: 24px; }
  .totalbar { display: none; }
  .list-panel {
    position: sticky; top: 24px; transform: none; max-height: calc(100vh - 48px);
    border-radius: var(--radius); border: 1px solid var(--border); box-shadow: var(--shadow); padding: 16px;
  }
  .list-panel .sheet-handle, .only-mobile { display: none; }
  .formula-panel { left: 50%; right: auto; bottom: auto; top: 50%; width: 560px; max-height: 86vh; border-radius: 20px; transform: translate(-50%, -46%); opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; }
  .formula-panel.open { transform: translate(-50%, -50%); opacity: 1; pointer-events: auto; }
  .formula-panel .sheet-handle { display: none; }
  .toast { bottom: 24px; }
}

@media (max-width: 360px) {
  .field input { font-size: 18px; padding: 0 8px; }
  .field label { font-size: 12px; }
  .vol { font-size: 38px; }
}
