/**
 * StudyMate app shell & chat layout
 * Loaded after studymate-app.css — fixes missing shell rules and scroll chain.
 */

/* ─── Root shell ─── */
html {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  font-family: var(--sm-font);
  font-size: var(--sm-text-base);
  line-height: var(--sm-leading-normal);
  color: var(--sm-text);
  background: var(--sm-bg);
  background-image: none;
  -webkit-font-smoothing: antialiased;
}

.app-container {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  min-height: 0;
}

/* ─── Top navigation ─── */
.top-nav {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  height: var(--topnav-height);
  min-height: var(--topnav-height);
  padding: 0 var(--space-4);
  background: var(--sm-surface-raised);
  border-bottom: 1px solid var(--sm-border);
  box-shadow: none;
  z-index: 10;
}

.top-nav .logo {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: var(--space-3);
  font-size: var(--sm-text-lg);
  font-weight: var(--sm-weight-bold);
  color: var(--sm-text);
  letter-spacing: -0.02em;
}

.top-nav .logo::before {
  content: '';
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  background: url('../assets/logo.png') center / contain no-repeat;
}

.top-nav .toggle-sidebar-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  margin: 0;
  padding: 0;
  border-radius: var(--radius-sm);
  background: var(--sm-surface-muted);
  border: 1px solid var(--sm-border);
  color: var(--sm-ink-secondary);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  line-height: 1;
  transition: background var(--duration-fast) var(--ease-out);
}

.top-nav .toggle-sidebar-btn:hover {
  background: var(--sm-surface-inset);
  color: var(--sm-text);
  transform: none;
  box-shadow: none;
}

.profile-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  background: var(--sm-surface-soft);
  border: 1px solid var(--sm-border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color var(--duration-fast) var(--ease-out),
    box-shadow var(--duration-fast) var(--ease-out);
  box-shadow: none;
}

.profile-btn::before {
  content: '';
  width: 18px;
  height: 18px;
  background: center / contain no-repeat
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%23667085' stroke-width='1.75'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M15.75 6a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0ZM4.5 20.25a8.25 8.25 0 0 1 15 0'/%3E%3C/svg%3E");
}

.profile-btn:hover {
  border-color: var(--sm-ai-border);
  transform: none;
  box-shadow: none;
}

.logout-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  background: var(--sm-surface-muted);
  border: 1px solid var(--sm-border);
  color: var(--sm-muted);
  font-size: 1rem;
  padding: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-shadow: none;
}

.logout-btn:hover {
  background: var(--sm-danger-bg);
  border-color: rgba(217, 45, 32, 0.25);
  color: var(--sm-danger);
  transform: none;
}

/* ─── Main grid: sidebar | chat | tools ─── */
.main-content {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  gap: var(--space-3);
  padding: var(--space-3);
  overflow: hidden;
}

/* ─── Sidebar ─── */
.chat-selector {
  flex: 0 0 var(--sidebar-width);
  width: var(--sidebar-width);
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--sm-surface);
  border: 1px solid var(--sm-border);
  border-radius: var(--radius-lg);
  box-shadow: none;
  overflow: hidden;
}

.chat-selector-header {
  flex-shrink: 0;
  padding: var(--space-4);
  border-bottom: 1px solid var(--sm-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  font-size: var(--sm-text-sm);
  font-weight: var(--sm-weight-semibold);
  color: var(--sm-text);
}

.chat-search-container {
  flex-shrink: 0;
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--sm-border);
}

.chat-search-input {
  width: 100%;
  padding: var(--space-2) var(--space-3);
  font-size: var(--sm-text-sm);
  border: 1px solid var(--sm-border);
  border-radius: var(--radius-sm);
  background: var(--sm-surface-muted);
  color: var(--sm-text);
}

.chat-search-input::placeholder {
  color: var(--sm-ink-placeholder);
}

.chat-search-input:focus {
  outline: none;
  border-color: var(--sm-border-focus);
  box-shadow: var(--shadow-focus);
  background: var(--sm-surface);
}

.academic-profile-banner {
  flex-shrink: 0;
  margin: var(--space-2) var(--space-3) 0;
  padding: var(--space-3);
  border-radius: var(--radius-sm);
  background: var(--sm-surface-soft);
  border: 1px solid var(--sm-ai-border);
  font-size: var(--sm-text-xs);
  line-height: var(--sm-leading-relaxed);
  color: var(--sm-ink-secondary);
}

