/* =========================================
   КОМПОНЕНТЫ И АДАПТИВНОСТЬ (Components)
   ========================================= */

:root {
  --color-bg: #0a0a0f;
  --color-surface: #12121a;
  --color-surface-hover: #1a1a25;
  --color-border: rgba(255, 255, 255, 0.08);
  --color-text: #ffffff;
  --color-text-muted: #a0a0b0;
  --color-primary: #00d4aa;
  --color-primary-hover: #00e6b8;
  --color-secondary: #6c5ce7;
  --color-danger: #ff4757;
  --color-success: #00d4aa;
  --color-warning: #ffa502;
  --color-info: #3742fa;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 4px 20px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 20px 60px rgba(0, 0, 0, 0.5);
}

/* --- SIDEBAR --- */
.app-layout { display: flex; min-height: 100vh; background-color: var(--color-bg); position: relative; }

.sidebar {
  width: 260px;
  background-color: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  padding: 1.5rem 1rem;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  z-index: 1000;
  transition: transform 0.3s ease;
}

.sidebar-toggle {
  display: none;
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  border-radius: var(--radius-sm);
  width: 40px;
  height: 40px;
  cursor: pointer;
  color: var(--color-text);
  align-items: center;
  justify-content: center;
  z-index: 1001;
  transition: background 0.2s;
}

.sidebar-toggle:hover { background: rgba(255, 255, 255, 0.15); }

.mobile-menu-open {
  display: none;
  position: fixed;
  top: 1rem;
  left: 1rem;
  background: var(--color-primary);
  border: none;
  border-radius: var(--radius-sm);
  width: 44px;
  height: 44px;
  cursor: pointer;
  color: white;
  align-items: center;
  justify-content: center;
  z-index: 999;
  box-shadow: 0 4px 12px rgba(0, 212, 170, 0.3);
  transition: transform 0.2s, background 0.2s;
}

.mobile-menu-open:hover { background: var(--color-primary-hover); transform: scale(1.05); }

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--color-text);
  padding: 0 0.75rem 1.5rem;
  border-bottom: 1px solid var(--color-border);
  margin-bottom: 1.5rem;
}

.sidebar-logo .nav-logo-icon { font-size: 1.5rem; color: var(--color-primary); }
.sidebar-logo span:last-child { color: var(--color-primary); }

.sidebar-nav { display: flex; flex-direction: column; gap: 0.5rem; flex: 1; }

.sidebar-link {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.875rem 1rem;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  text-decoration: none;
  font-weight: 500;
  font-size: 0.95rem;
  transition: all 0.2s ease;
  min-height: 48px;
}

.sidebar-link:hover { background-color: rgba(255, 255, 255, 0.05); color: var(--color-text); }
.sidebar-link.active { background-color: rgba(0, 212, 170, 0.15); color: var(--color-primary); }

.sidebar-footer { padding-top: 1.5rem; border-top: 1px solid var(--color-border); }

.user-info-sidebar { display: flex; flex-direction: column; align-items: center; text-align: center; padding: 0 0.5rem; }

.sidebar-overlay {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 999;
  backdrop-filter: blur(4px);
}

.sidebar-overlay.show { display: block; }

/* --- MAIN PAGE --- */
.page {
  flex: 1;
  margin-left: 260px;
  padding: 2rem;
  width: calc(100% - 260px);
  transition: margin-left 0.3s ease;
}

.container { max-width: 1200px; margin: 0 auto; }

/* --- КНОПКИ --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.2s ease;
  min-height: 44px;
  white-space: nowrap;
}

.btn:disabled { opacity: 0.6; cursor: not-allowed; }

.btn-primary { background: var(--color-primary); color: #000000; }
.btn-primary:hover:not(:disabled) { background: var(--color-primary-hover); transform: translateY(-1px); }

.btn-secondary { background: rgba(255, 255, 255, 0.05); color: var(--color-text); border: 1px solid var(--color-border); }
.btn-secondary:hover:not(:disabled) { background: rgba(255, 255, 255, 0.1); }

.btn-ghost { background: transparent; color: var(--color-text-muted); }
.btn-ghost:hover:not(:disabled) { background: rgba(255, 255, 255, 0.05); color: var(--color-text); }

.btn-sm { padding: 0.5rem 1rem; font-size: 0.875rem; min-height: 36px; }
.btn-full { width: 100%; }

.btn-icon {
  background: transparent;
  border: none;
  color: var(--color-text-muted);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 6px;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-icon:hover { background: rgba(255, 255, 255, 0.05); color: var(--color-text); }
.btn-delete:hover { color: var(--color-danger); background: rgba(255, 71, 87, 0.1); }

.btn-loader { display: inline-flex; align-items: center; justify-content: center; }
.spinner { animation: spin 1s linear infinite; }

@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

/* --- ФОРМЫ --- */
.form-group { display: flex; flex-direction: column; gap: 0.5rem; margin-bottom: 1rem; }

