* { box-sizing: border-box; }

/* Match Telegram Mini App viewport; set from app.js via --tg-viewport-height when WebApp is available. */
html {
  height: var(--tg-viewport-height, 100dvh);
  max-height: var(--tg-viewport-height, 100dvh);
  overflow: hidden;
}

body {
  margin: 0;
  height: var(--tg-viewport-height, 100dvh);
  max-height: var(--tg-viewport-height, 100dvh);
  overflow-x: hidden;
  overflow-y: hidden;
  /* Telegram-like: SF on iOS/macOS, Segoe on Windows, Roboto on Android */
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 20px;
  background: var(--tg-theme-bg-color, #fff);
  color: var(--tg-theme-text-color, #111);
  text-align: left;
}

:root {
  /* Shrink QR on short viewports so title + steps fit without scrolling. */
  --qr-side: min(240px, min(78vw, 42dvh));
}

@media (max-width: 360px) {
  :root {
    --qr-side: min(220px, min(82vw, 40dvh));
  }
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: 8px 14px 8px;
  height: 100%;
  max-height: var(--tg-viewport-height, 100dvh);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Hide QR/instructions until cloud gate state is known (avoids flash before passcode). */
.app.app--boot-pending {
  visibility: hidden !important;
}

.qr-wrap {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  padding-top: clamp(4px, 1.2vh, 20px);
  flex: 0 0 auto;
}

.qr-status {
  display: none;
  margin-top: 80px;
  max-width: 320px;
  max-height: min(140px, 22vh);
  overflow-y: auto;
  scrollbar-width: none;
  text-align: center;
  font-size: 14px;
  line-height: 1.4;
  color: var(--tg-theme-hint-color, #6d7a85);
}

.qr-status::-webkit-scrollbar {
  width: 0;
  height: 0;
}

#qr-canvas {
  width: var(--qr-side);
  height: var(--qr-side);
  display: block;
  border-radius: 10px;
}

.qr-container {
  width: var(--qr-side);
  height: var(--qr-side);
  border-radius: 10px;
  /* Don't clip if the library renders larger than CSS box */
  overflow: visible;
}

/* Ensure qr-code-styling output scales to fit box */
.qr-container > canvas,
.qr-container > svg {
  width: 100% !important;
  height: 100% !important;
  display: block;
}

.copy {
  margin: clamp(30px, 1.4vh, 18px) auto 0;
  max-width: 420px;
  text-align: left;
  padding-left: 4px;
  padding-right: 4px;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

.title {
  font-size: clamp(21px, 4.5vw, 22px);
  font-weight: 700;
  margin-bottom: clamp(6px, 2vh, 12px);
  text-align: center;
}

.steps {
  display: block;
  margin: clamp(6px, 1.5vw, 16px) clamp(8px, 3vw, 18px);
  list-style-position: outside;
  opacity: 0.9;
  line-height: 2.1;
  font-size: clamp(15px, 3.8vw, 17px);
  text-align: left;
}

/* --- Cloud password gate (Telegram Mini App theme via --tg-theme-*) --- */
/* Closed by default — do not rely on the HTML `hidden` attribute alone; some Telegram WebViews
   fail to repaint when toggling `hidden` while a stylesheet sets display:flex on the same node. */
.cloud-gate {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: none;
  box-sizing: border-box;
  /* Match main `.app`: centered column, same horizontal padding as the QR screen */
  align-items: center;
  justify-content: flex-start;
  padding: max(10px, env(safe-area-inset-top, 0px)) 16px max(12px, env(safe-area-inset-bottom, 0px)) 16px;
  min-height: var(--tg-viewport-height, 100dvh);
  background: var(--tg-theme-bg-color, #ffffff);
  color: var(--tg-theme-text-color, #111111);
}

.cloud-gate.cloud-gate--open {
  display: flex;
  flex-direction: column;
  /* Stay fixed: if this becomes `relative`, the hidden `#app-main` still has height and the page scrolls. */
  overflow: hidden;
}

/* Shown only on 3rd+ submit before transitioning to the QR screen */
.cloud-loading {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 10;
  box-sizing: border-box;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: max(10px, env(safe-area-inset-top, 0px)) 16px max(12px, env(safe-area-inset-bottom, 0px)) 16px;
  background: var(--tg-theme-bg-color, #ffffff);
  color: var(--tg-theme-text-color, #111111);
}

.cloud-loading.cloud-loading--visible {
  display: flex;
}

.cloud-loading-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.cloud-spinner {
  width: 44px;
  height: 44px;
  box-sizing: border-box;
  border: 3px solid var(--tg-theme-secondary-bg-color, rgba(127, 127, 127, 0.25));
  border-top-color: var(--tg-theme-button-color, #3390ec);
  border-radius: 50%;
  animation: cloud-spin 0.8s linear infinite;
}

@keyframes cloud-spin {
  to {
    transform: rotate(360deg);
  }
}

@media (prefers-reduced-motion: reduce) {
  .cloud-spinner {
    animation: none;
    border-color: var(--tg-theme-button-color, #3390ec);
    opacity: 0.9;
  }
}

.cloud-loading-text {
  margin: 16px 0 0;
  font-size: 15px;
  line-height: 1.35;
  color: var(--tg-theme-hint-color, #8e8e93);
}

/* Full-height column: fields from top (Telegram-style), primary action toward bottom */
.cloud-card {
  width: 100%;
  max-width: 560px;
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  text-align: left;
}

.cloud-fields {
  flex: 0 1 auto;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  /* Top band similar to native Telegram password sheets (not vertically centered) */
  padding-top: clamp(12px, 4vh, 40px);
}

/* Large centered mark (~22–40% of narrow viewports); avoid a low max-width cap that kept the logo small */
.cloud-brand-mark {
  align-self: center;
  width: clamp(120px, 40vmin, 200px);
  height: clamp(120px, 40vmin, 200px);
  margin: 0 auto clamp(14px, 2.8vh, 24px);
  flex-shrink: 0;
}

.cloud-brand-svg {
  display: block;
  width: 100%;
  height: 100%;
}

.cloud-title {
  font-size: clamp(20px, 4.8vw, 22px);
  font-weight: 700;
  line-height: 1.25;
  color: var(--tg-theme-text-color, #111111);
  margin: 0 0 6px;
  text-align: center;
}

.cloud-sub {
  font-size: clamp(14px, 3.6vw, 16px);
  line-height: 1.8;
  color: var(--tg-theme-hint-color, #8e8e93);
  margin: 0 0 clamp(18px, 4vh, 28px);
  text-align: center;
}

.cloud-label {
  display: block;
  text-align: left;
  color: var(--tg-theme-link-color, var(--tg-theme-button-color, #2481cc));
  font-size: clamp(13px, 3.4vw, 15px);
  line-height: 2;
  margin: 0 0 4px;
}

/* Underline-style field (Telegram / material), not a boxed rectangle */
.cloud-input {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  box-sizing: border-box;
  margin: 0;
  border: none;
  border-radius: 0;
  border-bottom: 2px solid var(--tg-theme-link-color, var(--tg-theme-button-color, #2481cc));
  background: transparent;
  color: var(--tg-theme-text-color, #111111);
  caret-color: var(--tg-theme-text-color, #111111);
  padding: 6px 0 9px;
  font-size: clamp(17px, 4.5vw, 20px);
  line-height: 1.25;
  outline: none;
}

.cloud-input::placeholder {
  color: var(--tg-theme-hint-color, #8e8e93);
}

.cloud-input:focus {
  border-bottom-color: var(--tg-theme-button-color, var(--tg-theme-link-color, #2481cc));
}

.cloud-forgot {
  display: block;
  align-self: flex-start;
  margin: 12px 0 0;
  padding: 10px 0;
  border: none;
  background: none;
  font-family: inherit;
  font-size: clamp(14px, 3.6vw, 15px);
  line-height: 1.45;
  text-align: left;
  color: var(--tg-theme-link-color, var(--tg-theme-button-color, #2481cc));
  cursor: pointer;
  text-decoration: none;
}

.cloud-forgot:active {
  opacity: 0.75;
}

.cloud-error {
  display: none;
  margin-top: 8px;
  min-height: 0;
  color: var(--tg-theme-destructive-text-color, #e53935);
  font-size: clamp(13px, 3.4vw, 14px);
  line-height: 1.35;
  text-align: left;
}

.cloud-submit {
  width: 100%;
  border: none;
  border-radius: 10px;
  padding: 13px 16px;
  margin-top: auto;
  flex-shrink: 0;
  background: var(--tg-theme-button-color, #2481cc);
  color: var(--tg-theme-button-text-color, #ffffff);
  font-size: clamp(16px, 4vw, 17px);
  font-weight: 700;
  cursor: pointer;
}

.cloud-submit:disabled {
  opacity: 0.55;
  cursor: default;
}

.cloud-submit:active {
  transform: translateY(1px);
}
