:root {
  --app-bg: var(--tg-theme-bg-color, #ffffff);
  --app-text: var(--tg-theme-text-color, #111111);
  --app-hint: var(--tg-theme-hint-color, #707579);
  --app-link: var(--tg-theme-link-color, #2481cc);
  --app-button: var(--tg-theme-button-color, #2481cc);
  --app-button-text: var(--tg-theme-button-text-color, #ffffff);
  --app-secondary-bg: var(--tg-theme-secondary-bg-color, #f4f4f5);
  --app-header-bg: var(--tg-theme-header-bg-color, var(--tg-theme-bg-color, #ffffff));
  --app-accent: var(--tg-theme-accent-text-color, var(--tg-theme-link-color, #2481cc));
  --app-section-bg: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --app-section-header: var(--tg-theme-section-header-text-color, var(--tg-theme-hint-color, #707579));
  --app-subtitle: var(--tg-theme-subtitle-text-color, var(--tg-theme-hint-color, #707579));
  --app-destructive: var(--tg-theme-destructive-text-color, #ff3b30);
  --separator: color-mix(in srgb, var(--app-hint) 22%, transparent);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color-scheme: var(--tg-color-scheme, light);
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--app-bg); }
body {
  min-height: var(--tg-viewport-stable-height, 100vh);
  margin: 0;
  background: var(--app-bg);
  color: var(--app-text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 17px;
  line-height: 1.32;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: inherit; text-decoration: none; }
button, input, textarea { font: inherit; }
button { color: inherit; }

.app-shell {
  width: min(680px, 100%);
  min-height: var(--tg-viewport-stable-height, 100vh);
  margin: 0 auto;
  padding: calc(18px + env(safe-area-inset-top)) 16px calc(96px + env(safe-area-inset-bottom));
}

.screen { display: none; }
.screen.active { display: block; }
.authorized-app[hidden] { display: none; }

.page-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 6px 2px 22px;
}

h1, h2, p { margin: 0; }
h1 { font-size: 28px; line-height: 1.12; font-weight: 600; letter-spacing: -0.02em; }
h2 { font-size: 28px; line-height: 1.12; font-weight: 600; letter-spacing: -0.02em; margin: 12px 0 8px; }
.subtitle { color: var(--app-subtitle); font-size: 17px; font-weight: 400; margin-top: 5px; }
.caption { color: var(--app-hint); font-size: 13px; font-weight: 400; }
.setup-action-copy { margin-top: 10px; color: var(--app-link); }

.hero-icon {
  width: 74px;
  height: 74px;
  border-radius: 22px;
  display: grid;
  place-items: center;
  color: var(--app-accent);
  background: var(--app-secondary-bg);
  flex: 0 0 auto;
}
.hero-icon.small { width: 48px; height: 48px; border-radius: 15px; }
.hero-icon svg { width: 38px; height: 38px; fill: currentColor; }
.hero-icon.small svg { width: 27px; height: 27px; }
.denied .hero-icon { color: var(--app-destructive); }

.state-screen {
  min-height: calc(var(--tg-viewport-stable-height, 100vh) - 150px);
  text-align: center;
  align-content: center;
  justify-items: center;
  padding: 38px 18px;
}
.state-screen h1 { margin-top: 18px; }
.state-screen .subtitle { max-width: 320px; }
.denied h1, #error { color: var(--app-destructive); }

.welcome-hero {
  text-align: center;
  display: grid;
  justify-items: center;
  padding: 22px 14px 28px;
}
.welcome-hero .subtitle { max-width: 360px; }

.section-group { margin-top: 35px; }
.screen > .section-group:first-child, form > .section-group:first-child { margin-top: 0; }
.section-group + .section-group { margin-top: 16px; }
.section-title {
  margin: 0 16px 7px;
  color: var(--app-section-header);
  font-size: 13px;
  line-height: 1.25;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
#status-heading {
  color: var(--app-text);
  font-size: 17px;
  text-transform: none;
  letter-spacing: 0;
}
.section-card {
  overflow: hidden;
  background: var(--app-section-bg);
  border-radius: 10px;
}
.row {
  min-height: 44px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 16px;
  background: var(--app-section-bg);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.status-row { cursor: default; }
.row + .row { border-top: 1px solid var(--separator); }
.row span:nth-child(2) { min-width: 0; flex: 1; display: grid; gap: 2px; }
.row strong { color: var(--app-text); font-size: 17px; font-weight: 400; }
.row small { color: var(--app-hint); font-size: 13px; font-weight: 400; }
.muted-value { color: var(--app-hint); }
.chevron { color: var(--app-hint); font-size: 25px; line-height: 1; }

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--app-hint);
  flex: 0 0 auto;
}
.status-dot.progress { background: var(--app-accent); }
.status-dot.connected,
.status-dot.success { background: #34c759; }
.secondary-button {
  min-height: 36px;
  border: 0;
  padding: 0 0 0 12px;
  background: transparent;
  color: var(--app-button);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  flex: 0 0 auto;
}
.button-group {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.secondary-button.destructive {
  min-height: 30px;
  padding-left: 8px;
  color: var(--app-destructive);
  font-size: 12px;
  font-weight: 500;
}

.form-card { padding: 0 16px; }
.field {
  min-height: 72px;
  display: grid;
  align-content: center;
  gap: 7px;
  padding: 11px 0;
}
.field + .field { border-top: 1px solid var(--separator); }
.field > span:first-child {
  color: var(--app-text);
  font-size: 17px;
  font-weight: 400;
}
.field em { color: var(--app-hint); font-size: 13px; font-style: normal; }
input, textarea {
  width: 100%;
  min-height: 44px;
  border: 0;
  border-radius: 0;
  outline: none;
  padding: 7px 0;
  background: transparent;
  color: var(--app-text);
  caret-color: var(--app-link);
  font-size: 17px;
}
textarea { min-height: 118px; resize: vertical; }
input:focus, textarea:focus { border-bottom: 1px solid var(--app-link); }
input::placeholder, textarea::placeholder { color: var(--app-hint); opacity: 1; }
.password-wrap { display: flex; align-items: center; gap: 10px; }
.icon-button {
  min-width: 54px;
  min-height: 44px;
  border: 0;
  padding: 0 0 0 10px;
  background: transparent;
  color: var(--app-hint);
  font-size: 13px;
  cursor: pointer;
}
.footer-note { margin: 8px 16px 0; color: var(--app-hint); font-size: 13px; }
#error { min-height: 22px; margin: 14px 16px 0; font-size: 13px; font-weight: 500; }

@media (max-width: 420px) {
  .app-shell { padding-left: 12px; padding-right: 12px; }
  h1, h2 { font-size: 26px; }
}

.app-version {
  text-align: center;
  font-size: 11px;
  color: var(--tg-theme-hint-color, #999);
  opacity: 0.6;
  padding: 16px 8px 24px;
  user-select: text;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 0.02em;
}

/* Toast overlay (disconnect/success feedback) */
.toast-overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(4px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease-out;
  z-index: 9999;
}
.toast-overlay.toast-visible {
  opacity: 1;
  pointer-events: auto;
}
.toast-card {
  background: var(--tg-theme-bg-color, #fff);
  color: var(--tg-theme-text-color, #111);
  padding: 22px 28px;
  border-radius: 14px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  min-width: 200px;
  max-width: 280px;
  text-align: center;
  transform: scale(0.94);
  transition: transform 180ms ease-out;
}
.toast-overlay.toast-visible .toast-card { transform: scale(1); }
.toast-icon {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: #22c55e;
  color: #fff;
  font-size: 26px;
  line-height: 48px;
  font-weight: 600;
}
.toast-message {
  font-size: 15px;
  font-weight: 500;
}
