:root {
  color-scheme: dark;
  --bg: #212121;
  --panel: #191919;
  --surface: #2f2f2f;
  --surface-hover: #383838;
  --line: #353535;
  --text: #ececec;
  --muted: #a1a1aa;
  --accent: #10a37f;
  --danger: #ef4444;
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { width: 100%; height: 100%; margin: 0; overflow: hidden; background: var(--bg); color: var(--text); }
button, textarea, input, select { font: inherit; }
button { color: inherit; }

.access-gate {
  display: grid;
  width: 100%;
  height: 100dvh;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 50% 35%, rgb(44 44 44 / 85%), transparent 42%),
    #171717;
}
.access-card {
  width: min(100%, 380px);
  padding: 30px;
  border: 1px solid #353535;
  border-radius: 18px;
  background: #202020;
  box-shadow: 0 24px 70px rgb(0 0 0 / 38%);
  text-align: center;
}
.access-card > img { width: 66px; height: 66px; border-radius: 50%; object-fit: cover; background: #fff; }
.access-card h1 { margin: 14px 0 22px; font-size: 23px; }
.access-state { display: flex; align-items: center; justify-content: center; gap: 9px; min-height: 24px; font-size: 14px; }
.access-state .spinner { color: var(--accent); }
.access-card p { margin: 10px auto 22px; color: var(--muted); font-size: 13px; line-height: 1.55; }
.access-login {
  height: 38px;
  padding: 0 17px;
  border: 1px solid #4b4b4b;
  border-radius: 9px;
  background: #2c2c2c;
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  font-weight: 650;
}
.access-login:hover { border-color: #626262; background: #353535; }

.shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(360px, 420px);
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  transition: grid-template-columns 180ms ease;
}

.shell.chat-collapsed { grid-template-columns: minmax(0, 1fr) 52px; }
.editor-panel { display: flex; flex-direction: column; width: 100%; height: 100%; min-width: 0; min-height: 0; overflow: hidden; }
.editor-frame-wrap { flex: 1 1 auto; width: 100%; height: 100%; min-width: 0; min-height: 0; position: relative; background: #fff; overflow: hidden; }
#editorFrame { display: block; width: 100%; height: 100%; border: 0; background: #fff; }
.editor-overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 24px;
  color: #fff;
  background: rgb(0 0 0 / 46%);
  backdrop-filter: blur(2px);
  font-size: 14px;
  font-weight: 600;
}

.spinner, .progress-spinner {
  width: 16px;
  height: 16px;
  flex: 0 0 auto;
  border: 2px solid rgb(255 255 255 / 25%);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.chat-panel {
  position: relative;
  z-index: 10;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100dvh;
  overflow: hidden;
  border-left: 1px solid var(--line);
  background: var(--panel);
  box-shadow: -8px 0 28px rgb(0 0 0 / 12%);
}

.chat-bar {
  position: relative;
  z-index: 22;
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 52px;
  height: 52px;
  padding: 0 10px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.chat-avatar { width: 27px; height: 27px; flex: 0 0 auto; border-radius: 50%; object-fit: cover; background: #fff; }
.chat-heading { min-width: 0; display: flex; flex: 1 1 auto; flex-direction: column; line-height: 1.15; }
.chat-heading strong { overflow: hidden; font-size: 13px; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
.chat-heading span { margin-top: 3px; overflow: hidden; color: var(--muted); font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.chat-actions { display: flex; align-items: center; gap: 2px; flex: 0 0 auto; }
.icon-button, .attach-button, .send-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
}
.icon-button:hover, .attach-button:hover { background: var(--surface-hover); }
.icon-button.active { background: var(--surface); }
.icon-button svg, .attach-button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.login-button {
  min-width: 48px;
  height: 30px;
  padding: 0 10px;
  border: 1px solid #4a4a4a;
  border-radius: 8px;
  background: transparent;
  color: #ddd;
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
}
.login-button:hover { background: var(--surface-hover); }
.login-button.admin { border-color: rgb(16 163 127 / 55%); color: #7ee2c6; }

.history-drawer {
  position: absolute;
  z-index: 20;
  top: 52px;
  right: 0;
  left: 0;
  display: flex;
  flex-direction: column;
  max-height: min(58vh, 520px);
  overflow: hidden;
  border-bottom: 1px solid var(--line);
  background: #1d1d1d;
  box-shadow: 0 16px 30px rgb(0 0 0 / 38%);
}
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px 8px; font-size: 12px; }
.drawer-head span { color: var(--muted); font-size: 11px; }
.history-list { min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 3px 7px 10px; scrollbar-width: thin; }
.history-row { display: flex; align-items: center; gap: 2px; border-radius: 8px; }
.history-row:hover, .history-row.active { background: var(--surface); }
.history-item { display: flex; align-items: center; gap: 9px; min-width: 0; flex: 1 1 auto; height: 36px; padding: 0 8px; border: 0; background: transparent; text-align: left; cursor: pointer; }
.history-item span:last-child { overflow: hidden; font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.project-dot { width: 6px; height: 6px; flex: 0 0 auto; border-radius: 50%; background: #616161; }
.project-dot.ready { background: var(--accent); }
.history-delete { display: grid; width: 28px; height: 28px; flex: 0 0 auto; place-items: center; border: 0; border-radius: 7px; background: transparent; color: #8c8c8c; cursor: pointer; opacity: 0; }
.history-row:hover .history-delete, .history-row:focus-within .history-delete { opacity: 1; }
.history-delete:hover { background: #4a2929; color: #ffb4b4; }
.history-delete svg { width: 15px; fill: currentColor; }
.model-control { display: grid; gap: 6px; padding: 10px 14px 13px; border-top: 1px solid var(--line); color: var(--muted); font-size: 11px; }
.model-control select { width: 100%; height: 34px; padding: 0 9px; border: 1px solid #484848; border-radius: 7px; background: #292929; color: var(--text); font-size: 12px; }

.messages {
  min-height: 0;
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 18px 17px 22px;
  scrollbar-color: #4a4a4a transparent;
  scrollbar-width: thin;
}
.empty { display: grid; min-height: 100%; place-content: center; justify-items: center; padding: 35px; color: var(--muted); text-align: center; }
.empty img { width: 58px; height: 58px; border-radius: 50%; object-fit: cover; background: #fff; }
.empty h1 { margin: 13px 0 4px; color: var(--text); font-size: 20px; }
.empty p { max-width: 260px; margin: 0; font-size: 13px; line-height: 1.5; }
.message { width: 100%; max-width: 500px; margin: 0 auto 22px; font-size: 13px; line-height: 1.55; }
.message.user { width: fit-content; max-width: 88%; margin-right: 0; padding: 9px 13px; border-radius: 16px 16px 4px 16px; background: var(--surface); }
.message-heading { display: flex; align-items: center; gap: 8px; margin-bottom: 7px; }
.message.user .message-heading { display: none; }
.message-heading img { width: 23px; height: 23px; border-radius: 50%; object-fit: cover; background: #fff; }
.message-label { font-size: 12px; font-weight: 650; }
.message-content { overflow-wrap: anywhere; }
.message.user .message-content { white-space: pre-wrap; }
.message.assistant .message-content > :first-child { margin-top: 0; }
.message.assistant .message-content > :last-child { margin-bottom: 0; }
.message.assistant .message-content p { margin: 0 0 10px; }
.message.assistant .message-content h1,
.message.assistant .message-content h2,
.message.assistant .message-content h3,
.message.assistant .message-content h4 { margin: 15px 0 7px; line-height: 1.3; }
.message.assistant .message-content h1 { font-size: 18px; }
.message.assistant .message-content h2 { font-size: 16px; }
.message.assistant .message-content h3,
.message.assistant .message-content h4 { font-size: 14px; }
.message.assistant .message-content ul,
.message.assistant .message-content ol { margin: 7px 0 11px; padding-left: 22px; }
.message.assistant .message-content li { margin: 3px 0; }
.message.assistant .message-content blockquote { margin: 10px 0; padding: 2px 0 2px 11px; border-left: 3px solid #555; color: #c7c7c7; }
.message.assistant .message-content code { padding: 2px 5px; border-radius: 4px; background: #2b2b2b; color: #f1d18a; font: 11px/1.5 ui-monospace, SFMono-Regular, Consolas, monospace; }
.message.assistant .message-content pre { max-width: 100%; margin: 10px 0; padding: 10px 11px; overflow-x: auto; border: 1px solid #3d3d3d; border-radius: 8px; background: #202020; }
.message.assistant .message-content pre code { padding: 0; background: transparent; color: #e4e4e4; }
.message.assistant .message-content a { color: #7fc8ff; text-decoration: underline; text-underline-offset: 2px; }
.message.assistant .message-content table { display: block; max-width: 100%; margin: 10px 0; overflow-x: auto; border-collapse: collapse; }
.message.assistant .message-content th,
.message.assistant .message-content td { padding: 6px 8px; border: 1px solid #444; text-align: left; }
.message.assistant .message-content hr { margin: 14px 0; border: 0; border-top: 1px solid #3d3d3d; }
.message.assistant .message-content .katex-display { max-width: 100%; overflow-x: auto; overflow-y: hidden; }
.message-error { margin-top: 8px; color: #ff9f9f; white-space: pre-wrap; }
.project-verified { margin-top: 10px; padding: 7px 9px; border: 1px solid #315a50; border-radius: 8px; background: #1d302b; color: #8ad7c3; font-size: 11px; }
.message-files { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; color: #c8c8c8; font-size: 11px; }
.live-progress { margin-top: 7px; padding: 10px 11px; border: 1px solid #3d3d3d; border-radius: 9px; background: #242424; }
.progress-line { display: flex; align-items: center; gap: 8px; }
.progress-spinner { width: 14px; height: 14px; color: #8ad7c3; }
.progress-label { min-width: 0; flex: 1; font-size: 12px; font-weight: 600; }
.progress-clock { color: var(--muted); font-size: 10px; white-space: nowrap; }
.progress-note { margin: 6px 0 0 22px; color: var(--muted); font-size: 11px; }
.activity-details { margin: 8px 0; color: #c5c5c5; font-size: 11px; }
.activity-details summary { cursor: pointer; color: var(--muted); }
.execution-plan { margin: 9px 0; padding: 9px 11px; border: 1px solid #414141; border-radius: 8px; }
.execution-plan summary { cursor: pointer; font-size: 11px; color: #8ad7c3; }
.execution-plan .message-content { padding-top: 8px; }
.activity-list { display: grid; gap: 5px; margin: 8px 0 0; padding-left: 21px; }
.activity-list li { padding-left: 2px; }
.activity-list li small { float: right; margin-left: 10px; color: #777; }
.activity-list li.error { color: #ff9f9f; }
.activity-error-detail { display: block; margin: 3px 0 0 17px; color: #ffb7b7; overflow-wrap: anywhere; }
.composer-wrap { flex: 0 0 auto; padding: 7px 13px calc(8px + env(safe-area-inset-bottom)); background: var(--panel); }
.save-row { display: flex; align-items: center; min-height: 25px; padding: 0 3px 5px; }
.save-cloud-button { display: inline-flex; align-items: center; gap: 5px; margin-right: auto; min-height: 28px; padding: 0 9px; border: 1px solid #43594f; border-radius: 7px; background: #23352e; color: #a5e2cb; cursor: pointer; font-size: 11px; white-space: nowrap; }
.save-cloud-button svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.save-cloud-button:hover:not(:disabled) { background: #2b4439; border-color: #648c76; }
.save-cloud-button:disabled { opacity: .45; cursor: default; }
.save-cloud-button.saving svg { animation: spin 1s linear infinite; }
.project-status { min-height: 17px; margin: 0 3px 4px; color: var(--muted); font-size: 10px; line-height: 1.4; }
.project-status.saved { color: #8ad7c3; }
.project-status.error { color: #ff9f9f; }
.attachments { display: flex; gap: 5px; overflow-x: auto; padding: 0 3px 5px; }
.attachments:empty { display: none; }
.file-chip { display: inline-flex; align-items: center; gap: 5px; max-width: 230px; padding: 5px 7px; border-radius: 7px; background: var(--surface); font-size: 11px; white-space: nowrap; }
.file-chip button { padding: 0; border: 0; background: transparent; color: var(--muted); cursor: pointer; }
.composer { display: flex; align-items: flex-end; gap: 5px; min-height: 49px; padding: 7px; border: 1px solid #424242; border-radius: 15px; background: #2b2b2b; box-shadow: 0 2px 10px rgb(0 0 0 / 16%); }
.composer:focus-within { border-color: #565656; }
.attach-button { flex: 0 0 32px; color: #c5c5c5; }
#prompt { width: 100%; min-height: 32px; max-height: 150px; padding: 6px 4px 5px; resize: none; overflow-y: auto; border: 0; outline: 0; background: transparent; color: var(--text); font-size: 13px; line-height: 1.45; }
#prompt::placeholder { color: #909090; }
.send-button { flex: 0 0 32px; border-radius: 50%; background: #f4f4f4; color: #1f1f1f; font-size: 19px; font-weight: 700; }
.send-button:hover { background: #fff; }
.send-button.stop { position: relative; background: #f4f4f4; font-size: 0; }
.send-button.stop::before { width: 10px; height: 10px; border-radius: 2px; background: #222; content: ""; }
.send-button.stopping { opacity: .55; cursor: wait; }
.composer-hint { margin: 5px 0 0; color: #737373; font-size: 9px; text-align: center; }

.shell.chat-collapsed .chat-bar { height: 100%; flex-basis: 100%; align-items: flex-start; justify-content: center; padding: 10px; border-bottom: 0; }
.shell.chat-collapsed .collapse-button svg { transform: rotate(180deg); }
.shell.chat-collapsed .chat-avatar,
.shell.chat-collapsed .chat-heading,
.shell.chat-collapsed .chat-actions,
.shell.chat-collapsed .history-drawer,
.shell.chat-collapsed .messages,
.shell.chat-collapsed .composer-wrap { display: none !important; }

.modal-backdrop { position: fixed; z-index: 100; inset: 0; display: grid; place-items: center; padding: 18px; background: rgb(0 0 0 / 68%); backdrop-filter: blur(4px); }
.modal-card { position: relative; width: min(360px, 100%); padding: 25px; border: 1px solid #444; border-radius: 15px; background: #242424; box-shadow: 0 24px 70px #000; }
.modal-card > img { width: 46px; height: 46px; border-radius: 50%; object-fit: cover; background: #fff; }
.modal-card h2 { margin: 12px 0 4px; font-size: 19px; }
.modal-card > p { margin: 0 0 18px; color: var(--muted); font-size: 12px; }
.modal-card form, .modal-card label { display: grid; gap: 7px; }
.modal-card form { gap: 13px; }
.modal-card label { color: #cfcfcf; font-size: 11px; font-weight: 600; }
.modal-card input { height: 40px; padding: 0 10px; border: 1px solid #4a4a4a; border-radius: 8px; outline: 0; background: #191919; color: var(--text); }
.modal-card input:focus { border-color: #888; }
.modal-card form > button { height: 40px; border: 0; border-radius: 8px; background: var(--accent); color: #fff; cursor: pointer; font-weight: 650; }
.modal-close { position: absolute; top: 10px; right: 10px; width: 30px; height: 30px; border: 0; border-radius: 7px; background: transparent; color: #aaa; cursor: pointer; font-size: 21px; }
.modal-close:hover { background: var(--surface-hover); }
.muse-card { width: min(460px, 100%); max-height: calc(100dvh - 36px); overflow-y: auto; }
.muse-login-link { display: block; padding: 11px; border: 1px solid #4a4a4a; border-radius: 8px; color: var(--text); text-align: center; text-decoration: none; }
.muse-login-link:hover { background: var(--surface-hover); }
.muse-steps { margin: 16px 0; padding-left: 22px; font-size: 12px; line-height: 1.7; color: #cfcfcf; }
.muse-steps li + li { margin-top: 7px; }
.muse-controls { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0 16px; }
.muse-card .muse-status { margin: 16px 0 9px; line-height: 1.6; color: var(--text); }
.muse-card .muse-note { margin: 0; font-size: 11px; line-height: 1.6; color: var(--muted); }
.muse-card input { width: 100%; min-width: 0; }
.muse-card button:disabled { cursor: wait; opacity: .6; }
.form-error { min-height: 16px; margin: -3px 0; color: #ff9f9f !important; font-size: 11px !important; }

@media (max-width: 900px) {
  .shell { display: block; position: relative; }
  .editor-panel { width: 100%; }
  .chat-panel { position: absolute; top: 0; right: 0; bottom: 0; width: min(420px, calc(100vw - 22px)); }
  .shell.chat-collapsed .chat-panel { width: 52px; }
}

@media (max-width: 520px) {
  .chat-panel { width: 100vw; }
  .chat-bar { padding-inline: 7px; }
  .messages { padding-inline: 13px; }
  .login-button { min-width: 44px; padding-inline: 7px; }
}
.project-context-note { margin-top: 12px; color: #aaa; font-size: 13px; line-height: 1.5; }
