:root {
  color-scheme: dark;
  --bg: #0a0b0d;
  --bg-elev: #121418;
  --bg-elev-2: #181b20;
  --border: #24272e;
  --text: #e8e6e2;
  --text-dim: #7d828b;
  --accent: #d97757;
  --accent-bright: #e88a68;
  --accent-dim: #a85d43;
  --accent-glow: rgba(217, 119, 87, 0.35);
  --danger: #f85149;
  --ok: #3fb950;
  --radius: 10px;
  --mono: ui-monospace, "Cascadia Code", "JetBrains Mono", "Fira Code", Menlo, Consolas, "Liberation Mono", monospace;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; margin: 0; overflow: hidden; }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }
* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }

body {
  background: #000;
  color: var(--text);
  font-family: var(--mono);
  font-size: 15px;
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.dim { color: var(--text-dim); }

@keyframes fadein { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: translateY(0); } }
@keyframes blink { 50% { opacity: 0; } }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

.cursor-blink { display: inline-block; color: var(--accent); animation: blink 1s steps(1) infinite; }

/* ---------- traffic lights (shared: app titlebar + login card) ---------- */
.tl-lights { display: flex; gap: 7px; flex-shrink: 0; }
.tl { width: 11px; height: 11px; border-radius: 50%; }
.tl.red { background: #ff5f57; }
.tl.yellow { background: #febc2e; }
.tl.green { background: #28c840; }

/* ---------- terminal window shell ---------- */
.term-window {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  background: var(--bg);
}

@media (min-width: 720px) {
  body:not(.login-body) {
    align-items: center;
    justify-content: center;
    padding: 28px;
  }
  .term-window {
    width: 100%;
    max-width: 880px;
    height: min(88vh, 920px);
    border-radius: 12px;
    border: 1px solid var(--border);
    box-shadow: 0 40px 100px -24px rgba(0, 0, 0, 0.75), 0 0 0 1px rgba(255, 255, 255, 0.02);
    overflow: hidden;
  }
}

/* ---------- Login page ---------- */
.login-body { background: #000; }
.login-wrap {
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding-bottom: 24px;
  overflow: hidden;
  box-shadow: 0 40px 100px -24px rgba(0, 0, 0, 0.75), 0 0 0 1px rgba(255, 255, 255, 0.02);
}
.login-card .tl-lights {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev-2);
}
.login-boot { padding: 20px 22px 4px; }
.login-boot p { margin: 0 0 6px; font-size: 13px; color: var(--text); }
.login-boot p.dim { font-size: 12px; }
.login-label { display: block; padding: 14px 22px 6px; font-size: 12.5px; color: var(--text-dim); }
.login-card input {
  display: block;
  width: calc(100% - 44px);
  margin: 0 22px 14px;
  padding: 11px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #000;
  color: var(--text);
  font-size: 16px;
  font-family: var(--mono);
}
.login-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
.login-card button {
  display: block;
  width: calc(100% - 44px);
  margin: 4px 22px 0;
  padding: 11px 14px;
  border-radius: 8px;
  border: 1px solid var(--accent-dim);
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  font-family: var(--mono);
  box-shadow: 0 2px 0 0 var(--accent-dim);
}
.login-card button:active { transform: translateY(1px); box-shadow: none; }
.error-msg { color: var(--danger); font-size: 12.5px; min-height: 18px; margin: 10px 22px 0; }

/* ---------- Titlebar ---------- */
header.topbar {
  padding-top: calc(10px + var(--safe-top));
  padding-bottom: 10px;
  padding-left: 12px;
  padding-right: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  flex-shrink: 0;
}
.tl-title {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 12.5px;
  color: var(--text-dim);
}
.tl-name { color: var(--text); font-weight: 600; white-space: nowrap; flex-shrink: 0; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-dim); flex-shrink: 0; }
.status-dot.connected { background: var(--ok); }
.status-dot.working { background: var(--accent); animation: pulse 1s infinite; }
.cwd { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tl-actions { display: flex; gap: 2px; flex-shrink: 0; }
button.icon-btn, a.icon-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  background: none;
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--text-dim);
  text-decoration: none;
  cursor: pointer;
  transition: background-color .12s ease, color .12s ease, border-color .12s ease;
}
.icon-btn svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.75;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}
button.icon-btn:active, a.icon-btn:active { background: var(--bg-elev-2); color: var(--text); border-color: var(--border); }

