/* ===== CSS VARIABLES & THEMING ===== */
:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --surface-2: #f8f9fa;
  --border: #e8eaed;
  --text: #1a1a2e;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --accent: #3b82f6;
  --accent-light: #dbeafe;
  --accent-dark: #1d4ed8;
  --success: #22c55e;
  --success-light: #dcfce7;
  --warning: #f59e0b;
  --warning-light: #fef3c7;
  --danger: #ef4444;
  --danger-light: #fee2e2;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.07), 0 2px 4px rgba(0,0,0,0.06);
  --radius: 14px;
  --radius-sm: 10px;
  --radius-xs: 6px;
  --font: 'Manrope', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --header-h: 60px;
  --tabs-h: 56px;
  --transition: 0.2s ease;
}

/* Тёмная тема через Telegram */
.dark {
  --bg: #1c1c1e;
  --surface: #2c2c2e;
  --surface-2: #3a3a3c;
  --border: #38383a;
  --text: #f2f2f7;
  --text-secondary: #aeaeb2;
  --text-muted: #636366;
  --accent: #0a84ff;
  --accent-light: #1c3a5e;
  --shadow: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.3);
}

/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition: background var(--transition), color var(--transition);
}

#app {
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ===== HEADER ===== */
.header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
}

.header-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.header-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.3px;
}

.header-group {
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
}

.refresh-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--surface-2);
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  flex-shrink: 0;
}

.refresh-btn:active {
  transform: scale(0.9);
  background: var(--accent-light);
  color: var(--accent);
}

.refresh-btn.spinning svg {
  animation: spin 0.8s linear infinite;
}

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

