/**
 * FIA Prep Portal — Mobile Enhancements & Additive UI Layer
 * Loads ON TOP of the original style.css. Does not rewrite existing rules.
 * Adds: login gate, toast notifications, loading states, mobile-first responsive tweaks.
 *
 * @package FIA_Prep_Portal
 */

/* ===========================================================
 *  1. CSS Variables (additive — falls back to existing palette)
 * =========================================================== */
:root {
	--fia-radius-lg: 14px;
	--fia-radius-md: 10px;
	--fia-shadow-soft: 0 6px 24px rgba(15, 23, 42, 0.08);
	--fia-shadow-pop:  0 14px 42px rgba(15, 23, 42, 0.18);
	--fia-gate-bg: linear-gradient(135deg, #f8fafc 0%, #eef2ff 100%);
	--fia-accent: #2563eb;
	--fia-accent-hover: #1d4ed8;
	--fia-success: #16a34a;
	--fia-danger: #dc2626;
	--fia-info: #0ea5e9;
	--fia-text-soft: #475569;
	--fia-border-soft: #e2e8f0;
}

/* ===========================================================
 *  2. Login Gate
 * =========================================================== */
.fia-portal--gated {
	min-height: 70vh;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 32px 16px;
	background: var(--fia-gate-bg);
	border-radius: var(--fia-radius-lg);
}

.fia-gate {
	width: 100%;
	max-width: 480px;
	display: flex;
	justify-content: center;
}

.fia-gate-card {
	background: #ffffff;
	border-radius: var(--fia-radius-lg);
	box-shadow: var(--fia-shadow-pop);
	padding: 40px 28px 32px;
	text-align: center;
	width: 100%;
	border: 1px solid var(--fia-border-soft);
}

.fia-gate-icon {
	width: 64px;
	height: 64px;
	margin: 0 auto 18px;
	border-radius: 50%;
	background: #eff6ff;
	display: flex;
	align-items: center;
	justify-content: center;
	color: var(--fia-accent);
}

.fia-gate-icon svg {
	width: 32px;
	height: 32px;
}

.fia-gate-title {
	font-size: 22px;
	font-weight: 700;
	color: #0f172a;
	margin: 0 0 8px;
}

.fia-gate-msg {
	font-size: 14.5px;
	line-height: 1.6;
	color: var(--fia-text-soft);
	margin: 0 0 22px;
}

.fia-gate-actions {
	display: flex;
	flex-wrap: wrap;
	gap: 10px;
	justify-content: center;
	margin-bottom: 18px;
}

.fia-gate-foot {
	font-size: 12.5px;
	color: #94a3b8;
	margin-top: 12px;
}

/* ===========================================================
 *  3. Buttons used by gate + toast actions
 * =========================================================== */
.fia-btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: 6px;
	min-height: 44px;
	padding: 10px 22px;
	border-radius: 10px;
	font-size: 14.5px;
	font-weight: 600;
	text-decoration: none;
	border: 1px solid transparent;
	cursor: pointer;
	transition: all 0.18s ease;
	line-height: 1.2;
	white-space: nowrap;
}

.fia-btn-primary {
	background: var(--fia-accent);
	color: #fff;
}

.fia-btn-primary:hover,
.fia-btn-primary:focus {
	background: var(--fia-accent-hover);
	color: #fff;
	transform: translateY(-1px);
	box-shadow: 0 6px 14px rgba(37, 99, 235, 0.28);
}

.fia-btn-ghost {
	background: #fff;
	color: var(--fia-accent);
	border-color: #cbd5e1;
}

.fia-btn-ghost:hover,
.fia-btn-ghost:focus {
	background: #f1f5f9;
	color: var(--fia-accent-hover);
	border-color: #94a3b8;
}

/* ===========================================================
 *  4. Toast Notifications
 * =========================================================== */
.fia-toast-host {
	position: fixed;
	right: 16px;
	bottom: 16px;
	z-index: 99999;
	display: flex;
	flex-direction: column;
	gap: 10px;
	pointer-events: none;
	max-width: calc(100vw - 32px);
}

.fia-toast {
	pointer-events: auto;
	min-width: 240px;
	max-width: 360px;
	padding: 12px 16px 12px 14px;
	border-radius: var(--fia-radius-md);
	background: #0f172a;
	color: #fff;
	font-size: 14px;
	line-height: 1.4;
	box-shadow: var(--fia-shadow-pop);
	transform: translateY(20px);
	opacity: 0;
	transition: transform 0.25s ease, opacity 0.25s ease;
	border-left: 4px solid #475569;
}

.fia-toast.is-in {
	transform: translateY(0);
	opacity: 1;
}

.fia-toast--success { border-left-color: var(--fia-success); }
.fia-toast--error   { border-left-color: var(--fia-danger);  }
.fia-toast--info    { border-left-color: var(--fia-info);    }

