:root {
  --bg: #0a0c10;
  --panel: #12151c;
  --panel-2: #171b24;
  --border: #222838;
  --text: #e8eaf0;
  --text-dim: #8b93a7;
  --gold: #e8c15a;
  --gold-dim: #b3923c;
  --green: #4ade80;
  --red: #f87171;
  --blue: #6ea8fe;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

.hidden { display: none !important; }

/* ── Login ─────────────────────────────── */
.login {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(120% 90% at 50% 0%, #141a26 0%, var(--bg) 60%);
  padding: 24px;
}
.login-card {
  width: 100%; max-width: 360px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 20px; padding: 32px 24px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.login-mark {
  font-family: ui-monospace, "Cascadia Code", monospace;
  font-size: 30px; color: var(--gold); margin-bottom: 12px;
}
.login-card h1 { font-size: 22px; letter-spacing: .5px; }
.login-sub { color: var(--text-dim); font-size: 13px; margin: 8px 0 22px; line-height: 1.5; }
#login-form { display: flex; flex-direction: column; gap: 12px; }
#login-pass {
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  color: var(--text); padding: 13px 14px; font-size: 16px; outline: none;
}
#login-pass:focus { border-color: var(--gold-dim); }
.login-err { color: var(--red); font-size: 13px; margin-top: 12px; min-height: 18px; }

.btn {
  border: none; border-radius: 10px; padding: 13px; font-size: 15px; font-weight: 600;
  cursor: pointer;
}
.btn.gold { background: var(--gold); color: #1a1405; }
.btn.gold:active { transform: scale(.98); }

/* ── App frame ─────────────────────────── */
.app {
  height: 100dvh; display: flex; flex-direction: column;
  padding-top: var(--safe-top); padding-bottom: var(--safe-bottom);
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 8px; }
.brand-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--green); box-shadow: 0 0 8px var(--green); }
.brand-name { font-weight: 700; letter-spacing: .4px; font-size: 16px; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }

.chip {
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 999px; padding: 7px 12px; font-size: 13px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 7px; white-space: nowrap;
}
.chip:active { background: #1d2330; }
.chip-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--gold-dim); }
.model-chip { max-width: 46vw; overflow: hidden; text-overflow: ellipsis; }
.newchat-btn { width: 34px; height: 34px; font-size: 17px; }

/* ── RD pixel icons ─────────────────────── */
.ic { width: 18px; height: 18px; display: inline-block; vertical-align: -3px; image-rendering: pixelated; }
.ic.inline { margin-right: 6px; vertical-align: -4px; }
.ic.sm { width: 13px; height: 13px; vertical-align: -2px; }
.icon-btn .ic { pointer-events: none; }
.push-btn.on img { filter: drop-shadow(0 0 4px rgba(232, 193, 90, 0.9)); opacity: 1; }
.push-btn { opacity: 0.35; }
.push-btn.on { opacity: 1; }
.tab-ic .ic { width: 20px; height: 20px; vertical-align: middle; }
.welcome-mark-img { width: 76px; height: 76px; image-rendering: pixelated; }
.msg.err .bubble::before {
  content: ""; display: inline-block; width: 15px; height: 15px;
  margin-right: 6px; vertical-align: -2px;
  background: url("/static/icons/hc_warn.png") center/contain no-repeat;
  image-rendering: pixelated;
}