.form-group label { font-size: 0.85rem; font-weight: 500; color: var(--color-text-muted); }

.form-input {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 0.875rem 1rem;
  color: var(--color-text);
  font-size: 0.95rem;
  font-family: inherit;
  transition: all 0.2s ease;
  width: 100%;
}

.form-input:focus { outline: none; border-color: var(--color-primary); background: rgba(255, 255, 255, 0.05); }
.form-input::placeholder { color: var(--color-text-muted); opacity: 0.5; }
.form-input[readonly] { opacity: 0.6; cursor: not-allowed; }

textarea.form-input { resize: vertical; min-height: 80px; font-family: inherit; }

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }

select.form-input {
  background: rgba(255, 255, 255, 0.03);
  color: var(--color-text);
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23a0a0b0' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 1rem center;
  padding-right: 2.5rem;
}

select.form-input option { background: var(--color-surface, #12121a); color: var(--color-text, #ffffff); padding: 0.5rem; }

.password-wrapper { position: relative; }
.password-wrapper .form-input { padding-right: 3rem; }

.password-toggle {
  position: absolute;
  right: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: none;
  color: var(--color-text-muted);
  cursor: pointer;
  padding: 0.25rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s ease;
}

.password-toggle:hover { color: var(--color-text); }
.password-toggle.visible { color: var(--color-primary); }

/* --- МОДАЛЬНЫЕ ОКНА --- */
.modal {
  display: none;
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  z-index: 2000;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  backdrop-filter: blur(4px);
}

.modal-content {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 2rem;
  max-width: 500px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--color-border);
}

.modal-header h2 { margin: 0; font-size: 1.5rem; color: var(--color-text); }

.modal-close {
  background: transparent;
  border: none;
  color: var(--color-text-muted);
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0.25rem;
  line-height: 1;
  transition: color 0.2s;
}

.modal-close:hover { color: var(--color-text); }

.modal-actions { display: flex; gap: 1rem; margin-top: 1.5rem; }
.modal-actions .btn { flex: 1; }

/* --- HEADER СТРАНИЦЫ --- */
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.page-title { font-size: 1.75rem; font-weight: 700; color: var(--color-text); margin: 0; }
.quick-actions { display: flex; gap: 0.75rem; flex-wrap: wrap; }

/* --- ПОРТФОЛИО БАЛАНС --- */
.portfolio-balance {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 2rem;
  margin-bottom: 2rem;
}

.balance-label { font-size: 0.875rem; color: var(--color-text-muted); margin-bottom: 0.5rem; }

.balance-value {
  font-size: 2.5rem;
  font-weight: 700;
  color: var(--color-primary);
  font-family: 'JetBrains Mono', monospace;
  margin-bottom: 0.5rem;
}

.balance-change { display: flex; align-items: center; gap: 0.5rem; font-size: 0.875rem; }

/* --- СЕКЦИИ --- */
.section { margin-bottom: 1.5rem; }

.section-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; flex-wrap: wrap; gap: 0.5rem; }

.section-title { font-size: 1.25rem; font-weight: 600; color: var(--color-text); margin: 0; }