@media (max-width: 600px) {
	.fia-toast-host {
		left: 12px;
		right: 12px;
		bottom: 12px;
		align-items: stretch;
	}
	.fia-toast {
		max-width: none;
		min-width: 0;
		width: 100%;
	}
}

/* ===========================================================
 *  5. Loading state + spinner
 * =========================================================== */
.is-loading {
	position: relative;
	opacity: 0.55;
	pointer-events: none;
	user-select: none;
}

.is-loading::after {
	content: "";
	position: absolute;
	top: 50%;
	left: 50%;
	width: 28px;
	height: 28px;
	margin: -14px 0 0 -14px;
	border: 3px solid rgba(37, 99, 235, 0.22);
	border-top-color: var(--fia-accent);
	border-radius: 50%;
	animation: fia-spin 0.8s linear infinite;
}

@keyframes fia-spin {
	to { transform: rotate(360deg); }
}

/* ===========================================================
 *  6. Mobile Sidebar Enhancements (works with existing markup)
 *
 *  Existing markup has: .fia-burger toggle, #fia-sidebar, #fia-portal.
 *  Our JS adds .is-open on #fia-sidebar and .fia-sidebar-open on #fia-portal
 *  when the burger is tapped on mobile.
 * =========================================================== */
@media (max-width: 900px) {
	#fia-sidebar {
		position: fixed;
		top: 0;
		left: 0;
		bottom: 0;
		width: 82%;
		max-width: 320px;
		z-index: 9000;
		transform: translateX(-105%);
		transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
		box-shadow: 6px 0 28px rgba(15, 23, 42, 0.16);
		overflow-y: auto;
		-webkit-overflow-scrolling: touch;
	}

	#fia-sidebar.is-open {
		transform: translateX(0);
	}

	#fia-portal.fia-sidebar-open::before {
		content: "";
		position: fixed;
		inset: 0;
		background: rgba(15, 23, 42, 0.45);
		z-index: 8500;
		animation: fia-fade-in 0.2s ease;
	}

	.fia-burger {
		min-width: 44px;
		min-height: 44px;
		display: inline-flex;
		align-items: center;
		justify-content: center;
	}
}

@keyframes fia-fade-in {
	from { opacity: 0; }
	to   { opacity: 1; }
}

/* ===========================================================
 *  7. Responsive Grids — soften existing layouts on small screens
 * =========================================================== */
@media (max-width: 900px) {
	.fia-tests-grid,
	.fia-posts-grid,
	.fia-stat-grid,
	.fia-plan-grid,
	.fia-results-grid {
		grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
		gap: 12px !important;
	}

	.fia-panel,
	.fia-card,
	.fia-section {
		padding: 16px !important;
	}

	.fia-h1, .fia-page-title { font-size: 22px !important; }
	.fia-h2                  { font-size: 18px !important; }
}

@media (max-width: 600px) {
	.fia-tests-grid,
	.fia-posts-grid,
	.fia-stat-grid,
	.fia-plan-grid,
	.fia-results-grid {
		grid-template-columns: 1fr !important;
		gap: 10px !important;
	}

	.fia-panel,
	.fia-card,
	.fia-section {
		padding: 14px !important;
		border-radius: 12px !important;
	}

	.fia-h1, .fia-page-title { font-size: 20px !important; }
	.fia-h2                  { font-size: 16.5px !important; }
	.fia-h3                  { font-size: 14.5px !important; }

	body { font-size: 14.5px; }

	.fia-row,
	.fia-toolbar {
		flex-wrap: wrap !important;
		gap: 8px !important;
	}

	.fia-btn,
	button,
	.fia-tab,
	.fia-nav-item {
		min-height: 44px;
	}

	input[type="text"],
	input[type="email"],
	input[type="password"],
	input[type="number"],
	input[type="search"],
	select,
	textarea {
		font-size: 16px !important; /* prevents iOS zoom on focus */
		min-height: 44px;
		width: 100%;
		box-sizing: border-box;
	}
}

/* ===========================================================
 *  8. Tables → horizontal scroll on small screens
 * =========================================================== */
.fia-table-wrap,
.fia-results-table-wrap,
.fia-mistakes-wrap {
	overflow-x: auto;
	-webkit-overflow-scrolling: touch;
	border-radius: var(--fia-radius-md);
}

@media (max-width: 700px) {
	.fia-table,
	.fia-results-table,
	.fia-mistakes-table {
		min-width: 560px;
	}
}

/* ===========================================================
 *  9. Test runner mobile polish
 * =========================================================== */
