:root {
  --bg: #0f1115;
  --bg2: #151920;
  --bg3: #1d222c;
  --border: #262c38;
  --border-soft: #1f2530;
  --fg: #e8eaf0;
  --muted: #8b93a5;
  --accent: #d97757;
  --accent2: #e08d70;
  --green: #55b586;
  --red: #e05c5c;
  --yellow: #d9a557;
  --r-sm: 7px;
  --r-md: 10px;
  --r-lg: 14px;
  font-size: 15px;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
::selection { background: rgba(217, 119, 87, 0.35); }
* { scrollbar-width: thin; scrollbar-color: var(--bg3) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb { background: var(--bg3); border-radius: 4px; }
*::-webkit-scrollbar-track { background: transparent; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 0.8rem; }
.error { color: var(--red); }
h2 { letter-spacing: -0.015em; }

button {
  font: inherit; border: 1px solid var(--border); border-radius: var(--r-sm);
  background: var(--bg3); color: var(--fg); padding: 6px 12px; cursor: pointer;
  transition: border-color 0.12s, background 0.12s, color 0.12s;
}
button:hover { border-color: var(--accent); }
button:active { transform: translateY(0.5px); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: var(--accent2); border-color: var(--accent2); }
button.danger { background: var(--red); border-color: var(--red); color: #fff; }
button.ghost { background: transparent; }
button.tiny { padding: 3px 10px; font-size: 0.76rem; border-radius: 6px; }
button.big { width: 100%; padding: 10px; margin-bottom: 8px; }
.icon-btn {
  background: none; border: none; padding: 5px; color: var(--muted);
  display: inline-flex; align-items: center; justify-content: center; border-radius: var(--r-sm);
}
.icon-btn:hover { color: var(--fg); background: var(--bg3); }
.icon-btn svg { width: 19px; height: 19px; }
.icon-btn.framed { border: 1px solid var(--border); }
.icon-btn.on { color: var(--accent); }

input, select, textarea {
  font: inherit; background: var(--bg2); color: var(--fg);
  border: 1px solid var(--border); border-radius: var(--r-sm); padding: 7px 10px;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); }

.wordmark {
  display: inline-flex; align-items: center; gap: 9px;
  font-size: 1.14rem; font-weight: 650; letter-spacing: -0.01em;
}
.logo { width: 21px; height: 21px; color: var(--accent); flex-shrink: 0; }
.logo-big { width: 56px; height: 56px; color: var(--accent); }

.layout { display: flex; height: 100vh; height: 100dvh; }

/* ---------- sidebar ---------- */
.sidebar {
  width: 300px; min-width: 300px; background: var(--bg2);
  border-right: 1px solid var(--border-soft);
  display: flex; flex-direction: column; padding: 14px; gap: 4px;
  overflow-y: auto;
}
.brand { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; cursor: pointer; }
.section-title {
  font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.09em;
  color: var(--muted); margin: 14px 0 6px;
}
.section-title.row { display: flex; justify-content: space-between; align-items: center; }
.session-list { flex: 1; min-height: 120px; }
.session-item {
  padding: 8px 10px; border-radius: var(--r-sm); cursor: pointer; margin-bottom: 3px;
  border: 1px solid transparent; position: relative;
  transition: background 0.12s;
}
.session-item:hover { background: var(--bg3); }
.session-item.active { background: var(--bg3); border-color: var(--border); }
.session-item .name { font-weight: 600; display: flex; align-items: center; gap: 7px; min-width: 0; }
.session-item .name > span:nth-child(2) { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-item .cwd { font-size: 0.72rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 1px; }
.session-item .close-x {
  position: absolute; right: 4px; top: 5px; color: var(--muted);
  font-size: 0.85rem; padding: 2px 6px; border: none; background: none; opacity: 0;
}
.session-item:hover .close-x { opacity: 1; }
.session-item .close-x:hover { color: var(--red); }
.dot { width: 7px; height: 7px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.dot.idle { background: var(--green); }
.dot.working { background: var(--yellow); animation: pulse 1.3s ease-in-out infinite; }
.dot.starting { background: var(--yellow); }
.dot.stopped { background: #3a4150; }
@keyframes pulse { 50% { opacity: 0.25; } }
.perm-badge {
  background: var(--yellow); color: #14181f; border-radius: 9px; font-size: 0.66rem;
  font-weight: 700; padding: 1px 7px; margin-left: auto; flex-shrink: 0;
}

/* ---------- usage ---------- */
.usage-card { border-top: 1px solid var(--border-soft); padding-top: 8px; }
.rl-row { margin-bottom: 8px; }
.rl-label { display: flex; justify-content: space-between; font-size: 0.76rem; margin-bottom: 3px; font-variant-numeric: tabular-nums; }
.rl-bar { height: 6px; background: var(--bg3); border-radius: 3px; overflow: hidden; }
.rl-fill { height: 100%; background: var(--green); border-radius: 3px; transition: width 0.4s; }
.rl-fill.warn { background: var(--yellow); }
.rl-fill.hot { background: var(--red); }
.usage-tokens { font-size: 0.76rem; color: var(--muted); margin: 8px 0; line-height: 1.55; font-variant-numeric: tabular-nums; }
.usage-tokens b { color: var(--fg); font-weight: 600; }

/* ---------- main / chat ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.empty-state { margin: 12vh auto 0; text-align: center; color: var(--muted); line-height: 1.6; }
.empty-state .logo-big { margin-bottom: 14px; opacity: 0.9; }
.chat-view { display: flex; flex-direction: column; height: 100%; }
.chat-header {
  display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 8px;
  padding: 10px 16px; border-bottom: 1px solid var(--border-soft); background: var(--bg2);
}
.chat-title { display: flex; flex-direction: column; min-width: 0; }
#chat-name { font-weight: 650; cursor: pointer; border-radius: 4px; padding: 0 2px; }
#chat-name:hover { background: var(--bg3); }
#chat-name[contenteditable="true"] { background: var(--bg3); outline: 1px solid var(--accent); }
.chat-controls { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.chat-controls select { font-size: 0.78rem; padding: 4px 7px; color: var(--muted); }
.chat-controls select:hover { color: var(--fg); }
.state-badge { font-size: 0.73rem; padding: 2px 10px; border-radius: 9px; background: var(--bg3); color: var(--muted); }
.state-badge.working { color: var(--yellow); }
.state-badge.idle { color: var(--green); }

.messages { flex: 1; overflow-y: auto; padding: 18px 20px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 860px; line-height: 1.55; word-wrap: break-word; }
.msg.user {
  align-self: flex-end; background: var(--bg3); border: 1px solid var(--border-soft);
  padding: 9px 14px; border-radius: var(--r-lg); border-bottom-right-radius: 4px; white-space: pre-wrap;
}
.msg.assistant { align-self: flex-start; }
.msg.assistant p { margin: 0.4em 0; }
.msg.assistant pre {
  background: var(--bg2); border: 1px solid var(--border-soft); border-radius: var(--r-sm);
  padding: 10px 12px; overflow-x: auto; font-size: 0.84rem;
}
.msg.assistant code { background: var(--bg2); padding: 1px 5px; border-radius: 4px; font-size: 0.88em; }
.msg.assistant pre code { background: none; padding: 0; }
.msg.thinking {
  align-self: flex-start; color: var(--muted); font-style: italic; font-size: 0.84rem;
  border-left: 2px solid var(--border); padding-left: 10px; white-space: pre-wrap;
  max-height: 88px; overflow: hidden; cursor: pointer;
}
.msg.thinking.expanded { max-height: none; }
.msg.sysline { align-self: center; color: var(--muted); font-size: 0.76rem; }
.msg.errline { align-self: center; color: var(--red); font-size: 0.8rem; }

.tool-chip {
  align-self: flex-start; background: var(--bg2); border: 1px solid var(--border-soft);
  border-radius: var(--r-sm); padding: 6px 10px; font-size: 0.8rem; max-width: 860px; width: fit-content;
}
.tool-chip .tool-head { cursor: pointer; display: flex; gap: 8px; align-items: center; }
.tool-chip .tool-status { font-size: 0.9em; width: 1em; text-align: center; color: var(--muted); }
.tool-chip .tool-status.ok { color: var(--green); }
.tool-chip .tool-status.err { color: var(--red); }
.tool-chip .tool-name { color: var(--accent); font-weight: 600; }
.tool-chip .tool-head .muted { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 480px; }
.tool-chip .tool-body { display: none; margin-top: 6px; }
.tool-chip.open .tool-body { display: block; }
.tool-chip pre {
  margin: 4px 0; background: var(--bg); border-radius: 6px; padding: 8px 10px;
  overflow-x: auto; max-height: 260px; overflow-y: auto; font-size: 0.76rem; white-space: pre-wrap;
}
.tool-chip pre.diff-del { background: rgba(224, 92, 92, 0.1); color: #eda6a6; }
.tool-chip pre.diff-add { background: rgba(85, 181, 134, 0.1); color: #a3d9bd; }
.result-line { align-self: flex-start; color: var(--muted); font-size: 0.74rem; padding: 2px 0 8px; font-variant-numeric: tabular-nums; }

/* ---------- suggestion / permission bars ---------- */
.suggestion-bar {
  display: flex; align-items: center; gap: 8px; padding: 6px 16px;
  border-top: 1px solid var(--border-soft); background: var(--bg2);
}
.sug-label { color: var(--muted); font-size: 0.74rem; }
.sug-chip {
  background: var(--bg3); border: 1px dashed var(--border); color: var(--muted);
  border-radius: 13px; font-size: 0.79rem; max-width: 90%; text-align: left;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.sug-chip:hover { color: var(--fg); border-color: var(--accent); }

.permission-bar {
  border-top: 2px solid var(--yellow); background: #221e17; padding: 12px 16px;
}
.perm-req { margin-bottom: 8px; }
.perm-req .perm-title { font-weight: 650; color: var(--yellow); margin-bottom: 4px; }
.perm-req pre {
  background: var(--bg); border-radius: 6px; padding: 8px 10px; max-height: 160px;
  overflow: auto; font-size: 0.76rem; white-space: pre-wrap; margin: 4px 0 8px;
}
.perm-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- composer ---------- */
.composer-wrap { border-top: 1px solid var(--border-soft); background: var(--bg2); }
.composer { display: flex; gap: 10px; padding: 12px 16px; align-items: flex-end; }
.composer textarea { flex: 1; resize: vertical; min-height: 44px; max-height: 300px; }
.composer-buttons { display: flex; flex-direction: column; gap: 6px; }
.attachments { display: flex; gap: 8px; padding: 8px 16px 0; flex-wrap: wrap; }
.attach-chip { position: relative; }
.attach-chip img { height: 52px; border-radius: 6px; border: 1px solid var(--border); display: block; }
.attach-chip span {
  position: absolute; top: -6px; right: -6px; background: var(--red); color: #fff;
  border-radius: 50%; width: 18px; height: 18px; font-size: 0.7rem;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}

.cmd-palette {
  max-height: 260px; overflow-y: auto; margin: 8px 16px 0;
  border: 1px solid var(--border); border-radius: var(--r-md); background: var(--bg3);
}
.cmd-item {
  padding: 7px 12px; cursor: pointer; font-size: 0.83rem;
  display: flex; gap: 8px; align-items: baseline; border-bottom: 1px solid var(--border-soft);
}
.cmd-item:last-child { border-bottom: none; }
.cmd-item:hover { background: var(--bg2); }
.cmd-item b { color: var(--accent); font-weight: 600; }
.cmd-desc { color: var(--muted); font-size: 0.77rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- modals ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(6, 8, 11, 0.65);
  display: flex; align-items: center; justify-content: center; z-index: 50;
  backdrop-filter: blur(2px);
}
.modal {
  background: var(--bg2); border: 1px solid var(--border); border-radius: var(--r-lg);
  padding: 22px; width: 360px; max-width: 92vw; display: flex; flex-direction: column; gap: 10px;
  max-height: 88vh; max-height: 88dvh; overflow-y: auto;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
}
.modal.wide { width: 560px; }
.modal h2 { margin: 0 0 4px; font-size: 1.15rem; }
.modal label { display: flex; flex-direction: column; gap: 4px; font-size: 0.83rem; color: var(--muted); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.login-modal { align-items: stretch; text-align: center; gap: 14px; padding: 28px; }
.login-modal .wordmark { flex-direction: column; justify-content: center; gap: 12px; font-size: 1.4rem; margin-bottom: 4px; }
.login-modal .logo { width: 52px; height: 52px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.checkline { flex-direction: row !important; align-items: center; gap: 8px !important; }
.checkline input { width: auto; }
.dir-picker { border: 1px solid var(--border); border-radius: var(--r-sm); overflow: hidden; }
.dir-path { background: var(--bg3); padding: 7px 10px; font-size: 0.8rem; font-family: ui-monospace, monospace; }
.dir-list { max-height: 200px; overflow-y: auto; }
.dir-list > div { padding: 6px 12px; cursor: pointer; font-size: 0.85rem; display: flex; align-items: center; gap: 8px; }
.dir-list > div:hover { background: var(--bg3); }
.dir-list > div.up { color: var(--muted); }
.dir-list svg { width: 15px; height: 15px; color: var(--muted); flex-shrink: 0; }

.import-list { max-height: 340px; overflow-y: auto; border: 1px solid var(--border); border-radius: var(--r-sm); }
.import-item { padding: 9px 12px; cursor: pointer; border-bottom: 1px solid var(--border-soft); }
.import-item:last-child { border-bottom: none; }
.import-item:hover { background: var(--bg3); }
.import-title { font-size: 0.87rem; font-weight: 600; margin-bottom: 2px; }

.usage-table { width: 100%; border-collapse: collapse; font-size: 0.81rem; font-variant-numeric: tabular-nums; }
.usage-table th, .usage-table td { text-align: right; padding: 5px 8px; border-bottom: 1px solid var(--border-soft); }
.usage-table th { color: var(--muted); font-weight: 500; font-size: 0.73rem; text-transform: uppercase; letter-spacing: 0.05em; }
.usage-table th:first-child, .usage-table td:first-child { text-align: left; }
.chart { display: flex; align-items: flex-end; gap: 4px; height: 100px; margin: 8px 0 18px; }
.chart-col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; text-align: center; }
.chart-bar { background: var(--accent); border-radius: 3px 3px 0 0; min-height: 3px; opacity: 0.85; }
.chart-col:hover .chart-bar { opacity: 1; }
.chart-label { font-size: 0.62rem; color: var(--muted); margin-top: 3px; }

/* ---------- dashboard ---------- */
.dashboard { flex: 1; overflow-y: auto; padding: 22px 26px; }
.dash-header { display: flex; align-items: baseline; gap: 14px; margin-bottom: 14px; }
.dash-header h2 { margin: 0; font-size: 1.25rem; }
.tasks-header { margin-top: 30px; }
.tasks-header button { margin-left: 8px; align-self: center; }
.dash-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(310px, 1fr)); gap: 13px; }
.dash-card {
  background: var(--bg2); border: 1px solid var(--border-soft); border-radius: var(--r-md);
  padding: 13px 14px; cursor: pointer; display: flex; flex-direction: column; gap: 8px;
  transition: border-color 0.13s;
}
.dash-card > * { min-width: 0; }
.dash-card:hover { border-color: var(--accent); }
.dash-card.st-working { border-left: 3px solid var(--yellow); }
.dash-card.st-idle { border-left: 3px solid var(--green); }
.dash-card.st-stopped { border-left: 3px solid var(--border); opacity: 0.72; }
.dash-card.needs-attention { border-color: var(--yellow); }
.dash-card-head { display: flex; align-items: center; gap: 8px; }
.dash-name { font-weight: 650; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dash-state {
  font-size: 0.68rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.07em;
  white-space: nowrap; flex-shrink: 0; font-variant-numeric: tabular-nums;
}
.dash-cwd { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dash-badges { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  font-size: 0.69rem; background: var(--bg3); border: 1px solid var(--border-soft);
  border-radius: 9px; padding: 1px 8px; color: var(--muted);
}
.badge-warn { color: var(--red); border-color: rgba(224, 92, 92, 0.5); }
.dash-perm {
  font-size: 0.79rem; color: var(--yellow); background: rgba(217, 165, 87, 0.09);
  border-radius: 6px; padding: 6px 9px;
}
.dash-activity {
  font-size: 0.78rem; color: var(--fg); background: var(--bg);
  border-radius: 6px; padding: 7px 9px; font-family: ui-monospace, "SF Mono", monospace;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-height: 1.2em;
}
.task-lastrun {
  font-size: 0.77rem; color: var(--green); background: rgba(85, 181, 134, 0.07);
  border-radius: 6px; padding: 5px 9px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.task-lastrun.task-fail { color: var(--red); background: rgba(224, 92, 92, 0.09); }
.dash-foot { display: flex; justify-content: space-between; align-items: center; margin-top: auto; gap: 8px; }
.dash-actions { display: flex; gap: 6px; flex-shrink: 0; }

/* ================= responsive ================= */
.topbar { display: none; }
.drawer-backdrop { display: none; }
.brand-mini { font-weight: 650; flex: 1; text-align: center; letter-spacing: -0.01em; }

@media (min-width: 901px) and (max-width: 1200px) {
  .sidebar { width: 258px; min-width: 258px; }
  .dash-grid { grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); }
  .dashboard { padding: 18px; }
  .chat-controls select { max-width: 150px; }
}

@media (max-width: 900px) {
  :root { font-size: 16px; }

  .topbar {
    display: flex; position: fixed; top: 0; left: 0; right: 0; z-index: 30;
    align-items: center; gap: 6px;
    background: var(--bg2); border-bottom: 1px solid var(--border-soft);
    padding: calc(6px + env(safe-area-inset-top)) 10px 6px;
  }

  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 40;
    width: min(85vw, 320px); min-width: 0;
    transform: translateX(-105%); transition: transform 0.2s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.5);
    padding-top: calc(14px + env(safe-area-inset-top));
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
  }
  body.drawer-open .sidebar { transform: none; }
  body.drawer-open .drawer-backdrop {
    display: block; position: fixed; inset: 0; z-index: 35;
    background: rgba(6, 8, 11, 0.55);
  }

  .main { padding-top: calc(44px + env(safe-area-inset-top)); }

  .dashboard { padding: 14px 12px calc(14px + env(safe-area-inset-bottom)); }
  .dash-grid { grid-template-columns: minmax(0, 1fr); }
  .dash-header { flex-wrap: wrap; gap: 8px; }

  .chat-header { padding: 8px 10px; gap: 6px; }
  .chat-controls { width: 100%; }
  .chat-controls select { flex: 1; min-width: 0; max-width: none; font-size: 0.76rem; }
  .messages { padding: 12px 10px; }
  .msg.user { max-width: 88%; }
  .composer { padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); gap: 8px; }
  .composer textarea { min-height: 42px; }
  .composer-buttons { flex-direction: row; flex-wrap: wrap; align-content: flex-end; }
  .cmd-palette { margin: 8px 10px 0; }
  .suggestion-bar { padding: 6px 10px; }
  .permission-bar { padding: 10px; max-height: 45vh; overflow-y: auto; }
  .session-item .close-x { opacity: 1; }

  button { padding: 9px 14px; }
  button.tiny { padding: 5px 10px; }
  .modal { width: 94vw; padding: 16px; max-height: 86dvh; }
  .grid3 { grid-template-columns: 1fr; }
  .dir-list { max-height: 34dvh; }
  .import-list { max-height: 46dvh; }
}

@media (max-width: 380px) {
  .chat-controls select { font-size: 0.71rem; }
  .dash-badges .badge { font-size: 0.64rem; }
}
