:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f4f6fa);
  --text: var(--tg-theme-text-color, #16181f);
  --hint: var(--tg-theme-hint-color, #8a8f9c);
  --link: var(--tg-theme-link-color, #2481cc);
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --border: rgba(0,0,0,0.08);
  --accent-2: #6a5cff;
  --grant: #0aa15c;
  --contract: #e08a1e;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 16px;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.screen { display: none; }
.screen.active { display: block; animation: fadeIn 0.2s ease; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.page-title {
  font-size: 21px;
  font-weight: 800;
  margin: 4px 0 16px;
  letter-spacing: -0.3px;
}

/* ===== Asosiy menyu kartochkalari ===== */
.menu-card {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--secondary-bg);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 12px;
  cursor: pointer;
  border: none;
  width: 100%;
  text-align: left;
  color: var(--text);
  font-size: 16px;
  font-family: inherit;
}
.menu-card:active { opacity: 0.7; }
.menu-card .icon { font-size: 28px; }
.menu-card .label { flex: 1; font-weight: 600; }
.menu-card .arrow { color: var(--hint); font-size: 18px; }
.menu-card.disabled { opacity: 0.45; pointer-events: none; }
.menu-card .sub {
  display: block;
  font-size: 12px;
  color: var(--hint);
  font-weight: 400;
  margin-top: 2px;
}

/* ===== Forma maydonlari (umumiy) ===== */
.field { margin-bottom: 12px; }
.field label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--hint);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

/* ---- Custom dropdown (select o'rniga) - to'liq nazorat qilinadigan uslub ---- */
.custom-select { position: relative; }

.custom-select-trigger {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 13px 14px;
  border-radius: 14px;
  border: 2px solid transparent;
  border-left: 4px solid var(--select-accent, var(--link));
  background: var(--secondary-bg);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  text-align: left;
}
.custom-select-trigger .custom-select-value {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.custom-select-trigger .custom-select-value.placeholder { color: var(--hint); font-weight: 500; }

.custom-select-trigger .custom-select-arrow {
  width: 10px;
  height: 10px;
  border-right: 2.5px solid var(--link);
  border-bottom: 2.5px solid var(--link);
  transform: rotate(45deg);
  transition: transform 0.2s;
  flex-shrink: 0;
}
.custom-select.open .custom-select-trigger .custom-select-arrow { transform: rotate(-135deg); }
.custom-select.open .custom-select-trigger { border-color: var(--link); background: var(--bg); }

.custom-select-trigger.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  pointer-events: none;
}

.custom-select-dropdown {
  position: absolute;
  left: 0; right: 0;
  top: calc(100% + 6px);
  background: var(--bg);
  border-radius: 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.18);
  border: 1px solid var(--border);
  max-height: 260px;
  overflow-y: auto;
  z-index: 50;
  display: none;
  padding: 6px;
}
.custom-select.open .custom-select-dropdown { display: block; }

.custom-select-option {
  padding: 11px 12px;
  border-radius: 10px;
  font-size: 14.5px;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  transition: background 0.1s;
}
.custom-select-option:active { background: var(--secondary-bg); }
.custom-select-option.selected {
  background: rgba(36, 129, 204, 0.12);
  color: var(--link);
}
.custom-select-empty {
  padding: 14px 12px;
  color: var(--hint);
  font-size: 13.5px;
  text-align: center;
}

/* Har bir dropdown alohida rang - ko'zga tez tashlanishi uchun */
#field-region { --select-accent: #ff6b6b; }
#field-university { --select-accent: #4dabf7; }
#field-edtype { --select-accent: #51cf66; }
#field-edlang { --select-accent: #cc5de8; }

#field-elig-edtype { --select-accent: #51cf66; }
#field-elig-edlang { --select-accent: #cc5de8; }
#field-elig-region { --select-accent: #ff6b6b; }
#field-elig-university { --select-accent: #4dabf7; }

/* Natija qatorida OTM+Hudud ko'rsatiladigan qo'shimcha qator */
.direction-university {
  font-size: 10.5px;
  color: var(--link);
  font-weight: 700;
  margin-top: 2px;
}

/* ===== "Ballim bilan qayerga kira olaman" - maxsus komponentlar ===== */
.summary-banner {
  background: linear-gradient(135deg, var(--link), var(--accent-2));
  border-radius: 16px;
  padding: 16px;
  color: #fff;
  margin-bottom: 16px;
}
.summary-banner .score-label { font-size: 12px; opacity: 0.85; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; }
.summary-banner .score-value { font-size: 28px; font-weight: 800; margin-top: 2px; }
.summary-banner .score-sub { font-size: 12.5px; opacity: 0.9; margin-top: 6px; }

.best-match-card {
  background: #fff8ea;
  border: 1.5px solid #ffd875;
  border-radius: 16px;
  padding: 14px;
  margin-bottom: 16px;
}
.best-match-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: #ffb703;
  color: #4a3200;
  font-size: 10.5px;
  font-weight: 800;
  padding: 3px 9px;
  border-radius: 100px;
  margin-bottom: 8px;
}
.best-match-name { font-size: 15px; font-weight: 800; color: var(--text); }
.best-match-uni { font-size: 12px; color: var(--link); font-weight: 700; margin-top: 2px; }
.best-match-loc { font-size: 11px; color: var(--hint); margin-top: 1px; }
.best-match-scores { display: flex; gap: 10px; margin-top: 10px; }
.best-match-score-box { flex: 1; background: var(--bg); border-radius: 10px; padding: 8px 10px; }
.best-match-score-box .k { font-size: 10px; color: var(--hint); font-weight: 700; text-transform: uppercase; }
.best-match-score-box .v { font-size: 16px; font-weight: 800; margin-top: 2px; }

