/* ==========================================================================
   Smart Library DSS — global responsive layer (loads last on every page)
   Purpose: make every shell / grid / table / form adapt fluidly from
   small phones (320px) through tablets to wide desktops.
   Breakpoints: 1100px (small laptop), 820px (tablet portrait / drawer),
   640px (large phone), 480px (compact phone).
   ========================================================================== */

/* ---------- 1. Fluid foundations (all viewports) ---------- */
html,
body {
	max-width: 100%;
	overflow-x: visible;
}

body {
	-webkit-text-size-adjust: 100%;
	text-size-adjust: 100%;
}

img,
video,
svg,
canvas {
	max-width: 100%;
	height: auto;
}

input,
select,
textarea,
button {
	max-width: 100%;
}

table {
	max-width: 100%;
}

/* Long titles, ISBNs, emails and descriptions must wrap instead of
   forcing horizontal scroll. */
h1, h2, h3, h4, p, li, dd, dt, td, th, small,
.book-card h3, .book-card p, .book-card small,
.recommendation-item h3, .recommendation-item p,
.history-item h2, .history-item p,
.detail-copy h2, .detail-copy p,
.metric-card strong, .metric-card span,
.panel h2, .activity-item strong, .activity-item p,
.notification-item strong, .notification-item p {
	overflow-wrap: anywhere;
	word-break: break-word;
}

.dashboard-main,
.library-main,
.admin-main,
.admin-wrap,
.form-wrap,
.library-panel,
.panel,
.section,
.auth-card,
.metric-card,
.book-card,
.recommendation-item,
.history-item,
.detail-panel,
.comparison-grid > div {
	min-width: 0;
}

/* Scrollable tables never break the viewport. */
.table-wrap {
	max-width: 100%;
	overflow-x: auto;
	overflow-y: hidden;
	-webkit-overflow-scrolling: touch;
}

.side-nav .side-nav-group {
	display: grid;
	gap: 2px;
}

.side-nav .side-nav-group > summary {
	display: flex;
	align-items: center;
	gap: 14px;
	padding: 10px 12px;
	border-radius: 8px;
	color: #a8c0dc;
	font-size: .82rem;
	font-weight: 700;
	cursor: pointer;
	list-style: none;
}

.side-nav .side-nav-group > summary::-webkit-details-marker {
	display: none;
}

.side-nav .side-nav-group > summary::after {
	content: "+";
	margin-left: auto;
	color: #12b9d6;
	font-size: 1rem;
}

.side-nav .side-nav-group[open] > summary::after {
	content: "\2212";
}

.side-nav .side-nav-group > summary:hover,
.side-nav .side-nav-group[open] > summary {
	color: #fff;
	background: rgba(255, 255, 255, .1);
}

.side-nav .side-nav-group > summary span {
	width: 18px;
	color: #12b9d6;
	font-size: 1.1rem;
	text-align: center;
}

.side-nav .side-nav-group-links {
	display: grid;
	gap: 3px;
	padding-left: 12px;
}

.side-nav .side-nav-group-links a {
	padding-top: 8px;
	padding-bottom: 8px;
	font-size: .76rem;
}

.side-nav > a {
	padding: 10px 12px;
	border-radius: 8px;
}

.side-nav a:focus-visible,
.side-nav .side-nav-group > summary:focus-visible {
	outline: 2px solid #12b9d6;
	outline-offset: 2px;
}

/* Keep independent desktop scroll panes intact (dashboard / library
   shells scroll inside their own pane, not the window). */
@media (min-width: 821px) {
	html:has(.dashboard-shell, .library-shell),
	body:has(.dashboard-shell, .library-shell) {
		height: 100%;
		overflow: visible;
	}
}

/* ---------- 1b. Maximize available screen width (all viewports) ----------
   The app workspace fills everything next to the sidebar — no narrow
   centered column, no wasted gutter on wide monitors. Padding stays
   fluid via clamp() so it breathes on every screen size. */
