/* ==========================================================================
   Tirusai Chatbot — Frontend Widget Styles
   All selectors are scoped to .tirusai-chatbot-* to avoid theme conflicts.
   ========================================================================== */

/* --------------------------------------------------------------------------
   CSS Custom Properties (defaults; overridden inline per settings)
   -------------------------------------------------------------------------- */
/* Guard against themes that set box-sizing: content-box globally, which
   would otherwise break every padded element inside the widget. */
.tirusai-chatbot-root,
.tirusai-chatbot-root *,
.tirusai-chatbot-root *::before,
.tirusai-chatbot-root *::after {
	box-sizing: border-box;
}

.tirusai-chatbot-root {
	--bubble-color:       #6366f1;
	--bubble-color-hover: #6366f1cc;
	--bubble-color-light: #6366f120;

	/* Light theme tokens */
	--tc-bg:           #ffffff;
	--tc-surface:      #f3f4f6;
	--tc-border:       #e5e7eb;
	--tc-text:         #111827;
	--tc-text-muted:   #6b7280;
	--tc-input-bg:     #f9fafb;
	--tc-bot-bubble:   #f3f4f6;
	--tc-bot-text:     #111827;
	--tc-user-text:    #ffffff;
	--tc-shadow:       0 8px 32px rgba(0,0,0,0.14);
	--tc-radius:       18px;
	--tc-font:         -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* Dark theme overrides */
.tirusai-chatbot-root[data-theme="dark"] {
	--tc-bg:           #1f2937;
	--tc-surface:      #111827;
	--tc-border:       #374151;
	--tc-text:         #f9fafb;
	--tc-text-muted:   #9ca3af;
	--tc-input-bg:     #374151;
	--tc-bot-bubble:   #374151;
	--tc-bot-text:     #f9fafb;
	--tc-user-text:    #ffffff;
	--tc-shadow:       0 8px 32px rgba(0,0,0,0.45);
}

/* --------------------------------------------------------------------------
   Toggle Button
   -------------------------------------------------------------------------- */
.tirusai-chatbot-toggle {
	position:         fixed;
	bottom:           max(24px, env(safe-area-inset-bottom));
	right:            max(24px, env(safe-area-inset-right));
	width:            56px;
	height:           56px;
	border-radius:    50%;
	background:       var(--bubble-color);
	border:           none;
	cursor:           pointer;
	display:          flex;
	align-items:      center;
	justify-content:  center;
	box-shadow:       0 4px 16px rgba(0,0,0,0.22);
	transition:       background 0.2s ease, transform 0.2s ease, box-shadow 0.2s ease;
	z-index:          99999;
	padding:          0;
	overflow:         hidden;
}

.tirusai-chatbot-toggle:hover {
	background:  var(--bubble-color-hover);
	transform:   scale(1.07);
	box-shadow:  0 6px 22px rgba(0,0,0,0.28);
}

.tirusai-chatbot-toggle:focus-visible {
	outline:        3px solid var(--bubble-color);
	outline-offset: 3px;
}

.tirusai-chatbot-toggle img {
	width:         100%;
	height:        100%;
	object-fit:    cover;
	border-radius: 50%;
}

.tirusai-chatbot-toggle svg {
	width:  28px;
	height: 28px;
	fill:   #ffffff;
}

/* --------------------------------------------------------------------------
   Chat Window
   -------------------------------------------------------------------------- */
.tirusai-chatbot-window {
	position:      fixed;
	bottom:        92px;
	right:         24px;
	/* Never wider than the viewport (large phones / landscape / small tablets). */
	width:         min(360px, calc(100vw - 32px));
	height:        520px;
	/* Never taller than the space between the launcher and the top of the screen. */
	max-height:    calc(100vh - 116px);
	max-height:    calc(100dvh - 116px);
	background:    var(--tc-bg);
	border:        1px solid var(--tc-border);
	border-radius: 16px;
	box-shadow:    var(--tc-shadow);
	display:       flex;
	flex-direction: column;
	z-index:       99998;
	font-family:   var(--tc-font);
	font-size:     14px;
	color:         var(--tc-text);
	overflow:      hidden;

	/* Hidden state */
	opacity:        0;
	transform:      translateY(16px) scale(0.97);
	pointer-events: none;
	transition:     opacity 0.22s ease, transform 0.22s ease;
}

.tirusai-chatbot-window.tirusai-open {
	opacity:        1;
	transform:      translateY(0) scale(1);
	pointer-events: auto;
}

/* --------------------------------------------------------------------------
   Header
   -------------------------------------------------------------------------- */
.tirusai-chatbot-header {
	display:         flex;
	align-items:     center;
	justify-content: space-between;
	padding:         14px 16px;
	background:      var(--bubble-color);
	color:           #ffffff;
	border-radius:   16px 16px 0 0;
	flex-shrink:     0;
}

.tirusai-chatbot-header-info {
	display:     flex;
	align-items: center;
	gap:         10px;
}

.tirusai-chatbot-header-avatar {
	width:         34px;
	height:        34px;
	border-radius: 50%;
	object-fit:    cover;
	border:        2px solid rgba(255,255,255,0.5);
	flex-shrink:   0;
}

.tirusai-chatbot-header-avatar-placeholder {
	width:           34px;
	height:          34px;
	border-radius:   50%;
	background:      rgba(255,255,255,0.25);
	display:         flex;
	align-items:     center;
	justify-content: center;
	flex-shrink:     0;
}

.tirusai-chatbot-header-avatar-placeholder svg {
	width:  18px;
	height: 18px;
	fill:   #ffffff;
}

.tirusai-chatbot-bot-name {
	font-weight:  600;
	font-size:    15px;
	letter-spacing: 0.01em;
}

.tirusai-chatbot-status {
	font-size:   11px;
	opacity:     0.82;
	margin-top:  1px;
}

.tirusai-chatbot-close {
	background:    none;
	border:        none;
	cursor:        pointer;
	color:         #ffffff;
	opacity:       0.85;
	padding:       4px;
	border-radius: 50%;
	display:       flex;
	align-items:   center;
	transition:    opacity 0.15s ease, background 0.15s ease;
}

.tirusai-chatbot-close:hover {
	opacity:    1;
	background: rgba(255,255,255,0.18);
}

.tirusai-chatbot-close svg {
	width:  20px;
	height: 20px;
	fill:   currentColor;
}

/* --------------------------------------------------------------------------
   Messages Area
   -------------------------------------------------------------------------- */
.tirusai-chatbot-messages {
	flex:        1;
	overflow-y:  auto;
	padding:     16px 14px;
	display:     flex;
	flex-direction: column;
	gap:         10px;
	background:  var(--tc-bg);
	scroll-behavior: smooth;
}

/* Custom scrollbar */
.tirusai-chatbot-messages::-webkit-scrollbar {
	width: 5px;
}
.tirusai-chatbot-messages::-webkit-scrollbar-track {
	background: transparent;
}
.tirusai-chatbot-messages::-webkit-scrollbar-thumb {
	background:    var(--tc-border);
	border-radius: 4px;
}

/* Message rows */
.tirusai-chatbot-msg-row {
	display:    flex;
	max-width:  100%;
}

.tirusai-chatbot-msg-row.tirusai-user {
	justify-content: flex-end;
}

.tirusai-chatbot-msg-row.tirusai-bot {
	justify-content: flex-start;
}

/* Bubble */
.tirusai-chatbot-bubble {
	max-width:     75%;
	padding:       10px 14px;
	border-radius: var(--tc-radius);
	line-height:   1.5;
	word-break:    break-word;
	white-space:   pre-wrap;
}

/* Bot bubbles with rendered markdown — override pre-wrap so HTML elements flow normally */
.tirusai-chatbot-bubble--markdown {
	white-space: normal;
}

.tirusai-chatbot-bubble--markdown p {
	margin: 0 0 6px 0;
}

.tirusai-chatbot-bubble--markdown p:last-child {
	margin-bottom: 0;
}

.tirusai-chatbot-bubble--markdown ul,
.tirusai-chatbot-bubble--markdown ol {
	margin:      4px 0 6px 0;
	padding-left: 18px;
}

.tirusai-chatbot-bubble--markdown li {
	margin-bottom: 3px;
}

.tirusai-chatbot-bubble--markdown strong {
	font-weight: 700;
}

.tirusai-chatbot-bubble--markdown em {
	font-style: italic;
}

.tirusai-chatbot-bubble--markdown br {
	display: block;
	margin:  4px 0;
	content: '';
}

.tirusai-chatbot-msg-row.tirusai-user .tirusai-chatbot-bubble {
	background:         var(--bubble-color);
	color:              var(--tc-user-text);
	border-bottom-right-radius: 4px;
}

.tirusai-chatbot-msg-row.tirusai-bot .tirusai-chatbot-bubble {
	background:       var(--tc-bot-bubble);
	color:            var(--tc-bot-text);
	border-bottom-left-radius: 4px;
}

/* Typing indicator */
.tirusai-chatbot-typing {
	display:     flex;
	align-items: center;
	gap:         5px;
	padding:     12px 16px;
}

.tirusai-chatbot-typing span {
	width:         8px;
	height:        8px;
	border-radius: 50%;
	background:    var(--tc-text-muted);
	display:       inline-block;
	animation:     tirusai-bounce 1.2s ease-in-out infinite;
}

.tirusai-chatbot-typing span:nth-child(2) { animation-delay: 0.2s; }
.tirusai-chatbot-typing span:nth-child(3) { animation-delay: 0.4s; }

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

/* --------------------------------------------------------------------------
   Input Area
   -------------------------------------------------------------------------- */
.tirusai-chatbot-input-area {
	display:       flex;
	align-items:   center;
	gap:           8px;
	padding:       12px 14px;
	border-top:    1px solid var(--tc-border);
	background:    var(--tc-bg);
	flex-shrink:   0;
	border-radius: 0 0 16px 16px;
}

.tirusai-chatbot-input {
	flex:          1;
	border:        1px solid var(--tc-border);
	border-radius: 24px;
	padding:       9px 16px;
	font-size:     14px;
	font-family:   var(--tc-font);
	background:    var(--tc-input-bg);
	color:         var(--tc-text);
	outline:       none;
	resize:        none;
	transition:    border-color 0.15s ease, box-shadow 0.15s ease;
	line-height:   1.4;
	max-height:    96px;
	overflow-y:    auto;
	min-width:     auto;
}

.tirusai-chatbot-input::placeholder {
	color: var(--tc-text-muted);
}

.tirusai-chatbot-input:focus {
	border-color: var(--bubble-color);
	box-shadow:   0 0 0 3px var(--bubble-color-light);
}

.tirusai-chatbot-send {
	width:           38px;
	height:          38px;
	border-radius:   50%;
	background:      var(--bubble-color);
	border:          none;
	cursor:          pointer;
	display:         flex;
	align-items:     center;
	justify-content: center;
	flex-shrink:     0;
	transition:      background 0.15s ease, transform 0.1s ease;
}

.tirusai-chatbot-send:hover:not(:disabled) {
	background: var(--bubble-color-hover);
	transform:  scale(1.07);
}

.tirusai-chatbot-send:disabled {
	opacity: 0.5;
	cursor:  not-allowed;
}

.tirusai-chatbot-send svg {
	width:  18px;
	height: 18px;
	fill:   #ffffff;
}

/* --------------------------------------------------------------------------
   Responsive — full screen on mobile
   -------------------------------------------------------------------------- */
@media (max-width: 520px) {
	.tirusai-chatbot-window {
		/* Fill the screen. Use 100% (not 100vw) so a scrollbar can't push
		   content sideways, and pin all four edges with inset. */
		width:         100%;
		max-width:     100%;
		/* vh first as a fallback, then dvh so the layout tracks the mobile
		   URL bar and on-screen keyboard on browsers that support it. */
		height:        100vh;
		height:        100dvh;
		max-height:    100vh;
		max-height:    100dvh;
		inset:         0;
		border-radius: 0;
		border:        none;
	}

	/* Pad the header down past the notch / status bar in fullscreen mode. */
	.tirusai-chatbot-header {
		border-radius: 0;
		padding-top:   max(14px, env(safe-area-inset-top));
		padding-left:  max(16px, env(safe-area-inset-left));
		padding-right: max(16px, env(safe-area-inset-right));
	}

	.tirusai-chatbot-input-area {
		border-radius:  0;
		padding-bottom: max(12px, env(safe-area-inset-bottom));
		padding-left:   max(14px, env(safe-area-inset-left));
		padding-right:  max(14px, env(safe-area-inset-right));
	}

	/* 16px keeps iOS Safari from auto-zooming the page when the field
	   is focused — a frequent cause of "broken" mobile layouts. */
	.tirusai-chatbot-input {
		font-size: 16px;
	}

	.tirusai-chatbot-toggle {
		bottom: max(16px, env(safe-area-inset-bottom));
		right:  max(16px, env(safe-area-inset-right));
	}

	/* Wider bubbles read better on a full-width screen. */
	.tirusai-chatbot-bubble {
		max-width: 85%;
	}

	/* Hide the round launcher while the full-screen chat is open on mobile.
	   The header already provides a close button. */
	.tirusai-chatbot-toggle[aria-expanded="true"] {
		display: none;
	}

	/* Lock the background page behind the fullscreen chat (class toggled by JS). */
	html.tirusai-chatbot-scroll-locked,
	body.tirusai-chatbot-scroll-locked {
		overflow: hidden;
	}
}