@media (hover: hover) and (pointer: fine) {
  button.icon-btn:hover, a.icon-btn:hover { background: var(--bg-elev-2); color: var(--text); border-color: var(--border); }
  button.icon-btn.project-del:hover { color: var(--danger); }
}

.icon-btn:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

button.icon-btn#notifyBtn.on { color: var(--accent-bright); }
button.icon-btn#notifyBtn .i-off { display: none; }
button.icon-btn#notifyBtn.off .i-on { display: none; }
button.icon-btn#notifyBtn.off .i-off { display: inline; }
button.icon-btn#fileSaveBtn { color: var(--ok); border-color: var(--ok); }

.badge-dot {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 2px var(--bg-elev);
}
.badge-dot.pending { background: var(--danger); animation: pulse 1s infinite; }
.badge-dot.hidden { display: none; }

/* ---------- Messages (terminal log) ---------- */
#messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 14px 10px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 13.5px;
  line-height: 1.55;
  background: var(--bg);
}

.line { display: flex; gap: 10px; align-items: flex-start; max-width: 100%; animation: fadein .15s ease-out; }
.line .marker { flex-shrink: 0; width: 1ch; color: var(--text-dim); padding-top: 1px; user-select: none; }
.line.user .marker { color: var(--accent); font-weight: 700; }

.line-content { min-width: 0; flex: 1; white-space: pre-wrap; word-wrap: break-word; }
.line-content p { margin: 0 0 8px; }
.line-content p:last-child { margin-bottom: 0; }
.line-content pre {
  background: #000;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  font-size: 12.5px;
  margin: 8px 0;
}
.line-content code { font-family: var(--mono); }
.line-content :not(pre) > code {
  background: rgba(217, 119, 87, 0.15);
  color: var(--accent-bright);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 12.5px;
}

/* tool / system cards */
.tool-card {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px;
  margin-left: 1ch;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-left: 2px solid var(--text-dim);
  border-radius: 6px;
  padding: 7px 10px;
  font-size: 12px;
}
.tool-tag { color: var(--accent-bright); font-weight: 600; flex-shrink: 0; }
.tool-summary { color: var(--text-dim); flex: 1; min-width: 0; overflow-wrap: anywhere; }
.tool-status { font-weight: 700; flex-shrink: 0; }
.tool-card.error { border-left-color: var(--danger); }
.tool-card.error .tool-status { color: var(--danger); }
.tool-card.ok { border-left-color: var(--ok); }
.tool-card.ok .tool-status { color: var(--ok); }

.system-note {
  align-self: center;
  color: var(--text-dim);
  font-size: 11.5px;
  padding: 4px 10px;
}