.dashboard-main,
.library-main,
.dashboard-main.admin-main,
.admin-main {
	width: 100%;
	max-width: none;
	margin: 0;
}

.dashboard-main,
.library-main {
	padding: clamp(24px, 3.5vw, 42px) clamp(16px, 4vw, 48px);
}

.dashboard-main.admin-main {
	padding: clamp(20px, 3vw, 36px) clamp(12px, 3vw, 40px);
}

.dashboard-main.admin-main .admin-wrap,
.admin-wrap {
	width: 100%;
	max-width: none;
	margin: 0;
}

.dashboard-shell,
.library-shell,
.admin-shell {
	grid-template-columns: 232px minmax(0, 1fr);
}

.dashboard-shell .sidebar,
.library-shell .sidebar,
.admin-shell .sidebar {
	padding: 26px 18px;
}

.dashboard-shell .side-nav,
.library-shell .side-nav,
.admin-shell .side-nav {
	gap: 4px;
	margin-top: clamp(30px, 5vh, 52px);
}

.metric-card {
	min-height: 124px;
	padding: 20px;
	box-shadow: 0 8px 24px rgba(25, 65, 117, .06);
}

.metric-card strong {
	margin-top: 14px;
}

/* ---------- 2. Small laptops / landscape tablets (<= 1100px) ---------- */
@media (max-width: 1100px) {
	.library-grid {
		grid-template-columns: 1fr;
	}

	.metric-grid {
		grid-template-columns: repeat(2, minmax(0, 1fr));
	}

	.workspace-grid,
	.summary-grid {
		grid-template-columns: 1fr;
	}

	.search-form {
		grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr));
	}

	.search-form .button,
	.search-form .filter-actions {
		grid-column: 1 / -1;
	}

	.comparison-grid {
		grid-template-columns: repeat(2, minmax(0, 1fr));
	}

	.detail-panel {
		grid-template-columns: 1fr;
	}
}

/* ---------- 3. Unified sidebar drawer (<= 820px) ----------
   Every shell reuses the same off-canvas navy sidebar. The toggle and
   backdrop buttons are injected by assets/js/sidebar-layout.js. */
.sidebar-toggle,
.sidebar-backdrop {
	display: none;
}

