/* ─── Reset & base ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --color-bg:        #12121e;
  --color-surface:   #1a1a2e;
  --color-surface2:  #1e2850;
  --color-accent:    #e94560;
  --color-text:      #e8e8f4;
  --color-text-muted:#a8a8c4;
  --color-border:    #38385c;
  --color-user-bg:   #e94560;
  --color-user-text: #fff;
  --color-asst-bg:   #1a1a2e;
  --color-asst-text: #e8e8f4;
  --sidebar-w:       280px;
  --header-h:        52px;
  --statusbar-h:     32px;
  font-size: 15px;
}

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ─── App container ─────────────────────────────────────────────────────── */
#app {
  width: 100%;
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ─── Header ───────────────────────────────────────────────────────────── */
#header {
  height: var(--header-h);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  padding: 0 1rem;
  gap: 1rem;
  flex-shrink: 0;
  z-index: 10;
}

#header .logo {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--color-accent);
  flex: 1;
}

#header .logo-icon {
  width: 1.1rem;
  height: 1.1rem;
  display: block;
  flex-shrink: 0;
  transform: translateY(1px);
}

#header .logo span {
  line-height: 1;
}

#header .hamburger {
  display: none;
  background: none;
  border: none;
  color: var(--color-text);
  font-size: 1.4rem;
  cursor: pointer;
  padding: 0.25rem;
}

#header .admin-link {
  color: var(--color-text-muted);
  text-decoration: none;
  font-size: 0.85rem;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  padding: 4px 10px;
  transition: color 0.2s, border-color 0.2s;
}
#header .admin-link:hover { color: var(--color-text); border-color: var(--color-text-muted); }

#header .share-btn {
  background: none;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  color: var(--color-text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 4px 10px;
  transition: color 0.2s, border-color 0.2s;
}
#header .share-btn:hover { color: var(--color-text); border-color: var(--color-text-muted); }

.fullscreen-btn {
  background: none;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  color: var(--color-text-muted);
  cursor: pointer;
  font-size: 1rem;
  padding: 4px 8px;
  transition: color 0.2s, border-color 0.2s;
  line-height: 1;
}
.fullscreen-btn:hover { color: var(--color-text); border-color: var(--color-text-muted); }

/* ─── Layout: main row ─────────────────────────────────────────────────── */
#main {
  flex: 1;
  display: flex;
  overflow: hidden;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
}

:fullscreen #main,
:-webkit-full-screen #main {
  max-width: none;
}

/* ─── Sidebar ──────────────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}

.sidebar-section { display: flex; flex-direction: column; overflow: hidden; flex: 1; }
.sidebar-section + .sidebar-section { border-top: 1px solid var(--color-border); }

.sidebar-section h2 {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-muted);
  padding: 0.6rem 1rem;
  flex-shrink: 0;
}

.sidebar-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
}

.sidebar-list li {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 1rem;
  cursor: pointer;
  border-radius: 0;
  transition: background 0.15s;
  font-size: 0.9rem;
}
.sidebar-list li:hover { background: var(--color-surface2); }
.sidebar-list li.active { background: var(--color-surface2); color: var(--color-accent); }

.sidebar-list li .avatar-thumb {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--color-border);
}

.char-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* conversation list */
.sidebar-list li.conv-item { flex-wrap: nowrap; }
.conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.85rem; }
.conv-count { font-size: 0.75rem; color: var(--color-text-muted); flex-shrink: 0; }
.conv-delete {
  background: none;
  border: none;
  color: var(--color-text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 2px 4px;
  border-radius: 3px;
  flex-shrink: 0;
  opacity: 0;
  transition: opacity 0.15s;
}
.sidebar-list li:hover .conv-delete { opacity: 1; }
.conv-delete:hover { color: var(--color-accent); }

.new-conv-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: none;
  border: none;
  border-top: 1px solid var(--color-border);
  color: var(--color-accent);
  cursor: pointer;
  font-size: 0.85rem;
  width: 100%;
  text-align: left;
  transition: background 0.15s;
  flex-shrink: 0;
}
.new-conv-btn:hover { background: var(--color-surface2); }

