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

:root {
  --bg: #000000;
  --bg2: #1c1c1e;
  --bg3: #2c2c2e;
  --bg4: #3a3a3c;
  --text: #f5f5f7;
  --text2: #d1d1d6;
  --muted: #8e8e93;
  --accent: #0a84ff;
  --user-bg: #0a84ff;
  --danger: #ff453a;
  --success: #30d158;
  --warning: #ffd60a;
  --sep: rgba(255,255,255,0.08);
  --glass: rgba(0,0,0,0.85);
}

body {
  font-family: -apple-system, 'SF Pro Text', system-ui, sans-serif;
  background: var(--bg); color: var(--text);
  height: 100dvh; overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
.screen { height: 100dvh; }
.hidden { display: none !important; }

/* ── Login ── */
#login { display: flex; align-items: center; justify-content: center; }
.login-box { text-align: center; padding: 2rem; width: 100%; max-width: 340px; }
.login-box h1 { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em; margin-bottom: 0.25rem; }
.login-box p { color: var(--muted); margin-bottom: 2rem; font-size: 0.85rem; }
.login-box input {
  display: block; width: 100%; margin-bottom: 0.75rem;
  padding: 0.85rem 1rem; background: var(--bg2); border: 1px solid var(--sep);
  border-radius: 12px; color: var(--text); font-size: 1rem;
}
.login-box input:focus { outline: none; border-color: var(--accent); }
.login-box button, .oauth-btn {
  width: 100%; padding: 0.85rem; border: none; border-radius: 12px;
  font-size: 1rem; font-weight: 600; cursor: pointer;
}
.login-box button { background: var(--accent); color: #fff; }
.login-box button:active, .oauth-btn:active { opacity: 0.8; }
.oauth-btn { display: block; margin-bottom: 0.75rem; background: var(--success); color: #fff; }
.login-divider { display: flex; align-items: center; margin: 0.75rem 0; color: var(--muted); font-size: 0.78rem; }
.login-divider::before, .login-divider::after { content: ''; flex: 1; border-bottom: 1px solid var(--sep); }
.login-divider span { padding: 0 0.75rem; }

/* ── Header ── */
header {
  display: flex; align-items: center; padding: 0.5rem;
  background: var(--glass);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--sep);
  min-height: 44px; z-index: 10; position: relative;
}
.header-title {
  flex: 1; display: flex; align-items: center; gap: 0.5rem;
  justify-content: center; font-weight: 600; font-size: 0.92rem;
}
.icon-btn {
  background: none; border: none; color: var(--accent);
  font-size: 1.4rem; cursor: pointer; padding: 0.1rem 0.4rem;
  min-width: 44px; min-height: 44px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 300; border-radius: 8px;
}
.icon-btn:active { opacity: 0.5; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warning); flex-shrink: 0; }
.dot.connected { background: var(--success); }
.dot.error { background: var(--danger); }

/* ── Context bar ── */
#context-bar { position: relative; height: 2px; display: none; flex-shrink: 0; }
#context-bar.visible { display: block; }
#context-fill { position: absolute; top: 0; left: 0; height: 100%; transition: width 0.4s ease; width: 0%; }
#context-fill.green { background: var(--success); opacity: 0.5; }
#context-fill.yellow { background: var(--warning); opacity: 0.5; }
#context-fill.red { background: var(--danger); opacity: 0.6; }
#context-label { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 0; color: transparent; z-index: 1; }

/* ── Messages ── */
#chat { display: flex; flex-direction: column; }
#messages {
  flex: 1; overflow-y: auto; padding: 0.5rem 0;
  display: flex; flex-direction: column; gap: 0;
  -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
}

.msg {
  font-size: 0.93rem; line-height: 1.65; word-wrap: break-word; overflow-wrap: break-word;
  animation: msgIn 0.2s ease-out;
}
@keyframes msgIn { from { opacity: 0; } to { opacity: 1; } }

.msg.user {
  padding: 0.8rem 1rem;
  background: var(--bg2);
  color: var(--text);
  white-space: pre-wrap;
  border-bottom: 1px solid var(--sep);
}

.msg.assistant {
  padding: 0.8rem 1rem;
  background: transparent;
  border-bottom: 1px solid var(--sep);
}

