/*
 * Breakpoints, gutters and mobile rules (docs/ui-style-guide.md §5).
 * Tokens: desktop-sm 1100, mobile 768, phone 480, tiny 380.
 * Tap target stays >= 40px (WCAG 2.2 AA).
 */

@media (max-width: 1100px) {
	:root {
		--sidebar-width: 240px;
		--page-padding: 1.5rem;
	}
}

/* ===========================================================================
   TOUCH / TABLET AND BELOW (≤992px): navigation becomes a hamburger + drawer.
   The floating rail is icon-only and only reveals its labels on HOVER, which a
   touch screen does not have — so on these widths it slides off-canvas and the
   top bar owns navigation. 992px (not 768) because a portrait tablet is touch too.
   =========================================================================== */
@media (max-width: 992px) {
	.mobilebar {
		display: flex;
	}
	.console-shell {
		flex-direction: column;
	}
	/* The rail becomes an off-canvas drawer: full height, labels visible, wide enough
	   to read, and it slides in over the page instead of squeezing it. */
	.aside {
		position: fixed;
		left: 0;
		top: 0;
		height: 100vh;
		/* See the modal: dvh matches what the phone actually shows, so the logout row at the
		   bottom of the drawer does not end up under the browser's address bar. */
		height: 100dvh;
		width: min(300px, 86vw);
		max-width: none;
		z-index: 1060;
		border-radius: 0;
		border: 0;
		border-right: 1px solid rgba(255, 255, 255, 0.08);
		flex-direction: column;
		gap: 0;
		padding: 1rem 0.7rem;
		overflow-y: auto;
		transform: translateX(-100%);
		/* Override the desktop width transition — here it is the slide that animates. */
		transition: transform 0.22s cubic-bezier(0.22, 0.61, 0.21, 1);
		box-shadow: none;
		/* The rail's bottom fade belongs to the collapsed icon strip, not to this list. */
		mask-image: none;
		-webkit-mask-image: none;
	}
	.aside--open {
		transform: translateX(0);
		box-shadow: 8px 0 40px rgba(0, 0, 0, 0.35);
	}
	/* Hover must not widen or reveal anything here — the drawer is already open or shut. */
	.aside:hover,
	.aside:focus-within {
		width: min(300px, 86vw);
	}
	/* Labels are always visible inside the drawer. */
	.aside__brand span,
	.aside__workshop,
	.aside__link span,
	.aside__user {
		opacity: 1;
	}
	.aside__nav {
		flex: 1 1 auto;
		flex-direction: column;
		flex-wrap: nowrap;
		gap: 2px;
		overflow: visible;
	}
	/* Comfortable tap targets: 44px tall rows, not 0.45rem chips. */
	.aside__link {
		flex: 0 0 auto;
		min-height: 44px;
		padding: 0.6rem 0.75rem;
		font-size: 0.95rem;
	}
	.aside__user {
		display: block; /* in a drawer it fits, and logout has to be reachable */
	}
	.aside__backdrop {
		display: block;
	}
	.console-content {
		margin-left: 0;
		/* Room for the help FAB (fixed, bottom-right) so it never sits on the last row. */
		padding-bottom: 5rem;
	}
	/* Two sticky bars stacked would eat a third of the screen, and the top bar already
	   names the section while you scroll — so the page header scrolls away here. */
	.std-page-header {
		position: static;
		backdrop-filter: none;
	}
	/* The dock lives in the top bar here. Hidden everywhere else — not just in
	   `.std-page-header`, because a couple of pages (the OR detail) build their own header
	   and render DockButtons themselves, which showed the dock buttons twice. */
	.quick-dock {
		display: none;
	}
	.mobilebar__dock .quick-dock {
		display: flex;
	}
}

/* Teléfono: barra de pestañas abajo. Su «Menú» abre el cajón, así que la hamburguesa de arriba
   sobra y deja sitio al título y a los botones del dock. */
@media (max-width: 768px) {
	body:has(.tabbar) {
		--tabbar-h: calc(56px + env(safe-area-inset-bottom, 0px));
	}
	.tabbar {
		display: flex;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 1050;
		padding: 0 0.25rem env(safe-area-inset-bottom, 0px);
		background: var(--aside-bg, #0a0a0c);
		box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.18);
	}
	.mobilebar > .mobilebar__burger:first-child {
		display: none;
	}
	.console-content {
		padding-bottom: calc(5rem + var(--tabbar-h, 0px));
	}
	.snackbar-host {
		left: 0.75rem;
		right: 0.75rem;
		max-width: none;
		align-items: stretch;
	}
}

