/* App shell */
#app {
  width: 100%;
  max-width: 780px;
  margin: 0 auto;
  padding: 0 16px;
}

/* Chapter header (sticky container for topbar + tab bar) */
.chapter-header {
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 100;
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0 8px;
  gap: 12px;
}

/* Topbar center: chapter title + optional section indicator */
.topbar-center {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.topbar h1 {
  flex: 1;
  min-width: 0;
  font-size: 1.2rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: font-size 0.2s ease;
}
.topbar h1.app-title {
  font-family: var(--font-title);
  font-weight: 800;
  font-size: 1.5rem;
  letter-spacing: -0.5px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.topbar-section {
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-height: 0;
  opacity: 0;
  transition: max-height 0.2s ease, opacity 0.2s ease;
}
.topbar.scrolled h1 {
  font-size: 0.9rem;
}
.topbar.scrolled .topbar-section {
  max-height: 1.2em;
  opacity: 1;
}

/* Back button */
.btn-back {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: var(--tap-size);
  height: var(--tap-size);
  padding: 0;
  border: none;
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  cursor: pointer;
  flex-shrink: 0;
  touch-action: manipulation;
  font-size: 0.9rem;
  font-weight: 600;
  transition: all var(--transition);
  white-space: nowrap;
}
.btn-back:active { opacity: 0.7; }

/* Icon buttons */
.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--tap-size);
  height: var(--tap-size);
  border: none;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius-sm);
  flex-shrink: 0;
  touch-action: manipulation;
  font-size: 1.3rem;
}
.btn-icon:active { opacity: 0.7; }

.topbar-actions {
  display: flex;
  gap: 0;
  flex-shrink: 0;
}

/* View container */
.view {
  padding-bottom: max(24px, env(safe-area-inset-bottom, 24px));
}

/* Sections */
.section { margin-bottom: 24px; }

.section-title {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-top: 8px;
  margin-bottom: 12px;
}
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.section-header .section-title {
  margin-bottom: 0;
}

/* Grid for subject cards */
.subject-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
}

/* ══════════════════════════════════════
   TAB BAR (all screen sizes)
   ══════════════════════════════════════ */

.tab-bar {
  display: flex;
  gap: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}
.tab-bar::-webkit-scrollbar { display: none; }
.tab-bar button {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 8px 6px;
  min-height: var(--tap-size);
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 600;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  touch-action: manipulation;
  transition: all var(--transition);
  white-space: nowrap;
}
.tab-bar button.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
.tab-bar button:active { opacity: 0.7; }

/* Full label visible by default, short label hidden */
.tab-bar-label-short { display: none; }

/* Medium screens (420–599px): short labels */
@media (max-width: 599px) {
  .tab-bar-label { display: none; }
  .tab-bar-label-short { display: inline; }
}

/* Small screens (< 420px): icons only */
@media (max-width: 419px) {
  .tab-bar button { padding: 8px 4px; }
  .tab-bar-label-short { display: none; }
}

/* Filter slot (legacy, kept for compatibility) */
.tab-filter-slot:empty { display: none; }

/* Responsive — tablet */
@media (min-width: 600px) {
  #app { padding: 0 24px; }
  .subject-grid { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
}

/* Responsive — desktop */
@media (min-width: 1024px) {
  #app { max-width: 960px; padding: 0 32px; }

  /* Desktop tab bar: horizontal with labels inline */
  .tab-bar {
    position: static;
    margin-bottom: 20px;
  }
  .tab-bar button {
    flex: 0 0 auto;
    flex-direction: row;
    gap: 6px;
    padding: 12px 16px;
    font-size: 0.85rem;
    color: var(--text-secondary);
  }
}

/* Safe area for notched phones */
@supports (padding: max(0px)) {
  body {
    padding-left: env(safe-area-inset-left, 0);
    padding-right: env(safe-area-inset-right, 0);
  }
}
