/* Permissioner SPA styles. Custom properties drive light/dark/auto theming. */

:root {
	--bg: #f6f7f9;
	--surface: #ffffff;
	--surface-2: #f0f2f5;
	--border: #dfe3e8;
	--text: #1b1f24;
	--text-dim: #5b626b;
	--accent: #4f46e5;
	--accent-contrast: #ffffff;
	--primary-bg: #4f46e5;
	--primary-fg: #ffffff;
	--ok: #137a4b;
	--ok-bg: #e3f5ec;
	--warn: #9a6700;
	--warn-bg: #fbf1d6;
	--danger: #b3261e;
	--danger-bg: #fbe6e4;
	--info: #2563a8;
	--info-bg: #e4eefb;
	--shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.1);
	--radius: 10px;
	--mono: ui-monospace, "SF Mono", "JetBrains Mono", "Cascadia Code", Menlo, Consolas, monospace;
	--sans: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
	color-scheme: light;
	accent-color: var(--accent);
}

:root[data-theme="light"] { color-scheme: light; }

:root[data-theme="dark"] {
	color-scheme: dark;
	--bg: #0f1115;
	--surface: #181b21;
	--surface-2: #20242c;
	--border: #2c313a;
	--text: #e6e8ec;
	--text-dim: #9aa1ac;
	--accent: #818cf8;
	--accent-contrast: #0f1115;
	/* Keep --accent light for link/badge legibility on dark, but give the primary button a
	   stronger indigo fill with white text so the main CTA isn't washed out. */
	--primary-bg: #6366f1;
	--primary-fg: #ffffff;
	--ok: #4ade80;
	--ok-bg: #10291d;
	--warn: #fbbf24;
	--warn-bg: #2c2410;
	--danger: #f87171;
	--danger-bg: #2c1514;
	--info: #7dd3fc;
	--info-bg: #122430;
	--shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

@media (prefers-color-scheme: dark) {
	:root[data-theme="auto"] {
		color-scheme: dark;
		--bg: #0f1115;
		--surface: #181b21;
		--surface-2: #20242c;
		--border: #2c313a;
		--text: #e6e8ec;
		--text-dim: #9aa1ac;
		--accent: #818cf8;
		--accent-contrast: #0f1115;
		--primary-bg: #6366f1;
		--primary-fg: #ffffff;
		--ok: #4ade80;
		--ok-bg: #10291d;
		--warn: #fbbf24;
		--warn-bg: #2c2410;
		--danger: #f87171;
		--danger-bg: #2c1514;
		--info: #7dd3fc;
		--info-bg: #122430;
		--shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
	}
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
	background: var(--bg);
	color: var(--text);
	font-family: var(--sans);
	font-size: 15px;
	line-height: 1.5;
	-webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.boot { padding: 4rem; text-align: center; color: var(--text-dim); }

/* Layout ------------------------------------------------------------------ */

.appbar {
	display: flex;
	flex-direction: column;
	align-items: stretch;
	gap: 0.5rem;
	padding: 0.6rem 1rem;
	background: var(--surface);
	border-bottom: 1px solid var(--border);
	position: sticky;
	top: 0;
	z-index: 10;
}
.appbar-top { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.appbar-actions { display: flex; align-items: center; gap: 0.25rem; }
.appbar .brand { font-weight: 700; letter-spacing: -0.01em; display: flex; align-items: center; gap: 0.45rem; }
.appbar .brand i { color: var(--accent); }
.appbar nav { display: flex; gap: 0.25rem; flex-wrap: wrap; }
.appbar nav a {
	padding: 0.35rem 0.7rem;
	border-radius: 8px;
	color: var(--text-dim);
	font-weight: 500;
}
.appbar nav a.active { background: var(--surface-2); color: var(--text); }
.appbar nav a:hover { text-decoration: none; background: var(--surface-2); }

main { max-width: 880px; margin: 0 auto; padding: 1.25rem 1rem 4rem; }

.page-head { display: flex; align-items: baseline; gap: 0.75rem; margin: 0.5rem 0 1.25rem; }
.page-head h1 { font-size: 1.4rem; margin: 0; letter-spacing: -0.02em; }
.page-head .sub { color: var(--text-dim); font-size: 0.9rem; }

/* Buttons ----------------------------------------------------------------- */

button, .btn {
	font: inherit;
	font-weight: 600;
	border: 1px solid var(--border);
	background: var(--surface);
	color: var(--text);
	padding: 0.45rem 0.85rem;
	border-radius: 8px;
	cursor: pointer;
	display: inline-flex;
	align-items: center;
	gap: 0.4rem;
	transition: background 0.12s, border-color 0.12s;
}
button:hover, .btn:hover { background: var(--surface-2); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary-bg); border-color: var(--primary-bg); color: var(--primary-fg); }
.btn-primary:hover { filter: brightness(1.07); background: var(--primary-bg); }
.btn-danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.btn-danger:hover { background: var(--danger-bg); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { padding: 0.28rem 0.55rem; font-size: 0.85rem; }
.btn-row { display: flex; gap: 0.5rem; flex-wrap: wrap; }

/* Inputs ------------------------------------------------------------------ */

input[type="text"], input[type="url"], textarea, select {
	font: inherit;
	width: 100%;
	padding: 0.5rem 0.6rem;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: var(--surface);
	color: var(--text);
}
textarea { resize: vertical; min-height: 4rem; }
label { display: block; font-weight: 600; font-size: 0.85rem; margin: 0.75rem 0 0.3rem; color: var(--text-dim); }
input:focus, textarea:focus, select:focus { outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent); outline-offset: 1px; border-color: var(--accent); }
input[type="range"] { width: 100%; accent-color: var(--accent); }

/* Keyboard focus ring for the interactive elements the input rule doesn't cover
   (buttons, links, nav, pager arrows, the accordion summary, modal actions).
   :focus-visible keeps it keyboard-only — no ring on mouse clicks. */
button:focus-visible, .btn:focus-visible, a:focus-visible, summary:focus-visible, [tabindex]:focus-visible {
	outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent);
	outline-offset: 2px;
}
summary.card-summary:focus:not(:focus-visible) { outline: none; }

/* Cards ------------------------------------------------------------------- */

.card {
	background: var(--surface);
	border: 1px solid var(--border);
	border-radius: var(--radius);
	box-shadow: var(--shadow);
	padding: 1rem 1.1rem;
	margin-bottom: 0.9rem;
}
.card.dim { opacity: 0.85; }
.card-head { display: flex; align-items: center; gap: 0.7rem; }
.card-head .logo { width: 30px; height: 30px; border-radius: 7px; object-fit: cover; background: var(--surface-2); flex: none; }
.card-head .logo-fallback { display: flex; align-items: center; justify-content: center; color: var(--text-dim); }
.card-head .title { font-weight: 650; flex: 1; min-width: 0; }
.card-head .client { color: var(--text-dim); font-size: 0.82rem; }
.callback-row { display: flex; align-items: center; gap: 0.3rem; min-width: 0; }
.callback-row .callback-url { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.btn-icon { padding: 0.1rem 0.35rem; line-height: 1; flex: none; color: var(--text-dim); }
.btn-icon:hover { color: var(--text); }
.card .desc { color: var(--text); margin: 0 0 0.55rem; white-space: pre-wrap; overflow-wrap: anywhere; }
.card .meta { display: flex; gap: 1rem; flex-wrap: wrap; color: var(--text-dim); font-size: 0.8rem; margin-top: 0.4rem; }

/* Accordion cards (queue): native <details>; summary is the header (scoped via
   .card-summary so the clients page's create-card .u-summary is untouched). */
.card > summary.card-summary { display: flex; align-items: center; gap: 0.7rem; cursor: pointer; list-style: none; }
.card > summary.card-summary::-webkit-details-marker { display: none; }
.card-summary .chevron { color: var(--text-dim); display: inline-flex; flex: none; transition: transform 0.15s ease; }
.card[open] > .card-summary .chevron { transform: rotate(90deg); }
.card-summary .logo { width: 30px; height: 30px; border-radius: 7px; object-fit: cover; background: var(--surface-2); flex: none; }
.card-summary .logo-fallback { display: flex; align-items: center; justify-content: center; color: var(--text-dim); }
.card-summary .summary-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.card-summary .summary-title { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-summary .summary-client { color: var(--text-dim); font-size: 0.82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-summary .summary-time { color: var(--text-dim); font-size: 0.8rem; white-space: nowrap; flex: none; }
.card-body { margin-top: 0.8rem; padding-top: 0.8rem; border-top: 1px solid var(--border); }

/* Structured body as a definition list: two-column table that stacks on narrow screens. */
.fields { display: grid; grid-template-columns: minmax(6rem, max-content) 1fr; gap: 0.35rem 1rem; margin: 0.4rem 0; font-size: 0.9rem; }
.fields dt { color: var(--text-dim); font-weight: 600; overflow-wrap: anywhere; }
.fields dd { margin: 0; overflow-wrap: anywhere; }
.fields .field-list { margin: 0; padding-left: 0; list-style: none; }
.fields .field-list li { margin: 0.1rem 0; }
@media (max-width: 30rem) {
	.fields { grid-template-columns: 1fr; gap: 0; }
	.fields dt { margin-top: 0.5rem; }
	.fields dd { margin-bottom: 0.2rem; }
}

/* Queue sections (WAITING / HISTORY) */
.queue-section { margin-bottom: 1.6rem; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; flex-wrap: wrap; margin-bottom: 0.7rem; padding-bottom: 0.4rem; border-bottom: 2px solid var(--border); }
.section-title { display: flex; align-items: baseline; gap: 0.5rem; }
.section-name { font-weight: 700; font-size: 1.02rem; }
.section-count { color: var(--text-dim); font-variant-numeric: tabular-nums; font-size: 0.85rem; }
.section-head .pager { margin-bottom: 0; }

/* Step-up controls (config view) */
.toggle-row { display: flex; align-items: center; gap: 0.5rem; }
.toggle-row input[type="checkbox"] { width: auto; margin: 0; flex: none; }
.stepup-binding { display: flex; align-items: center; gap: 0.8rem; }
.stepup-slider { flex: 1; }
.stepup-window-val { font-variant-numeric: tabular-nums; color: var(--text-dim); white-space: nowrap; min-width: 6.5rem; text-align: right; }
.stepup-hint { display: inline-flex; align-items: center; gap: 0.3rem; color: var(--text-dim); font-size: 0.78rem; margin-left: auto; }

/* Status pills ------------------------------------------------------------ */

.pill {
	display: inline-flex;
	align-items: center;
	gap: 0.3rem;
	font-size: 0.74rem;
	font-weight: 700;
	text-transform: uppercase;
	letter-spacing: 0.03em;
	padding: 0.15rem 0.5rem;
	border-radius: 999px;
}
.pill.waiting { background: var(--info-bg); color: var(--info); }
.pill.approved { background: var(--ok-bg); color: var(--ok); }
.pill.rejected { background: var(--danger-bg); color: var(--danger); }
.pill.expired { background: var(--surface-2); color: var(--text-dim); }
.countdown { font-variant-numeric: tabular-nums; font-weight: 600; }
/* Near-expiry cue is not colour-only: add a wavy underline so it reads without hue. */
.countdown.soon { color: var(--warn); text-decoration: underline; text-decoration-style: wavy; text-underline-offset: 2px; }

/* Sign-in (login) request distinctions */
.card-summary .summary-title-row { display: flex; align-items: center; gap: 0.5rem; min-width: 0; }
/* F15: let the ellipsis title shrink beside the badge instead of forcing overflow. */
.card-summary .summary-title-row .summary-title { min-width: 0; flex: 1 1 0; }
.kind-badge {
	display: inline-flex;
	align-items: center;
	gap: 0.3rem;
	flex: none;
	font-size: 0.7rem;
	font-weight: 700;
	text-transform: uppercase;
	letter-spacing: 0.03em;
	padding: 0.12rem 0.45rem;
	border-radius: 999px;
}
.kind-badge.login { background: var(--info-bg); color: var(--info); }

/* Automatic-login WAITING card: not inline-decidable — a tap-to-review link into the
   approval screen. Reuses the card chrome; the CTA replaces the status pill. */
a.review-card { display: flex; align-items: center; gap: 0.7rem; text-decoration: none; color: inherit; cursor: pointer; }
a.review-card:hover { border-color: color-mix(in srgb, var(--info) 45%, var(--border)); }
a.review-card .summary-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
a.review-card .logo { width: 30px; height: 30px; border-radius: 7px; object-fit: cover; background: var(--surface-2); flex: none; }
a.review-card .logo-fallback { display: flex; align-items: center; justify-content: center; color: var(--text-dim); }
.review-cta { color: var(--info); font-weight: 650; font-size: 0.85rem; white-space: nowrap; flex: none; display: inline-flex; align-items: center; gap: 0.2rem; }

/* Decided/expired cards keep a structural (empty for now) actions area — a future
   action (e.g. revoke a login authorization) lands here as an addition. */
.card-actions:empty { display: none; }

/* A login approval authorizes a sign-in — give its scopes a tinted panel so the
   owner reads them as "review this" rather than an ordinary request body. */
.scopes { border: 1px solid color-mix(in srgb, var(--info) 30%, var(--border)); background: var(--info-bg); border-radius: 8px; padding: 0.5rem 0.7rem; margin: 0.5rem 0; }
.scopes-label { font-weight: 700; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--info); margin: 0 0 0.2rem; }
.scopes .fields { margin: 0; }
/* The scope list is self-contained-scrollable on BOTH axes (component-level, never the
   page): long/wide scope sets stay reachable on mobile without pushing the decision
   buttons off-screen. Values clip with an ellipsis so truncation is VISIBLE; the full
   value is always reachable — title tooltip on desktop, tap-to-expand everywhere
   (`.expanded`, toggled by a listener in components/fields.ts). */
.scopes-scroll { max-height: 16rem; overflow: auto; }
.scopes-scroll .fields { grid-template-columns: minmax(6rem, max-content) minmax(12rem, 1fr); }
.scopes-scroll .fields dd,
.scopes-scroll .fields .field-list li { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer; }
.scopes-scroll .fields dd.expanded,
.scopes-scroll .fields .field-list li.expanded { white-space: normal; overflow: visible; text-overflow: clip; overflow-wrap: anywhere; }

/* Automatic-login approval screen (in-shell, /login/approve) */
.approval { display: flex; justify-content: center; padding: 1rem 0; }
.approval-card { width: 100%; max-width: 34rem; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); padding: 1rem 1.1rem; }
.approval-head { display: flex; align-items: flex-start; gap: 0.7rem; }
.approval-head .logo { width: 40px; height: 40px; border-radius: 9px; object-fit: cover; background: var(--surface-2); flex: none; }
.approval-head .logo-fallback { display: flex; align-items: center; justify-content: center; color: var(--text-dim); }
.approval-title { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 0.15rem; }
.approval-title h1 { font-size: 1.15rem; margin: 0; overflow-wrap: anywhere; }
.approval-title .kind-badge { align-self: flex-start; }
.approval-close { flex: none; }
.approval-note { font-size: 0.8rem; margin: 0.7rem 0 0; }
.code-label { margin-top: 0.6rem; }
.code-input { font-family: var(--mono); letter-spacing: 0.02em; }
.code-input.invalid { border-color: var(--danger); outline-color: color-mix(in srgb, var(--danger) 55%, transparent); }
.field-error { color: var(--danger); font-size: 0.8rem; margin: 0.25rem 0 0; }
.spin { display: inline-flex; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Narrow viewports: reflow the accordion summary so the login badge can't collide with
   the status pill. Chevron + logo + pill + countdown stay on row 1 (all small); the
   title/client block drops to its own full-width row 2 and can wrap freely. */
@media (max-width: 30rem) {
	.card > summary.card-summary { flex-wrap: wrap; row-gap: 0.35rem; align-items: center; }
	.card-summary .summary-main { order: 5; flex-basis: 100%; }
	.card-summary .summary-title-row { flex-wrap: wrap; }
	.card-summary .summary-time { margin-left: auto; }
}

/* Filter pane ------------------------------------------------------------- */

.filters { display: flex; gap: 0.75rem; flex-wrap: wrap; align-items: flex-end; margin-bottom: 1.1rem; }
.filters .field { display: flex; flex-direction: column; gap: 0.2rem; }
.filters label { margin: 0; }
.filters select { width: auto; min-width: 9rem; }

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

.pager { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; flex-wrap: wrap; margin-bottom: 1.1rem; }
.pager-nav { display: flex; align-items: center; gap: 0.35rem; }
.pager-nav[hidden] { display: none; } /* override .pager-nav's display:flex when hidden on a single page */
.pager-side { display: flex; align-items: center; gap: 0.5rem; }
.pager-btn { padding: 0.3rem 0.5rem; line-height: 1; }
.pager-label { font-variant-numeric: tabular-nums; padding: 0 0.4rem; color: var(--text-dim); white-space: nowrap; }

@media (max-width: 560px) {
	.pager { flex-direction: column; align-items: stretch; }
	.pager-nav, .pager-side { justify-content: space-between; }
}

/* copyField --------------------------------------------------------------- */

.copyfield { display: flex; align-items: stretch; gap: 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; max-width: 100%; }
.copyfield .value { font-family: var(--mono); font-size: 0.85rem; padding: 0.45rem 0.6rem; background: var(--surface-2); flex: 1; overflow-x: auto; white-space: nowrap; user-select: all; }
.copyfield button { border: none; border-left: 1px solid var(--border); border-radius: 0; background: var(--surface); }
.copyfield button.copied { color: var(--ok); }

/* Modal ------------------------------------------------------------------- */

.modal-backdrop { position: fixed; inset: 0; background: rgba(8, 11, 16, 0.45); display: flex; align-items: center; justify-content: center; padding: 1rem; z-index: 50; }
.modal { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 12px 40px rgba(0,0,0,0.3); width: min(520px, 100%); padding: 1.25rem; }
.modal h2 { margin: 0 0 0.5rem; font-size: 1.1rem; }
.modal .modal-actions { display: flex; justify-content: flex-end; gap: 0.5rem; margin-top: 1.1rem; }

/* Misc -------------------------------------------------------------------- */

.empty { text-align: center; color: var(--text-dim); padding: 3rem 1rem; }
.empty i { font-size: 2rem; display: block; margin-bottom: 0.5rem; opacity: 0.6; }
/* Section-level empty (e.g. an empty History below a populated Waiting): compact, so it
   doesn't render as a full-height block that makes the page look broken. */
.empty.empty-compact { padding: 1.1rem 1rem; }
.empty.empty-compact i { font-size: 1.1rem; margin-bottom: 0.2rem; }
.empty.empty-compact p { margin: 0; font-size: 0.9rem; }
.toast-host { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 0.5rem; align-items: center; }
.toast { background: var(--text); color: var(--bg); padding: 0.5rem 0.9rem; border-radius: 8px; font-size: 0.88rem; box-shadow: var(--shadow); }
/* Fixed dark-red (not the theme --danger, which is a light red in dark mode giving
   ~2.7:1 on white) so error toasts stay legible in both themes. */
.toast.error { background: #b3261e; color: #fff; }
.row { display: flex; gap: 0.5rem; align-items: center; }
.list-item { display: flex; align-items: center; gap: 0.75rem; padding: 0.7rem 0; border-bottom: 1px solid var(--border); }
.list-item:last-child { border-bottom: none; }
.list-item .grow { flex: 1; min-width: 0; }
.list-item .name { font-weight: 600; }
.muted { color: var(--text-dim); font-size: 0.85rem; }
.fp { font-family: var(--mono); font-size: 0.8rem; color: var(--text-dim); word-break: break-all; }
.warnbox { background: var(--warn-bg); color: var(--warn); border-radius: 8px; padding: 0.6rem 0.8rem; font-size: 0.86rem; margin: 0.6rem 0; }

/* Login ------------------------------------------------------------------- */

.login-wrap { min-height: 80vh; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.login-card { width: min(380px, 100%); text-align: center; }
.login-card .brand-lg { font-size: 1.6rem; font-weight: 750; margin-bottom: 0.3rem; display: flex; gap: 0.5rem; align-items: center; justify-content: center; }
.login-card .brand-lg i { color: var(--accent); }
.login-card p { color: var(--text-dim); margin-top: 0; }
.login-card button { width: 100%; justify-content: center; margin-top: 0.6rem; }

/* Utilities (CSP: replace inline style attributes) ------------------------ */

.u-mt-0 { margin-top: 0; }
.u-mt-4 { margin-top: .4rem; }
.u-mt-5 { margin-top: .5rem; }
.u-mt-6 { margin-top: .6rem; }
.u-mt-7 { margin-top: .7rem; }
.u-mt-8 { margin-top: .8rem; }
.u-mb-3 { margin-bottom: .3rem; }
.u-fs-1 { font-size: 1rem; }
.u-text-danger { color: var(--danger); }
.u-divider { border: none; border-top: 1px solid var(--border); margin: 1.1rem 0; }
.u-summary { cursor: pointer; font-weight: 650; }
.u-justify-center { justify-content: center; }

@media (max-width: 560px) {
	.filters { flex-direction: column; align-items: stretch; }
	.filters select { width: 100%; }
}

/* Standalone branded error page (GET /login/automatic failures — served by the worker,
   outside the SPA). Static copy only; nothing user-controlled is ever rendered. */
body.standalone-error { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 1rem; }
.error-card { max-width: 26rem; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); padding: 1.2rem 1.4rem; }
.error-card h1 { font-size: 1.1rem; margin: 0 0 0.5rem; }
.error-card p { color: var(--text-dim); margin: 0.3rem 0; }

/* Respect reduced-motion: drop chevron rotation, button transitions, spinner. */
@media (prefers-reduced-motion: reduce) {
	* { transition: none !important; animation: none !important; }
}
