:root {
  --gold: #e8c547;
  --gold-light: #f8e9a8;
  --gold-dark: #b8941e;
  --gold-glow: rgba(232, 197, 71, 0.35);
  --green: #2dd4a0;
  --red: #f07178;
  --bg: #0b0a09;
  --bg-secondary: #141210;
  --card: #16140f;
  --text: #faf6eb;
  --text-soft: #b5a98c;
  --border: rgba(232, 197, 71, 0.18);
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.45);
  --radius: 16px;
}

[data-theme="light"] {
  --gold: #c9a227;
  --gold-light: #e8d48a;
  --gold-dark: #9a7b18;
  --gold-glow: rgba(201, 162, 39, 0.25);
  --green: #0d9f6e;
  --red: #d64545;
  --bg: #f7f3e8;
  --bg-secondary: #efe9d8;
  --card: #fffdf7;
  --text: #1f1a10;
  --text-soft: #7a6f58;
  --border: rgba(180, 145, 43, 0.28);
  --shadow: 0 8px 28px rgba(88, 67, 12, 0.08);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: "Vazirmatn", "Segoe UI", Tahoma, sans-serif;
  background:
    radial-gradient(ellipse 120% 80% at 50% -20%, rgba(232, 197, 71, 0.09), transparent 50%),
    var(--bg);
  color: var(--text);
  line-height: 1.65;
  min-height: 100vh;
  padding-bottom: 88px;
  transition: background-color 0.35s, color 0.35s;
  -webkit-font-smoothing: antialiased;
}
button, input { font-family: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.container {
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding: 0 16px;
}

/* Header */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: linear-gradient(180deg, var(--bg) 65%, transparent);
  padding: calc(12px + env(safe-area-inset-top, 0px)) 0 10px;
  backdrop-filter: blur(12px);
}
.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo {
  width: 44px; height: 44px; border-radius: 12px;
  object-fit: contain;
  background: linear-gradient(145deg, #1a1814, #0d0c0a);
  border: 1px solid var(--border);
  padding: 3px;
}
.brand-text h1 {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--gold-light), var(--gold), var(--gold-dark));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.brand-text p {
  margin: 2px 0 0;
  color: var(--text-soft);
  font-size: 0.72rem;
  font-weight: 500;
}
.header-actions { display: flex; align-items: center; gap: 6px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--gold);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; font-size: 1rem;
}
.lang-select {
  appearance: none;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--gold);
  border-radius: 12px;
  padding: 8px 10px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
}

/* Cards */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.section-title {
  margin: 22px 0 12px;
  padding-inline-start: 14px;
  font-size: 1.08rem;
  font-weight: 700;
  border-inline-start: 3px solid var(--gold);
}

/* Chart */
.chart-card { padding: 14px; overflow: hidden; }
.chart-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 10px; flex-wrap: wrap; gap: 8px;
}
.chart-header h3 { margin: 0; font-size: 0.95rem; font-weight: 700; }
.tv-wrap {
  height: 280px;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border);
}

/* Manual price */
.manual-price-box { margin-top: 14px; }
.manual-price-hint {
  margin: 0 0 10px;
  text-align: center;
  font-size: 0.88rem;
  color: var(--gold);
  font-weight: 600;
}
.manual-price-wrap {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-secondary);
  border: 1.5px solid var(--gold);
  border-radius: 14px;
  padding: 12px 16px;
  max-width: 300px;
  margin: 0 auto;
  box-shadow: 0 0 24px var(--gold-glow);
}
.manual-price-wrap .currency-sign {
  color: var(--gold);
  font-size: 1.4rem;
  font-weight: 700;
}
#manualGoldPrice {
  width: 100%;
  border: none; outline: none;
  background: transparent;
  color: var(--gold);
  font-size: 1.5rem;
  font-weight: 700;
  direction: ltr;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
#manualGoldPrice::-webkit-outer-spin-button,
#manualGoldPrice::-webkit-inner-spin-button { -webkit-appearance: none; }
.price-note {
  margin: 8px 0 0;
  text-align: center;
  font-size: 0.72rem;
  color: var(--text-soft);
}

