/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #1a202c;
  --text-muted: #718096;
  --primary: #2b6cb0;
  --primary-light: #ebf4ff;
  --gold: #d4a017;
  --silver: #8a9bb0;
  --bronze: #cd7f32;
  --green: #276749;
  --green-bg: #f0fff4;
  --radius: 12px;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.12);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

.container { max-width: 960px; margin: 0 auto; padding: 0 20px; }

/* ── Header ── */
header {
  background: linear-gradient(135deg, #1a365d 0%, #2b6cb0 60%, #3182ce 100%);
  color: #fff;
  padding: 48px 20px 40px;
  text-align: center;
}
header h1 { font-size: 2rem; font-weight: 800; letter-spacing: -0.5px; }
header .subtitle { margin-top: 8px; font-size: 1rem; opacity: 0.85; }

/* ── Calculator Section ── */
.calculator-section { margin-top: -20px; padding-bottom: 12px; }

.calc-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 28px 28px 24px;
}

.calc-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}
@media (max-width: 540px) { .calc-row { grid-template-columns: 1fr; } }

.calc-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.calc-group input,
.calc-group select {
  width: 100%;
  padding: 12px 14px;
  border: 2px solid var(--border);
  border-radius: 8px;
  font-size: 1.1rem;
  font-family: inherit;
  color: var(--text);
  background: #fff;
  transition: border-color 0.2s;
  outline: none;
}
.calc-group input:focus,
.calc-group select:focus { border-color: var(--primary); }

.toggle-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.toggle-label { font-size: 0.9rem; font-weight: 600; color: var(--text-muted); white-space: nowrap; }
.toggle-group { display: flex; gap: 8px; flex-wrap: wrap; }

.toggle-btn {
  padding: 9px 20px;
  border: 2px solid var(--border);
  border-radius: 50px;
  background: #fff;
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.2s;
  color: var(--text-muted);
  font-weight: 500;
}
.toggle-btn.active {
  border-color: var(--primary);
  background: var(--primary);
  color: #fff;
}
.toggle-btn:hover:not(.active) { border-color: var(--primary); color: var(--primary); }

.calc-btn {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, #2b6cb0, #3182ce);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.1s;
  font-family: inherit;
  letter-spacing: 0.03em;
}
.calc-btn:hover { opacity: 0.92; }
.calc-btn:active { transform: scale(0.99); }

/* ── Merchant Search ── */
.search-wrap {
  position: relative;
  display: flex;
  align-items: center;
}
.search-icon {
  position: absolute;
  left: 12px;
  font-size: 1rem;
  pointer-events: none;
  z-index: 1;
}
.search-wrap input {
  width: 100%;
  padding: 12px 40px 12px 38px;
  border: 2px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  font-family: inherit;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}
.search-wrap input:focus { border-color: var(--primary); }
.search-clear {
  position: absolute;
  right: 10px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.85rem;
  padding: 4px 6px;
  border-radius: 4px;
  display: none;
}
.search-clear:hover { background: var(--bg); }
.search-clear.visible { display: block; }

/* Dropdown */
.search-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0; right: 0;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-lg);
  z-index: 100;
  max-height: 280px;
  overflow-y: auto;
  display: none;
}
.search-dropdown.open { display: block; }
.dropdown-item {
  padding: 11px 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}
.dropdown-item:last-child { border-bottom: none; }
.dropdown-item:hover, .dropdown-item.focused { background: var(--primary-light); }
.dropdown-item-name { font-weight: 600; font-size: 0.9rem; flex: 1; }
.dropdown-item-name mark { background: #fef9c3; color: inherit; border-radius: 2px; }
.dropdown-cat-tag {
  font-size: 0.72rem;
  padding: 2px 7px;
  border-radius: 10px;
  background: #e2e8f0;
  color: #4a5568;
  white-space: nowrap;
}
.dropdown-linepay {
  font-size: 0.7rem;
  padding: 2px 7px;
  border-radius: 10px;
  background: #dcfce7;
  color: #166534;
  white-space: nowrap;
}
.dropdown-empty {
  padding: 16px 14px;
  color: var(--text-muted);
  font-size: 0.88rem;
  text-align: center;
}

/* Selected chip */
.selected-chip {
  margin-top: 8px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--primary-light);
  border: 1.5px solid var(--primary);
  border-radius: 20px;
  padding: 5px 10px 5px 14px;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--primary);
}
.selected-chip button {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--primary);
  font-size: 0.85rem;
  padding: 0 2px;
  line-height: 1;
}
.chip-cat {
  font-size: 0.72rem;
  background: var(--primary);
  color: #fff;
  border-radius: 8px;
  padding: 1px 7px;
  font-weight: 500;
}
.chip-linepay {
  font-size: 0.72rem;
  background: #dcfce7;
  color: #166534;
  border-radius: 8px;
  padding: 1px 7px;
  font-weight: 500;
}

