:root {
  --bg-deep: #1b1410;
  --bg-panel: #241a14;
  --bg-panel-raised: #2c1f17;
  --burnt-orange: #c1440e;
  --burnt-orange-bright: #e0611f;
  --parchment: #f1e3c9;
  --parchment-dim: #c9b896;
  --border-soft: #3a2a1e;
  --error-red: #d9534f;
  --ok-green: #7fae4d;
  --font-body: 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'Consolas', 'Cascadia Code', monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg-deep);
  color: var(--parchment);
  font-family: var(--font-body);
  overflow: hidden;
}

.app {
  display: flex;
  height: 100vh;
}

/* Sidebar */
.sidebar {
  width: 260px;
  flex-shrink: 1;
  min-width: 160px;
  background: var(--bg-panel);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px 18px;
  border-bottom: 1px solid var(--border-soft);
  margin-bottom: 14px;
}

.brand-mark { color: var(--burnt-orange-bright); font-size: 18px; }
.brand-text { font-weight: 600; letter-spacing: 0.3px; }

.brand-version {
  font-size: 11px;
  font-weight: 400;
  color: var(--parchment-dim);
  margin-left: 2px;
}

.sidebar-section-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--parchment-dim);
  padding: 4px 6px;
  margin-bottom: 4px;
}

.project-list {
  list-style: none;
  margin: 0;
  padding: 0;
  flex: 1;
  overflow-y: auto;
}

.project-list li {
  padding: 9px 10px;
  margin-bottom: 4px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13.5px;
  border: 1px solid transparent;
  color: var(--parchment-dim);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
}

.project-list li:hover {
  background: var(--bg-panel-raised);
  color: var(--parchment);
}

.project-list li.active {
  background: linear-gradient(90deg, rgba(193,68,14,0.25), rgba(193,68,14,0.05));
  border-color: var(--burnt-orange);
  color: var(--parchment);
}

.project-list li .rename-icon {
  opacity: 0.55;
  font-size: 11px;
}

.project-list li .project-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.project-list li .delete-btn {
  flex-shrink: 0;
  border: none;
  background: transparent;
  color: var(--parchment-dim);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
  opacity: 0;
}

.project-list li:hover .delete-btn {
  opacity: 0.7;
}

.project-list li .delete-btn:hover {
  opacity: 1;
  color: var(--error-red);
  background: rgba(217, 83, 79, 0.12);
}

.empty-state {
  color: var(--parchment-dim);
  font-style: italic;
  padding: 6px 6px;
  font-size: 13px;
}

/* Editor body: file explorer + editor column, side by side */
.editor-body {
  display: flex;
  flex: 1;
  min-height: 0;
  /* file-explorer's own min-width (120) + explorer-resizer (5) +
     editor-main's own min-width (200) — see the note on .main above for
     why this can't be left at its flex default. */
  min-width: 325px;
}

.editor-main {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 200px;
}

.code-editor-row {
  display: flex;
  flex: 1;
  min-height: 0;
}

.line-numbers {
  flex-shrink: 0;
  padding: 14px 10px 14px 14px;
  background: var(--bg-deep);
  color: var(--parchment-dim);
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.5;
  text-align: right;
  user-select: none;
  overflow: hidden;
  white-space: pre;
  border-right: 1px solid var(--border-soft);
}

/* File explorer (vertical list, VS-Code-ish) */
.file-explorer {
  width: 190px;
  flex-shrink: 1;
  min-width: 120px;
  display: flex;
  flex-direction: column;
  background: var(--bg-panel);
}

.file-explorer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--parchment-dim);
  border-bottom: 1px solid var(--border-soft);
}

.icon-btn {
  background: transparent;
  border: 1px solid var(--border-soft);
  color: var(--parchment-dim);
  border-radius: 5px;
  width: 20px;
  height: 20px;
  line-height: 1;
  font-size: 13px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}
.icon-btn:hover { color: var(--parchment); border-color: var(--burnt-orange); }
.icon-btn:disabled { opacity: 0.45; cursor: default; pointer-events: none; }

.file-explorer-list {
  list-style: none;
  margin: 0;
  padding: 6px;
  flex: 1;
  overflow-y: auto;
}

.file-explorer-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 8px;
  margin-bottom: 2px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12.5px;
  font-family: var(--font-mono);
  color: var(--parchment-dim);
  border: 1px solid transparent;
}

.file-explorer-list li:hover {
  background: var(--bg-panel-raised);
  color: var(--parchment);
}

.file-explorer-list li.active {
  border-color: var(--burnt-orange);
  color: var(--parchment);
  background: linear-gradient(90deg, rgba(193,68,14,0.25), rgba(193,68,14,0.05));
}

.file-explorer-list li .file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.file-explorer-list li .file-delete-btn {
  flex-shrink: 0;
  border: none;
  background: transparent;
  color: inherit;
  opacity: 0;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  padding: 1px 3px;
  border-radius: 4px;
}

