:root {
  --bg: #f5f3ef; --card: #fff; --ink: #1f2a24; --muted: #6b746f; --line: #e3e0d8;
  --brand: #2f5d50; --brand-2: #e8f0ec; --accent: #b8863b; --warn: #b54a3a; --warn-bg: #fbecea; --ok: #2f7d4f;
  --radius: 10px;
  font-family: -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.55; }
a { color: var(--brand); }
.top { position: sticky; top: 0; z-index: 20; display: flex; gap: 16px; align-items: center; padding: calc(10px + env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) 10px max(20px, env(safe-area-inset-left)); background: var(--brand); color: #fff; flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 18px; letter-spacing: .5px; white-space: nowrap; }
.search { position: relative; flex: 1; min-width: 220px; max-width: 520px; }
.search input { width: 100%; padding: 8px 12px; border-radius: 8px; border: 0; font-size: 15px; }
.qdrop { position: absolute; top: 40px; left: 0; right: 0; background: #fff; color: var(--ink); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.18); overflow: hidden; }
.qdrop a { display: block; padding: 8px 12px; text-decoration: none; color: inherit; border-bottom: 1px solid var(--line); }
.qdrop a:hover, .qdrop a.on { background: var(--brand-2); }
.qdrop small { color: var(--muted); margin-left: 6px; }
nav { display: flex; gap: 4px; flex-wrap: wrap; }
nav a { color: #e6efe9; text-decoration: none; padding: 6px 12px; border-radius: 6px; }
nav a.on, nav a:hover { background: rgba(255,255,255,.16); color: #fff; }
.banner { background: #fff6e0; border-bottom: 1px solid #ecd9a8; padding: 10px 20px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
main { max-width: 1280px; margin: 0 auto; padding: 20px; }
h2 { margin: 0 0 4px; font-size: 22px; }
h3 { margin: 0 0 12px; font-size: 16px; color: var(--brand); }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; margin-bottom: 16px; }
.muted { color: var(--muted); }
.btn { display: inline-flex; align-items: center; gap: 4px; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--brand); background: var(--brand); color: #fff; font-size: 14px; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { filter: brightness(1.08); }
.btn.ghost { background: #fff; color: var(--brand); }
.btn.warn { background: #fff; color: var(--warn); border-color: var(--warn); }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.bar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.spacer { flex: 1; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px 16px; }
.f { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.f.wide { grid-column: 1 / -1; }
.f > span { font-weight: 600; }
.req { color: var(--warn); }
input, select, textarea { font: inherit; color: var(--ink); padding: 7px 9px; border: 1px solid var(--line); border-radius: 7px; background: #fff; width: 100%; }
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #9cc3b4; border-color: var(--brand); }
.radios { display: flex; gap: 14px; padding: 6px 0; color: var(--ink); }
.radios label, .check { display: inline-flex; gap: 5px; align-items: center; color: var(--ink); font-size: 14px; }
.radios input, .check input { width: auto; }
.hint { font-size: 12px; color: var(--muted); }
.hint.bad { color: var(--warn); }
.rows { display: flex; flex-direction: column; gap: 6px; margin-bottom: 6px; }
.row { display: flex; gap: 6px; align-items: flex-start; }
.row select { width: 110px; flex: none; }
.row input.rocd { width: 150px; flex: none; }
.row .x { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; padding: 6px; }
.row .x:hover { color: var(--warn); }
.errors { background: var(--warn-bg); color: var(--warn); border-radius: 8px; padding: 10px 14px; margin-bottom: 12px; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; background: var(--brand-2); color: var(--brand); margin: 0 4px 4px 0; white-space: nowrap; }
.badge.gold { background: #f6ecd9; color: #8a6224; }
.badge.gray { background: #eceae5; color: #6b746f; }
.badge.red { background: var(--warn-bg); color: var(--warn); }

/* list */
.filters { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px 10px; }
.filters .f { font-size: 12px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 3px 12px; cursor: pointer; font-size: 13px; }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.tablewrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; background: #fff; }
th, td { padding: 9px 10px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; font-size: 14px; }
th { background: #faf9f6; font-size: 13px; color: var(--muted); white-space: nowrap; position: sticky; top: 0; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: #f4f8f6; }
td.name { font-weight: 600; white-space: nowrap; }
td .clip { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; max-width: 260px; }

/* detail */
.detail { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); gap: 16px; align-items: start; }
@media (max-width: 900px) { .detail, .import { grid-template-columns: 1fr !important; } }
dl { display: grid; grid-template-columns: 110px 1fr; gap: 6px 12px; margin: 0; }
dt { color: var(--muted); font-size: 13px; }
dd { margin: 0; white-space: pre-wrap; word-break: break-word; }
dd.empty { color: #c2c0b9; }
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { border-left: 3px solid var(--brand-2); padding: 4px 0 14px 14px; position: relative; }
.timeline li::before { content: ""; position: absolute; left: -7px; top: 9px; width: 11px; height: 11px; border-radius: 50%; background: var(--brand); }
.timeline .when { font-weight: 600; color: var(--brand); font-size: 14px; }
.timeline .body { white-space: pre-wrap; word-break: break-word; margin-top: 2px; }
.timeline .ops { float: right; display: flex; gap: 4px; }
.visit-new { border: 2px solid var(--brand); }
.sugg { margin-top: 12px; border: 1px dashed var(--accent); background: #fffaf1; border-radius: 8px; padding: 10px 12px; }
.sugg h4 { margin: 0 0 8px; color: #8a6224; font-size: 14px; }
.sugg .s { display: grid; grid-template-columns: 22px 110px 1fr; gap: 6px; align-items: start; padding: 5px 0; border-top: 1px solid #f0e4cc; }
.sugg .s:first-of-type { border-top: 0; }
.sugg .old { font-size: 12px; color: var(--muted); }
.thumbs { display: flex; gap: 8px; flex-wrap: wrap; }
.thumbs a { display: block; width: 96px; height: 96px; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); background: #faf9f6; font-size: 12px; text-align: center; color: var(--muted); }
.thumbs img { width: 100%; height: 100%; object-fit: cover; }
details summary { cursor: pointer; color: var(--muted); }
pre.raw { white-space: pre-wrap; font-size: 13px; background: #faf9f6; padding: 10px; border-radius: 8px; max-height: 360px; overflow: auto; }

/* import */
.drop { border: 2px dashed #b9c9c1; border-radius: var(--radius); padding: 26px; text-align: center; color: var(--muted); cursor: pointer; background: #fbfcfb; }
.drop.over { background: var(--brand-2); border-color: var(--brand); }
.import { display: grid; grid-template-columns: 280px minmax(0, 1fr); gap: 16px; align-items: start; }
.queue { max-height: 75vh; overflow: auto; padding: 0; }
.queue .qi { display: flex; justify-content: space-between; gap: 8px; padding: 9px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.queue .qi.on { background: var(--brand-2); }
.queue .qi small { color: var(--muted); display: block; font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 170px; }
.confirmbar { position: sticky; bottom: 0; background: rgba(245,243,239,.96); padding: 12px 0; display: flex; gap: 8px; flex-wrap: wrap; border-top: 1px solid var(--line); }
.notice { background: #eef5f1; border-left: 4px solid var(--brand); padding: 10px 14px; border-radius: 6px; margin-bottom: 12px; }
.notice.warn { background: var(--warn-bg); border-color: var(--warn); }
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); background: #1f2a24; color: #fff; padding: 10px 18px; border-radius: 8px; z-index: 50; box-shadow: 0 6px 20px rgba(0,0,0,.25); }
.spin { display: inline-block; width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: sp .8s linear infinite; vertical-align: -2px; }
@keyframes sp { to { transform: rotate(360deg); } }

/* 同步按鈕、橫幅 */
.syncbtn { border: 1px solid rgba(255,255,255,.35); background: rgba(255,255,255,.1); color: #fff; border-radius: 999px; padding: 5px 12px; font-size: 13px; cursor: pointer; white-space: nowrap; }
.syncbtn.warn { background: #fff6e0; color: #8a6224; border-color: #fff6e0; }
.banner .x { border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 16px; }
.fsum { font-weight: 600; color: var(--brand); margin-bottom: 8px; }
details[open] > .fsum { margin-bottom: 10px; }
main { padding-bottom: calc(20px + env(safe-area-inset-bottom)); }

/* 平板 */
@media (max-width: 900px) {
  .import { grid-template-columns: 1fr; }
  .queue { max-height: 32vh; }
}
/* 手機 */
@media (max-width: 700px) {
  body { font-size: 15px; }
  input, select, textarea { font-size: 16px; }   /* iPhone 輸入時不自動放大 */
  main { padding: 12px max(14px, env(safe-area-inset-left)); }
  .top { gap: 8px; padding-left: 14px; padding-right: 14px; }
  .brand { font-size: 16px; flex: 1; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  nav { order: 4; width: 100%; justify-content: space-between; }
  nav a { padding: 6px 8px; font-size: 14px; }
  h2 { font-size: 19px; }
  .card { padding: 14px; }
  dl { grid-template-columns: 88px 1fr; }
  .grid { grid-template-columns: 1fr; }
  .filters { grid-template-columns: 1fr 1fr; }
  .filters .f[style] { grid-column: 1 / -1 !important; }
  /* 客戶列表改成卡片 */
  thead { display: none; }
  table, tbody { display: block; }
  tbody tr { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
  tbody td { display: block; border: 0; padding: 0; font-size: 13px; }
  tbody td.name { font-size: 16px; }
  tbody td:nth-child(3), tbody td:nth-child(5), tbody td:nth-child(8) { display: none; }
  tbody td:nth-child(2), tbody td:nth-child(6) { text-align: right; }
  tbody td[colspan] { grid-column: 1 / -1; }
  .row { flex-wrap: wrap; }
  .row input.rocd { width: calc(100% - 40px); }
  .row select { width: 100%; }
  .row textarea, .row input[type=text] { flex: 1 1 100%; }
  .sugg .s { grid-template-columns: 22px 1fr; }
  .sugg .s > div:last-child { grid-column: 2; }
  .confirmbar { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .timeline .ops { float: none; margin-bottom: 4px; }
}


/* 保單週年日清單 */
.plist { list-style: none; padding: 0; margin: 0; }
.plist li { padding: 8px 0; border-top: 1px solid var(--line); }
.plist li:first-child { border-top: 0; padding-top: 0; }
.row input[data-k="company"], .row input[data-k="number"] { max-width: 160px; }
@media (max-width: 700px) { .row input[data-k="company"], .row input[data-k="number"] { max-width: none; flex: 1 1 40%; } }

/* 民國年日期選擇器 */
input.rocd { cursor: pointer; }
input.rocd.bad { border-color: var(--warn); background: var(--warn-bg); }
.rocpop { position: fixed; z-index: 60; width: 272px; background: #fff; border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 10px 28px rgba(0,0,0,.18); padding: 10px; font-size: 14px; }
.rocpop button { border: 0; background: none; font: inherit; color: var(--ink); cursor: pointer; border-radius: 6px; }
.rp-head { display: flex; align-items: center; gap: 2px; margin-bottom: 6px; }
.rp-head b { flex: 1; text-align: center; color: var(--brand); }
.rp-head button { width: 30px; height: 30px; font-size: 16px; color: var(--brand); }
.rp-head button:hover, .rp-grid button:hover, .rp-foot button:hover { background: var(--brand-2); }
.rp-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; text-align: center; }
.rp-grid i { font-style: normal; font-size: 12px; color: var(--muted); padding: 4px 0; }
.rp-grid button { height: 34px; }
.rp-grid button.today { outline: 1px solid var(--accent); }
.rp-grid button.sel { background: var(--brand); color: #fff; }
.rp-foot { display: flex; justify-content: space-between; margin-top: 8px; border-top: 1px solid var(--line); padding-top: 6px; }
.rp-foot button { padding: 6px 10px; color: var(--brand); }
