:root {
  --bg: #f5f7fb;
  --card: #ffffff;
  --fg: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #0f172a;
  --primary-hover: #1e293b;
  --accent: #b91c1c;
  --warn: #b45309;
  --success: #047857;
  --danger: #b91c1c;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --card: #111827;
    --fg: #e5e7eb;
    --muted: #9ca3af;
    --border: #1f2937;
    --primary: #e5e7eb;
    --primary-hover: #f3f4f6;
    --shadow: 0 1px 3px rgba(0,0,0,0.4);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Microsoft JhengHei", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.55;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom, 20px);
}

header {
  background: var(--primary);
  color: var(--bg);
  padding: calc(env(safe-area-inset-top, 0px) + 18px) 16px 14px;
  text-align: center;
}
header h1 { margin: 0; font-size: 19px; letter-spacing: 0.5px; }
.subtitle { margin: 4px 0 0; font-size: 12px; opacity: 0.85; }

.case-bar {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 10px;
  flex-wrap: wrap;
}
.case-bar-btn {
  background: rgba(255,255,255,0.15);
  color: inherit;
  border: 1px solid rgba(255,255,255,0.3);
  padding: 6px 12px;
  border-radius: 16px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  min-height: 0;
  font-family: inherit;
}
.case-bar-btn:hover {
  background: rgba(255,255,255,0.25);
}

.tabs {
  display: flex;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  position: sticky;
  top: 0;
  z-index: 50;
  -webkit-overflow-scrolling: touch;
}
.tab {
  flex: 1 1 auto;
  min-width: 80px;
  padding: 14px 8px;
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  font-family: inherit;
}
.tab.active {
  color: var(--primary);
  border-bottom-color: var(--accent);
  font-weight: 600;
}

main { padding: 16px; max-width: 820px; margin: 0 auto; }

.page { display: none; }
.page.active { display: block; }

h2 { font-size: 18px; margin: 8px 0 12px; }
h3 { font-size: 16px; margin: 0 0 10px; }
h4 { font-size: 14px; margin: 14px 0 8px; color: var(--muted); }

.case-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}

label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin: 10px 0 4px;
  color: var(--fg);
}
input[type=text], input[type=date], input[type=number], input[type=tel], input[type=email],
textarea, select {
  display: block;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;  /* iOS 不會自動 zoom */
  background: var(--bg);
  color: var(--fg);
  font-family: inherit;
}
textarea { resize: vertical; min-height: 80px; }
input:focus, textarea:focus, select:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

button {
  padding: 12px 18px;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  min-height: 44px;
  transition: background 0.15s;
}
button:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary {
  background: var(--accent);
  color: #fff;
  width: 100%;
  margin-top: 12px;
}
.btn-primary:not(:disabled):hover { background: #991b1b; }

.btn-secondary {
  background: var(--border);
  color: var(--fg);
  margin-top: 8px;
}
.btn-secondary:hover { background: var(--muted); color: var(--bg); }

.btn-row { display: flex; gap: 8px; margin-top: 8px; }
.btn-row button { flex: 1; }

.btn-large { font-size: 16px; padding: 14px; }

.hint { color: var(--muted); font-size: 13px; margin: 0 0 12px; }

/* Modal */
.modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal.show { display: flex; }
.modal-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 480px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-card h2 { margin-top: 0; }
.modal-card ul { padding-left: 20px; }
.modal-card li { margin: 6px 0; }
.checkbox {
  display: flex !important;
  align-items: flex-start;
  gap: 8px;
  font-weight: normal !important;
  font-size: 13px;
  margin: 16px 0 !important;
  cursor: pointer;
}
.checkbox input { width: auto !important; margin-top: 3px; }

/* Evidence list */
.evidence-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}
.evidence-item {
  display: flex;
  gap: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  align-items: flex-start;
}
.evidence-thumb {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 6px;
  background: var(--border);
  flex-shrink: 0;
}
.evidence-info { flex: 1; min-width: 0; }
.evidence-title {
  font-weight: 600;
  font-size: 13px;
  word-break: break-all;
}
.evidence-meta {
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}
.evidence-ocr {
  margin-top: 6px;
  font-size: 12px;
  color: var(--fg);
  background: var(--card);
  padding: 6px;
  border-radius: 4px;
  border-left: 3px solid var(--accent);
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  white-space: pre-wrap;
  max-height: 80px;
  overflow-y: auto;
}
.evidence-extracted {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}
.tag {
  display: inline-block;
  padding: 2px 8px;
  background: var(--accent);
  color: #fff;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 500;
}
.tag.warn { background: var(--warn); }
.tag.success { background: var(--success); }
.tag.muted { background: var(--muted); }

.evidence-del {
  background: transparent;
  color: var(--danger);
  padding: 4px 8px;
  min-height: 0;
  font-size: 12px;
}

/* Timeline */
.timeline-event {
  display: flex;
  gap: 12px;
  padding: 12px;
  background: var(--card);
  border-left: 3px solid var(--primary);
  border-radius: 8px;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
}
.timeline-event.transaction { border-left-color: var(--success); }
.timeline-event.communication { border-left-color: var(--accent); }
.timeline-event.deadline {
  border-left-color: var(--warn);
  background: #fef3c7;
}
@media (prefers-color-scheme: dark) {
  .timeline-event.deadline { background: #422006; }
}
.timeline-date {
  flex-shrink: 0;
  width: 80px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}
.timeline-date .day { font-size: 22px; color: var(--fg); display: block; }
.timeline-body { flex: 1; }
.timeline-title { font-weight: 600; margin: 0 0 4px; }
.timeline-desc { font-size: 13px; color: var(--muted); margin: 0; }

/* Missing evidence */
.missing-item {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 12px;
  background: var(--card);
  border-radius: 8px;
  margin-bottom: 8px;
  border-left: 3px solid var(--warn);
}
.missing-item.have { border-left-color: var(--success); opacity: 0.7; }
.missing-check {
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--warn);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  color: var(--warn);
}
.missing-item.have .missing-check {
  border-color: var(--success);
  background: var(--success);
  color: #fff;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: var(--primary);
  color: var(--bg);
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 14px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  opacity: 0;
  transition: all 0.3s;
  z-index: 200;
  max-width: 80%;
  text-align: center;
  pointer-events: none;
}
.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* OCR progress */
.ocr-progress {
  margin-top: 6px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}
.ocr-progress-bar {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.2s;
}

/* OCR Suggestion Badge */
.ocr-suggest {
  margin-top: 6px;
  min-height: 0;
}
.ocr-suggest:empty { display: none; }
.ocr-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  background: #fef3c7;
  color: #92400e;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  border: 1px solid #fbbf24;
  cursor: pointer;
  transition: background 0.15s;
}
.ocr-badge:hover { background: #fde68a; }
.ocr-badge::before {
  content: '✨';
}
@media (prefers-color-scheme: dark) {
  .ocr-badge { background: #422006; color: #fbbf24; border-color: #78350f; }
  .ocr-badge:hover { background: #5b2d0f; }
}

/* OCR progress overlay (image items) */
.ocr-overlay {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(0,0,0,0.7);
  color: #fff;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 500;
}
.evidence-item { position: relative; }