* {
  box-sizing: border-box;
}

:root {
  --bg:              #faf6f1;
  --surface:         #ffffff;
  --surface-hover:   #f5ede3;
  --surface-alt:     #f8f3ec;
  --border:          #e8ddd0;
  --border-focus:    #d9894b;
  --text:            #2c2117;
  --text-muted:      #7a6b5a;
  --accent:          #c97b3a;
  --accent-hover:    #b5692f;
  --accent-light:    #fef3e6;
  --error:           #c94a3d;
  --user-bubble:     #c97b3a;
  --assistant-bubble:#ffffff;
  --skeleton:        #ede4d8;
  --skeleton-shine:  #f5ede3;
}

body {
  margin: 0;
  font-family: "Noto Sans SC", "Inter", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, .chat-empty-title, .login-card h1 {
  font-family: "Noto Serif SC", Georgia, serif;
}

.page {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.page.hidden,
.hidden {
  display: none !important;
}

/* ======================== */
/* Login                    */
/* ======================== */
#login-page {
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at 50% 0%, #ffe9cf 0%, var(--bg) 65%);
}

.login-card {
  background: var(--surface);
  border: none;
  border-radius: 16px;
  padding: 2.5rem 2.5rem 2rem;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 8px 32px rgba(44,33,23,0.08);
  backdrop-filter: blur(8px);
}

.login-card h1 {
  margin: 0 0 0.25rem;
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--text);
}

.login-tagline {
  margin: 0 0 1.75rem;
  font-size: 0.9rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.login-card form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.login-card input {
  padding: 0.75rem 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.login-card input::placeholder {
  color: var(--text-muted);
}

.login-card input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}

.login-card button {
  padding: 0.75rem 1rem;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-size: 1rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  margin-top: 0.5rem;
  box-shadow: 0 2px 8px rgba(201,123,58,0.2);
  transition: all 0.2s ease;
}

.login-card button:hover {
  background: var(--accent-hover);
  box-shadow: 0 4px 12px rgba(201,123,58,0.3);
  transform: translateY(-1px);
}

.login-card button:active {
  transform: translateY(0);
  box-shadow: 0 2px 8px rgba(201,123,58,0.2);
}

.error {
  color: var(--error);
  font-size: 0.875rem;
  margin-top: 0.75rem;
  min-height: 1.25rem;
}

/* ======================== */
/* Focus-visible (a11y)     */
/* ======================== */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.login-card input:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}

/* ======================== */
/* Main layout              */
/* ======================== */
#main-page {
  flex-direction: row;
  overflow: hidden;
  height: 100vh;
  position: relative;
}

/* ======================== */
/* Sidebar                  */
/* ======================== */
.thread-sidebar {
  width: 260px;
  min-width: 260px;
  flex-shrink: 0;
  background: var(--surface-alt);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  transition: transform 0.3s ease;
}

.sidebar-header {
  padding: 1.25rem 1rem 0.25rem;
}

.sidebar-header h2 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
}

.sidebar-header p {
  margin: 0.35rem 0 0;
  color: var(--text-muted);
  font-size: 0.78rem;
  line-height: 1.4;
}

.btn-new-thread {
  margin: 0.75rem 1rem;
  padding: 0.6rem 1rem;
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-new-thread:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-light);
}

.thread-list-wrap {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
}

.thread-list-empty {
  padding: 1rem 1rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  list-style: none;
}

#thread-list {
  list-style: none;
  margin: 0;
  padding: 0 0.5rem 0.5rem;
}

#thread-list li {
  padding: 0.65rem 0.75rem;
  border-radius: 10px;
  cursor: pointer;
  margin-bottom: 0.2rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  transition: all 0.2s ease;
}

#thread-list li:hover {
  background: var(--surface-hover);
}

#thread-list li.active {
  background: var(--accent-light);
  border-left: none;
  padding-left: 0.75rem;
  color: var(--accent-hover);
  font-weight: 500;
}

