:root {
  --bg: #0f0f0f;
  --bg2: #141414;
  --bg3: #1a1a1a;
  --glass: rgba(255, 255, 255, 0.045);
  --glass2: rgba(255, 255, 255, 0.08);
  --border: rgba(255, 255, 255, 0.09);
  --border2: rgba(255, 255, 255, 0.14);
  --text: #e8e8e8;
  --muted: #9a9a9a;
  --dim: #6b6b6b;
  --cyan: #22d3ee;
  --yellow: #fbbf24;
  --purple: #a78bfa;
  --green: #34d399;
  --red: #f87171;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --radius: 12px;
  --topbar-h: 54px;
  --sidebar-w: 252px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

input, select, textarea {
  font-family: inherit;
  font-size: 13px;
  color: var(--text);
}

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

/* ---------- Topbar ---------- */
.topbar {
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 14px;
  background: rgba(18, 18, 18, 0.85);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
  z-index: 40;
}

.icon-btn {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
  flex: 0 0 auto;
}
.icon-btn:hover { background: var(--glass2); color: var(--text); }

.brand { display: flex; align-items: center; gap: 9px; min-width: 0; margin-right: auto; }
.bolt {
  width: 30px; height: 30px;
  flex: 0 0 auto;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 9px;
  color: #071019;
  background: linear-gradient(135deg, var(--cyan), var(--purple));
  box-shadow: 0 0 18px rgba(34, 211, 238, 0.45);
}
.bolt.big { width: 64px; height: 64px; border-radius: 18px; margin: 0 auto; box-shadow: 0 0 34px rgba(34, 211, 238, 0.5); }
.brand-name { font-weight: 700; letter-spacing: 0.2px; white-space: nowrap; }
.brand-sub { color: var(--muted); font-size: 11px; margin-top: 2px; white-space: nowrap; }

.top-actions { display: flex; align-items: center; gap: 8px; }

.route-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 11px;
  border-radius: 999px;
  font-size: 11px;
  color: var(--cyan);
  background: rgba(34, 211, 238, 0.1);
  border: 1px solid rgba(34, 211, 238, 0.28);
  white-space: nowrap;
}
.route-pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--cyan); box-shadow: 0 0 8px var(--cyan); }
.route-pill.busy .dot { animation: pulse 0.9s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.35; transform: scale(0.7); } }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 38px;
  padding: 0 16px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  transition: transform 0.12s, box-shadow 0.12s, background 0.15s;
  white-space: nowrap;
}
.btn:active { transform: scale(0.97); }
.btn-primary {
  color: #06141c;
  background: linear-gradient(135deg, var(--cyan), #38bdf8);
  box-shadow: 0 4px 18px rgba(34, 211, 238, 0.35);
}
.btn-primary:hover { box-shadow: 0 6px 24px rgba(34, 211, 238, 0.5); }
.btn-primary:disabled { opacity: 0.55; cursor: not-allowed; box-shadow: none; }
.btn-ghost { color: var(--text); background: var(--glass); border: 1px solid var(--border2); }
.btn-ghost:hover { background: var(--glass2); }
.btn-danger { color: var(--red); background: rgba(248, 113, 113, 0.1); border: 1px solid rgba(248, 113, 113, 0.3); }
.btn-danger:hover { background: rgba(248, 113, 113, 0.18); }

/* ---------- Sidebar ---------- */
.sidebar {
  position: fixed;
  top: var(--topbar-h);
  bottom: 0;
  left: 0;
  width: var(--sidebar-w);
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 14px 12px;
  background: rgba(16, 16, 16, 0.82);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-right: 1px solid var(--border);
  z-index: 35;
  transform: translateX(0);
  transition: transform 0.22s ease;
  overflow-y: auto;
}
.sidebar.closed { transform: translateX(-100%); }

.side-sec { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.side-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--dim);
  padding: 6px 8px;
}
.file-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.file-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 10px;
  border-radius: 9px;
  cursor: pointer;
  font-size: 13px;
  color: var(--muted);
  transition: background 0.13s, color 0.13s;
  min-height: 38px;
}
.file-item:hover { background: var(--glass); color: var(--text); }
.file-item.active {
  background: rgba(167, 139, 250, 0.14);
  color: var(--purple);
  box-shadow: inset 2px 0 0 var(--purple);
}
.file-item .fi {
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 5px;
  background: var(--glass2);
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.file-item .del { margin-left: auto; color: var(--dim); padding: 2px 6px; border-radius: 6px; display: none; }
.file-item:hover .del { display: inline-block; }
.file-item .del:hover { color: var(--red); background: rgba(248, 113, 113, 0.12); }

.side-btn {
  display: flex;
  align-items: center;
  min-height: 40px;
  padding: 0 12px;
  border-radius: 9px;
  color: var(--muted);
  background: var(--glass);
  border: 1px solid var(--border);
  font-size: 13px;
  text-align: left;
  transition: background 0.13s, color 0.13s;
}
.side-btn:hover { background: var(--glass2); color: var(--text); }
.side-btn.danger { color: var(--red); }
.side-btn.danger:hover { background: rgba(248, 113, 113, 0.1); }

.side-foot {
  margin-top: auto;
  padding: 12px 8px 4px;
  font-size: 11px;
  line-height: 1.6;
  color: var(--dim);
  border-top: 1px solid var(--border);
}

.scrim {
  position: fixed;
  inset: var(--topbar-h) 0 0 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
  z-index: 30;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.scrim.show { opacity: 1; pointer-events: auto; }

/* ---------- Main layout ---------- */
.main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin-left: var(--sidebar-w);
  padding-top: 0;
  position: relative;
}

.view-tabs {
  display: none;
  flex: 0 0 auto;
  padding: 8px 10px 0;
  gap: 6px;
}
.vtab {
  flex: 1;
  min-height: 42px;
  border-radius: 10px 10px 0 0;
  background: var(--glass);
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  border: 1px solid var(--border);
  border-bottom: none;
}
.vtab.active { color: var(--cyan); background: rgba(34, 211, 238, 0.1); }

.pane {
  display: none;
  min-height: 0;
  flex-direction: column;
  background: var(--bg2);
}
.pane.active { display: flex; }

.pane-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: rgba(20, 20, 20, 0.85);
  backdrop-filter: blur(10px);
  min-height: 46px;
}
.pane-title { font-weight: 600; font-size: 13px; color: var(--text); }
.pane-tools { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  padding: 6px 12px;
  border-radius: 8px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.13s, color 0.13s;
}
.tab:hover { background: var(--glass2); color: var(--text); }
.tab.active { background: var(--purple); color: #fff; box-shadow: 0 2px 12px rgba(167, 139, 250, 0.35); }

.save-pill {
  font-size: 11px;
  color: var(--green);
  background: rgba(52, 211, 153, 0.1);
  border: 1px solid rgba(52, 211, 153, 0.28);
  padding: 4px 9px;
  border-radius: 999px;
  white-space: nowrap;
}
.save-pill.saving { color: var(--yellow); background: rgba(251, 191, 36, 0.1); border-color: rgba(251, 191, 36, 0.3); }

.mini-btn {
  min-height: 32px;
  padding: 0 11px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  background: var(--glass);
  border: 1px solid var(--border);
  transition: background 0.13s, color 0.13s;
}
.mini-btn:hover { background: var(--glass2); color: var(--text); }

.mini-select {
  min-height: 32px;
  padding: 0 8px;
  border-radius: 8px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  color: var(--text);
  outline: none;
  max-width: 170px;
}

/* ---------- Editor ---------- */
.editor-pane { flex: 1; }

.code-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  background: #0d0d0f;
  overflow: hidden;
}
.hl, .code {
  position: absolute;
  inset: 0;
  margin: 0;
  padding: 16px 18px;
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.6;
  tab-size: 2;
  border: 0;
  white-space: pre;
  overflow: auto;
  background: transparent;
}
.hl { pointer-events: none; color: #d7d7d7; z-index: 1; }
.code {
  z-index: 2;
  color: transparent;
  caret-color: #fff;
  resize: none;
  outline: none;
  -webkit-text-fill-color: transparent;
  text-shadow: none;
}
.code::selection { background: rgba(34, 211, 238, 0.25); -webkit-text-fill-color: transparent; }

.tok-com { color: #6b7280; font-style: italic; }
.tok-str { color: #7ee787; }
.tok-kw { color: #ff7b72; }
.tok-num { color: #79c0ff; }
.tok-fn { color: #d2a8ff; }
.tok-tag { color: #ff7b72; }
.tok-attr { color: #79c0ff; }
.tok-sel { color: #ffa657; }
.tok-prop { color: #79c0ff; }
.tok-brace { color: #8b949e; }

.editor-foot {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 5px 14px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--dim);
  border-top: 1px solid var(--border);
  background: var(--bg3);
}
.editor-foot span:last-child { margin-left: auto; }

/* ---------- Preview ---------- */
.preview-pane { flex: 1; }
#preview {
  flex: 1;
  min-height: 0;
  width: 100%;
  border: 0;
  background: #fff;
}

.err-overlay {
  position: absolute;
  inset: 46px 12px 12px;
  z-index: 5;
  background: rgba(15, 15, 15, 0.92);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(248, 113, 113, 0.4);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: var(--shadow);
}
.preview-pane { position: relative; }
.err-overlay.hidden { display: none; }
.err-head { color: var(--red); font-weight: 700; font-size: 14px; }
.err-msg {
  font-family: var(--mono);
  font-size: 12px;
  color: #ffd2d2;
  background: rgba(248, 113, 113, 0.08);
  border: 1px solid rgba(248, 113, 113, 0.25);
  padding: 10px;
  border-radius: 8px;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 130px;
  overflow: auto;
}
.err-actions { display: flex; gap: 10px; align-items: center; margin-top: 4px; }

/* ---------- Chat ---------- */
.chat-pane { flex: 1; }
.chat-log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.msg {
  max-width: 86%;
  padding: 10px 13px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  animation: rise 0.18s ease;
}
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.msg.user {
  align-self: flex-end;
  background: linear-gradient(135deg, rgba(34, 211, 238, 0.22), rgba(56, 189, 248, 0.22));
  border: 1px solid rgba(34, 211, 238, 0.3);
  color: #eafcff;
}
.msg.ai {
  align-self: flex-start;
  background: var(--glass);
  border: 1px solid var(--border2);
  color: var(--text);
}
.msg.ai .msg-meta { display: block; font-size: 10px; color: var(--dim); margin-top: 7px; font-family: var(--mono); }
.msg.sys {
  align-self: center;
  background: transparent;
  border: 1px dashed var(--border2);
  color: var(--muted);
  font-size: 12px;
  padding: 7px 12px;
}
.msg.error { align-self: flex-start; background: rgba(248, 113, 113, 0.1); border: 1px solid rgba(248, 113, 113, 0.3); color: #ffd2d2; }

.thinking {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 11px 15px;
  border-radius: 12px;
  background: var(--glass);
  border: 1px solid var(--border2);
  color: var(--muted);
  font-size: 12px;
}
.thinking .spinner {
  width: 14px; height: 14px;
  border: 2px solid var(--glass2);
  border-top-color: var(--cyan);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.chat-compose {
  flex: 0 0 auto;
  display: flex;
  gap: 10px;
  padding: 12px;
  border-top: 1px solid var(--border);
  background: rgba(18, 18, 18, 0.9);
  backdrop-filter: blur(10px);
}
#chatInput {
  flex: 1;
  min-height: 44px;
  max-height: 140px;
  resize: vertical;
  padding: 11px 13px;
  border-radius: 11px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  outline: none;
  line-height: 1.5;
}
#chatInput:focus { border-color: var(--cyan); box-shadow: 0 0 0 3px rgba(34, 211, 238, 0.15); }

/* ---------- Modals ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(6, 6, 6, 0.6);
  backdrop-filter: blur(6px);
}
.modal.hidden { display: none; }

.modal-card {
  width: 620px;
  max-width: 100%;
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  background: rgba(20, 20, 22, 0.92);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border2);
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.modal-card.small { width: 460px; }

.modal-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-head h2 { margin: 0; font-size: 17px; }

.modal-body { flex: 1; overflow-y: auto; padding: 16px 18px; display: flex; flex-direction: column; gap: 14px; }
.modal-foot {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.2);
}
.hub-note { font-size: 11px; color: var(--dim); }

.provider-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--glass);
  overflow: hidden;
}
.provider-card .pc-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  background: rgba(255, 255, 255, 0.02);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.provider-card .pc-name { font-weight: 700; font-size: 14px; }
.provider-card .pc-link {
  font-size: 11px;
  color: var(--cyan);
  text-decoration: none;
  padding: 4px 9px;
  border-radius: 7px;
  background: rgba(34, 211, 238, 0.08);
  border: 1px solid rgba(34, 211, 238, 0.25);
}
.provider-card .pc-link:hover { background: rgba(34, 211, 238, 0.16); }
.pc-status {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--dim);
}
.pc-status.ok { color: var(--green); }
.pc-status.bad { color: var(--red); }
.pc-status .dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

.pc-body { display: flex; flex-direction: column; gap: 10px; padding: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.6px; }
.field input {
  min-height: 40px;
  padding: 0 12px;
  border-radius: 9px;
  background: var(--bg3);
  border: 1px solid var(--border2);
  outline: none;
  font-family: var(--mono);
}
.field input:focus { border-color: var(--purple); box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.15); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.pc-actions { display: flex; gap: 8px; align-items: center; }
.pc-actions .mini-btn { flex: 1; min-height: 36px; }

/* ---------- About ---------- */
.about-hero { text-align: center; display: flex; flex-direction: column; gap: 8px; padding: 10px 0; }
.about-hero h3 { margin: 0; font-size: 18px; }
.about-hero .muted { margin: 0; font-size: 12px; }
.about-list { margin: 6px 0 0; padding: 0 0 0 18px; display: flex; flex-direction: column; gap: 8px; font-size: 13px; color: var(--muted); line-height: 1.5; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  z-index: 80;
  padding: 11px 18px;
  border-radius: 10px;
  background: rgba(25, 25, 28, 0.96);
  border: 1px solid var(--border2);
  box-shadow: var(--shadow);
  color: var(--text);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  max-width: 90vw;
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { border-color: rgba(248, 113, 113, 0.5); color: #ffd2d2; }

/* ---------- Desktop layout ---------- */
@media (min-width: 1024px) {
  .main { margin-left: var(--sidebar-w); }
  .view-tabs { display: none; }

  .editor-pane, .preview-pane { display: flex; }
  .view-code, .view-preview { display: flex; }

  .chat-pane {
    position: fixed;
    right: 16px;
    bottom: 16px;
    width: 390px;
    height: 540px;
    z-index: 25;
    display: flex;
    border: 1px solid var(--border2);
    border-radius: 14px;
    overflow: hidden;
    background: rgba(16, 16, 18, 0.92);
    backdrop-filter: blur(18px);
    box-shadow: var(--shadow);
  }
  .chat-pane.closed { display: none; }
  #chatForm { border-radius: 0 0 14px 14px; }
}

/* ---------- Mobile layout ---------- */
@media (max-width: 1023px) {
  .main { margin-left: 0; }
  .view-tabs { display: flex; }
  .sidebar { transform: translateX(-100%); box-shadow: var(--shadow); }
  .sidebar.open { transform: translateX(0); }
  .brand-sub { display: none; }
  .route-pill { display: none; }
  .btn-ghost#exportBtn span { display: none; }
  .field-row { grid-template-columns: 1fr; }
  #newBtn { display: none; }
}
