/*
 * NeccoSpeak — widget styles
 * Everything is scoped with the .neccospeak- prefix to minimize theme interference.
 * widget.js sets brand_color inline as --neccospeak-brand on
 * #neccospeak-root (widget.js:108).
 * A fallback is provided for when it is not set.
 *
 * Class names must match the DOM generated by widget.js exactly (a contract):
 *   Launcher    : .neccospeak-launcher / .neccospeak-launcher-icon
 *   Panel toggle: .neccospeak-panel.neccospeak-open
 *   Header      : .neccospeak-header / .neccospeak-header-title / .neccospeak-close
 *   List        : .neccospeak-messages
 *   Message     : .neccospeak-msg(.neccospeak-msg-user|.neccospeak-msg-assistant)
 *                 > .neccospeak-bubble
 *   Sending     : .neccospeak-typing / .neccospeak-dot
 *   Status      : .neccospeak-status(.neccospeak-status-error)
 *   Input row   : .neccospeak-input-row / .neccospeak-input / .neccospeak-send
 *   Voice       : .neccospeak-voice-btn(.neccospeak-voice-active|.neccospeak-voice-connecting)
 */

/* Root container: the anchor point for variable definitions and resets */
#neccospeak-root {
	--neccospeak-brand: #2f6fed;
	/* Derived colors (foreground, border, background) */
	--aal-color-fg: #ffffff;
	--aal-surface: #ffffff;
	--aal-surface-alt: #f4f6fb;
	--aal-text: #1f2533;
	--aal-text-muted: #6b7280;
	--aal-border: #e2e6ef;
	--aal-shadow: 0 12px 32px rgba(17, 24, 39, 0.18);
	--aal-radius: 14px;
	--aal-z: 2147483000;

	position: static;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
		"Helvetica Neue", Arial, "Hiragino Sans", "Noto Sans JP", sans-serif;
	font-size: 15px;
	line-height: 1.5;
	color: var(--aal-text);
}

/* Normalize box-sizing within the scope (minimal rules to override the theme's * selector) */
.neccospeak-launcher,
.neccospeak-launcher *,
.neccospeak-panel,
.neccospeak-panel * {
	box-sizing: border-box;
}

/* ===== Launcher button ===== */
.neccospeak-launcher {
	position: fixed;
	right: 20px;
	bottom: 20px;
	z-index: var(--aal-z);
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 60px;
	height: 60px;
	padding: 0;
	margin: 0;
	border: none;
	border-radius: 50%;
	background: var(--neccospeak-brand);
	color: var(--aal-color-fg);
	cursor: pointer;
	box-shadow: var(--aal-shadow);
	-webkit-appearance: none;
	appearance: none;
	transition: transform 0.18s ease, box-shadow 0.18s ease,
		opacity 0.18s ease;
}

.neccospeak-launcher:hover {
	transform: translateY(-2px);
	box-shadow: 0 16px 36px rgba(17, 24, 39, 0.24);
}

.neccospeak-launcher:active {
	transform: translateY(0);
}

.neccospeak-launcher:focus-visible {
	outline: 3px solid var(--neccospeak-brand);
	outline-offset: 3px;
}

.neccospeak-launcher svg,
.neccospeak-launcher .neccospeak-launcher-icon {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 28px;
	height: 28px;
	pointer-events: none;
}

/* ===== Chat panel ===== */
.neccospeak-panel {
	position: fixed;
	right: 20px;
	bottom: 20px;
	z-index: var(--aal-z);
	display: flex;
	flex-direction: column;
	width: 380px;
	max-width: calc(100vw - 32px);
	height: 560px;
	max-height: calc(100vh - 40px);
	margin: 0;
	background: var(--aal-surface);
	border: 1px solid var(--aal-border);
	border-radius: var(--aal-radius);
	box-shadow: var(--aal-shadow);
	overflow: hidden;
	opacity: 0;
	transform: translateY(12px) scale(0.98);
	transform-origin: bottom right;
	pointer-events: none;
	transition: opacity 0.2s ease, transform 0.2s ease;
}

.neccospeak-panel.neccospeak-open {
	opacity: 1;
	transform: translateY(0) scale(1);
	pointer-events: auto;
}

/* ===== Header ===== */
.neccospeak-header {
	display: flex;
	align-items: center;
	gap: 8px;
	padding: 14px 16px;
	background: var(--neccospeak-brand);
	color: var(--aal-color-fg);
	flex: 0 0 auto;
}

