/* ===== Base & Reset ===== */
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, sans-serif;
  background: #0f0e17;
  color: #e0e0e0;
  /* Use dvh to handle mobile browser chrome (address bar / nav bar) */
  min-height: 100vh;
  min-height: 100dvh;
}
#app {
  min-height: 100vh;
  min-height: 100dvh;
}
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #2a2850; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #3d3b6b; }
input, textarea, select { font-family: inherit; }

/* ===== Brand color overrides ===== */
.bg-brand { background-color: #8b7cf6; }
.text-brand { color: #8b7cf6; }
.border-soft-purple { border-color: #8b7cf6; }
.focus\:border-soft-purple:focus { border-color: #8b7cf6; }

/* ===== Layout animations ===== */
#sidebar { transition: margin-left 0.3s ease; }
#sidebar.collapsed { margin-left: -18rem; }
#memoryPanel { transition: margin-right 0.3s ease; }
#memoryPanel.collapsed { margin-right: -20rem; }

/* ===== Message bubbles ===== */
.message-row { display: flex; gap: 12px; animation: fadeSlideIn 0.3s ease; }
@keyframes fadeSlideIn {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}
.msg-avatar {
  width: 36px; height: 36px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; font-size: 14px; font-weight: 600;
}
.msg-avatar.ai { background: linear-gradient(135deg, #8b7cf6, #6c5ce7); }
.msg-avatar.user { background: linear-gradient(135deg, #3d3b6b, #232147); }
.msg-content { flex: 1; min-width: 0; }

/* ===== Markdown rendering ===== */
.msg-md { line-height: 1.7; word-break: break-word; }
.msg-md h1, .msg-md h2, .msg-md h3, .msg-md h4 { margin: 1em 0 0.5em; font-weight: 600; color: #fff; }
.msg-md h1 { font-size: 1.5em; } .msg-md h2 { font-size: 1.3em; } .msg-md h3 { font-size: 1.1em; }
.msg-md p { margin: 0.5em 0; }
.msg-md ul, .msg-md ol { margin: 0.5em 0; padding-left: 1.5em; }
.msg-md li { margin: 0.25em 0; }
.msg-md code { background: #1a1830; padding: 2px 6px; border-radius: 4px; font-size: 0.88em; font-family: 'Courier New', monospace; color: #c3a6ff; }
.msg-md pre { background: #1a1830; padding: 12px 16px; border-radius: 8px; overflow-x: auto; margin: 0.8em 0; }
.msg-md pre code { background: none; padding: 0; }
.msg-md blockquote { border-left: 3px solid #8b7cf6; padding-left: 12px; margin: 0.8em 0; color: #a0a0b0; }
.msg-md a { color: #8b7cf6; text-decoration: underline; }
.msg-md table { border-collapse: collapse; margin: 0.8em 0; width: 100%; }
.msg-md th, .msg-md td { border: 1px solid #2a2850; padding: 6px 12px; text-align: left; }
.msg-md th { background: #1a1830; }
.msg-md strong { color: #fff; }

/* ===== Typing indicator ===== */
.typing-dots span {
  display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: #8b7cf6; margin: 0 2px;
  animation: typing 1.4s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-8px); opacity: 1; }
}

/* ===== Gacha card grid ===== */
.gacha-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px;
}
.gacha-card {
  padding: 14px; border-radius: 10px; border: 1px solid #2a2850; background: #1a1830;
  cursor: pointer; transition: all 0.2s; position: relative;
}
.gacha-card:hover { border-color: #8b7cf6; transform: translateY(-2px); box-shadow: 0 4px 12px rgba(139,124,246,0.15); }
.gacha-card .card-index {
  position: absolute; top: 8px; right: 10px; font-size: 11px; color: #6c5ce7;
  background: rgba(139,124,246,0.15); padding: 1px 8px; border-radius: 4px;
}
.gacha-card .card-select {
  position: absolute; bottom: 8px; right: 10px; font-size: 11px; color: gray;
  background: rgba(139,124,246,0.15); padding: 1px 4px; border-radius: 4px;
}
.gacha-card.selected { border-color: #6c5ce7; box-shadow: 0 0 0 2px rgba(139,124,246,0.3); }
.gacha-card .card-content { font-size: 13px; line-height: 1.6; max-height: 120px; overflow: hidden; }

/* ===== Project list ===== */
.project-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  border-radius: 8px; cursor: pointer; transition: all 0.2s;
  border: 1px solid transparent;
}
.project-item:hover { background: #232147; }
.project-item.active { background: #232147; border-color: #8b7cf6; }
.project-item .pi-icon { width: 36px; height: 36px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 16px; flex-shrink: 0; }
.project-item .pi-info { flex: 1; min-width: 0; }
.project-item .pi-title { font-size: 13px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.project-item .pi-meta { font-size: 11px; color: #888; margin-top: 2px; }
.project-item .pi-delete { opacity: 0; transition: opacity 0.2s; color: #888; }
.project-item:hover .pi-delete { opacity: 1; }
.project-item .pi-delete:hover { color: #e84393; }
.project-chapters { padding-left: 22px; }
.chapter-item {
  display: flex; align-items: center; gap: 6px; padding: 6px 10px;
  font-size: 12px; color: #999; cursor: pointer; border-radius: 4px; transition: all 0.15s;
}
.chapter-item:hover { color: #ccc; background: rgba(255,255,255,0.03); }
.chapter-item.active { color: #8b7cf6; background: rgba(139,124,246,0.1); }

/* ===== Memory items ===== */
.memory-tag {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 10px; border-radius: 6px; font-size: 12px;
  background: #232147; border: 1px solid #2a2850; margin: 0 4px 4px 0;
  transition: all 0.15s;
}
.memory-tag:hover { border-color: #8b7cf6; }
.memory-tag .mt-del { opacity: 0.5; cursor: pointer; font-size: 12px; }
.memory-tag .mt-del:hover { opacity: 1; color: #e84393; }

/* ===== Tool modal ===== */
#nameCategories .ncat {
  padding: 4px 12px; border-radius: 6px; font-size: 12px; cursor: pointer;
  background: #1a1830; border: 1px solid #2a2850; transition: all 0.15s;
}
#nameCategories .ncat:hover { border-color: #8b7cf6; }
#nameCategories .ncat.active { background: #8b7cf6; border-color: #8b7cf6; color: white; }

/* ===== Toast animations ===== */
.toast-anim {
  animation: toastIn 0.3s ease;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateX(20px); }
  to   { opacity: 1; transform: translateX(0); }
}

/* ===== Copy button ===== */
.copy-btn { opacity: 0; transition: opacity 0.2s; color: #888; font-size: 12px; display: inline-flex; align-items: center; gap: 4px; }
.copy-btn:hover { color: #8b7cf6; }
.msg-content:hover .copy-btn { opacity: 1; }

/* ===== Copy button On mobile: always visible ===== */
@media (max-width: 768px) {
  .copy-btn { opacity: 1 !important; }
}

/* ===== Wizard styles ===== */
.wizard-option:active {
  transform: scale(0.97);
}
.wizard-option:focus-visible {
  outline: 2px solid #8b7cf6;
  outline-offset: 2px;
}

/* ===== Mobile: handle virtual navigation bar ===== */

/* iOS safe area */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .chat-input-bar {
    padding-bottom: calc(16px + env(safe-area-inset-bottom)) !important;
  }
  #sidebar .px-4.py-3.border-t.border-border-soft {
    padding-bottom: calc(12px + env(safe-area-inset-bottom)) !important;
  }
  #memoryPanel .p-3.border-t.border-border-soft {
    padding-bottom: calc(12px + env(safe-area-inset-bottom)) !important;
  }
}

/* Android virtual nav bar: force enough bottom padding to clear the nav (≈48-56px) */
@media (max-width: 768px) {
  .chat-input-bar {
    padding-bottom: 56px !important;
  }
  /* Reserve space so messages aren't hidden behind the input bar */
  #messagesList {
    padding-bottom: 30px !important;
  }
  /* Add space below sidebar bottom buttons */
  #sidebar .px-4.py-3.border-t.border-border-soft {
    padding-bottom: 24px !important;
  }
  /* Add space below memory panel bottom buttons */
  #memoryPanel .p-3.border-t.border-border-soft {
    padding-bottom: 24px !important;
  }
}

/* ===== Responsive ===== */

/* Tablet: narrower sidebar */
@media (max-width: 1024px) {
  #sidebar { width: 220px; }
  #memoryPanel { width: 280px; }
}

/* Mobile: overlay sidebar & panel */
@media (max-width: 768px) {
  /* Sidebar becomes full-height overlay */
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 80%;
    max-width: 300px;
    z-index: 60;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    border-right: 1px solid #2a2850;
    box-shadow: 4px 0 20px rgba(0,0,0,0.5);
  }
  #sidebar.open {
    transform: translateX(0);
  }
  #sidebar.collapsed {
    margin-left: 0; /* override desktop */
    transform: translateX(-100%);
  }

  /* Memory panel becomes full-height overlay */
  #memoryPanel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 85%;
    max-width: 320px;
    z-index: 55;
    transform: translateX(100%);
    transition: transform 0.3s ease;
    border-left: 1px solid #2a2850;
    box-shadow: -4px 0 20px rgba(0,0,0,0.5);
    margin-right: 0 !important;
  }
  #memoryPanel.open {
    transform: translateX(0);
  }
  #memoryPanel.collapsed {
    margin-right: 0; /* override desktop */
    transform: translateX(100%);
  }

  /* Overlay backdrop */
  #sidebarBackdrop, #memoryBackdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 50;
  }
  #sidebarBackdrop.active, #memoryBackdrop.active {
    display: block;
  }

  /* Gacha single column */
  .gacha-grid { grid-template-columns: 1fr; }

  /* Modals: full width on mobile */
  #settingsModal > div,
  #newProjectModal > div {
    width: 90% !important;
    max-width: 90% !important;
  }
  #nameToolModal > div {
    width: 95% !important;
    max-width: 95% !important;
    max-height: 90vh !important;
  }

  /* Reduce padding on mobile */
  #messagesList { padding: 12px 10px !important; }
  .message-row { gap: 8px; }
  .msg-avatar { width: 30px; height: 30px; font-size: 12px; }

  /* Input area */
  .border-t.px-4 > div:first-child { flex-wrap: wrap; gap: 6px; }
  #chatInput { font-size: 14px; }

  /* Top toolbar */
  header.px-6 { padding: 0 12px !important; }
  header .text-sm { font-size: 12px; }

  /* Toast */
  #toastContainer { top: 8px; right: 8px; left: 8px; }
  #toastContainer > div { font-size: 12px; padding: 8px 12px; }

  /* Welcome quick-action buttons */
  #welcomeState .flex.flex-wrap { flex-direction: column; gap: 8px; align-items: stretch; }

  /* Header: stack on very small screens */
  header .gap-2 { gap: 4px; }

  /* Adjust icon sizes for touch */
  i { font-size: 16px; }
  button { min-height: 36px; }
  .project-item { padding: 8px 10px; }
  .chapter-item { padding: 5px 8px; }
}

/* Very small phones */
@media (max-width: 380px) {
  #sidebar { width: 85%; }
  #memoryPanel { width: 90%; }
  .msg-md { font-size: 13px; }
  header > div:first-child { font-size: 12px; }
  .px-6 { padding-left: 8px !important; padding-right: 8px !important; }
}

/* ===== Loader ===== */
.loader {
  width: 16px; height: 16px; border: 2px solid #2a2850;
  border-top-color: #8b7cf6; border-radius: 50%;
  animation: spin 0.8s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Settings button active state ===== */
#gachaModeBtn.active {
  background: rgba(139,124,246,0.2) !important;
  color: #8b7cf6;
}
