:root {
	--bg: #f7f7f8;
	--panel: #ffffff;
	--panel-2: #f1f1f3;
	--border: #e2e2e6;
	--text: #1b1b1f;
	--muted: #6b6b76;
	--accent: #f6821f;
	--accent-strong: #d96a0b;
	--accent-soft: #fff1e5;
	--danger: #c62828;
	--danger-soft: #fdecec;
	--user-bubble: #fff1e5;
	--radius: 10px;
	--mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
	color-scheme: light;
}

@media (prefers-color-scheme: dark) {
	:root {
		--bg: #111114;
		--panel: #19191d;
		--panel-2: #222228;
		--border: #2e2e36;
		--text: #ececf1;
		--muted: #9a9aa6;
		--accent-soft: #3a2614;
		--danger-soft: #3a1717;
		--user-bubble: #2f2216;
		color-scheme: dark;
	}
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
	margin: 0;
	background: var(--bg);
	color: var(--text);
	font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	display: flex;
	flex-direction: column;
}

code, .code { font-family: var(--mono); font-size: 12.5px; }

button, input, select, textarea { font: inherit; color: inherit; }

.muted { color: var(--muted); }
.small { font-size: 12.5px; }

/* ---------- Top bar ---------- */
.topbar {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 12px;
	padding: 10px 16px;
	background: var(--panel);
	border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand h1 { margin: 0; font-size: 16px; font-weight: 650; white-space: nowrap; }
.logo {
	width: 22px; height: 22px; border-radius: 6px; flex: none;
	background: linear-gradient(135deg, #fbad41, var(--accent));
}
.topbar-meta { display: flex; align-items: center; gap: 10px; }

/* ---------- Buttons & inputs ---------- */
button {
	cursor: pointer;
	border-radius: 8px;
	border: 1px solid var(--border);
	background: var(--panel);
	padding: 7px 14px;
}
button:disabled { opacity: .55; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover:not(:disabled) { background: var(--accent-strong); }
button.ghost { background: transparent; }
button.ghost:hover:not(:disabled) { background: var(--panel-2); }
button.danger { color: var(--danger); }

input[type="text"], input[type="search"], input[type="number"], select, textarea {
	width: 100%;
	background: var(--panel);
	border: 1px solid var(--border);
	border-radius: 8px;
	padding: 8px 10px;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible {
	outline: 2px solid var(--accent);
	outline-offset: 1px;
}

/* ---------- Layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }

.sidebar {
	width: 320px;
	flex: none;
	display: flex;
	flex-direction: column;
	border-right: 1px solid var(--border);
	background: var(--panel);
	min-height: 0;
}
.sidebar-controls { display: grid; gap: 8px; padding: 12px; border-bottom: 1px solid var(--border); }
.model-list { overflow-y: auto; flex: 1; padding: 4px 0 16px; }

.task-group summary {
	position: sticky; top: 0;
	background: var(--panel);
	padding: 10px 14px 6px;
	font-size: 11.5px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
	color: var(--muted);
	cursor: pointer;
	list-style: none;
	display: flex; gap: 4px; justify-content: space-between;
}
.task-group summary::-webkit-details-marker { display: none; }
.task-group summary::before { content: "▾"; }
.task-group:not([open]) summary::before { content: "▸"; }
.task-group summary span { margin-left: auto; font-weight: 500; }

.model-item {
	display: block; width: 100%;
	text-align: left;
	border: 0; border-radius: 0;
	background: transparent;
	padding: 7px 14px 7px 26px;
}
.model-item:hover { background: var(--panel-2); }
.model-item[aria-current="true"] { background: var(--accent-soft); box-shadow: inset 3px 0 var(--accent); }
.model-item .mi-name { display: block; font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-item .mi-author { display: block; font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mi-flag { font-size: 10.5px; padding: 0 5px; border-radius: 4px; margin-left: 4px; background: var(--panel-2); color: var(--muted); font-weight: 600; }

.main { flex: 1; min-width: 0; overflow-y: auto; padding: 20px 24px 40px; }

.empty-state { max-width: 560px; margin: 12vh auto 0; text-align: center; }
.empty-state h2 { font-size: 22px; margin-bottom: 6px; }

/* ---------- Model view ---------- */
.model-view { max-width: 980px; margin: 0 auto; }
.model-title-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.model-title-row h2 { margin: 0; font-size: 20px; word-break: break-word; }
.badge {
	font-size: 12px; font-weight: 600;
	padding: 2px 9px; border-radius: 999px;
	background: var(--accent-soft); color: var(--accent-strong);
}
@media (prefers-color-scheme: dark) { .badge { color: #fbad41; } }
.model-id {
	margin-top: 6px;
	font-family: var(--mono); font-size: 12.5px;
	padding: 2px 8px; background: var(--panel-2); border: 0;
	max-width: 100%; overflow-wrap: anywhere; text-align: left;
}
.model-desc { color: var(--muted); margin: 10px 0; }
.props { display: flex; flex-wrap: wrap; gap: 6px; }
.prop {
	font-size: 12px;
	border: 1px solid var(--border); border-radius: 6px;
	padding: 2px 8px;
	background: var(--panel);
	max-width: 100%; overflow-wrap: anywhere;
}
.prop b { font-weight: 600; }
.prop a { color: inherit; }

.tabs { display: flex; gap: 4px; margin: 20px 0 0; border-bottom: 1px solid var(--border); }
.tab {
	border: 0; border-radius: 8px 8px 0 0; background: transparent;
	padding: 8px 14px; color: var(--muted); font-weight: 550;
}
.tab[aria-selected="true"] { color: var(--text); box-shadow: inset 0 -2px var(--accent); }

.tab-panel {
	background: var(--panel);
	border: 1px solid var(--border); border-top: 0;
	border-radius: 0 0 var(--radius) var(--radius);
	padding: 16px;
}

.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; }
.field { display: grid; gap: 5px; }
.field > span { font-size: 12.5px; font-weight: 600; }
.field.wide { grid-column: 1 / -1; }
.field .help { font-size: 12px; color: var(--muted); font-weight: 400; }
.field.check { display: flex; align-items: center; gap: 8px; }
.field.check > span { font-weight: 500; }
.note { font-size: 12.5px; color: var(--muted); margin: 0 0 12px; }

.actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 14px; }

.dropzone {
	border: 1.5px dashed var(--border); border-radius: 8px;
	padding: 10px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dropzone img { max-height: 90px; max-width: 160px; border-radius: 6px; }
.dropzone audio { max-width: 100%; }

/* ---------- Chat ---------- */
.chat { display: flex; flex-direction: column; gap: 12px; }
.chat-log {
	display: flex; flex-direction: column; gap: 10px;
	max-height: 52vh; overflow-y: auto;
	padding: 4px 2px;
}
.chat-empty { color: var(--muted); text-align: center; padding: 24px 0; }
.msg { max-width: 85%; padding: 9px 13px; border-radius: 12px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user-bubble); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--panel-2); border-bottom-left-radius: 4px; }
.msg.error { align-self: stretch; max-width: none; background: var(--danger-soft); color: var(--danger); }
.msg .reasoning {
	display: block; font-size: 12.5px; color: var(--muted);
	border-left: 2px solid var(--border); padding-left: 8px; margin-bottom: 6px;
}
.msg .meta { display: block; font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.chat-compose { display: flex; gap: 8px; align-items: flex-end; }
.chat-compose textarea { flex: 1; min-height: 44px; }
.chat-settings summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.chat-settings .form-grid { margin-top: 10px; }

/* ---------- Output ---------- */
.output {
	margin-top: 18px;
	background: var(--panel);
	border: 1px solid var(--border);
	border-radius: var(--radius);
	padding: 16px;
}
.output-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 10px; }
.output-head h3 { margin: 0; font-size: 14px; }
.output-text { white-space: pre-wrap; overflow-wrap: anywhere; margin: 0 0 10px; }
.output img.result-img { max-width: 100%; border-radius: 8px; display: block; }
.output audio { width: 100%; }
.output .error { color: var(--danger); background: var(--danger-soft); padding: 10px 12px; border-radius: 8px; white-space: pre-wrap; overflow-wrap: anywhere; }
.output a.download { display: inline-block; margin-top: 8px; color: var(--accent-strong); }
pre.json {
	background: var(--panel-2);
	border-radius: 8px;
	padding: 10px 12px;
	overflow: auto;
	max-height: 420px;
	font-family: var(--mono); font-size: 12px;
	margin: 0;
}
.raw-toggle summary { cursor: pointer; color: var(--muted); font-size: 12.5px; margin: 10px 0 6px; }

.bars { display: grid; gap: 6px; }
.bar-row { display: grid; grid-template-columns: minmax(80px, 200px) 1fr 56px; gap: 8px; align-items: center; font-size: 13px; }
.bar-row .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { background: var(--panel-2); border-radius: 4px; height: 10px; overflow: hidden; }
.bar-fill { background: var(--accent); height: 100%; }
.bar-row .score { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }

.detect-wrap { position: relative; display: inline-block; max-width: 100%; }
.detect-wrap img { max-width: 100%; display: block; border-radius: 8px; }
.detect-box { position: absolute; border: 2px solid var(--accent); border-radius: 3px; }
.detect-box span {
	position: absolute; top: -20px; left: -2px;
	background: var(--accent); color: #fff; font-size: 11px; padding: 0 5px; border-radius: 3px; white-space: nowrap;
}

.spinner {
	width: 16px; height: 16px; border-radius: 50%;
	border: 2px solid var(--border); border-top-color: var(--accent);
	animation: spin .8s linear infinite; display: inline-block; vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
	position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
	background: var(--text); color: var(--bg);
	padding: 8px 14px; border-radius: 8px; font-size: 13px;
	opacity: 0; transition: opacity .2s; pointer-events: none;
}
.toast.show { opacity: 1; }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
	body { height: auto; }
	.layout { flex-direction: column; }
	.sidebar { width: auto; border-right: 0; border-bottom: 1px solid var(--border); }
	.model-list { max-height: 38vh; }
	.main { padding: 16px; overflow: visible; }
	.topbar-meta #model-count { display: none; }
	.msg { max-width: 95%; }
}
