/* ===== AI Layer Variables ===== */
:root {
  --ai-purple: #7c3aed;
  --ai-purple-light: #f5f3ff;
  --ai-purple-border: #ddd6fe;
  --ai-purple-dark: #5b21b6;
}

/* ===== API Key Modal ===== */
.ai-key-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ai-key-overlay.hidden { display: none; }
.ai-key-modal {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 32px;
  max-width: 420px;
  width: 90%;
  box-shadow: 0 20px 60px rgba(0,0,0,0.15);
}
.ai-key-modal h3 {
  font-size: 1.1rem;
  font-weight: 700;
  margin-bottom: 8px;
}
.ai-key-modal p {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 16px;
  line-height: 1.6;
}
.ai-key-modal input {
  width: 100%;
  padding: 10px 14px;
  border: 1.5px solid var(--border);
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 0.85rem;
  direction: ltr;
  margin-bottom: 12px;
}
.ai-key-modal input:focus {
  outline: none;
  border-color: var(--ai-purple);
}
.ai-key-modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-start;
}
.ai-key-save {
  padding: 8px 20px;
  background: var(--ai-purple);
  color: #fff;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 500;
}
.ai-key-save:hover { background: var(--ai-purple-dark); }
.ai-key-cancel {
  padding: 8px 16px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.85rem;
  color: var(--text-secondary);
}

/* ===== Selection Tooltip ===== */
.ai-tooltip {
  position: absolute;
  z-index: 500;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  padding: 4px;
  display: flex;
  gap: 2px;
}
.ai-tooltip.hidden { display: none; }
.ai-tooltip button {
  padding: 6px 12px;
  border: none;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--ai-purple);
  white-space: nowrap;
  transition: background 0.15s;
}
.ai-tooltip button:hover {
  background: var(--ai-purple-light);
}