@media (max-width: 768px) {
	:root {
		--page-padding: 1rem;
	}
	/* Title over actions, and the actions get the full width to lay out in. */
	.std-page-header {
		flex-direction: column;
		align-items: flex-start;
	}
	.std-page-header__title {
		font-size: 1.4rem;
	}
	.std-page-header__actions {
		width: 100%;
		flex-wrap: wrap;
		gap: 0.5rem;
		justify-content: flex-start;
		margin-left: 0;
	}
	/* A lone page action becomes a full-width primary button — the usual phone shape. */
	.std-page-header__actions > .btn:only-child {
		width: 100%;
	}
	.std-page-header__actions .btn {
		--btn-h: 42px;
	}

	/* --- Toolbars (search + filters above a list) ---------------------------
	   The search box is capped at 24rem and the selects size to content on the desktop;
	   on a phone each control takes the full width and they stack, which is both readable
	   and thumb-sized. */
	.ds-toolbar {
		gap: 0.5rem;
	}
	.ds-search {
		min-width: 100%;
		max-width: none;
	}
	.ds-toolbar .form-select,
	.ds-toolbar .form-control {
		width: 100%;
	}
	.ds-toolbar__end {
		margin-left: 0;
		width: 100%;
	}
	.ds-toolbar .form-select,
	.ds-toolbar .form-control,
	.form-select,
	.form-control {
		/* 16px stops iOS Safari from zooming the page when a field takes focus. */
		font-size: 16px;
		min-height: 42px;
	}

	/* --- Tab strips: one swipeable row instead of a wrapped pile ------------- */
	.ds-tabs {
		display: flex;
		flex-wrap: nowrap;
		overflow-x: auto;
		scroll-snap-type: x proximity;
		max-width: 100%;
		border-radius: var(--radius-md, 0.75rem);
		-webkit-overflow-scrolling: touch;
		scrollbar-width: none;
	}
	.ds-tabs::-webkit-scrollbar {
		display: none;
	}
	.ds-tab {
		flex: 0 0 auto;
		scroll-snap-align: start;
		min-height: 40px;
	}

	/* --- Button groups wrap instead of overflowing -------------------------- */
	.btn-group {
		flex-wrap: wrap;
	}

	/* Cards lose a little padding before they lose content. */
	.ds-card__body {
		padding: 0.9rem;
	}
	/* A card header is title + actions on one nowrap row; two buttons and a title need
	   ~410px, which pushed the whole page sideways on the OR detail. Let it wrap. */
	.ds-card__head {
		flex-wrap: wrap;
		gap: 0.5rem 0.75rem;
		padding: 0.8rem 0.9rem;
	}

	/* A 16px checkbox is a miss waiting to happen. Switches (always `role="switch"` here)
	   keep their 2em track — resizing those would break the toggle. */
	.form-check-input:not([role='switch']) {
		width: 1.15rem;
		height: 1.15rem;
		margin-top: 0.15rem;
	}
	.form-check-label {
		padding: 0.15rem 0;
	}
}

/* --- Móvil (≤640px) ---
   Va ANTES que los bloques de 576px a propósito. Este fichero se ordena por
   TEMA, no por ancho, y a igualdad de especificidad gana el último: con este
   bloque al final, su `.ds-modal__body` le ganaba al de la hoja de abajo del
   móvil (≤576px) y le devolvía el 78vh y el relleno que ese bloque quita a
   propósito. Aquí arriba, el de 576 manda, que es lo que se espera.
   Cuidado si tocas esto: que un bloque más ancho esté más abajo NO es raro en
   este fichero (quedan uno de 768px y otro de 992px), y mientras no repitan
   selector Y propiedad da igual. Antes de añadir una regla a un bloque más
   ancho, mira si algún bloque más estrecho de más arriba toca lo mismo. */
