/* Viewport — no position:fixed to avoid white gaps on mobile Safari */
html, body {
  width: 100%;
  margin: 0;
  padding: 0;
  overscroll-behavior-y: none;
}

/* App container */
.app {
  width: 100%;
  max-width: var(--max-width);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  position: relative;
  background: var(--white);
}

/* Safe area for iPhone notch — on app container */
@supports (padding: max(0px)) {
  .app {
    padding-left: max(0px, env(safe-area-inset-left));
    padding-right: max(0px, env(safe-area-inset-right));
  }
}

/* ===== SCREEN MANAGEMENT — CRITICAL SPA LOGIC ===== */
/* ALL screens hidden by default. !important beats any class overrides in auth.css etc. */
.screen {
  display: none !important;
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  opacity: 0;
  pointer-events: none;
}

/* Only the active screen is visible */
.screen.active {
  display: flex !important;
  flex-direction: column;
  opacity: 1;
  pointer-events: auto;
  z-index: 1;
  animation: screenFadeIn var(--transition-base) ease;
}

@keyframes screenFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Screen with bottom nav — add padding for bottom nav + safe area */
.screen.has-nav {
  padding-bottom: calc(var(--bottom-nav-height) + env(safe-area-inset-bottom, 0px));
}

/* Fix for iOS Safari viewport */
@supports (-webkit-touch-callout: none) {
  body {
    min-height: -webkit-fill-available;
  }
  .screen {
    min-height: -webkit-fill-available;
  }
}

/* ===== iOS TOUCH FIXES ===== */

/* 1. Disable double-tap zoom via touch-action: manipulation */
*, *::before, *::after {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* 2. Prevent iOS auto-zoom on input focus (must be 16px+) */
input, textarea, select {
  font-size: 16px !important;
  touch-action: auto;
  -webkit-user-select: text;
  user-select: text;
}

/* 3. Allow file inputs to work normally */
input[type="file"] {
  touch-action: auto;
  -webkit-user-select: auto;
  user-select: auto;
}

button, .btn, .bottom-nav__item {
  min-height: 44px;
}

/* Screen header */
.screen-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-4);
  min-height: var(--header-height);
  background: var(--white);
  position: sticky;
  top: 0;
  z-index: 10;
}

.screen-header__title {
  font-size: var(--text-lg);
  font-weight: var(--font-semibold);
  color: var(--gray-900);
}

.screen-header__back {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  transition: background var(--transition-fast);
}

.screen-header__back:active {
  background: var(--gray-100);
}

.screen-header__actions {
  display: flex;
  gap: var(--space-2);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-6);
  border-radius: var(--radius-xl);
  font-weight: var(--font-medium);
  font-size: var(--text-base);
  transition: all var(--transition-fast);
  white-space: nowrap;
}

.btn:active {
  transform: scale(0.97);
}

.btn--primary {
  background: var(--primary);
  color: var(--white);
}

.btn--primary:active {
  background: var(--primary-dark);
}

.btn--secondary {
  background: var(--gray-100);
  color: var(--gray-700);
}

.btn--secondary:active {
  background: var(--gray-200);
}

.btn--outline {
  border: 1.5px solid var(--gray-200);
  color: var(--gray-700);
  background: var(--white);
}

.btn--outline:active {
  background: var(--gray-50);
}

.btn--danger {
  background: var(--error);
  color: var(--white);
}

.btn--danger:active {
  background: #DC2626;
}

.btn--ghost {
  color: var(--primary);
  background: transparent;
}

.btn--ghost:active {
  background: var(--primary-bg);
}

.btn--full {
  width: 100%;
}

.btn--lg {
  padding: var(--space-4) var(--space-8);
  font-size: var(--text-lg);
}

.btn--sm {
  padding: var(--space-2) var(--space-4);
  font-size: var(--text-sm);
}

.btn--icon {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: var(--radius-full);
}

/* Input fields */
.input-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.input-group__label {
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--gray-600);
}

.input-field {
  width: 100%;
  padding: var(--space-3) var(--space-4);
  background: var(--gray-50);
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-lg);
  font-size: var(--text-base);
  color: var(--gray-800);
  transition: border-color var(--transition-fast);
}

