:root {
  color-scheme: dark;
  --bg: #0b0f17;
  --bg-elev: #0f1522;
  --surface: rgba(20, 27, 45, 0.7);
  --surface-solid: #131a2b;
  --surface-strong: rgba(20, 27, 45, 0.9);
  --border: rgba(148, 163, 184, 0.12);
  --border-strong: rgba(148, 163, 184, 0.22);
  --text: #e6edf3;
  --text-dim: #94a3b8;
  --text-mute: #64748b;

  --accent: #22c55e;
  --accent-2: #10b981;
  --accent-soft: rgba(34, 197, 94, 0.12);
  --accent-ring: rgba(34, 197, 94, 0.3);

  --gold: #f59e0b;
  --link: #60a5fa;
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.1);

  --radius-sm: 10px;
  --radius: 16px;
  --radius-lg: 22px;

  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.2);
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  --shadow-glow: 0 8px 30px rgba(34, 197, 94, 0.18);

  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', Consolas, monospace;

  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  min-height: 100dvh;
  background: var(--bg);
  position: relative;
  overflow-x: hidden;
  padding:
    max(20px, env(safe-area-inset-top))
    max(20px, env(safe-area-inset-right))
    max(20px, env(safe-area-inset-bottom))
    max(20px, env(safe-area-inset-left));
  display: flex;
  justify-content: center;
}

.bg-grid,
.bg-glow {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
}

.bg-grid {
  background-image:
    linear-gradient(rgba(148, 163, 184, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(148, 163, 184, 0.04) 1px, transparent 1px);
  background-size: 48px 48px;
  mask-image: radial-gradient(ellipse at 50% 0%, rgba(0, 0, 0, 0.6), transparent 70%);
  -webkit-mask-image: radial-gradient(ellipse at 50% 0%, rgba(0, 0, 0, 0.6), transparent 70%);
}

.bg-glow {
  background:
    radial-gradient(600px 400px at 15% 0%, rgba(34, 197, 94, 0.18), transparent 60%),
    radial-gradient(500px 400px at 100% 30%, rgba(96, 165, 250, 0.1), transparent 55%),
    radial-gradient(700px 500px at 50% 100%, rgba(245, 158, 11, 0.06), transparent 60%);
}

.app {
  width: 100%;
  max-width: 520px;
  position: relative;
  z-index: 1;
  display: grid;
  gap: 18px;
  padding: 8px 0 24px;
}

/* ─── Header ─────────────────────────────── */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 2px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-logo {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: var(--shadow-glow);
  display: grid;
  place-items: center;
  color: #06140c;
  font-weight: 800;
  font-size: 1.3rem;
  flex-shrink: 0;
}

.brand-title {
  font-weight: 700;
  font-size: 1.02rem;
  letter-spacing: -0.01em;
}

.brand-sub {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-top: 1px;
}

.rate-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.18s ease, background 0.18s ease, border-color 0.18s ease;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.rate-badge:hover:not(:disabled) {
  transform: translateY(-1px);
  border-color: var(--accent-ring);
  background: var(--surface-strong);
}

.rate-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.18);
  animation: pulse 2s ease-in-out infinite;
}

.rate-badge.loading .rate-dot { animation: pulse-fast 1s ease-in-out infinite; background: var(--gold); box-shadow: 0 0 0 4px rgba(245, 158, 11, 0.2); }
.rate-badge.error .rate-dot { animation: none; background: var(--danger); box-shadow: 0 0 0 4px rgba(239, 68, 68, 0.2); }

.rate-label {
  color: var(--text-dim);
  font-weight: 500;
}

.rate-price {
  color: var(--text);
  letter-spacing: -0.01em;
}

.rate-refresh {
  color: var(--text-dim);
  font-size: 0.9rem;
  transition: transform 0.4s ease;
}

.rate-badge.loading .rate-refresh {
  animation: spin 0.9s linear infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}
@keyframes pulse-fast {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.35); }
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ─── Hero Split ─────────────────────────── */
.hero-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.hero-card {
  position: relative;
  padding: 22px 20px;
  border-radius: var(--radius-lg);
  background: linear-gradient(180deg, rgba(19, 26, 43, 0.95), rgba(11, 15, 23, 0.95));
  border: 1px solid var(--border-strong);
  box-shadow: var(--shadow);
  overflow: hidden;
  min-width: 0;
}