/* ===== AI Callout (inline responses) ===== */
.ai-callout {
  background: var(--ai-purple-light);
  border: 1px solid var(--ai-purple-border);
  border-radius: var(--radius);
  padding: 14px 18px;
  margin: 14px 0;
  font-size: 0.9rem;
  line-height: 1.7;
  position: relative;
}
.ai-callout-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.ai-callout-label {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--ai-purple);
  display: flex;
  align-items: center;
  gap: 4px;
}
.ai-callout-close {
  width: 24px;
  height: 24px;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 1rem;
  color: var(--text-secondary);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ai-callout-close:hover { background: rgba(0,0,0,0.05); }
.ai-callout-body {
  color: var(--text);
}
.ai-callout-body p { margin-bottom: 8px; }
.ai-callout-body p:last-child { margin-bottom: 0; }
.ai-callout-body h3,
.ai-callout-body h4,
.ai-callout-body h5 {
  margin: 12px 0 6px;
  font-weight: 700;
  color: var(--ai-purple-dark, #5b21b6);
}
.ai-callout-body h3 { font-size: 1rem; }
.ai-callout-body h4 { font-size: 0.92rem; }
.ai-callout-body h5 { font-size: 0.88rem; }
.ai-callout-body ul,
.ai-callout-body ol {
  padding-right: 22px;
  margin: 6px 0;
}
.ai-callout-body li { margin-bottom: 4px; line-height: 1.65; }
.ai-callout-body code {
  background: rgba(124, 58, 237, 0.08);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: var(--mono, monospace);
  font-size: 0.85em;
}
.ai-callout-body strong { font-weight: 700; }

/* Streaming cursor */
.ai-streaming::after {
  content: '\25CF';
  color: var(--ai-purple);
  animation: ai-blink 0.6s infinite alternate;
  margin-inline-start: 2px;
}
@keyframes ai-blink { from { opacity: 1; } to { opacity: 0.2; } }

/* ===== Quick-Action Toolbar ===== */
.prose-block {
  position: relative;
}
.ai-quick-bar {
  position: absolute;
  top: 4px;
  left: 4px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
  z-index: 50;
}
.prose-block:hover .ai-quick-bar {
  opacity: 1;
  pointer-events: auto;
}
.ai-quick-btn {
  padding: 4px 10px;
  border: 1px solid var(--ai-purple-border);
  background: var(--card-bg);
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.7rem;
  font-weight: 500;
  color: var(--ai-purple);
  white-space: nowrap;
  transition: all 0.15s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.ai-quick-btn:hover {
  background: var(--ai-purple-light);
  border-color: var(--ai-purple);
}

/* ===== Side Chat Panel ===== */
.ai-chat-panel {
  position: fixed;
  top: 0;
  left: 0;
  width: 400px;
  height: 100%;
  background: var(--card-bg);
  border-right: 1px solid var(--border);
  box-shadow: 4px 0 24px rgba(0,0,0,0.08);
  z-index: 900;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
}
.ai-chat-panel.open {
  transform: translateX(0);
}
.ai-chat-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.ai-chat-header-title {
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--ai-purple);
  display: flex;
  align-items: center;
  gap: 6px;
}
.ai-chat-close {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 1.2rem;
  color: var(--text-secondary);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ai-chat-close:hover { background: #f3f4f6; }
.ai-chat-context {
  padding: 10px 18px;
  background: #f9fafb;
  border-bottom: 1px solid var(--border);
  font-size: 0.78rem;
  color: var(--text-secondary);
  line-height: 1.5;
  max-height: 80px;
  overflow-y: auto;
  flex-shrink: 0;
}
.ai-chat-context strong {
  color: var(--text);
  font-weight: 600;
}
.ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.ai-chat-msg {
  max-width: 92%;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 0.88rem;
  line-height: 1.65;
  word-break: break-word;
}
.ai-chat-msg.user {
  background: var(--accent-light);
  color: var(--text);
  align-self: flex-end;
  border-bottom-left: 4px;
}
.ai-chat-msg.assistant {
  background: var(--ai-purple-light);
  color: var(--text);
  align-self: flex-start;
  border-bottom-right: 4px;
}
.ai-chat-msg.assistant p { margin-bottom: 6px; }
.ai-chat-msg.assistant p:last-child { margin-bottom: 0; }
.ai-chat-msg.assistant h3,
.ai-chat-msg.assistant h4,
.ai-chat-msg.assistant h5 {
  margin: 10px 0 4px;
  font-weight: 700;
  color: var(--ai-purple-dark, #5b21b6);
}
.ai-chat-msg.assistant h3 { font-size: 0.95rem; }
.ai-chat-msg.assistant h4 { font-size: 0.9rem; }
.ai-chat-msg.assistant ul,
.ai-chat-msg.assistant ol {
  padding-right: 20px;
  margin: 4px 0;
}
.ai-chat-msg.assistant li { margin-bottom: 3px; line-height: 1.6; }
.ai-chat-msg.assistant code {
  background: rgba(124, 58, 237, 0.08);
  padding: 1px 4px;
  border-radius: 3px;
  font-family: var(--mono, monospace);
  font-size: 0.84em;
}
.ai-chat-msg.assistant strong { font-weight: 700; }
.ai-chat-input-row {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}
.ai-chat-input {
  flex: 1;
  padding: 8px 12px;
  border: 1.5px solid var(--border);
  border-radius: 6px;
  font-family: var(--font);
  font-size: 0.88rem;
  resize: none;
  min-height: 38px;
  max-height: 100px;
}
.ai-chat-input:focus { outline: none; border-color: var(--ai-purple); }
.ai-chat-send {
  padding: 8px 16px;
  background: var(--ai-purple);
  color: #fff;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 500;
  flex-shrink: 0;
}
.ai-chat-send:hover { background: var(--ai-purple-dark); }
.ai-chat-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* Suggest improvement button inside chat */
.ai-chat-suggest-btn {
  margin-top: 8px;
  padding: 6px 14px;
  background: transparent;
  border: 1px solid var(--ai-purple-border);
  border-radius: 6px;
  color: var(--ai-purple);
  font-family: var(--font);
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
}
.ai-chat-suggest-btn:hover { background: var(--ai-purple-light); }

/* Improvement diff */
.ai-improve-box {
  margin: 10px 0;
  border: 1px solid var(--ai-purple-border);
  border-radius: 8px;
  overflow: hidden;
}
.ai-improve-preview {
  padding: 12px 14px;
  font-size: 0.82rem;
  line-height: 1.6;
  max-height: 200px;
  overflow-y: auto;
  background: #fefefe;
}
.ai-improve-actions {
  display: flex;
  gap: 6px;
  padding: 8px 14px;
  border-top: 1px solid var(--ai-purple-border);
  background: #f9fafb;
}
.ai-improve-accept {
  padding: 5px 14px;
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.78rem;
}
.ai-improve-discard {
  padding: 5px 14px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.78rem;
  color: var(--text-secondary);
}

/* ===== Wrong-Answer Tutor ===== */
.ai-tutor-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 10px;
  padding: 6px 14px;
  background: transparent;
  border: 1px solid currentColor;
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.8rem;
  font-weight: 500;
  color: inherit;
  opacity: 0.8;
}
.ai-tutor-btn:hover { opacity: 1; background: rgba(255,255,255,0.3); }

.ai-tutor-response {
  margin-top: 12px;
  padding: 12px 16px;
  background: rgba(255,255,255,0.5);
  border-radius: var(--radius);
  font-size: 0.86rem;
  line-height: 1.7;
}
.ai-tutor-response p { margin-bottom: 6px; }
.ai-tutor-response p:last-child { margin-bottom: 0; }
.ai-tutor-response strong { font-weight: 700; }
.ai-tutor-response ul,
.ai-tutor-response ol {
  padding-right: 20px;
  margin: 4px 0;
}
.ai-tutor-response li { margin-bottom: 3px; }

.ai-similar-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 10px;
  padding: 6px 14px;
  background: var(--ai-purple);
  color: #fff;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.8rem;
  font-weight: 500;
}
.ai-similar-btn:hover { background: var(--ai-purple-dark); }

/* ===== Exam Question Generator ===== */
.ai-gen-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 16px;
  background: var(--ai-purple-light);
  border: 1px solid var(--ai-purple-border);
  border-radius: 6px;
  color: var(--ai-purple);
  font-family: var(--font);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}
.ai-gen-btn:hover { background: #ede9fe; border-color: var(--ai-purple); }
.ai-gen-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ===== Smart Summary ===== */
.ai-summary-section {
  background: var(--ai-purple-light);
  border: 1px solid var(--ai-purple-border);
  border-radius: var(--radius);
  padding: 20px 24px;
  margin-bottom: 32px;
}
.ai-summary-title {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--ai-purple);
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.ai-summary-body {
  font-size: 0.9rem;
  line-height: 1.75;
  color: var(--text);
}
.ai-summary-body h3 { margin: 12px 0 6px; font-size: 0.95rem; }
.ai-summary-body ul { padding-right: 20px; margin: 6px 0; }
.ai-summary-body li { margin-bottom: 4px; }

.ai-summary-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  background: var(--ai-purple);
  color: #fff;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.88rem;
  font-weight: 500;
  margin-top: 16px;
}
.ai-summary-btn:hover { background: var(--ai-purple-dark); }
.ai-summary-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ===== Improved Prose Block Overlay ===== */
.prose-block--improved {
  position: relative;
}
.prose-block--improved::before {
  content: 'AI improved';
  position: absolute;
  top: -8px;
  left: 8px;
  font-size: 0.6rem;
  font-weight: 700;
  color: var(--ai-purple);
  background: var(--card-bg);
  padding: 0 6px;
  border-radius: 4px;
  border: 1px solid var(--ai-purple-border);
}

/* ===== Chat Panel Overlay (dim background) ===== */
.ai-chat-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.2);
  z-index: 899;
  transition: opacity 0.25s;
}
.ai-chat-backdrop.hidden { display: none; }