.academic-profile-banner strong {
  color: var(--sm-text);
  font-weight: var(--sm-weight-semibold);
}

.sidebar-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}

.classes-panel {
  flex-shrink: 0;
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--sm-border);
  background: var(--sm-surface-muted);
}

.classes-panel-title,
.sidebar-section-label {
  font-size: 0.6875rem;
  font-weight: var(--sm-weight-semibold);
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--sm-muted);
}

.chat-list {
  flex: 0 1 auto;
  padding: var(--space-2);
  gap: 2px;
}

.chat-item {
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  background: transparent;
  border: 1px solid transparent;
  color: var(--sm-text);
  font-size: var(--sm-text-sm);
}

.chat-item:hover {
  background: var(--sm-surface-muted);
  border-color: transparent;
  transform: none;
}

.chat-item.active {
  background: var(--sm-ai-highlight);
  border-color: var(--sm-ai-border);
  box-shadow: none;
}

.chat-item-title {
  font-size: var(--sm-text-sm);
  font-weight: var(--sm-weight-medium);
}

.delete-chat-btn {
  width: 26px;
  height: 26px;
  min-width: 26px;
  border-radius: var(--radius-sm);
  background: transparent !important;
  border: none;
  color: var(--sm-muted) !important;
  font-size: 0.75rem;
  opacity: 0;
  box-shadow: none !important;
  transition: opacity var(--duration-fast), background var(--duration-fast),
    color var(--duration-fast);
}

.chat-item:hover .delete-chat-btn,
.chat-item:focus-within .delete-chat-btn {
  opacity: 1;
}

.delete-chat-btn:hover {
  background: var(--sm-danger-bg) !important;
  color: var(--sm-danger) !important;
  transform: none !important;
}

.new-chat-btn {
  width: 32px;
  height: 32px;
  min-width: 32px;
  font-size: 1.25rem;
  background: var(--sm-gradient-cta);
  color: var(--sm-text);
  border: 1px solid var(--sm-ai-border);
  box-shadow: none;
}

.class-project-header.active {
  background: var(--sm-ai-highlight);
}

.class-context-badge {
  background: var(--sm-surface-soft);
  color: var(--sm-brand-700);
  border: 1px solid var(--sm-ai-border);
  font-weight: var(--sm-weight-medium);
}

/* ─── Chat panel ─── */
.chat-container {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--sm-surface);
  border: 1px solid var(--sm-border);
  border-radius: var(--radius-lg);
  box-shadow: none;
  overflow: hidden;
}

.chat-panel-body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-header {
  flex-shrink: 0;
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--sm-border);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) var(--space-3);
  color: var(--sm-text);
  font-size: var(--sm-text-sm);
}

.chat-header-title {
  flex: 1 1 180px;
  min-width: 0;
  font-weight: var(--sm-weight-semibold);
  font-size: var(--sm-text-base);
  line-height: var(--sm-leading-tight);
}

.chat-header-actions {
  flex: 1 1 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: var(--space-2);
}

.advanced-reasoning-toggle,
.step-by-step-toggle {
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-sm);
  background: var(--sm-surface-muted);
  border: 1px solid var(--sm-border);
  color: var(--sm-ink-secondary);
  font-size: var(--sm-text-xs);
  font-weight: var(--sm-weight-medium);
  gap: var(--space-2);
}

.advanced-reasoning-toggle.active {
  background: var(--sm-surface-soft);
  border-color: var(--sm-ai-border);
  color: var(--sm-text);
}

.step-by-step-toggle.active {
  background: var(--sm-success-bg);
  border-color: rgba(7, 148, 85, 0.35);
  color: var(--sm-success);
}

.toggle-switch.active {
  background: color-mix(in srgb, var(--sm-lavender) 55%, var(--sm-border-strong));
}

.export-btn {
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-sm);
  background: var(--sm-surface);
  border: 1px solid var(--sm-border);
  color: var(--sm-ink-secondary);
  font-size: var(--sm-text-xs);
  font-weight: var(--sm-weight-medium);
}

.export-btn:hover {
  background: var(--sm-surface-muted);
  color: var(--sm-text);
}

.entitlements-chip {
  font-size: var(--sm-text-xs);
  color: var(--sm-muted);
  background: var(--sm-surface-muted);
  border-color: var(--sm-border);
}

