:root {
  --bg-base: #09090d;
  --bg-sidebar: #0e0e14;
  --bg-surface: #14141d;
  --bg-elevated: #1c1c28;
  --bg-hover: #242434;
  --border: rgba(255, 255, 255, 0.08);
  --border-focus: rgba(139, 92, 246, 0.55);
  --accent: #8b5cf6;
  --accent-bright: #a78bfa;
  --accent-glow: rgba(139, 92, 246, 0.22);
  --text-main: #f3f4f6;
  --text-muted: #9ca3af;
  --text-dim: #6b7280;
  --danger: #f43f5e;
  --success: #10b981;
  --radius-lg: 14px;
  --radius-md: 10px;
  --radius-sm: 7px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  height: 100dvh;
  background: var(--bg-base);
  color: var(--text-main);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.12); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.22); }

button {
  font-family: inherit;
  font-size: 0.86rem;
  font-weight: 500;
  color: var(--text-main);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 13px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  transition: all 0.16s ease;
}
button:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: rgba(255,255,255,0.16);
}
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary {
  background: linear-gradient(135deg, #8b5cf6 0%, #6d28d9 100%);
  border-color: rgba(167, 139, 250, 0.35);
  color: #fff;
  font-weight: 600;
  box-shadow: 0 4px 16px var(--accent-glow);
}
button.primary:hover:not(:disabled) {
  filter: brightness(1.08);
  transform: translateY(-1px);
}
button.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-muted);
}
button.ghost:hover {
  background: rgba(255,255,255,0.06);
  color: var(--text-main);
}

input, select, textarea {
  font-family: inherit;
  font-size: 0.92rem;
  color: var(--text-main);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 10px 13px;
  transition: border-color 0.16s, box-shadow 0.16s;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.14);
}

#toast {
  position: fixed;
  bottom: 22px;
  right: 22px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-focus);
  color: var(--text-main);
  padding: 10px 15px;
  border-radius: var(--radius-md);
  font-size: 0.85rem;
  box-shadow: 0 12px 32px rgba(0,0,0,0.65);
  z-index: 99;
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
  transition: all 0.2s ease;
}
#toast.show { opacity: 1; transform: translateY(0); }

/* AUTH SCREEN */
#authScreen {
  height: 100dvh;
  display: none;
  place-items: center;
  padding: 20px;
  background:
    radial-gradient(circle at 20% 20%, rgba(139, 92, 246, 0.14), transparent 40%),
    radial-gradient(circle at 80% 80%, rgba(79, 70, 229, 0.12), transparent 40%),
    var(--bg-base);
}
.auth-card {
  width: 100%;
  max-width: 410px;
  background: rgba(20, 20, 29, 0.85);
  backdrop-filter: blur(18px);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 28px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.65);
  display: flex;
  flex-direction: column;
  gap: 16px;
  animation: fadeUp 0.35s ease;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 12px var(--accent);
}
.brand span { color: var(--accent-bright); }
.auth-sub { font-size: 0.86rem; color: var(--text-muted); margin-top: -8px; }
.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--bg-base);
  padding: 4px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
}
.tabs button {
  background: transparent;
  border: none;
  color: var(--text-muted);
  padding: 8px;
}
.tabs button.active {
  background: var(--bg-elevated);
  color: var(--text-main);
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 0.78rem; color: var(--text-muted); font-weight: 500; }
.err-box { color: var(--danger); font-size: 0.83rem; min-height: 18px; }

/* MAIN APP LAYOUT */
#appScreen {
  height: 100dvh;
  display: none;
  grid-template-columns: 270px 1fr;
}

