/* ==========================================================================
   FIX 1 — Mobile topbar sticky
   ========================================================================== */

.topbar {
  position: sticky;
  z-index: 30;
  top: 0;

  display: none;
  align-items: center;
  justify-content: space-between;

  min-height: 56px;
  padding: 10px 16px;

  background: var(--surface);
  border-bottom: 1px solid var(--border);

  /* ensure it stays on top of content */
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

body.app-shell .topbar {
  display: flex;
}

@media (min-width: 1024px) {
  body.app-shell .topbar {
    display: none;
  }
}


/* ==========================================================================
   FIX 2 — AI Study Buddy page — cleaner, more breathable layout
   ========================================================================== */

/* Tighter, cleaner header */
.buddy-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;

  margin-bottom: 14px;
  padding-bottom: 14px;

  border-bottom: 1px solid var(--border);
}

.buddy-header-title {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 12px;
}

/* Smaller icon */
.buddy-header-icon {
  display: inline-flex;
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  align-items: center;
  justify-content: center;

  color: #ffffff;
  background: linear-gradient(135deg, var(--blue), var(--purple));
  border-radius: 11px;
  box-shadow: 0 5px 14px rgba(79, 110, 247, 0.22);

  font-size: 17px;
}

/* Tighter title text */
.buddy-header-title .page-eyebrow {
  margin-bottom: 1px;
  font-size: 10px;
}

.buddy-header-title h2 {
  margin: 0 0 2px;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.buddy-header-title p {
  color: var(--text-2);
  font-size: 10px;
  line-height: 1.4;
}

/* Smaller action buttons */
.buddy-header-actions {
  display: flex;
  flex-shrink: 0;
  gap: 7px;
}

.buddy-header-actions .btn {
  min-height: 34px;
  padding: 5px 11px;
  font-size: 12px;
}


/* Chat card — more height for messages */
.buddy-chat-card {
  display: flex;
  flex-direction: column;

  height: calc(100vh - 148px);
  min-height: 520px;
  max-height: 860px;

  overflow: hidden;

  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-sm);
}

/* Chat scroll area */
.buddy-chat {
  flex: 1;
  min-height: 0;
  overflow-y: auto;

  padding: 20px;

  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}

.buddy-chat::-webkit-scrollbar {
  width: 4px;
}

.buddy-chat::-webkit-scrollbar-track {
  background: transparent;
}

.buddy-chat::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 999px;
}


/* Composer — cleaner, more visible input */
.buddy-composer-wrap {
  flex-shrink: 0;
  padding: 12px 16px 14px;

  background: var(--surface);
  border-top: 1px solid var(--border);
}

.buddy-composer {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) 40px;
  align-items: end;
  gap: 8px;

  padding: 6px 8px 6px 10px;

  background: var(--surface-2);
  border: 1.5px solid var(--border-strong);
  border-radius: 14px;

  transition: border-color 0.15s, box-shadow 0.15s;
}

.buddy-composer:focus-within {
  background: var(--surface);
  border-color: var(--blue);
  box-shadow: 0 0 0 3px var(--blue-light);
}

/* Attachment button — smaller, tighter */
.buddy-attachment-button {
  display: inline-flex;
  width: 34px;
  height: 34px;
  align-items: center;
  justify-content: center;

  padding: 0;

  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 9px;

  cursor: pointer;
  font-size: 16px;

  transition: color 0.15s, background 0.15s;
}

.buddy-attachment-button:hover {
  color: var(--blue);
  background: var(--blue-light);
}

.buddy-attachment-button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

/* Textarea — clean, visible, auto-growing */
.buddy-composer textarea {
  width: 100%;
  min-height: 36px;
  max-height: 120px;

  padding: 8px 4px;
  overflow-y: auto;
  resize: none;

  color: var(--text);
  background: transparent;
  border: 0;
  outline: 0;

  font-size: 13px;
  line-height: 1.55;
}

.buddy-composer textarea::placeholder {
  color: var(--muted);
  font-size: 13px;
}