/* Message list — sole scroll region */
.chat-messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  scroll-behavior: smooth;
}

.chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: var(--sm-border-strong);
  border-radius: var(--radius-full);
}

/* Composer — pinned bottom */
.chat-input-area {
  flex-shrink: 0;
  padding: var(--space-3) var(--space-4);
  border-top: 1px solid var(--sm-border);
  background: var(--sm-surface-muted);
  border-radius: 0;
}

.chat-ai-disclaimer {
  margin: var(--space-2) 0 0;
  padding: 0;
  font-size: 11px;
  line-height: 1.35;
  color: var(--sm-muted);
  text-align: center;
  user-select: none;
  pointer-events: none;
}

.chat-input {
  background: var(--sm-surface-raised);
  border: 1px solid var(--sm-border);
  color: var(--sm-text);
  border-radius: var(--radius-md);
  min-height: 44px;
  padding: var(--space-3) var(--space-4);
}

.chat-input:focus {
  border-color: var(--sm-border-focus);
  box-shadow: var(--shadow-focus);
}

.chat-input::placeholder {
  color: var(--sm-ink-placeholder);
}

.send-btn {
  width: 44px;
  height: 44px;
  background: var(--sm-gradient-cta);
  color: var(--sm-text);
  border: 1px solid var(--sm-ai-border);
  box-shadow: none;
}

.send-btn.is-generating {
  background: var(--sm-surface-muted);
  border-color: var(--sm-border);
  color: var(--sm-ink);
}

.attach-btn {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  background: var(--sm-surface-muted);
  border: 1px solid var(--sm-border);
  color: var(--sm-muted);
  font-size: 1.1rem;
}

.attach-btn:hover {
  background: var(--sm-surface-soft);
  border-color: var(--sm-ai-border);
  transform: none;
}

/* Messages */
.message {
  max-width: min(85%, 40rem);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  font-size: var(--sm-text-base);
  line-height: var(--sm-leading-relaxed);
}

.message.ai {
  align-self: flex-start;
  background: var(--sm-surface-raised);
  border: 1px solid var(--sm-border);
  color: var(--sm-text);
  box-shadow: none;
}

.message.student {
  align-self: flex-end;
  background: var(--sm-gradient-user);
  color: var(--sm-text);
  font-weight: var(--sm-weight-medium);
  border: 1px solid var(--sm-user-bubble-border);
  box-shadow: 0 1px 2px rgba(90, 106, 158, 0.08);
}

.message.ai .message code {
  background: var(--sm-surface-muted);
  color: var(--sm-text);
}

.message.ai .message pre {
  background: #1e293b;
}

.welcome-message {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 8rem;
  padding: var(--space-6);
  text-align: center;
  color: var(--sm-muted);
}

.welcome-message h2 {
  font-size: var(--sm-text-lg);
  font-weight: var(--sm-weight-semibold);
  color: var(--sm-text);
  margin-bottom: var(--space-2);
}

.welcome-message p {
  font-size: var(--sm-text-sm);
  color: var(--sm-muted);
  max-width: 22rem;
  line-height: var(--sm-leading-relaxed);
}

.typing-indicator {
  background: var(--sm-surface-muted);
  border: 1px solid var(--sm-border);
  color: var(--sm-muted);
}

.typing-dot {
  background: var(--sm-lavender);
}

.selection-tooltip {
  position: fixed;
  z-index: 500;
}

/* ─── Right tools panel ─── */
.progress-sidebar {
  flex: 0 0 var(--tools-width);
  width: var(--tools-width);
  min-width: 0;
  min-height: 0;
  max-width: var(--tools-width);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  overflow-y: auto;
  overflow-x: hidden;
  align-self: stretch;
}

.progress-card {
  flex: 0 0 auto;
  padding: var(--space-4);
  background: var(--sm-surface);
  border: 1px solid var(--sm-border);
  border-radius: var(--radius-md);
  box-shadow: none;
}

.progress-card:hover {
  transform: none;
  box-shadow: none;
  border-color: var(--sm-border-strong);
}

.progress-sidebar .pref-card {
  flex: 0 0 auto;
  padding: var(--space-3);
}

.progress-sidebar .card-title {
  margin-bottom: var(--space-2);
}

.progress-sidebar .ai-desc-card {
  flex: 0 0 auto;
  padding: var(--space-3);
}

.progress-sidebar .ai-desc-card .card-title {
  margin-bottom: var(--space-2);
}

