/* Colours come from the Telegram theme; the fallbacks follow iOS system colours
   and are only used outside Telegram. */
:root {
  --fb-bg: #ffffff;
  --fb-bg2: #f2f2f7;
  --fb-text: #000000;
  --fb-hint: #8e8e93;
  --fb-accent: #007aff;
  --fb-sep: rgba(60, 60, 67, .18);
  --fb-fill: rgba(118, 118, 128, .12);

  --bg: var(--tg-theme-bg-color, var(--fb-bg));
  --bg2: var(--tg-theme-secondary-bg-color, var(--fb-bg2));
  --section: var(--tg-theme-section-bg-color, var(--bg));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --accent: var(--tg-theme-button-color, var(--fb-accent));
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, var(--accent));
  --sep: var(--tg-theme-section-separator-color, var(--fb-sep));
  --danger: var(--tg-theme-destructive-text-color, #ff3b30);
  --fill: var(--fb-fill);
  --tint: color-mix(in srgb, var(--accent) 12%, transparent);
  --ok: #34c759;

  --safe-bottom: max(env(safe-area-inset-bottom), var(--tg-safe-area-inset-bottom, 0px));
  --tabbar-h: 56px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --fb-bg: #1c1c1e;
  --fb-bg2: #000000;
  --fb-text: #ffffff;
  --fb-hint: #8e8e93;
  --fb-accent: #0a84ff;
  --fb-sep: rgba(84, 84, 88, .55);
  --fb-fill: rgba(118, 118, 128, .24);
  --ok: #30d158;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
  overflow-x: hidden;
  padding-bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 8px);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--hint); }

.view { display: none; max-width: 720px; margin: 0 auto; padding: 0 16px; overflow-x: clip; }
.view.is-active { display: block; }

/* ---------- Header ---------- */
.top { padding: 16px 0 4px; }
.top h1 { margin: 0; font-size: 30px; font-weight: 700; letter-spacing: -.02em; line-height: 1.15; }
.lead { margin: 2px 0 0; color: var(--hint); font-size: 15px; }