.thread-item-thumb {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--border);
  box-shadow: 0 1px 4px rgba(44,33,23,0.06);
  transition: transform 0.2s ease;
}

#thread-list li:hover .thread-item-thumb {
  transform: scale(1.05);
}

.thread-item-text {
  flex: 1;
  min-width: 0;
  font-size: 0.85rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-delete-thread {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: all 0.2s ease;
}

#thread-list li:hover .btn-delete-thread {
  opacity: 0.6;
}

.btn-delete-thread:hover {
  opacity: 1 !important;
  background: rgba(201, 74, 61, 0.1);
  color: var(--error);
}

/* ======================== */
/* Chat area                */
/* ======================== */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.chat-empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  text-align: center;
}

.chat-empty-icon {
  margin-bottom: 1rem;
  opacity: 0.6;
}

.chat-empty-title {
  font-size: 1.4rem;
  font-weight: 600;
  margin: 0 0 0.75rem;
  color: var(--text);
}

.chat-empty-desc {
  color: var(--text-muted);
  font-size: 0.95rem;
  max-width: 420px;
  line-height: 1.6;
  margin: 0;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem 1.5rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.chat-messages.hidden {
  display: none;
}

.chat-messages:not(.hidden) ~ .chat-empty {
  display: none;
}

/* ======================== */
/* Messages                 */
/* ======================== */
.msg {
  display: flex;
  gap: 0.75rem;
  max-width: 85%;
  align-self: flex-start;
  animation: msgFadeIn 0.3s ease;
}

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

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

.msg.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg-avatar {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 500;
}

.msg.user .msg-avatar {
  background: linear-gradient(135deg, var(--accent), var(--accent-hover));
  color: white;
}

.msg.assistant .msg-avatar {
  background: linear-gradient(135deg, var(--border), var(--skeleton));
  color: var(--text-muted);
}

.msg-body {
  position: relative;
  border-radius: 16px;
  padding: 0.75rem 1rem;
  font-size: 0.95rem;
  line-height: 1.5;
}

.msg.user .msg-body {
  background: var(--user-bubble);
  color: white;
  box-shadow: 0 2px 8px rgba(201,123,58,0.15);
}

.msg.assistant .msg-body {
  background: var(--assistant-bubble);
  border: none;
  box-shadow: 0 2px 12px rgba(44,33,23,0.06);
}

.msg-body .msg-prompt {
  margin: 0 0 0.5rem;
  white-space: pre-wrap;
  word-break: break-word;
  padding-right: 0;
}

.msg.user .msg-body .msg-prompt {
  padding-right: 52px;
}

.msg-ref-thumb {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  object-fit: cover;
  border: 1px solid rgba(255,255,255,0.3);
  transition: transform 0.2s ease;
}

.msg-ref-thumb:hover {
  transform: scale(1.1);
}

/* Skeleton loading */
.msg-skeleton {
  padding: 0.25rem 0;
}

.skeleton-line {
  height: 14px;
  border-radius: 7px;
  background: var(--skeleton);
  margin-bottom: 0.5rem;
  animation: skeletonPulse 1.5s ease-in-out infinite;
}

.skeleton-line:nth-child(1) { width: 70%; }
.skeleton-line:nth-child(2) { width: 50%; animation-delay: 0.1s; }

.skeleton-image {
  width: 100%;
  max-width: 280px;
  height: 180px;
  border-radius: 12px;
  background: var(--skeleton);
  animation: skeletonPulse 1.5s ease-in-out infinite;
  animation-delay: 0.2s;
}

@keyframes skeletonPulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.5; }
}

/* Loading hint with spinner */
.msg-loading-hint {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-muted);
  font-size: 0.88rem;
  margin-bottom: 0.5rem;
}

.loading-spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.msg-body .msg-loading {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-top: 0.5rem;
}