/* ─── Chat area ─────────────────────────────────────────────────────────── */
#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* character header */
#char-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1rem;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
  min-height: 52px;
}

#char-header .char-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--color-border);
}

#char-header .char-name {
  font-weight: 600;
  font-size: 1rem;
}

/* empty state */
#empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  color: var(--color-text-muted);
  text-align: center;
  padding: 2rem;
}
#empty-state a { color: var(--color-accent); }

/* message list */
#message-list {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

/* message bubbles */
.msg {
  display: flex;
  flex-direction: column;
  max-width: 75%;
}
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant, .msg.system { align-self: flex-start; align-items: flex-start; }

.msg-bubble {
  padding: 0.6rem 0.9rem;
  border-radius: 12px;
  line-height: 1.5;
  word-break: break-word;
}

.msg.user .msg-bubble {
  background: var(--color-user-bg);
  color: var(--color-user-text);
  border-bottom-right-radius: 3px;
}

.msg.assistant .msg-bubble, .msg.system .msg-bubble {
  background: var(--color-asst-bg);
  color: var(--color-asst-text);
  border: 1px solid var(--color-border);
  border-bottom-left-radius: 3px;
}

.msg-bubble p:not(:last-child) { margin-bottom: 0.4em; }
.msg-bubble pre { background: rgba(0,0,0,0.3); border-radius: 6px; padding: 0.6em; overflow-x: auto; font-size: 0.85em; margin: 0.4em 0; }
.msg-bubble code { font-size: 0.88em; }
.msg-bubble pre code { background: none; }
.msg-bubble :not(pre) > code { background: rgba(0,0,0,0.25); border-radius: 3px; padding: 0.1em 0.35em; }

/* Markdown-formatted roleplay: stage directions (italic) and character names (bold) */
.msg.assistant .msg-bubble em,
.msg.system .msg-bubble em {
  font-style: italic;
  color: var(--color-text-muted);
}
.msg.assistant .msg-bubble strong,
.msg.system .msg-bubble strong {
  font-weight: 700;
  color: var(--color-accent);
}

.msg-bubble .rp-hint {
  display: inline-block;
  padding: 0.13em 0.65em;
  margin: 0.05em 0.07em;
  border-radius: 999px;
  font-style: italic;
  font-size: 0.88em;
  font-weight: 500;
  line-height: 1.55;
  vertical-align: middle;
  color: var(--color-accent);
  background: rgba(233, 69, 96, 0.1);
  box-shadow: 0 0 0 1px rgba(233, 69, 96, 0.35),
              inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

.msg.user .msg-bubble .rp-hint {
  color: rgba(255, 255, 255, 0.95);
  background: rgba(255, 255, 255, 0.15);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.4),
              inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

/* images inside messages */
.msg-images { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.5rem; min-width: 0; }

/* image wrapper: provides positioning context for copy button */
.img-wrapper {
  position: relative;
  display: inline-block;
  max-width: 100%;
}
.img-wrapper:hover .img-copy-btn,
.img-copy-btn:focus { opacity: 1; }

.msg-image { max-width: min(520px, 100%); max-height: 520px; border-radius: 8px; object-fit: contain; cursor: pointer; border: 1px solid var(--color-border); display: block; }

.msg-media-status {
  margin-top: 0.45rem;
  padding: 0.7rem 0.85rem;
  border-radius: 10px;
  border: 1px solid rgba(233,69,96,0.45);
  background: linear-gradient(135deg, rgba(233,69,96,0.16), rgba(30,40,80,0.75));
  box-shadow: 0 0 0 1px rgba(233,69,96,0.08) inset;
}

.msg-media-status-title {
  font-size: 0.84rem;
  font-weight: 700;
  color: var(--color-text);
}

.msg-media-status-subtitle {
  margin-top: 0.2rem;
  font-size: 0.78rem;
  color: var(--color-text-muted);
}

/* copy-to-clipboard button on hover over generated images */
.img-copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(0,0,0,0.6);
  color: #fff;
  border: none;
  border-radius: 4px;
  width: 28px;
  height: 28px;
  font-size: 1rem;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.img-copy-btn:hover { background: rgba(0,0,0,0.85); }

/* image placeholder */
.img-placeholder {
  width: min(240px, 100%);
  min-height: 164px;
  background: var(--color-surface2);
  border-radius: 8px;
  border: 1px solid rgba(233,69,96,0.35);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  color: var(--color-text);
  margin-top: 0.5rem;
  padding: 0.85rem;
  text-align: center;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.03);
}