.ai-desc-textarea {
  width: 100%;
  box-sizing: border-box;
  height: 128px;
  min-height: 128px;
  max-height: 128px;
  margin-bottom: var(--space-2);
  padding: var(--space-2);
  border: 1px solid var(--sm-border);
  border-radius: var(--radius-sm);
  background: var(--sm-surface-inset);
  color: var(--sm-text);
  font-family: inherit;
  font-size: var(--sm-text-sm);
  line-height: var(--sm-leading-normal);
  resize: none;
  overflow-y: auto;
}

.ai-desc-textarea::placeholder {
  color: var(--sm-ink-placeholder);
}

.ai-desc-textarea:focus {
  outline: none;
  border-color: var(--sm-ai-border);
  box-shadow: var(--shadow-focus);
}

.ai-desc-footer {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-1);
  width: 100%;
}

.ai-desc-save-btn {
  width: 100%;
  box-sizing: border-box;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--sm-ai-border);
  border-radius: var(--radius-sm);
  background: var(--sm-gradient-cta);
  color: var(--sm-ink-inverse);
  font-size: var(--sm-text-sm);
  font-weight: var(--sm-weight-semibold);
  font-family: inherit;
  text-align: center;
  cursor: pointer;
  box-shadow: var(--shadow-brand);
}

.ai-desc-save-btn:hover:not(:disabled) {
  background: var(--sm-gradient-cta-hover);
}

.ai-desc-save-btn:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.progress-sidebar .ai-description-status {
  font-size: var(--sm-text-xs);
  line-height: var(--sm-leading-normal);
  color: var(--sm-muted);
}