.account {
  display: flex; align-items: center; gap: 12px; margin: 12px 0 0; padding: 10px 12px;
  border-radius: 12px; background: var(--bg2); font-size: 15px;
}
.account-text { flex: 1; min-width: 0; }
.account-text b { display: block; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-text small { color: var(--hint); font-size: 13px; }
.account button { flex: none; border: 0; background: none; color: var(--link); font-size: 15px; cursor: pointer; padding: 6px 0; }

/* ---------- Search + categories ---------- */
.toolbar { position: sticky; top: 0; z-index: 5; margin: 0 -16px; padding: 10px 16px 8px; background: var(--bg); }
.search {
  display: flex; align-items: center; gap: 6px; height: 38px; padding: 0 4px 0 10px;
  border-radius: 10px; background: var(--fill);
}
.search svg { width: 17px; height: 17px; fill: var(--hint); flex: none; }
.search input {
  flex: 1; min-width: 0; height: 100%; border: 0; outline: 0; background: none;
  color: var(--text); font: inherit; font-size: 16px;
}
.search input::placeholder { color: var(--hint); }
.search input::-webkit-search-cancel-button { display: none; }
.icon-btn {
  width: 28px; height: 28px; border: 0; border-radius: 50%; background: none; color: var(--hint);
  font-size: 20px; line-height: 1; cursor: pointer;
}

.chips {
  display: flex; gap: 8px; overflow-x: auto; margin: 10px -16px 0; padding: 0 16px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; height: 32px; padding: 0 12px; border: 0; border-radius: 16px;
  background: var(--fill); font-size: 14px; font-weight: 500; white-space: nowrap; cursor: pointer;
}
.chip small { margin-left: 2px; color: var(--hint); font-size: 13px; font-weight: 400; }
.chip.is-active { background: var(--accent); color: var(--accent-text); }
.chip.is-active small { color: inherit; opacity: .75; }

/* ---------- App rows ---------- */
.list { display: flex; flex-direction: column; }
.group-title {
  margin: 20px 0 2px; font-size: 20px; font-weight: 700; letter-spacing: -.01em;
}
.app {
  position: relative; display: flex; align-items: center; gap: 12px; width: 100%; min-width: 0;
  padding: 10px 0; border: 0; background: none; text-align: left; cursor: pointer;
  content-visibility: auto; contain-intrinsic-size: auto 76px;
}
.app::after {
  content: ""; position: absolute; left: 72px; right: 0; bottom: 0; height: 1px;
  background: var(--sep); transform: scaleY(.5); transform-origin: bottom;
}
.app:active { opacity: .6; }
.app-text { flex: 1; min-width: 0; }
.app-name, .app-desc, .app-meta {
  display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.app-name { font-size: 16px; font-weight: 500; }
.app-desc { color: var(--hint); font-size: 13px; margin-top: 1px; }
.app-meta { color: var(--hint); font-size: 12px; margin-top: 2px; }
.own-badge { color: var(--ok); font-weight: 600; }
.fav-dot { color: #ff9500; }

.get {
  flex: none; min-width: 84px; height: 30px; padding: 0 14px; border: 0; border-radius: 15px;
  background: var(--tint); color: var(--accent); font-size: 14px; font-weight: 700; cursor: pointer;
}
.get:active { opacity: .6; }
.get.is-off { background: var(--fill); color: var(--hint); }

.app-icon {
  flex: none; position: relative; width: 60px; height: 60px; border-radius: 14px; overflow: hidden;
  display: grid; place-items: center; font-size: 22px; font-weight: 600; color: var(--hint);
  background: var(--fill);
}
.app-icon::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none;
  box-shadow: inset 0 0 0 .5px var(--sep);
}
.app-icon img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.app-icon.lg { width: 96px; height: 96px; border-radius: 22px; font-size: 34px; }

mark { background: none; color: var(--accent); }

.skeleton { height: 60px; margin: 10px 0; border-radius: 14px; background: var(--fill); }

.empty { text-align: center; color: var(--hint); padding: 48px 24px; font-size: 15px; }
.extras { margin-top: 4px; }

/* ---------- Grouped sections (download, help, sheet) ---------- */
.page-head { padding: 16px 0 4px; }
.page-head h2 { margin: 0; font-size: 30px; font-weight: 700; letter-spacing: -.02em; }
.page-head p { margin: 4px 0 0; color: var(--hint); font-size: 15px; }
.section-label {
  margin: 24px 16px 6px; color: var(--hint); font-size: 13px; text-transform: uppercase; letter-spacing: .02em;
}
.group { border-radius: 12px; background: var(--bg2); overflow: hidden; }
.sheet .group, .sheet .field input { background: var(--section); }
.row {
  position: relative; display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 10px 14px;
  color: inherit; text-decoration: none;
}
.row + .row::after {
  content: ""; position: absolute; left: 14px; right: 0; top: 0; height: 1px;
  background: var(--sep); transform: scaleY(.5); transform-origin: top;
}
.steps .row + .row::after { left: 50px; }
.row-text { flex: 1; min-width: 0; }
.row-text b { display: block; font-weight: 400; }
.row-text span, .row-text code { display: block; color: var(--hint); font-size: 13px; margin-top: 1px; }
.row-text code { font: 13px ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.row.is-owned code { color: var(--ok); }
.row .tag { flex: none; color: var(--accent); font-size: 13px; font-weight: 600; }
.chevron { flex: none; width: 8px; height: 8px; border: solid var(--hint); border-width: 0 2px 2px 0; transform: rotate(-45deg); opacity: .6; }
.copy {
  flex: none; height: 30px; padding: 0 10px; border: 0; border-radius: 15px; cursor: pointer;
  background: var(--fill); color: var(--text); font-size: 13px; font-weight: 600;
}
.footnote { margin: 8px 16px 0; color: var(--hint); font-size: 13px; line-height: 1.4; }
.card { padding: 14px; border-radius: 12px; background: var(--bg2); }
.card h3 { margin: 0 0 4px; font-size: 16px; font-weight: 600; }
.card p { margin: 0; color: var(--hint); font-size: 14px; line-height: 1.45; }
.card p + p { margin-top: 8px; }

.steps { counter-reset: s; list-style: none; margin: 0; padding: 0; }
.steps li { counter-increment: s; display: flex; gap: 12px; align-items: flex-start; }
.steps li::before {
  content: counter(s); flex: none; width: 24px; height: 24px; margin-top: 1px; border-radius: 50%;
  display: grid; place-items: center; background: var(--accent); color: var(--accent-text); font-size: 13px; font-weight: 700;
}
.steps li .row-text span { margin-top: 2px; }

.faq details + details { box-shadow: inset 0 .5px 0 var(--sep); }
.faq summary {
  display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 10px 14px;
  list-style: none; cursor: pointer;
}
.faq summary::-webkit-details-marker { display: none; }
.faq summary span { flex: 1; }
.faq details[open] .chevron { transform: rotate(45deg); }
.faq p { margin: 0 14px 12px; color: var(--hint); font-size: 14px; line-height: 1.45; }
.install-only { display: none; }
.can-install .install-only { display: block; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: center;
  height: calc(var(--tabbar-h) + var(--safe-bottom)); padding-bottom: var(--safe-bottom);
  background: var(--bg); box-shadow: 0 -.5px 0 var(--sep);
}
.tab {
  flex: 1; max-width: 140px; border: 0; background: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 10px; font-weight: 500; color: var(--hint);
}
.tab svg { width: 26px; height: 26px; fill: currentColor; }
.tab.is-active { color: var(--accent); }

/* ---------- Bottom sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .4);
  opacity: 0; transition: opacity .25s;
}
.sheet-backdrop.is-open { opacity: 1; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; max-width: 720px; margin: 0 auto;
  max-height: 90vh; overflow-y: auto; overscroll-behavior: contain;
  border-radius: 14px 14px 0 0; background: var(--bg2);
  padding: 6px 16px calc(24px + var(--safe-bottom));
  transform: translateY(100%); transition: transform .3s cubic-bezier(.2, .9, .3, 1);
}
.sheet.is-open { transform: none; }
.sheet-grip { width: 36px; height: 5px; border-radius: 3px; background: var(--hint); opacity: .35; margin: 0 auto 14px; }
.sheet-head { display: flex; gap: 14px; align-items: flex-start; }
.sheet-head > div { flex: 1; min-width: 0; padding-top: 4px; }
.sheet-head h2 { margin: 0; font-size: 22px; font-weight: 700; line-height: 1.2; }
.sheet-head p { margin: 3px 0 0; color: var(--hint); font-size: 14px; }
.sheet-head .actions { display: flex; align-items: center; gap: 10px; margin-top: 12px; }
.sheet-head .get { background: var(--accent); color: var(--accent-text); }
.sheet-head .get.is-off { background: var(--fill); color: var(--hint); }
.star {
  width: 30px; height: 30px; border: 0; border-radius: 50%; background: var(--fill); cursor: pointer;
  color: var(--hint); font-size: 16px; line-height: 30px;
}
.star.is-on { color: #ff9500; }
.desc { margin: 14px 0 0; font-size: 15px; line-height: 1.45; }

.btn {
  width: 100%; height: 50px; border: 0; border-radius: 12px; cursor: pointer;
  background: var(--accent); color: var(--accent-text); font-size: 17px; font-weight: 600;
}
.btn:disabled { opacity: .5; }
.btn.secondary { background: var(--section); color: var(--accent); }
.btn-row { display: flex; gap: 10px; margin-top: 16px; }
.btn-row .btn { flex: 1; }

/* Login */
.login h2 { margin: 4px 0 6px; font-size: 22px; font-weight: 700; }
.login .muted { margin: 0 0 16px; font-size: 15px; line-height: 1.4; }
.field { display: block; margin-top: 10px; }
.field span { display: block; margin: 0 0 6px 4px; color: var(--hint); font-size: 13px; }
.field input {
  width: 100%; height: 46px; padding: 0 14px; border: 0; border-radius: 12px; outline: 0;
  color: var(--text); font: inherit; font-size: 17px;
}
.field input:focus { box-shadow: 0 0 0 2px var(--accent); }
.field input[inputmode="numeric"] { letter-spacing: .3em; font-size: 22px; text-align: center; font-variant-numeric: tabular-nums; }
.form-msg { margin: 12px 4px 0; color: var(--hint); font-size: 14px; }
.form-msg.is-error { color: var(--danger); }
.login .btn { margin-top: 16px; }

/* Job */
.job-status { margin-top: 16px; padding: 14px; border-radius: 12px; background: var(--section); font-size: 15px; }
.job-status.is-done { color: var(--ok); font-weight: 600; }
.job-status.is-error { color: var(--danger); }
.progress { position: relative; height: 4px; margin-top: 12px; border-radius: 2px; overflow: hidden; background: var(--fill); }
.progress::after {
  content: ""; position: absolute; inset: 0; width: 35%; border-radius: 2px; background: var(--accent);
  animation: indeterminate 1.2s ease-in-out infinite;
}
@keyframes indeterminate { from { transform: translateX(-100%); } to { transform: translateX(290%); } }
.job-status + .btn { margin-top: 16px; }

.job-pill {
  position: fixed; left: 12px; right: 12px; bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 8px); z-index: 11;
  max-width: 696px; margin: 0 auto; display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border: 0; border-radius: 12px; cursor: pointer; text-align: left;
  background: var(--section); box-shadow: 0 2px 12px rgba(0, 0, 0, .15), 0 0 0 .5px var(--sep);
  font-size: 14px; font-weight: 500;
}
.job-pill span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.job-pill-dot {
  flex: none; width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--fill); border-top-color: var(--accent); animation: spin .8s linear infinite;
}
.job-pill.is-done .job-pill-dot { border-color: var(--ok); background: var(--ok); animation: none; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 16px); z-index: 30;
  transform: translateX(-50%); padding: 10px 16px; border-radius: 10px; font-size: 14px;
  background: rgba(30, 30, 30, .92); color: #fff; white-space: nowrap;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