@media (max-width: 640px) {
	/* La cifra hero oscura baja un punto para no comerse la card. */
	.ds-kpi--dark .ds-kpi__value {
		font-size: 2.1rem;
	}
	/* Gráficos de columnas de 12 meses: etiquetas alternas para que no choquen. */
	.gmb-col__label,
	.svy-col__label {
		font-size: 0.58rem;
	}
	.gmb-col:nth-child(even) .gmb-col__label,
	.svy-col:nth-child(even) .svy-col__label {
		visibility: hidden;
	}
	/* Acciones de tabla: algo más compactas (la tabla ya hace scroll lateral). */
	.ds-table__actions {
		gap: 0.6rem;
	}
	/* KPIs y stats a columna única sin mínimos que fuercen scroll. */
	.ds-kpi-row {
		grid-template-columns: repeat(auto-fit, minmax(9.5rem, 1fr));
	}
	.ds-modal__body {
		max-height: 78vh;
		--ds-modal-body-fill: calc(78vh - 2.5rem);
	}
}

/* ===========================================================================
   KPI CARDS (≤576px). Two 170px cards per row cannot hold "11.007,37 €" at 28px:
   the number ran past the card edge. The value now scales with the viewport (and
   can break as a last resort), and the cards give back some padding.
   =========================================================================== */
@media (max-width: 576px) {
	.ds-kpi-row {
		gap: 0.6rem;
	}
	.ds-kpi {
		padding: 1rem 1.05rem;
	}
	.ds-kpi__value {
		font-size: clamp(1.15rem, 5.4vw, 1.75rem);
		overflow-wrap: anywhere;
	}
	.ds-kpi__label {
		font-size: 0.66rem;
	}
}

/* ===========================================================================
   FORM GRID (≤576px). Bootstrap's `.col-*` classes already collapse, but a plain
   `.col` splits the row evenly at ANY width — three of them on a phone is 100px
   each. Here they wrap two per line at most, and a fourth/third drops below.
   =========================================================================== */
@media (max-width: 576px) {
	.row > .col {
		flex: 1 1 45%;
		min-width: 0;
	}
	/* An empty `.col` is only there to hold a desktop column open. */
	.row > .col:empty {
		display: none;
	}
}
@media (max-width: 400px) {
	/* A 360px phone: 45% is 150px, too narrow for a labelled field. One per line. */
	.row > .col {
		flex: 1 1 100%;
	}
}

/* ===========================================================================
   MODALS (≤576px): a bottom sheet that fills the screen instead of a centred box.
   The head stays put, the body scrolls, and the footer buttons are thumb-sized.
   =========================================================================== */
@media (max-width: 576px) {
	.ds-modal,
	.ds-modal--sm,
	.ds-modal--lg,
	.ds-modal--xl {
		width: calc(100vw - 12px);
		max-width: none;
		top: auto;
		bottom: 6px;
		left: 6px;
		transform: none;
		max-height: calc(100vh - 12px);
		/* dvh, not vh: on a phone `100vh` is the height WITHOUT the browser's address bar,
		   so a vh-sized sheet hides its last rows under the chrome. The vh line above stays
		   as the fallback for engines that do not know dvh. */
		max-height: calc(100dvh - 12px);
		display: flex;
		flex-direction: column;
		border-radius: 1rem;
	}
	.ds-modal__head,
	.ds-modal__foot {
		flex: 0 0 auto;
	}
	.ds-modal__body {
		/* The flex column owns the height now; a fixed 70vh would double-scroll.
		   `min-height: 0` is what makes it actually scroll: a flex item defaults to
		   `min-height: auto`, so without this it refuses to shrink below its content
		   and the sheet clips the tail of a long form instead of scrolling it. */
		flex: 1 1 auto;
		min-height: 0;
		max-height: none;
		padding: 1rem;
		/* En la hoja de abajo no hay saltos que evitar —el panel va anclado y ocupa lo
		   que hay—, así que nadie tiene que llenar nada: forzarlo dejaba media pantalla
		   en blanco debajo de un paso corto. */
		--ds-modal-body-fill: 0;
	}
	.ds-modal__foot {
		padding: 0.75rem 1rem;
	}
	.ds-modal__foot .btn {
		flex: 1 1 auto;
		--btn-h: 44px;
	}
}

/* ===========================================================================
   TABLES ON A PHONE (≤768px): one card per row instead of a sideways drag.
   Each cell shows its column name (stamped as `data-label` by DataTable from the
   table's own <thead>). Rows the component could not map 1:1 — spanning summary
   lines, empty-state rows — keep the normal rendering, which is why the selectors
   below are scoped to `[data-stackable]`.
   =========================================================================== */
