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

:root {
  --bg: #0f172a;
  --bg-chat: #1e293b;
  --bg-user: #1d4ed8;
  --bg-assistant: #334155;
  --bg-input: #1e293b;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --border: #334155;
  --success: #22c55e;
  --radius: 12px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  height: 100vh; /* fallback */
  overflow: hidden;
}

@supports (height: 100dvh) {
  body { height: 100dvh; }
}

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 900px;
  margin: 0 auto;
}

/* Header */
header {
  padding: 16px 20px;
  padding-top: calc(16px + env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

header h1 {
  font-size: 20px;
  font-weight: 600;
}

.header-actions {
  display: flex;
  gap: 6px;
}

#resetBtn, #historyBtn {
  background: var(--bg-assistant);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.2s;
}

#resetBtn:hover, #historyBtn:hover {
  background: var(--accent);
}

/* Session History Dropdown */
.history-dropdown {
  position: absolute;
  top: 56px;
  right: 20px;
  background: var(--bg-chat);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 320px;
  max-height: 400px;
  overflow-y: auto;
  z-index: 100;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

.history-dropdown.hidden { display: none; }

.history-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}

.history-item:hover { background: rgba(255,255,255,0.05); }
.history-item:last-child { border-bottom: none; }

.history-item .history-preview {
  font-size: 13px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-item .history-meta {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 4px;
  opacity: 0.6;
}

.history-empty {
  padding: 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

/* Chat Area */
main {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message {
  display: flex;
  flex-direction: column;
  max-width: 80%;
  animation: fadeIn 0.3s ease;
}

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

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

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

.message-content {
  padding: 12px 16px;
  border-radius: var(--radius);
  line-height: 1.6;
  font-size: 15px;
}

.message.user .message-content {
  background: var(--bg-user);
  border-bottom-right-radius: 4px;
}

.message.assistant .message-content {
  background: var(--bg-assistant);
  border-bottom-left-radius: 4px;
}

.message-content p {
  margin-bottom: 8px;
}

.message-content p:last-child {
  margin-bottom: 0;
}

.message-content ul, .message-content ol {
  margin: 8px 0 8px 20px;
}

.message-content li {
  margin-bottom: 4px;
}

.message-content code {
  background: rgba(0,0,0,0.3);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
}

.message-content strong {
  color: #fff;
}

/* Download Card */
.download-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(34, 197, 94, 0.15);
  border: 1px solid rgba(34, 197, 94, 0.3);
  border-radius: 10px;
  padding: 12px 16px;
  margin-top: 10px;
  cursor: pointer;
  transition: background 0.2s;
}

.download-card:hover {
  background: rgba(34, 197, 94, 0.25);
}

.download-icon {
  font-size: 28px;
}

.download-info {
  flex: 1;
}

.download-info .filename {
  font-weight: 600;
  color: var(--success);
  font-size: 14px;
}

.download-info .meta {
  color: var(--text-muted);
  font-size: 12px;
  margin-top: 2px;
}

/* Loading Card */
.loading-card {
  padding: 16px 20px;
  background: var(--bg-assistant);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  min-width: 280px;
}

.loading-status {
  font-size: 15px;
  margin-bottom: 10px;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.loading-bar-track {
  width: 100%;
  height: 6px;
  background: rgba(255,255,255,0.1);
  border-radius: 3px;
  overflow: hidden;
}

.loading-bar-fill {
  height: 100%;
  width: 5%;
  background: linear-gradient(90deg, var(--accent), #60a5fa);
  border-radius: 3px;
  transition: width 1s ease;
  position: relative;
}

.loading-bar-fill::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
  animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

.loading-elapsed {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 6px;
  text-align: right;
}

/* Options Bar */
.options-bar {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 10px;
  padding: 0 2px;
}

.toggle-option {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
}

.toggle-option input {
  display: none;
}

.toggle-slider {
  width: 36px;
  height: 20px;
  background: var(--border);
  border-radius: 10px;
  position: relative;
  transition: background 0.2s;
  flex-shrink: 0;
}

.toggle-slider::after {
  content: '';
  position: absolute;
  width: 16px;
  height: 16px;
  background: var(--text-muted);
  border-radius: 50%;
  top: 2px;
  left: 2px;
  transition: all 0.2s;
}

.toggle-option input:checked + .toggle-slider {
  background: var(--accent);
}

.toggle-option input:checked + .toggle-slider::after {
  left: 18px;
  background: white;
}

.toggle-label {
  font-size: 13px;
  color: var(--text-muted);
  transition: color 0.2s;
}

.toggle-option input:checked ~ .toggle-label {
  color: var(--accent);
}

/* Footer / Input */
footer {
  padding: 16px 20px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.file-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.file-preview.hidden {
  display: none;
}

.file-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-assistant);
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--text-muted);
}

.file-chip .remove {
  cursor: pointer;
  color: var(--text-muted);
  font-size: 16px;
  line-height: 1;
}

.file-chip .remove:hover {
  color: #ef4444;
}

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

.upload-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  background: var(--bg-assistant);
  border-radius: 10px;
  cursor: pointer;
  font-size: 20px;
  transition: background 0.2s;
  flex-shrink: 0;
}

.upload-btn:hover {
  background: var(--accent);
}

#messageInput {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  resize: none;
  max-height: 150px;
  line-height: 1.4;
}

#messageInput:focus {
  outline: none;
  border-color: var(--accent);
}

#messageInput::placeholder {
  color: var(--text-muted);
}

#sendBtn {
  width: 42px;
  height: 42px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s;
}

