:root {
  --bg: #0d1117;
  --surface: #161b22;
  --surface2: #1f2630;
  --border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #388bfd;
  --green: #238636;
  --green-hi: #3fb950;
  --red: #da3633;
  --amber: #d29922;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans JP', sans-serif;
  font-size: 15px; line-height: 1.5;
}
.hidden { display: none !important; }

/* Login */
.login-screen {
  min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 20px;
}
.login-card {
  width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 24px;
}
.brand-badge {
  display: inline-block; font-size: 12px; color: #79c0ff; background: #1f6feb22;
  border: 1px solid #388bfd55; padding: 4px 10px; border-radius: 99px; margin-bottom: 10px;
}
.login-card h1 { margin: 0 0 8px; font-size: 21px; }
.muted { color: var(--muted); font-size: 13px; margin: 0 0 18px; }
input, textarea {
  width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 11px 12px; font-size: 15px; font-family: inherit;
}
input:focus, textarea:focus { outline: none; border-color: var(--accent); }
.error-box {
  margin-top: 12px; background: #3d191e; border: 1px solid #f85149; color: #ffa198;
  border-radius: 10px; padding: 10px 12px; font-size: 13px;
}

/* Buttons & Pills */
.btn {
  border: 1px solid var(--border); background: var(--surface2); color: var(--text);
  border-radius: 10px; padding: 10px 14px; font-size: 14px; font-weight: 600; cursor: pointer;
  text-decoration: none; display: inline-flex; align-items: center; justify-content: center;
}
.btn.primary { background: var(--green); border-color: #2ea043; color: #fff; }
.btn.danger { background: var(--red); border-color: #f85149; color: #fff; }
.btn.small { padding: 7px 11px; font-size: 13px; border-radius: 8px; }
.btn.full { width: 100%; margin-top: 10px; }
.icon-btn {
  background: transparent; border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; width: 36px; height: 36px; font-size: 18px; cursor: pointer;
}
.pill {
  border-radius: 99px; border: 1px solid var(--border); padding: 5px 10px; font-size: 12px;
  font-weight: 700; cursor: pointer; background: var(--surface2); color: var(--muted);
}
.pill.auto { background: #1f6feb33; border-color: #388bfd; color: #79c0ff; }
.pill.confirm { background: #d2992233; border-color: #d29922; color: #e3b341; }
.pill.on { background: #23863644; border-color: #3fb950; color: #7ee787; }

/* App Shell */
.app {
  display: flex; flex-direction: column; height: 100dvh; max-width: 900px; margin: 0 auto;
  position: relative; overflow: hidden;
}
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px 12px; background: var(--surface); border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.title-wrap { min-width: 0; flex: 1; }
.session-title { font-weight: 700; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.live-status { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.live-status.busy { color: var(--green-hi); }
.topbar-right { display: flex; gap: 6px; flex-shrink: 0; }

/* Drawer */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 40; }
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; width: min(86vw, 340px); z-index: 50;
  background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column;
  padding: 14px; gap: 12px;
}
.drawer-head { display: flex; justify-content: space-between; align-items: center; font-weight: 700; }
.drawer-cwd label { display: block; font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.cwd-row { display: flex; gap: 6px; }
.cwd-row input { padding: 8px 10px; font-size: 13px; }
.session-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.sess-item {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 10px; border-radius: 10px; background: var(--bg); border: 1px solid var(--border); cursor: pointer;
}
.sess-item.active { border-color: var(--accent); background: #1f6feb1a; }
.sess-meta { min-width: 0; flex: 1; }
.sess-name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sess-sub { font-size: 11px; color: var(--muted); }
.sess-del { background: transparent; border: none; color: var(--muted); font-size: 16px; cursor: pointer; padding: 4px; }

/* Tabs */
.tab-panel { display: none; flex: 1; min-height: 0; flex-direction: column; position: relative; }
.tab-panel.active { display: flex; }
.panel-scroll { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 12px; }

/* Chat Timeline */
.timeline { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 94%; border-radius: 14px; padding: 10px 13px; font-size: 14px; word-break: break-word; }
.msg.user { align-self: flex-end; background: #1f6feb; color: #fff; border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; width: 100%; }
.msg.error { align-self: stretch; background: #3d191e; border: 1px solid #f85149; color: #ffa198; }
.msg-foot { margin-top: 6px; font-size: 11px; color: var(--muted); display: flex; justify-content: space-between; }

details.trace {
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 7px 10px; font-size: 12px;
}
details.trace summary { cursor: pointer; color: #79c0ff; font-weight: 600; list-style: none; }
details.trace.think summary { color: #d2a8ff; }
details.trace pre { margin: 8px 0 0; white-space: pre-wrap; color: var(--muted); font-family: ui-monospace, Consolas, monospace; font-size: 12px; overflow-x: auto; }

.md p { margin: 0 0 8px; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: 4px 0 8px; padding-left: 20px; }
.md code { background: #21262d; padding: 2px 5px; border-radius: 5px; font-family: ui-monospace, Consolas, monospace; font-size: 13px; }
.code-wrap { position: relative; margin: 8px 0; }
.code-wrap pre {
  background: #0b0f14; border: 1px solid var(--border); border-radius: 10px;
  padding: 28px 10px 10px; overflow-x: auto; font-family: ui-monospace, Consolas, monospace; font-size: 12px; margin: 0;
}
.copy-code {
  position: absolute; top: 5px; right: 6px; font-size: 11px; padding: 3px 7px; border-radius: 6px;
  background: var(--surface2); border: 1px solid var(--border); color: var(--text); cursor: pointer;
}

/* Approval Banner */
.approval-banner {
  margin: 0 12px 8px; padding: 12px; background: #2d210b; border: 1px solid var(--amber); border-radius: 12px;
}
.apr-head { font-weight: 700; color: #f2cc60; margin-bottom: 6px; font-size: 14px; }
.apr-args {
  margin: 0 0 10px; padding: 8px; background: var(--bg); border-radius: 8px;
  max-height: 120px; overflow: auto; font-size: 12px; white-space: pre-wrap;
}
.apr-actions { display: flex; gap: 8px; }
.apr-actions .btn { flex: 1; }

/* Composer & Attachments */
.composer {
  display: flex; gap: 8px; padding: 10px 12px; background: var(--surface); border-top: 1px solid var(--border);
  align-items: flex-end; flex-shrink: 0;
}
.composer textarea { max-height: 120px; resize: none; line-height: 1.4; }
.composer .btn { height: 42px; flex-shrink: 0; }
.attach-btn { height: 42px; width: 42px; flex-shrink: 0; font-size: 19px; background: var(--surface2); }
.attach-btn:hover { border-color: var(--accent); }

.pending-atts {
  display: flex; gap: 8px; flex-wrap: nowrap; overflow-x: auto; padding: 8px 12px;
  background: var(--surface); border-top: 1px solid var(--border); flex-shrink: 0;
}
.att-chip {
  display: inline-flex; align-items: center; gap: 8px; background: var(--bg);
  border: 1px solid var(--border); border-radius: 10px; padding: 5px 8px; max-width: 240px; flex-shrink: 0;
}
.att-chip.uploading { border-color: var(--accent); opacity: 0.85; }
.att-thumb { width: 38px; height: 38px; object-fit: cover; border-radius: 6px; flex-shrink: 0; }
.att-icon { width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; font-size: 20px; flex-shrink: 0; }
.att-info { min-width: 0; display: flex; flex-direction: column; }
.att-name { font-size: 12px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 145px; }
.att-sub { font-size: 10px; color: var(--muted); }
.att-rm {
  background: transparent; border: none; color: var(--muted); cursor: pointer; font-size: 14px;
  padding: 2px 4px; flex-shrink: 0;
}
.att-rm:hover { color: #ffa198; }

.msg-atts { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 6px; }
.msg-atts:last-child { margin-bottom: 0; }
.msg-att-img {
  max-width: 220px; max-height: 220px; border-radius: 10px; border: 1px solid rgba(255,255,255,0.22);
  object-fit: cover; cursor: pointer; display: block;
}
.msg-att-file {
  display: inline-flex; align-items: center; gap: 6px; background: rgba(0,0,0,0.25);
  border: 1px solid rgba(255,255,255,0.22); color: #fff; text-decoration: none;
  border-radius: 9px; padding: 6px 10px; font-size: 12px;
}
.msg-att-file:hover { background: rgba(0,0,0,0.4); }
.msg-text { white-space: pre-wrap; }

.drop-overlay {
  position: absolute; inset: 0; z-index: 30; background: rgba(13, 17, 23, 0.82);
  display: flex; align-items: center; justify-content: center; padding: 20px; pointer-events: none;
}
.drop-box {
  border: 2px dashed var(--accent); border-radius: 16px; padding: 28px 36px;
  background: rgba(31, 111, 235, 0.14); color: #79c0ff; font-weight: 700; font-size: 16px; text-align: center;
}

.img-modal {
  position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,0.92);
  display: flex; flex-direction: column; padding: 12px; gap: 10px;
}
.img-modal-bar { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.img-modal-title { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.img-modal-full { max-width: 100%; max-height: calc(100dvh - 70px); object-fit: contain; margin: auto; border-radius: 8px; }

/* Cards & Meters */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px; }
.card h2 { margin: 0; font-size: 15px; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; gap: 8px; }
.btn-row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.btn-row.compact { margin-top: 0; }
.kv-list { font-size: 13px; display: grid; gap: 4px; color: var(--muted); }
.kv-list strong { color: var(--text); }
.meter { margin-bottom: 10px; }
.meter-lbl { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 4px; }
.bar { height: 8px; background: var(--bg); border-radius: 99px; overflow: hidden; border: 1px solid var(--border); }
.bar > div { height: 100%; background: var(--accent); }
.screen-img { width: 100%; border-radius: 10px; border: 1px solid var(--border); margin-top: 8px; }
.pre-box {
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 10px;
  font-family: ui-monospace, Consolas, monospace; font-size: 12px; white-space: pre-wrap;
  word-break: break-word; max-height: 280px; overflow: auto; margin: 8px 0 0;
}
.proc-list { display: grid; gap: 4px; font-size: 12px; }
.proc-row { display: flex; justify-content: space-between; padding: 4px 0; border-bottom: 1px solid #21262d; }
.field-lbl { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.ctx-area { font-family: ui-monospace, Consolas, monospace; font-size: 12px; }

/* Files */
.shortcut-row { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.chip {
  background: var(--surface2); border: 1px solid var(--border); color: var(--text);
  border-radius: 99px; padding: 5px 10px; font-size: 12px; cursor: pointer;
}
.path-bar { font-family: ui-monospace, Consolas, monospace; font-size: 12px; color: var(--muted); margin-bottom: 8px; word-break: break-all; }
.file-list { display: flex; flex-direction: column; gap: 4px; }
.file-row {
  display: flex; justify-content: space-between; align-items: center; padding: 9px 10px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 9px; cursor: pointer; font-size: 13px;
}
.file-sz { font-size: 11px; color: var(--muted); flex-shrink: 0; margin-left: 8px; }

/* Bottom Nav */
.bottom-nav {
  display: grid; grid-template-columns: repeat(4, 1fr); background: var(--surface);
  border-top: 1px solid var(--border); padding-bottom: env(safe-area-inset-bottom); flex-shrink: 0;
}
.nav-btn {
  background: transparent; border: none; color: var(--muted); padding: 8px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 16px; cursor: pointer;
}
.nav-btn span { font-size: 11px; font-weight: 600; }
.nav-btn.active { color: #79c0ff; }
