/* ===========================================================================
   Bot Hub ACP — application styles

   Everything here consumes tokens.css and nothing here declares a raw colour.
   The load-bearing rule of the whole file is the table→card breakpoint at the
   bottom: every list page in the ACP renders one `<table class="data">`, and
   below 720px each row becomes a card with its column headers inlined. That is
   what makes 3,287 tracked users survive a phone screen.
   =========================================================================== */

* {
	box-sizing: border-box;
}

/* `hidden` is a semantic assertion, not a style suggestion — and the button
   base below sets `display: inline-flex` through :where(), which would
   otherwise beat the user-agent rule and un-hide it. */
[hidden] {
	display: none !important;
}

html,
body {
	margin: 0;
	min-height: 100dvh;
	background: var(--bg);
	color: var(--text);
	font-family: var(--font-body);
	font-size: var(--fs-body);
	line-height: 1.5;
	-webkit-text-size-adjust: 100%;
}

a {
	color: var(--accent);
}

code,
kbd,
samp,
.mono {
	font-family: var(--font-mono);
	font-size: 0.92em;
}

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

.app {
	display: flex;
	flex-direction: column;
	min-height: 100dvh;
}

.app-header {
	position: sticky;
	top: 0;
	z-index: 20;
	display: flex;
	align-items: center;
	gap: var(--sp-3);
	min-height: calc(var(--header-height) + var(--safe-top));
	padding: var(--safe-top) calc(var(--sp-4) + var(--safe-right)) 0 calc(var(--sp-4) + var(--safe-left));
	background: var(--bg2);
	border-bottom: 1px solid var(--border);
}

.brand {
	display: flex;
	align-items: baseline;
	gap: var(--sp-2);
	font-size: var(--fs-subtitle);
	font-weight: 600;
	letter-spacing: 0.01em;
	text-decoration: none;
	color: var(--text);
}

.brand .env {
	font-size: var(--fs-chip);
	color: var(--muted);
	text-transform: uppercase;
	letter-spacing: 0.08em;
}

.header-spacer {
	flex: 1;
}