/* ── Markdown in chat ────────────────────── */
.bubble .md-p { margin: 2px 0; }
.queued-tag {
  display: inline-block; margin-left: 8px; padding: 1px 7px;
  border: 1px dashed var(--gold-dim); border-radius: 8px;
  color: var(--gold); font-size: 10.5px; font-style: italic;
  vertical-align: 1px;
}
.bubble .md-h { margin: 8px 0 4px; font-weight: 700; }
.bubble .md-h1 { font-size: 16px; }
.bubble .md-h2 { font-size: 15px; }
.bubble .md-h3 { font-size: 14px; }
.bubble .md-code {
  background: #0d0f14; border: 1px solid var(--border); color: #9be3ff;
  padding: 1px 5px; border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px;
}
.bubble pre.md-code { display: block; padding: 8px 10px; margin: 6px 0; overflow-x: auto; white-space: pre; }
.bubble pre.md-code .md-code, .bubble pre.md-code code { background: none; border: 0; padding: 0; }
.bubble .md-li { margin: 2px 0; }
.bubble .md-bullet { color: var(--gold); margin-right: 5px; }
.bubble .md-quote { border-left: 3px solid var(--gold-dim); padding: 2px 8px; margin: 4px 0; color: var(--text-dim); }
.bubble a { color: var(--gold); text-decoration: underline; }
.bubble .md-table-wrap { overflow-x: auto; margin: 6px 0; }
.bubble .md-table { border-collapse: collapse; font-size: 12.5px; min-width: 60%; }
.bubble .md-table th, .bubble .md-table td { border: 1px solid var(--border); padding: 3px 8px; text-align: left; }
.bubble .md-table th { background: var(--panel-2); color: var(--gold); }

