:root, [data-theme="dark"] {
	color-scheme: dark;
	--bg: #0d1117;
	--surface: #161b22;
	--surface-2: #1f242c;
	--border: #30363d;
	--text: #c9d1d9;
	--muted: #8b949e;
	--accent: #58a6ff;
	--user-tint: rgba(88, 166, 255, 0.06);
	--success: #3fb950;
	--danger: #f85149;
	--on-accent: #0d1117;
	--composer-bg: rgba(13, 17, 23, 0.85);
	--thinking: #d787af;
}

[data-theme="light"] {
	color-scheme: light;
	--bg: #ffffff;
	--surface: #f6f8fa;
	--surface-2: #eaeef2;
	--border: #d0d7de;
	--text: #1f2328;
	--muted: #656d76;
	--accent: #0969da;
	--user-tint: rgba(9, 105, 218, 0.06);
	--success: #1a7f37;
	--danger: #cf222e;
	--on-accent: #ffffff;
	--composer-bg: rgba(255, 255, 255, 0.85);
	--thinking: #af5f87;
}

:root {
	--font-sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
	--font-mono: ui-monospace, "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
	margin: 0;
	padding: 0;
	background: var(--bg);
	color: var(--text);
	font: 15px/1.6 var(--font-sans);
}

a { color: inherit; text-decoration: none; }
a:hover { color: var(--accent); }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

:focus-visible {
	outline: 2px solid var(--accent);
	outline-offset: 2px;
	border-radius: 4px;
}

/* ── shell: sidebar + main ───────────────────────────────────── */
.shell {
	display: flex;
	height: 100vh;
	height: 100dvh;
	overflow: hidden;
}
.sidebar {
	width: 260px;
	flex-shrink: 0;
	background: var(--surface);
	border-right: 1px solid var(--border);
	display: flex;
	flex-direction: column;
	min-height: 0;
}
.sidebar-header {
	padding: 12px;
	border-bottom: 1px solid var(--border);
	display: flex;
	flex-direction: column;
	gap: 10px;
	flex-shrink: 0;
}
.sidebar-title-row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 8px;
}
.sidebar-footer {
	padding: 10px 16px;
	border-top: 1px solid var(--border);
	flex-shrink: 0;
}

/* ── account chip (sidebar footer) ───────────────────────────── */
.account-chip-wrap {
	position: relative;
	border-top: 1px solid var(--border);
	flex-shrink: 0;
}
.account-chip {
	display: flex;
	align-items: center;
	gap: 10px;
	width: 100%;
	background: transparent;
	border: none;
	border-radius: 0;
	color: var(--text);
	padding: 12px 16px;
	font-size: 14px;
	text-align: left;
	min-width: 0;
}
.account-chip:hover { background: var(--surface-2); }
.account-chip[aria-expanded="true"] { background: var(--surface-2); }
.account-chip .avatar {
	width: 26px;
	height: 26px;
	border-radius: 50%;
	background: var(--accent);
	color: var(--on-accent);
	display: inline-flex;
	align-items: center;
	justify-content: center;
	font-weight: 600;
	font-size: 12px;
	text-transform: uppercase;
	flex-shrink: 0;
}
.account-chip .account-name {
	flex: 1;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	min-width: 0;
}
.account-chip svg {
	color: var(--muted);
	flex-shrink: 0;
}
.account-chip[aria-expanded="true"] svg {
	transform: rotate(180deg);
}
.account-popout {
	position: absolute;
	bottom: calc(100% + 4px);
	left: 10px;
	right: 10px;
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 8px;
	box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
	padding: 4px;
	z-index: 60;
	display: flex;
	flex-direction: column;
}
.account-popout[hidden] { display: none; }
.account-popout > a,
.account-popout form button {
	display: block;
	width: 100%;
	text-align: left;
	background: transparent;
	border: none;
	color: var(--text);
	padding: 8px 12px;
	font: inherit;
	font-size: 14px;
	border-radius: 4px;
	cursor: pointer;
}
.account-popout > a:hover,
.account-popout form button:hover {
	background: var(--surface-2);
	color: var(--text);
}
.account-popout form { margin: 0; }