/* Send button */
.buddy-send-button {
  display: inline-flex;
  width: 38px;
  height: 38px;
  align-items: center;
  justify-content: center;

  color: #ffffff;
  background: var(--blue);
  border: 0;
  border-radius: 10px;

  font-size: 15px;

  transition: background 0.15s, transform 0.15s;
}

.buddy-send-button:hover {
  background: var(--blue-dark);
  transform: scale(1.04);
}

.buddy-send-button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
  transform: none;
}

/* Meta row below composer */
.buddy-composer-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;

  padding: 6px 2px 0;

  color: var(--muted);
  font-size: 9px;
}

/* Privacy note */
.buddy-image-privacy {
  display: none; /* hidden by default, only show when image attached */
}

.has-image .buddy-image-privacy {
  display: block;
  margin: 7px 0 0;
  color: var(--muted);
  font-size: 8px;
  line-height: 1.45;
  text-align: center;
}


/* ==========================================================================
   FIX 3 — Message bubbles — cleaner, more readable
   ========================================================================== */

.buddy-message {
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr);
  align-items: flex-start;
  gap: 9px;

  max-width: 100%;
  margin-bottom: 16px;
  animation: buddy-msg-in 0.18s ease both;
}

@keyframes buddy-msg-in {
  from { opacity: 0; transform: translateY(5px); }
  to   { opacity: 1; transform: translateY(0); }
}

.buddy-message-user {
  grid-template-columns: minmax(0, 1fr) 32px;
  margin-left: auto;
  max-width: 85%;
}

.buddy-message-user .buddy-message-avatar {
  order: 2;
}

.buddy-message-user .buddy-message-body {
  order: 1;
}

.buddy-message-avatar {
  display: inline-flex;
  width: 32px;
  height: 32px;
  align-items: center;
  justify-content: center;

  color: var(--blue);
  background: var(--blue-light);
  border: 1px solid var(--blue-mid);
  border-radius: 9px;

  font-size: 13px;
}

.buddy-message-user .buddy-message-avatar {
  color: var(--text-2);
  background: var(--surface-2);
  border-color: var(--border);
}