@media (max-width: 600px) {
	.fia-test-question,
	.fia-test-option {
		font-size: 15.5px;
		line-height: 1.55;
	}

	.fia-test-option {
		padding: 14px 14px;
		min-height: 52px;
		border-radius: 12px;
	}

	.fia-test-header,
	.fia-test-footer {
		flex-wrap: wrap;
		gap: 8px;
	}

	.fia-test-timer {
		font-size: 18px;
		font-weight: 700;
	}

	.fia-test-progress {
		width: 100%;
		order: 99;
	}
}

/* ===========================================================
 * 10. Accessibility — respect reduced motion
 * =========================================================== */
@media (prefers-reduced-motion: reduce) {
	*, *::before, *::after {
		animation-duration: 0.001ms !important;
		animation-iteration-count: 1 !important;
		transition-duration: 0.001ms !important;
		scroll-behavior: auto !important;
	}
}

/* ===========================================================
 * 11. Focus visible — better keyboard accessibility
 * =========================================================== */
.fia-btn:focus-visible,
button:focus-visible,
a:focus-visible,
[data-fia-action]:focus-visible {
	outline: 3px solid rgba(37, 99, 235, 0.45);
	outline-offset: 2px;
}

/* ===========================================================
 * 12. Body lock when sidebar open on mobile (no horizontal scroll)
 * =========================================================== */
@media (max-width: 900px) {
	body.fia-no-scroll {
		overflow: hidden;
	}
}

/* ===========================================================
 * 13. MCQ option text — prevent overflow on narrow screens
 *
 *     Symptom (pre-fix): long option text like
 *     "The importance and scarcity of fresh water" clipped past
 *     the right edge of the screen on mobile.
 *
 *     Cause: .fia-option is a flex row (letter circle + text span).
 *     The text span had no min-width:0 and no overflow-wrap, so a
 *     long word could push the flex box wider than its parent.
 *
 *     Fix: let the text span shrink (min-width:0) and break long
 *     words / phrases that don't fit on a line; also clamp the
 *     option button itself so it can never exceed its container.
 * =========================================================== */
.fia-portal .fia-option {
	width: 100%;
	max-width: 100%;
	box-sizing: border-box;
	min-width: 0;
}

.fia-portal .fia-option-text {
	flex: 1 1 auto;
	min-width: 0;
	white-space: normal;
	word-break: break-word;
	overflow-wrap: anywhere;
	hyphens: auto;
	line-height: 1.5;
}

.fia-portal .fia-option-letter {
	flex: 0 0 28px;
}

/* The list and the question card must also stay inside their parent
 * so an oversize option can't blow out the layout. */
.fia-portal .fia-option-list,
.fia-portal .fia-question-card {
	max-width: 100%;
	box-sizing: border-box;
}

/* Belt-and-braces: on the smallest screens, tighten padding and
 * shrink the inline font a touch so a long line wraps to at most
 * 2–3 lines instead of forcing a horizontal scroll. */
@media (max-width: 420px) {
	.fia-portal .fia-option {
		padding: 12px 12px;
		gap: 10px;
		font-size: 14.5px;
	}
	.fia-portal .fia-option-letter {
		width: 26px;
		height: 26px;
		font-size: 12px;
	}
	.fia-portal .fia-question-card {
		padding: 12px;
	}
}

/* Stop the whole portal shell from ever introducing a horizontal
 * scroll on mobile — a single misbehaving child shouldn't break
 * the page. */
@media (max-width: 900px) {
	.fia-portal,
	.fia-portal .fia-shell,
	.fia-portal .fia-main {
		max-width: 100%;
		overflow-x: hidden;
	}
}

/* ===== Recent Activity — keep long subject·topic labels inside the card ===
 * On mobile, lines like "General Knowledge · Everyday Science" were being
 * forced onto a single ellipsis-truncated line that, combined with the
 * trailing score badge, could overflow the card on narrow phones.
 * Wrap to a max of 2 lines instead. Desktop styling (single line + ellipsis)
 * is untouched. */
@media (max-width: 600px) {
	.fia-portal .fia-recent-item {
		min-width: 0;
		gap: 8px;
		padding: 10px 12px;
	}
	.fia-portal .fia-recent-item-main {
		min-width: 0;
		flex: 1 1 auto;
	}
	.fia-portal .fia-recent-item-title {
		/* Allow up to 2 lines instead of nowrap+ellipsis */
		white-space: normal;
		overflow: hidden;
		text-overflow: clip;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		line-clamp: 2;
		-webkit-box-orient: vertical;
		word-break: break-word;
		overflow-wrap: anywhere;
		line-height: 1.3;
		font-size: 13.5px;
	}
	.fia-portal .fia-recent-item-sub {
		font-size: 11.5px;
	}
	.fia-portal .fia-recent-score {
		flex: 0 0 auto;
		font-size: 13.5px;
	}
}