/* permission request */
.permission-card {
  align-self: stretch;
  background: var(--bg-elev-2);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 12px 14px;
  box-shadow: 0 0 24px -10px var(--accent-glow);
}
.permission-card .ptitle { font-weight: 600; margin-bottom: 4px; font-size: 13px; }
.permission-card .ptag { color: var(--accent-bright); }
.permission-card .pdetail { font-size: 12px; color: var(--text-dim); margin-bottom: 10px; }
.permission-card pre { background: #000; border-radius: 6px; padding: 8px; font-size: 11.5px; overflow-x: auto; border: 1px solid var(--border); margin: 6px 0 10px; }
.permission-actions { display: flex; gap: 8px; }
.permission-actions button {
  flex: 1;
  padding: 10px;
  border-radius: 6px;
  border: 1px solid;
  background: transparent;
  font-family: var(--mono);
  font-size: 12.5px;
  font-weight: 600;
}
.permission-actions .allow { color: var(--ok); border-color: var(--ok); }
.permission-actions .allow:active { background: rgba(63, 185, 80, 0.15); }
.permission-actions .deny { color: var(--danger); border-color: var(--danger); }
.permission-actions .deny:active { background: rgba(248, 81, 73, 0.15); }
.permission-actions button:disabled { opacity: 0.45; }

/* typing indicator */
.typing { align-self: flex-start; display: flex; align-items: baseline; gap: 6px; padding: 2px 0 2px 1ch; font-size: 12.5px; }

/* ---------- Composer ---------- */
#composer {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg-elev);
}
.composer-prompt { color: var(--accent); font-weight: 700; font-size: 16px; flex-shrink: 0; padding-left: 2px; }
#composer textarea {
  flex: 1;
  resize: none;
  max-height: 120px;
  min-height: 40px;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  font-size: 15px;
  font-family: var(--mono);
  line-height: 1.35;
  transition: border-color .15s, box-shadow .15s;
}
#composer textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
#composer button#sendBtn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 1px solid var(--accent-dim);
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 0 0 var(--accent-dim), 0 4px 10px -4px var(--accent-glow);
}
#composer button#sendBtn:active { transform: translateY(1px); box-shadow: none; }
#composer button#sendBtn:disabled { opacity: 0.4; box-shadow: none; }
#composer button#stopBtn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 1px solid #8f2320;
  background: var(--danger);
  color: #fff;
  font-size: 15px;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 0 0 #8f2320;
}
#composer button#stopBtn:active { transform: translateY(1px); box-shadow: none; }

/* ---------- Overlay (shared by drawer + file viewer) ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 20;
  opacity: 0;
  transition: opacity .18s ease-out;
}
.overlay.show { opacity: 1; }
.overlay.hidden { display: none; }

/* ---------- Drawer (loyihalar / fayllar) ---------- */
.drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: min(85vw, 340px);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  z-index: 21;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform .2s ease-out;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  box-shadow: 40px 0 80px -20px rgba(0, 0, 0, 0.6);
}
.drawer.open { transform: translateX(0); }

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  padding: 8px 8px 8px 14px;
  flex-shrink: 0;
}
.drawer-tabs { display: flex; gap: 4px; }
.drawer-tab {
  background: none;
  border: 1px solid transparent;
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 12.5px;
  font-weight: 600;
  padding: 7px 12px;
  border-radius: 6px;
}
.drawer-tab.active { color: var(--accent-bright); background: var(--bg-elev-2); border-color: var(--border); }

.mode-row {
  flex-shrink: 0;
  display: flex;
  justify-content: flex-end;
  padding: 4px 12px 0;
  background: var(--bg-elev);
}
.mode-pill {
  background: none;
  border: none;
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  padding: 4px 2px;
  cursor: pointer;
}
.mode-pill[data-mode="plan"] { color: var(--accent-bright); }
.mode-pill[data-mode="acceptEdits"] { color: var(--ok); }

.drawer-panel {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 13px;
}
.drawer-panel.hidden { display: none; }

.add-project-form { display: flex; gap: 8px; flex-shrink: 0; }
.add-project-form input {
  flex: 1;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
  min-width: 0;
}
.add-project-form input:focus { outline: none; border-color: var(--accent); }
.add-project-form button {
  flex-shrink: 0;
  width: 40px;
  border-radius: 8px;
  border: 1px solid var(--accent-dim);
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 600;
}

.new-project-toggle {
  align-self: flex-start;
  background: none;
  border: 1px dashed var(--border);
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: 12px;
  padding: 8px 12px;
  border-radius: 8px;
}
.new-project-toggle:active { color: var(--text); border-color: var(--text-dim); }

.create-project-form { display: flex; flex-direction: column; gap: 8px; }
.create-project-form.hidden { display: none; }
.create-project-form input {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
}
.create-project-form input:focus { outline: none; border-color: var(--accent); }
.create-project-form button[type="submit"] {
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--accent-dim);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  font-family: var(--mono);
  font-size: 13px;
}