/* --- СТАТИСТИКА (уменьшена на 15% по высоте) --- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.9rem;
}

.stats-grid-compact {
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.8rem;
}

.stat-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 0.95rem;
}

.stat-label { font-size: 0.65rem; color: var(--color-text-muted); margin-bottom: 0.3rem; }

.stat-value {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--color-text);
  font-family: 'JetBrains Mono', monospace;
  word-break: break-word;
  line-height: 1.2;
}

.stat-value.positive { color: var(--color-success); }
.stat-value.negative { color: var(--color-danger); }

/* --- ТАБЛИЦЫ --- */
.table-container {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.table { width: 100%; border-collapse: collapse; }

.table th,
.table td {
  padding: 0.875rem 1rem;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  color: var(--color-text);
  font-size: 0.9rem;
}

.table th {
  background: rgba(255, 255, 255, 0.02);
  font-weight: 600;
  color: var(--color-text-muted);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.table tr:last-child td { border-bottom: none; }
.table tr:hover td { background: rgba(255, 255, 255, 0.02); }

/* --- БЕЙДЖИ --- */
.badge {
  display: inline-block;
  padding: 0.25rem 0.625rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.badge-success { background: rgba(0, 212, 170, 0.15); color: var(--color-success); }
.badge-danger { background: rgba(255, 71, 87, 0.15); color: var(--color-danger); }
.badge-warning { background: rgba(255, 165, 2, 0.15); color: var(--color-warning); }

/* --- EMPTY STATE --- */
.empty-state { text-align: center; padding: 3rem 1rem; color: var(--color-text-muted); }

.empty-state-icon { width: 64px; height: 64px; margin: 0 auto 1rem; opacity: 0.4; }

.empty-state-title { font-size: 1.125rem; font-weight: 600; color: var(--color-text); margin-bottom: 0.5rem; }
.empty-state p { margin: 0 0 1rem; font-size: 0.9rem; }

/* --- PnL ЦВЕТА --- */
.pnl-positive { color: var(--color-success); font-weight: 600; }
.pnl-negative { color: var(--color-danger); font-weight: 600; }

/* --- ТЕКСТ --- */
.text-muted { color: var(--color-text-muted); }
.text-positive { color: var(--color-success); }
.text-negative { color: var(--color-danger); }

/* --- TOAST УВЕДОМЛЕНИЯ --- */
#toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 10000;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.toast {
  background: var(--color-surface);
  border-left: 4px solid var(--color-primary);
  color: var(--color-text);
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 250px;
  max-width: 400px;
  pointer-events: auto;
  animation: slideIn 0.3s ease;
  font-size: 0.95rem;
}

.toast.success { border-left-color: var(--color-success); }
.toast.error { border-left-color: var(--color-danger); }
.toast.warning { border-left-color: var(--color-warning); }
.toast.info { border-left-color: var(--color-info); }

@keyframes slideIn { from { transform: translateX(400px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes slideOut { from { transform: translateX(0); opacity: 1; } to { transform: translateX(400px); opacity: 0; } }

/* --- FOOTER --- */
.footer { margin-left: 260px; padding: 2rem; border-top: 1px solid var(--color-border); background: var(--color-surface); }

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 1rem;
}

.footer-links { display: flex; gap: 1.5rem; flex-wrap: wrap; }

.footer-link { color: var(--color-text-muted); text-decoration: none; font-size: 0.875rem; transition: color 0.2s; }
.footer-link:hover { color: var(--color-primary); }

/* =========================================
   МОБИЛЬНАЯ АДАПТИВНОСТЬ
   ========================================= */
@media (max-width: 768px) {
  .sidebar { transform: translateX(-100%); box-shadow: var(--shadow-lg); }
  .sidebar.open { transform: translateX(0); }
  .sidebar-toggle { display: flex; }
  .mobile-menu-open { display: flex; }

  .page { margin-left: 0; width: 100%; padding: 1rem; padding-top: 5rem; }
  .footer { margin-left: 0; }

  .stats-grid,
  .stats-grid-compact { grid-template-columns: repeat(2, 1fr); gap: 0.6rem; }

  .stat-card { padding: 0.75rem; }
  .stat-label { font-size: 0.62rem; }
  .stat-value { font-size: 1rem; }

  .table-container { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table { min-width: 600px; }

  .btn { min-height: 44px; padding: 0.75rem 1rem; }

  .portfolio-balance { padding: 1.5rem 1rem; }
  .balance-value { font-size: 2rem; }

  .page-header { flex-direction: column; align-items: flex-start; }
  .page-title { font-size: 1.5rem; }

  .form-row { grid-template-columns: 1fr; }
  .modal-content { padding: 1.5rem; }

  .footer-content { flex-direction: column; text-align: center; }
  .footer-links { justify-content: center; }
}

@media (max-width: 480px) {
  .stats-grid,
  .stats-grid-compact { grid-template-columns: repeat(2, 1fr); gap: 0.5rem; }

  .stat-card { padding: 0.65rem; }
  .stat-label { font-size: 0.6rem; }
  .stat-value { font-size: 0.95rem; }

  .balance-value { font-size: 1.75rem; }

  .quick-actions { width: 100%; }
  .quick-actions .btn { flex: 1; }
}

/* =========================================
   МОБИЛЬНАЯ ВЕРСИЯ — REDMI NOTE 13 (412px)
   ========================================= */

@media (max-width: 480px) {
  /* Сайдбар */
  .sidebar {
    width: 280px;
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); }
  
  .page {
    margin-left: 0;
    width: 100%;
    padding: 1rem;
    padding-top: 4.5rem;
  }

  /* Кнопка меню */
  .mobile-menu-open {
    display: flex !important;
    position: fixed;
    top: 0.75rem;
    left: 0.75rem;
    z-index: 999;
  }

  /* Заголовки */
  .page-title {
    font-size: 1.4rem !important;
    margin-top: 0.5rem;
  }

  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.75rem;
    margin-bottom: 1rem;
  }

  .quick-actions {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .quick-actions .btn {
    flex: 1;
    min-width: 120px;
    font-size: 0.85rem;
    padding: 0.6rem 0.8rem;
  }

  /* Статистика — 2 колонки */
  .stats-grid,
  .stats-grid-compact {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 0.6rem !important;
  }

  .stat-card {
    padding: 0.75rem !important;
  }

  .stat-label {
    font-size: 0.65rem !important;
  }

  .stat-value {
    font-size: 1rem !important;
  }

  /* Выделенная карточка "Общая прибыль" — без scale на мобильных */
  .stat-card.highlight-card {
    transform: none !important;
    grid-column: span 2 !important;
    padding: 1rem !important;
  }

  .stat-card.highlight-card .stat-value {
    font-size: 1.3rem !important;
  }

  /* Таблицы — горизонтальная прокрутка */
  .table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 8px;
  }

  .table {
    min-width: 600px;
    font-size: 0.85rem;
  }

  .table th, .table td {
    padding: 0.6rem 0.5rem;
    font-size: 0.8rem;
  }

  /* Фильтры периодов */
  .period-filters,
  .period-filters > button,
  .pill-btn {
    font-size: 0.8rem;
    padding: 0.4rem 0.7rem;
    min-height: 36px;
  }

  /* Модалки */
  .modal-content {
    padding: 1.25rem;
    margin: 0.5rem;
    max-height: 95vh;
  }

  .modal-header h2 {
    font-size: 1.2rem;
  }

  /* Формы */
  .form-row {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }

  .form-input {
    font-size: 16px !important; /* Предотвращает зум на iOS */
    padding: 0.75rem;
  }

  /* Кнопки — минимум 44px для пальца */
  .btn {
    min-height: 44px;
    font-size: 0.9rem;
  }

  .btn-sm {
    min-height: 38px;
    font-size: 0.8rem;
    padding: 0.4rem 0.7rem;
  }

  .btn-icon {
    width: 44px;
    height: 44px;
    padding: 0.5rem;
  }

  /* Карточки токенов/ботов */
  .token-card,
  .bot-card,
  .position-card {
    padding: 1rem !important;
  }

  /* Пустое состояние */
  .empty-state {
    padding: 2rem 1rem;
  }

  .empty-state-icon {
    width: 48px;
    height: 48px;
  }

  /* Toast уведомления */
  #toast-container {
    top: auto;
    bottom: 1rem;
    right: 1rem;
    left: 1rem;
  }

  .toast {
    min-width: auto;
    max-width: none;
    font-size: 0.85rem;
  }

  /* Сайдбар — ссылки */
  .sidebar-link {
    padding: 0.75rem 0.875rem;
    font-size: 0.9rem;
    min-height: 44px;
  }

  /* Шаблоны на странице Капитал */
  .templates-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
  }

  .template-btn {
    padding: 0.6rem;
    font-size: 0.8rem;
  }

  /* Детали позиции */
  .position-details {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
  }

  /* Скрытие лишних колонок на мобильных */
  .table th:nth-child(7),
  .table td:nth-child(7),
  .table th:nth-child(8),
  .table td:nth-child(8) {
    display: none;
  }
}

/* Очень маленькие экраны */
@media (max-width: 360px) {
  .stats-grid,
  .stats-grid-compact {
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
  }

  .stat-value {
    font-size: 0.95rem;
  }

  .page-title {
    font-size: 1.25rem;
  }
}

/* Альбомная ориентация на телефоне */
@media (max-height: 500px) and (orientation: landscape) {
  .sidebar {
    width: 220px;
  }
  
  .page {
    padding-top: 1rem;
  }
}