/* ===== AI Highlight ===== */
.ai-highlight {
  background: #fef08a;
  border-bottom: 2px solid #eab308;
  border-radius: 2px;
  cursor: pointer;
  transition: background 0.15s;
}
.ai-highlight:hover {
  background: #fde047;
}
[data-theme="dark"] .ai-highlight {
  background: rgba(234, 179, 8, 0.25);
  border-bottom-color: #ca8a04;
}
[data-theme="dark"] .ai-highlight:hover {
  background: rgba(234, 179, 8, 0.4);
}
.ai-highlight-popup {
  position: absolute;
  z-index: 501;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  padding: 4px;
}
.ai-highlight-popup button {
  padding: 6px 14px;
  border: none;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--font);
  font-size: 0.78rem;
  font-weight: 500;
  color: #b91c1c;
  white-space: nowrap;
  transition: background 0.15s;
}
.ai-highlight-popup button:hover {
  background: #fef2f2;
}

/* ===== Bug Report Button & Modal ===== */
.bug-report-btn {
  position: fixed;
  bottom: 1.2rem;
  left: 1.2rem;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 2px solid #e5e7eb;
  background: #fff;
  font-size: 1.3rem;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
  z-index: 9999;
  transition: transform 0.15s, box-shadow 0.15s;
}
.bug-report-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 14px rgba(0,0,0,0.18);
}
.bug-modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
}
.bug-modal-backdrop.hidden { display: none; }
.bug-modal {
  background: #fff;
  border-radius: 12px;
  padding: 1.5rem;
  width: min(520px, 90vw);
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
}
.bug-modal-header {
  font-size: 1.15rem;
  font-weight: 700;
  margin-bottom: 0.75rem;
}
.bug-modal-input {
  width: 100%;
  border: 1.5px solid #d1d5db;
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  font-size: 0.95rem;
  font-family: inherit;
  resize: vertical;
  box-sizing: border-box;
}
.bug-modal-input:focus {
  outline: none;
  border-color: var(--ai-purple);
}
.bug-modal-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  margin-top: 0.75rem;
}
.bug-modal-cancel {
  padding: 0.45rem 1rem;
  border: 1.5px solid #d1d5db;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  font-size: 0.9rem;
}
.bug-modal-submit {
  padding: 0.45rem 1rem;
  border: none;
  border-radius: 8px;
  background: var(--ai-purple);
  color: #fff;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
}
.bug-modal-submit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}
.bug-modal-output {
  margin-top: 0.75rem;
  padding: 0.75rem;
  background: #1e1e2e;
  color: #cdd6f4;
  border-radius: 8px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 0.8rem;
  white-space: pre-wrap;
  max-height: 300px;
  overflow-y: auto;
}
.bug-modal-output.hidden { display: none; }