aside {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px;
  gap: 10px;
  z-index: 30;
  transition: transform 0.25s ease;
}
.sidebar-top { display: flex; align-items: center; justify-content: space-between; padding: 4px 4px 4px; }
#newChatBtn {
  width: 100%;
  justify-content: flex-start;
  padding: 10px 12px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  font-weight: 600;
}
#newChatBtn:hover { border-color: var(--border-focus); background: var(--bg-elevated); }
.history-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  padding: 2px 6px 0;
}
#sessionList {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.session-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.85rem;
  border: 1px solid transparent;
  transition: all 0.14s;
}
.session-item:hover { background: var(--bg-surface); color: var(--text-main); }
.session-item.active {
  background: var(--bg-elevated);
  color: var(--text-main);
  border-color: var(--border);
}
.session-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.session-actions { display: flex; gap: 2px; opacity: 0; transition: opacity 0.14s; }
.session-item:hover .session-actions { opacity: 1; }
.icon-mini {
  padding: 3px 7px;
  font-size: 0.75rem;
  color: var(--text-muted);
  background: rgba(255,255,255,0.04);
  border: 1px solid transparent;
}
.icon-mini:hover { color: var(--text-main); background: rgba(255,255,255,0.1); }
.icon-mini.danger:hover { color: var(--danger); background: rgba(244, 63, 94, 0.14); }

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.usage-box {
  font-size: 0.75rem;
  color: var(--text-muted);
  background: var(--bg-base);
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  text-align: center;
}
.user-pill {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 0.84rem;
}
.role-tag {
  font-size: 0.72rem;
  padding: 2px 7px;
  border-radius: 99px;
  background: rgba(139, 92, 246, 0.18);
  color: var(--accent-bright);
  border: 1px solid rgba(139, 92, 246, 0.3);
}
.sidebar-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }

/* WORKSPACE SPLIT (CHAT + LIVE PREVIEW) */
.workspace {
  display: grid;
  grid-template-columns: 1fr;
  height: 100dvh;
  min-width: 0;
}
.workspace.with-preview {
  grid-template-columns: 1fr 1fr;
}

.main-area {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  min-width: 0;
  position: relative;
  background: radial-gradient(circle at 50% 0%, rgba(139, 92, 246, 0.06), transparent 45%), var(--bg-base);
}
.topbar {
  height: 54px;
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(9, 9, 13, 0.75);
  backdrop-filter: blur(12px);
}
.model-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-muted);
  background: var(--bg-surface);
  border: 1px solid var(--border);
  padding: 5px 11px;
  border-radius: 99px;
}
.model-badge i {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 8px var(--success);
}
#menuBtn { display: none; padding: 6px 10px; }

/* LIVE PREVIEW PANE */
.preview-pane {
  display: none;
  flex-direction: column;
  border-left: 1px solid var(--border);
  background: #0d0d12;
  height: 100dvh;
  min-width: 0;
}
.workspace.with-preview .preview-pane { display: flex; }
.preview-bar {
  height: 54px;
  border-bottom: 1px solid var(--border);
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-sidebar);
}
.preview-stage {
  flex: 1;
  display: flex;
  justify-content: center;
  background: #1a1a22;
  overflow: hidden;
}
#previewFrame {
  width: 100%;
  height: 100%;
  border: none;
  background: #fff;
  transition: width 0.25s ease;
}
#previewFrame.mobile {
  width: 390px;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
}