#sendBtn:hover {
  background: var(--accent-hover);
}

#sendBtn:disabled {
  background: var(--border);
  cursor: not-allowed;
}

/* Scrollbar */
main::-webkit-scrollbar {
  width: 6px;
}

main::-webkit-scrollbar-track {
  background: transparent;
}

main::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

/* ═══════════════════════════════════
   Mobile Responsive
   ═══════════════════════════════════ */
@media (max-width: 768px) {
  .app {
    max-width: 100%;
  }

  header {
    padding: 10px 12px;
  }

  header h1 {
    font-size: 16px;
  }

  #resetBtn {
    padding: 5px 10px;
    font-size: 13px;
  }

  main {
    padding: 12px;
    gap: 12px;
  }

  .message {
    max-width: 95%;
  }

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

  footer {
    padding: 10px 12px;
  }

  .options-bar {
    margin-bottom: 8px;
  }

  .toggle-label {
    font-size: 12px;
  }

  .input-row {
    gap: 6px;
  }

  .upload-btn {
    width: 38px;
    height: 38px;
    font-size: 18px;
  }

  #messageInput {
    font-size: 14px;
    padding: 9px 12px;
  }

  #sendBtn {
    width: 38px;
    height: 38px;
    font-size: 16px;
  }

  /* Download card mobile */
  .download-card {
    padding: 10px 12px;
    gap: 10px;
  }

  .download-icon {
    font-size: 24px;
  }

  .download-info .filename {
    font-size: 13px;
  }

  /* Loading card mobile */
  .loading-card {
    min-width: auto;
    padding: 12px 14px;
  }

  /* Slide grid mobile */
  .slide-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 6px;
    padding: 8px;
    max-height: 400px;
  }

  .slide-editor-header {
    padding: 8px 12px;
    font-size: 13px;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
  }

  /* Image sheet mobile tweaks */
  .image-search-bar {
    padding: 8px 12px;
    gap: 6px;
  }

  .image-search-bar input {
    font-size: 16px; /* prevents iOS zoom */
  }

  .image-results {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    padding: 8px 12px;
    gap: 8px;
  }

  .image-result img {
    height: 80px;
  }
}

/* Extra small screens */
@media (max-width: 380px) {
  header h1 {
    font-size: 14px;
  }

  .slide-grid {
    grid-template-columns: 1fr 1fr;
  }

  .image-results {
    grid-template-columns: 1fr 1fr;
  }
}

/* ═══════════════════════════════════
   Slide Editor & Preview
   ═══════════════════════════════════ */

.slide-editor {
  margin-top: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.slide-editor-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 16px;
  background: rgba(255,255,255,0.05);
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
}

.slide-hint {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 400;
}

.slide-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 8px;
  padding: 12px;
  max-height: 500px;
  overflow-y: auto;
}

.slide-thumb {
  position: relative;
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.2s, transform 0.2s;
}

.slide-thumb:hover {
  border-color: var(--accent);
  transform: scale(1.02);
}

.slide-thumb img {
  width: 100%;
  display: block;
}

.slide-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.2s;
}

.slide-thumb:hover .slide-overlay {
  opacity: 1;
}

.slide-add-img {
  padding: 8px 16px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
}

.slide-number {
  position: absolute;
  bottom: 4px;
  left: 4px;
  background: rgba(0,0,0,0.7);
  color: white;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 11px;
}

/* ═══════════════════════════════════
   Image Search Modal
   ═══════════════════════════════════ */

/* Bottom Sheet for image selection (mobile-first) */
.image-sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 999;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(2px);
}

.image-sheet {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  background: var(--bg-main);
  border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: translateY(100%);
  transition: transform 0.3s ease;
}

.image-sheet.open {
  transform: translateY(0);
}

.image-sheet-handle {
  width: 36px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  margin: 8px auto 4px;
}

.image-sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 15px;
}

/* Desktop: center as modal */
@media (min-width: 769px) {
  .image-sheet {
    top: 50%;
    left: 50%;
    right: auto;
    bottom: auto;
    width: 90%;
    max-width: 700px;
    max-height: 80vh;
    border-radius: 12px;
    transform: translate(-50%, -50%) scale(0.95);
    opacity: 0;
    transition: transform 0.2s ease, opacity 0.2s ease;
  }
  .image-sheet.open {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
  .image-sheet-handle { display: none; }
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px;
}

.modal-close:hover {
  color: white;
}

.image-search-bar {
  display: flex;
  gap: 8px;
  padding: 12px 20px;
}

.image-search-bar input {
  flex: 1;
  padding: 10px 14px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-main);
  font-size: 14px;
}

.image-search-bar button {
  padding: 10px 20px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}

.image-upload-bar {
  padding: 0 20px 12px;
}

.upload-label {
  display: inline-block;
  padding: 8px 16px;
  background: rgba(255,255,255,0.08);
  border: 1px dashed var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.2s;
}

.upload-label:hover {
  background: rgba(255,255,255,0.15);
}

.image-results {
  flex: 1;
  overflow-y: auto;
  padding: 12px 20px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
  min-height: 200px;
}

.image-results-hint {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--text-muted);
  padding: 40px 0;
}

.image-result {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.2s, transform 0.15s;
}

.image-result:hover {
  border-color: var(--accent);
  transform: scale(1.03);
}

.image-result img {
  width: 100%;
  height: 120px;
  object-fit: cover;
  display: block;
}

.image-credit {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0,0,0,0.7);
  color: white;
  font-size: 10px;
  padding: 2px 6px;
}

/* Position auto-detected from placeholder */