.img-placeholder-title {
  font-weight: 700;
  color: var(--color-text);
}

.img-placeholder-prompt {
  font-size: 0.74rem;
  color: var(--color-text-muted);
  line-height: 1.4;
  max-width: 100%;
  padding: 0.45rem 0.55rem;
  border-radius: 6px;
  background: rgba(0,0,0,0.2);
  border: 1px solid rgba(255,255,255,0.06);
  word-break: break-word;
}

.img-error {
  font-size: 0.78rem;
  color: var(--color-accent);
  margin-top: 0.4rem;
  padding: 0.25rem 0.5rem;
  background: rgba(233,69,96,0.1);
  border-radius: 4px;
  border: 1px solid rgba(233,69,96,0.3);
}

/* typing indicator */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0.6rem 0.9rem;
  background: var(--color-asst-bg);
  border: 1px solid var(--color-border);
  border-radius: 12px;
  border-bottom-left-radius: 3px;
  width: fit-content;
}
.typing-dot {
  width: 7px;
  height: 7px;
  background: var(--color-text-muted);
  border-radius: 50%;
  animation: bounce 1.2s infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.6; }
  40% { transform: translateY(-6px); opacity: 1; }
}

/* spinner */
.spinner {
  width: 20px; height: 20px;
  border: 2px solid var(--color-border);
  border-top-color: var(--color-accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* image generation progress bar */
.img-progress {
  width: 80%;
  height: 6px;
  background: var(--color-border);
  border-radius: 3px;
  overflow: hidden;
  margin-top: 0.25rem;
}
.img-progress-bar {
  height: 100%;
  background: var(--color-accent);
  border-radius: 3px;
  width: 0%;
  transition: width 0.3s ease;
}
.img-progress-label {
  font-size: 0.72rem;
  color: var(--color-text-muted);
}

/* inline error */
.msg-error,
.msg-warning {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.82rem;
  padding: 0.5rem 0.8rem;
  border-radius: 8px;
  align-self: flex-start;
}

.msg-error {
  color: var(--color-accent);
  background: rgba(233,69,96,0.1);
  border: 1px solid rgba(233,69,96,0.3);
}

.msg-warning {
  color: #b8860b;
  background: rgba(255,193,7,0.1);
  border: 1px solid rgba(255,193,7,0.4);
  max-width: 80%;
}

/* retry button inside error message */
.msg-retry-btn {
  background: var(--color-accent);
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 2px 10px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.2s;
}
.msg-retry-btn:hover { opacity: 0.85; }

/* toast */
#toast {
  position: fixed;
  top: 60px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(233,69,96,0.9);
  color: #fff;
  padding: 0.5rem 1.2rem;
  border-radius: 6px;
  font-size: 0.85rem;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s;
}
#toast.show { opacity: 1; }

/* ─── Input area ─────────────────────────────────────────────────────────── */
#input-area {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  flex-shrink: 0;
}