/* MESSAGES */
#chatScroll { flex: 1; overflow-y: auto; padding: 22px 18px 16px; }
#chatContainer {
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.empty-state {
  margin: auto;
  max-width: 640px;
  padding: 36px 12px 10px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 20px;
  animation: fadeUp 0.3s ease;
}
.empty-state h2 {
  font-size: 1.55rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: linear-gradient(to right, #fff, #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.empty-state p { color: var(--text-muted); font-size: 0.92rem; margin-top: -10px; }
.starter-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; text-align: left; }
.starter-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 13px 15px;
  cursor: pointer;
  transition: all 0.16s;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.starter-card:hover { background: var(--bg-elevated); border-color: var(--border-focus); transform: translateY(-2px); }
.starter-card strong { font-size: 0.86rem; color: var(--text-main); }
.starter-card span { font-size: 0.79rem; color: var(--text-muted); }

.msg-row { display: flex; gap: 12px; animation: fadeUp 0.2s ease; }
.msg-row.user { justify-content: flex-end; }
.avatar {
  width: 30px; height: 30px; border-radius: 8px; flex-shrink: 0;
  display: grid; place-items: center;
  font-size: 0.76rem; font-weight: 700;
  background: linear-gradient(135deg, #8b5cf6, #4f46e5);
  color: #fff; margin-top: 2px;
}
.bubble {
  max-width: 85%;
  padding: 13px 16px;
  border-radius: var(--radius-lg);
  font-size: 0.94rem;
  line-height: 1.62;
  word-wrap: break-word;
}
.msg-row.user .bubble {
  background: var(--bg-elevated);
  border: 1px solid rgba(139, 92, 246, 0.28);
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}
.msg-row.assistant .bubble {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-top-left-radius: 4px;
  width: 100%;
  max-width: 100%;
}

.markdown p { margin-bottom: 10px; }
.markdown p:last-child { margin-bottom: 0; }
.markdown ul, .markdown ol { margin: 8px 0 10px 22px; }
.markdown li { margin-bottom: 4px; }
.markdown h1, .markdown h2, .markdown h3 { margin: 14px 0 8px; font-weight: 600; line-height: 1.3; }
.markdown blockquote { border-left: 3px solid var(--accent); padding-left: 12px; color: var(--text-muted); margin: 8px 0; }
.markdown table { width: 100%; border-collapse: collapse; margin: 10px 0; font-size: 0.86rem; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; }
.markdown th { background: var(--bg-elevated); }
.markdown :not(pre) > code {
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.84em;
  background: rgba(139, 92, 246, 0.14);
  color: var(--accent-bright);
  padding: 2px 6px;
  border-radius: 5px;
}

.code-wrap {
  margin: 10px 0;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: #1a1b26;
}
.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background: #13141c;
  border-bottom: 1px solid var(--border);
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.75rem;
  color: var(--text-muted);
}
.code-btns { display: flex; gap: 6px; }
.copy-code-btn, .run-code-btn {
  padding: 3px 8px;
  font-size: 0.74rem;
  background: rgba(255,255,255,0.06);
  border-color: transparent;
}
.run-code-btn {
  background: rgba(139, 92, 246, 0.22);
  color: var(--accent-bright);
}
.code-wrap pre {
  margin: 0;
  padding: 12px 14px;
  overflow-x: auto;
  font-family: 'JetBrains Mono', monospace;
  font-size: 0.85rem;
  line-height: 1.5;
}

.msg-meta {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  padding-top: 6px;
  border-top: 1px solid rgba(255,255,255,0.05);
  font-size: 0.74rem;
  color: var(--text-dim);
}
.meta-btn {
  padding: 2px 8px;
  font-size: 0.73rem;
  background: transparent;
  border-color: var(--border);
  color: var(--text-muted);
}

.typing { display: inline-flex; gap: 5px; padding: 4px 2px; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent-bright);
  animation: bounce 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

/* COMPOSER */
.composer-wrap {
  padding: 10px 18px 16px;
  background: linear-gradient(to top, var(--bg-base) 75%, transparent);
}
.composer {
  max-width: 820px;
  margin: 0 auto;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 8px 10px 8px 14px;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.45);
  transition: border-color 0.16s, box-shadow 0.16s;
}
.composer:focus-within {
  border-color: var(--border-focus);
  box-shadow: 0 10px 35px rgba(139, 92, 246, 0.14);
}
#input {
  flex: 1;
  background: transparent;
  border: none;
  box-shadow: none;
  padding: 6px 0;
  resize: none;
  max-height: 180px;
  min-height: 24px;
  line-height: 1.5;
}
.hint-line {
  max-width: 820px;
  margin: 6px auto 0;
  text-align: center;
  font-size: 0.73rem;
  color: var(--text-dim);
}

/* MODALS */
dialog {
  background: var(--bg-surface);
  color: var(--text-main);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 22px;
  max-width: 560px;
  width: 92%;
  margin: auto;
  box-shadow: 0 25px 60px rgba(0,0,0,0.75);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.7); backdrop-filter: blur(4px); }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.gen-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
.sub-box { background: var(--bg-elevated); border: 1px solid var(--border); border-radius: var(--radius-md); padding: 12px; }
.inv-list { max-height: 280px; overflow-y: auto; display: flex; flex-direction: column; gap: 7px; }
.inv-item {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  font-size: 0.84rem;
}
.inv-code { font-family: 'JetBrains Mono', monospace; color: var(--accent-bright); font-weight: 500; }
.status-free { color: var(--success); }
.status-used { color: var(--text-dim); }

#backdrop { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 20; }

@keyframes fadeUp { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
@keyframes bounce { 0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; } 40% { transform: scale(1); opacity: 1; } }

@media (max-width: 920px) {
  .workspace.with-preview { grid-template-columns: 1fr; }
  .workspace.with-preview .preview-pane { position: fixed; inset: 0; z-index: 40; }
}
@media (max-width: 780px) {
  #appScreen { grid-template-columns: 1fr; }
  aside { position: fixed; top: 0; bottom: 0; left: 0; width: 275px; transform: translateX(-100%); }
  aside.open { transform: translateX(0); }
  #backdrop.open { display: block; }
  #menuBtn { display: inline-flex; }
  .starter-grid { grid-template-columns: 1fr; }
  .bubble { max-width: 92%; }
}