@media (max-width: 820px) {
	body.sidebar-open {
		overflow: hidden;
	}

	.dashboard-shell,
	.library-shell,
	.admin-shell {
		display: block;
	}

	.dashboard-shell .sidebar,
	.library-shell .sidebar,
	.admin-shell .sidebar,
	.admin-page .admin-sidebar {
		position: fixed;
		inset: 0 auto 0 0;
		z-index: 20;
		width: min(290px, 88vw);
		height: 100vh;
		height: 100dvh;
		max-height: 100vh;
		max-height: 100dvh;
		min-height: 0;
		padding: 22px 18px;
		overflow-y: auto;
		overscroll-behavior: contain;
		transform: translateX(-105%);
		transition: transform 0.2s ease;
		box-shadow: 18px 0 40px rgba(7, 25, 60, 0.22);
	}

	body.sidebar-open .dashboard-shell .sidebar,
	body.sidebar-open .library-shell .sidebar,
	body.sidebar-open .admin-shell .sidebar,
	body.sidebar-open .admin-page .admin-sidebar {
		transform: translateX(0);
	}

	.dashboard-shell .side-nav,
	.library-shell .side-nav,
	.admin-shell .side-nav,
	.admin-page .admin-sidebar .side-nav {
		display: grid;
		margin-top: 32px;
		overflow: visible;
	}

	.dashboard-shell .side-nav a,
	.library-shell .side-nav a,
	.admin-shell .side-nav a,
	.admin-page .admin-sidebar .side-nav a {
		min-width: 0;
		white-space: normal;
	}

	.dashboard-shell .sidebar-footer,
	.library-shell .sidebar-footer {
		display: flex;
	}

	.sidebar-toggle {
		position: fixed;
		top: calc(14px + env(safe-area-inset-top, 0px));
		right: calc(14px + env(safe-area-inset-right, 0px));
		z-index: 30;
		display: grid;
		place-content: center;
		gap: 4px;
		width: 44px;
		height: 44px;
		padding: 10px;
		border: 1px solid rgba(16, 42, 99, 0.16);
		border-radius: 4px;
		color: var(--navy, #102a63);
		background: var(--white, #fff);
		box-shadow: 0 8px 20px rgba(16, 42, 99, 0.14);
		cursor: pointer;
	}

	.sidebar-toggle span {
		display: block;
		width: 22px;
		height: 2px;
		background: currentColor;
	}

	.sidebar-backdrop {
		position: fixed;
		inset: 0;
		z-index: 19;
		border: 0;
		background: rgba(8, 24, 55, 0.46);
		cursor: pointer;
	}

	body.sidebar-open .sidebar-backdrop {
		display: block;
	}

	/* Main content becomes a normal scrolling page under the drawer. */
	.dashboard-main,
	.library-main,
	.admin-main,
	.dashboard-main.admin-main {
		width: 100%;
		height: auto;
		max-height: none;
		padding: 32px 20px;
		overflow: visible;
	}

	/* Prevent iOS auto-zoom on form fields. */
	input,
	select,
	textarea {
		font-size: 16px;
	}

	.button,
	.small-button,
	.compare-button {
		min-height: 44px;
	}
}

/* ---------- 4. Large phones / small tablets (<= 640px) ---------- */
@media (max-width: 640px) {
	.dashboard-header,
	.library-header,
	.admin-head {
		flex-direction: column;
		align-items: flex-start;
		gap: 16px;
	}

	.dashboard-header h1,
	.library-header h1 {
		font-size: clamp(1.9rem, 8vw, 2.2rem);
	}

	.metric-grid,
	.form-grid,
	.form-grid.three {
		grid-template-columns: 1fr;
	}

	.form-grid .full,
	.field.full,
	.search-form .button,
	.search-form .filter-actions {
		grid-column: auto;
	}

	.search-form,
	.recommend-form,
	.year-grid {
		grid-template-columns: 1fr;
	}

	.workspace-grid,
	.summary-grid,
	.chart-grid {
		grid-template-columns: 1fr;
	}

	.comparison-grid {
		grid-template-columns: 1fr;
	}

	.book-card {
		grid-template-columns: 1fr;
		gap: 12px;
	}

	.book-meta {
		justify-items: start;
		text-align: left;
	}

	.card-actions {
		display: flex;
		flex-wrap: wrap;
		gap: 8px;
	}

	.card-actions > * {
		flex: 1 1 140px;
	}

	.recommendation-item {
		grid-template-columns: 30px minmax(0, 1fr);
		gap: 10px;
	}

	.recommendation-item > strong {
		grid-column: 2;
		font-size: 1.1rem;
	}

	.factor-list {
		display: flex;
		flex-wrap: wrap;
		gap: 6px;
		padding: 0;
		margin: 8px 0 0;
		list-style: none;
	}

	.factor-list li {
		padding: 4px 8px;
		border: 1px solid var(--line, #dce8f0);
		border-radius: 20px;
		font-size: 0.68rem;
	}

	/* Book detail (book.php) */
	.detail-panel {
		display: grid;
		grid-template-columns: 1fr;
		gap: 20px;
		padding: 20px 16px;
	}

	.detail-facts {
		display: grid;
		grid-template-columns: auto 1fr;
		gap: 8px 14px;
		margin: 0;
	}

	.detail-facts dt {
		color: var(--muted, #62728a);
	}

	.detail-facts dd {
		margin: 0;
		text-align: right;
	}

	.library-header .actions,
	.filter-actions,
	.form-actions,
	.history-actions {
		display: flex;
		flex-wrap: wrap;
		gap: 10px;
		width: 100%;
	}

	/* Recommendation history (history.php) */
	.history-item {
		display: grid;
		grid-template-columns: 1fr;
		gap: 14px;
		padding: 18px 0;
	}

	.history-item + .history-item {
		border-top: 1px solid var(--line, #dce8f0);
	}

	.history-actions {
		align-items: center;
		justify-content: flex-start;
	}

	.active-filters {
		display: flex;
		flex-wrap: wrap;
		gap: 8px;
		align-items: center;
	}

	.filter-chip {
		display: inline-block;
		padding: 4px 10px;
		border: 1px solid var(--line, #dce8f0);
		border-radius: 20px;
		background: var(--pale, #eef7fb);
		font-size: 0.7rem;
		font-weight: 700;
	}

	.panel,
	.library-panel,
	.section {
		padding: 20px 16px;
	}

	.panel-heading {
		flex-wrap: wrap;
		gap: 12px;
	}

	.activity-item {
		grid-template-columns: 28px minmax(0, 1fr);
	}

	.activity-item time {
		grid-column: 2;
		text-align: left;
	}

	.notification-item {
		grid-template-columns: 1fr;
		gap: 10px;
	}

	.notification-item .small-link {
		justify-self: start;
	}

	/* Admin: filters, tabs and nav scroll instead of overflowing. */
	.report-filters {
		flex-direction: column;
		align-items: stretch;
	}

	.report-filters .field,
	.report-filters .form-actions {
		width: 100%;
	}

	.admin-nav,
	.tabs {
		flex-wrap: nowrap;
		overflow-x: auto;
		scrollbar-width: thin;
		-webkit-overflow-scrolling: touch;
	}

	.admin-nav a,
	.tabs a {
		flex: 0 0 auto;
	}

	.bar-row {
		grid-template-columns: 1fr;
		gap: 6px;
	}

	.bar-row b {
		text-align: left;
	}

	.log {
		grid-template-columns: 1fr;
		gap: 2px;
	}
}

/* ---------- 5. Compact phones (<= 480px) ---------- */
@media (max-width: 480px) {
	.dashboard-main,
	.library-main,
	.admin-main,
	.dashboard-main.admin-main {
		padding: 24px 12px;
	}

	.dashboard-header {
		margin-bottom: 28px;
	}

	.metric-card {
		min-height: 0;
		padding: 17px;
	}

	.metric-card strong {
		font-size: clamp(1.6rem, 8vw, 1.9rem);
	}

	.header-actions,
	.dashboard-header .button,
	.library-header .button,
	.search-form .button,
	.recommend-form .button,
	.form-actions .button,
	.form-actions .small-button {
		width: 100%;
		justify-content: center;
		white-space: normal;
		text-align: center;
	}

	.quick-links {
		grid-template-columns: 1fr;
	}

	.table-wrap table {
		min-width: 0;
	}

	.table-wrap th,
	.table-wrap td {
		white-space: normal;
	}

	/* Landing page polish for very small screens. */
	.hero-actions {
		flex-direction: column;
		align-items: stretch;
	}

	.hero-actions .button {
		justify-content: center;
	}

	.value-grid {
		grid-template-columns: 1fr;
	}

	.footer-grid {
		grid-template-columns: 1fr;
	}

	.footer-bottom {
		flex-direction: column;
		gap: 6px;
	}
}

/* ---------- 6. Very small phones (<= 380px) ---------- */
@media (max-width: 380px) {
	.dashboard-main,
	.library-main {
		padding: 20px 10px;
	}

	.panel,
	.library-panel,
	.auth-card {
		padding: 18px 12px;
	}

	.detail-facts {
		grid-template-columns: 1fr;
		gap: 2px;
	}

	.detail-facts dd {
		text-align: left;
		margin-bottom: 8px;
	}

	.comparison-grid dl {
		grid-template-columns: 1fr;
	}

	.comparison-grid dd {
		text-align: left;
	}
}

/* ---------- 7. Auth + landing safety nets ---------- */
@media (max-width: 800px) {
	.auth-shell {
		display: block;
	}

	.auth-main {
		padding: 28px 16px;
	}

	.brand-panel {
		min-height: 280px;
	}
}

@media (max-width: 480px) {
	.auth-main {
		padding: 20px 12px;
	}

	.auth-card {
		padding: 24px 16px;
	}

	.auth-card .form-grid {
		grid-template-columns: 1fr;
	}
}

/* Landing page: keep the marketing grids fluid even where the base
   stylesheet stops (between 680px and 420px). */
@media (max-width: 680px) {
	.pwa-content {
		flex-direction: column;
		align-items: flex-start;
	}
}

/* ---------- 8. Explore-library components (all viewports) ----------
   These elements had no base layout, so desktop rendered them with
   browser defaults and wasted the maximized width. */
.library-header .actions,
.filter-actions,
.card-actions,
.history-actions {
	display: flex;
	flex-wrap: wrap;
	gap: 10px;
	align-items: center;
}

.factor-list {
	display: flex;
	flex-wrap: wrap;
	gap: 6px;
	padding: 0;
	margin: 8px 0 0;
	list-style: none;
}

.factor-list li {
	padding: 4px 8px;
	border: 1px solid var(--line, #dce8f0);
	border-radius: 20px;
	background: var(--white, #fff);
	font-size: 0.68rem;
}

.factor-list li.matched {
	border-color: #bfe3da;
	background: #e4f6f0;
	color: #0d5f4e;
}

.factor-list li.missed {
	border-color: #efc3c3;
	background: #fbeaea;
	color: #7d3030;
}

.status-tag {
	display: inline-block;
	padding: 4px 10px;
	border-radius: 20px;
	background: var(--pale, #eef7fb);
	color: var(--navy, #102a63);
	font-size: 0.68rem;
	font-weight: 800;
	white-space: nowrap;
}

.history-item {
	display: grid;
	grid-template-columns: 1fr auto;
	gap: 14px;
	align-items: center;
	padding: 18px 0;
}

.history-item + .history-item {
	border-top: 1px solid var(--line, #dce8f0);
}

.history-item h2 {
	margin: 0 0 4px;
	font-size: 1.05rem;
}

.history-item p {
	margin: 0;
	color: var(--muted, #62728a);
	font-size: 0.78rem;
}

.active-filters {
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
	align-items: center;
	margin-top: 14px;
}

.filter-chip {
	display: inline-block;
	padding: 4px 10px;
	border: 1px solid var(--line, #dce8f0);
	border-radius: 20px;
	background: var(--pale, #eef7fb);
	font-size: 0.7rem;
	font-weight: 700;
}

/* Book detail (book.php): two columns on desktop, stacked on phones. */
.detail-panel {
	display: grid;
	grid-template-columns: 1.2fr 0.8fr;
	gap: 28px;
	align-items: start;
}

.detail-copy h2 {
	margin: 6px 0 10px;
}

.detail-authors {
	color: var(--muted, #62728a);
	font-size: 0.82rem;
}

.detail-facts {
	display: grid;
	grid-template-columns: auto 1fr;
	gap: 8px 14px;
	margin: 0;
	padding: 18px;
	border: 1px solid var(--line, #dce8f0);
	background: var(--pale, #eef7fb);
}

.detail-facts dt {
	color: var(--muted, #62728a);
	font-size: 0.75rem;
}

.detail-facts dd {
	margin: 0;
	text-align: right;
	font-weight: 700;
	font-size: 0.8rem;
}

.detail-facts dd.available {
	color: #0d5f4e;
}

.detail-facts dd.unavailable {
	color: #7d3030;
}

/* ---------- 9. Admin tables become labelled cards on phones ----------
   Tables marked .stacked-table (with data-label cells) stack instead of
   forcing horizontal scroll. Unmarked tables keep their scroll container. */
@media (max-width: 700px) {
	.table-wrap.stacked-wrap {
		border: 0;
		background: transparent;
		overflow: visible;
		box-shadow: none;
	}

	.stacked-table,
	.stacked-table tbody {
		display: grid;
		gap: 12px;
	}

	.table-wrap.stacked-wrap table.stacked-table {
		min-width: 0;
		width: 100%;
		border-collapse: separate;
	}

	.stacked-table thead {
		display: none;
	}

	.stacked-table tr {
		display: grid;
		gap: 0;
		border: 1px solid var(--line, #dde6f0);
		border-radius: 12px;
		background: var(--white, #ffffff);
		overflow: hidden;
	}

	.stacked-table tbody tr:hover td {
		background: var(--white, #ffffff);
	}

	.stacked-wrap .stacked-table td {
		display: block;
		border-bottom: 1px solid var(--line, #dde6f0);
		padding: 12px 14px;
	}

	.stacked-wrap .stacked-table td:last-child {
		border-bottom: 0;
	}

	.stacked-table td[data-label]::before {
		content: attr(data-label);
		display: block;
		margin-bottom: 6px;
		color: var(--muted, #6c7a91);
		font-size: 0.62rem;
		font-weight: 800;
		letter-spacing: 0.08em;
		text-transform: uppercase;
	}

	/* Inline min-widths on row editors must not force overflow in cards. */
	.stacked-table td input,
	.stacked-table td select,
	.stacked-table td textarea {
		width: 100% !important;
		min-width: 0 !important;
		max-width: 100%;
	}

	.stacked-table td form.actions {
		display: flex;
		flex-wrap: wrap;
		gap: 8px;
	}

	.stacked-table .actions .small-button,
	.stacked-table .actions .button {
		flex: 1 1 auto;
		justify-content: center;
	}

	/* Hand-rolled pagination uses the shared page style. */
	.pagination a:not(.page),
	.pagination span.current:not(.page) {
		display: inline-block;
		min-width: 32px;
		padding: 7px 10px;
		border: 1px solid var(--line, #dde6f0);
		border-radius: 8px;
		background: var(--white, #ffffff);
		color: var(--navy, #102a63);
		font-size: 0.75rem;
		font-weight: 700;
		text-align: center;
		text-decoration: none;
	}

	.pagination span.current:not(.page) {
		background: var(--navy, #102a63);
		border-color: var(--navy, #102a63);
		color: var(--white, #ffffff);
	}
}

/* ---------- 10. Landing decorative orbits stay inside small screens ----- */
.visual-orbit {
	max-width: 100%;
}

@media (max-width: 420px) {
	.orbit-one {
		width: min(300px, 80vw);
		height: min(300px, 80vw);
	}

	.orbit-two {
		width: min(230px, 62vw);
		height: min(230px, 62vw);
	}

	.hero-visual {
		min-height: 340px;
	}

	.floating-label {
		max-width: 70vw;
	}
}

@media (prefers-reduced-motion: reduce) {
	*,
	*::before,
	*::after {
		transition-duration: 0.01ms !important;
		animation-duration: 0.01ms !important;
	}
}

/* Reusable sizing for form controls across all responsive app pages. */
input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="submit"]):not([type="button"]):not([type="reset"]):not([type="image"]),
select,
textarea {
	box-sizing: border-box;
	width: 100%;
	min-width: 0;
	max-width: 100%;
	padding-inline: 12px;
}

select {
	text-overflow: ellipsis;
	white-space: nowrap;
}

input[type="date"] {
	box-sizing: border-box;
	width: 100%;
	min-width: 0;
}

/* Let page-level content use the full area instead of stopping at a fixed cap. */
.container,
.auth-shell,
.dashboard-shell,
.library-shell,
.admin-shell {
	width: 100%;
	max-width: none;
}

.container {
	margin-inline: 0;
	padding-inline: clamp(16px, 4vw, 64px);
}

.auth-shell {
	margin: 0;
}

.auth-card {
	width: 100%;
	max-width: none;
}

.admin-wrap,
.form-wrap {
	width: 100%;
	max-width: none;
}