.progress-sidebar .ai-description-status.error {
  color: var(--sm-danger, #c0392b);
}

.card-title {
  color: var(--sm-text);
  font-size: var(--sm-text-sm);
  font-weight: var(--sm-weight-semibold);
  margin-bottom: var(--space-3);
}

.timer-display {
  color: var(--sm-text);
  font-size: var(--sm-text-2xl);
  text-shadow: none;
}

/* Compact work timer — no header row; tighter spacing in the right sidebar */
.progress-sidebar .timer-card {
  padding: 10px var(--space-3) var(--space-2);
}

.progress-sidebar .timer-card .timer-display {
  font-size: 1.625rem;
  font-weight: var(--sm-weight-semibold);
  text-align: center;
  margin: 0 0 var(--space-1);
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}

.progress-sidebar .timer-card .timer-controls {
  display: flex;
  gap: var(--space-1);
  margin-bottom: var(--space-1);
}

.progress-sidebar .timer-card .timer-btn {
  flex: 1;
  padding: 5px var(--space-2);
  border: none;
  border-radius: var(--radius-sm);
  font-weight: var(--sm-weight-semibold);
  font-size: var(--sm-text-xs);
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.progress-sidebar .timer-card .timer-presets {
  display: flex;
  gap: var(--space-1);
  margin-bottom: var(--space-1);
}

.progress-sidebar .timer-card .preset-btn {
  flex: 1;
  padding: 3px var(--space-1);
  border-radius: var(--radius-sm);
  font-size: var(--sm-text-xs);
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.progress-sidebar .timer-card .timer-custom {
  display: flex;
  gap: var(--space-1);
  padding-top: var(--space-1);
  margin-top: 0;
  border-top: 1px solid var(--sm-border);
}

.progress-sidebar .timer-card .custom-time-input {
  flex: 1;
  padding: 4px var(--space-2);
  border-radius: var(--radius-sm);
  font-size: var(--sm-text-xs);
}

.progress-sidebar .timer-card .custom-time-btn {
  padding: 4px var(--space-2);
  border: none;
  border-radius: var(--radius-sm);
  font-weight: var(--sm-weight-semibold);
  font-size: var(--sm-text-xs);
  cursor: pointer;
  color: var(--sm-ink-inverse);
  transition: background-color 0.15s ease;
}

.timer-btn-start {
  background: var(--sm-success);
  color: #fff;
}

.timer-btn-pause {
  background: var(--sm-warning);
  color: #fff;
}

.preset-btn.active {
  background: var(--sm-ai-highlight);
  border-color: var(--sm-ai-border);
  color: var(--sm-text);
}

.pref-slider::-webkit-slider-thumb,
.pref-slider::-moz-range-thumb {
  background: var(--sm-lavender);
}

.pref-slider-labels,
.stat-label,
.progress-text {
  color: var(--sm-muted);
}

/* Progress sidebar — readable controls on light lavender cards */
.progress-sidebar .pref-slider {
  background: var(--sm-surface-inset);
}

.progress-sidebar .pref-slider-value,
.progress-sidebar .pref-detail-value {
  background: var(--sm-ai-highlight);
  border: 1px solid var(--sm-ai-border);
  color: var(--sm-brand-700);
}

.progress-sidebar .timer-btn-reset {
  background: var(--sm-surface-soft);
  color: var(--sm-ink);
  border: 1px solid var(--sm-border);
}

.progress-sidebar .timer-btn-reset:hover:not(:disabled) {
  background: var(--sm-ai-highlight);
  border-color: var(--sm-ai-border);
  color: var(--sm-brand-700);
}

.progress-sidebar .timer-btn-reset:disabled {
  background: var(--sm-surface-muted);
  color: var(--sm-ink-muted);
  border-color: var(--sm-border);
  opacity: 0.9;
  cursor: not-allowed;
}

.progress-sidebar .timer-btn-reset:focus-visible {
  outline: 2px solid var(--sm-border-focus);
  outline-offset: 2px;
}

.progress-sidebar .custom-time-input {
  background: var(--sm-surface-soft);
  color: var(--sm-ink);
  border: 1px solid var(--sm-border);
}

.progress-sidebar .custom-time-input::placeholder {
  color: var(--sm-ink-placeholder);
}

.progress-sidebar .custom-time-input:focus {
  background: var(--sm-surface-raised);
  border-color: var(--sm-border-focus);
  box-shadow: var(--shadow-focus);
}

.progress-sidebar .custom-time-input:disabled {
  background: var(--sm-surface-muted);
  color: var(--sm-ink-muted);
  cursor: not-allowed;
}

.progress-sidebar .timer-custom {
  border-top-color: var(--sm-border);
}

.progress-sidebar .preset-btn {
  background: var(--sm-surface-muted);
  color: var(--sm-ink-secondary);
  border-color: var(--sm-border);
}

.progress-sidebar .preset-btn:hover {
  background: var(--sm-surface-inset);
  border-color: var(--sm-border-strong);
  color: var(--sm-ink);
}

/* ─── Responsive ─── */
@media (max-width: 1100px) {
  .progress-sidebar {
    display: none;
  }
}

@media (max-width: 900px) {
  :root {
    --sidebar-width: 248px;
  }
}

/* Focus */
button:focus-visible,
input:focus-visible,
textarea:focus-visible,
[role='button']:focus-visible {
  outline: 2px solid var(--sm-lavender);
  outline-offset: 2px;
}

/* ─── Ask StudyMate: selection + mini composer / inline Q&A ─── */
.message.ai .message-text ::selection {
  background: color-mix(in srgb, var(--sm-periwinkle) 34%, var(--sm-surface-muted));
  color: inherit;
}

.message.ai mark.sm-saved-highlight {
  background: var(--sm-ai-highlight);
  color: inherit;
  border-radius: 4px;
  padding: 0.1em 0.2em;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

.sm-mini-card {
  box-sizing: border-box;
  width: min(320px, calc(100vw - 24px));
  padding: var(--space-3) var(--space-4);
  background: var(--sm-surface);
  border: 1px solid var(--sm-ai-border);
  border-radius: var(--radius-lg);
  box-shadow:
    0 4px 6px rgba(26, 31, 46, 0.04),
    0 16px 40px rgba(90, 106, 158, 0.14);
  font-family: var(--sm-font);
  font-size: var(--sm-text-sm);
  line-height: var(--sm-leading-normal);
  color: var(--sm-text);
}

.sm-mini-header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-2);
  font-weight: var(--sm-weight-semibold);
  font-size: var(--sm-text-sm);
  color: var(--sm-brand-700);
}

.sm-mini-logo {
  flex-shrink: 0;
  border-radius: 4px;
}

.sm-mini-quote {
  margin: 0 0 var(--space-3);
  padding: var(--space-2) var(--space-3);
  border-left: 3px solid var(--sm-lavender);
  background: var(--sm-ai-highlight);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: var(--sm-text-sm);
  color: var(--sm-muted);
  font-style: italic;
  line-height: var(--sm-leading-relaxed);
}

.sm-mini-question-input {
  display: block;
  box-sizing: border-box;
  width: 100%;
  min-height: 42px;
  margin: 0;
  padding: 10px 12px;
  border: 1px solid var(--sm-border);
  border-radius: var(--radius-md);
  background: var(--sm-surface-soft);
  font-family: var(--sm-font);
  font-size: var(--sm-text-base);
  line-height: var(--sm-leading-normal);
  color: var(--sm-text);
}

.sm-mini-question-input::placeholder {
  color: var(--sm-ink-placeholder);
}

.sm-mini-question-input:focus {
  outline: none;
  border-color: var(--sm-lavender);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--sm-lavender) 22%, transparent);
  background: var(--sm-surface);
}