.hero-card.buyer {
  border-color: rgba(34, 197, 94, 0.35);
  background:
    radial-gradient(120% 100% at 0% 0%, rgba(34, 197, 94, 0.18), transparent 55%),
    linear-gradient(180deg, rgba(19, 26, 43, 0.95), rgba(11, 15, 23, 0.95));
}

.hero-card.agent {
  border-color: rgba(245, 158, 11, 0.32);
  background:
    radial-gradient(120% 100% at 0% 0%, rgba(245, 158, 11, 0.16), transparent 55%),
    linear-gradient(180deg, rgba(19, 26, 43, 0.95), rgba(11, 15, 23, 0.95));
}

.hero-card::after {
  content: '';
  position: absolute;
  top: -40%;
  right: -30%;
  width: 220px;
  height: 220px;
  border-radius: 50%;
  pointer-events: none;
  filter: blur(10px);
}

.hero-card.buyer::after {
  background: radial-gradient(circle, rgba(34, 197, 94, 0.18), transparent 65%);
}

.hero-card.agent::after {
  background: radial-gradient(circle, rgba(245, 158, 11, 0.16), transparent 65%);
}

.hero-tag {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--text-dim);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin-bottom: 10px;
  position: relative;
  z-index: 1;
}

.hero-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
}

.hero-card.buyer .hero-dot {
  background: var(--accent);
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.18);
}

.hero-card.agent .hero-dot {
  background: var(--gold);
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.18);
}

.hero-amount {
  display: flex;
  align-items: baseline;
  gap: 6px;
  flex-wrap: wrap;
  position: relative;
  z-index: 1;
}

.hero-value {
  font-family: var(--font-mono);
  font-size: clamp(1.75rem, 6.5vw, 2.4rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1;
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
  transition: opacity 0.2s ease;
}

.hero-card.buyer .hero-value {
  background-image: linear-gradient(180deg, #f0fdf4, #86efac);
}

.hero-card.agent .hero-value {
  background-image: linear-gradient(180deg, #fffbeb, #fcd34d);
}

.hero-value.flash {
  animation: flash 0.4s ease;
}
@keyframes flash {
  0% { transform: scale(1); }
  40% { transform: scale(1.03); }
  100% { transform: scale(1); }
}

.hero-unit {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  font-weight: 600;
}

.hero-card.buyer .hero-unit { color: var(--accent); }
.hero-card.agent .hero-unit { color: var(--gold); }

.hero-sub {
  margin-top: 10px;
  color: var(--text-dim);
  font-size: 0.78rem;
  font-family: var(--font-mono);
  min-height: 1.2em;
  position: relative;
  z-index: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ─── Form Card ──────────────────────────── */
.form-card {
  padding: 20px;
  border-radius: var(--radius-lg);
  background: var(--surface);
  border: 1px solid var(--border);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: var(--shadow-sm);
  display: grid;
  gap: 18px;
}

.field {
  display: grid;
  gap: 10px;
  min-width: 0;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.field-label {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 0.88rem;
}

.field-label > span:first-child {
  font-weight: 600;
  color: var(--text);
}

.field-hint {
  color: var(--text-mute);
  font-size: 0.78rem;
}

.input-wrap {
  position: relative;
  display: flex;
  align-items: center;
  background: rgba(3, 7, 14, 0.6);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.input-wrap:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-soft);
  background: rgba(3, 7, 14, 0.85);
}

.input-wrap input {
  flex: 1;
  min-width: 0;
  width: 100%;
  padding: 14px 14px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 1.05rem;
  font-family: var(--font-mono);
  font-weight: 600;
  letter-spacing: -0.01em;
  outline: none;
}

.input-wrap input::placeholder {
  color: var(--text-mute);
  font-weight: 500;
}

.input-prefix,
.input-suffix {
  padding: 0 4px 0 14px;
  color: var(--text-dim);
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 1.05rem;
  pointer-events: none;
}

.input-suffix {
  padding: 0 14px 0 4px;
}

.input-action {
  margin-right: 6px;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: none;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.15s ease, transform 0.2s ease;
}

.input-action:hover:not(:disabled) {
  background: rgba(34, 197, 94, 0.22);
}

.input-action.loading {
  pointer-events: none;
}
.input-action.loading #fetchRateIcon {
  display: inline-block;
  animation: spin 0.9s linear infinite;
}

.preset-chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  padding-bottom: 2px;
  margin: 0 -2px;
}

.preset-chips::-webkit-scrollbar { display: none; }

.chip {
  flex-shrink: 0;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.6);
  color: var(--text-dim);
  font-size: 0.82rem;
  font-weight: 600;
  font-family: var(--font-mono);
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.chip:hover {
  border-color: var(--accent-ring);
  color: var(--text);
  background: var(--accent-soft);
}

.chip.active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

.form-error {
  margin: 0;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: var(--danger);
  font-size: 0.88rem;
}

/* ─── Breakdown ──────────────────────────── */
.breakdown-card {
  padding: 20px;
  border-radius: var(--radius-lg);
  background: var(--surface);
  border: 1px solid var(--border);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

.breakdown-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.breakdown-header h2 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 0.02em;
}

.ghost-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-dim);
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.ghost-btn:hover {
  color: var(--text);
  border-color: var(--border-strong);
  background: var(--surface);
}

.ghost-btn.success {
  color: var(--accent);
  border-color: var(--accent-ring);
  background: var(--accent-soft);
}

.copy-icon {
  font-size: 0.95rem;
}

.breakdown-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 2px;
}

