:root {
  --bg: #f4f6f9;
  --card: #ffffff;
  --text: #111827;
  --muted: #4b5563;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --success: #15803d;
  --warning: #b45309;
  --danger: #b91c1c;
  --border: #d1d5db;
  --radius: 12px;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  max-width: 100%;
  overflow-x: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

.hidden {
  display: none !important;
}

/* Только для скринридеров (визуально скрыто, но доступно для озвучки) */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Ссылка быстрого перехода к контенту с клавиатуры */
.skip-link {
  position: absolute;
  left: -9999px;
  top: 8px;
  z-index: 100;
  background: var(--primary);
  color: #fff;
  padding: 12px 16px;
  border-radius: 8px;
  font-weight: 700;
  text-decoration: none;
}

.skip-link:focus {
  left: 12px;
}

/* Чёткая рамка фокуса для клавиатуры и скринридеров */
:focus-visible {
  outline: 3px solid var(--primary);
  outline-offset: 2px;
}

.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 18px 14px calc(64px + env(safe-area-inset-bottom));
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  margin-bottom: 16px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

h2, h3, h4 {
  margin-top: 0;
}

h2:focus, #study-phrase-region:focus, #main-content:focus {
  outline: none;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 0.9rem;
}

.mt-1 {
  margin-top: 10px;
}

/* Авторизация */
.auth-wrapper {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.auth-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px 20px;
  width: 100%;
  max-width: 420px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.06);
}

.auth-logo {
  font-size: 1.5rem;
  font-weight: 800;
  text-align: center;
  margin: 0;
}

.auth-subtitle {
  text-align: center;
  color: var(--muted);
  margin: 6px 0 20px;
  font-size: 0.95rem;
}

.tabs-row {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.tab-btn {
  flex: 1;
  min-height: 44px;
  padding: 10px;
  border: 1px solid var(--border);
  background: #f9fafb;
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
}

.tab-btn.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

label {
  display: block;
  font-size: 0.9rem;
  font-weight: 600;
  margin: 12px 0 4px;
}

/* font-size: 16px предотвращает нежелательный автозум на iPhone */
input, select {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  background: #fff;
  color: var(--text);
}

input:focus, select:focus {
  border-color: var(--primary);
}

/* Кнопки (удобный размер для касания на телефонах >= 44px) */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s;
  text-align: center;
}

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

.btn-block {
  width: 100%;
  margin-top: 16px;
}

.btn-lg {
  min-height: 48px;
  padding: 12px 20px;
  font-size: 1rem;
}

.btn-sm {
  min-height: 38px;
  padding: 6px 12px;
  font-size: 0.875rem;
}

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

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-secondary {
  background: #e0e7ff;
  color: #1e3a8a;
}

.btn-warning {
  background: #fef3c7;
  color: #78350f;
  border-color: #fcd34d;
}

.btn-danger {
  background: #fee2e2;
  color: #7f1d1d;
  border-color: #fca5a5;
}

.btn-outline {
  background: #fff;
  border-color: var(--border);
  color: var(--text);
}

.btn-outline:hover:not(:disabled) {
  background: #f3f4f6;
}

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

.btn-ghost:hover:not(:disabled) {
  background: #f3f4f6;
  color: var(--text);
}

.icon-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  min-width: 40px;
  background: #f3f4f6;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
}

.icon-btn[aria-pressed="true"] {
  background: #eff6ff;
  border-color: #93c5fd;
  color: #1e3a8a;
}

.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 600;
  min-height: 36px;
  padding: 4px 8px;
}

/* Шапка и навигация */
.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-inner {
  max-width: 1080px;
  margin: 0 auto;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.brand {
  background: none;
  border: none;
  padding: 6px 4px;
  min-height: 40px;
  font-weight: 800;
  font-size: 1.2rem;
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.topbar-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.user-label {
  font-weight: 600;
  font-size: 0.9rem;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pill {
  background: #eff6ff;
  color: #1e40af;
  padding: 5px 10px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 700;
  white-space: nowrap;
}

.pill-fire {
  background: #fff7ed;
  color: #9a3412;
  border: 1px solid #fed7aa;
}

.badge {
  background: var(--danger);
  color: #fff;
  border-radius: 999px;
  padding: 1px 6px;
  font-size: 0.75rem;
  font-weight: 700;
}

.notif-wrap {
  position: relative;
}

.notif-dropdown {
  position: absolute;
  right: 0;
  top: 46px;
  width: 320px;
  max-width: calc(100vw - 24px);
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.14);
  padding: 12px;
  z-index: 50;
}

.notif-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.notif-list {
  max-height: 280px;
  overflow-y: auto;
}

.notif-item {
  padding: 8px 4px;
  border-bottom: 1px solid #f3f4f6;
  font-size: 0.875rem;
}

.notif-item.unread {
  background: #eff6ff;
  padding: 8px;
  border-radius: 6px;
  margin-top: 6px;
}

/* Главная навигация (вкладки) */
.main-nav {
  max-width: 1080px;
  margin: 12px auto 0;
  padding: 0 14px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.nav-btn {
  min-height: 44px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.nav-btn.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.mini-count {
  background: rgba(0, 0, 0, 0.12);
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 0.8rem;
}

.nav-btn.active .mini-count {
  background: rgba(255, 255, 255, 0.25);
  color: #fff;
}

/* Дашборд и коробки */
.hero-grid {
  display: grid;
  grid-template-columns: 1.3fr 1fr;
  gap: 16px;
}

.hero-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 16px;
}

.preset-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.inline-form {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.inline-form input,
.inline-form select {
  width: auto;
  flex: 1;
  min-width: 130px;
}

.section-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 12px;
}

.boxes-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
}

.box-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 92px;
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 12px 8px;
  text-align: center;
  cursor: pointer;
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
  transition: transform 0.12s, box-shadow 0.12s;
}

.box-tile:hover {
  transform: translateY(-2px);
}

.box-tile[aria-expanded="true"] {
  box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.35);
}

