:root {
  --ink: #17312f;
  --muted: #6c7e7b;
  --paper: #f7f5ef;
  --card: rgba(255, 255, 255, .88);
  --brand: #123d3a;
  --brand-2: #1f625c;
  --accent: #e9a86c;
  --line: #dce4df;
  --danger: #b9473d;
  --shadow: 0 18px 55px rgba(22, 55, 51, .11);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { min-height: 100%; background: var(--paper); }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 90% -10%, rgba(233, 168, 108, .22), transparent 32rem),
    linear-gradient(180deg, #f8f6f0 0%, #eef3ef 100%);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", sans-serif;
}
button, input, select, textarea { font: inherit; }
button, label.action-card { -webkit-tap-highlight-color: transparent; }
button { cursor: pointer; }

.app-shell { width: min(1180px, 100%); margin: auto; padding: 0 24px 80px; }
.topbar {
  min-height: 86px; display: flex; align-items: center; justify-content: space-between;
  padding-top: env(safe-area-inset-top);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo { width: 48px; height: 48px; border-radius: 14px; box-shadow: 0 8px 20px rgba(18,61,58,.18); }
.brand h1 { font-size: 20px; margin: 1px 0 0; letter-spacing: .02em; }
.eyebrow { margin: 0; font-size: 10px; letter-spacing: .18em; font-weight: 800; color: var(--brand-2); }

.hero {
  overflow: hidden; position: relative; min-height: 220px; border-radius: 30px; padding: 36px;
  display: flex; justify-content: space-between; align-items: flex-end; gap: 20px;
  color: white; background: linear-gradient(135deg, #123d3a, #1f625c);
  box-shadow: var(--shadow);
}
.hero::after {
  content: ""; position: absolute; width: 320px; height: 320px; right: -70px; top: -170px;
  border: 1px solid rgba(255,255,255,.22); border-radius: 50%;
  box-shadow: 0 0 0 45px rgba(255,255,255,.035), 0 0 0 90px rgba(255,255,255,.025);
}
.hero-kicker { color: #cbe0da; font-weight: 700; margin: 0 0 12px; }
.hero h2 { position: relative; z-index: 1; margin: 0; max-width: 610px; font-size: clamp(28px, 5vw, 48px); line-height: 1.15; letter-spacing: -.035em; }
.hero-stats { position: relative; z-index: 1; display: flex; gap: 12px; }
.hero-stats div {
  min-width: 112px; padding: 16px; border-radius: 18px;
  background: rgba(255,255,255,.12); backdrop-filter: blur(12px);
}
.hero-stats strong { display: block; font-size: 28px; }
.hero-stats span { font-size: 12px; color: #cbe0da; }

.action-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin: 20px 0 30px; }
.action-card {
  min-height: 96px; border: 1px solid rgba(18,61,58,.08); border-radius: 20px; padding: 18px;
  display: flex; align-items: center; gap: 14px; text-align: left; color: var(--ink);
  background: var(--card); box-shadow: 0 8px 30px rgba(22,55,51,.06);
  transition: transform .18s ease, box-shadow .18s ease, border-color .18s ease;
}
.action-card:hover { transform: translateY(-2px); box-shadow: 0 14px 35px rgba(22,55,51,.1); border-color: rgba(31,98,92,.3); }
.scan-card { color: white; background: linear-gradient(135deg, #d78e50, #ecb37d); }
.action-icon {
  flex: 0 0 42px; height: 42px; border-radius: 13px; display: grid; place-items: center;
  background: rgba(18,61,58,.08); font-size: 26px; font-weight: 300;
}
.scan-card .action-icon { background: rgba(255,255,255,.19); }
.action-card strong, .action-card small { display: block; }
.action-card strong { font-size: 15px; margin-bottom: 4px; }
.action-card small { color: var(--muted); font-size: 11px; line-height: 1.35; }
.scan-card small { color: rgba(255,255,255,.8); }

.toolbar { display: flex; gap: 14px; align-items: center; margin-bottom: 34px; }
.search-wrap {
  flex: 1; display: flex; align-items: center; gap: 10px; height: 48px; padding: 0 15px;
  background: white; border: 1px solid var(--line); border-radius: 15px;
}
.search-wrap span { font-size: 24px; color: var(--muted); transform: rotate(-15deg); }
.search-wrap input { width: 100%; border: 0; outline: 0; background: transparent; color: var(--ink); }
.filter-row { display: flex; gap: 8px; }
select, .secondary-btn, .primary-btn, .icon-btn {
  min-height: 44px; border-radius: 13px; padding: 0 14px; font-weight: 700; font-size: 13px;
}
select { border: 1px solid var(--line); color: var(--ink); background: white; }
.secondary-btn, .icon-btn { border: 1px solid var(--line); color: var(--ink); background: white; }
.primary-btn { border: 0; color: white; background: var(--brand); padding-inline: 20px; }
.detail-link { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }

.section-heading { display: flex; justify-content: space-between; align-items: end; margin: 0 2px 14px; }
.section-heading h2 { margin: 4px 0 0; font-size: 25px; }
.section-heading > span { color: var(--muted); font-size: 13px; }
.contact-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.company-groups { display: grid; gap: 14px; }
.company-group { overflow: hidden; border: 1px solid rgba(18,61,58,.1); border-radius: 22px; background: rgba(255,255,255,.62); box-shadow: 0 9px 32px rgba(22,55,51,.055); }
.company-group summary { min-height: 68px; display: flex; align-items: center; gap: 13px; padding: 14px 18px; list-style: none; cursor: pointer; user-select: none; }
.company-group summary::-webkit-details-marker { display: none; }
.company-group summary::after { content: "⌄"; margin-left: auto; color: var(--muted); font-size: 22px; transition: transform .18s ease; }
.company-group[open] summary::after { transform: rotate(180deg); }
.company-mark { flex: 0 0 42px; height: 42px; display: grid; place-items: center; border-radius: 13px; color: var(--brand); background: var(--group-color, #dce9e5); font-size: 18px; font-weight: 900; }
.company-summary-text { min-width: 0; }
.company-summary-text strong, .company-summary-text small { display: block; }
.company-summary-text strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 16px; }
.company-summary-text small { margin-top: 3px; color: var(--muted); font-size: 11px; }
.company-group-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; padding: 4px 14px 14px; border-top: 1px solid rgba(18,61,58,.07); }
.company-group-grid .contact-card { margin-top: 10px; }
.contact-card {
  position: relative; overflow: hidden; min-height: 220px; padding: 22px; border: 1px solid rgba(18,61,58,.08);
  border-radius: 22px; background: var(--card); box-shadow: 0 10px 35px rgba(22,55,51,.06);
  transition: transform .18s ease, box-shadow .18s ease;
}
.contact-card:hover { transform: translateY(-3px); box-shadow: 0 18px 42px rgba(22,55,51,.11); }
.contact-card::after {
  content: ""; position: absolute; right: -42px; bottom: -48px; width: 120px; height: 120px;
  border-radius: 50%; background: var(--avatar-color, #dce9e5); opacity: .55;
}
.card-head { display: flex; gap: 13px; align-items: center; position: relative; z-index: 1; }
.avatar {
  flex: 0 0 48px; height: 48px; border-radius: 15px; display: grid; place-items: center;
  background: var(--avatar-color, #dce9e5); color: var(--brand); font-size: 20px; font-weight: 900;
}
.card-head h3 { margin: 0; font-size: 18px; }
.card-head p { margin: 4px 0 0; font-size: 12px; color: var(--muted); }
.company-line { margin: 20px 0 12px; font-weight: 800; font-size: 14px; }
.contact-lines { position: relative; z-index: 1; display: grid; gap: 8px; color: var(--muted); font-size: 12px; }
.contact-lines a { color: inherit; text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag-list { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 14px; position: relative; z-index: 1; }
.tag { padding: 4px 8px; border-radius: 999px; color: var(--brand-2); background: #e9f1ed; font-size: 10px; font-weight: 700; }
.card-menu { position: absolute; top: 14px; right: 14px; z-index: 2; display: flex; gap: 5px; }
.more-btn { width: 34px; height: 34px; border: 0; border-radius: 10px; background: rgba(255,255,255,.8); color: var(--muted); font-size: 20px; }
.quick-delete-btn { width: 34px; height: 34px; border: 0; border-radius: 10px; color: var(--danger); background: #fff4f2; font-size: 21px; line-height: 1; }
.quick-delete-btn:hover { color: white; background: var(--danger); }
.card-click-area { cursor: pointer; }
.contact-card { touch-action: pan-y; }
.contact-card.swipe-ready { transition: transform .06s linear, box-shadow .18s ease; }
.contact-card.swipe-danger { box-shadow: 0 0 0 3px rgba(185,71,61,.22), 0 18px 42px rgba(185,71,61,.16); }

.empty-state { padding: 65px 20px; text-align: center; color: var(--muted); }
.empty-state[hidden] { display: none; }
.empty-illustration { width: 72px; height: 72px; margin: auto; display: grid; place-items: center; border-radius: 22px; color: var(--brand-2); background: #dfeae5; font-size: 34px; transform: rotate(-3deg); }
.empty-state h3 { color: var(--ink); margin-bottom: 6px; }
.empty-state p { margin: 0 0 20px; }

dialog {
  width: min(760px, calc(100% - 28px)); max-height: calc(100dvh - 28px); border: 0; border-radius: 26px;
  padding: 0; color: var(--ink); background: #fbfcfa; box-shadow: 0 30px 90px rgba(10,35,32,.28);
}
dialog::backdrop { background: rgba(9, 32, 29, .52); backdrop-filter: blur(5px); }
dialog form, #detailContent { padding: 26px; overflow: auto; max-height: calc(100dvh - 28px); }
.dialog-header { display: flex; justify-content: space-between; align-items: start; margin-bottom: 22px; }
.dialog-header h2 { margin: 5px 0 0; }
.close-btn { width: 40px; height: 40px; border: 0; border-radius: 13px; background: #edf1ee; font-size: 25px; color: var(--muted); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 15px; }
.form-grid label span { display: block; margin: 0 0 7px 3px; font-size: 12px; font-weight: 800; color: #536864; }
.form-grid input, .form-grid textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 13px; padding: 12px 13px;
  outline: none; color: var(--ink); background: white; resize: vertical;
}
.form-grid input:focus, .form-grid textarea:focus { border-color: var(--brand-2); box-shadow: 0 0 0 3px rgba(31,98,92,.1); }
.full { grid-column: 1 / -1; }
.dialog-actions { position: sticky; bottom: -26px; display: flex; justify-content: flex-end; gap: 8px; margin: 24px -26px -26px; padding: 16px 26px calc(16px + env(safe-area-inset-bottom)); background: rgba(251,252,250,.94); border-top: 1px solid var(--line); backdrop-filter: blur(12px); }
.scan-preview-area { display: grid; grid-template-columns: minmax(160px, 1fr) 1fr; gap: 14px; margin: -4px 0 20px; padding: 12px; border-radius: 16px; background: #edf3ef; }
.card-side-previews { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.card-side-previews figure { position: relative; margin: 0; }
.card-side-previews img { width: 100%; height: 100px; object-fit: cover; border-radius: 11px; background: #d8e0dc; }
.card-side-previews figcaption { position: absolute; left: 7px; bottom: 7px; padding: 3px 7px; border-radius: 999px; color: white; background: rgba(18,61,58,.8); font-size: 10px; font-weight: 800; }
.ocr-panel { display: flex; flex-direction: column; justify-content: center; gap: 11px; }
.ocr-status { display: flex; align-items: center; gap: 13px; }
.ocr-status strong, .ocr-status small { display: block; }
.ocr-status small { color: var(--muted); margin-top: 5px; }
.add-back-btn { width: max-content; min-height: 36px; display: inline-flex; align-items: center; padding: 0 12px; border: 1px dashed #8eaaa2; border-radius: 11px; color: var(--brand-2); background: rgba(255,255,255,.58); font-size: 12px; font-weight: 800; cursor: pointer; }
.add-back-btn:hover { background: white; }
.spinner { width: 28px; height: 28px; border: 3px solid #c9d8d2; border-top-color: var(--brand-2); border-radius: 50%; animation: spin .85s linear infinite; }
.spinner.done { animation: none; border-color: #51a77d; position: relative; }
.spinner.done::after { content: "✓"; position: absolute; inset: -5px; display: grid; place-items: center; color: #31865f; font-weight: 900; }
@keyframes spin { to { transform: rotate(360deg); } }

.detail-top { display: flex; justify-content: space-between; gap: 12px; }
.detail-person { display: flex; gap: 15px; align-items: center; }
.detail-person .avatar { width: 60px; height: 60px; }
.detail-person h2 { margin: 0; }
.detail-person p { margin: 5px 0 0; color: var(--muted); }
.detail-actions { display: flex; gap: 7px; flex-wrap: wrap; margin: 24px 0; }
.detail-list { display: grid; gap: 10px; }
.detail-row { display: grid; grid-template-columns: 90px 1fr; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.detail-row span { color: var(--muted); }
.detail-row a { color: var(--brand-2); text-decoration: none; word-break: break-all; }
.detail-card-images { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin-top: 18px; }
.detail-card-images figure { margin: 0; }
.detail-card-image { width: 100%; height: 220px; object-fit: contain; border-radius: 15px; background: #edf1ee; }
.detail-card-images figcaption { margin-top: 5px; color: var(--muted); text-align: center; font-size: 11px; font-weight: 800; }
.danger-btn { min-height: 44px; border: 1px solid #efd0cd; border-radius: 13px; padding: 0 14px; color: var(--danger); background: #fff8f7; font-weight: 700; }
.toast {
  position: fixed; z-index: 50; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translate(-50%, 30px);
  padding: 12px 17px; border-radius: 13px; color: white; background: #173b38; box-shadow: var(--shadow);
  opacity: 0; pointer-events: none; transition: .22s ease; font-size: 13px; font-weight: 700;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 900px) {
  .action-grid { grid-template-columns: repeat(2, 1fr); }
  .contact-grid { grid-template-columns: repeat(2, 1fr); }
  .company-group-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .toolbar { align-items: stretch; flex-direction: column; }
  .filter-row { overflow-x: auto; padding-bottom: 3px; }
  .filter-row > * { flex: 0 0 auto; }
}

@media (max-width: 600px) {
  .app-shell { padding: 0 14px 94px; }
  .topbar { min-height: 74px; }
  .brand-logo { width: 42px; height: 42px; border-radius: 12px; }
  .brand h1 { font-size: 18px; }
  .desktop-only { display: none; }
  .hero { min-height: 210px; padding: 25px 22px; border-radius: 24px; flex-direction: column; align-items: flex-start; justify-content: space-between; }
  .hero h2 { font-size: 30px; }
  .hero-stats { width: 100%; }
  .hero-stats div { flex: 1; min-width: 0; padding: 12px 14px; }
  .hero-stats strong { font-size: 23px; }
  .action-grid { gap: 9px; margin-top: 12px; }
  .action-card { min-height: 90px; padding: 13px; border-radius: 17px; align-items: flex-start; flex-direction: column; gap: 8px; }
  .action-icon { flex-basis: 34px; width: 34px; height: 34px; border-radius: 10px; font-size: 21px; }
  .action-card small { display: none; }
  .toolbar { margin-bottom: 28px; }
  .filter-row { margin-right: -14px; padding-right: 14px; }
  .contact-grid { grid-template-columns: 1fr; }
  .company-group-grid { grid-template-columns: 1fr; padding: 2px 10px 10px; }
  .company-group summary { min-height: 62px; padding: 11px 14px; }
  .contact-card { min-height: 205px; }
  dialog { width: 100%; max-height: 94dvh; margin: auto 0 0; border-radius: 25px 25px 0 0; }
  dialog form, #detailContent { max-height: 94dvh; padding: 21px 17px; }
  .form-grid { grid-template-columns: 1fr; gap: 13px; }
  .full { grid-column: auto; }
  .dialog-actions { bottom: -21px; margin: 22px -17px -21px; padding: 13px 17px calc(13px + env(safe-area-inset-bottom)); }
  .scan-preview-area { grid-template-columns: 1fr; }
  .card-side-previews img { height: 92px; }
  .ocr-status { gap: 8px; }
  .ocr-status strong { font-size: 12px; }
  .ocr-status small { font-size: 10px; }
  .detail-card-images { grid-template-columns: 1fr; }
  .detail-card-image { height: auto; max-height: 280px; }
  .detail-top { align-items: start; }
  .detail-row { grid-template-columns: 72px 1fr; }
}
