/* ============================================
   FXCard — Travel Credit Card Optimizer
   Mobile-first responsive styles
   ============================================ */

:root {
  --primary: #1a73e8;
  --primary-dark: #1557b0;
  --primary-light: #e8f0fe;
  --success: #0d904f;
  --success-bg: #e6f4ea;
  --warning: #e37400;
  --warning-bg: #fef7e0;
  --danger: #d93025;
  --text: #202124;
  --text-secondary: #5f6368;
  --border: #dadce0;
  --bg: #ffffff;
  --bg-surface: #f8f9fa;
  --bg-card: #ffffff;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.1);
  --shadow-md: 0 2px 8px rgba(0,0,0,0.12);
  --shadow-lg: 0 4px 16px rgba(0,0,0,0.15);
  --radius: 12px;
  --radius-sm: 8px;
  --transition: 0.2s ease;
}

@media (prefers-color-scheme: dark) {
  :root {
    --primary: #8ab4f8;
    --primary-dark: #669df6;
    --primary-light: #1a2744;
    --success: #81c995;
    --success-bg: #1a3a2a;
    --warning: #fdd663;
    --warning-bg: #3a3018;
    --danger: #f28b82;
    --text: #e8eaed;
    --text-secondary: #9aa0a6;
    --border: #3c4043;
    --bg: #202124;
    --bg-surface: #292a2d;
    --bg-card: #2d2e31;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
    --shadow-md: 0 2px 8px rgba(0,0,0,0.4);
    --shadow-lg: 0 4px 16px rgba(0,0,0,0.5);
  }
}

/* Reset */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  background: var(--bg-surface);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* App shell */
.app-header {
  background: var(--primary);
  color: #fff;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow-md);
}

.app-header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-header h1 img {
  width: 28px;
  height: 28px;
  border-radius: 6px;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.header-btn {
  background: rgba(255,255,255,0.15);
  border: none;
  color: #fff;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition);
}

.header-btn:hover {
  background: rgba(255,255,255,0.25);
}

.main-content {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px;
}

/* Tabs */
.tabs {
  display: flex;
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  margin-bottom: 16px;
  overflow: hidden;
}

.tab-btn {
  flex: 1;
  padding: 12px 8px;
  border: none;
  background: none;
  color: var(--text-secondary);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  position: relative;
}

.tab-btn.active {
  color: var(--primary);
}

.tab-btn.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 20%;
  width: 60%;
  height: 3px;
  background: var(--primary);
  border-radius: 3px 3px 0 0;
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}

/* Cards / panels */
.panel {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 16px;
  margin-bottom: 16px;
}

.panel-title {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Form elements */
.form-group {
  margin-bottom: 12px;
}

.form-group label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.form-row {
  display: flex;
  gap: 12px;
}

.form-row .form-group {
  flex: 1;
}

input[type="text"],
input[type="number"],
select {
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  color: var(--text);
  background: var(--bg);
  transition: border-color var(--transition);
  -webkit-appearance: none;
  appearance: none;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(26,115,232,0.15);
}

select {
  cursor: pointer;
  padding-right: 32px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2 4l4 4 4-4' fill='none' stroke='%235f6368' stroke-width='1.5' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  font-size: 0.9375rem;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: all var(--transition);
  min-height: 44px;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-dark);
  box-shadow: var(--shadow-sm);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--bg-surface);
  color: var(--text);
  border: 1.5px solid var(--border);
}

.btn-secondary:hover {
  background: var(--border);
}

.btn-danger {
  background: none;
  color: var(--danger);
  padding: 6px 12px;
  font-size: 0.8125rem;
}

.btn-danger:hover {
  background: rgba(217,48,37,0.08);
}

.btn-sm {
  padding: 6px 12px;
  font-size: 0.8125rem;
  min-height: 32px;
}

.btn-block {
  width: 100%;
}

/* Compare section */
.compare-controls {
  display: flex;
  gap: 12px;
  align-items: flex-end;
}

.compare-controls .form-group:first-child {
  flex: 2;
}

.compare-controls .form-group:nth-child(2) {
  flex: 1;
}

.compare-btn {
  flex-shrink: 0;
  margin-bottom: 12px;
}

/* Results */
.results-container {
  margin-top: 16px;
}