.empty-hint { color: var(--text-dim); font-size: 12.5px; padding: 10px 2px; }

.project-list { display: flex; flex-direction: column; gap: 6px; }
.project-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 10px 10px 12px;
}
.project-row.active { border-color: var(--accent); box-shadow: 0 0 0 1px rgba(217, 119, 87, 0.25); }
.project-info { flex: 1; min-width: 0; cursor: pointer; }
.project-label { font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.project-row.active .project-label::before { content: '●'; color: var(--accent); font-size: 9px; }
.project-path {
  font-size: 11px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-badge {
  flex-shrink: 0;
  font-size: 10.5px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 4px;
  border: 1px solid;
}
.row-badge.busy { color: var(--accent-bright); border-color: var(--accent-dim); animation: pulse 1.4s infinite; }
.row-badge.pending { color: var(--danger); border-color: var(--danger); }

.this-device {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}
.this-device-label {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 6px;
}
.this-device-list { display: flex; flex-direction: column; gap: 4px; }
.this-device-addr {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 9px;
  border-radius: 6px;
  background: var(--bg-elev);
  border: 1px solid transparent;
}
.this-device-addr:active { border-color: var(--accent); }
.this-device-addr .addr-url {
  color: var(--accent-bright);
  font-size: 12.5px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.this-device-addr .addr-tag { flex-shrink: 0; color: var(--text-dim); font-size: 10.5px; }

.online-dot {
  flex-shrink: 0;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-dim);
}
.online-dot.online { background: var(--ok); }
.project-del { flex-shrink: 0; }
button.icon-btn.project-del:active { color: var(--danger); }

.file-breadcrumb-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.file-breadcrumb-row .file-breadcrumb { flex: 1; min-width: 0; padding-bottom: 0; }
.file-breadcrumb-row .icon-btn { flex-shrink: 0; }

.file-breadcrumb {
  font-size: 12px;
  color: var(--text-dim);
  padding-bottom: 4px;
  overflow-x: auto;
  white-space: nowrap;
  flex-shrink: 0;
}
.file-breadcrumb .crumb { cursor: pointer; }
.file-breadcrumb .crumb:active { color: var(--text); }
.file-breadcrumb .crumb:last-child { color: var(--accent-bright); font-weight: 600; }

.file-tree { display: flex; flex-direction: column; gap: 1px; }
.file-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 8px;
  border-radius: 6px;
  font-size: 13px;
}
.file-row:active { background: var(--bg-elev-2); }
.file-row .file-type { flex-shrink: 0; width: 1ch; color: var(--text-dim); }
.file-row.dir .file-type { color: var(--accent-bright); }
.file-row .file-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-row.dir .file-name { font-weight: 600; }
.file-row .file-size { flex-shrink: 0; color: var(--text-dim); font-size: 11px; }
.file-row .file-dl { flex-shrink: 0; width: 32px; height: 32px; }
.file-row .file-dl svg { width: 16px; height: 16px; }
.file-row .file-dl:active { color: var(--accent-bright); }
#uploadFileInput.hidden, .empty-hint.hidden { display: none; }

/* ---------- File viewer ---------- */
.file-viewer {
  position: fixed;
  inset: 0;
  z-index: 22;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform .2s ease-out;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}
.file-viewer.open { transform: translateY(0); }
.file-viewer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
  flex-shrink: 0;
}
.file-viewer-header span {
  flex: 1;
  min-width: 0;
  font-size: 12.5px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.file-viewer-content {
  flex: 1;
  margin: 0;
  padding: 14px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  font-size: 12.5px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
  background: #000;
}
.file-viewer-editor {
  flex: 1;
  margin: 0;
  padding: 14px;
  border: none;
  resize: none;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--text);
  background: #000;
}
.file-viewer-editor:focus { outline: none; }
.file-viewer-editor.hidden { display: none; }
button.icon-btn.hidden { display: none; }
