:root {
  --bg: #0a0a0b;
  --bg2: #141416;
  --bg3: #1e1e22;
  --bg4: #28282e;
  --text: #e4e4e7;
  --text2: #a1a1aa;
  --text3: #52525b;
  --border: #27272a;
  --primary: #7c3aed;
  --primary-h: #8b5cf6;
  --accent: #06b6d4;
  --danger: #ef4444;
  --success: #22c55e;
  --sidebar-w: 260px;
  --input-h: 80px;
}

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

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

.hidden { display: none !important; }

#app { display: flex; height: 100vh; }

/* ===== Login ===== */
#login-view {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  animation: fadeIn 0.3s;
}
.login-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px;
  text-align: center;
  max-width: 380px;
  width: 90%;
}
.login-icon { font-size: 48px; margin-bottom: 12px; }
.login-card h1 { font-size: 24px; font-weight: 600; margin-bottom: 4px; }
.login-sub { font-size: 13px; color: var(--text3); margin-bottom: 24px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; }
.login-card input {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 14px;
  outline: none;
}
.login-card input:focus { border-color: var(--primary); }
.login-card input::placeholder { color: var(--text3); }
.btn-login {
  background: var(--primary);
  border: none;
  color: #fff;
  padding: 12px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.btn-login:hover { background: var(--primary-h); }
.login-hint { font-size: 11px; color: var(--text3); margin-top: 16px; }

.btn-logout {
  background: none;
  border: 1px solid var(--border);
  color: var(--text3);
  padding: 3px 10px;
  border-radius: 6px;
  font-size: 11px;
  cursor: pointer;
  margin-top: 4px;
}
.btn-logout:hover { color: var(--danger); border-color: var(--danger); }

/* ===== Sidebar ===== */
#sidebar {
  width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-top { padding: 12px; }

.btn-new {
  width: 100%;
  padding: 10px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.btn-new:hover { background: var(--bg3); border-color: var(--primary); }

#session-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}

.session-item {
  display: flex;
  align-items: center;
  padding: 10px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text2);
  transition: all 0.15s;
  margin-bottom: 2px;
}
.session-item:hover { background: var(--bg3); }
.session-item.active { background: var(--bg3); color: var(--text); }

.session-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-del {
  background: none;
  border: none;
  font-size: 11px;
  cursor: pointer;
  opacity: 0;
  padding: 2px 4px;
  transition: opacity 0.15s;
}
.session-item:hover .session-del { opacity: 1; }
.session-del:hover { opacity: 1; }

.sidebar-bottom {
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text3);
}

/* ===== Main Chat ===== */
#chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 0;
}

/* Empty state */
#empty-state {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -60%);
  text-align: center;
  color: var(--text3);
}
#empty-state.hidden { display: none; }
.empty-icon { font-size: 48px; margin-bottom: 12px; }
#empty-state h2 { font-size: 22px; color: var(--text); margin-bottom: 6px; font-weight: 600; }
#empty-state p { font-size: 14px; margin-bottom: 20px; }

.empty-tips { display: flex; flex-direction: column; gap: 8px; align-items: center; }
.tip {
  background: var(--bg3);
  padding: 8px 18px;
  border-radius: 20px;
  font-size: 13px;
  color: var(--text2);
  cursor: pointer;
  transition: all 0.15s;
  max-width: 400px;
}
.tip:hover { background: var(--bg4); color: var(--text); }

/* Message bubbles */
.message {
  display: flex;
  gap: 12px;
  padding: 12px 40px;
  max-width: 820px;
  margin: 0 auto;
  width: 100%;
}
.message.user {  }
.message.assistant {  }

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
  background: var(--bg3);
}
.message.user .avatar { background: var(--primary); }
.message.assistant .avatar { background: var(--accent); }

.bubble {
  flex: 1;
  min-width: 0;
}

.msg-text {
  font-size: 14px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Reference images in message */
.msg-ref-images {
  display: flex;
  gap: 6px;
  margin-top: 8px;
  flex-wrap: wrap;
}
.ref-thumb {
  width: 60px;
  height: 60px;
  object-fit: cover;
  border-radius: 6px;
  cursor: pointer;
  border: 1px solid var(--border);
  transition: transform 0.15s;
}
.ref-thumb:hover { transform: scale(1.05); }

/* Generated images */
.msg-gen-images {
  display: flex;
  gap: 10px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.gen-img-wrap {
  position: relative;
  display: inline-block;
}
.gen-img {
  max-width: 512px;
  max-height: 512px;
  border-radius: 10px;
  cursor: pointer;
  display: block;
  box-shadow: 0 4px 20px rgba(0,0,0,0.3);
  transition: transform 0.2s;
}
.gen-img:hover { transform: scale(1.02); }

.gen-actions {
  position: absolute;
  bottom: 8px;
  right: 8px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.2s;
}
.gen-img-wrap:hover .gen-actions { opacity: 1; }
.gen-actions button {
  background: rgba(0,0,0,0.7);
  border: none;
  color: #fff;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(8px);
}
.gen-actions button:hover { background: var(--primary); }

/* Loading dots */
.loading-dots {
  display: flex;
  gap: 6px;
  padding: 8px 0;
}
.loading-dots span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text3);
  animation: dotPulse 1.4s ease-in-out infinite;
}
.loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.loading-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes dotPulse {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1.2); }
}
.loading-hint {
  font-size: 11px;
  color: var(--text3);
  margin-top: 4px;
}
.loading-timer {
  color: var(--text3);
  font-size: 12px;
  margin-left: 4px;
}