.msg-body .msg-image {
  max-width: 100%;
  max-height: 320px;
  border-radius: 12px;
  display: block;
  margin-top: 0.5rem;
  box-shadow: 0 2px 12px rgba(44,33,23,0.08);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.msg-body .msg-image:hover {
  transform: scale(1.02);
  box-shadow: 0 4px 20px rgba(44,33,23,0.12);
}

.msg-time {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 0.35rem;
}

.msg.user .msg-time {
  color: rgba(255,255,255,0.7);
}

/* ======================== */
/* Input area (card layout) */
/* ======================== */
.chat-input-wrap {
  position: relative;
  padding: 1rem 1.5rem 1.25rem;
  border-top: none;
  background: var(--surface);
  box-shadow: 0 -4px 16px rgba(44,33,23,0.04);
}

.chat-input-card {
  max-width: 720px;
  margin: 0 auto;
  background: #e8e8e8;
  border: 1px solid rgba(0,0,0,0.06);
  border-radius: 20px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.06);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: box-shadow 0.2s ease;
}

.chat-input-card:focus-within {
  box-shadow: 0 4px 24px rgba(0,0,0,0.08);
}

.chat-input-top {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  padding: 0.75rem 1rem 0.5rem;
  min-width: 0;
}

.chat-input-ref {
  flex-shrink: 0;
  width: 56px;
  height: 56px;
  background: rgba(255,255,255,0.6);
  border: 2px dashed #b0b0b0;
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transform: rotate(-2deg);
  transition: border-color 0.2s ease, background 0.2s ease;
}

.chat-input-ref:hover {
  border-color: var(--accent);
  background: rgba(255,255,255,0.9);
}

.chat-input-ref .ref-panel-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  cursor: pointer;
  transform: rotate(2deg);
}

.chat-input-ref .ref-panel-img.hidden {
  display: none;
}

.chat-input-ref .ref-panel-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  cursor: pointer;
  color: #888;
  font-size: 1.5rem;
  font-weight: 300;
  line-height: 1;
  border: none;
  background: transparent;
  padding: 0;
}

.chat-input-ref .ref-panel-plus {
  pointer-events: none;
}

.chat-input-ref .ref-panel-placeholder.hidden {
  display: none;
}

.chat-input-ref .ref-panel-change {
  position: absolute;
  bottom: 4px;
  right: 4px;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: white;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease;
}

.chat-input-ref .ref-panel-change:hover {
  background: var(--accent-hover);
}

.chat-input-ref .ref-panel-change.hidden {
  display: none !important;
}

.chat-input-top textarea {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
  resize: none;
  min-height: 40px;
  max-height: 200px;
  padding: 0.5rem 0;
  line-height: 1.5;
  overflow-y: auto;
}

.chat-input-top textarea::placeholder {
  color: #9a9a9a;
}

.chat-input-top textarea:focus {
  outline: none;
}

.chat-input-bottom {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem 0.75rem;
  flex-wrap: wrap;
}

.input-toolbar-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.75rem;
  border: 1px solid rgba(0,0,0,0.08);
  border-radius: 10px;
  background: rgba(255,255,255,0.7);
  color: var(--text-muted);
  font-size: 0.85rem;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

.input-toolbar-btn:hover:not(:disabled) {
  background: rgba(255,255,255,0.95);
  color: var(--text);
}

.input-toolbar-btn-primary {
  color: #0d9488;
  border-color: rgba(13,148,136,0.25);
  background: rgba(13,148,136,0.08);
}

.input-toolbar-btn-primary:hover {
  background: rgba(13,148,136,0.12);
  color: #0f766e;
}

.input-toolbar-btn select {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.input-toolbar-btn .input-toolbar-text {
  white-space: nowrap;
}

.input-toolbar-btn .input-toolbar-caret {
  font-size: 0.65rem;
  color: #aaa;
  margin-left: 0.1rem;
}

.input-toolbar-icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  opacity: 0.8;
}