.breakdown-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 12px 2px;
  border-bottom: 1px solid var(--border);
  font-size: 0.92rem;
}

.breakdown-row:last-child {
  border-bottom: none;
}

.breakdown-row.placeholder {
  justify-content: center;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-mute);
  padding: 22px 12px;
  font-size: 0.88rem;
}

.breakdown-row.net {
  padding: 14px 14px;
  margin-top: 6px;
  border: 1px solid var(--accent-ring);
  border-radius: var(--radius);
  background: linear-gradient(90deg, var(--accent-soft), transparent);
}

.breakdown-label {
  color: var(--text-dim);
  flex-shrink: 0;
}

.breakdown-row.net .breakdown-label {
  color: var(--accent);
  font-weight: 600;
}

.breakdown-value {
  color: var(--text);
  font-family: var(--font-mono);
  font-weight: 600;
  text-align: right;
  letter-spacing: -0.01em;
}

.breakdown-row.net .breakdown-value {
  color: #f0fdf4;
  font-size: 1.05rem;
}

.breakdown-formula {
  color: var(--text-mute);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  margin-left: 8px;
  font-weight: 500;
}

/* ─── Footer ─────────────────────────────── */
.app-footer {
  text-align: center;
  padding: 8px 12px 0;
  color: var(--text-mute);
  font-size: 0.78rem;
  line-height: 1.5;
}

/* ─── Mobile ─────────────────────────────── */
@media (max-width: 480px) {
  .app { gap: 14px; padding-top: 4px; }
  .brand-logo { width: 38px; height: 38px; font-size: 1.2rem; }
  .brand-title { font-size: 0.95rem; }
  .brand-sub { font-size: 0.72rem; }
  .rate-badge { padding: 7px 10px; font-size: 0.78rem; gap: 6px; }
  .rate-label { display: none; }

  .hero-split { grid-template-columns: 1fr; gap: 10px; }
  .hero-card { padding: 18px 18px; }
  .hero-tag { font-size: 0.7rem; }
  .hero-value { font-size: clamp(2rem, 10vw, 2.6rem); }
  .form-card, .breakdown-card { padding: 18px 16px; }

  .field-row { gap: 10px; }
  .input-wrap input { font-size: 1rem; padding: 13px 12px; }
  .input-prefix, .input-suffix { font-size: 1rem; padding-left: 12px; padding-right: 4px; }
  .input-suffix { padding-left: 4px; padding-right: 12px; }

  .breakdown-row { font-size: 0.88rem; flex-wrap: wrap; }
  .breakdown-formula { width: 100%; margin-left: 0; margin-top: 2px; }
}

/* ─── Tap optimization ──────────────────── */
@media (hover: none) {
  button, input, .chip { -webkit-tap-highlight-color: transparent; }
}

/* ─── Reduced motion ────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
