:root {
  color-scheme: dark;
  --bg: #0c0f17;
  --panel: #131824;
  --panel-2: #192030;
  --border: rgba(255, 255, 255, 0.1);
  --text: #eef2ff;
  --muted: #9aa6c4;
  --accent: #4f7cff;
  --accent-hover: #3e6bef;
  --danger: #e5484d;
  --success: #3dd68c;
  --radius: 10px;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
a { color: #8ab4ff; }

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.9em;
  background: rgba(255, 255, 255, 0.08);
  padding: 2px 5px;
  border-radius: 5px;
}

/* Buttons & Inputs */
button {
  font: inherit;
  cursor: pointer;
  border: 0;
  border-radius: 8px;
  transition: background 0.15s, opacity 0.15s;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  padding: 8px 14px;
  font-weight: 600;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-ghost {
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  padding: 8px 12px;
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: rgba(255, 255, 255, 0.12); }

.btn-danger {
  background: rgba(229, 72, 77, 0.18);
  color: #ff999c;
  padding: 8px 12px;
  border: 1px solid rgba(229, 72, 77, 0.35);
}
.btn-danger:hover { background: rgba(229, 72, 77, 0.3); }

.sm { padding: 6px 10px; font-size: 12.5px; }
.xs { padding: 4px 8px; font-size: 12px; }
.full { width: 100%; }

input[type="text"],
input[type="password"],
input[type="search"],
textarea,
.select-input {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
  outline: none;
}

input:focus, textarea:focus, .select-input:focus {
  border-color: var(--accent);
}

/* Auth Screen */
.auth-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(circle at top, #18233c, var(--bg));
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 26px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}
.brand-row h1 { margin: 0; font-size: 1.25rem; }
.brand-row p { margin: 2px 0 0; font-size: 12.5px; }

.brand-logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, #4f7cff, #2dd4bf);
  color: #09101d;
  font-weight: 800;
  display: grid;
  place-items: center;
}
.brand-logo.sm { width: 28px; height: 28px; font-size: 12px; border-radius: 7px; }

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  background: var(--bg);
  padding: 4px;
  border-radius: 10px;
  margin-bottom: 16px;
}

.tab-btn {
  padding: 7px;
  background: transparent;
  color: var(--muted);
  font-weight: 600;
}
.tab-btn.active {
  background: var(--panel-2);
  color: var(--text);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.auth-form label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 12.5px;
  color: var(--muted);
}

.auth-note {
  margin: 14px 0 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.45;
}

.error-box {
  background: rgba(229, 72, 77, 0.16);
  border: 1px solid rgba(229, 72, 77, 0.4);
  color: #ffb3b5;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 12.5px;
}

.status-box {
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 12.5px;
  background: rgba(61, 214, 140, 0.14);
  border: 1px solid rgba(61, 214, 140, 0.35);
  color: #9bf5c7;
}
.status-box.err {
  background: rgba(229, 72, 77, 0.16);
  border-color: rgba(229, 72, 77, 0.4);
  color: #ffb3b5;
}

/* Main App Shell */
.app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 265px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.brand-mini {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
}

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 6px;
}
.section-header .section-title { margin-bottom: 0; }

.search-input {
  width: 100%;
  margin-bottom: 8px;
  padding: 6px 9px !important;
  font-size: 12.5px !important;
}

.nav-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.nav-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 7px 9px;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  border: 1px solid transparent;
}
.nav-item:hover { background: rgba(255, 255, 255, 0.05); }
.nav-item.active {
  background: rgba(79, 124, 255, 0.16);
  border-color: rgba(79, 124, 255, 0.35);
}