.input-toolbar-icon-agent {
  background: currentColor;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2'%3E%3Cpath d='M12 2a4 4 0 0 1 4 4v2a4 4 0 0 1-8 0V6a4 4 0 0 1 4-4z'/%3E%3Cpath d='M4 14v2a8 8 0 0 0 16 0v-2'/%3E%3C/svg%3E") center/contain no-repeat;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2'%3E%3Cpath d='M12 2a4 4 0 0 1 4 4v2a4 4 0 0 1-8 0V6a4 4 0 0 1 4-4z'/%3E%3Cpath d='M4 14v2a8 8 0 0 0 16 0v-2'/%3E%3C/svg%3E") center/contain no-repeat;
}

.input-toolbar-icon-auto {
  background: currentColor;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2'%3E%3Cline x1='4' y1='8' x2='20' y2='8'/%3E%3Cline x1='4' y1='12' x2='14' y2='12'/%3E%3Cline x1='4' y1='16' x2='18' y2='16'/%3E%3C/svg%3E") center/contain no-repeat;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2'%3E%3Cline x1='4' y1='8' x2='20' y2='8'/%3E%3Cline x1='4' y1='12' x2='14' y2='12'/%3E%3Cline x1='4' y1='16' x2='18' y2='16'/%3E%3C/svg%3E") center/contain no-repeat;
}

.input-toolbar-icon-bulb {
  background: currentColor;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2'%3E%3Cpath d='M9 18h6'/%3E%3Cpath d='M10 22h4'/%3E%3Cpath d='M15.09 14c.18-.98.65-1.74 1.41-2.5A4.65 4.65 0 0 0 18 8 6 6 0 0 0 6 8c0 1 .23 2.23 1.5 3.5A4.61 4.61 0 0 1 8.91 14'/%3E%3C/svg%3E") center/contain no-repeat;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2'%3E%3Cpath d='M9 18h6'/%3E%3Cpath d='M10 22h4'/%3E%3Cpath d='M15.09 14c.18-.98.65-1.74 1.41-2.5A4.65 4.65 0 0 0 18 8 6 6 0 0 0 6 8c0 1 .23 2.23 1.5 3.5A4.61 4.61 0 0 1 8.91 14'/%3E%3C/svg%3E") center/contain no-repeat;
}

.input-toolbar-btn-extra {
  opacity: 0.85;
  transition: opacity 0.2s ease, background 0.2s ease;
}

.input-toolbar-btn-extra:disabled {
  cursor: default;
  opacity: 0.5;
}

#btn-storyboard {
  opacity: 1;
  color: #7c3aed;
  border-color: rgba(124,58,237,0.2);
  background: rgba(124,58,237,0.06);
}

#btn-storyboard:hover {
  background: rgba(124,58,237,0.12);
  color: #6d28d9;
}

.btn-send-circle {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  margin-left: auto;
  border: none;
  border-radius: 50%;
  background: rgba(255,255,255,0.9);
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
  transition: all 0.2s ease;
}

.btn-send-circle:hover:not(:disabled) {
  background: var(--accent);
  color: white;
  box-shadow: 0 2px 10px rgba(201,123,58,0.3);
  transform: scale(1.05);
}

.btn-send-circle:active:not(:disabled) {
  transform: scale(0.98);
}

.btn-send-circle:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ======================== */
/* Context menu             */
/* ======================== */
.context-menu {
  position: fixed;
  z-index: 1000;
  min-width: 170px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(44,33,23,0.12);
  padding: 0.3rem;
  opacity: 0;
  transform: scale(0.95);
  transition: opacity 0.15s ease, transform 0.15s ease;
  pointer-events: none;
}

.context-menu.show {
  opacity: 1;
  transform: scale(1);
  pointer-events: auto;
}

.context-menu.hidden {
  display: none !important;
}

.context-menu-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.55rem 0.75rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s ease;
}