/* ── profile page ─────────────────────────────────────────────── */
.profile-section {
	max-width: 420px;
}
.profile-section h2 {
	margin: 0 0 12px;
	font-size: 1rem;
	font-weight: 600;
}
.profile-form {
	display: flex;
	flex-direction: column;
	gap: 12px;
}
.profile-form label {
	display: flex;
	flex-direction: column;
	gap: 4px;
	font-size: 13px;
	color: var(--muted);
}
.profile-form input[type="password"] {
	padding: 10px 12px;
	background: var(--bg);
	border: 1px solid var(--border);
	border-radius: 6px;
	color: var(--text);
	font: inherit;
}
.profile-form input[type="password"]:focus {
	outline: none;
	border-color: var(--accent);
}
.profile-form .btn-primary { align-self: flex-start; margin-top: 4px; }
.notice {
	padding: 8px 12px;
	border-radius: 6px;
	font-size: 13px;
	margin: 0 0 12px;
}
.notice.success {
	background: color-mix(in srgb, var(--success) 15%, transparent);
	color: var(--success);
	border: 1px solid color-mix(in srgb, var(--success) 30%, transparent);
}
.notice.error {
	background: color-mix(in srgb, var(--danger) 15%, transparent);
	color: var(--danger);
	border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent);
}
.back-btn {
	display: inline-flex;
	align-items: center;
	gap: 4px;
	padding: 6px 8px;
	border-radius: 6px;
	font-size: 13px;
	color: var(--muted);
	background: transparent;
	border: 1px solid transparent;
}
.back-btn:hover { color: var(--text); background: var(--surface-2); }
.sidebar-title {
	font-size: 0.8rem;
	font-weight: 600;
	color: var(--muted);
	text-transform: uppercase;
	letter-spacing: 0.05em;
	margin: 0;
}
.sidebar-list {
	list-style: none;
	margin: 0;
	padding: 8px 0;
	overflow-y: auto;
	flex: 1;
}
.sidebar-list li a {
	display: block;
	padding: 10px 16px;
	border-left: 2px solid transparent;
	color: var(--text);
	font-size: 14px;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}
.sidebar-list li a:hover { background: var(--surface-2); color: var(--text); }
.sidebar-list li a.active { background: var(--surface-2); border-left-color: var(--accent); }
.sidebar-list .session-meta {
	display: block;
	font-size: 12px;
	color: var(--muted);
	margin-top: 2px;
}

.main {
	flex: 1;
	min-width: 0;
	min-height: 0;
	display: flex;
	flex-direction: column;
	overflow: hidden;
}

/* ── header inside main column ───────────────────────────────── */
.main-header {
	padding: 16px 20px;
	border-bottom: 1px solid var(--border);
	background: var(--bg);
	flex-shrink: 0;
}
.main-header .breadcrumbs {
	font-size: 13px;
	color: var(--muted);
	margin-bottom: 4px;
}
.main-header h1 {
	margin: 0;
	font-size: 1rem;
	font-weight: 600;
	color: var(--text);
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}
.main-header .submeta {
	font-size: 12px;
	color: var(--muted);
	margin-top: 2px;
}

/* ── general main content ────────────────────────────────────── */
.content {
	max-width: 768px;
	width: 100%;
	margin: 0 auto;
	padding: 20px;
	flex: 1;
	overflow-y: auto;
}

/* ── login ───────────────────────────────────────────────────── */
body.login {
	display: flex;
	align-items: center;
	justify-content: center;
	min-height: 100vh;
	padding: 24px;
}
body.login main {
	width: 100%;
	max-width: 360px;
}
body.login .login-card {
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 10px;
	padding: 28px 24px;
	box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}
body.login h1 {
	margin: 0 0 4px;
	font-size: 20px;
	font-weight: 600;
}
body.login .sub {
	margin: 0 0 20px;
	color: var(--muted);
	font-size: 13px;
}
body.login form { display: flex; flex-direction: column; gap: 12px; }
body.login input[type="text"],
body.login input[type="password"] {
	width: 100%;
	padding: 10px 12px;
	font: inherit;
	background: var(--bg);
	border: 1px solid var(--border);
	border-radius: 6px;
	color: var(--text);
}
body.login input[type="text"]:focus,
body.login input[type="password"]:focus {
	outline: 2px solid var(--accent);
	outline-offset: 0;
	border-color: transparent;
}
body.login button[type="submit"] {
	margin-top: 4px;
	background: var(--accent);
	color: var(--on-accent);
	border: 1px solid var(--accent);
	padding: 10px 14px;
	border-radius: 6px;
	font-weight: 600;
	font-size: 14px;
}
body.login button[type="submit"]:hover { filter: brightness(1.1); }
.err { color: var(--danger); font-size: 13px; margin: 0; }

