* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #fafafa;
  color: #1a1a1a;
}

/* Document area */
#document {
  padding: 16px;
  padding-bottom: 80px;
  min-height: calc(100vh - 56px);
}

/* Connection status */
.connection {
  position: fixed;
  bottom: 70px;
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 16px;
  background: #ff9800;
  color: #fff;
  border-radius: 16px;
  font-size: 0.8rem;
  z-index: 300;
}
.connection.hidden { display: none; }


/* === OVERLAY === */
.chat-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.25);
  z-index: 199;
  transition: opacity 0.3s;
}
.chat-overlay.hidden { display: none; }

/* === BOTTOM PANEL === */
.bottom-panel {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 200;
  background: #fff;
  border-top: 1px solid #e8e8e8;
  display: flex;
  flex-direction: column;
}

/* --- Toolbar (always visible) --- */
.toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 6px 10px;
  padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
  min-height: 48px;
}

.toolbar-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border: none;
  background: none;
  color: #555;
  cursor: pointer;
  border-radius: 10px;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s, color 0.15s;
}
.toolbar-btn:active { background: #f0f0f0; }
.toolbar-btn.active {
  background: #111;
  color: #fff;
}
.toolbar-btn.active:active {
  background: #333;
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid #ddd;
  border-top-color: #888;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --- Input panel (toggled by pencil icon) --- */
.input-panel {
  padding: 8px 12px;
  border-bottom: 1px solid #f0f0f0;
  animation: slideUp 0.2s ease-out;
}
.input-panel.hidden { display: none; }

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.input-row textarea {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid #ddd;
  border-radius: 16px;
  resize: none;
  overflow-y: auto;
  min-height: 40px;
  max-height: 120px;
  line-height: 1.4;
  font-family: inherit;
  font-size: 1rem;
  outline: none;
  background: #f5f5f5;
}
.input-row textarea:focus {
  border-color: #999;
  background: #fff;
}

.btn-send {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  background: #1a1a1a;
  color: #fff;
  cursor: pointer;
  border-radius: 50%;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}
.btn-send:disabled { background: #ccc; cursor: default; }
.btn-send:active:not(:disabled) { background: #333; }

/* --- Chat panel (toggled by chat icon) --- */
.chat-panel {
  max-height: 50vh;
  display: flex;
  flex-direction: column;
  border-bottom: 1px solid #f0f0f0;
  animation: slideUp 0.2s ease-out;
}
.chat-panel.hidden { display: none; }

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

/* Chat messages */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 120px;
  max-height: 50vh;
}

.chat-bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 0.9rem;
  line-height: 1.45;
  word-break: break-word;
}
.chat-bubble.user {
  align-self: flex-end;
  background: #111;
  color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-bubble.bot {
  align-self: flex-start;
  background: #f0f0f0;
  color: #1a1a1a;
  border-bottom-left-radius: 4px;
}
.chat-bubble.bot.error {
  background: #fdecea;
  color: #b71c1c;
}
.chat-bubble .chat-time {
  font-size: 0.7rem;
  opacity: 0.5;
  margin-top: 4px;
  display: block;
}

/* Date separator */
.chat-date-separator {
  align-self: center;
  font-size: 0.75rem;
  color: #999;
  background: #f0f0f0;
  padding: 3px 12px;
  border-radius: 10px;
  margin: 4px 0;
}

/* Typing indicator (animated dots inside chat) */
.chat-typing-indicator {
  align-self: flex-start;
  display: flex;
  gap: 4px;
  padding: 12px 16px;
  background: #f0f0f0;
  border-radius: 16px;
  border-bottom-left-radius: 4px;
}
.chat-typing-indicator .dot {
  width: 8px;
  height: 8px;
  background: #999;
  border-radius: 50%;
  animation: typingBounce 1.4s infinite both;
}
.chat-typing-indicator .dot:nth-child(2) { animation-delay: 0.2s; }
.chat-typing-indicator .dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes typingBounce {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

/* === TOAST NOTIFICATION (above toolbar) === */
.toast-container {
  position: fixed;
  left: 0; right: 0;
  bottom: calc(48px + env(safe-area-inset-bottom, 0px) + 6px);
  z-index: 201;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 0 10px;
  pointer-events: none;
}

.toast {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 0.85rem;
  line-height: 1.4;
  word-break: break-word;
  pointer-events: auto;
  animation: toastIn 0.25s ease-out;
  box-shadow: 0 2px 12px rgba(0,0,0,0.12);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  max-width: 100%;
  margin-bottom: 6px;
  touch-action: pan-x;
  transition: transform 0.15s ease-out, opacity 0.15s ease-out;
  will-change: transform;
}
.toast.user {
  background: rgba(17,17,17,0.9);
  color: #fff;
  align-self: flex-end;
  max-width: 85%;
}
.toast.bot {
  background: rgba(255,255,255,0.92);
  color: #1a1a1a;
  border: 1px solid #e0e0e0;
  align-self: flex-start;
  max-width: 85%;
}
.toast.bot.error {
  background: rgba(253,236,234,0.95);
  color: #b71c1c;
  border-color: #f5c6cb;
}
.toast.thinking {
  background: rgba(245,245,245,0.92);
  color: #888;
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 6px;
}
.toast.fade-out {
  animation: toastOut 0.3s ease-in forwards;
}

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

/* Diff highlights */
.diff-added {
  background: #d4edda;
  border-left: 3px solid #28a745;
  padding-left: 4px;
}
.diff-removed {
  background: #f8d7da;
  text-decoration: line-through;
  opacity: 0.6;
}