.context-menu-item:hover {
  background: var(--surface-hover);
}

.context-menu-item.hidden {
  display: none !important;
}

.context-menu-item .ctx-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  opacity: 0.7;
}

/* ======================== */
/* Lightbox                 */
/* ======================== */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  opacity: 0;
  transition: opacity 0.2s ease;
  pointer-events: none;
}

.lightbox.show {
  opacity: 1;
  pointer-events: auto;
}

.lightbox.show .lightbox-img {
  transform: scale(1);
}

.lightbox.hidden {
  display: none !important;
}

.lightbox-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(44, 33, 23, 0.65);
  cursor: pointer;
  backdrop-filter: blur(4px);
}

.lightbox-img {
  position: relative;
  z-index: 1;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 12px;
  pointer-events: none;
  transform: scale(0.92);
  transition: transform 0.25s ease;
}

/* ======================== */
/* Library popover           */
/* ======================== */
.library-popover {
  position: absolute;
  bottom: calc(100% - 0.5rem);
  left: 1.5rem;
  z-index: 200;
  width: 340px;
  max-height: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 8px 32px rgba(44,33,23,0.14);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.15s ease, transform 0.15s ease;
  pointer-events: none;
}

.library-popover.show {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.library-popover.hidden {
  display: none !important;
}

.library-popover-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.65rem 0.85rem;
  border-bottom: 1px solid var(--border);
}

.library-popover-title {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
}

.library-upload-btn {
  font-size: 0.8rem;
  color: var(--accent);
  cursor: pointer;
  padding: 0.3rem 0.65rem;
  border: 1px solid rgba(201,123,58,0.25);
  border-radius: 6px;
  background: var(--accent-light);
  transition: background 0.15s ease;
}

.library-upload-btn:hover {
  background: rgba(201,123,58,0.15);
}

.library-grid {
  flex: 1;
  overflow-y: auto;
  padding: 0.6rem;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.45rem;
  align-content: start;
}

.library-empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.85rem;
  padding: 1.5rem 0;
}

.library-thumb-wrap {
  position: relative;
  aspect-ratio: 1;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.library-thumb-wrap:hover {
  border-color: var(--accent);
  transform: scale(1.05);
}

.library-thumb-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.library-thumb-delete {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  border: none;
  border-radius: 50%;
  background: rgba(0,0,0,0.55);
  color: white;
  font-size: 0.7rem;
  line-height: 1;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
}

.library-thumb-wrap:hover .library-thumb-delete {
  display: flex;
}

.library-thumb-name {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0,0,0,0.5);
  color: white;
  font-size: 0.65rem;
  padding: 1px 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.library-popover-footer {
  padding: 0.5rem 0.85rem;
  border-top: 1px solid var(--border);
}

.library-direct-upload-btn {
  font-size: 0.78rem;
  color: var(--text-muted);
  cursor: pointer;
  transition: color 0.15s ease;
}

.library-direct-upload-btn:hover {
  color: var(--accent);
}

/* ======================== */
/* Storyboard modal         */
/* ======================== */
.storyboard-modal {
  position: fixed;
  inset: 0;
  z-index: 1500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  opacity: 0;
  transition: opacity 0.2s ease;
  pointer-events: none;
}

.storyboard-modal.show {
  opacity: 1;
  pointer-events: auto;
}

.storyboard-modal.hidden {
  display: none !important;
}

.storyboard-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(44, 33, 23, 0.5);
  backdrop-filter: blur(4px);
  cursor: pointer;
}

.storyboard-panel {
  position: relative;
  z-index: 1;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(44,33,23,0.15);
  width: 100%;
  max-width: 540px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: scale(0.95) translateY(8px);
  transition: transform 0.25s ease;
}

.storyboard-modal.show .storyboard-panel {
  transform: scale(1) translateY(0);
}

.storyboard-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.storyboard-header h3 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  font-family: "Noto Serif SC", Georgia, serif;
}

