:root {
	--bg: #f7f7f8;
	--surface: #ffffff;
	--border: #e3e3e8;
	--text: #1f2024;
	--muted: #8a8a93;
	--user-bg: #2f6df6;
	--user-text: #ffffff;
	--model-bg: #ececf1;
	--model-text: #1f2024;
}

* {
	box-sizing: border-box;
}

body {
	margin: 0;
	font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
	line-height: 1.6;
	color: var(--text);
	background: var(--bg);
}

/* header */
header {
	display: flex;
	align-items: center;
	padding: 12px 20px;
	background: var(--surface);
	border-bottom: 1px solid var(--border);
}

header h1 {
	margin: 0;
	font-size: 1.15rem;
}

.header-nav {
	display: flex;
	align-items: center;
	gap: 12px;
	margin-left: auto;
}

.header-nav a {
	color: var(--muted);
	text-decoration: none;
	font-size: 0.9rem;
}
.header-nav a:hover { color: var(--text); }
.header-nav form { margin: 0; }

#logout-form button {
	background: transparent;
	color: #e5534b;
	border: 1px solid var(--border);
}
#logout-form button:hover { background: var(--bg); }

main {
	max-width: 720px;
	margin: 0 auto;
	padding: 20px 16px 40px;
}

/* --- history area --- */
.chat-box {
	padding: 12px;
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 12px;
}
#history {
	display: flex;
	flex-direction: column;
	gap: 12px;
	max-height: 65vh;
	overflow-y: auto;
}

.empty {
	color: var(--muted);
	text-align: center;
	margin: 24px 0;
}

/* --- empty-conversation placeholder --- */
.empty-chat-history {
	display: none;
}

#history:not(:has(.chatmessage)) + .empty-chat-history {
	display: block;
}

/* --- message bubbles --- */
.chatmessage {
	max-width: 78%;
	padding: 8px 14px;
	border-radius: 16px;
	word-wrap: break-word;
}

.chatmessage .role {
	display: block;
	font-size: 0.7rem;
	opacity: 0.7;
	margin-bottom: 2px;
}

.chatmessage p {
	margin: 0;
	white-space: pre-wrap;
}

/* user: right-aligned, blue */
.chatmessage-user {
	align-self: flex-end;
	background: var(--user-bg);
	color: var(--user-text);
	border-bottom-right-radius: 4px;
}

/* model: left-aligned, gray */
.chatmessage-model {
	align-self: flex-start;
	background: var(--model-bg);
	color: var(--model-text);
	border-bottom-left-radius: 4px;
}

.message-images {
	display: flex;
	flex-wrap: wrap;
	gap: 6px;
	margin-bottom: 6px;
}

.message-images img {
	max-width: 240px;
	max-height: 240px;
	border-radius: 8px;
	display: block;
}

.streaming:empty::before {
  content: "考え中…";
  color: #888;
  font-style: italic;
}


/* --- input form --- */
form {
	display: flex;
	gap: 8px;
	margin-top: 16px;
}

input[type="text"],
#compose textarea {
	flex: 1;
	padding: 10px 14px;
	border: 1px solid var(--border);
	border-radius: 999px;
	font-size: 1rem;
	background: var(--surface);
	color: var(--text);
}

input[type="text"]:focus,
#compose textarea:focus {
	outline: none;
	border-color: var(--user-bg);
}

/* A grown box needs a real radius, not 999px: at one line 22px still reads as
   a pill, at ten it stays a rounded box instead of a lozenge. font-family has
   to be spelled out because a textarea does not inherit it from the page. */
#compose textarea {
	border-radius: 22px;
	resize: none;
	overflow-y: auto;
	max-height: 40vh;
	font-family: inherit;
	line-height: 1.5;
}

button {
	padding: 10px 18px;
	border: none;
	border-radius: 999px;
	background: var(--user-bg);
	color: #fff;
	font-size: 0.95rem;
	cursor: pointer;
}

button:hover {
	opacity: 0.9;
}

/* --- auth forms (login / register): stacked --- */
/* The default form is flex-row for the chat input. Auth forms switch to a
   column so each label sits above its input, and text/password inputs
   share the same look. */
#login-form,
#register-form {
	flex-direction: column;
	align-items: stretch;
	gap: 6px;
	max-width: 360px;
	margin: 24px auto;
}

#login-form label,
#register-form label {
	margin-top: 6px;
	font-size: 0.9rem;
	color: var(--text);
}

