/*
 * Kimia webview — three layouts, one document.
 *
 * The three are not one layout at three widths. Each drops or promotes
 * something the others cannot afford:
 *
 *   mobile   < 768   one column, composer pinned to the viewport, product
 *                    cards in a horizontal strip so they cost one row instead
 *                    of four.
 *   tablet   768+    centred reading column, cards two-up, composer inside the
 *                    column rather than pinned to the glass.
 *   desktop  1024+   a side rail appears with the membership level and the
 *                    conversation openers, which the narrow layouts hide
 *                    because vertical space is the scarce thing there.
 *
 * Mobile first, so the base rules are the phone and each breakpoint only adds.
 */

:root {
	--ink: #241c33;
	--ink-soft: #6b6379;
	--line: #e7e2ee;
	--page: #f7f5fa;
	--card: #ffffff;
	--brand: #4b2678;
	--brand-soft: #f3edfb;
	--warn: #8a4b12;
	--warn-soft: #fdf1e3;
	--radius: 14px;
	/* The phone's home-indicator strip. Zero everywhere else. */
	--safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
	box-sizing: border-box;
}

html,
body {
	height: 100%;
	margin: 0;
}

body.kimia {
	background: var(--page);
	color: var(--ink);
	font: 15px/1.75 Vazirmatn, "IRANSans", Tahoma, system-ui, sans-serif;
	/* The composer is fixed on mobile; without this the whole page rubber-bands
	   behind it when the log is short. */
	overscroll-behavior: none;
}

.kimia__sronly {
	position: absolute;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	overflow: hidden;
	clip: rect(0 0 0 0);
	white-space: nowrap;
	border: 0;
}

/* ------------------------------------------------------------------ frame */

/*
 * dvh, not vh. On mobile Safari and Chrome the URL bar makes vh taller than
 * what is actually on screen, which pushes the composer under the fold — the
 * one element that must never be off-screen.
 */
.kimia__frame {
	display: grid;
	grid-template-areas:
		"bar"
		"talk";
	grid-template-rows: auto minmax(0, 1fr);
	height: 100dvh;
	max-height: 100dvh;
}

@supports not (height: 100dvh) {
	.kimia__frame {
		height: 100vh;
		max-height: 100vh;
	}
}

/* -------------------------------------------------------------------- bar */

.kimia__bar {
	grid-area: bar;
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 12px;
	padding: 10px 14px;
	background: var(--card);
	border-bottom: 1px solid var(--line);
}

.kimia__id {
	display: flex;
	align-items: center;
	gap: 10px;
	min-width: 0;
}

.kimia__mark {
	display: grid;
	place-items: center;
	width: 36px;
	height: 36px;
	flex: 0 0 36px;
	border-radius: 50%;
	background: var(--brand);
	color: #fff;
	font-weight: 700;
}

.kimia__names {
	display: flex;
	flex-direction: column;
	min-width: 0;
}

.kimia__names strong {
	font-size: 15px;
	line-height: 1.3;
}