/* ===== TAB NAVIGATION ===== */
.tab-nav {
  height: var(--tabs-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: stretch;
  flex-shrink: 0;
  padding: 6px 8px;
  gap: 4px;
}

.tab-btn {
  flex: 1;
  border: none;
  background: transparent;
  border-radius: var(--radius-xs);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
  padding: 0 8px;
  white-space: nowrap;
}

.tab-btn .tab-icon {
  font-size: 14px;
  line-height: 1;
}

.tab-btn.active {
  background: var(--accent-light);
  color: var(--accent);
  font-weight: 600;
}

.tab-btn:active:not(.active) {
  background: var(--surface-2);
}

/* ===== CONTROLS TOGGLE ===== */
.controls-toggle-wrap {
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.controls-toggle-btn {
  width: 100%;
  min-height: 46px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #ffffff;
  font-family: var(--font);
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.2px;
  box-shadow: 0 8px 18px rgba(29, 78, 216, 0.28);
  cursor: pointer;
  transition: all var(--transition);
}

.controls-toggle-btn.collapsed {
  background: var(--surface-2);
  color: var(--text-secondary);
  border-color: var(--border);
  box-shadow: none;
}

.controls-toggle-btn:active {
  transform: scale(0.985);
  box-shadow: 0 4px 10px rgba(29, 78, 216, 0.2);
}

/* ===== CONTENT ===== */
.content {
  flex: 1;
  overflow-y: auto;
  padding: 12px 12px 24px;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

/* ===== LOADING ===== */
.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 200px;
  gap: 16px;
  color: var(--text-secondary);
  font-size: 14px;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

/* ===== ERROR STATE ===== */
.error-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  text-align: center;
  gap: 12px;
}

.state-icon { font-size: 48px; line-height: 1; }
.state-title { font-size: 17px; font-weight: 600; color: var(--text); }
.state-desc { font-size: 14px; color: var(--text-secondary); max-width: 240px; }

.retry-btn {
  margin-top: 8px;
  padding: 10px 24px;
  border-radius: var(--radius-sm);
  border: none;
  background: var(--accent);
  color: white;
  font-family: var(--font);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity var(--transition);
}

.retry-btn:active { opacity: 0.8; }

/* ===== EMPTY STATE ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  text-align: center;
  gap: 10px;
}

.empty-state .state-icon { font-size: 52px; }
.empty-state .state-title { font-size: 17px; font-weight: 600; }
.empty-state .state-desc { font-size: 14px; color: var(--text-secondary); }

/* ===== DATE HEADER ===== */
.date-header {
  margin-bottom: 12px;
  padding: 12px 14px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.date-header .day-name {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
}

.date-header .date-str {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ===== CURRENT LESSON BANNER ===== */
.current-lesson-banner {
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 12px;
  color: white;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
}

.banner-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  opacity: 0.85;
  margin-bottom: 4px;
}

.banner-subject {
  font-size: 16px;
  font-weight: 700;
  line-height: 1.3;
}

.banner-meta {
  font-size: 13px;
  opacity: 0.85;
  margin-top: 6px;
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.banner-time {
  font-weight: 600;
}

/* ===== LESSON CARD ===== */
.lessons-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.lesson-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 15px;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  display: flex;
  gap: 12px;
  position: relative;
  overflow: hidden;
  transition: transform var(--transition), box-shadow var(--transition);
  animation: slideIn 0.25s ease both;
}

.lesson-card:active {
  transform: scale(0.98);
}

@keyframes slideIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.lesson-card.current {
  border-left: 3px solid var(--success);
  background: var(--success-light);
}

.dark .lesson-card.current {
  background: rgba(34, 197, 94, 0.1);
}

.lesson-card.next {
  border-left: 3px solid var(--warning);
}

.lesson-card.cancelled {
  opacity: 0.5;
}

/* Левая колонка: номер + время */
.lesson-time-col {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 44px;
  flex-shrink: 0;
  padding-top: 2px;
}

.lesson-num {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent-light);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 6px;
  flex-shrink: 0;
}

.lesson-card.current .lesson-num {
  background: var(--success);
  color: white;
}

.lesson-card.next .lesson-num {
  background: var(--warning);
  color: white;
}

.lesson-time {
  font-size: 11px;
  color: var(--text-secondary);
  text-align: center;
  line-height: 1.4;
  font-weight: 500;
}

.lesson-time span {
  display: block;
}

/* Правая колонка: данные пары */
.lesson-info {
  flex: 1;
  min-width: 0;
}

.lesson-subject {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
  margin-bottom: 6px;
  word-break: break-word;
}

.lesson-subject .cancelled-badge {
  font-size: 11px;
  font-weight: 600;
  background: var(--danger);
  color: white;
  padding: 2px 6px;
  border-radius: 4px;
  margin-left: 6px;
  vertical-align: middle;
}

.lesson-meta {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.lesson-meta-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-link {
  color: var(--accent);
  text-decoration: underline;
  word-break: break-all;
}

.lesson-meta-icon {
  font-size: 12px;
  flex-shrink: 0;
}

.lesson-type-badge {
  position: absolute;
  top: 12px;
  right: 12px;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 2px 7px;
  border-radius: 4px;
  background: var(--accent-light);
  color: var(--accent);
}

.lesson-type-badge.lab {
  background: rgba(34, 197, 94, 0.15);
  color: var(--success);
}

.lesson-type-badge.practice {
  background: rgba(245, 158, 11, 0.15);
  color: var(--warning);
}

.lesson-notes {
  margin-top: 6px;
  font-size: 12px;
  color: var(--text-secondary);
  font-style: italic;
  padding: 4px 8px;
  background: var(--surface-2);
  border-radius: var(--radius-xs);
}

/* ===== WEEK VIEW ===== */
.week-header {
  padding: 10px 14px 14px;
  font-size: 13px;
  color: var(--text-secondary);
  font-weight: 500;
}

.week-day-block {
  margin-bottom: 16px;
}

.week-day-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  margin-bottom: 8px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.week-day-header.today {
  background: var(--accent);
  color: white;
}

.week-day-name {
  font-size: 15px;
  font-weight: 700;
}

.week-day-date {
  font-size: 13px;
  opacity: 0.7;
}

.week-day-count {
  font-size: 12px;
  font-weight: 600;
  background: rgba(255,255,255,0.2);
  padding: 2px 8px;
  border-radius: 10px;
}

.week-day-header:not(.today) .week-day-count {
  background: var(--accent-light);
  color: var(--accent);
}

.week-empty {
  text-align: center;
  padding: 16px;
  font-size: 14px;
  color: var(--text-muted);
  background: var(--surface);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
}

/* Компактные карточки для недели */
.week-lessons-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow: hidden;
}

.week-lesson-row {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 11px 12px;
  position: relative;
  background: var(--surface);
}

.week-lesson-row + .week-lesson-row {
  border-top: 2px solid var(--border);
}

.week-lesson-row:nth-child(odd) {
  background: color-mix(in srgb, var(--surface) 92%, var(--surface-2) 8%);
}

.week-lesson-row::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: color-mix(in srgb, var(--accent) 70%, transparent 30%);
  opacity: 0.7;
}

.week-lesson-row.cancelled {
  opacity: 0.55;
}

.week-lesson-left {
  width: 64px;
  min-width: 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding-top: 2px;
}

.week-lesson-num {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent-light);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
}

.week-lesson-time {
  font-size: 11px;
  line-height: 1.3;
  text-align: center;
  color: var(--text-secondary);
  font-weight: 600;
}

.week-lesson-right {
  flex: 1;
  min-width: 0;
  padding-right: 0;
}

.week-lesson-subject {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text);
  margin-bottom: 6px;
  word-break: break-word;
}

