/* Codex Remote PWA - dark, codex-aesthetic mobile-first styles */

:root {
  --bg: #0b0b0e;
  --bg-elev: #15151b;
  --bg-elev-2: #1c1c24;
  --border: #2a2a34;
  --border-strong: #3a3a46;
  --text: #ececf1;
  --text-muted: #9090a0;
  --text-dim: #6b6b78;
  --primary: #10a37f;
  --primary-hover: #0e8e6e;
  --primary-soft: rgba(16, 163, 127, 0.12);
  --error: #f87171;
  --warn: #f59e0b;
  --info: #60a5fa;
  --code-bg: #0e0e13;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  --radius: 8px;
  --radius-sm: 6px;
  --radius-lg: 12px;
  --safe-top: env(safe-area-inset-top, 0);
  --safe-bottom: env(safe-area-inset-bottom, 0);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  text-rendering: optimizeLegibility;
}
button, input, textarea { font: inherit; color: inherit; }
button { background: transparent; border: 0; cursor: pointer; }
a { color: var(--info); text-decoration: none; }
code, pre, kbd { font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace; }

.text-muted { color: var(--text-muted); }
.text-dim { color: var(--text-dim); }
.text-error { color: var(--error); }
.text-small { font-size: 12px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 12px; border-radius: var(--radius); border: 1px solid transparent;
  background: var(--bg-elev); color: var(--text); font-weight: 500;
  transition: background 120ms, border-color 120ms, opacity 120ms;
  min-height: 36px;
}
.btn:hover { background: var(--bg-elev-2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-block { width: 100%; justify-content: center; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; }
.btn-ghost:hover { background: var(--bg-elev); }
.btn-icon { width: 36px; height: 36px; padding: 0; justify-content: center; }
.btn-sm { padding: 4px 10px; min-height: 30px; font-size: 13px; }

/* Forms */
.form-control {
  width: 100%; padding: 10px 12px; border-radius: var(--radius);
  background: var(--bg-elev); border: 1px solid var(--border);
  color: var(--text); outline: none; transition: border-color 120ms;
  min-height: 40px;
}
.form-control:focus { border-color: var(--primary); }
.form-control::placeholder { color: var(--text-dim); }
textarea.form-control { resize: none; min-height: 40px; max-height: 200px; }

.form-label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-muted); margin-bottom: 14px; }
.form-label > span { padding-left: 2px; }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.form-hint { margin: 8px 0 0; }

/* Screens & views */
.screen { height: 100vh; height: 100dvh; overflow: hidden; }
.screen[hidden] { display: none !important; }
.view {
  display: none; flex-direction: column;
  height: 100vh; height: 100dvh;
  min-height: 0; overflow: hidden;
  padding-top: var(--safe-top); padding-bottom: var(--safe-bottom);
}
.view.is-active { display: flex; }

/* App header (sticky top bar) */
.app-header {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
  min-height: 52px;
}
.brand { display: flex; align-items: center; gap: 10px; flex: 1; }
.brand-mark {
  width: 32px; height: 32px; border-radius: 8px;
  display: grid; place-items: center;
  background: var(--primary-soft); color: var(--primary);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { font-size: 15px; }
.brand-text span { color: var(--text-muted); }

.chat-title { flex: 1; min-width: 0; line-height: 1.2; min-height: 38px; }
.chat-title-name { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-title-project { font-size: 11px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-title-status { display: flex; align-items: center; gap: 6px; font-size: 11px; }

.status-dot {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--text-dim); flex-shrink: 0;
}
.status-dot[data-state="ready"] { background: var(--primary); }
.status-dot[data-state="running"] { background: var(--info); animation: pulse 1.5s ease-in-out infinite; }
.status-dot[data-state="error"] { background: var(--error); }
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: 0.4 } }

/* Sidebar */
.sidebar-search {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  color: var(--text-muted);
}
.sidebar-search .form-control { background: var(--bg-elev); }
.section-label {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 16px 6px; margin-top: 4px;
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--text-dim);
}
.conn-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--text-dim);
  text-transform: none; letter-spacing: 0;
}
.conn-dot[data-state="on"] { background: var(--primary); }
.conn-dot[data-state="connecting"] { background: var(--warn); animation: pulse 1.5s ease-in-out infinite; }
.conn-dot[data-state="off"] { background: var(--error); }

.project-list {
  list-style: none; margin: 0; padding: 4px 8px 8px;
  flex: 1 1 auto; min-height: 0; overflow-y: auto;
  overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
}
.project-empty { padding: 24px 16px; text-align: center; }