.nav-item-main {
  min-width: 0;
  flex: 1;
}
.nav-item-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
}
.nav-item-sub {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.nav-del-btn {
  background: transparent;
  color: var(--muted);
  padding: 2px 6px;
  font-size: 12px;
  opacity: 0.7;
}
.nav-del-btn:hover { color: #ff8e92; opacity: 1; }

.sidebar-footer {
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.user-pill {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
}
.username-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
  font-size: 13px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
  flex-shrink: 0;
}
.status-dot.ok { background: var(--success); }

.footer-actions { display: flex; gap: 6px; }

/* Workspace */
.workspace {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: 50px;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.chat-title-text {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.layout-switch {
  display: flex;
  background: var(--bg);
  padding: 3px;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.layout-btn {
  background: transparent;
  color: var(--muted);
  padding: 4px 10px;
  font-size: 12px;
}
.layout-btn.active {
  background: var(--panel-2);
  color: var(--text);
}

.api-banner {
  background: rgba(79, 124, 255, 0.14);
  border-bottom: 1px solid rgba(79, 124, 255, 0.3);
  padding: 8px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 13px;
}

.split-body {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 1.15fr;
}
.split-body.layout-chat { grid-template-columns: 1fr; }
.split-body.layout-chat .editor-pane { display: none; }
.split-body.layout-editor { grid-template-columns: 1fr; }
.split-body.layout-editor .chat-pane { display: none; }

/* Chat Pane */
.chat-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-right: 1px solid var(--border);
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.empty-chat {
  margin: auto;
  max-width: 460px;
  text-align: center;
  color: var(--muted);
}
.empty-chat h2 { color: var(--text); margin-bottom: 8px; font-size: 1.2rem; }
.quick-prompts {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 14px;
}
.quick-chip {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 7px 11px;
  font-size: 12.5px;
}
.quick-chip:hover { border-color: var(--accent); }

.msg-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 100%;
}
.msg-row.user { align-items: flex-end; }

.msg-bubble {
  padding: 10px 14px;
  border-radius: 12px;
  line-height: 1.55;
  max-width: 92%;
  word-wrap: break-word;
}
.msg-row.user .msg-bubble {
  background: #233357;
  border: 1px solid rgba(79, 124, 255, 0.35);
}
.msg-row.assistant .msg-bubble {
  background: var(--panel);
  border: 1px solid var(--border);
  width: 100%;
  max-width: 100%;
}

.msg-actions-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.reasoning-box {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 10px;
  font-size: 12.5px;
  color: var(--muted);
}
.reasoning-box summary {
  cursor: pointer;
  font-weight: 600;
  color: #b8c4e0;
}
.reasoning-box pre {
  white-space: pre-wrap;
  margin: 8px 0 0;
  font-family: inherit;
}

.code-block-card {
  margin: 10px 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: #0a0d14;
}
.code-block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  background: #171d2b;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
}
.code-block-actions { display: flex; gap: 6px; }
.code-block-card pre {
  margin: 0;
  padding: 10px;
  overflow-x: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.45;
}

.chat-composer {
  padding: 10px 14px 12px;
  border-top: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.composer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--muted);
}
.attach-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}
.composer-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.composer-row textarea {
  flex: 1;
  resize: vertical;
  min-height: 44px;
  max-height: 160px;
}

/* Editor & Preview Pane */
.editor-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
}

.editor-toolbar {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.snippet-meta {
  display: flex;
  gap: 8px;
  flex: 1;
  min-width: 200px;
}
.title-input { flex: 1.3; min-width: 120px; }
.tags-input { flex: 0.9; min-width: 100px; }

.editor-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.dirty-badge {
  font-size: 11px;
  padding: 3px 7px;
  border-radius: 999px;
  background: rgba(245, 158, 11, 0.2);
  color: #fcd34d;
}

.editor-subbar {
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.code-tabs, .preview-modes {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.code-tab, .mode-btn {
  background: transparent;
  color: var(--muted);
  padding: 4px 10px;
  font-size: 12px;
  border: 1px solid transparent;
}
.code-tab.active, .mode-btn.active {
  background: rgba(79, 124, 255, 0.18);
  color: var(--text);
  border-color: rgba(79, 124, 255, 0.4);
}

.studio-body {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-rows: 1fr 1fr;
}
.studio-body.mode-code { grid-template-rows: 1fr; }
.studio-body.mode-code .preview-wrap { display: none; }
.studio-body.mode-preview { grid-template-rows: 1fr; }
.studio-body.mode-preview .code-area-wrap { display: none; }

.code-area-wrap {
  display: flex;
  min-height: 0;
  border-bottom: 1px solid var(--border);
  background: #0a0d14;
}

.line-numbers {
  margin: 0;
  padding: 12px 8px 12px 10px;
  min-width: 42px;
  text-align: right;
  color: #5d6a89;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  user-select: none;
  overflow: hidden;
  border-right: 1px solid rgba(255, 255, 255, 0.06);
}

.code-editor {
  flex: 1;
  border: 0 !important;
  border-radius: 0 !important;
  background: transparent !important;
  padding: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  resize: none;
  tab-size: 2;
  white-space: pre;
  overflow: auto;
}

.preview-wrap {
  position: relative;
  min-height: 0;
  background: #fff;
  display: flex;
  flex-direction: column;
}
.preview-frame {
  width: 100%;
  flex: 1;
  border: 0;
  background: #fff;
}
.preview-error {
  background: #3a1216;
  color: #ffb3b5;
  padding: 6px 10px;
  font-size: 12px;
  border-bottom: 1px solid #7a262c;
}

/* Modal & Toast */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 50;
}
.modal-card {
  width: 100%;
  max-width: 460px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.modal-header h3 { margin: 0; }
.settings-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.settings-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12.5px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.toast {
  position: fixed;
  bottom: 18px;
  right: 18px;
  background: #1d2638;
  color: var(--text);
  border: 1px solid var(--accent);
  padding: 9px 14px;
  border-radius: 9px;
  font-size: 13px;
  z-index: 60;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.45);
}

.mobile-only { display: none; }

@media (max-width: 960px) {
  .mobile-only { display: inline-block; }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.2s;
  }
  .sidebar.open { transform: translateX(0); }
  .split-body { grid-template-columns: 1fr; }
}