/* calc-group position relative for dropdown */
.calc-group { position: relative; }

.label-note { font-size: 0.75rem; font-weight: 400; color: var(--text-muted); }

/* ── Results Section ── */
.results-section { padding: 32px 0; }

.results-header { text-align: center; margin-bottom: 24px; }
.results-header h2 { font-size: 1.5rem; font-weight: 700; }
.results-subtitle { color: var(--text-muted); margin-top: 6px; font-size: 0.95rem; }

.results-grid {
  display: grid;
  gap: 12px;
}

/* ── Result Card ── */
.result-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  display: grid;
  grid-template-columns: 48px 1fr auto;
  align-items: center;
  gap: 16px;
  border-left: 5px solid transparent;
  transition: transform 0.15s, box-shadow 0.15s;
  position: relative;
}
.result-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }

.result-card.rank-1 { border-left-color: var(--gold); background: linear-gradient(to right, #fffbeb, #fff); }
.result-card.rank-2 { border-left-color: var(--silver); }
.result-card.rank-3 { border-left-color: var(--bronze); }

.rank-badge {
  width: 48px; height: 48px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.4rem;
  font-weight: 800;
  flex-shrink: 0;
}
.rank-1 .rank-badge { background: #fef3c7; color: var(--gold); }
.rank-2 .rank-badge { background: #f1f5f9; color: var(--silver); }
.rank-3 .rank-badge { background: #fdf2e9; color: var(--bronze); }
.rank-other .rank-badge { background: var(--bg); color: var(--text-muted); font-size: 1rem; }

.card-info { min-width: 0; }
.card-name { font-weight: 700; font-size: 0.95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-bank { font-size: 0.78rem; color: var(--text-muted); margin-top: 1px; }
.card-rate-note { font-size: 0.8rem; color: var(--primary); margin-top: 4px; }
.card-special-note { font-size: 0.75rem; color: #e07b20; margin-top: 3px; }

.card-rewards { text-align: right; flex-shrink: 0; }
.reward-value {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--green);
  white-space: nowrap;
}
.rank-1 .reward-value { color: #b7791f; }
.reward-pct { font-size: 0.82rem; color: var(--text-muted); margin-top: 2px; }
.reward-unit { font-size: 0.72rem; color: var(--text-muted); }

/* ── Placeholder ── */
.placeholder {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.placeholder-icon { font-size: 3rem; margin-bottom: 12px; }
.placeholder p { font-size: 1rem; }

/* ── Rate Table ── */
.table-section { padding: 8px 0 48px; }
.table-section h2 { font-size: 1.4rem; font-weight: 700; margin-bottom: 8px; }
.table-note {
  font-size: 0.82rem;
  color: #c05621;
  background: #fff3cd;
  border: 1px solid #ffc107;
  border-radius: 6px;
  padding: 8px 12px;
  margin-bottom: 16px;
}
.table-wrapper { overflow-x: auto; -webkit-overflow-scrolling: touch; }

.rate-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  font-size: 0.88rem;
  min-width: 700px;
}
.rate-table th {
  background: #2d3748;
  color: #fff;
  padding: 12px 14px;
  text-align: left;
  font-weight: 600;
  font-size: 0.8rem;
  letter-spacing: 0.03em;
  white-space: nowrap;
}
.rate-table td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: top; }
.rate-table tr:last-child td { border-bottom: none; }
.rate-table tr:hover td { background: var(--primary-light); }

.rate-table .card-col { font-weight: 700; white-space: nowrap; }
.rate-table .bank-tag {
  display: inline-block;
  font-size: 0.7rem;
  padding: 1px 6px;
  border-radius: 10px;
  margin-top: 3px;
  font-weight: 600;
}

.cell-good { color: var(--green); font-weight: 700; }
.cell-best { color: #b7791f; font-weight: 700; background: #fef9c3; border-radius: 4px; padding: 1px 4px; }
.cell-note { font-size: 0.72rem; color: var(--text-muted); display: block; margin-top: 2px; }
.cell-na { color: #cbd5e0; }

.source-tag {
  display: inline-block;
  font-size: 0.68rem;
  background: #fef3c7;
  color: #92400e;
  border-radius: 4px;
  padding: 1px 5px;
  margin-left: 4px;
  font-weight: 600;
  vertical-align: middle;
}

/* ── Footer ── */
footer {
  background: #2d3748;
  color: #a0aec0;
  text-align: center;
  padding: 20px;
  font-size: 0.82rem;
}