/* Project group: one cwd with its threads underneath. */
.project-group { margin-bottom: 4px; }
.project-head {
  width: 100%;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 10px; border-radius: var(--radius);
  cursor: pointer; transition: background 100ms;
  background: transparent; border: 0;
  color: var(--text); text-align: left; font: inherit;
}
.project-head:hover { background: var(--bg-elev); }
.project-caret {
  width: 18px; height: 18px;
  display: grid; place-items: center;
  color: var(--text-dim);
  transition: transform 120ms;
  flex-shrink: 0;
}
.project-group.is-expanded .project-caret { transform: rotate(90deg); }
.project-icon { width: 30px; height: 30px; }
.project-add {
  width: 26px; height: 26px; border-radius: 6px;
  display: grid; place-items: center;
  color: var(--text-dim);
  background: var(--bg-elev-2);
  flex-shrink: 0;
  transition: background 100ms, color 100ms;
  margin-left: 4px;
}
.project-add:hover { background: var(--primary-soft); color: var(--primary); }

/* Thread list under a project group. */
.thread-list {
  list-style: none; margin: 0; padding: 0 0 4px 28px;
  display: flex; flex-direction: column; gap: 1px;
}
.thread-item {
  position: relative;
  padding: 7px 10px; border-radius: 6px;
  cursor: pointer; transition: background 100ms;
  display: flex; flex-direction: column; gap: 2px;
}
.thread-item::before {
  content: ''; position: absolute;
  left: -10px; top: 50%;
  width: 6px; height: 1px; background: var(--border);
}
.thread-item:hover { background: var(--bg-elev); }
.thread-item.is-active { background: var(--bg-elev-2); }
.thread-item.is-active::before { background: var(--primary); height: 2px; }
.thread-title {
  font-size: 13px; line-height: 1.3;
  overflow: hidden; text-overflow: ellipsis;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
}
.thread-sub { font-size: 11px; color: var(--text-dim); }

.project-icon {
  width: 36px; height: 36px; border-radius: 8px;
  display: grid; place-items: center;
  background: var(--bg-elev-2); color: var(--text);
  flex-shrink: 0;
}
.project-icon[data-color="1"] { background: rgba(16,163,127,0.15); color: #34d399; }
.project-icon[data-color="2"] { background: rgba(96,165,250,0.15); color: #60a5fa; }
.project-icon[data-color="3"] { background: rgba(167,139,250,0.15); color: #a78bfa; }
.project-icon[data-color="4"] { background: rgba(251,191,36,0.15); color: #fbbf24; }
.project-icon[data-color="5"] { background: rgba(244,114,182,0.15); color: #f472b6; }
.project-icon[data-color="6"] { background: rgba(248,113,113,0.15); color: #f87171; }
.project-icon[data-color="7"] { background: rgba(45,212,191,0.15); color: #2dd4bf; }
.project-icon[data-color="8"] { background: rgba(251,146,60,0.15); color: #fb923c; }
.project-meta { flex: 1; min-width: 0; }
.project-name { font-weight: 500; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.project-sub { color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.project-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-dim); flex-shrink: 0; }
.project-dot.is-running { background: var(--primary); }
.project-badge {
  font-size: 11px; padding: 2px 8px; border-radius: 10px;
  background: var(--bg-elev-2); color: var(--text-muted);
  flex-shrink: 0;
}

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 10px 12px;
}
.user-chip { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--primary-soft); color: var(--primary);
  display: grid; place-items: center; font-weight: 600; font-size: 14px;
}
.user-meta { flex: 1; min-width: 0; line-height: 1.25; }
.user-name { font-size: 14px; font-weight: 500; }

/* New project form */
.new-form {
  padding: 20px 16px;
  display: flex; flex-direction: column; gap: 8px;
}

/* Chat */
.chat-scroll {
  flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 12px 12px 0;
  overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
}
.empty-state {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  color: var(--text-dim); padding: 60px 20px; text-align: center;
}

/* Compact (summary-only) mode: hide all "process" items so the user only
   sees their own messages and the agent's final answer per turn. */
.chat-scroll.is-compact .msg.msg-process { display: none; }
#btn-compact[data-on="1"] {
  background: var(--primary-soft); color: var(--primary);
}
.day-divider { text-align: center; margin: 8px 0 4px; }
.day-divider span {
  display: inline-block; padding: 2px 10px; border-radius: 10px;
  background: var(--bg-elev); color: var(--text-muted); font-size: 11px;
}

.msg { display: flex; flex-direction: column; gap: 4px; }
.msg-user { align-items: flex-end; max-width: 88%; margin-left: auto; }
.msg-bubble {
  background: var(--primary); color: #fff;
  padding: 8px 12px; border-radius: 14px 14px 4px 14px;
  white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere;
}
.msg-time { padding: 0 4px; }

.msg-codex { display: flex; gap: 8px; align-items: flex-start; max-width: 95%; }
.msg-avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--primary-soft); color: var(--primary);
  display: grid; place-items: center; flex-shrink: 0;
}
.msg-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 8px; }
.msg-text { white-space: pre-wrap; word-wrap: break-word; overflow-wrap: anywhere; }
.msg-text code { background: var(--code-bg); padding: 1px 6px; border-radius: 4px; font-size: 13px; }
.msg-list { margin: 4px 0; padding-left: 20px; }
.msg-list li { margin: 2px 0; }

.tool-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--code-bg);
  overflow: hidden;
}
.tool-head {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px; border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--text-muted);
  background: var(--bg-elev);
}
.tool-head .text-small { color: var(--text-muted); }
.tool-body {
  margin: 0; padding: 10px 12px;
  font-size: 13px; line-height: 1.5;
  white-space: pre-wrap; overflow-wrap: anywhere;
  max-height: 360px; overflow-y: auto;
}