.file-explorer-list li:hover .file-delete-btn { opacity: 0.6; }

.file-explorer-list li .file-delete-btn:hover {
  opacity: 1;
  color: var(--error-red);
  background: rgba(217, 83, 79, 0.12);
}

/* GitHub Sync modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-overlay.hidden { display: none; }

.modal-panel {
  width: 420px;
  max-width: 90vw;
  max-height: 85vh;
  overflow-y: auto;
  background: var(--bg-panel);
  border: 1px solid var(--border-soft);
  border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-soft);
  font-weight: 600;
  color: var(--parchment);
  font-size: 14px;
}

.modal-close-btn {
  background: transparent;
  border: none;
  color: var(--parchment-dim);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}
.modal-close-btn:hover { color: var(--parchment); }

.modal-body { padding: 16px; }

.modal-field-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.modal-field-row label {
  width: 78px;
  flex-shrink: 0;
  font-size: 12px;
  color: var(--parchment-dim);
}

.modal-field-row input {
  flex: 1;
  background: var(--bg-deep);
  border: 1px solid var(--border-soft);
  color: var(--parchment);
  border-radius: 6px;
  padding: 7px 9px;
  font-size: 13px;
  min-width: 0;
}

.modal-hint {
  font-size: 11.5px;
  color: var(--parchment-dim);
  margin: -2px 0 14px 88px;
}
.modal-hint.ok { color: var(--ok-green); }
.modal-hint.warn { color: var(--burnt-orange-bright); }

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.modal-log {
  max-height: 160px;
}

.envvars-textarea {
  width: 100%;
  height: 140px;
  background: var(--bg-deep);
  border: 1px solid var(--border-soft);
  color: var(--parchment);
  border-radius: 6px;
  padding: 10px;
  font-family: var(--font-mono);
  font-size: 12.5px;
  resize: vertical;
  margin-bottom: 10px;
}

.modal-log a {
  color: var(--burnt-orange-bright);
  text-decoration: underline;
  cursor: pointer;
}

.ghost-btn {
  background: transparent;
  border: 1px dashed var(--border-soft);
  color: var(--parchment-dim);
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  margin-top: 6px;
}
.ghost-btn:hover { border-color: var(--burnt-orange); color: var(--parchment); }
.ghost-btn:disabled {
  opacity: 0.45;
  cursor: default;
  pointer-events: none;
}

.sidebar-footer { margin-top: 14px; }

.status-pill {
  display: inline-block;
  font-size: 11.5px;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--bg-panel-raised);
  color: var(--parchment-dim);
  border: 1px solid var(--border-soft);
}
.status-pill.ok { color: var(--ok-green); border-color: var(--ok-green); }
.status-pill.error { color: var(--error-red); border-color: var(--error-red); }

/* Main */
.main {
  flex: 1;
  display: flex;
  /* Must reflect editor-pane's own min-width (380) + the chat-resizer (5) +
     chat-pane's own min-width (260) — NOT 0. A flex item's min-width:0
     tells its OWN parent container "I need no space", so the parent never
     shrinks earlier siblings (the sidebar) to make room — .main's children
     just silently overflow past the window edge instead. This is exactly
     what let a large sidebar/editor-pane width (e.g. from a previous
     maximized-window resize) push the chat pane off-screen once the window
     shrank back down. */
  min-width: 645px;
}

.editor-pane, .chat-pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.editor-pane {
  flex: 0 1 620px;
  min-width: 380px;
}

.chat-pane {
  flex: 1;
  min-width: 260px;
}

/* Drag handles between resizable panels */
.resizer {
  flex-shrink: 0;
  width: 5px;
  cursor: col-resize;
  background: transparent;
  position: relative;
}

.resizer::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 2px;
  width: 1px;
  background: var(--border-soft);
}

.resizer:hover::after,
.resizer.dragging::after {
  left: 0;
  width: 5px;
  background: var(--burnt-orange);
}

/* Horizontal handle: sits between the code editor and the log under it. */
.resizer-row {
  width: auto;
  height: 5px;
  cursor: row-resize;
}

.resizer-row::after {
  top: 2px;
  bottom: auto;
  left: 0;
  right: 0;
  width: auto;
  height: 1px;
  background: transparent; /* the log's own top border is the line at rest */
}

.resizer-row:hover::after,
.resizer-row.dragging::after {
  top: 0;
  left: 0;
  width: auto;
  height: 5px;
}

.pane-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  row-gap: 8px;
  gap: 14px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border-soft);
  background: var(--bg-panel);
  font-size: 13.5px;
  font-weight: 600;
  color: var(--parchment);
}

#active-project-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.runtime-toggle {
  display: flex;
  border: 1px solid var(--border-soft);
  border-radius: 6px;
  overflow: hidden;
  flex-shrink: 0;
}