.buddy-message-label {
  display: block;
  margin-bottom: 4px;

  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.buddy-message-user .buddy-message-label {
  text-align: right;
}

/* AI message bubble */
.buddy-message-content {
  padding: 12px 14px;

  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px 14px 14px 14px;

  font-size: 13px;
  line-height: 1.7;
}

/* User message bubble */
.buddy-message-user .buddy-message-content {
  color: #ffffff;
  background: var(--blue);
  border-color: var(--blue);
  border-radius: 14px 4px 14px 14px;
}

/* Markdown content inside bubbles */
.buddy-message-content p { margin-bottom: 9px; }
.buddy-message-content p:last-child { margin-bottom: 0; }
.buddy-message-content h1 { font-size: 16px; margin: 14px 0 6px; color: var(--text); }
.buddy-message-content h2 { font-size: 14px; margin: 12px 0 5px; color: var(--text); }
.buddy-message-content h3 { font-size: 13px; margin: 10px 0 4px; color: var(--text); }
.buddy-message-content ul,
.buddy-message-content ol {
  display: grid;
  gap: 5px;
  margin: 8px 0;
  padding-left: 18px;
  list-style: initial;
}
.buddy-message-content ol { list-style: decimal; }
.buddy-message-content li::marker { color: var(--blue); }
.buddy-message-user .buddy-message-content li::marker { color: rgba(255,255,255,0.6); }
.buddy-message-content strong { color: var(--text); font-weight: 700; }
.buddy-message-user .buddy-message-content strong { color: #ffffff; }
.buddy-message-content code {
  padding: 2px 5px;
  color: var(--purple);
  background: var(--purple-bg);
  border-radius: 4px;
  font-size: 0.9em;
}

/* Copy action */
.buddy-message-actions { display: flex; gap: 4px; margin-top: 5px; }
.buddy-message-action {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 7px;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 6px;
  font-size: 9px;
  transition: color 0.15s, background 0.15s;
}
.buddy-message-action:hover { color: var(--text); background: var(--surface-2); }


/* ==========================================================================
   FIX 4 — Welcome screen — cleaner
   ========================================================================== */

.buddy-welcome {
  display: flex;
  min-height: 100%;
  flex-direction: column;
  align-items: center;
  justify-content: center;

  padding: 28px 20px;
  text-align: center;
}

.buddy-welcome-icon {
  width: 56px;
  height: 56px;
  margin-bottom: 12px;
  font-size: 22px;
  border-radius: 16px;
}

.buddy-welcome .badge { margin-bottom: 12px; }

.buddy-welcome h2 {
  max-width: 520px;
  margin-bottom: 6px;
  font-size: 21px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.buddy-welcome > p {
  max-width: 480px;
  color: var(--text-2);
  font-size: 12px;
  line-height: 1.6;
}

.buddy-suggestions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  width: min(100%, 640px);
  gap: 8px;
  margin-top: 20px;
}

.buddy-suggestion-card {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) 14px;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  color: var(--text);
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 11px;
  transition: border-color 0.15s, background 0.15s, transform 0.15s;
}

.buddy-suggestion-card:hover {
  background: var(--blue-light);
  border-color: var(--blue-mid);
  transform: translateY(-1px);
}

.buddy-suggestion-icon {
  width: 36px;
  height: 36px;
  color: var(--blue);
  background: var(--blue-light);
  border-radius: 9px;
  font-size: 14px;
}

.buddy-suggestion-card strong { font-size: 11px; margin-bottom: 2px; }
.buddy-suggestion-card small {
  color: var(--muted);
  font-size: 9px;
  line-height: 1.35;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.buddy-suggestion-card > i { color: var(--muted); font-size: 11px; }


/* ==========================================================================
   FIX 5 — Thinking indicator
   ========================================================================== */

.buddy-thinking {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 11px 14px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px 14px 14px 14px;
}

.buddy-thinking > span {
  width: 6px; height: 6px;
  background: var(--blue);
  border-radius: 50%;
  animation: buddy-thinking-bounce 1.1s ease-in-out infinite;
}
.buddy-thinking > span:nth-child(2) { animation-delay: 0.15s; }
.buddy-thinking > span:nth-child(3) { animation-delay: 0.3s; }
.buddy-thinking small { margin-left: 6px; color: var(--muted); font-size: 9px; }


/* ==========================================================================
   FIX 6 — Image preview — tighter
   ========================================================================== */

.buddy-image-preview {
  display: grid;
  grid-template-columns: 70px minmax(0, 1fr) 30px;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  padding: 9px;
  background: var(--blue-light);
  border: 1px solid var(--blue-mid);
  border-radius: 12px;
}

.buddy-image-thumbnail-wrap {
  position: relative;
  width: 70px;
  height: 58px;
}

.buddy-image-thumbnail {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
}

.buddy-image-remove {
  display: inline-flex;
  width: 28px; height: 28px;
  align-items: center; justify-content: center;
  padding: 0;
  color: var(--text-2);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  transition: color 0.15s, border-color 0.15s;
}
.buddy-image-remove:hover { color: var(--red); border-color: var(--red); }


/* ==========================================================================
   MOBILE — buddy page
   ========================================================================== */

@media (max-width: 767px) {
  .buddy-header {
    padding: 0 0 12px;
    margin-bottom: 12px;
    flex-wrap: nowrap;
  }

  .buddy-header-title h2 { font-size: 16px; }
  .buddy-header-title p { display: none; }

  .buddy-header-actions .btn {
    min-height: 32px;
    padding: 5px 9px;
    font-size: 11px;
  }

  .buddy-chat-card {
    height: calc(100dvh - 140px - env(safe-area-inset-bottom));
    min-height: 420px;
    border-radius: 12px;
  }

  .buddy-chat { padding: 14px 12px; }

  .buddy-message { gap: 7px; }
  .buddy-message-user { max-width: 92%; }

  .buddy-message-content {
    padding: 10px 12px;
    font-size: 12px;
  }

  .buddy-suggestions { grid-template-columns: 1fr; }

  .buddy-composer-wrap {
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  }

  .buddy-composer {
    grid-template-columns: 34px minmax(0, 1fr) 36px;
    gap: 6px;
  }

  .buddy-composer-meta > span:first-child { display: none; }
  .buddy-composer-meta { justify-content: flex-end; }
}