.storyboard-close {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.4rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
}

.storyboard-close:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.storyboard-body {
  padding: 1.25rem;
  overflow-y: auto;
  flex: 1;
}

.storyboard-label {
  display: block;
  font-size: 0.88rem;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 0.4rem;
}

.storyboard-story-input {
  width: 100%;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
  resize: none;
  line-height: 1.5;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.storyboard-story-input::placeholder {
  color: var(--text-muted);
}

.storyboard-story-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}

.storyboard-hint {
  margin: 0.5rem 0 0;
  font-size: 0.82rem;
  color: var(--text-muted);
  line-height: 1.4;
}

.storyboard-lang-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.75rem;
}

.storyboard-lang-label {
  font-size: 0.85rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.storyboard-lang-select {
  padding: 0.4rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.2s ease;
}

.storyboard-lang-select:focus {
  outline: none;
  border-color: var(--accent);
}

.storyboard-btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  margin-top: 1rem;
  padding: 0.65rem 1.5rem;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-size: 0.95rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(201,123,58,0.2);
  transition: all 0.2s ease;
  width: 100%;
}

.storyboard-btn-primary:hover {
  background: var(--accent-hover);
  box-shadow: 0 4px 12px rgba(201,123,58,0.3);
}

.storyboard-btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.storyboard-btn-secondary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  padding: 0.6rem 1.25rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.2s ease;
}

.storyboard-btn-secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-light);
}

.storyboard-loading {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-muted);
  font-size: 0.9rem;
  padding: 1rem 0;
}

.storyboard-result {
  margin-bottom: 0.25rem;
}

.storyboard-prompt-output {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  resize: vertical;
  min-height: 140px;
  line-height: 1.6;
  transition: border-color 0.2s ease;
}

.storyboard-prompt-output:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}

.storyboard-actions {
  display: flex;
  gap: 0.75rem;
  margin-top: 1rem;
}

.storyboard-actions .storyboard-btn-primary {
  flex: 1;
  width: auto;
}

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

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

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

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ======================== */
/* Mobile sidebar toggle    */
/* ======================== */
.btn-sidebar-toggle {
  display: none;
  position: fixed;
  top: 0.75rem;
  left: 0.75rem;
  z-index: 110;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 2px 8px rgba(44,33,23,0.08);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.btn-sidebar-toggle:hover {
  background: var(--surface-hover);
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 99;
  background: rgba(44,33,23,0.3);
  backdrop-filter: blur(2px);
}

.msg-ref-thumb,
.msg-image,
.ref-panel-img {
  cursor: pointer;
}

/* ======================== */
/* Reduced motion           */
/* ======================== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ======================== */
/* Responsive ≤768px        */
/* ======================== */
@media (max-width: 768px) {
  .btn-sidebar-toggle {
    display: flex;
  }

  .thread-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 100;
    transform: translateX(-100%);
    box-shadow: none;
  }

  #main-page.sidebar-open .thread-sidebar {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(44,33,23,0.12);
  }

  #main-page.sidebar-open .sidebar-overlay {
    display: block;
  }

  .chat-area {
    width: 100%;
  }

  .chat-messages {
    padding: 1rem 1rem 0.5rem;
  }

  .msg {
    max-width: 95%;
  }

  .chat-input-wrap {
    padding: 0.75rem 1rem 1rem;
  }

  .chat-input-card {
    max-width: none;
  }

  .chat-input-ref {
    width: 48px;
    height: 48px;
  }

  .chat-input-bottom {
    gap: 0.4rem;
    padding: 0.4rem 0.75rem 0.6rem;
  }

  .input-toolbar-btn {
    padding: 0.4rem 0.6rem;
    font-size: 0.8rem;
  }

  .btn-send-circle {
    width: 36px;
    height: 36px;
  }

  .login-card {
    margin: 0 1rem;
  }
}