.runtime-btn {
  background: var(--bg-panel-raised);
  color: var(--parchment-dim);
  border: none;
  padding: 6px 10px;
  font-size: 11.5px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.runtime-btn:hover { color: var(--parchment); }

.runtime-btn.active {
  background: var(--burnt-orange);
  color: var(--parchment);
}

.runtime-btn:disabled { opacity: 0.5; cursor: default; }

.pane-actions { display: flex; flex-wrap: wrap; row-gap: 8px; gap: 8px; min-width: 0; }

.primary-btn {
  background: var(--burnt-orange);
  color: var(--parchment);
  border: none;
  padding: 7px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 600;
}
.primary-btn:hover { background: var(--burnt-orange-bright); }
.primary-btn:disabled {
  opacity: 0.45;
  cursor: default;
  pointer-events: none;
}

#code-editor {
  flex: 1;
  background: var(--bg-deep);
  color: var(--parchment);
  border: none;
  outline: none;
  resize: none;
  padding: 14px 16px;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.5;
}

.editor-log {
  max-height: 140px;
  flex-shrink: 0;
  overflow-y: auto;
  border-top: 1px solid var(--border-soft);
  background: var(--bg-panel);
  padding: 8px 14px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--parchment-dim);
}
.editor-log .log-error { color: var(--error-red); }
.editor-log .log-ok { color: var(--ok-green); }

.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  background: var(--bg-deep);
  font-size: 13.5px;
}

.chat-line { margin-bottom: 8px; line-height: 1.4; }
.chat-line .who { font-weight: 700; margin-right: 6px; }
.chat-line.user .who { color: #9db5d6; }
.chat-line.bot .who { color: var(--burnt-orange-bright); }
.chat-line.system .who { color: var(--parchment-dim); font-style: italic; }
.chat-line.error .who { color: var(--error-red); }

.chat-form {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border-soft);
  background: var(--bg-panel);
  align-items: center;
}

.chat-form input[type="text"] {
  background: var(--bg-deep);
  border: 1px solid var(--border-soft);
  color: var(--parchment);
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 13px;
}

#chat-username { width: 100px; }
#chat-message { flex: 1; min-width: 160px; }

.toggle {
  font-size: 12px;
  color: var(--parchment-dim);
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
}

/* --- Web build additions --- */
[hidden] { display: none !important; }

.web-back {
  display: block;
  font-size: 12px;
  color: var(--parchment-dim);
  text-decoration: none;
  padding: 0 6px 12px;
}
.web-back:hover { color: var(--burnt-orange-bright); }

.web-note {
  font-size: 11px;
  line-height: 1.45;
  color: var(--parchment-dim);
  margin: 10px 2px 0;
}

/* Narrow screens: stack the sidebar, editor and chat instead of side by side. */
@media (max-width: 900px) {
  html, body { overflow: auto; height: auto; }
  .app { flex-direction: column; height: auto; min-height: 100vh; }
  .resizer { display: none; }
  .sidebar { width: auto !important; min-width: 0; padding: 12px 16px; }
  .project-list { max-height: 160px; }
  .main { flex-direction: column; min-width: 0; }
  .editor-pane, .chat-pane { flex: none !important; width: auto !important; min-width: 0; }
  .editor-pane { height: 75vh; }
  .chat-pane { height: 70vh; }
  .editor-body { min-width: 0; }
  .file-explorer { width: 120px !important; min-width: 90px; }
  .editor-main { min-width: 0; }
}

.storage-line { font-size: 11.5px; line-height: 1.45; color: var(--parchment-dim); margin: 0 2px 8px; }
.storage-line strong { display: block; font-size: 12px; color: var(--parchment); font-weight: 600; }
.storage-line.ok strong { color: var(--ok-green); }
.storage-line.error strong { color: var(--error-red); }
.storage-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.storage-actions button { margin-top: 0; padding: 6px 10px; font-size: 12px; }

.modal-body a { color: var(--burnt-orange-bright); }
.backups-list { list-style: none; margin: 12px 0 0; padding: 0; max-height: 280px; overflow-y: auto; }
.backups-list li {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 4px; border-bottom: 1px solid var(--border-soft);
}
.backups-list li.empty-state { display: block; border: 0; }
.backup-info { display: flex; flex-direction: column; min-width: 0; font-size: 13px; }
.backup-info span { font-size: 11.5px; color: var(--parchment-dim); }
.backup-actions { display: flex; gap: 6px; align-items: center; flex-shrink: 0; }
.backup-actions .ghost-btn { margin-top: 0; padding: 5px 9px; font-size: 12px; }
.backups-file { margin-top: 16px; padding-top: 12px; border-top: 1px solid var(--border-soft); }
.backups-file .sidebar-section-label { padding-left: 0; }
@media (max-width: 520px) { .backups-list li { flex-direction: column; align-items: stretch; } }
.google-signin-btn { display: flex; justify-content: center; min-height: 44px; }
.danger-btn { color: var(--error-red); border-color: rgba(217, 83, 79, 0.5); }
.danger-btn:hover { color: #fff; background: var(--error-red); }
.backups-file .modal-hint { text-align: left; }