.status-inline {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 6px 10px; border-radius: var(--radius);
  background: var(--bg-elev); color: var(--text-muted); font-size: 13px;
}
.spinner {
  width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid var(--border-strong); border-top-color: var(--primary);
  animation: spin 0.7s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

.msg-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }

.patch-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
  overflow: hidden;
}
.patch-head {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 10px; font-size: 13px; color: var(--text-muted);
  background: var(--bg-elev-2);
}
.patch-body {
  font-family: ui-monospace, monospace; font-size: 12.5px;
  padding: 0; margin: 0; max-height: 280px; overflow: auto;
  background: var(--code-bg);
}
.patch-body .line { display: block; padding: 0 10px; white-space: pre; }
.patch-body .add { color: #6ee7b7; background: rgba(16,163,127,0.08); }
.patch-body .del { color: #fca5a5; background: rgba(248,113,113,0.08); }
.patch-body .meta { color: var(--text-dim); }

.plan-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-elev);
  padding: 10px 12px;
}
.plan-title { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 6px; }
.plan-text { white-space: pre-wrap; font-size: 14px; }

.reasoning-card {
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg-elev);
  padding: 8px 10px;
  color: var(--text-muted);
  font-size: 13px;
  white-space: pre-wrap;
}

/* Composer */
.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 8px 12px calc(8px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg);
  position: sticky; bottom: 0;
}
.composer-input { flex: 1; }
.btn-send { width: 40px; height: 40px; }
.btn-send:disabled { background: var(--bg-elev); color: var(--text-dim); }

/* Login screen */
.screen-login {
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background: radial-gradient(ellipse at top, rgba(16,163,127,0.08), transparent 60%), var(--bg);
}
.login-card {
  width: 100%; max-width: 360px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px 22px;
  text-align: center;
  box-shadow: var(--shadow);
}
.login-mark {
  width: 64px; height: 64px; border-radius: 16px;
  display: grid; place-items: center; margin: 0 auto 14px;
  background: var(--primary-soft); color: var(--primary);
}
.login-title { font-size: 20px; margin: 0 0 6px; }
.login-sub { margin: 0 0 20px; }
.login-form { display: flex; flex-direction: column; gap: 10px; }
.login-error { margin: 4px 0 0; }
.login-hint { margin: 16px 0 0; }