.header-user {
	font-size: var(--fs-meta-sm);
	color: var(--muted);
	max-width: 40vw;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* Primary navigation. A horizontal scroller on a phone rather than a hamburger:
   seven destinations, all of them one tap away, and no drawer state to get stuck. */
.app-nav {
	position: sticky;
	top: calc(var(--header-height) + var(--safe-top));
	z-index: 19;
	display: flex;
	gap: var(--sp-1);
	padding: var(--sp-2) calc(var(--sp-3) + var(--safe-right)) var(--sp-2) calc(var(--sp-3) + var(--safe-left));
	background: var(--bg);
	border-bottom: 1px solid var(--border);
	overflow-x: auto;
	scrollbar-width: none;
}

.app-nav::-webkit-scrollbar {
	display: none;
}

.app-nav a {
	display: inline-flex;
	align-items: center;
	min-height: var(--tap);
	padding: 0 var(--sp-3);
	border-radius: var(--r-pill);
	color: var(--muted);
	text-decoration: none;
	font-size: var(--fs-control);
	white-space: nowrap;
}

.app-nav a[aria-current="page"] {
	background: var(--bg3);
	color: var(--text);
}

.app-main {
	flex: 1;
	width: 100%;
	max-width: var(--content-max);
	margin: 0 auto;
	padding: var(--sp-4) calc(var(--sp-4) + var(--safe-right)) calc(var(--sp-6) + var(--safe-bottom))
		calc(var(--sp-4) + var(--safe-left));
}

.page-head {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: var(--sp-3);
	margin-bottom: var(--sp-4);
}

.page-head h1 {
	margin: 0;
	font-size: var(--fs-title);
}

.page-head .page-actions {
	margin-left: auto;
	display: flex;
	flex-wrap: wrap;
	gap: var(--sp-2);
}

.page-sub {
	margin: calc(-1 * var(--sp-2)) 0 var(--sp-4);
	color: var(--muted);
	font-size: var(--fs-meta-md);
}

/* ---------------------------------------------------------------------------
   Cards, sections, chips
   --------------------------------------------------------------------------- */

.card {
	background: var(--bg2);
	border: 1px solid var(--border);
	border-radius: var(--r-xl);
	padding: var(--sp-4);
	margin-bottom: var(--sp-4);
}

.card > h2 {
	margin: 0 0 var(--sp-3);
	font-size: var(--fs-subtitle);
}

.card-note {
	margin: 0 0 var(--sp-3);
	color: var(--muted);
	font-size: var(--fs-meta-sm);
}

.chip {
	display: inline-flex;
	align-items: center;
	gap: var(--sp-1);
	padding: 2px var(--sp-2);
	border-radius: var(--r-pill);
	background: var(--bg3);
	border: 1px solid var(--border);
	color: var(--muted);
	font-size: var(--fs-chip);
	white-space: nowrap;
}

.chip.ok {
	color: var(--success);
	border-color: color-mix(in srgb, var(--success) 40%, var(--border));
}

.chip.warn {
	color: var(--warn);
	border-color: color-mix(in srgb, var(--warn) 40%, var(--border));
}

.chip.bad {
	color: var(--danger);
	border-color: color-mix(in srgb, var(--danger) 40%, var(--border));
}

.stack {
	display: flex;
	flex-direction: column;
	gap: var(--sp-3);
}

.row {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: var(--sp-2);
}

.muted {
	color: var(--muted);
}

.meta {
	color: var(--muted);
	font-size: var(--fs-meta-sm);
}

.truncate {
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
	max-width: 100%;
}

/* ---------------------------------------------------------------------------
   Buttons and forms
   --------------------------------------------------------------------------- */

:where(button, .btn) {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: var(--sp-2);
	min-height: var(--tap);
	padding: 0 var(--sp-4);
	border-radius: var(--r-lg);
	border: 1px solid var(--border-strong);
	background: var(--bg3);
	color: var(--text);
	font: inherit;
	font-size: var(--fs-control);
	text-decoration: none;
	cursor: pointer;
}

:where(button, .btn):disabled,
:where(button, .btn)[aria-disabled="true"] {
	opacity: 0.55;
	cursor: not-allowed;
}

.btn-primary {
	background: var(--accent);
	border-color: var(--accent);
	color: var(--accent-fg);
	font-weight: 600;
}

.btn-danger {
	background: transparent;
	border-color: color-mix(in srgb, var(--danger) 55%, var(--border));
	color: var(--danger);
}

.btn-quiet {
	background: transparent;
	border-color: transparent;
	color: var(--muted);
	padding: 0 var(--sp-2);
}

.btn-sm {
	min-height: 36px;
	padding: 0 var(--sp-3);
	font-size: var(--fs-meta-md);
}

/* Row-level actions stay ≥44px worth of hit area on touch even when the button
   itself is the compact variant. */
@media (pointer: coarse) {
	.btn-sm {
		min-height: var(--tap);
	}
}

label {
	display: block;
	font-size: var(--fs-meta-md);
	color: var(--muted);
	margin-bottom: var(--sp-1);
}

input,
select,
textarea {
	width: 100%;
	min-height: var(--tap);
	padding: 0 var(--sp-3);
	border-radius: var(--r-lg);
	border: 1px solid var(--border-strong);
	background: var(--bg);
	color: var(--text);
	font: inherit;
	/* 16px minimum on iOS or focusing the field zooms the whole page. */
	font-size: max(16px, var(--fs-control));
}

textarea {
	padding: var(--sp-2) var(--sp-3);
	min-height: 96px;
	line-height: 1.45;
	resize: vertical;
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible,
a:focus-visible {
	outline: 2px solid var(--accent);
	outline-offset: 2px;
}

.field {
	margin-bottom: var(--sp-3);
}

.field-hint {
	margin-top: var(--sp-1);
	color: var(--muted);
	font-size: var(--fs-meta-xs);
}

.form-grid {
	display: grid;
	gap: var(--sp-3);
	grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

.form-actions {
	display: flex;
	flex-wrap: wrap;
	gap: var(--sp-2);
	margin-top: var(--sp-3);
}

.switch {
	display: inline-flex;
	align-items: center;
	gap: var(--sp-2);
	min-height: var(--tap);
	color: var(--text);
	font-size: var(--fs-control);
	margin: 0;
}

.switch input {
	width: auto;
	min-height: 0;
	accent-color: var(--accent);
	transform: scale(1.25);
}

/* The search + filter strip above every paged list. */
.toolbar {
	display: flex;
	flex-wrap: wrap;
	gap: var(--sp-2);
	margin-bottom: var(--sp-3);
}

.toolbar .grow {
	flex: 1 1 200px;
}

/* Filter dropdowns wrap onto their own line before they shrink to unreadable:
   inputs are width:100% by default, which as a bare flex item would fight every
   other control in the strip. */
.toolbar > select {
	flex: 1 1 160px;
	width: auto;
	min-width: 0;
}

.tabs {
	display: flex;
	gap: var(--sp-1);
	margin-bottom: var(--sp-3);
	overflow-x: auto;
	scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
	display: none;
}

.tabs button {
	min-height: 38px;
	padding: 0 var(--sp-3);
	border-radius: var(--r-pill);
	background: transparent;
	border-color: var(--border);
	color: var(--muted);
	white-space: nowrap;
}

.tabs button[aria-selected="true"] {
	background: var(--bg3);
	border-color: var(--border-strong);
	color: var(--text);
}

/* ---------------------------------------------------------------------------
   Tables — and the breakpoint that turns them into cards
   --------------------------------------------------------------------------- */

.table-wrap {
	overflow-x: auto;
	-webkit-overflow-scrolling: touch;
}

table.data {
	width: 100%;
	border-collapse: collapse;
	font-size: var(--fs-meta-md);
}

table.data th,
table.data td {
	padding: var(--sp-2) var(--sp-3);
	text-align: left;
	vertical-align: top;
	border-bottom: 1px solid var(--border);
}

table.data th {
	color: var(--muted);
	font-weight: 600;
	font-size: var(--fs-meta-sm);
	white-space: nowrap;
}

table.data tbody tr:last-child td {
	border-bottom: none;
}

table.data td.actions {
	white-space: nowrap;
}

.pager {
	display: flex;
	align-items: center;
	gap: var(--sp-3);
	margin-top: var(--sp-3);
}

.pager .pager-info {
	color: var(--muted);
	font-size: var(--fs-meta-sm);
}

/* A list row whose identity cell is a link to its detail page. The whole cell is
   the tap target — on a phone the name is the biggest thing on the card — so it
   must not look like body text, and must not shout like a link either. */
.row-link {
	display: block;
	color: inherit;
	text-decoration: none;
}

.row-link:hover .truncate:first-child,
.row-link:focus-visible .truncate:first-child {
	text-decoration: underline;
}

/* ---------------------------------------------------------------------------
   Detail pages: fact lists, payloads, revealed content
   --------------------------------------------------------------------------- */

/* A subject's own fields are already a label/value pair at every width, so this
   is a two-column grid rather than a table — the table→card breakpoint exists to
   rescue *lists*, and there is nothing here for it to rescue. 120px is the
   narrowest label column that still fits "Detected by" on a 320px screen. */
.facts {
	display: grid;
	grid-template-columns: minmax(120px, 30%) 1fr;
	gap: var(--sp-2) var(--sp-3);
	margin: 0;
}

.facts dt {
	color: var(--muted);
	font-size: var(--fs-meta-sm);
}

.facts dd {
	margin: 0;
	font-size: var(--fs-meta-md);
	min-width: 0;
	overflow-wrap: anywhere;
}

.payload-details summary {
	cursor: pointer;
	color: var(--muted);
	font-size: var(--fs-meta-sm);
	min-height: var(--tap);
	display: flex;
	align-items: center;
}

/* The payload can be a long line of JSON on a 320px screen, so it wraps rather
   than pushing the whole card sideways. */
.payload {
	margin: var(--sp-2) 0 0;
	padding: var(--sp-3);
	border-radius: var(--r-lg);
	background: var(--inline-code-bg);
	border: 1px solid var(--border);
	font-family: var(--font-mono);
	font-size: var(--fs-meta-sm);
	white-space: pre-wrap;
	overflow-wrap: anywhere;
	max-height: 40vh;
	overflow-y: auto;
}

/* Revealed message text. Deliberately distinct from every other value on the
   page: it is the one thing here that is somebody's words, and it exists only
   until this render is torn down. */
.revealed {
	padding: var(--sp-2) var(--sp-3);
	border-radius: var(--r-lg);
	background: var(--bg3);
	border: 1px solid var(--border-strong);
	white-space: pre-wrap;
	overflow-wrap: anywhere;
}

/* ---------------------------------------------------------------------------
   States: empty, error, loading, toast, dialog
   --------------------------------------------------------------------------- */

.state {
	padding: var(--sp-5) var(--sp-4);
	text-align: center;
	color: var(--muted);
	font-size: var(--fs-meta-md);
}

.state.error {
	color: var(--danger);
}

.spinner {
	display: inline-block;
	width: 18px;
	height: 18px;
	border: 2px solid var(--border-strong);
	border-top-color: var(--accent);
	border-radius: 50%;
	animation: spin 0.7s linear infinite;
	vertical-align: -3px;
}

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

@media (prefers-reduced-motion: reduce) {
	.spinner {
		animation-duration: 2.5s;
	}
}

.toasts {
	position: fixed;
	left: 50%;
	transform: translateX(-50%);
	bottom: calc(var(--sp-4) + var(--safe-bottom));
	z-index: 60;
	display: flex;
	flex-direction: column;
	gap: var(--sp-2);
	width: min(520px, calc(100vw - 2 * var(--sp-4)));
	pointer-events: none;
}

.toast {
	pointer-events: auto;
	background: var(--bg3);
	border: 1px solid var(--border-strong);
	border-radius: var(--r-lg);
	box-shadow: var(--shadow-lift);
	padding: var(--sp-3) var(--sp-4);
	font-size: var(--fs-meta-md);
}

.toast.error {
	border-color: color-mix(in srgb, var(--danger) 55%, var(--border));
	color: var(--danger);
}

.toast.ok {
	border-color: color-mix(in srgb, var(--success) 45%, var(--border));
}

dialog {
	width: min(560px, calc(100vw - 2 * var(--sp-4)));
	max-height: calc(100dvh - 2 * var(--sp-5));
	padding: var(--sp-4);
	border: 1px solid var(--border);
	border-radius: var(--r-2xl);
	background: var(--bg2);
	color: var(--text);
	box-shadow: var(--shadow-modal);
}

dialog::backdrop {
	background: var(--overlay);
}

dialog h2 {
	margin: 0 0 var(--sp-3);
	font-size: var(--fs-subtitle);
}

.secret-box {
	margin: var(--sp-3) 0;
	padding: var(--sp-3);
	border-radius: var(--r-lg);
	background: var(--inline-code-bg);
	border: 1px solid var(--border);
	font-family: var(--font-mono);
	font-size: var(--fs-meta-sm);
	word-break: break-all;
	user-select: all;
}

/* ---------------------------------------------------------------------------
   Login — the one page that is not inside the app frame
   --------------------------------------------------------------------------- */

.login-wrap {
	min-height: 100dvh;
	display: grid;
	place-items: center;
	padding: calc(var(--sp-5) + var(--safe-top)) var(--sp-4) calc(var(--sp-5) + var(--safe-bottom));
}

.login-card {
	width: min(400px, 100%);
	background: var(--bg2);
	border: 1px solid var(--border);
	border-radius: var(--r-2xl);
	padding: var(--sp-5);
}

.login-card h1 {
	margin: 0 0 var(--sp-1);
	font-size: var(--fs-title);
}

.login-card .page-sub {
	margin: 0 0 var(--sp-4);
}

/* ---------------------------------------------------------------------------
   The table→card breakpoint

   Below 720px every `table.data` stops being a table. Each `<td>` carries a
   `data-label` (the page builders set it from the column header) and renders as
   a label/value line inside a card. Without this a tracker page is a horizontal
   scroll bar on a phone, which is the same as not shipping the page.
   --------------------------------------------------------------------------- */

@media (max-width: 720px) {
	table.data,
	table.data tbody,
	table.data tr,
	table.data td {
		display: block;
		width: 100%;
	}

	table.data thead {
		display: none;
	}

	table.data tr {
		background: var(--bg2);
		border: 1px solid var(--border);
		border-radius: var(--r-xl);
		padding: var(--sp-2) var(--sp-3);
		margin-bottom: var(--sp-3);
	}

	table.data td {
		display: grid;
		grid-template-columns: minmax(88px, 34%) 1fr;
		gap: var(--sp-3);
		padding: var(--sp-2) 0;
		border-bottom: 1px solid var(--border);
	}

	table.data tr td:last-child {
		border-bottom: none;
	}

	table.data td::before {
		content: attr(data-label);
		color: var(--muted);
		font-size: var(--fs-meta-sm);
	}

	/* An action cell is buttons, not a value: it gets the full width. */
	table.data td.actions {
		display: flex;
		flex-wrap: wrap;
		gap: var(--sp-2);
		white-space: normal;
	}

	table.data td.actions::before {
		content: none;
	}

	.card {
		padding: var(--sp-3);
	}
}