.box-1 { border-color: #fca5a5; background: #fef2f2; }
.box-2 { border-color: #fdba74; background: #fff7ed; }
.box-3 { border-color: #facc15; background: #fefce8; }
.box-4 { border-color: #86efac; background: #f0fdf4; }
.box-5 { border-color: #93c5fd; background: #eff6ff; }

.box-num {
  font-size: 1.6rem;
  font-weight: 800;
  margin: 2px 0;
}

.box-details {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  max-height: 280px;
  overflow-y: auto;
}

/* Темы */
.topics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 12px;
  margin-top: 14px;
}

.topic-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 12px;
  background: #fafafa;
}

/* Экран тренировки */
.study-card {
  max-width: 680px;
  margin: 0 auto;
}

.study-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.timer-wrap {
  margin-bottom: 14px;
}

.timer-bar-bg {
  width: 100%;
  height: 8px;
  background: #e5e7eb;
  border-radius: 999px;
  overflow: hidden;
}

.timer-bar-fill {
  height: 100%;
  width: 100%;
  background: var(--primary);
  transition: width 0.1s linear;
}

.timer-label {
  text-align: right;
  font-size: 0.875rem;
  font-weight: 700;
  margin-top: 4px;
}

.phrase-box {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px 16px;
  text-align: center;
  margin-bottom: 16px;
}

.topic-tag {
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 6px;
}

.phrase-text {
  font-size: 1.35rem;
  font-weight: 700;
  word-break: break-word;
}

.answer-input {
  font-size: 1.1rem;
  min-height: 48px;
  padding: 12px 14px;
  margin-bottom: 12px;
}

.study-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.hint-box {
  background: #fefce8;
  border: 1px solid #facc15;
  border-radius: 8px;
  padding: 12px 14px;
  margin-bottom: 12px;
  font-size: 0.95rem;
}

.feedback-box {
  margin-top: 16px;
  padding: 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.feedback-ok {
  background: #f0fdf4;
  border-color: #86efac;
}

.feedback-wrong {
  background: #fef2f2;
  border-color: #fca5a5;
}

.feedback-title {
  font-weight: 700;
  font-size: 1.05rem;
  margin-bottom: 6px;
}

.feedback-body {
  word-break: break-word;
}

.feedback-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
  align-items: center;
  flex-wrap: wrap;
}

/* Админка и таблицы */
.subtabs {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}

.subtab-btn {
  min-height: 44px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
}

.subtab-btn.active {
  background: #1f2937;
  color: #fff;
  border-color: #1f2937;
}

.grid-form {
  display: grid;
  grid-template-columns: 1fr 1fr 1.5fr auto;
  gap: 8px;
  align-items: center;
}

.stack-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.row-2 {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 8px;
}

.btn-group {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th, td {
  text-align: left;
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
}

th {
  background: #f9fafb;
  font-weight: 700;
  white-space: nowrap;
}

.list-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.list-item {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fafafa;
  word-break: break-word;
}

.alert {
  padding: 12px;
  border-radius: 8px;
  margin-top: 12px;
  font-size: 0.92rem;
}

.alert-danger {
  background: #fee2e2;
  color: #7f1d1d;
  border: 1px solid #fca5a5;
}

/* АДАПТАЦИЯ ПОД ТЕЛЕФОНЫ И ПЛАНШЕТЫ */
@media (max-width: 768px) {
  .hero-grid {
    grid-template-columns: 1fr;
  }

  .grid-form {
    grid-template-columns: 1fr;
  }

  .row-2 {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 600px) {
  .container {
    padding: 14px 10px calc(64px + env(safe-area-inset-bottom));
  }

  .card {
    padding: 14px;
    border-radius: 10px;
  }

  .topbar-inner {
    padding: 8px 10px;
  }

  .btn-caption {
    display: none;
  }

  .user-label {
    max-width: 95px;
    font-size: 0.85rem;
  }

  /* Вкладки в 2 колонки: все разделы сразу видны на экране телефона */
  .main-nav {
    padding: 0 10px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  .nav-btn {
    width: 100%;
    padding: 8px 10px;
    font-size: 0.9rem;
  }

  /* Коробки Лейтнера: 2 в ряд, 5-я на всю ширину */
  .boxes-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }

  .boxes-grid .box-tile:last-child {
    grid-column: 1 / -1;
  }

  .topics-grid {
    grid-template-columns: 1fr;
  }

  .notif-dropdown {
    position: fixed;
    left: 10px;
    right: 10px;
    top: 58px;
    width: auto;
    max-width: none;
  }
}

@media (max-width: 480px) {
  .hero-actions,
  .study-actions,
  .feedback-actions,
  .subtabs {
    flex-direction: column;
    width: 100%;
  }

  .hero-actions .btn,
  .study-actions .btn,
  .feedback-actions .btn,
  .subtabs .subtab-btn,
  .topic-card .btn-group .btn {
    width: 100%;
  }

  .topic-card .btn-group {
    flex-direction: column;
    width: 100%;
  }

  .inline-form {
    flex-direction: column;
  }

  .inline-form input,
  .inline-form select,
  .inline-form .btn {
    width: 100%;
  }

  .phrase-text {
    font-size: 1.2rem;
  }
}