/* Toast */
.toast-region {
  position: fixed; left: 0; right: 0; bottom: 80px;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  pointer-events: none; z-index: 1000;
}
.toast {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px; border-radius: var(--radius);
  font-size: 13px; box-shadow: var(--shadow);
  max-width: 80%;
  pointer-events: auto;
  animation: toast-in 200ms ease;
}
.toast.is-error { border-color: rgba(248,113,113,0.5); color: #fecaca; }
.toast.is-success { border-color: rgba(16,163,127,0.5); color: #bbf7d0; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px) } to { opacity: 1; transform: none } }

/* "load earlier" control at the top of a long thread */
.history-more {
  align-self: center;
  margin: 4px auto 10px;
  padding: 7px 14px;
  border: 1px solid var(--border, rgba(255,255,255,0.14));
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted, #9aa0a6);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}
.history-more:hover { color: var(--text, #e8eaed); }

/* Appearance: home-screen icon */
.icon-pane { padding: 16px 16px 24px; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; min-height: 0; }
.icon-preview {
  display: flex; align-items: center; gap: 14px;
  padding: 14px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg-elev); margin-bottom: 6px;
}
.icon-preview img {
  width: 76px; height: 76px; border-radius: 17px;
  object-fit: cover; background: var(--bg);
  box-shadow: 0 1px 3px rgba(0,0,0,0.35);
}
.icon-preview-meta { min-width: 0; }
.icon-preview-name { font-weight: 600; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-status { margin: 4px 0 0; color: var(--text-muted); }
.icon-status[data-kind="error"] { color: var(--error); }
.icon-status[data-kind="ok"] { color: var(--primary); }

/* Attachment strip: pending uploads shown above the composer */
.attach-strip {
  list-style: none; margin: 0;
  padding: 8px 12px 0;
  display: flex; flex-wrap: wrap; gap: 8px;
  background: var(--bg);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.attach-chip {
  display: flex; align-items: center; gap: 8px;
  max-width: 100%;
  padding: 4px 6px 4px 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elev);
  font-size: 12px;
}
.attach-chip img {
  width: 32px; height: 32px; border-radius: 4px;
  object-fit: cover; display: block; flex-shrink: 0;
}
.attach-chip .attach-glyph {
  width: 32px; height: 32px; border-radius: 4px;
  display: grid; place-items: center; flex-shrink: 0;
  background: var(--bg-elev-2); color: var(--text-muted);
}
.attach-chip .attach-name {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  max-width: 150px;
}
.attach-chip .attach-size { color: var(--text-dim); flex-shrink: 0; }
.attach-chip[data-state="uploading"] { opacity: 0.6; }
.attach-chip[data-state="error"] { border-color: rgba(248,113,113,0.5); }
.attach-remove {
  border: none; background: transparent; color: var(--text-dim);
  cursor: pointer; padding: 2px 4px; line-height: 1;
  display: grid; place-items: center; flex-shrink: 0;
}
.attach-remove:hover { color: var(--text); }

/* Stop button: replaces send while a turn is running */
.btn-stop {
  width: 40px; height: 40px;
  background: var(--bg-elev);
  border: 1px solid var(--border-strong);
  color: var(--error);
}
.btn-stop:hover { background: var(--bg-elev-2); }

/* Approval sheet */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 12px;
  padding-bottom: calc(12px + var(--safe-bottom));
}
.sheet-backdrop[hidden] { display: none !important; }
.sheet {
  width: 100%; max-width: 520px;
  max-height: 82dvh; overflow-y: auto;
  background: var(--bg-elev);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 16px;
  display: flex; flex-direction: column; gap: 10px;
}
.sheet-head { display: flex; flex-direction: column; gap: 6px; }
.sheet-badge {
  align-self: flex-start;
  padding: 2px 8px; border-radius: 10px;
  background: rgba(245, 158, 11, 0.15); color: var(--warn);
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em;
}
.sheet-title { margin: 0; font-size: 16px; font-weight: 600; }
.sheet-reason { margin: 0; }
.sheet-code {
  margin: 0; padding: 10px 12px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px; line-height: 1.5;
  white-space: pre-wrap; overflow-wrap: anywhere;
  max-height: 30dvh; overflow-y: auto;
}
.sheet-meta {
  margin: 0; display: grid;
  grid-template-columns: auto 1fr; gap: 4px 10px;
  font-size: 12px;
}
.sheet-meta dt { color: var(--text-dim); }
.sheet-meta dd {
  margin: 0; color: var(--text-muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.sheet-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 2px; }
.sheet-actions .btn { width: 100%; justify-content: center; min-height: 42px; }

/* Thread origin badge + open-in-desktop button */
.thread-row { display: flex; align-items: center; gap: 6px; min-width: 0; }
.thread-origin {
  display: inline-flex; align-items: center; justify-content: center;
  width: 16px; height: 16px; flex-shrink: 0;
  font-size: 12px; line-height: 1;
  color: var(--text-dim);
}
.thread-origin[data-origin="pwa"] { color: var(--primary); }
.thread-origin[data-origin="desktop"] { color: var(--info); }
.thread-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-open-desktop {
  background: transparent; border: none; color: var(--text-dim);
  padding: 2px 4px; border-radius: 4px; cursor: pointer;
  display: none; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.thread-item:hover .thread-open-desktop,
.thread-open-desktop:focus-visible { display: inline-flex; }
.thread-open-desktop:hover { color: var(--text); background: var(--bg-elev); }