/* с.101 (v904): единый стандарт высоты однострочных полей формы.
   Факт: нативный type=date в WebKit был на 2px выше остальных инпутов (48.5 vs 46.5),
   кастомный дропдаун #goal-display — 51px. Компактные тайм-поля --sm (расписание)
   не трогаем — их размер осознанный. textarea многострочный — вне стандарта. */
input.input-field:not(.input-field--sm) {
  height: 48px;
  box-sizing: border-box;
}

#goal-display.input-field {
  height: 48px;
  box-sizing: border-box;
}

.input-field:focus {
  border-color: var(--primary);
  background: var(--white);
}

.input-field::placeholder {
  color: var(--gray-400);
}

.input-field--error {
  border-color: var(--error);
}

.input-error {
  font-size: var(--text-xs);
  color: var(--error);
  margin-top: var(--space-1);
}

textarea.input-field {
  resize: vertical;
  min-height: 80px;
}

/* Cards */
.card {
  background: var(--white);
  border-radius: var(--radius-xl);
  padding: var(--space-4);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--gray-100);
}

.card--elevated {
  box-shadow: var(--shadow-md);
  border: none;
}

/* Avatar */
.avatar {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-full);
  object-fit: cover;
  background: var(--primary-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--primary);
  font-weight: var(--font-semibold);
  font-size: var(--text-lg);
  flex-shrink: 0;
  overflow: hidden;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar--sm {
  width: 32px;
  height: 32px;
  font-size: var(--text-sm);
}

.avatar--lg {
  width: 64px;
  height: 64px;
  font-size: var(--text-2xl);
}

.avatar--xl {
  width: 96px;
  height: 96px;
  font-size: var(--text-3xl);
}

/* Badge */
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: var(--font-semibold);
  color: var(--white);
  background: var(--primary);
}

/* Chips / Tags */
.chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-full);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  background: var(--gray-100);
  color: var(--gray-600);
}

.chip--active {
  background: var(--primary);
  color: var(--white);
}

/* Bottom Navigation — hidden by default, shown via body.logged-in + .visible */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: var(--max-width);
  background: var(--white);
  border-top: 1px solid var(--gray-200);
  display: none;
  align-items: center;
  justify-content: space-around;
  z-index: 100;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

body.logged-in .bottom-nav.visible {
  display: flex;
}

.bottom-nav__item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: var(--space-1) var(--space-2);
  color: var(--gray-400);
  font-size: 11px;
  font-weight: 500;
  transition: color var(--transition-fast);
  min-width: 56px;
}

.bottom-nav__item.active {
  color: var(--primary);
}

.bottom-nav__item svg {
  width: 26px;
  height: 26px;
}

/* FAB (Floating Action Button) */
.fab {
  position: fixed;
  bottom: calc(var(--bottom-nav-height) + var(--space-4) + env(safe-area-inset-bottom, 0px));
  right: var(--space-4);
  width: 56px;
  height: 56px;
  border-radius: var(--radius-full);
  background: var(--primary);
  color: var(--white);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-lg);
  z-index: 50;
  transition: all var(--transition-fast);
}

.fab:active {
  transform: scale(0.93);
  box-shadow: var(--shadow-md);
}

.fab svg {
  width: 28px;
  height: 28px;
}

/* Divider */
.divider {
  height: 1px;
  background: var(--gray-200);
  margin: var(--space-4) 0;
}

/* Empty state */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: var(--space-16) var(--space-4);
  text-align: center;
  gap: var(--space-4);
}

.empty-state__icon {
  width: 64px;
  height: 64px;
  color: var(--gray-300);
}

.empty-state__text {
  font-size: var(--text-base);
  color: var(--gray-400);
}

/* Section padding */
.section-padding {
  padding: 0 var(--space-4);
}

/* Flex utilities */
.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.items-center { align-items: center; }
.justify-center { justify-content: center; }
.justify-between { justify-content: space-between; }
.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }
.flex-1 { flex: 1; }

/* Spacing utilities */
.mt-2 { margin-top: var(--space-2); }
.mt-4 { margin-top: var(--space-4); }
.mt-6 { margin-top: var(--space-6); }
.mt-8 { margin-top: var(--space-8); }
.mb-2 { margin-bottom: var(--space-2); }
.mb-4 { margin-bottom: var(--space-4); }
.mb-6 { margin-bottom: var(--space-6); }
.p-4 { padding: var(--space-4); }