/* API controls */
.api-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 12px;
  padding: 10px 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  max-width: 340px;
  margin-left: auto;
  margin-right: auto;
}
.api-toggle-wrap {
  display: flex; align-items: center; gap: 8px;
  font-size: 0.82rem; color: var(--text-soft); font-weight: 500;
}
.api-toggle { position: relative; width: 44px; height: 24px; flex-shrink: 0; }
.api-toggle input { opacity: 0; width: 0; height: 0; }
.api-toggle .slider {
  position: absolute; cursor: pointer; inset: 0;
  background: #444; border-radius: 24px; transition: 0.25s;
}
.api-toggle .slider::before {
  content: "";
  position: absolute; height: 18px; width: 18px;
  left: 3px; bottom: 3px;
  background: #fff; border-radius: 50%; transition: 0.25s;
}
.api-toggle input:checked + .slider { background: var(--gold); }
.api-toggle input:checked + .slider::before { transform: translateX(20px); }
.api-quota {
  text-align: start;
  font-size: 0.78rem;
  color: var(--text-soft);
  line-height: 1.4;
}
.api-quota strong { color: var(--gold); font-variant-numeric: tabular-nums; }
.api-quota .quota-warn { color: var(--red); }

/* Inputs & calc */
.input-wrap {
  display: flex; align-items: center; gap: 6px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  min-width: 0;
}
.input-wrap:focus-within { border-color: var(--gold); }
.input-wrap .currency-sign { color: var(--gold); font-weight: 700; flex-shrink: 0; }
.input-wrap input {
  flex: 1 1 0%;
  min-width: 0;
  width: 100%;
  border: none; outline: none;
  background: transparent; color: var(--text);
  font-size: 1rem; direction: ltr; text-align: right;
  font-variant-numeric: tabular-nums;
  box-sizing: border-box;
}
.kabul-grid, .calc-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}
.calc-card h3 {
  margin: 0 0 8px;
  font-size: 0.85rem;
  color: var(--text-soft);
  font-weight: 600;
}
.calc-value {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
  color: var(--gold);
  font-variant-numeric: tabular-nums;
}
.calc-note {
  margin: 6px 0 0;
  font-size: 0.72rem;
  color: var(--text-soft);
}
.calc-card-diff.diff-positive .calc-value { color: var(--green); }
.calc-card-diff.diff-negative .calc-value { color: var(--red); }

.weight-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}
.weight-grid > div {
  min-width: 0; /* critical: prevent grid blowout on mobile */
  overflow: hidden;
}
.weight-grid label {
  display: block;
  font-size: 0.78rem;
  color: var(--text-soft);
  margin-bottom: 5px;
}
.weight-grid .input-wrap {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}
.weight-grid input {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
}

/* Activation */
.activation-row {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
}
.activation-row .input-wrap { flex: 1; min-width: 140px; }
.btn-primary {
  background: linear-gradient(135deg, var(--gold), var(--gold-dark));
  color: #1a1508;
  border: none;
  border-radius: 12px;
  padding: 12px 18px;
  font-weight: 700;
  cursor: pointer;
  font-size: 0.9rem;
}
.activation-status {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  margin-top: 10px;
}
.activation-status.active { background: rgba(45, 212, 160, 0.15); color: var(--green); }
.activation-status.inactive { background: rgba(240, 113, 120, 0.12); color: var(--red); }

/* Bottom nav */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 100;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom, 0px));
}
.bottom-nav-inner {
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  justify-content: space-around;
}
.nav-item {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: none; border: none;
  color: var(--text-soft);
  cursor: pointer;
  font-size: 0.68rem;
  font-weight: 500;
  padding: 6px 10px;
  min-width: 56px;
  border-radius: 12px;
}
.nav-item .ni { font-size: 1.25rem; }
.nav-item.active {
  color: var(--gold);
  background: rgba(232, 197, 71, 0.1);
}

@media (max-width: 420px) {
  .tv-wrap { height: 240px; }
  .brand-text h1 { font-size: 1.1rem; }
  .weight-grid { grid-template-columns: 1fr 1fr; gap: 8px; }
  .weight-grid label { font-size: 0.72rem; }
  .calc-grid { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 340px) {
  .weight-grid { grid-template-columns: 1fr; }
}


/* Autofill / validation: prevent browser yellow/red "wrong field" highlight */
input:-webkit-autofill,
input:-webkit-autofill:hover,
input:-webkit-autofill:focus,
input:-webkit-autofill:active {
  -webkit-box-shadow: 0 0 0 40px var(--card) inset !important;
  -webkit-text-fill-color: var(--text) !important;
  caret-color: var(--text);
  transition: background-color 99999s ease-in-out 0s;
}
input:invalid,
input:user-invalid {
  box-shadow: none;
  outline: none;
  border-color: inherit;
}
.input-wrap input:invalid,
.input-wrap input:user-invalid {
  box-shadow: none;
  outline: none;
}
.input-wrap:has(input:invalid),
.input-wrap:has(input:user-invalid) {
  border-color: var(--border);
}
.input-wrap:has(input:focus) {
  border-color: var(--gold);
}
