/* Telegram Web A - Auth Page Styles
   Based on github.com/Ajaxy/telegram-tt */

:root {
  --color-primary: #3390ec;
  --color-primary-shade: #2b7cd3;
  --color-primary-shade-rgb: 43, 124, 211;
  --color-text: #000;
  --color-text-secondary: #707579;
  --color-text-secondary-rgb: 112, 117, 121;
  --color-background: #fff;
  --color-background-secondary: #f4f4f5;
  --color-borders-input: #dadce0;
  --color-error: #e53935;
  --color-error-rgb: 229, 57, 53;
  --color-white: #fff;
  --color-chat-hover: #f4f4f5;
  --color-scrollbar: rgba(90, 90, 90, 0.3);
  --color-telegram-blue: #3390ec;
  --font-family: "Roboto", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
  --font-weight-normal: 400;
  --font-weight-medium: 500;
  --border-radius-button: 0.625rem;
  --border-radius-default: 0.75rem;
}

html.night {
  --color-primary: #8774e1;
  --color-primary-shade: #7b6ad8;
  --color-primary-shade-rgb: 123, 106, 216;
  --color-text: #fff;
  --color-text-secondary: #aaa;
  --color-text-secondary-rgb: 170, 170, 170;
  --color-background: #212121;
  --color-background-secondary: #181818;
  --color-borders-input: #2f2f2f;
  --color-error: #ff5959;
  --color-error-rgb: 255, 89, 89;
  --color-chat-hover: #2c2c2c;
  --color-scrollbar: rgba(255, 255, 255, 0.2);
  --color-telegram-blue: #8774e1;
}

/* ---- Reset ---- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
button { font-family: inherit; }

html, body { height: 100%; overflow: hidden; }

body {
  font-family: var(--font-family);
  font-size: 16px;
  line-height: 1.3125;
  color: var(--color-text);
  background-color: var(--color-background);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

::selection { background: rgba(51, 144, 236, 0.3); }

/* ---- Layout ---- */
.whole { width: 100%; height: 100%; }
#auth-pages { width: 100%; height: 100%; }

.tabs-container {
  width: 100%;
  height: 100%;
  position: relative;
  display: grid;
  grid-template-columns: 100%;
  grid-template-rows: 100%;
}

.tabs-tab {
  display: none;
  grid-row-start: 1;
  grid-column-start: 1;
  overflow-y: auto;
  height: 100%;
}
.tabs-tab.active { display: block; }

/* ---- Auth Form Container ---- */
.auth-form {
  width: 100%;
  max-width: 25.5rem;
  margin: 0 auto;
  padding: 6.8125rem 1.5rem 1.5rem;
  text-align: center;
}
.auth-form.qr { padding-top: 4rem; }

/* ---- Logo (phone form) ---- */
#logo {
  display: block;
  width: 10rem;
  height: 10rem;
  margin: 0 auto 2.5rem;
}

.tg-plane-logo {
  width: 100%;
  height: 100%;
  display: block;
  animation: logo-appear .6s ease-out;
}

.logo-circle {
  transform-origin: center;
}

.logo-plane {
  animation: plane-float 3s ease-in-out infinite;
  transform-origin: center;
}

@keyframes logo-appear {
  0% {
    transform: scale(0.5);
    opacity: 0;
  }
  60% {
    transform: scale(1.08);
    opacity: 1;
  }
  100% {
    transform: scale(1);
  }
}

@keyframes plane-float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-6px);
  }
}

/* ---- Typography ---- */
.auth-form h1 {
  font-size: 2rem;
  font-weight: var(--font-weight-medium);
  line-height: 1.5;
  margin: 0;
}
.auth-form.qr h1 {
  margin: 1.5rem 0 1rem;
  font-size: 1.25rem;
}

.auth-form .note {
  margin-top: 0.5rem;
  margin-bottom: 2.5rem;
  font-size: 1rem;
  line-height: 1.35;
  color: var(--color-text-secondary);
}