/* Screenshot preview */
.bug-screenshot-preview {
  margin-top: 0.5rem;
}
.bug-screenshot-preview.hidden { display: none; }
.bug-screenshot-label {
  font-size: 0.8rem;
  color: #6b7280;
  margin-bottom: 0.3rem;
}
.bug-screenshot-img {
  width: 100%;
  max-height: 180px;
  object-fit: contain;
  border-radius: 6px;
  border: 1px solid #e5e7eb;
}
.bug-info-preview {
  margin-top: 0.4rem;
  font-size: 0.78rem;
  color: #9ca3af;
  font-family: 'IBM Plex Mono', monospace;
}

/* Dark mode */
[data-theme="dark"] .bug-report-btn {
  background: #1e1e2e;
  border-color: #45475a;
  color: #cdd6f4;
}
[data-theme="dark"] .bug-modal {
  background: #1e1e2e;
  color: #cdd6f4;
}
[data-theme="dark"] .bug-modal-input {
  background: #313244;
  border-color: #45475a;
  color: #cdd6f4;
}
[data-theme="dark"] .bug-modal-cancel {
  background: #313244;
  border-color: #45475a;
  color: #cdd6f4;
}

/* ===== Responsive ===== */
@media (max-width: 700px) {
  .ai-chat-panel { width: 100%; }
  .ai-quick-bar { display: none; }
  .ai-tooltip { transform: scale(0.9); }
}