/* ── Quick-command chips ─────────────────── */
.chips {
  flex: 0 0 auto;
  display: flex; gap: 8px; overflow-x: auto;
  padding: 6px 12px 2px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.qchip {
  flex: 0 0 auto;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 14px;
  padding: 5px 12px;
  font-size: 12.5px;
  white-space: nowrap;
  transition: border-color .15s, color .15s;
}
.qchip:active { border-color: var(--gold-dim); color: var(--gold); }
.qchip-add { color: var(--gold); border-style: dashed; }
.chip-mgr { display: flex; flex-direction: column; gap: 10px; }
.chip-mgr-hint { font-size: 12px; color: var(--text-dim); }
.chip-mgr-list { display: flex; flex-direction: column; gap: 6px; max-height: 40vh; overflow-y: auto; }
.chip-mgr-row { display: flex; align-items: center; gap: 8px; background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; }
.chip-mgr-info { flex: 1; min-width: 0; }
.chip-mgr-label { font-size: 13.5px; font-weight: 600; }
.chip-mgr-prompt { font-size: 12px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip-mgr-addrow { display: flex; gap: 6px; flex-wrap: wrap; }
.chip-mgr-input { flex: 1 1 40%; min-width: 0; background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; color: var(--text); font-size: 13px; }
.chip-mgr-reset { align-self: flex-start; background: none; border: none; color: var(--text-dim); font-size: 12px; padding: 4px 0; text-decoration: underline; }

/* ── Status tab ──────────────────────────── */
.status-body { flex: 1; overflow-y: auto; padding: 10px 12px 20px; display: flex; flex-direction: column; gap: 10px; }
.st-card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 12px; }
.st-head { display: flex; align-items: center; margin-bottom: 8px; }
.st-title { font-weight: 700; font-size: 14px; }
.st-row { display: flex; justify-content: space-between; align-items: center; padding: 3px 0; font-size: 13px; }
.st-label { color: var(--text-dim); }
.st-value { color: var(--text); display: flex; align-items: center; gap: 6px; }
.st-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.st-dot.ok { background: var(--green); box-shadow: 0 0 6px rgba(74, 222, 128, 0.6); }
.st-dot.bad { background: var(--red); box-shadow: 0 0 6px rgba(239, 68, 68, 0.6); }
.st-list { border-top: 1px solid var(--border); margin-top: 6px; padding-top: 4px; }
.st-cont { font-size: 12.5px; }

/* ── Tabs & panels ──────────────────────── */
.tab-panel {
  flex: 1 1 auto; min-height: 0;
  display: flex; flex-direction: column; overflow: hidden;
}
.panel-head {
  flex: 0 0 auto; display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px 8px;
}
.panel-title-wrap { display: flex; flex-direction: column; }
.panel-title { font-weight: 700; font-size: 16px; }
.panel-sub { font-size: 11.5px; color: var(--text-dim); margin-top: 1px; }
.newchat-wide {
  flex: 0 0 auto; margin: 2px 12px 10px;
  background: var(--panel-2); border: 1px dashed var(--gold-dim); color: var(--gold);
  border-radius: 12px; padding: 12px; font-size: 14px; cursor: pointer;
}
.newchat-wide:active { background: #1d2330; }
.welcome-btn { margin-top: 18px; padding: 12px 22px; font-size: 14px; }

.tabbar {
  flex: 0 0 auto;
  display: flex; align-items: stretch;
  background: var(--panel); border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
}
.tab {
  flex: 1; position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: none; border: none; color: var(--text-dim);
  padding: 9px 4px 8px; font-size: 11px; cursor: pointer;
  font-family: inherit;
}
.tab .tab-ic { font-size: 19px; line-height: 1; }
.tab.active { color: var(--gold); }
.tab.active .tab-ic { filter: drop-shadow(0 0 6px rgba(232,193,90,.45)); }
.tab-badge {
  position: absolute; top: 6px; right: calc(50% - 22px);
  min-width: 8px; height: 8px; border-radius: 50%;
  background: var(--red); box-shadow: 0 0 6px var(--red);
}
.tab-badge.approval { animation: pulse 1.2s ease-in-out infinite; }

/* ── Messages ──────────────────────────── */
.messages {
  flex: 1 1 auto; overflow-y: auto;
  padding: 16px 14px 12px;
  display: flex; flex-direction: column; gap: 14px;
  scroll-behavior: smooth;
}
.welcome { text-align: center; color: var(--text-dim); margin: auto; padding: 40px 20px; }
.welcome-mark { font-size: 40px; color: var(--gold); opacity: .9; margin-bottom: 14px; }
.welcome h2 { color: var(--text); font-size: 18px; margin-bottom: 8px; }
.welcome p { font-size: 13.5px; line-height: 1.6; }

.msg { max-width: 88%; display: flex; flex-direction: column; gap: 6px; }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant { align-self: flex-start; align-items: flex-start; }

.bubble {
  border-radius: var(--radius); padding: 11px 14px; font-size: 15px; line-height: 1.55;
  overflow-wrap: anywhere; white-space: pre-wrap;
}
.msg.user .bubble { background: #1e2634; border: 1px solid #2a3448; border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--panel); border: 1px solid var(--border); border-bottom-left-radius: 4px; }

.msg .attached-img { border-radius: 10px; max-width: 180px; max-height: 180px; object-fit: cover; border: 1px solid var(--border); }

.msg .tools { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px; }
.tool-chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11.5px; color: var(--text-dim);
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 4px 9px; border-radius: 999px;
}
.tool-chip .spin {
  width: 9px; height: 9px; border-radius: 50%;
  border: 2px solid var(--border); border-top-color: var(--gold);
  animation: spin 0.8s linear infinite;
}
.tool-chip.done { color: var(--green); }
@keyframes spin { to { transform: rotate(360deg); } }

.cursor-blink::after { content: "▌"; color: var(--gold); animation: blink 1s steps(1) infinite; margin-left: 1px; }
@keyframes blink { 50% { opacity: 0; } }

.msg.err .bubble { border-color: #5b2b2b; background: #211619; color: var(--red); font-size: 13.5px; }

/* ── Composer ──────────────────────────── */
.composer {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  background: var(--panel);
  padding: 10px 12px calc(10px + var(--safe-bottom) / 2);
}
.composer-row { display: flex; align-items: flex-end; gap: 8px; }
#input {
  flex: 1; resize: none; max-height: 130px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
  color: var(--text); font-size: 15.5px; line-height: 1.45;
  padding: 11px 13px; outline: none; font-family: inherit;
}
#input:focus { border-color: var(--gold-dim); }
.icon-btn {
  width: 42px; height: 42px; flex: 0 0 auto;
  border-radius: 50%; border: 1px solid var(--border);
  background: var(--panel-2); color: var(--text);
  font-size: 18px; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.icon-btn:active { background: #1d2330; }
.icon-btn.send { background: var(--gold); color: #1a1405; border-color: var(--gold); font-size: 16px; }
.icon-btn.send:disabled { opacity: .4; }
.menu-btn { width: 36px; height: 36px; font-size: 17px; }
.attach-preview { position: relative; display: inline-block; margin-bottom: 8px; }
.attach-preview img { width: 74px; height: 74px; object-fit: cover; border-radius: 10px; border: 1px solid var(--border); }
.attach-remove {
  position: absolute; top: -7px; right: -7px;
  width: 22px; height: 22px; border-radius: 50%;
  border: none; background: #3a1620; color: var(--red);
  font-size: 11px; cursor: pointer;
}
.composer-status { font-size: 11.5px; color: var(--text-dim); min-height: 15px; margin-top: 5px; padding-left: 2px; }

/* ── Model sheet ───────────────────────── */
.sheet { position: fixed; inset: 0; z-index: 60; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.55); }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0;
  background: var(--panel); border-top: 1px solid var(--border);
  border-radius: 20px 20px 0 0;
  max-height: 78dvh; display: flex; flex-direction: column;
  padding-bottom: var(--safe-bottom);
}
.sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px 10px; border-bottom: 1px solid var(--border);
}
.sheet-head h3 { font-size: 16px; }
.sheet-body { overflow-y: auto; padding: 12px 14px 20px; }

.provider-group { margin-bottom: 16px; }
.provider-name {
  font-size: 12px; text-transform: uppercase; letter-spacing: 1px;
  color: var(--text-dim); margin: 0 4px 8px;
  display: flex; align-items: center; gap: 8px;
}
.provider-name .prov-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--gold-dim); }
.model-row {
  width: 100%; text-align: left;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 11px;
  color: var(--text); padding: 11px 13px; margin-bottom: 7px;
  font-size: 14.5px; cursor: pointer;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.model-row.active { border-color: var(--gold); }
.model-row:active { background: #1d2330; }
.model-row .badges { display: flex; gap: 5px; }
.badge {
  font-size: 10px; padding: 2px 7px; border-radius: 999px;
  border: 1px solid var(--border); color: var(--text-dim);
}
.badge.fast { color: var(--gold); border-color: var(--gold-dim); }
.badge.reasoning { color: var(--blue); border-color: #3b5c8c; }

.sheet-options { border-top: 1px solid var(--border); margin-top: 14px; padding-top: 14px; }
.opt-label { font-size: 12px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 1px; margin-bottom: 8px; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button {
  flex: 1; min-width: 64px;
  background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 9px; padding: 9px 6px; font-size: 13px; cursor: pointer;
}
.seg button.active { border-color: var(--gold); color: var(--gold); }
.toggle-row { display: flex; align-items: center; justify-content: space-between; margin-top: 14px; }
.toggle-row span { font-size: 14px; }
.switch { position: relative; width: 46px; height: 26px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; inset: 0; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--border); cursor: pointer;
  transition: .15s;
}
.slider::before {
  content: ""; position: absolute; width: 18px; height: 18px; border-radius: 50%;
  left: 3px; top: 3px; background: var(--text-dim); transition: .15s;
}
.switch input:checked + .slider { background: var(--gold-dim); border-color: var(--gold); }
.switch input:checked + .slider::before { transform: translateX(20px); background: #fff; }

/* ── History tab ────────────────────────── */
.drawer-list { flex: 1; overflow-y: auto; padding: 4px 12px 20px; }
.drawer-empty { color: var(--text-dim); font-size: 13.5px; text-align: center; padding: 40px 16px; }
.conv-item {
  width: 100%; text-align: left;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 12px;
  padding: 11px 12px; margin-bottom: 8px; cursor: pointer;
  display: flex; align-items: center; gap: 10px;
}
.conv-item:active { background: #1d2330; }
.conv-item.active { border-color: var(--gold); }
.conv-item .conv-main { flex: 1; min-width: 0; }
.conv-item .conv-title {
  font-size: 14px; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-item .conv-meta { font-size: 11.5px; color: var(--text-dim); margin-top: 3px; }
.conv-del {
  flex: 0 0 auto; width: 28px; height: 28px; border-radius: 50%;
  border: none; background: #3a1620; color: var(--red); font-size: 12px; cursor: pointer;
}
.conv-del:active { background: #4d1d2b; }

/* ── Runs tab ───────────────────────────── */
.rc-launch {
  flex: 0 0 auto; display: flex; align-items: flex-end; gap: 8px;
  padding: 4px 12px 10px; border-bottom: 1px solid var(--border);
}
#rc-input {
  flex: 1; resize: none; max-height: 110px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
  color: var(--text); font-size: 15px; line-height: 1.45;
  padding: 10px 12px; outline: none; font-family: inherit;
}
#rc-input:focus { border-color: var(--gold-dim); }
.rc-list { flex: 1; overflow-y: auto; padding: 10px 12px 20px; display: flex; flex-direction: column; gap: 10px; }

.run-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  overflow: hidden;
}
.run-card .run-card-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 11px 13px; cursor: pointer;
}
.run-card .run-main { flex: 1; min-width: 0; }
.run-card .run-title {
  font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.run-card .run-meta { font-size: 11.5px; color: var(--text-dim); margin-top: 3px; }
.run-status {
  flex: 0 0 auto; font-size: 10.5px; text-transform: uppercase; letter-spacing: .6px;
  padding: 4px 9px; border-radius: 999px; border: 1px solid var(--border); color: var(--text-dim);
}
.run-status.running { color: var(--gold); border-color: var(--gold-dim); }
.run-status.queued { color: var(--blue); border-color: #3b5c8c; }
.run-status.waiting_for_approval { color: var(--red); border-color: #7a2f2f; animation: pulse 1.4s ease-in-out infinite; }
.run-status.completed { color: var(--green); border-color: #2a5c3a; }
.run-status.failed { color: var(--red); border-color: #7a2f2f; }
.run-status.cancelled, .run-status.expired { color: var(--text-dim); }
@keyframes pulse { 50% { opacity: .55; } }

.run-detail { border-top: 1px solid var(--border); padding: 12px 13px; }
.run-output {
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; font-size: 13.5px; line-height: 1.55;
  white-space: pre-wrap; overflow-wrap: anywhere; max-height: 260px; overflow-y: auto;
  font-family: ui-monospace, "Cascadia Code", monospace;
}
.run-log { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.run-approval {
  border: 1px solid #7a2f2f; background: #1c1216; border-radius: 12px;
  padding: 12px; margin-bottom: 10px;
}
.run-approval .ap-title { color: var(--red); font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.run-approval .ap-cmd {
  font-family: ui-monospace, monospace; font-size: 12.5px; color: var(--text);
  background: var(--bg); border-radius: 8px; padding: 8px 10px;
  overflow-wrap: anywhere; white-space: pre-wrap; margin-bottom: 10px;
}
.run-approval .ap-btns { display: flex; gap: 7px; flex-wrap: wrap; }
.ap-btn {
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text);
  border-radius: 9px; padding: 9px 12px; font-size: 13px; cursor: pointer;
}
.ap-btn.approve { background: var(--green); color: #04150a; border-color: var(--green); font-weight: 600; }
.ap-btn.deny { background: #3a1620; color: var(--red); border-color: #7a2f2f; }
.ap-btn:active { opacity: .8; }
.run-actions { display: flex; gap: 8px; margin-top: 10px; }
.run-actions .act-btn {
  border: 1px solid var(--border); background: var(--panel-2); color: var(--text-dim);
  border-radius: 9px; padding: 8px 12px; font-size: 12.5px; cursor: pointer;
}
.run-actions .act-btn.stop { color: var(--red); border-color: #7a2f2f; }
.run-actions .act-btn:active { background: #1d2330; }
.run-err { color: var(--red); font-size: 12.5px; margin-top: 8px; white-space: pre-wrap; overflow-wrap: anywhere; }