#login-form input[type="text"],
#login-form input[type="password"],
#register-form input[type="text"],
#register-form input[type="password"] {
	flex: none;
	width: 100%;
	box-sizing: border-box;
	padding: 10px 14px;
	border: 1px solid var(--border);
	border-radius: 8px;
	font-size: 1rem;
	background: var(--surface);
	color: var(--text);
}

#login-form input:focus,
#register-form input:focus {
	outline: none;
	border-color: var(--user-bg);
}

#login-form button,
#register-form button {
	margin-top: 12px;
}

#login-message,
#register-message {
	max-width: 360px;
	margin: 16px auto 0;
	color: #e5534b;
	font-size: 0.9rem;
}

/* --- conversation list sidebar --- */
/* widen main only on the chat screen (other pages stay at 720px) */
main:has(.chat-layout) { max-width: 1080px; }

.chat-layout {
	display: flex;
	gap: 16px;
	align-items: flex-start;
}

.sidebar {
	width: 240px;
	flex-shrink: 0;
	display: flex;
	flex-direction: column;
	gap: 8px;
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 12px;
	padding: 10px;
}

.sidebar form { margin: 0; }       /* cancel the default form margin-top */
.new-conv { flex: 1; }             /* make the new-conversation button full width */

.conv-list {
	display: flex;
	flex-direction: column;
	gap: 2px;
	max-height: 60vh;
	overflow-y: auto;
}

.conv-row {
	display: flex;
	align-items: center;
	border-radius: 8px;
}
.conv-row:hover { background: var(--bg); }
.conv-row.active { background: var(--model-bg); font-weight: 600; }

.conv-title {
	flex: 1;
	min-width: 0;   /* allow the flex item to shrink so ellipsis works */
	padding: 8px 10px;
	text-decoration: none;
	color: var(--text);
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.conv-del {
	border: none;
	background: none;
	padding: 4px 8px;
	border-radius: 6px;
	color: var(--muted, #888);
	cursor: pointer;
	font-size: 12px;
	line-height: 1;
}

.chat-main {
	flex: 1;
	min-width: 0;   /* allow the flex item to shrink (needed for #history overflow) */
}

/* --- settings / sudo pages --- */
/* render each settings section as a card */
#account,
#persona,
#logout {
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: 12px;
	padding: 16px 18px;
	margin: 16px 0;
}
#account h3 { margin-top: 0; }
#account h4 { margin-bottom: 6px; }
#persona h3 { margin-top: 0; }

/* persona editor: stacked form (label above field), like the auth forms */
#persona form {
	flex-direction: column;
	align-items: stretch;
	gap: 6px;
	margin-top: 0;
}
#persona label {
	margin-top: 6px;
	font-size: 0.9rem;
	color: var(--text);
}
#persona input[type="text"] {
	flex: none;
	width: 100%;
	border-radius: 8px;   /* not the 999px pill used by the chat input */
}
#persona textarea {
	width: 100%;
	min-height: 160px;
	padding: 10px 14px;
	border: 1px solid var(--border);
	border-radius: 8px;
	font: inherit;
	line-height: 1.6;
	background: var(--surface);
	color: var(--text);
	resize: vertical;
}
#persona textarea:focus,
#persona input[type="text"]:focus {
	outline: none;
	border-color: var(--user-bg);
}
#persona button {
	margin-top: 12px;
	align-self: flex-start;   /* natural width, not stretched into a blob */
}

/* login page footer: register and Google login as matching outline buttons */
.login-alt {
	display: flex;
	gap: 10px;
	max-width: 360px;
	margin: 16px auto 0;
}
.login-alt a {
	flex: 1;
	text-align: center;
	padding: 9px 12px;
	border-radius: 999px;
	background: var(--surface);
	color: var(--text);
	border: 1px solid var(--border);
	text-decoration: none;
	font-size: 0.9rem;
	font-weight: 600;
	white-space: nowrap;
}
.login-alt a:hover { background: var(--bg); }

/* settings link-Google uses the same outline (distinct from the primary blue) */
.google-login {
	display: inline-block;
	padding: 9px 16px;
	border-radius: 999px;
	background: var(--surface);
	color: var(--text);
	border: 1px solid var(--border);
	text-decoration: none;
	font-size: 0.9rem;
	font-weight: 600;
	white-space: nowrap;
}
.google-login:hover { background: var(--bg); }