.kimia__names small {
	color: var(--ink-soft);
	font-size: 12px;
	line-height: 1.3;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.kimia__quota {
	flex: 0 0 auto;
	padding: 5px 11px;
	border-radius: 999px;
	background: var(--brand-soft);
	color: var(--brand);
	font-size: 12px;
	font-weight: 700;
	white-space: nowrap;
}

.kimia__quota.is-low {
	background: var(--warn-soft);
	color: var(--warn);
}

.kimia__quota.is-out {
	background: #fdeaea;
	color: #98241f;
}

/* ------------------------------------------------------------------- rail */

/* Hidden until there is width to spare. display:none rather than a media query
   around the markup, so the same document serves all three. */
.kimia__rail {
	display: none;
	grid-area: rail;
}

/* ------------------------------------------------------------------- talk */

.kimia__talk {
	grid-area: talk;
	display: grid;
	grid-template-rows: minmax(0, 1fr) auto auto;
	min-height: 0;
}

.kimia__log {
	overflow-y: auto;
	-webkit-overflow-scrolling: touch;
	padding: 16px 14px 8px;
	display: flex;
	flex-direction: column;
	gap: 14px;
}

/* --------------------------------------------------------------- messages */

.kimia__msg {
	max-width: 86%;
	padding: 10px 14px;
	border-radius: var(--radius);
	white-space: pre-wrap;
	word-break: break-word;
}

.kimia__msg--me {
	align-self: flex-start;
	background: var(--brand);
	color: #fff;
	border-end-start-radius: 4px;
}

.kimia__msg--her {
	align-self: flex-end;
	background: var(--card);
	border: 1px solid var(--line);
	border-end-end-radius: 4px;
}

.kimia__msg--err {
	align-self: center;
	max-width: 100%;
	background: #fdeaea;
	color: #98241f;
	border: 1px solid #f5c9c7;
	font-size: 13px;
	text-align: center;
}

/* The follow-up question, set apart from the answer above it so it reads as a
   prompt to reply rather than as more prose. */
.kimia__ask {
	margin-top: 8px;
	padding-top: 8px;
	border-top: 1px dashed var(--line);
	font-weight: 700;
}

/* The three dots while she thinks. */
.kimia__wait {
	align-self: flex-end;
	display: flex;
	gap: 4px;
	padding: 14px 16px;
	background: var(--card);
	border: 1px solid var(--line);
	border-radius: var(--radius);
}

.kimia__wait i {
	width: 6px;
	height: 6px;
	border-radius: 50%;
	background: var(--ink-soft);
	animation: kimia-bounce 1.2s infinite;
}

.kimia__wait i:nth-child(2) {
	animation-delay: 0.15s;
}

.kimia__wait i:nth-child(3) {
	animation-delay: 0.3s;
}

@keyframes kimia-bounce {
	0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
	30%           { transform: translateY(-4px); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
	.kimia__wait i {
		animation: none;
	}
}

/* --------------------------------------------------------------- products */

/*
 * Mobile: a horizontal strip. Four stacked cards would bury the answer that
 * introduced them and push the composer off screen, so they scroll sideways
 * and cost one row.
 */
.kimia__goods {
	align-self: stretch;
	/*
	 * Kimia speaks from the left of an RTL page, and these cards are part of
	 * what she just said — so the first one has to begin where her bubble
	 * begins. An RTL grid lays its first column against the right edge, which
	 * put the opening card under the customer's own messages, a screen away
	 * from the sentence that introduced it. The cards themselves are set back
	 * to rtl below, so the Persian inside them is unaffected.
	 */
	direction: ltr;
	display: grid;
	grid-auto-flow: column;
	grid-auto-columns: 66%;
	gap: 10px;
	overflow-x: auto;
	-webkit-overflow-scrolling: touch;
	scroll-snap-type: x mandatory;
	padding-bottom: 4px;
	/*
	 * The strip runs to both edges of the screen so a card can scroll off it,
	 * while its own padding keeps the first card level with the text above.
	 */
	margin: 0 -14px;
	padding-inline: 14px;
	/*
	 * Snapping otherwise lands the first card's edge on the container's edge,
	 * scrolling straight past that padding — the strip opened at scrollLeft
	 * 14.4 and the card sat at -0.4, hard against the screen while Kimia's
	 * bubble began at 14. Telling the snap where the content starts is what
	 * lines the two up.
	 */
	scroll-padding-inline: 14px;
}

.kimia__good {
	/* The strip is laid out left to right; the name and price inside are not. */
	direction: rtl;
	scroll-snap-align: start;
	display: flex;
	flex-direction: column;
	background: var(--card);
	border: 1px solid var(--line);
	border-radius: var(--radius);
	overflow: hidden;
	text-decoration: none;
	color: inherit;
}

/*
 * The square is reserved before the photo arrives, so a row of cards does not
 * reflow as each one loads. It stays a plain tile while loading and only shows
 * the "no picture" mark when there is genuinely no image.
 */
.kimia__good-pic {
	position: relative;
	width: 100%;
	aspect-ratio: 1;
	/* The card is a flex column and flex items shrink by default, which
	   quietly wins over aspect-ratio: the square became 228×205 and every
	   product photo was squashed by a tenth. */
	flex: 0 0 auto;
	background: var(--page);
	overflow: hidden;
}

/*
 * Visible from the start. An earlier version faded the photo in on the JS load
 * event, which meant a single missed event left a fully downloaded image at
 * opacity 0 — a blank card with the picture sitting right there in it. The
 * reserved square above already stops the reflow that the fade was for, so
 * nothing is gained by making paint depend on script.
 */
.kimia__good-pic img {
	width: 100%;
	height: 100%;
	object-fit: cover;
}

.kimia__good-pic.is-blank::after {
	content: '';
	position: absolute;
	inset: 50% auto auto 50%;
	width: 26px;
	height: 22px;
	transform: translate(-50%, -50%);
	border: 2px solid var(--line);
	border-radius: 4px;
	background:
		radial-gradient(circle at 30% 33%, var(--line) 2.5px, transparent 3px),
		linear-gradient(to top right, transparent 46%, var(--line) 46%, var(--line) 60%, transparent 60%);
}

@media (prefers-reduced-motion: reduce) {
	.kimia__good-pic img {
		transition: none;
	}
}

/* Specs: two rows of label/value, kept narrow so the card stays a tile. */
.kimia__good-specs {
	display: grid;
	grid-template-columns: auto 1fr;
	gap: 1px 8px;
	margin: 5px 0 0;
	padding: 0 10px;
	font-size: 11px;
	line-height: 1.7;
	color: var(--ink-soft);
}

.kimia__good-specs dt {
	white-space: nowrap;
}

.kimia__good-specs dd {
	margin: 0;
	color: var(--ink);
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* The old price sits before the new one and must not shout. */
.kimia__good span del {
	display: block;
	color: var(--ink-soft);
	font-size: 11px;
	font-weight: 500;
}

.kimia__good span i {
	font-style: normal;
}

/*
 * Two lines. The cards are grid items and stretch to the tallest, so one long
 * tool name would otherwise add a row's worth of height to all of them.
 *
 * Two mechanisms on purpose. -webkit-line-clamp does the nice version, cutting
 * at the line and adding an ellipsis. max-height is the floor under it: the
 * clamp is a non-standard property whose interaction with blockified flex
 * items is engine-dependent, and this ships into iOS and Android webviews as
 * well as desktop browsers. Where the clamp is honoured the max-height never
 * binds; where it is not, the row still cannot stretch.
 */
.kimia__good b {
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
	overflow: hidden;
	/* padding-top + two lines at this font-size and line-height. */
	max-height: calc(9px + 2 * 1.6 * 13px);
	padding: 9px 10px 0;
	font-size: 13px;
	font-weight: 600;
	line-height: 1.6;
}

.kimia__good span {
	margin-top: auto;
	padding: 8px 10px 10px;
	color: var(--brand);
	font-size: 13px;
	font-weight: 700;
}

.kimia__good.is-out span {
	color: var(--ink-soft);
	font-weight: 600;
}

/* --------------------------------------------------------------- notice */

.kimia__notice {
	margin: 0 14px 10px;
	padding: 11px 13px;
	border-radius: var(--radius);
	background: var(--warn-soft);
	color: var(--warn);
	border: 1px solid #f0dcc2;
	font-size: 13px;
	line-height: 1.8;
}

.kimia__notice a {
	color: inherit;
	font-weight: 700;
}

/* -------------------------------------------------------------- composer */

.kimia__compose {
	display: flex;
	align-items: flex-end;
	gap: 8px;
	padding: 10px 14px calc(10px + var(--safe-bottom));
	background: var(--card);
	border-top: 1px solid var(--line);
}

.kimia__compose textarea {
	flex: 1 1 auto;
	min-width: 0;
	max-height: 132px;
	padding: 10px 14px;
	border: 1px solid var(--line);
	border-radius: 20px;
	background: var(--page);
	color: inherit;
	font: inherit;
	line-height: 1.7;
	resize: none;
	/* Under 16px, iOS Safari zooms the whole page on focus and never zooms
	   back. The layout survives it; the customer's patience does not. */
	font-size: 16px;
}

.kimia__compose textarea:focus {
	outline: 2px solid var(--brand);
	outline-offset: -2px;
	background: var(--card);
}

/* 44px is the smallest reliable touch target on both platforms; anything less
   and the send button starts eating taps on a phone. */
.kimia__compose button {
	display: grid;
	place-items: center;
	width: 44px;
	height: 44px;
	flex: 0 0 44px;
	border: 0;
	border-radius: 50%;
	background: var(--brand);
	color: #fff;
	cursor: pointer;
}

.kimia__compose button svg {
	width: 20px;
	height: 20px;
	fill: currentColor;
	/* The paper plane points left in an RTL document otherwise. */
	transform: scaleX(-1);
}

.kimia__compose button:disabled {
	opacity: 0.45;
	cursor: not-allowed;
}

.kimia__compose textarea:disabled {
	opacity: 0.6;
	cursor: not-allowed;
}

/* ================================================================= tablet */

@media (min-width: 768px) {
	.kimia__bar {
		padding: 12px 22px;
	}

	/*
	 * A centred column. Full-width lines on a 10-inch screen are tiring to
	 * read, and the chat is mostly prose.
	 */
	.kimia__log,
	.kimia__notice,
	.kimia__compose {
		width: min(680px, 100%);
		margin-inline: auto;
	}

	.kimia__log {
		padding: 22px 20px 10px;
		gap: 16px;
	}

	.kimia__msg {
		max-width: 78%;
	}

	/* Room for two cards side by side, which reads better than a strip once
	   there is width for it. */
	.kimia__goods {
		grid-auto-flow: row;
		grid-template-columns: 1fr 1fr;
		overflow-x: visible;
		margin: 0;
		padding-inline: 0;
	}

	.kimia__compose {
		padding: 12px 20px calc(14px + var(--safe-bottom));
		background: transparent;
		border-top: 0;
	}

	.kimia__compose textarea {
		background: var(--card);
		border-radius: 22px;
	}

	.kimia__notice {
		margin-inline: auto;
	}
}

/* ================================================================ desktop */

@media (min-width: 1024px) {
	/*
	 * The rail arrives. In an RTL grid the first named column is on the right,
	 * so the conversation keeps the reading side and the rail sits left.
	 */
	.kimia__frame {
		grid-template-areas:
			"bar  bar"
			"talk rail";
		grid-template-columns: minmax(0, 1fr) 280px;
	}

	.kimia__rail {
		display: flex;
		flex-direction: column;
		gap: 16px;
		padding: 22px 20px;
		background: var(--card);
		border-inline-start: 1px solid var(--line);
		overflow-y: auto;
	}

	.kimia__rail small {
		display: block;
		color: var(--ink-soft);
		font-size: 12px;
		margin-bottom: 8px;
	}

	.kimia__level strong {
		display: block;
		font-size: 19px;
		color: var(--brand);
		margin-bottom: 10px;
	}

	.kimia__meter {
		height: 6px;
		border-radius: 999px;
		background: var(--page);
		overflow: hidden;
	}

	.kimia__meter span {
		display: block;
		height: 100%;
		width: 0;
		border-radius: 999px;
		background: var(--brand);
		transition: width 0.3s ease;
	}

	.kimia__meter span.is-low {
		background: #d08a3c;
	}

	.kimia__meter span.is-out {
		background: #c0433d;
	}

	.kimia__level p {
		margin: 8px 0 0;
		color: var(--ink-soft);
		font-size: 12px;
		line-height: 1.8;
	}

	.kimia__openers {
		display: flex;
		flex-direction: column;
		gap: 8px;
	}

	.kimia__openers button {
		padding: 10px 12px;
		border: 1px solid var(--line);
		border-radius: 10px;
		background: var(--page);
		color: var(--ink);
		font: inherit;
		font-size: 13px;
		text-align: start;
		line-height: 1.7;
		cursor: pointer;
	}

	.kimia__openers button:hover {
		border-color: var(--brand);
		background: var(--brand-soft);
		color: var(--brand);
	}

	/* The quota chip is redundant next to the rail's meter. */
	.kimia__quota {
		display: none;
	}

	.kimia__log {
		width: min(760px, 100%);
		padding: 26px 24px 12px;
	}

	.kimia__msg {
		max-width: 72%;
	}

	.kimia__goods {
		grid-template-columns: repeat(3, 1fr);
	}

	.kimia__compose {
		width: min(760px, 100%);
		padding: 12px 24px 20px;
	}
}

/* ============================================================ dark scheme */

/*
 * The app's webview follows the phone's setting, and a white sheet at night is
 * what makes people close it.
 */
@media (prefers-color-scheme: dark) {
	:root {
		--ink: #ede9f2;
		--ink-soft: #a49bb3;
		--line: #322a40;
		--page: #17131f;
		--card: #201a2b;
		--brand: #b98bf0;
		--brand-soft: #2c2140;
		--warn: #e8b273;
		--warn-soft: #33260f;
	}

	.kimia__msg--me {
		color: #17131f;
	}

	.kimia__compose button {
		color: #17131f;
	}

	.kimia__msg--err {
		background: #3a1c1c;
		color: #f0b4b1;
		border-color: #5a2a28;
	}

	.kimia__quota.is-out {
		background: #3a1c1c;
		color: #f0b4b1;
	}
}