/* ---- QR Section ---- */
.qr-outer {
  position: relative;
  width: 280px;
  height: 280px;
  margin: 0 auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

.qr-canvas {
  overflow: hidden;
  border-radius: 1.5rem;
  background-color: white;
  width: 280px;
  height: 280px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.qr-canvas svg {
  width: 100%;
  height: 100%;
}

.preloader {
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 280px;
  height: 280px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preloader-circular {
  width: 48px;
  height: 48px;
  animation: preloader-rotate 2s linear infinite;
}

.preloader-path {
  stroke: var(--color-primary);
  stroke-dasharray: 1, 200;
  stroke-dashoffset: 0;
  stroke-linecap: round;
  animation: preloader-dash 1.5s ease-in-out infinite;
}

@keyframes preloader-rotate { 100% { transform: rotate(360deg); } }
@keyframes preloader-dash {
  0%   { stroke-dasharray: 1, 200; stroke-dashoffset: 0; }
  50%  { stroke-dasharray: 89, 200; stroke-dashoffset: -35; }
  100% { stroke-dasharray: 89, 200; stroke-dashoffset: -124; }
}

@keyframes grow-icon {
  0% {
    transform: scale(0.7);
    opacity: 0;
  }
  50% {
    transform: scale(1.1);
    opacity: 1;
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}
@keyframes hide-icon {
  from { transform: scale(1); opacity: 1; }
  to   { transform: scale(0.5); opacity: 0; }
}

/* ---- QR Ordered List (custom counters) ---- */
.auth-form.qr ol {
  counter-reset: item;
  padding: 0 1.75rem;
  list-style: none;
  text-align: left;
  max-width: 22rem;
  margin: 0 auto;
}

.auth-form.qr ol li {
  counter-increment: item;
  display: flex;
  align-items: flex-start;
  margin: 0.75rem 0;
  font-size: 1rem;
  line-height: 1.3125;
}

.auth-form.qr ol li::before {
  content: counter(item);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  min-width: 1.375rem;
  height: 1.375rem;
  margin: 0.0625rem 0.75rem 0 0;
  padding: 0;
  border-radius: 50%;
  font-size: 0.875rem;
  color: white;
  background: var(--color-primary);
}

/* ---- Auth Image (code/password icons) ---- */
.auth-image {
  width: 10rem;
  height: 10rem;
  margin: 0 auto 1.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
.auth-image svg { width: 100%; height: 100%; }

/* Monkey animation container */
.monkey-container {
  width: 10rem;
  height: 10rem;
  overflow: hidden;
  position: relative;
}
.monkey-container > div {
  position: absolute;
  top: 0;
  left: 0;
  width: 100% !important;
  height: 100% !important;
}

/* ---- Buttons ---- */
.btn-primary,
button.auth-button {
  cursor: pointer;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-shrink: 0;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 3.375rem;
  padding: 0.625rem;
  border: 0;
  border-radius: var(--border-radius-button);
  font-family: var(--font-family);
  font-weight: var(--font-weight-medium);
  font-size: 1rem;
  line-height: 1.2;
  text-transform: uppercase;
  text-decoration: none;
  outline: none;
  transition: background-color 0.15s, color 0.15s, opacity 0.15s;
}

.btn-primary {
  color: var(--color-white);
  background-color: var(--color-primary);
}
.btn-primary:hover:not(:disabled) {
  background-color: var(--color-primary-shade);
}
.btn-primary:disabled {
  opacity: 0.5;
  pointer-events: none;
}

button.auth-button {
  background-color: transparent;
  color: var(--color-primary);
}
button.auth-button:hover {
  background-color: rgba(var(--color-primary-shade-rgb), 0.08);
}

/* Button spacing */
.auth-button { margin-top: 2.5rem; }
.auth-button + .auth-button { margin-top: 0; }
.form .btn-primary { margin-top: 2.5rem; }
.form .btn-primary + .auth-button { margin-top: 0.5rem; }
#password-form .btn-primary { margin-top: 1.5rem; }

/* ---- Form ---- */
.form {
  text-align: left;
  min-height: 26.25rem;
}

/* ---- Input Fields ---- */
.input-field {
  position: relative;
  margin-bottom: 1.5rem;
}

.input-field-input {
  width: 100%;
  height: 3.375rem;
  padding: 0.75rem 1rem;
  border: 1px solid var(--color-borders-input);
  border-radius: var(--border-radius-default);
  background-color: var(--color-background);
  font-family: var(--font-family);
  font-size: 1rem;
  color: var(--color-text);
  outline: none;
  transition: border-color 0.15s;
  line-height: 1.3125;
}

.input-field-input::placeholder { color: transparent; }

.input-field label {
  position: absolute;
  top: 0;
  left: 1rem;
  height: 1.5rem;
  line-height: 1.5rem;
  font-size: 1rem;
  color: var(--color-text-secondary);
  background-color: var(--color-background);
  pointer-events: none;
  transform: translateY(calc((3.375rem - 1.5rem) / 2));
  transform-origin: left center;
  transition: transform 0.15s, color 0.15s, font-weight 0.15s;
  white-space: nowrap;
  z-index: 1;
}

/* Float label */
.input-field-input:focus ~ label,
.input-field-input:not(:placeholder-shown) ~ label,
.input-field.is-focused label,
.input-field.touched label {
  transform: translateY(-0.625rem) scale(0.75);
  padding: 0 0.3125rem;
  color: var(--color-primary);
  font-weight: var(--font-weight-medium);
}

.input-field-border {
  position: absolute;
  inset: 0;
  border: 2px solid var(--color-primary);
  border-radius: var(--border-radius-default);
  opacity: 0;
  pointer-events: none;
  z-index: 1;
  transition: opacity 0.15s;
}
.input-field-input:focus ~ .input-field-border,
.input-field.is-focused .input-field-border {
  opacity: 1;
}

/* Error state */
.input-field.error .input-field-input { border-color: var(--color-error); }
.input-field.error .input-field-border { border-color: var(--color-error); opacity: 1; }
.input-field.error label { color: var(--color-error) !important; }

.auth-form-error {
  color: var(--color-error);
  font-size: 0.875rem;
  margin-top: 0.5rem;
  text-align: center;
}

/* ---- Country Selector ---- */
#country-input-field { cursor: pointer; }
#country-input-field .input-field-input {
  cursor: pointer;
  padding-right: 2.5rem;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}

.arrow-down {
  position: absolute;
  top: 1rem;
  right: 1rem;
  width: 0.75rem;
  height: 0.75rem;
  border: 2px solid var(--color-text-secondary);
  border-top: 0;
  border-left: 0;
  transform: scaleY(1) rotate(45deg);
  z-index: 2;
  pointer-events: none;
  transition: color 0.2s ease, transform 0.2s ease, top 0.2s ease;
}
#country-input-field.open .arrow-down {
  top: 1.3125rem;
  transform: scaleY(-1) rotate(45deg);
  border-color: var(--color-primary);
}

/* Dropdown */
.select-wrapper {
  position: absolute;
  width: 100%;
  top: calc(100% + 0.5rem);
  left: 0;
  max-height: 23.5rem;
  overflow: hidden;
  background-color: var(--color-background);
  border-radius: var(--border-radius-default);
  z-index: 10;
  box-shadow: 0 0.25rem 0.5rem 0.125rem rgba(0, 0, 0, 0.12);
  transform: scale(0.95);
  transform-origin: top center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease-out, transform 0.2s ease-out;
}
.select-wrapper.active {
  transform: scale(1);
  opacity: 1;
  pointer-events: auto;
}

.scrollable.scrollable-y {
  max-height: 23.5rem;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.scrollable.scrollable-y::-webkit-scrollbar { width: 5px; }
.scrollable.scrollable-y::-webkit-scrollbar-track { background: transparent; }
.scrollable.scrollable-y::-webkit-scrollbar-thumb {
  background: var(--color-scrollbar);
  border-radius: 3px;
}

.scrollable ul { list-style: none; margin: 0.5rem 0; padding: 0; }
.scrollable li {
  display: flex;
  align-items: center;
  height: 3rem;
  padding: 0 1rem;
  cursor: pointer;
  font-size: 0.875rem;
  color: var(--color-text);
  transition: background 0.15s;
  white-space: nowrap;
}
.scrollable li:hover { background: var(--color-chat-hover); }
.scrollable li.selected { background: var(--color-chat-hover); }

.country-emoji {
  margin-right: 1rem;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.75rem;
  min-width: 1.75rem;
  height: 1.4rem;
}
.country-emoji img {
  width: 24px;
  height: 18px;
  object-fit: cover;
  border-radius: 2px;
  display: block;
}
.country-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  margin-right: 1rem;
  text-align: left;
}
.phone-code {
  margin-left: auto;
  padding-right: 0.25rem;
  opacity: 0.5;
  flex-shrink: 0;
}

/* ---- Checkbox ---- */
.checkbox-field {
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  min-height: 3rem;
  padding-left: 4rem;
  margin-bottom: 0;
  text-align: left;
  border-radius: var(--border-radius-default);
}
.checkbox-field:hover { background-color: var(--color-chat-hover); }

.checkbox-field-input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.checkbox-box {
  position: absolute;
  top: 50%;
  left: 1.125rem;
  transform: translateY(-50%);
  width: 1.25rem;
  height: 1.25rem;
  border-radius: 0.25rem;
  overflow: hidden;
}
.checkbox-box-border {
  position: absolute;
  inset: 0;
  border: 0.125rem solid var(--color-borders-input);
  border-radius: inherit;
  background-color: var(--color-background);
  transition: border-color 0.1s ease, background-color 0.1s ease;
}
.checkbox-box-background {
  position: absolute;
  inset: 0;
  background-color: var(--color-primary);
  border-radius: 50%;
  transform: scale(0);
  transition: transform 0.2s 0.05s ease-in-out;
}
.checkbox-box-check {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  opacity: 0;
  transition: opacity 0.2s;
}

.checkbox-field-input:checked ~ .checkbox-box .checkbox-box-border { border-color: var(--color-primary); }
.checkbox-field-input:checked ~ .checkbox-box .checkbox-box-background { transform: scale(1); }
.checkbox-field-input:checked ~ .checkbox-box .checkbox-box-check { opacity: 1; }

.checkbox-caption {
  font-size: 0.875rem;
  color: var(--color-text);
  line-height: 1.25rem;
}

/* ---- Code Form ---- */
#code-form-slide h1,
#code-form-slide .auth-form h1 {
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-number-edit {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0.5rem;
  color: var(--color-text-secondary);
  opacity: 0.75;
  transition: opacity 0.15s;
}
.auth-number-edit:hover { opacity: 1; }

#code-wrap { max-width: 21rem; margin: 0 auto; }
#code-wrap.has-error .input-field-input { border-color: var(--color-error); }
#code-wrap.has-error .input-field-border { border-color: var(--color-error); opacity: 1; }
#code-wrap.has-error label { color: var(--color-error) !important; }

.code-error {
  color: var(--color-error);
  font-size: 0.8125rem;
  line-height: 1.3;
  margin-top: 0.5rem;
  text-align: center;
}

/* ---- Password Form ---- */
.input-field-password .input-field-input { padding-right: 2.5rem; }
.input-field-password .input-field-input[type="password"] {
  font-size: 2.25rem;
  letter-spacing: 0.25rem;
  font-family: Arial, Helvetica, sans-serif;
}

.toggle-visible {
  position: absolute;
  right: 0.375rem;
  top: 50%;
  transform: translateY(-50%);
  z-index: 2;
  font-size: 1.5rem;
  color: var(--color-text-secondary);
  cursor: pointer;
  padding: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  transition: color 0.2s;
}
.toggle-visible:hover { color: var(--color-text); }
.toggle-visible .tgico { font-style: normal; font-size: 1.5rem; }

/* ---- Responsive ---- */
@media (max-width: 600px) {
  .auth-form { padding: 6rem 1rem 1rem; }
  .auth-form.qr { padding-top: 3rem; }

  #logo {
    width: 7.5rem;
    height: 7.5rem;
    margin-bottom: 1.75rem;
  }

  .auth-form h1 { font-size: 1.25rem; line-height: 1; }
  .auth-form .note { font-size: 0.875rem; }

  .auth-image {
    width: 7.5rem;
    height: 7.5rem;
    margin-bottom: 1.75rem;
  }

  .qr-outer { height: 200px; }
  .qr-canvas { width: 200px; height: 200px; }
  .preloader { width: 200px; height: 200px; }
}

@media (min-width: 601px) and (min-height: 450px) {
  #logo {
    width: 10rem;
    height: 10rem;
    margin-bottom: 2.5rem;
  }
  .auth-form .note { margin-bottom: 3rem; font-size: 1rem; }
}

/* =============================================
   Enhanced Mobile Responsiveness
   ============================================= */

/* --- Safe area insets (iPhone X+) --- */
.whole {
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* --- Touch optimization --- */
.btn-primary,
button.auth-button,
.checkbox-field,
.scrollable li,
#country-input-field {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* --- Prevent zoom on iOS inputs --- */
@supports (-webkit-touch-callout: none) {
  input[type="tel"],
  input[type="password"],
  input[type="text"] {
    font-size: 16px !important;
  }
}

/* --- Very small screens (320px and below) --- */
@media (max-width: 360px) {
  .auth-form {
    padding: 4.5rem 0.75rem 1rem;
  }
  .auth-form.qr {
    padding-top: 2rem;
  }

  #logo {
    width: 6rem;
    height: 6rem;
    margin-bottom: 1.25rem;
  }

  .auth-form h1 {
    font-size: 1.125rem;
    line-height: 1.3;
  }

  .auth-form .note {
    font-size: 0.8125rem;
    margin-bottom: 1.5rem;
  }

  .auth-image {
    width: 6rem;
    height: 6rem;
    margin-bottom: 1.25rem;
  }

  .qr-outer {
    width: 180px;
    height: 180px;
  }
  .qr-canvas {
    width: 180px;
    height: 180px;
  }
  .preloader {
    width: 180px;
    height: 180px;
  }

  .input-field-input {
    height: 3rem;
    padding: 0.625rem 0.875rem;
    font-size: 0.9375rem;
  }

  .btn-primary,
  button.auth-button {
    height: 3rem;
    font-size: 0.875rem;
  }

  .auth-form.qr ol {
    padding: 0 0.5rem;
  }

  .auth-form.qr ol li {
    font-size: 0.875rem;
  }

  .scrollable li {
    height: 2.75rem;
    padding: 0 0.75rem;
    font-size: 0.8125rem;
  }
}

/* --- Landscape optimization --- */
@media (max-height: 500px) and (orientation: landscape) {
  .auth-form {
    padding-top: 2rem;
  }
  .auth-form.qr {
    padding-top: 1rem;
  }

  #logo {
    width: 5rem;
    height: 5rem;
    margin-bottom: 1rem;
  }

  .auth-image {
    width: 5rem;
    height: 5rem;
    margin-bottom: 1rem;
  }

  .auth-form h1 {
    font-size: 1.25rem;
    margin-bottom: 0.25rem;
  }

  .auth-form .note {
    margin-bottom: 1rem;
    font-size: 0.875rem;
  }

  .qr-outer {
    width: 160px;
    height: 160px;
  }
  .qr-canvas {
    width: 160px;
    height: 160px;
  }
  .preloader {
    width: 160px;
    height: 160px;
  }

  .auth-form.qr ol li {
    margin: 0.375rem 0;
  }

  .form {
    min-height: auto;
  }
}

/* --- Medium phones (375-480px) --- */
@media (min-width: 361px) and (max-width: 480px) {
  .auth-form {
    padding: 5rem 1rem 1rem;
  }

  #logo {
    width: 7rem;
    height: 7rem;
    margin-bottom: 1.5rem;
  }

  .auth-form h1 {
    font-size: 1.375rem;
  }

  .auth-image {
    width: 7rem;
    height: 7rem;
    margin-bottom: 1.5rem;
  }

  .qr-outer {
    width: 220px;
    height: 220px;
  }
  .qr-canvas {
    width: 220px;
    height: 220px;
  }
  .preloader {
    width: 220px;
    height: 220px;
  }
}

/* --- Reduced motion --- */
@media (prefers-reduced-motion: reduce) {
  .tg-plane-logo,
  .logo-plane,
  .preloader-circular,
  .preloader-path {
    animation: none !important;
  }
}

/* --- Select dropdown: constrain on small screens --- */
@media (max-width: 480px) {
  .select-wrapper {
    max-height: 60vh;
  }
  .scrollable.scrollable-y {
    max-height: 60vh;
  }
}