.eligible-list-title { font-size: 13px; font-weight: 700; color: var(--hint); margin-bottom: 10px; }

.result-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.result-info { flex: 1; min-width: 0; }
.result-name { font-size: 13.5px; font-weight: 700; color: var(--text); line-height: 1.3; }
.result-uni { font-size: 11px; color: var(--link); font-weight: 600; margin-top: 2px; }
.result-loc { font-size: 10.5px; color: var(--hint); margin-top: 1px; }

.margin-badge { flex-shrink: 0; text-align: center; padding: 8px 10px; border-radius: 12px; min-width: 64px; }
.margin-badge .real-score { font-size: 12px; font-weight: 700; color: var(--hint); }
.margin-badge .diff { font-size: 15px; font-weight: 800; margin-top: 2px; }
.margin-badge .label { font-size: 9px; font-weight: 700; text-transform: uppercase; margin-top: 1px; }
.margin-badge.safe { background: #e9f9f0; }
.margin-badge.safe .diff, .margin-badge.safe .label { color: var(--grant); }
.margin-badge.risky { background: #fef3e6; }
.margin-badge.risky .diff, .margin-badge.risky .label { color: var(--contract); }

/* ---- Matn kiritish maydonlari (natija ekrani) ---- */
.text-input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--border);
  background: var(--secondary-bg);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  transition: border-color 0.15s, background 0.15s;
}
.text-input.uppercase { text-transform: uppercase; }
.text-input:focus { outline: none; border-color: var(--link); }
.text-input.is-valid { border-color: var(--grant); background: #f0fdf6; }
.text-input.is-invalid { border-color: #e53935; background: #fff5f5; }

.field-error-msg {
  color: #e53935;
  font-size: 12.5px;
  margin-top: 5px;
  display: none;
}
.field-error-msg.show { display: block; }

.captcha-row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.captcha-row img {
  height: 44px;
  border-radius: 8px;
  background: var(--secondary-bg);
  flex: 1;
  object-fit: contain;
}
.captcha-refresh-btn {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--secondary-bg);
  font-size: 18px;
  cursor: pointer;
  flex-shrink: 0;
}

/* ---- Asosiy tugma - gradient + soya bilan jonli ko'rinish ---- */
.btn-primary {
  width: 100%;
  padding: 15px;
  border-radius: 14px;
  border: none;
  background: linear-gradient(135deg, var(--button), var(--accent-2));
  color: var(--button-text);
  font-size: 16px;
  font-weight: 800;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 8px 20px rgba(36, 129, 204, 0.3);
  transition: transform 0.1s, box-shadow 0.1s, opacity 0.15s;
}
.btn-primary:disabled { opacity: 0.5; box-shadow: none; }
.btn-primary:active:not(:disabled) { transform: scale(0.98); box-shadow: 0 4px 10px rgba(36, 129, 204, 0.25); }

/* ===== Natijalar sarlavhasi + sort tugmasi ===== */
.results-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 20px 0 10px;
}
.results-count { font-size: 13px; font-weight: 700; color: var(--hint); }
.sort-toggle {
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--secondary-bg);
  border: none;
  border-radius: 100px;
  padding: 7px 12px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--link);
  cursor: pointer;
  font-family: inherit;
}
.sort-toggle svg { width: 13px; height: 13px; transition: transform 0.15s; }
.sort-toggle svg.flipped { transform: scaleY(-1); }