@media (max-width: 768px) {
	/* The scroll wrapper must stop clipping once rows are cards. */
	.table-responsive:has(> .ds-table--stack) {
		overflow-x: visible;
	}
	.ds-table--stack {
		display: block;
		width: 100%;
		border-collapse: separate;
	}
	/* The header is redundant once every cell names itself. */
	.ds-table--stack > thead {
		display: none;
	}
	.ds-table--stack > tbody {
		display: block;
	}
	.ds-table--stack > tbody > tr[data-stackable] {
		display: block;
		background: var(--color-surface, #fff);
		border: 1px solid var(--color-border, #e5e7eb);
		border-radius: var(--radius-md, 0.75rem);
		padding: 0.5rem 0.7rem;
		margin-bottom: 0.55rem;
	}
	.ds-table--stack > tbody > tr[data-stackable] > td {
		display: flex;
		align-items: baseline;
		justify-content: space-between;
		gap: 0.75rem;
		width: auto;
		border: 0;
		padding: 0.28rem 0;
		text-align: left !important;
	}
	/* Label left, value right — a two-column read that never needs scrolling. */
	.ds-table--stack > tbody > tr[data-stackable] > td[data-label]::before {
		content: attr(data-label);
		flex: 0 0 auto;
		max-width: 45%;
		font-size: 0.68rem;
		font-weight: 700;
		text-transform: uppercase;
		letter-spacing: 0.3px;
		color: var(--color-muted-2, #6b7280);
	}
	/* The first column is the row's identity on almost every list (plate, customer,
	   invoice number), so it becomes the card's heading instead of one more label/value
	   pair: bigger, on its own line, ruled off from the fields below — and its link gets
	   a real tap target rather than a 22px line of text. */
	.ds-table--stack > tbody > tr[data-stackable] > td:first-child {
		display: block;
		font-size: 0.98rem;
		font-weight: 700;
		padding: 0.1rem 0 0.45rem;
		margin-bottom: 0.3rem;
		border-bottom: 1px solid var(--color-border-2, #eef0f3);
	}
	.ds-table--stack > tbody > tr[data-stackable] > td:first-child::before {
		display: none;
	}
	.ds-table--stack > tbody > tr[data-stackable] > td:first-child a {
		display: inline-block;
		padding: 0.2rem 0;
	}
	/* A cell with no header name (the actions column) spans the full width instead. */
	.ds-table--stack > tbody > tr[data-stackable] > td:not([data-label]) {
		justify-content: flex-end;
		padding-top: 0.45rem;
	}
	/* An empty cell adds a blank line for nothing. */
	.ds-table--stack > tbody > tr[data-stackable] > td:empty {
		display: none;
	}
	/* Numbers stop being right-aligned columns and become plain values. */
	.ds-table--stack > tbody > tr[data-stackable] > td.ds-table__num {
		font-variant-numeric: tabular-nums;
	}
	/* Totals keep their weight but as a distinct card. */
	.ds-table--stack > tbody > tr.ds-table__row--total[data-stackable],
	.ds-table--stack > tbody > tr.ds-table__row--grand[data-stackable] {
		background: var(--color-surface-2, #f7f8fa);
		font-weight: 700;
	}
	/* Row actions: full-width, thumb-sized, not a 12px icon strip. */
	.ds-table--stack .ds-table__actions {
		width: 100%;
		justify-content: flex-end;
		gap: 0.9rem;
	}
	.ds-table--stack .ds-table__actions > * {
		min-height: 40px;
		display: inline-flex;
		align-items: center;
	}
}

@media (max-width: 480px) {
	body {
		--page-padding: 0.85rem;
	}
}

@media (max-width: 380px) {
	.std-page-header__title {
		font-size: 1.1rem;
	}
}

/* --- Monitores grandes: más aire lateral, el contenido sigue siendo fluido. --- */
@media (min-width: 1800px) {
	:root {
		--page-padding: 2.75rem;
	}
}

/* --- Tablet / pantallas medias --- */
@media (max-width: 992px) {
	/* Los banners grandes pierden padding antes que legibilidad. */
	.ds-hero-banner,
	.ds-cta-banner {
		padding: 1.25rem 1.35rem;
	}
	.ds-hero-banner__steps {
		width: 100%;
		justify-content: center;
	}
}