.week-lesson-subject .cancelled-badge {
  font-size: 10px;
  font-weight: 600;
  background: var(--danger);
  color: #fff;
  padding: 2px 6px;
  border-radius: 4px;
  margin-left: 6px;
  vertical-align: middle;
}

.week-type-chip {
  display: inline-block;
  margin-top: 6px;
  font-size: 10px;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-light);
  border-radius: 999px;
  padding: 2px 8px;
}

/* ===== SCROLLBAR ===== */
.content::-webkit-scrollbar {
  width: 3px;
}
.content::-webkit-scrollbar-track {
  background: transparent;
}
.content::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

/* ===== SAFE AREAS (iPhone notch) ===== */
@supports (padding: max(0px)) {
  .content {
    padding-bottom: max(24px, env(safe-area-inset-bottom));
  }
}

/* ===== ANIMATIONS ===== */
.tab-content {
  animation: fadeIn 0.2s ease;
}

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

.course-panel {
  padding: 10px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.course-panel-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.course-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.course-option {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: var(--radius-sm);
  min-height: 34px;
  font-family: var(--font);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
}

.course-option:active {
  transform: scale(0.98);
  background: var(--accent-light);
  color: var(--accent);
}

/* ===== TEACHER PANEL ===== */
.teacher-panel {
  padding: 10px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: min(58vh, calc(100vh - var(--header-h) - var(--tabs-h) - 120px));
  overflow: hidden;
  flex-shrink: 0;
}

.teacher-panel-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.teacher-search {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  padding: 0 12px;
}

.teacher-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  max-height: min(36vh, 360px);
  overflow-y: auto;
  padding-right: 2px;
  align-content: start;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.teacher-list::-webkit-scrollbar {
  width: 6px;
}

.teacher-list::-webkit-scrollbar-track {
  background: transparent;
}

.teacher-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 999px;
}

.teacher-choice-btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: var(--radius-sm);
  min-height: 44px;
  padding: 8px 10px;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  text-align: left;
  line-height: 1.25;
  cursor: pointer;
  transition: all var(--transition);
  touch-action: manipulation;
}

.teacher-choice-btn.active {
  background: var(--accent-light);
  color: var(--accent);
  border-color: transparent;
}

.teacher-choice-btn:active {
  transform: scale(0.98);
}

.teacher-more-btn {
  border: 1px dashed var(--border);
  background: var(--surface-2);
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  min-height: 40px;
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.teacher-more-btn:active {
  transform: scale(0.98);
}

.teacher-scope-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.teacher-scope-btn {
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  border-radius: var(--radius-sm);
  min-height: 46px;
  font-family: var(--font);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
}

.teacher-scope-btn.active {
  background: var(--accent-light);
  border-color: transparent;
  color: var(--accent);
}

.teacher-scope-btn:active {
  transform: scale(0.98);
}

/* ===== TOAST ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%) translateY(20px);
  background: rgba(17, 24, 39, 0.92);
  color: #fff;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 12px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 1200;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast.error {
  background: rgba(127, 29, 29, 0.92);
}

@media (max-width: 420px) {
  .controls-toggle-wrap {
    padding: 7px 10px;
  }

  .controls-toggle-btn {
    min-height: 42px;
    font-size: 13px;
  }

  .teacher-list {
    grid-template-columns: 1fr;
    max-height: min(42vh, 320px);
  }

  .teacher-scope-btn {
    min-height: 42px;
    font-size: 13px;
  }

  .week-lesson-row {
    padding: 10px 10px;
  }

  .week-lesson-left {
    width: 58px;
    min-width: 58px;
  }

  .week-lesson-subject {
    font-size: 13px;
  }
}

/* ===== WEEK NAVIGATION ===== */
.week-nav {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 4px 0 8px;
}

.week-nav-btn {
  flex: 0 0 auto;
  width: 44px;
  height: 44px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  font-size: 16px;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: var(--transition);
}

.week-nav-btn:hover { background: var(--surface-2); }
.week-nav-btn:active { transform: scale(0.96); }
.week-nav-btn:disabled { opacity: 0.35; cursor: default; transform: none; }

.week-nav-label {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 8px;
  min-height: 44px;
  box-shadow: var(--shadow);
}

.week-nav-label .wk-prefix {
  font-weight: 700;
  font-size: 14px;
  color: var(--text);
}

.week-nav-label .wk-range {
  font-size: 12px;
  color: var(--text-secondary);
}

.week-today-link {
  display: block;
  width: 100%;
  margin: 0 0 10px;
  padding: 9px 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--accent-light);
  color: var(--accent-dark);
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  transition: var(--transition);
}

.week-today-link:active { transform: scale(0.98); }
.week-today-link.hidden { display: none; }