#msg-input {
  flex: 1;
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  color: var(--color-text);
  font-size: 0.95rem;
  font-family: inherit;
  padding: 0.55rem 0.8rem;
  resize: none;
  min-height: 92px;
  max-height: 200px;
  overflow-y: auto;
  line-height: 1.5;
  transition: border-color 0.2s;
}
#msg-input:focus { outline: none; border-color: var(--color-accent); }
#msg-input:disabled { opacity: 0.5; cursor: not-allowed; }

#send-btn {
  background: var(--color-accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0 1.1rem;
  height: 42px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.2s;
}
#send-btn:hover { opacity: 0.85; }
#send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

#input-actions {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  align-items: stretch;
  flex-shrink: 0;
}

#generate-image-btn {
  background: #7c3aed;
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0 0.8rem;
  height: 42px;
  font-size: 1.1rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.2s, background 0.2s;
  white-space: nowrap;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
}
#generate-image-btn:hover { background: #6d28d9; }
#generate-image-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ─── Status bar ─────────────────────────────────────────────────────────── */
#statusbar {
  height: var(--statusbar-h);
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  gap: 1.25rem;
  padding: 0 1rem;
  font-size: 0.75rem;
  color: var(--color-text-muted);
  flex-shrink: 0;
}

.status-item { display: flex; align-items: center; gap: 0.3rem; }
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-text-muted);
  flex-shrink: 0;
}
.status-dot.ok  { background: #3ac97a; }
.status-dot.err { background: var(--color-accent); }
.status-dot.na  { background: var(--color-text-muted); }

.status-help {
  margin-left: auto;
  position: relative;
}

.status-help-btn {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 1px solid var(--color-border);
  background: transparent;
  color: var(--color-text-muted);
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1;
  cursor: help;
}

.status-help-btn:hover,
.status-help-btn:focus-visible {
  color: var(--color-text);
  border-color: var(--color-text-muted);
  outline: none;
}

.status-help-tooltip {
  position: absolute;
  right: 0;
  bottom: calc(100% + 8px);
  width: min(360px, 80vw);
  background: #0f1228;
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  padding: 0.55rem 0.65rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  font-size: 0.74rem;
  line-height: 1.35;
  opacity: 0;
  pointer-events: none;
  transform: translateY(4px);
  transition: opacity 0.14s ease, transform 0.14s ease;
  z-index: 30;
}

.status-help:hover .status-help-tooltip,
.status-help:focus-within .status-help-tooltip {
  opacity: 1;
  transform: translateY(0);
}

/* ─── Scrollbars ─────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 3px; }

/* ─── Responsive ─────────────────────────────────────────────────────────── */
@media (max-width: 1023px) {
  :root { --sidebar-w: 240px; }
  #header .hamburger { display: block; }
  #sidebar {
    position: fixed;
    top: var(--header-h);
    left: 0;
    bottom: var(--statusbar-h);
    width: var(--sidebar-w);
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    border-right: 1px solid var(--color-border);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar-overlay {
    display: none;
    position: fixed;
    top: var(--header-h);
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.45);
    z-index: 15;
  }
  #sidebar.open ~ #sidebar-overlay { display: block; }
}

@media (max-width: 767px) {
  .msg { max-width: 90%; }
  #input-area { padding: 0.5rem; padding-bottom: calc(0.5rem + env(safe-area-inset-bottom, 0px)); }
  #char-header { padding: 0.5rem 0.75rem; }
  #message-list { padding: 0.75rem 0.5rem; }
}

/* ─── Lightbox ───────────────────────────────────────────────────────────── */
#lightbox {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.88);
  z-index: 200;
  align-items: center;
  justify-content: center;
}
#lightbox.open { display: flex; }

#lightbox-img {
  max-width: 90vw;
  max-height: 90vh;
  border-radius: 8px;
  object-fit: contain;
  box-shadow: 0 8px 40px rgba(0,0,0,0.6);
}

#lightbox-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: rgba(255,255,255,0.12);
  border: none;
  color: #fff;
  font-size: 1.4rem;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
#lightbox-close:hover { background: rgba(255,255,255,0.25); }