.winner-banner {
  background: var(--success-bg);
  border: 2px solid var(--success);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.winner-badge {
  background: var(--success);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 20px;
  white-space: nowrap;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.winner-info {
  flex: 1;
}

.winner-name {
  font-weight: 700;
  font-size: 1rem;
}

.winner-savings {
  font-size: 0.8125rem;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* Result cards (mobile) */
.result-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 14px 16px;
  margin-bottom: 10px;
  border-left: 4px solid transparent;
  transition: border-color var(--transition);
}

.result-card.rank-1 {
  border-left-color: var(--success);
}

.result-card.amex-group {
  background: var(--primary-light);
  border-left-color: var(--primary);
}

.result-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.result-card-name {
  font-weight: 700;
  font-size: 0.9375rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.result-card-cost {
  font-weight: 700;
  font-size: 1.125rem;
  color: var(--primary);
}

.result-card-details {
  display: none;
  grid-template-columns: 1fr 1fr;
  gap: 6px 16px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 0.8125rem;
  color: var(--text-secondary);
}

.result-card-details.show {
  display: grid;
}

.result-card-details dt {
  font-weight: 500;
}

.result-card-details dd {
  text-align: right;
  font-weight: 600;
  color: var(--text);
}

.toggle-details-btn {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 0.75rem;
  cursor: pointer;
  padding: 4px 0;
  margin-top: 4px;
  font-weight: 600;
}

/* Network logos */
.network-logo {
  width: 32px;
  height: 20px;
  object-fit: contain;
  flex-shrink: 0;
}

.network-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.625rem;
  font-weight: 700;
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  flex-shrink: 0;
}

.network-badge.visa { background: #1a1f71; }
.network-badge.mastercard { background: #F79E1B; }
.network-badge.unionpay { background: #d2232a; }
.network-badge.amex { background: #006fcf; }
.network-badge.cash { background: #5f6368; }

/* Approx badge */
.approx-badge {
  font-size: 0.6875rem;
  color: var(--warning);
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  cursor: default;
}

/* Help button */
.help-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--primary-light);
  color: var(--primary);
  font-size: 0.75rem;
  font-weight: 700;
  border: none;
  cursor: pointer;
  flex-shrink: 0;
  line-height: 1;
}

.help-btn:hover {
  background: var(--primary);
  color: #fff;
}

/* Card list */
.card-list {
  list-style: none;
}

.card-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.card-item:last-child {
  border-bottom: none;
}

.card-item-info {
  flex: 1;
  min-width: 0;
}

.card-item-name {
  font-weight: 600;
  font-size: 0.9375rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-item-meta {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 2px;
}

.card-item-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

/* Add card form */
.add-card-form {
  border-top: 1px solid var(--border);
  padding-top: 16px;
  margin-top: 8px;
}

/* Advanced settings */
.advanced-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 0.8125rem;
  font-weight: 600;
  cursor: pointer;
  padding: 8px 0;
}

.advanced-toggle .arrow {
  transition: transform var(--transition);
  font-size: 0.625rem;
}

.advanced-toggle.open .arrow {
  transform: rotate(90deg);
}

.advanced-content {
  display: none;
  padding: 12px 0;
}

.advanced-content.show {
  display: block;
}

/* Modal / Tooltip */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
  opacity: 0;
  visibility: hidden;
  transition: all 0.2s;
}

.modal-overlay.show {
  opacity: 1;
  visibility: visible;
}

.modal {
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 480px;
  width: 100%;
  box-shadow: var(--shadow-lg);
  max-height: 90vh;
  overflow-y: auto;
}

.modal h3 {
  font-size: 1.125rem;
  margin-bottom: 12px;
}

.modal p {
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-bottom: 10px;
  line-height: 1.6;
}

.modal-close {
  display: block;
  margin-top: 16px;
  margin-left: auto;
}

/* Stale indicator */
.stale-banner {
  background: var(--warning-bg);
  color: var(--warning);
  font-size: 0.8125rem;
  font-weight: 600;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  display: none;
  align-items: center;
  gap: 6px;
}

.stale-banner.show {
  display: flex;
}

/* Install banner */
.install-banner {
  background: var(--primary-light);
  border: 1.5px solid var(--primary);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 16px;
  display: none;
  align-items: center;
  gap: 12px;
}

.install-banner.show {
  display: flex;
}

.install-banner-text {
  flex: 1;
  font-size: 0.875rem;
  font-weight: 500;
}

.install-banner .btn-dismiss {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 1.25rem;
  padding: 4px;
}

/* Loading */
.loading-spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2.5px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-overlay {
  text-align: center;
  padding: 32px 16px;
  color: var(--text-secondary);
}

.loading-overlay .loading-spinner {
  width: 32px;
  height: 32px;
  margin-bottom: 12px;
}

/* Desktop table view */
@media (min-width: 768px) {
  .main-content {
    max-width: 900px;
    padding: 24px;
  }

  .result-card-details {
    display: grid;
  }

  .toggle-details-btn {
    display: none;
  }

  .compare-controls {
    align-items: flex-end;
  }

  .results-table {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    background: var(--bg-card);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
  }

  .results-table th {
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 10px 12px;
    background: var(--bg-surface);
    text-align: left;
    border-bottom: 1.5px solid var(--border);
  }

  .results-table td {
    padding: 10px 12px;
    font-size: 0.875rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
  }

  .results-table tr:last-child td {
    border-bottom: none;
  }

  .results-table tr.rank-1 {
    background: var(--success-bg);
  }

  .results-table tr.amex-group {
    background: var(--primary-light);
  }

  .results-table .cost-cell {
    font-weight: 700;
    color: var(--primary);
  }
}

/* Empty states */
.empty-state {
  text-align: center;
  padding: 32px 16px;
  color: var(--text-secondary);
}

.empty-state-icon {
  font-size: 2.5rem;
  margin-bottom: 8px;
}

.empty-state p {
  font-size: 0.875rem;
}

/* Utility */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  border: 0;
}

.text-right { text-align: right; }
.text-center { text-align: center; }
.mt-8 { margin-top: 8px; }
.mt-12 { margin-top: 12px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }
.gap-8 { gap: 8px; }
.flex { display: flex; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }

/* Optgroup-like divider in select */
option.divider {
  font-size: 0.75rem;
  color: var(--text-secondary);
  background: var(--bg-surface);
}

/* ============================================
   Rate History page styles
   ============================================ */

/* Back link in header */
.back-link {
  color: #fff;
  text-decoration: none;
  font-size: 1.4rem;
  line-height: 1;
  margin-right: 4px;
  display: inline-flex;
  align-items: center;
  opacity: 0.9;
  transition: opacity var(--transition);
}

.back-link:hover {
  opacity: 1;
}

/* History controls row */
.history-controls {
  display: flex;
  gap: 16px;
  align-items: flex-end;
  flex-wrap: wrap;
}

.history-controls .form-group {
  flex: 1;
  min-width: 140px;
}

.period-toggle {
  display: flex;
  gap: 0;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
}

.period-btn {
  flex: 1;
  padding: 8px 16px;
  border: none;
  background: var(--bg-surface);
  color: var(--text-secondary);
  font-size: 0.8125rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.period-btn:not(:last-child) {
  border-right: 1px solid var(--border);
}

.period-btn:hover {
  background: var(--primary-light);
  color: var(--primary);
}

.period-btn.active {
  background: var(--primary);
  color: #fff;
}

/* Chart container */
.chart-container {
  position: relative;
  width: 100%;
  height: 350px;
}

.chart-container canvas {
  width: 100% !important;
  height: 100% !important;
}

.chart-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 16px;
  color: var(--text-secondary);
  gap: 12px;
}

.chart-empty {
  text-align: center;
  padding: 48px 16px;
  color: var(--text-secondary);
}

.chart-empty .empty-state-icon {
  font-size: 2.5rem;
  margin-bottom: 8px;
}

.chart-error {
  text-align: center;
  padding: 24px 16px;
  color: var(--danger);
  font-size: 0.875rem;
}

/* Winner summary */
.winner-summary {
  border-left: 4px solid var(--success);
}

.winner-summary-title {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.winner-badge.tie {
  background: var(--bg-surface);
  color: var(--text);
  padding: 2px 10px;
  border-radius: 100px;
  font-size: 0.8125rem;
  font-weight: 600;
}

.summary-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 24px;
  margin-bottom: 12px;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.8125rem;
}

.stat-label {
  color: var(--text-secondary);
}

.stat-value {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.stat-value.visa-text {
  color: #1a1f71;
}

.stat-value.mc-text {
  color: #eb001b;
}

@media (prefers-color-scheme: dark) {
  .stat-value.visa-text {
    color: #7b8aff;
  }
  .stat-value.mc-text {
    color: #ff6b6b;
  }
}

.summary-note {
  font-size: 0.75rem;
  color: var(--text-secondary);
  line-height: 1.5;
  margin-top: 8px;
}

.summary-note .better-dot::before {
  content: '●';
  color: #0d904f;
  margin-right: 2px;
}

/* History link button in main header */
.header-btn-text {
  background: rgba(255,255,255,0.15);
  border: none;
  color: #fff;
  height: 36px;
  padding: 0 12px;
  border-radius: 18px;
  cursor: pointer;
  font-size: 0.8125rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: background var(--transition);
  text-decoration: none;
}

.header-btn-text:hover {
  background: rgba(255,255,255,0.25);
}

/* Responsive adjustments for history page */
@media (max-width: 480px) {
  .history-controls {
    flex-direction: column;
    gap: 12px;
  }

  .history-controls .form-group {
    min-width: 100%;
  }

  .chart-container {
    height: 260px;
  }

  .summary-stats {
    grid-template-columns: 1fr;
  }
}
