* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif; background: #f6f7fb; color: #1f2330; }
a { color: inherit; text-decoration: none; }

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

.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 10px 20px;
  background: #fff;
  border-bottom: 1px solid #e6e8ef;
  box-shadow: 0 1px 2px rgba(0,0,0,.02);
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand .logo { font-size: 22px; color: #6366f1; }
.brand .title { font-weight: 700; font-size: 17px; }
.brand .badge { font-size: 11px; padding: 2px 8px; background: #eef2ff; color: #4f46e5; border-radius: 999px; }
.nav { display: flex; gap: 14px; }
.nav a { padding: 6px 10px; border-radius: 8px; font-size: 14px; color: #4b5563; }
.nav a.active { background: #f3f4f6; color: #111827; font-weight: 600; }
.nav a:hover { background: #f3f4f6; }
.user-box { margin-left: auto; display: flex; align-items: center; gap: 10px; font-size: 13px; color: #6b7280; position: relative; }
.user-box input {
  border: 1px solid #e5e7eb; border-radius: 8px;
  padding: 6px 10px; font-size: 13px; width: 130px; outline: none;
  background: #fff;
}
.user-box input:focus { border-color: #6366f1; }

/* ---------- user menu (popover) ---------- */
.user-trigger {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px; border-radius: 8px; cursor: pointer;
  border: 1px solid transparent; background: transparent;
  color: #1f2937; font-size: 13px; font-family: inherit;
  transition: background .15s, border-color .15s;
  max-width: 220px;
}
.user-trigger:hover { background: #f3f4f6; border-color: #e5e7eb; }
.user-trigger:focus { outline: none; background: #f3f4f6; border-color: #e5e7eb; }
.user-trigger .avatar {
  width: 26px; height: 26px; border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: #fff; font-weight: 600; font-size: 12px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.user-trigger .who { display: flex; flex-direction: column; align-items: flex-start; line-height: 1.2; min-width: 0; }
.user-trigger .who .name { font-weight: 600; font-size: 13px; color: #111827; max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-trigger .who .role { font-size: 11px; color: #6b7280; }
.user-trigger .caret { font-size: 10px; color: #6b7280; }

.user-popover {
  position: absolute; top: calc(100% + 8px); right: 0;
  width: 280px; background: #fff;
  border: 1px solid #e5e7eb; border-radius: 12px;
  box-shadow: 0 12px 40px rgba(15, 23, 42, .12);
  padding: 14px 16px; z-index: 100;
  display: none;
}
.user-popover.open { display: block; }
.user-popover .head {
  display: flex; align-items: center; gap: 10px; padding-bottom: 10px;
  border-bottom: 1px solid #f1f2f5;
}
.user-popover .avatar-lg {
  width: 38px; height: 38px; border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: #fff; font-weight: 700; font-size: 15px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.user-popover .head-info { min-width: 0; }
.user-popover .head-info .name { font-weight: 600; font-size: 14px; color: #111827; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-popover .head-info .sub { font-size: 12px; color: #6b7280; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-popover .row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 0; font-size: 12px;
}
.user-popover .row .k { color: #6b7280; }
.user-popover .row .v { color: #111827; font-weight: 500; max-width: 60%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-popover .quota-bar {
  height: 6px; background: #f3f4f6; border-radius: 999px; overflow: hidden;
  margin-top: 4px;
}
.user-popover .quota-bar .fill {
  height: 100%; background: linear-gradient(90deg, #6366f1, #8b5cf6);
  transition: width .3s;
}
.user-popover .quota-bar .fill.warn { background: linear-gradient(90deg, #f59e0b, #ef4444); }
.user-popover .actions {
  margin-top: 10px; padding-top: 10px; border-top: 1px solid #f1f2f5;
  display: flex; flex-direction: column; gap: 6px;
}
.user-popover .actions .btn { width: 100%; text-align: left; padding: 8px 10px; font-size: 13px; }
.user-popover .actions .btn.danger { color: #dc2626; }

.workspace { flex: 1; display: grid; grid-template-columns: 240px minmax(360px, 42%) 1fr; gap: 14px; padding: 14px; min-height: 0; }
.workspace.rail-collapsed { grid-template-columns: 36px minmax(360px, 42%) 1fr; }
.pane { background: #fff; border-radius: 14px; border: 1px solid #ecedef; display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.pane-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; border-bottom: 1px solid #f1f2f5;
}
.pane-head h2 { margin: 0; font-size: 14px; font-weight: 600; color: #374151; }
.actions { display: flex; gap: 8px; }

.btn {
  border: 1px solid #e5e7eb; background: #fff; color: #374151;
  padding: 6px 12px; border-radius: 8px; font-size: 13px; cursor: pointer;
  transition: all .15s;
}
.btn:hover { background: #f9fafb; }
.btn.primary { background: #6366f1; border-color: #6366f1; color: #fff; }
.btn.primary:hover { background: #5457e0; }
.btn.primary:disabled { background: #c7d2fe; border-color: #c7d2fe; cursor: not-allowed; }
.btn.danger { color: #dc2626; border-color: #fecaca; }
.btn.danger:hover { background: #fef2f2; }
.btn.ghost { border-color: transparent; }
.btn.ghost:hover { background: #f3f4f6; }

.messages { flex: 1; overflow-y: auto; padding: 14px 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { display: flex; flex-direction: column; gap: 4px; }
.msg .role { font-size: 11px; color: #9ca3af; padding: 0 2px; }
.msg .bubble {
  padding: 10px 12px; border-radius: 10px; font-size: 14px;
  line-height: 1.55; word-wrap: break-word; white-space: pre-wrap;
}
.msg.user .role { align-self: flex-end; }
.msg.user .bubble { background: #eef2ff; color: #1e1b4b; align-self: flex-end; max-width: 90%; }
.msg.assistant .bubble { background: #f8fafc; color: #111827; border: 1px solid #eef0f3; }
.msg.assistant .bubble code { background: #eef0f3; padding: 1px 5px; border-radius: 4px; font-size: 13px; }

.composer { border-top: 1px solid #f1f2f5; padding: 10px 12px; }
.composer textarea {
  width: 100%; resize: vertical; border: 1px solid #e5e7eb; border-radius: 10px;
  padding: 10px 12px; font-size: 14px; font-family: inherit; outline: none;
  min-height: 56px; max-height: 200px; background: #fafbfc;
}
.composer textarea:focus { border-color: #6366f1; background: #fff; }
.composer-foot { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.hint { font-size: 11px; color: #9ca3af; }

.preview-wrap { flex: 1; position: relative; background: #fff; }
#preview { width: 100%; height: 100%; border: none; background: #fff; }
.empty {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 8px;
  color: #9ca3af; font-size: 14px; pointer-events: none;
}
.empty .muted { font-size: 12px; color: #c5c8d0; }

.streaming-dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: #6366f1; margin-left: 6px; animation: pulse 1s infinite; vertical-align: middle; }
@keyframes pulse { 0%,100% { opacity: .3; } 50% { opacity: 1; } }

.space { padding: 20px; overflow-y: auto; }
.space-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.space-head h2 { margin: 0; font-size: 18px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.card {
  background: #fff; border: 1px solid #ecedef; border-radius: 12px;
  overflow: hidden; display: flex; flex-direction: column; transition: all .2s;
}
.card:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(0,0,0,.06); }
.card .thumb { height: 160px; cursor: pointer; position: relative; overflow: hidden; display: flex; align-items: center; justify-content: center; padding: 16px; }
.card .thumb iframe { display: none; } /* legacy, no longer used */
.card .cover {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 14px 18px;
  font-weight: 700; font-size: 18px;
  color: rgba(255,255,255,.96);
  text-shadow: 0 1px 3px rgba(0,0,0,.18);
  text-align: center;
  line-height: 1.35;
  word-break: break-word;
  overflow: hidden;
}
/* 8 preset gradients */
.cover.c0 { background: linear-gradient(135deg, #6366f1, #8b5cf6); }
.cover.c1 { background: linear-gradient(135deg, #06b6d4, #3b82f6); }
.cover.c2 { background: linear-gradient(135deg, #10b981, #06b6d4); }
.cover.c3 { background: linear-gradient(135deg, #f59e0b, #ef4444); }
.cover.c4 { background: linear-gradient(135deg, #ec4899, #f43f5e); }
.cover.c5 { background: linear-gradient(135deg, #8b5cf6, #ec4899); }
.cover.c6 { background: linear-gradient(135deg, #14b8a6, #84cc16); }
.cover.c7 { background: linear-gradient(135deg, #475569, #0f172a); }
.card .body { padding: 10px 12px; }
.card .title-row { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.card .title { font-size: 14px; font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .meta { font-size: 11px; color: #9ca3af; margin-top: 4px; }
.card .ops { display: flex; gap: 6px; margin-top: 8px; }
.card .ops .btn { padding: 4px 8px; font-size: 12px; }
.empty-center { text-align: center; padding: 60px 20px; color: #9ca3af; }
.empty-center p { margin: 0 0 14px; }

.dlg { width: min(1100px, 92vw); height: min(800px, 88vh); padding: 0; border: none; border-radius: 14px; overflow: hidden; background: #fff; box-shadow: 0 20px 60px rgba(0,0,0,.2); }
.dlg::backdrop { background: rgba(15, 18, 30, .45); }
.dlg-bar { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid #f1f2f5; }
.dlg-title-input { flex: 1; border: none; outline: none; font-size: 16px; font-weight: 600; padding: 4px 6px; border-radius: 6px; }
.dlg-title-input:focus { background: #f9fafb; }
.dlg-actions { display: flex; gap: 6px; }
.dlg-frame { width: 100%; height: calc(100% - 52px); border: none; background: #fff; }

@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; grid-template-rows: auto 50% 50%; }
  .workspace.rail-collapsed { grid-template-columns: 1fr; }
  .nav { display: none; }
}

/* ---------- conversation rail (left sidebar) ---------- */
.rail {
  background: #fff; border: 1px solid #ecedef; border-radius: 14px;
  display: flex; flex-direction: column; min-height: 0; overflow: hidden;
}
.rail-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 12px; border-bottom: 1px solid #f1f2f5;
}
.rail-head h2 { margin: 0; font-size: 13px; font-weight: 600; color: #374151; }
.icon-only { padding: 4px 8px; font-size: 12px; }
.rail-actions { padding: 10px; }
.rail-actions .btn.full { width: 100%; }
.btn.full { width: 100%; }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn .plus { font-weight: 700; margin-right: 4px; }

.session-list {
  flex: 1; overflow-y: auto; padding: 4px 8px 8px;
}
.session-item {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; margin-bottom: 4px; border-radius: 8px;
  cursor: pointer; transition: background .12s;
  border: 1px solid transparent;
  position: relative;
}
.session-item:hover { background: #f3f4f6; }
.session-item.active { background: #eef2ff; border-color: #c7d2fe; }
.session-item .si-title {
  flex: 1; font-size: 13px; color: #1f2937;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.session-item.active .si-title { font-weight: 600; color: #1e1b4b; }
.session-item .si-meta {
  font-size: 10px; color: #9ca3af; flex-shrink: 0;
}
.session-item .si-del {
  display: none; background: transparent; border: none; cursor: pointer;
  color: #9ca3af; padding: 2px 4px; border-radius: 4px; font-size: 14px;
}
.session-item:hover .si-del { display: inline-block; }
.session-item .si-del:hover { background: #fee2e2; color: #dc2626; }

.session-empty {
  text-align: center; padding: 30px 14px; color: #9ca3af; font-size: 12px; line-height: 1.6;
}

.workspace.rail-collapsed .rail-head,
.workspace.rail-collapsed .rail-actions,
.workspace.rail-collapsed .session-list { display: none; }
.workspace.rail-collapsed .rail { padding: 6px 4px; }
.workspace.rail-collapsed .rail-foot { display: flex; }
.rail-foot { display: none; padding: 8px; }

/* ---------- save dialog (creator page) ---------- */
.dlg.dlg-lg { width: min(720px, 94vw); max-height: 92vh; }

.saveDlg-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 22px 26px 16px;
  background: linear-gradient(135deg, #eef2ff, #faf5ff);
  border-bottom: 1px solid #eceef5;
}
.saveDlg-head-text h2 {
  margin: 0 0 4px; font-size: 18px; font-weight: 700; color: #111827;
  letter-spacing: -0.01em;
}
.saveDlg-head-text p {
  margin: 0; font-size: 13px; color: #6b7280; line-height: 1.5;
}
.saveDlg-close {
  background: transparent; border: none; cursor: pointer;
  font-size: 28px; line-height: 1; color: #6b7280;
  width: 32px; height: 32px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s;
}
.saveDlg-close:hover { background: rgba(17,24,39,0.06); color: #111827; }

.saveDlg-body {
  display: grid; grid-template-columns: 1.1fr 1fr; gap: 24px;
  padding: 22px 26px;
}
@media (max-width: 640px) {
  .saveDlg-body { grid-template-columns: 1fr; }
}

.saveDlg-form { display: flex; flex-direction: column; gap: 16px; }
.saveDlg-form .field { display: flex; flex-direction: column; gap: 6px; }
.saveDlg-form .checkbox-line {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; font-weight: 500; color: #4b5563;
  cursor: pointer; padding: 8px 10px;
  background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 8px;
}
.saveDlg-form .checkbox-line input[type="checkbox"] {
  width: 16px; height: 16px; cursor: pointer; accent-color: #00a884;
}
.saveDlg-form label {
  font-size: 13px; font-weight: 600; color: #374151;
}
.saveDlg-form label .optional {
  font-weight: 400; color: #9ca3af; font-size: 11px; margin-left: 6px;
}
.saveDlg-form input {
  width: 100%; padding: 10px 12px;
  border: 1px solid #e5e7eb; border-radius: 8px;
  font-size: 14px; font-family: inherit;
  background: #fff; color: #111827;
  transition: border-color .15s, box-shadow .15s;
}
.saveDlg-form input:focus {
  outline: none; border-color: #6366f1;
  box-shadow: 0 0 0 3px rgba(99,102,241,.15);
}

.palette-row { display: flex; gap: 8px; flex-wrap: wrap; }
.palette-swatch {
  width: 36px; height: 36px; border-radius: 10px; cursor: pointer;
  border: 2px solid transparent; padding: 0;
  transition: transform .15s, box-shadow .15s, border-color .15s;
}
.palette-swatch:hover { transform: translateY(-2px); }
.palette-swatch.active {
  border-color: #fff;
  box-shadow: 0 0 0 2px #6366f1, 0 6px 14px rgba(15, 23, 42, .15);
}
.palette-hint {
  margin: 6px 0 0; font-size: 11px; color: #9ca3af;
}
.palette-hint span { color: #6366f1; font-weight: 600; }

.saveDlg-preview-col {
  display: flex; flex-direction: column; align-items: stretch; gap: 8px;
}
.preview-label {
  margin: 0; font-size: 12px; font-weight: 600;
  color: #6b7280; text-transform: uppercase; letter-spacing: 0.5px;
}
.palette-preview-wrap {
  width: 100%; aspect-ratio: 4 / 3;
  border-radius: 14px; overflow: hidden;
  box-shadow: 0 8px 24px rgba(15, 23, 42, .10);
  border: 1px solid rgba(15, 23, 42, .04);
  position: relative;
}
.palette-preview-wrap .cover {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  font-size: 22px; font-weight: 700;
  color: rgba(255,255,255,.96);
  text-shadow: 0 1px 4px rgba(0,0,0,.18);
  text-align: center; line-height: 1.35;
  word-break: break-word;
}
.preview-meta {
  margin: 0; font-size: 12px; color: #9ca3af; text-align: center;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.saveDlg-foot {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 14px 26px 18px;
  border-top: 1px solid #f1f2f5;
  background: #fafbfc;
}
.saveDlg-foot .btn { min-width: 88px; padding: 9px 16px; font-size: 14px; font-weight: 500; }

.saveDlg-form .msg {
  margin: 4px 0 0; min-height: 16px;
  font-size: 12px; color: #b91c1c;
}
.saveDlg-form .msg.ok { color: #047857; }

/* ---------- modal dialogs (modAlert / modConfirm / modPrompt) ---------- */
.mod-dlg {
  width: min(440px, 92vw);
  padding: 0;
  border: none;
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.18);
  overflow: hidden;
  color: #111827;
}
.mod-dlg::backdrop {
  background: rgba(15, 18, 30, 0.45);
  animation: modFadeIn 0.16s ease-out;
}
.mod-dlg[open] { animation: modPop 0.18s cubic-bezier(0.2, 0.9, 0.3, 1.2); }
@keyframes modFadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes modPop {
  from { opacity: 0; transform: translateY(8px) scale(0.96); }
  to   { opacity: 1; transform: none; }
}
.mod-head { display: flex; gap: 14px; padding: 22px 22px 14px; align-items: flex-start; }
.mod-icon { flex: 0 0 auto; width: 36px; height: 36px; display: inline-flex; align-items: center; justify-content: center; border-radius: 10px; background: #eef1ff; }
.mod-text { flex: 1; min-width: 0; min-height: 36px; display: flex; flex-direction: column; justify-content: center; }
.mod-title { font-size: 15px; font-weight: 600; color: #111827; line-height: 1.35; }
.mod-body { font-size: 14px; color: #4b5563; line-height: 1.55; margin-top: 2px; word-break: break-word; }
.mod-text > .mod-title + .mod-body { margin-top: 6px; }
.mod-input-wrap { padding: 0 22px 14px; }
.mod-input { width: 100%; box-sizing: border-box; padding: 9px 12px; border: 1px solid #e5e7eb; border-radius: 8px; font-size: 14px; outline: none; background: #fff; transition: border-color 0.12s, box-shadow 0.12s; }
.mod-input:focus { border-color: #6366f1; box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.18); }
.mod-input.danger { border-color: #ef4444; box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.15); }
.mod-input-area { min-height: 80px; resize: vertical; font-family: inherit; }
.mod-actions { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px 18px; }
.mod-actions .btn { min-width: 84px; padding: 9px 16px; font-size: 14px; font-weight: 500; border-radius: 8px; }

/* ---------- toast ---------- */
.toast-root {
  position: fixed; right: 20px; bottom: 24px; z-index: 10000;
  display: flex; flex-direction: column-reverse; gap: 8px; pointer-events: none;
}
.toast {
  pointer-events: auto;
  display: inline-flex; align-items: center; gap: 10px;
  background: #111827; color: #fff; padding: 10px 14px 10px 12px;
  border-radius: 999px; font-size: 13px; line-height: 1.3;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.22);
  opacity: 0; transform: translateY(8px); transition: opacity 0.18s, transform 0.18s;
  max-width: min(360px, 80vw);
}
.toast.show { opacity: 1; transform: none; }
.toast-icon {
  width: 18px; height: 18px; flex: 0 0 auto; border-radius: 999px;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 11px; font-weight: 700;
  background: #6366f1; color: #fff;
}
.toast-success .toast-icon { background: #10b981; }
.toast-error   .toast-icon { background: #ef4444; }
.toast-warn    .toast-icon { background: #f59e0b; color: #fff; }
.toast-text { word-break: break-word; }