.neccospeak-header-title {
	flex: 1 1 auto;
	margin: 0;
	font-size: 16px;
	font-weight: 600;
	line-height: 1.3;
	color: inherit;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.neccospeak-close {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 32px;
	height: 32px;
	padding: 0;
	margin: 0;
	border: none;
	border-radius: 8px;
	background: transparent;
	color: inherit;
	font-size: 22px;
	line-height: 1;
	cursor: pointer;
	-webkit-appearance: none;
	appearance: none;
	transition: background-color 0.15s ease;
}

.neccospeak-close:hover {
	background: rgba(255, 255, 255, 0.18);
}

.neccospeak-close:focus-visible {
	outline: 2px solid var(--aal-color-fg);
	outline-offset: 2px;
}

/* ===== Message list ===== */
.neccospeak-messages {
	flex: 1 1 auto;
	min-height: 0;
	padding: 16px;
	margin: 0;
	overflow-y: auto;
	-webkit-overflow-scrolling: touch;
	background: var(--aal-surface-alt);
	display: flex;
	flex-direction: column;
	gap: 10px;
}

/* Message row (wrapper) */
.neccospeak-msg {
	display: flex;
	max-width: 100%;
}

.neccospeak-msg-user {
	justify-content: flex-end;
}

.neccospeak-msg-assistant {
	justify-content: flex-start;
}

/* Bubble body */
.neccospeak-bubble {
	max-width: 82%;
	padding: 9px 13px;
	border-radius: 14px;
	font-size: 14.5px;
	line-height: 1.55;
	white-space: pre-wrap;
	overflow-wrap: anywhere;
	word-break: break-word;
}

/* User message (right-aligned, brand color) */
.neccospeak-msg-user .neccospeak-bubble {
	background: var(--neccospeak-brand);
	color: var(--aal-color-fg);
	border-bottom-right-radius: 4px;
}

/* Assistant message (left-aligned, white) */
.neccospeak-msg-assistant .neccospeak-bubble {
	background: var(--aal-surface);
	color: var(--aal-text);
	border: 1px solid var(--aal-border);
	border-bottom-left-radius: 4px;
}

/* ===== Typing indicator ===== */
/* The typing row is generated as .neccospeak-msg.neccospeak-msg-assistant.neccospeak-typing
 * and contains .neccospeak-bubble > .neccospeak-dot×3 inside. */
.neccospeak-typing .neccospeak-bubble {
	display: inline-flex;
	align-items: center;
	gap: 5px;
	padding: 11px 14px;
	background: var(--aal-surface);
	border: 1px solid var(--aal-border);
	border-bottom-left-radius: 4px;
}

.neccospeak-dot {
	width: 7px;
	height: 7px;
	border-radius: 50%;
	background: var(--aal-text-muted);
	opacity: 0.5;
	animation: neccospeak-bounce 1.2s infinite ease-in-out;
}

.neccospeak-dot:nth-child(2) {
	animation-delay: 0.18s;
}

.neccospeak-dot:nth-child(3) {
	animation-delay: 0.36s;
}

@keyframes neccospeak-bounce {
	0%, 60%, 100% {
		transform: translateY(0);
		opacity: 0.5;
	}
	30% {
		transform: translateY(-5px);
		opacity: 1;
	}
}

/* ===== Status row (error / voice state) ===== */
.neccospeak-status {
	flex: 0 0 auto;
	padding: 6px 12px 0;
	font-size: 12px;
	color: var(--aal-text-muted);
	background: var(--aal-surface);
	text-align: center;
}

.neccospeak-status:empty {
	display: none;
}

.neccospeak-status-error {
	color: #a31515;
}

/* ===== Input row (text field + buttons) ===== */
.neccospeak-input-row {
	flex: 0 0 auto;
	display: flex;
	align-items: flex-end;
	gap: 8px;
	padding: 12px;
	background: var(--aal-surface);
	border-top: 1px solid var(--aal-border);
}

.neccospeak-input {
	flex: 1 1 auto;
	min-width: 0;
	max-height: 120px;
	min-height: 42px;
	padding: 10px 12px;
	margin: 0;
	border: 1px solid var(--aal-border);
	border-radius: 10px;
	background: var(--aal-surface);
	color: var(--aal-text);
	font-family: inherit;
	font-size: 14.5px;
	line-height: 1.45;
	resize: none;
	overflow-y: auto;
	-webkit-appearance: none;
	appearance: none;
	transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.neccospeak-input::placeholder {
	color: var(--aal-text-muted);
	opacity: 1;
}

.neccospeak-input:focus,
.neccospeak-input:focus-visible {
	outline: none;
	border-color: var(--neccospeak-brand);
	box-shadow: 0 0 0 3px color-mix(in srgb, var(--neccospeak-brand) 22%, transparent);
}

/* Fallback for browsers that do not support color-mix */
@supports not (color: color-mix(in srgb, red 50%, blue)) {
	.neccospeak-input:focus,
	.neccospeak-input:focus-visible {
		box-shadow: 0 0 0 3px rgba(47, 111, 237, 0.22);
	}
}

/* Send button */
.neccospeak-send {
	flex: 0 0 auto;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	min-width: 56px;
	height: 42px;
	padding: 0 14px;
	margin: 0;
	border: none;
	border-radius: 10px;
	background: var(--neccospeak-brand);
	color: var(--aal-color-fg);
	font-family: inherit;
	font-size: 14px;
	font-weight: 600;
	cursor: pointer;
	-webkit-appearance: none;
	appearance: none;
	transition: opacity 0.15s ease, transform 0.1s ease;
}

.neccospeak-send:hover {
	opacity: 0.9;
}

.neccospeak-send:active {
	transform: scale(0.96);
}

.neccospeak-send:focus-visible {
	outline: 2px solid var(--neccospeak-brand);
	outline-offset: 2px;
}

.neccospeak-send:disabled {
	opacity: 0.45;
	cursor: not-allowed;
}

/* ===== Voice button ===== */
.neccospeak-voice-btn {
	flex: 0 0 auto;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 42px;
	height: 42px;
	padding: 0;
	margin: 0;
	border: 1px solid var(--aal-border);
	border-radius: 10px;
	background: var(--aal-surface);
	color: var(--aal-text);
	font-size: 18px;
	line-height: 1;
	cursor: pointer;
	-webkit-appearance: none;
	appearance: none;
	transition: background-color 0.15s ease, border-color 0.15s ease,
		color 0.15s ease;
}

.neccospeak-voice-btn:hover {
	border-color: var(--neccospeak-brand);
	color: var(--neccospeak-brand);
}

.neccospeak-voice-btn:focus-visible {
	outline: 2px solid var(--neccospeak-brand);
	outline-offset: 2px;
}

/* Connecting state */
.neccospeak-voice-connecting {
	border-color: var(--neccospeak-brand);
	color: var(--neccospeak-brand);
	opacity: 0.8;
}

/* Recording / in-call state */
.neccospeak-voice-active {
	background: var(--neccospeak-brand);
	border-color: var(--neccospeak-brand);
	color: var(--aal-color-fg);
	animation: neccospeak-pulse 1.6s infinite ease-in-out;
}

@keyframes neccospeak-pulse {
	0%, 100% {
		box-shadow: 0 0 0 0 color-mix(in srgb, var(--neccospeak-brand) 45%, transparent);
	}
	50% {
		box-shadow: 0 0 0 6px color-mix(in srgb, var(--neccospeak-brand) 0%, transparent);
	}
}

/* ===== Scrollbar (optional cosmetic tweaks) ===== */
.neccospeak-messages::-webkit-scrollbar {
	width: 8px;
}

.neccospeak-messages::-webkit-scrollbar-thumb {
	background: rgba(107, 114, 128, 0.35);
	border-radius: 8px;
}

/* ===== Responsive (small screens go near-fullscreen) ===== */
@media (max-width: 480px) {
	.neccospeak-panel {
		right: 0;
		left: 0;
		bottom: 0;
		width: 100%;
		max-width: 100%;
		height: 100%;
		max-height: 100%;
		border-radius: 0;
		border: none;
	}

	.neccospeak-launcher {
		right: 16px;
		bottom: 16px;
	}
}

/* ===== Reduced-motion support ===== */
@media (prefers-reduced-motion: reduce) {
	.neccospeak-launcher,
	.neccospeak-panel,
	.neccospeak-send,
	.neccospeak-voice-btn,
	.neccospeak-close,
	.neccospeak-input {
		transition: none !important;
	}

	.neccospeak-dot,
	.neccospeak-voice-active {
		animation: none !important;
	}

	.neccospeak-panel {
		transform: none;
	}
}