/* Markdown */
.msg.assistant .content { color: var(--text2); }
.msg.assistant .content > p { margin-bottom: 0.55em; }
.msg.assistant .content > p:last-child { margin-bottom: 0; }
.msg.assistant .content > ul, .msg.assistant .content > ol { margin: 0.4em 0 0.4em 1.3em; }
.msg.assistant .content li { margin-bottom: 0.1em; }
.msg.assistant pre {
  background: var(--bg2); border-radius: 10px; padding: 0.7rem 0.8rem;
  overflow-x: auto; margin: 0.5em 0; font-size: 0.82rem; line-height: 1.5;
  border: 1px solid var(--sep);
}
.msg.assistant code { font-family: 'SF Mono', 'Menlo', 'Fira Code', monospace; font-size: 0.85em; }
.msg.assistant code:not(pre code) { background: var(--bg3); padding: 0.12rem 0.35rem; border-radius: 5px; }
.msg.assistant h1, .msg.assistant h2, .msg.assistant h3 { margin: 0.6em 0 0.3em; font-size: 1em; font-weight: 600; color: var(--text); border: none; }
.msg.assistant blockquote { border-left: 3px solid var(--bg4); padding-left: 0.8rem; color: var(--muted); margin: 0.4em 0; }
.msg.assistant table { border-collapse: collapse; margin: 0.4em 0; font-size: 0.82rem; }
.msg.assistant th, .msg.assistant td { border: 1px solid var(--sep); padding: 0.3rem 0.5rem; }
.msg.assistant th { background: var(--bg2); }
.msg.assistant a { color: var(--accent); }

/* ── Tool blocks ── */
.tool-block {
  background: var(--bg2); border: 1px solid var(--sep);
  border-radius: 10px; margin: 0.4em 0; overflow: hidden; font-size: 0.82rem;
}
.tool-header {
  display: flex; align-items: center; padding: 0.5rem 0.7rem;
  cursor: pointer; gap: 0.4rem; color: var(--muted); user-select: none; min-height: 38px;
}
.tool-name { font-weight: 600; color: var(--accent); flex-shrink: 0; font-size: 0.78rem; }
.tool-summary { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: 'SF Mono', monospace; font-size: 0.75rem; }
.tool-chevron { margin-left: auto; transition: transform 0.2s ease; flex-shrink: 0; }
.tool-header.open .tool-chevron { transform: rotate(90deg); }
.tool-body { display: none; padding: 0 0.7rem 0.5rem; max-height: 250px; overflow-y: auto; border-top: 1px solid var(--sep); }
.tool-body.open { display: block; }
.tool-body pre { margin: 0; white-space: pre-wrap; word-break: break-all; color: var(--muted); font-size: 0.75rem; }
.tool-result-block {
  background: var(--bg2); border: 1px solid var(--sep); border-top: none;
  border-radius: 0 0 10px 10px; margin-top: -0.4em; margin-bottom: 0.4em;
  padding: 0.4rem 0.7rem; font-size: 0.75rem; max-height: 200px; overflow-y: auto; color: var(--muted);
}
.tool-result-block pre { margin: 0; white-space: pre-wrap; word-break: break-all; }

/* ── Thinking ── */
.thinking { display: flex; gap: 5px; padding: 0.8rem 1rem; align-self: flex-start; }
.thinking span {
  width: 6px; height: 6px; background: var(--muted); border-radius: 50%;
  animation: pulse 1.4s ease-in-out infinite;
}
.thinking span:nth-child(2) { animation-delay: 0.15s; }
.thinking span:nth-child(3) { animation-delay: 0.3s; }
@keyframes pulse { 0%, 80%, 100% { transform: scale(0.5); opacity: 0.3; } 40% { transform: scale(1); opacity: 1; } }

.stats { font-size: 0.68rem; color: var(--muted); text-align: center; padding: 0.2rem 0; }

/* ── Input ── */
#input-area {
  padding: 0.5rem 0.6rem;
  padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
  background: var(--glass);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--sep);
}
.input-row { display: flex; gap: 0.35rem; align-items: flex-end; }
#input {
  flex: 1; background: var(--bg2); border: 1px solid var(--sep);
  border-radius: 22px; padding: 0.6rem 0.9rem; color: var(--text);
  font-size: 1rem; font-family: inherit; resize: none;
  max-height: 120px; line-height: 1.35;
}
#input:focus { outline: none; border-color: var(--accent); }
#input::placeholder { color: var(--muted); }
#send-btn {
  width: 34px; height: 34px; border-radius: 50%; background: var(--accent);
  color: #fff; border: none; font-size: 1.1rem; cursor: pointer;
  flex-shrink: 0; display: flex; align-items: center; justify-content: center;
}
#send-btn:active { transform: scale(0.9); }
#send-btn:disabled { background: var(--bg4); color: var(--muted); }
#attach-btn { min-width: 34px; min-height: 34px; padding: 0; color: var(--muted); }
#attach-btn svg { display: block; }