/* ── buttons ─────────────────────────────────────────────────── */
button {
	font: inherit;
	cursor: pointer;
}
.btn-primary {
	background: var(--accent);
	color: var(--on-accent);
	border: 1px solid var(--accent);
	padding: 8px 14px;
	border-radius: 6px;
	font-weight: 600;
	font-size: 14px;
}
.btn-primary:hover { filter: brightness(1.1); }
.btn-outline {
	background: transparent;
	color: var(--text);
	border: 1px solid var(--border);
	padding: 8px 14px;
	border-radius: 6px;
	font-size: 14px;
	width: 100%;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: 6px;
}
.btn-outline:hover { background: var(--surface-2); border-color: var(--accent); color: var(--accent); }
.btn-link {
	background: none;
	border: none;
	color: var(--muted);
	padding: 4px 8px;
	font-size: 13px;
}
.btn-link:hover { color: var(--text); }

/* ── project cards (index) ───────────────────────────────────── */
.cards {
	list-style: none;
	padding: 0;
	margin: 0;
	display: grid;
	gap: 12px;
	grid-template-columns: 1fr;
}
.card {
	display: block;
	padding: 16px 18px;
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 8px;
	transition: border-color 0.15s, background 0.15s;
}
.card:hover { border-color: var(--accent); background: var(--surface-2); color: inherit; }
.card-name { font-weight: 600; margin-bottom: 4px; font-size: 15px; }
.card-cwd {
	color: var(--muted);
	font-size: 12px;
	font-family: var(--font-mono);
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* ── chat (session view) ─────────────────────────────────────── */
.chat-stream {
	flex: 1;
	min-height: 0;
	overflow-y: auto;
}
.chat-stream-inner {
	max-width: 768px;
	margin: 0 auto;
	padding: 0 20px;
}
.turn {
	padding: 16px 0;
	border-bottom: 1px solid var(--border);
	animation: fadeUp 180ms ease-out;
}
.turn:last-child { border-bottom: none; }
.turn.user { background: linear-gradient(to right, var(--user-tint), transparent 80%); margin: 0 -20px; padding: 16px 20px; }
.turn.user + .turn.user { border-top: 1px solid var(--border); }

.role {
	display: flex;
	align-items: center;
	gap: 8px;
	font-size: 12px;
	font-weight: 600;
	text-transform: uppercase;
	letter-spacing: 0.04em;
	color: var(--muted);
	margin-bottom: 8px;
}
.role .icon {
	width: 18px;
	height: 18px;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	color: var(--accent);
}
.turn.user .role .icon { color: var(--accent); }
.turn.assistant .role .icon { color: var(--success); }

.turn .content {
	white-space: pre-wrap;
	word-wrap: break-word;
	font-size: 15px;
	line-height: 1.6;
}
.turn .content.rendered { white-space: normal; }
.turn .content.rendered p { margin: 0 0 0.75em; }
.turn .content.rendered p:last-child { margin-bottom: 0; }
.turn .content.rendered ul,
.turn .content.rendered ol { padding-left: 1.6em; margin: 0.5em 0; }
.turn .content.rendered li { margin: 0.15em 0; }
.turn .content.rendered li > p { margin: 0; }
.turn .content.rendered h1,
.turn .content.rendered h2,
.turn .content.rendered h3,
.turn .content.rendered h4 { margin: 1.1em 0 0.5em; font-weight: 600; line-height: 1.3; }
.turn .content.rendered h1 { font-size: 1.4em; }
.turn .content.rendered h2 { font-size: 1.2em; }
.turn .content.rendered h3 { font-size: 1.05em; }
.turn .content.rendered h4 { font-size: 1em; color: var(--muted); }
.turn .content.rendered h1:first-child,
.turn .content.rendered h2:first-child,
.turn .content.rendered h3:first-child { margin-top: 0; }
.turn .content.rendered a { color: var(--accent); text-decoration: underline; text-decoration-color: color-mix(in srgb, var(--accent) 50%, transparent); }
.turn .content.rendered a:hover { text-decoration-color: var(--accent); }
.turn .content.rendered blockquote {
	border-left: 3px solid var(--border);
	padding: 2px 0 2px 12px;
	color: var(--muted);
	margin: 0.5em 0;
}
.turn .content.rendered hr {
	border: 0;
	border-top: 1px solid var(--border);
	margin: 1em 0;
}
.turn .content.rendered table {
	border-collapse: collapse;
	margin: 0.5em 0;
	font-size: 14px;
}
.turn .content.rendered th,
.turn .content.rendered td {
	border: 1px solid var(--border);
	padding: 6px 10px;
	text-align: left;
}
.turn .content.rendered th { background: var(--surface-2); }
.turn .content.rendered img { max-width: 100%; border-radius: 6px; }

details.thinking {
	margin-top: 8px;
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 6px;
	padding: 8px 12px;
	font-size: 14px;
}
details.thinking summary { cursor: pointer; color: var(--muted); }
details.thinking[open] summary { margin-bottom: 8px; }
details.thinking pre {
	margin: 0;
	white-space: pre-wrap;
	word-break: break-word;
	font-family: var(--font-mono);
	font-size: 12px;
	color: var(--muted);
	max-height: 320px;
	overflow: auto;
}

/* code blocks inside content (future markdown rendering) */
.content code {
	font-family: var(--font-mono);
	font-size: 13px;
	background: var(--surface-2);
	border: 1px solid var(--border);
	padding: 1px 6px;
	border-radius: 4px;
}
.content pre {
	background: var(--surface-2);
	border: 1px solid var(--border);
	border-radius: 6px;
	padding: 12px 14px;
	font-family: var(--font-mono);
	font-size: 13px;
	overflow-x: auto;
	line-height: 1.5;
}
.content pre code { background: none; border: none; padding: 0; }

/* ── composer (placeholder for live streaming) ──────────────── */
.composer {
	background: var(--composer-bg);
	backdrop-filter: blur(8px);
	-webkit-backdrop-filter: blur(8px);
	border-top: 1px solid var(--border);
	padding: 12px 20px;
	flex-shrink: 0;
}
.composer-inner {
	max-width: 768px;
	margin: 0 auto;
	position: relative;
}
.composer textarea {
	width: 100%;
	min-height: 48px;
	max-height: 200px;
	padding: 12px 56px 12px 14px;
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 10px;
	color: var(--text);
	font: inherit;
	resize: none;
	line-height: 1.5;
}
.composer textarea:focus {
	outline: none;
	border-color: var(--accent);
}
.composer textarea[disabled] {
	background: var(--surface-2);
	color: var(--muted);
}
.composer .send {
	position: absolute;
	right: 8px;
	bottom: 8px;
	width: 32px;
	height: 32px;
	border-radius: 6px;
	background: var(--accent);
	color: var(--on-accent);
	border: none;
	display: inline-flex;
	align-items: center;
	justify-content: center;
}
.composer .send[disabled] {
	background: var(--surface-2);
	color: var(--muted);
	cursor: not-allowed;
}
.composer .composer-hint {
	display: block;
	margin-top: 6px;
	font-size: 12px;
	color: var(--muted);
}
.composer-stop {
	position: absolute;
	right: 48px;
	bottom: 12px;
	font-size: 12px;
	color: var(--danger);
}
.composer-stop:hover { color: var(--danger); filter: brightness(1.2); }

/* ── empty states ────────────────────────────────────────────── */
.empty {
	padding: 48px 24px;
	text-align: center;
	color: var(--muted);
}
.empty h2 { margin: 0 0 8px; color: var(--text); font-weight: 600; font-size: 1rem; }

/* ── theme toggle ────────────────────────────────────────────── */
.theme-toggle {
	background: transparent;
	border: 1px solid var(--border);
	color: var(--muted);
	width: 32px;
	height: 32px;
	border-radius: 6px;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
}
.theme-toggle:hover { color: var(--text); border-color: var(--accent); }
.theme-toggle .sun { display: none; }
.theme-toggle .moon { display: inline-flex; }
[data-theme="light"] .theme-toggle .sun { display: inline-flex; }
[data-theme="light"] .theme-toggle .moon { display: none; }

/* ── thinking indicator ──────────────────────────────────────── */
.thinking-indicator {
	display: flex;
	align-items: baseline;
	gap: 8px;
	padding: 12px 0;
	color: var(--thinking);
	font-size: 14px;
	font-family: var(--font-mono);
	animation: fadeUp 180ms ease-out;
}
.thinking-indicator .glyph {
	display: inline-block;
	width: 1ch;
	font-size: 16px;
	color: var(--thinking);
}
.thinking-indicator .verb {
	color: var(--thinking);
	font-weight: 500;
	font-family: var(--font-sans);
	font-size: 14px;
}
.thinking-indicator .meta {
	color: var(--muted);
	font-size: 13px;
	font-family: var(--font-sans);
}

/* ── animations ──────────────────────────────────────────────── */
@keyframes fadeUp {
	from { opacity: 0; transform: translateY(4px); }
	to   { opacity: 1; transform: none; }
}

/* ── context menu (right-click on sidebar items) ─────────────── */
.context-menu {
	position: fixed;
	z-index: 100;
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 6px;
	box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
	padding: 4px;
	min-width: 140px;
	display: flex;
	flex-direction: column;
}
.context-menu[hidden] { display: none; }
.context-menu button {
	background: transparent;
	border: none;
	color: var(--text);
	text-align: left;
	padding: 8px 12px;
	font: inherit;
	font-size: 14px;
	border-radius: 4px;
	cursor: pointer;
}
.context-menu button:hover { background: var(--surface-2); }
.context-menu button.danger { color: var(--danger); }
.context-menu button.danger:hover { background: color-mix(in srgb, var(--danger) 15%, transparent); }

/* inline rename input inside sidebar */
.sidebar-list .inline-rename {
	width: 100%;
	background: var(--bg);
	border: 1px solid var(--accent);
	border-radius: 4px;
	color: var(--text);
	font: inherit;
	font-size: 14px;
	padding: 4px 6px;
	outline: none;
}

/* ── menu toggle (visible on mobile) ─────────────────────────── */
.menu-toggle {
	display: none;
	background: transparent;
	border: 1px solid var(--border);
	color: var(--text);
	width: 36px;
	height: 36px;
	border-radius: 6px;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
}
.menu-toggle:hover { border-color: var(--accent); color: var(--accent); }

.backdrop {
	display: none;
	position: fixed;
	inset: 0;
	background: rgba(0, 0, 0, 0.5);
	opacity: 0;
	pointer-events: none;
	transition: opacity 0.18s ease-out;
	z-index: 40;
}
.backdrop.show {
	opacity: 1;
	pointer-events: auto;
}

.main-header-row {
	display: flex;
	align-items: center;
	gap: 10px;
}
.main-header-row .header-title { flex: 1; min-width: 0; }

/* ── mobile ──────────────────────────────────────────────────── */
@media (max-width: 768px) {
	.menu-toggle { display: inline-flex; }
	.backdrop { display: block; }

	.sidebar {
		position: fixed;
		top: 0;
		bottom: 0;
		left: -300px;
		width: 280px;
		z-index: 50;
		transition: left 0.2s ease-out;
		box-shadow: none;
	}
	.sidebar.open {
		left: 0;
		box-shadow: 4px 0 24px rgba(0, 0, 0, 0.4);
	}

	.sidebar-list li a {
		padding: 14px 18px;
		font-size: 15px;
	}
	.sidebar-list .session-meta { font-size: 13px; margin-top: 4px; }
	.btn-outline { padding: 12px 14px; font-size: 15px; }
	.back-btn { padding: 10px 12px; font-size: 14px; }

	.composer {
		padding: 10px 12px env(safe-area-inset-bottom, 10px);
	}
	.composer textarea { min-height: 52px; padding-right: 60px; }
	.composer .send { width: 40px; height: 40px; right: 10px; bottom: 10px; }

	.content, .chat-stream-inner, .composer-inner {
		padding-left: 12px;
		padding-right: 12px;
	}
	.main-header { padding: 12px 14px; }
	.cards { grid-template-columns: 1fr; }
}

@media (min-width: 700px) {
	.cards { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
}