/* logout inside settings is a ghost button (matches the chat logout) */
#logout form { margin: 0; justify-content: flex-start; }
#logout button {
	background: transparent;
	color: #e5534b;
	border: 1px solid var(--border);
}
#logout button:hover { background: var(--bg); }

/* sudo password re-entry form (same column layout as the auth forms) */
#sudo-form {
	flex-direction: column;
	align-items: stretch;
	gap: 6px;
	max-width: 360px;
	margin: 16px 0;
}
#sudo-form input[type="password"] {
	flex: none;
	width: 100%;
	padding: 10px 14px;
	border: 1px solid var(--border);
	border-radius: 8px;
	font-size: 1rem;
	background: var(--surface);
	color: var(--text);
}
#sudo-form input:focus { outline: none; border-color: var(--user-bg); }
#sudo-form button { margin-top: 12px; align-self: flex-start; }

/* flash message (neutral color: used for both success and failure) */
#settings-message,
#sudo-message {
	max-width: 480px;
	margin: 12px 0;
	padding: 10px 14px;
	border-radius: 8px;
	font-size: 0.9rem;
	background: var(--model-bg);
	color: var(--text);
	border: 1px solid var(--border);
}

@media (hover: hover) {
	.conv-del { visibility: hidden;}
	.conv-row:hover .conv-del { visibility: visible; }
	.conv-del:hover { background: var(--border); color: var(--text); }
}

/* --- mobile view --- */
@media (max-width: 640px) {
	body:has(.chat-layout) {
		height: 100dvh;
		display: flex;
		flex-direction: column;
		overflow: hidden;
	}
	main:has(.chat-layout) { flex: 1; min-height: 0; display: flex; }

	.chat-layout {
		flex-direction: column;
		align-items: stretch;
		flex: 1;
		min-height: 0;
		min-width: 0;
	}

	.sidebar {
		width: auto;
		flex-shrink: 1;
	}

	.conv-list {
		flex-direction: row;
		overflow-x: auto;
		max-height: none;
		gap: 6px;
	}
	.conv-row { flex-shrink: 0; }

	.chat-main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
	.chat-box  { flex: 1; min-height: 0; display: flex; flex-direction: column; }
	#history   { flex: 1; min-height: 0; max-height: none; }

	.chatmessage {
		max-width: 90%;
	}

	main {
		padding: 12px 10px;
		width: 100%;
	}
}

/* --- dark mode --- */
@media (prefers-color-scheme: dark) {
	:root {
		--bg: #1a1a1d;
		--surface: #242428;
		--border: #34343a;
		--text: #ececf1;
		--muted: #9a9aa3;
		--model-bg: #34343a;
		--model-text: #ececf1;
	}
}

/* --- image attachments --- */

/* The tray takes a full-width line of its own, so the input row keeps its
   shape no matter how many images are attached. */
#compose { flex-wrap: wrap; align-items: flex-end; }

.attachments {
	flex-basis: 100%;
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
	margin-bottom: 4px;
}

.chip {
	position: relative;
	width: 56px;
	height: 56px;
	border: 1px solid var(--border);
	border-radius: 8px;
	overflow: hidden;
}

.chip img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* No hidden input inside means no key came back yet: still uploading, or it
   failed. The look is tied to the data, so a chip cannot appear ready while
   the key is missing. */
.chip:not(:has(input)) img { opacity: .45; }
.chip-failed { border-color: #e5534b; }

.chip-drop {
	position: absolute;
	top: 2px;
	right: 2px;
	width: 18px;
	height: 18px;
	padding: 0;
	display: grid;
	place-items: center;
	font-size: .8rem;
	line-height: 1;
	background: rgba(0, 0, 0, .55);
}

.attach {
	display: grid;
	place-items: center;
	width: 42px;
	height: 42px;
	padding: 0;
	border: 1px solid var(--border);
	border-radius: 999px;
	background: var(--surface);
	color: var(--muted);
}

/* The icon is a mask, not an <img>: the file supplies the shape and
   currentColor supplies the colour. One SVG serves every state, and no copy
   of the path data lives in a template. */
.attach::before {
	content: "";
	width: 24px;
	height: 24px;
	background: currentColor;
	-webkit-mask: url("/static/icon-image.svg") center / contain no-repeat;
	        mask: url("/static/icon-image.svg") center / contain no-repeat;
}

.attach:hover { color: var(--user-bg); border-color: var(--user-bg); opacity: 1; }

button:disabled { opacity: .45; cursor: default; }