/* ===== Input Area ===== */
#input-area {
  border-top: 1px solid var(--border);
  background: var(--bg2);
  padding: 12px 20px 16px;
  max-width: 820px;
  margin: 0 auto;
  width: 100%;
}

/* Ref preview */
#ref-preview {
  margin-bottom: 8px;
}
#ref-preview.hidden { display: none; }
#ref-images {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.ref-thumb-wrap {
  position: relative;
  display: inline-block;
}
.ref-thumb-wrap .ref-thumb {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 8px;
  border: 2px solid var(--primary);
}
.ref-check {
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--success);
  color: #fff;
  font-size: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ref-uploading {
  background: var(--bg3);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  border: 2px solid var(--accent);
}
.ref-progress-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 100%;
  background: rgba(6,182,212,0.2);
  transition: width 0.3s;
}
.ref-progress-label {
  position: relative;
  z-index: 1;
  font-size: 11px;
  color: var(--accent);
  font-weight: 600;
}
.ref-error {
  background: var(--bg3);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--danger);
  font-size: 16px;
}
.ref-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--danger);
  border: none;
  color: #fff;
  font-size: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Params bar */
#params-bar {
  display: flex;
  gap: 12px;
  margin-bottom: 8px;
}
.param-group {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text3);
}
.param-group select {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text2);
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 12px;
  outline: none;
  cursor: pointer;
}
.param-group select:focus { border-color: var(--primary); }

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

.btn-icon {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text2);
  width: 38px;
  height: 38px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.15s;
}
.btn-icon:hover { background: var(--bg4); color: var(--text); }

.input-wrap {
  flex: 1;
  position: relative;
}

#msg-input {
  width: 100%;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 22px;
  font-size: 14px;
  resize: none;
  outline: none;
  font-family: inherit;
  max-height: 150px;
  min-height: 38px;
  line-height: 1.5;
}
#msg-input:focus { border-color: var(--primary); }
#msg-input::placeholder { color: var(--text3); }

.btn-send {
  background: var(--primary);
  border: none;
  color: #fff;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 18px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.15s;
}
.btn-send:hover { background: var(--primary-h); transform: scale(1.05); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

/* ===== History Picker ===== */
#history-picker {
  position: absolute;
  bottom: 140px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  max-width: 600px;
  width: 90%;
  box-shadow: 0 8px 30px rgba(0,0,0,0.5);
  z-index: 20;
  animation: fadeIn 0.2s;
}
#history-picker.hidden { display: none; }

.picker-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
  font-size: 13px;
  color: var(--text2);
}

#history-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
  max-height: 250px;
  overflow-y: auto;
}

.history-thumb-wrap {
  cursor: pointer;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  transition: all 0.15s;
}
.history-thumb-wrap:hover { border-color: var(--primary); transform: scale(1.03); }
.history-thumb-wrap img { width: 100%; height: 80px; object-fit: cover; display: block; }
.history-thumb-label {
  padding: 4px 6px;
  font-size: 10px;
  color: var(--text3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: var(--bg3);
}

/* ===== Lightbox ===== */
#lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  cursor: pointer;
  animation: fadeIn 0.2s;
}
#lightbox.hidden { display: none; }
#lightbox img {
  max-width: 90vw;
  max-height: 80vh;
  border-radius: 10px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.5);
}

.lightbox-actions {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}
.lightbox-actions button {
  background: rgba(0,0,0,0.7);
  border: 1px solid var(--border);
  color: #fff;
  padding: 8px 18px;
  border-radius: 20px;
  cursor: pointer;
  font-size: 13px;
  backdrop-filter: blur(10px);
  transition: all 0.15s;
}
.lightbox-actions button:hover { background: var(--primary); border-color: var(--primary); }

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Scrollbar */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* Responsive */
@media (max-width: 768px) {
  #sidebar { display: none; }
  .message { padding: 10px 16px; }
  #input-area {
    padding: 8px 10px;
    max-width: 100%;
    position: sticky;
    bottom: 0;
    background: var(--bg2);
    z-index: 10;
  }
  .gen-img { max-width: 100%; }
  #params-bar { gap: 8px; }
  .param-group select { font-size: 11px; padding: 2px 6px; }
  .input-row { gap: 6px; }
  #msg-input { font-size: 16px; padding: 8px 14px; } /* 16px prevents iOS zoom */
  .btn-icon, .btn-send { width: 36px; height: 36px; font-size: 15px; }
  #empty-state { padding: 0 20px; }
  .empty-tips { gap: 6px; }
  .tip { font-size: 12px; padding: 6px 14px; }
  #chat-main {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* dynamic viewport height for mobile */
  }
  #messages {
    flex: 1;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
  }
  #input-area {
    flex-shrink: 0;
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }
}