.sm-mini-question,
.sm-mini-answer-body {
  margin: var(--space-2) 0;
  line-height: var(--sm-leading-relaxed);
}

.sm-mini-question {
  font-weight: var(--sm-weight-medium);
  color: var(--sm-text);
}

.sm-mini-answer-body {
  color: var(--sm-muted);
}

.sm-mini-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-3);
}

.sm-mini-btn {
  flex: 1 1 auto;
  min-height: 36px;
  padding: 8px 12px;
  border: 1px solid var(--sm-border);
  border-radius: var(--radius-md);
  background: var(--sm-surface-soft);
  font-family: var(--sm-font);
  font-size: var(--sm-text-sm);
  font-weight: var(--sm-weight-medium);
  color: var(--sm-text);
  cursor: pointer;
}

.sm-mini-btn:hover {
  background: var(--sm-ai-highlight);
  border-color: var(--sm-ai-border);
  color: var(--sm-brand-700);
}

.sm-mini-open-full {
  background: linear-gradient(135deg, var(--sm-lavender), var(--sm-periwinkle));
  border-color: transparent;
  color: #fff;
}

.sm-mini-open-full:hover {
  color: #fff;
  filter: brightness(1.05);
}

.sm-mini-error {
  margin: 0 0 var(--space-2);
  color: var(--sm-danger);
  font-size: var(--sm-text-sm);
}

.message.student.sm-ask-studymate-question {
  cursor: pointer;
}

.message.student.sm-ask-studymate-question:hover {
  box-shadow: 0 0 0 1px var(--sm-ai-border);
}

/* ═══════════════════════════════════════════════════════════════════════════
   Responsive — tablet & mobile only (desktop ≥1024px unchanged above)
   ═══════════════════════════════════════════════════════════════════════════ */

/* ─── Tablet responsive rules (768px–1023px, iPad Safari) ─── */
@media (min-width: 768px) and (max-width: 1023px) {
  html,
  body,
  .app-container {
    max-width: 100vw;
    overflow-x: hidden;
  }

  .main-content {
    flex-direction: row;
    padding: var(--space-2);
    gap: var(--space-2);
    min-width: 0;
    max-width: 100%;
  }

  .chat-selector {
    position: relative;
    top: auto;
    left: auto;
    bottom: auto;
    height: auto;
    flex: 0 0 min(248px, 30vw);
    width: min(248px, 30vw);
    max-width: 248px;
    min-width: 0;
    transform: none;
    box-shadow: none;
  }

  .chat-selector.hidden {
    transform: none;
    opacity: 0;
    width: 0;
    flex: 0;
    pointer-events: none;
  }

  .chat-container {
    flex: 1 1 auto;
    min-width: 0;
    max-width: 100%;
  }

  .top-nav {
    flex-wrap: wrap;
    row-gap: var(--space-2);
    height: auto;
    min-height: var(--topnav-height);
    padding-top: var(--space-2);
    padding-bottom: var(--space-2);
  }

  .chat-header {
    padding: var(--space-2) var(--space-3);
    gap: var(--space-2);
  }

  .chat-header-actions {
    flex-wrap: wrap;
    gap: var(--space-2);
  }

  .chat-messages {
    padding: var(--space-3);
  }

  .chat-input-area {
    padding: var(--space-2) var(--space-3);
  }

  .message {
    max-width: min(88%, 36rem);
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .message-text,
  .message-text p,
  .message-text li {
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  /* ─── Tablet tools behavior — tools row below chat, no horizontal overflow ─── */
  .chat-header-study-tools-row,
  .study-tools-btn {
    display: none !important;
  }

  .progress-sidebar {
    display: flex;
    flex: 1 1 100%;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: stretch;
    order: 10;
    gap: var(--space-3);
    padding: var(--space-3);
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--sm-border);
    border-radius: var(--radius-lg);
    background: var(--sm-surface-muted);
  }

  .progress-sidebar .progress-card {
    flex: 0 0 min(272px, 88vw);
    min-width: min(272px, 88vw);
    max-width: min(272px, 88vw);
  }

  .main-content {
    flex-wrap: wrap;
    align-content: flex-start;
    gap: var(--space-3);
    padding: var(--space-3);
  }
}

/* Study Tools header row — hidden by default; shown on mobile only (see ≤767px block) */
.chat-header-study-tools-row {
  display: none;
  flex: 1 1 100%;
  width: 100%;
  min-width: 0;
  box-sizing: border-box;
}

.study-tools-btn {
  display: none;
  box-sizing: border-box;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  width: 100%;
  min-height: 40px;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-md);
  background: var(--sm-surface-soft);
  border: 1px solid var(--sm-ai-border);
  color: var(--sm-brand-700);
  font-size: var(--sm-text-sm);
  font-weight: var(--sm-weight-semibold);
  font-family: inherit;
  cursor: pointer;
  line-height: var(--sm-leading-tight);
  position: relative;
  z-index: 2;
  -webkit-tap-highlight-color: transparent;
}