/* ── Attachment preview ── */
#attach-preview { display: flex; gap: 0.5rem; padding: 0.4rem 0; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.attach-thumb {
  position: relative; width: 56px; height: 56px; border-radius: 8px;
  overflow: hidden; flex-shrink: 0; background: var(--bg3); border: 1px solid var(--sep);
}
.attach-thumb img { width: 100%; height: 100%; object-fit: cover; }
.attach-thumb .attach-name { font-size: 0.55rem; color: var(--muted); padding: 0.15rem; text-align: center; word-break: break-all; overflow: hidden; display: flex; align-items: center; justify-content: center; height: 100%; }
.attach-remove { position: absolute; top: 2px; right: 2px; width: 18px; height: 18px; border-radius: 50%; background: rgba(0,0,0,0.8); color: #fff; border: none; font-size: 0.65rem; cursor: pointer; display: flex; align-items: center; justify-content: center; }

#interrupt-bar { margin-bottom: 0.35rem; text-align: center; }
#interrupt-btn { background: var(--danger); color: #fff; border: none; border-radius: 20px; padding: 0.35rem 1.5rem; font-size: 0.85rem; cursor: pointer; font-weight: 600; }
#interrupt-btn:active { opacity: 0.8; }

/* ── Sidebar ── */
#sidebar-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5); z-index: 99;
  opacity: 0; pointer-events: none; transition: opacity 0.3s ease;
  -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
}
#sidebar.open #sidebar-overlay { opacity: 1; pointer-events: auto; }
#sidebar-panel {
  position: fixed; top: 0; left: 0; bottom: 0; width: min(300px, 80vw);
  background: var(--bg2); z-index: 100; padding: 1rem 0.75rem;
  overflow-y: auto; border-right: 1px solid var(--sep);
  transform: translateX(-100%); transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
  -webkit-overflow-scrolling: touch;
}
#sidebar.open #sidebar-panel { transform: translateX(0); }
#sidebar-panel h2 { font-size: 1.2rem; margin-bottom: 0.75rem; font-weight: 700; }
#model-select { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.75rem; padding-bottom: 0.75rem; border-bottom: 1px solid var(--sep); }
#model-select label { font-size: 0.82rem; color: var(--muted); }
#model { flex: 1; background: var(--bg3); border: 1px solid var(--sep); border-radius: 8px; padding: 0.4rem 0.5rem; color: var(--text); font-size: 0.85rem; -webkit-appearance: none; }
.session-item { padding: 0.65rem 0.7rem; border-radius: 10px; cursor: pointer; margin-bottom: 0.15rem; transition: background 0.15s; }
.session-item:active, .session-item.active { background: var(--bg3); }
.session-title { font-size: 0.88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.session-meta { font-size: 0.7rem; color: var(--muted); margin-top: 0.15rem; }
.session-delete { float: right; background: none; border: none; color: var(--danger); cursor: pointer; font-size: 0.8rem; padding: 0.1rem 0.3rem; border-radius: 4px; opacity: 0.5; }
.session-delete:active { opacity: 1; }
.session-badge { display: inline-block; font-size: 0.58rem; padding: 0.05rem 0.3rem; border-radius: 4px; margin-left: 0.3rem; vertical-align: middle; font-weight: 600; letter-spacing: 0.02em; }
.session-badge.cli { background: rgba(10,132,255,0.12); color: var(--accent); }
.session-badge.gui { background: rgba(48,209,88,0.12); color: var(--success); }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 0; }
@media (hover: hover) {
  ::-webkit-scrollbar { width: 4px; }
  ::-webkit-scrollbar-track { background: transparent; }
  ::-webkit-scrollbar-thumb { background: var(--bg4); border-radius: 2px; }
}

/* ── Safe areas ── */
@supports (padding-top: env(safe-area-inset-top)) {
  header { padding-top: calc(0.5rem + env(safe-area-inset-top)); }
  #input-area { padding-bottom: max(0.5rem, env(safe-area-inset-bottom)); }
  #sidebar-panel { padding-top: calc(1rem + env(safe-area-inset-top)); padding-left: calc(0.75rem + env(safe-area-inset-left)); }
  #login { padding-top: env(safe-area-inset-top); }
}
@media (display-mode: standalone) {
  header { -webkit-user-select: none; user-select: none; }
  body { overscroll-behavior: none; }
  #messages { overscroll-behavior-y: contain; }
}

.msg.system { align-self: center; background: none; color: var(--muted); font-size: 0.78rem; text-align: center; padding: 0.3rem 0.6rem; max-width: 95%; }
.msg.system.error { color: var(--danger); }