/* ===== Natijalar jadvali ===== */
.results-table {
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--border);
  box-shadow: 0 2px 10px rgba(0,0,0,0.04);
}
.table-head {
  display: grid;
  grid-template-columns: 1fr 52px 52px;
  gap: 4px;
  padding: 11px 12px;
  background: var(--secondary-bg);
  font-size: 10.5px;
  font-weight: 800;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.table-head .col-score { text-align: right; }

.result-row {
  display: grid;
  grid-template-columns: 1fr 52px 52px;
  gap: 4px;
  align-items: center;
  padding: 11px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg);
  position: relative;
}
.result-row:nth-child(even) { background: var(--secondary-bg); }
.result-row.rank-1::before,
.result-row.rank-2::before,
.result-row.rank-3::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
}
.result-row.rank-1::before { background: #ffb703; }
.result-row.rank-2::before { background: #adb5bd; }
.result-row.rank-3::before { background: #cd8b60; }

.direction-cell { min-width: 0; }
.direction-name {
  font-size: 13.5px;
  font-weight: 700;
  line-height: 1.3;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.direction-code {
  font-size: 10.5px;
  color: var(--hint);
  font-variant-numeric: tabular-nums;
  margin-top: 1px;
  font-weight: 600;
}

.score-cell {
  text-align: right;
  font-size: 13.5px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.score-cell.grant { color: var(--grant); }
.score-cell.contract { color: var(--contract); }
.score-cell.zero { color: var(--hint); font-weight: 600; }

.legend { display: flex; gap: 14px; margin-top: 12px; padding: 0 4px; }
.legend-item { display: flex; align-items: center; gap: 5px; font-size: 11px; font-weight: 600; color: var(--hint); }
.legend-dot { width: 8px; height: 8px; border-radius: 50%; }
.legend-dot.grant { background: var(--grant); }
.legend-dot.contract { background: var(--contract); }

.empty-state { text-align: center; color: var(--hint); padding: 40px 16px; font-size: 14px; }
.loading { text-align: center; color: var(--hint); padding: 24px; font-size: 14px; }

/* ===== Natija muvaffaqiyatli yuborildi - tik belgi bilan ===== */
.natija-success {
  text-align: center;
  padding: 32px 16px;
  animation: natijaPopIn 0.35s ease;
}
@keyframes natijaPopIn {
  0% { opacity: 0; transform: scale(0.85); }
  100% { opacity: 1; transform: scale(1); }
}
.natija-success-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto 16px;
  border-radius: 50%;
  background: var(--grant);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
  color: #fff;
  box-shadow: 0 8px 20px rgba(10, 161, 92, 0.35);
}
.natija-success-title {
  font-size: 16px;
  font-weight: 800;
  color: var(--text);
  margin-bottom: 6px;
}
.natija-success-sub {
  font-size: 13px;
  color: var(--hint);
  line-height: 1.5;
}

/* ===== Maxfiylik bildirishnomasi (asosiy menyu ostida) ===== */
.privacy-note {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  background: var(--secondary-bg);
  border-radius: 14px;
  padding: 14px 16px;
  margin-top: 8px;
}
.privacy-note .icon { font-size: 18px; flex-shrink: 0; line-height: 1.3; }
.privacy-note .text {
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--hint);
}
.privacy-note .text b { color: var(--text); font-weight: 700; }

/* ===== Boshlang'ich yuklanish ekrani ===== */
.loading-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 70vh;
  gap: 18px;
}
.loading-spinner {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 3.5px solid var(--secondary-bg);
  border-top-color: var(--link);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-bot-name {
  font-size: 15px;
  font-weight: 700;
  color: var(--hint);
}