.study-tools-btn:hover {
  background: var(--sm-ai-highlight);
  color: var(--sm-text);
}

/* Desktop — keep Study Tools row hidden (right panel unchanged) */
@media (min-width: 1024px) {
  .chat-header-study-tools-row,
  #study-tools-btn.study-tools-btn {
    display: none !important;
    visibility: hidden !important;
  }
}

/* ─── Mobile sidebar drawer overlay (shown only ≤767px via JS) ─── */
.sidebar-drawer-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: var(--z-sticky);
  margin: 0;
  padding: 0;
  border: none;
  background: var(--sm-overlay);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.sidebar-drawer-overlay.is-visible {
  display: block;
}

/* ─── Mobile study tools drawer overlay (≤767px via JS) ─── */
.study-tools-drawer-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-sticky) + 2);
  margin: 0;
  padding: 0;
  border: none;
  background: var(--sm-overlay);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.study-tools-drawer-overlay.is-visible {
  display: block;
}

/* ─── Mobile responsive rules (≤767px, iPhone Safari) ─── */
@media (max-width: 767px) {
  html,
  body,
  .app-container,
  .main-content,
  .chat-container,
  .chat-panel-body,
  .chat-messages,
  .chat-header,
  .chat-input-area,
  .top-nav {
    box-sizing: border-box;
    max-width: 100%;
  }

  html,
  body {
    width: 100%;
    max-width: 100vw;
    overflow-x: hidden;
  }

  body.sm-sidebar-drawer-open,
  body.sm-study-tools-open {
    overflow: hidden;
    touch-action: none;
  }

  body.sm-sidebar-drawer-open .app-container,
  body.sm-study-tools-open .app-container {
    overflow: hidden;
  }

  .app-container {
    max-width: 100vw;
    overflow-x: hidden;
  }

  /* ─── Mobile spacing cleanup ─── */
  .main-content {
    flex-direction: column;
    padding: var(--space-3);
    gap: var(--space-3);
    position: relative;
    min-width: 0;
    max-width: 100vw;
    overflow: hidden;
  }

  .top-nav {
    margin-bottom: 0;
  }

  .chat-header {
    overflow: visible;
    align-items: stretch;
  }

  .chat-header-actions {
    overflow: visible;
  }

  /* Mobile Study Tools — full-width row at end of header */
  .chat-header-study-tools-row {
    display: block;
    flex: 1 1 100%;
    width: 100%;
    order: 100;
    margin-top: var(--space-1);
    padding-top: var(--space-2);
    border-top: 1px solid var(--sm-border);
  }

  #study-tools-btn.study-tools-btn,
  .chat-header-study-tools-row .study-tools-btn {
    display: flex !important;
    visibility: visible !important;
    opacity: 1 !important;
    width: 100%;
    max-width: 100%;
  }

  /* Mobile sidebar drawer */
  .chat-selector {
    position: fixed;
    left: 0;
    top: var(--topnav-height);
    bottom: 0;
    height: auto;
    z-index: calc(var(--z-sticky) + 1);
    width: min(80vw, 320px);
    max-width: min(80vw, 320px);
    flex: none;
    margin: 0;
    border-radius: 0 var(--radius-lg) var(--radius-lg) 0;
    box-shadow: var(--shadow-lg);
    transform: translateX(0);
    transition: transform var(--duration-normal) var(--ease-out),
      visibility var(--duration-normal);
    visibility: visible;
    opacity: 1;
    pointer-events: auto;
  }

  .chat-selector.hidden {
    transform: translateX(-105%);
    visibility: hidden;
    pointer-events: none;
    width: min(80vw, 320px);
    max-width: min(80vw, 320px);
    flex: none;
    opacity: 1;
    margin: 0;
    padding: 0;
    overflow: hidden;
  }

  .chat-container {
    flex: 1 1 auto;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }

  .chat-panel-body {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .chat-messages {
    flex: 1 1 auto;
    min-height: 0;
    padding: var(--space-4);
    gap: var(--space-3);
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Mobile study tools drawer — bottom sheet over chat */
  .progress-sidebar {
    display: flex;
    flex-direction: column;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: 100%;
    max-width: 100%;
    max-height: min(88vh, calc(100dvh - var(--topnav-height) - 12px));
    z-index: calc(var(--z-sticky) + 3);
    margin: 0;
    padding: var(--space-4);
    padding-bottom: calc(var(--space-4) + env(safe-area-inset-bottom, 0px));
    gap: var(--space-4);
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    background: var(--sm-surface);
    border: 1px solid var(--sm-border);
    border-bottom: none;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -8px 32px rgba(20, 24, 38, 0.12);
    transform: translateY(105%);
    visibility: hidden;
    pointer-events: none;
    transition: transform var(--duration-normal) var(--ease-out),
      visibility var(--duration-normal);
  }

  .progress-sidebar.is-open {
    transform: translateY(0);
    visibility: visible;
    pointer-events: auto;
  }

  .progress-sidebar .progress-card {
    flex: 0 0 auto;
    min-width: 0;
    max-width: 100%;
    width: 100%;
  }

  .top-nav {
    flex-wrap: nowrap;
    gap: var(--space-2);
    padding-left: max(var(--space-3), env(safe-area-inset-left, 0px));
    padding-right: max(var(--space-3), env(safe-area-inset-right, 0px));
  }

  .top-nav .logo {
    font-size: var(--sm-text-md);
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .chat-header {
    padding: var(--space-3) var(--space-4);
    flex-wrap: wrap;
    gap: var(--space-3);
    row-gap: var(--space-2);
  }

  .chat-header-title {
    flex: 1 1 100%;
    min-width: 0;
    margin-bottom: var(--space-1);
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .chat-header-actions {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: var(--space-2);
    row-gap: var(--space-2);
  }

  .advanced-reasoning-toggle,
  .step-by-step-toggle,
  .export-btn,
  .entitlements-chip {
    flex: 0 1 auto;
    max-width: 100%;
  }

  .chat-header-actions > * {
    max-width: 100%;
  }

  .message {
    max-width: 90%;
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .message.student {
    align-self: flex-end;
  }

  .message.ai {
    align-self: flex-start;
  }

  .message-text,
  .message-text p,
  .message-text a,
  .message-text li {
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .message-text pre {
    max-width: 100%;
    overflow-x: auto;
  }

  /* iOS Safari safe-area handling — composer stays above home indicator */
  .chat-input-area {
    flex-shrink: 0;
    margin-top: 0;
    padding-top: var(--space-3);
    padding-left: max(var(--space-4), env(safe-area-inset-left, 0px));
    padding-right: max(var(--space-4), env(safe-area-inset-right, 0px));
    padding-bottom: calc(var(--space-4) + env(safe-area-inset-bottom, 0px));
  }

  .chat-input-container {
    display: flex;
    flex-wrap: nowrap;
    align-items: flex-end;
    gap: var(--space-2);
    min-width: 0;
    width: 100%;
    max-width: 100%;
  }

  .chat-input {
    flex: 1 1 auto;
    min-width: 0;
    width: 0;
    max-width: 100%;
    min-height: 40px;
  }

  .attach-btn,
  .send-btn {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    min-width: 40px;
    max-width: 40px;
  }

  .sm-mini-card {
    width: min(320px, calc(100vw - 16px));
    max-width: calc(100vw - 16px);
  }
}