/* Text utilities */
.text-center { text-align: center; }
.text-sm { font-size: var(--text-sm); }
.text-xs { font-size: var(--text-xs); }
.text-lg { font-size: var(--text-lg); }
.text-gray { color: var(--gray-500); }
.font-medium { font-weight: var(--font-medium); }
.font-semibold { font-weight: var(--font-semibold); }

/* Scrollable content area */
.scroll-content {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* Search bar */
.search-bar {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  background: var(--gray-50);
  border-radius: var(--radius-xl);
  border: 1.5px solid var(--gray-200);
  margin: 0 var(--space-4);
}

.search-bar svg {
  width: 20px;
  height: 20px;
  color: var(--gray-400);
  flex-shrink: 0;
}

.search-bar input {
  flex: 1;
  background: none;
  font-size: var(--text-base);
  color: var(--gray-800);
}

.search-bar input::placeholder {
  color: var(--gray-400);
}

/* Tabs */
.tabs {
  display: flex;
  gap: var(--space-1);
  padding: var(--space-1);
  background: var(--gray-100);
  border-radius: var(--radius-lg);
}

.tab {
  flex: 1;
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--gray-500);
  text-align: center;
  transition: all var(--transition-fast);
}

.tab.active {
  background: var(--white);
  color: var(--gray-800);
  box-shadow: var(--shadow-sm);
}

/* Toggle */
.toggle {
  display: flex;
  background: var(--gray-100);
  border-radius: var(--radius-full);
  padding: 3px;
  position: relative;
}

.toggle__option {
  flex: 1;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-full);
  font-size: var(--text-sm);
  font-weight: var(--font-medium);
  color: var(--gray-500);
  text-align: center;
  transition: all var(--transition-fast);
  z-index: 1;
}

.toggle__option.active {
  background: var(--primary);
  color: var(--white);
}

/* =================== DISPLAY MODE: BROWSER vs STANDALONE =================== */

@media all and (display-mode: browser) {
  html {
    overflow: hidden;
  }
  body {
    overflow: auto;
    height: 100vh;
  }
}

@media all and (display-mode: standalone) {
  /* Standalone mode — address bar already hidden */
  body {
    overscroll-behavior: none;
  }
}

/* =================== INSTALL BANNER =================== */

.install-banner {
  position: fixed;
  bottom: calc(80px + env(safe-area-inset-bottom, 0px));
  left: 16px;
  right: 16px;
  z-index: 1000;
  background: var(--white);
  border-radius: var(--radius-xl);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  padding: var(--space-3) var(--space-4);
  animation: slideUpBanner 0.3s ease-out;
  max-width: calc(var(--max-width) - 32px);
  margin: 0 auto;
}

@keyframes slideUpBanner {
  from { transform: translateY(100%); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.install-banner__content {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.install-banner__text {
  flex: 1;
  min-width: 0;
}

.install-banner__text strong {
  display: block;
  font-size: var(--text-sm);
  font-weight: var(--font-semibold);
  color: var(--gray-900);
  margin-bottom: 2px;
}

.install-banner__text p {
  font-size: var(--text-xs);
  color: var(--gray-500);
  margin: 0;
}

.install-banner__close {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--gray-400);
  flex-shrink: 0;
  border-radius: var(--radius-full);
}

/* =================== NATIVE TIME INPUT STYLING =================== */

input[type="time"].input-field {
  cursor: pointer;
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
}

input[type="time"].input-field:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-bg);
  background: var(--white);
}

input[type="time"]::-webkit-calendar-picker-indicator {
  cursor: pointer;
}

/* iOS Toggle Switch */
.ios-toggle { position:relative; display:inline-block; width:51px; height:31px; flex-shrink:0; }
.ios-toggle input { opacity:0; width:0; height:0; }
.ios-toggle__slider { position:absolute; cursor:pointer; top:0; left:0; right:0; bottom:0; background:#E5E5EA; border-radius:31px; transition:.3s; }
.ios-toggle__slider:before { position:absolute; content:""; height:27px; width:27px; left:2px; bottom:2px; background:#fff; border-radius:50%; transition:.3s; box-shadow:0 2px 4px rgba(0,0,0,0.2); }
.ios-toggle input:checked + .ios-toggle__slider { background:#34C759; }
.ios-toggle input:checked + .ios-toggle__slider:before { transform:translateX(20px); }
