/* ==========================================================================
   Structured Cloud Portal — app chrome.
   Tokens follow Design System v2.0 "Liquid Glass" (BRAND-GUIDELINES.md).
   ========================================================================== */

@import url('https://fonts.googleapis.com/css2?family=Archivo:wght@400;500;600;700;800&family=Space+Grotesk:wght@300;400;500;600&family=JetBrains+Mono:wght@400;500&display=swap');

:root {
	--ink: #0C1219;
	--slate: #38454F;
	--accent: #5E73BB;
	--accent-light: #7FA3F0;
	--accent-bright: #9BD8EE;
	--gradient-cta: linear-gradient(120deg, #46599F 0%, #5E73BB 100%);
	--font-display: "Archivo", "Segoe UI", Helvetica, Arial, sans-serif;
	--font-body: "Space Grotesk", "Segoe UI", Helvetica, Arial, sans-serif;
	--font-mono: "JetBrains Mono", "Consolas", monospace;
	--radius: 12px;
	--radius-sm: 8px;
	--container: 1100px;
	--ease-std: cubic-bezier(0.4, 0, 0.2, 1);
}

:root, [data-theme="dark"] {
	--bg: #0C1219;
	--bg-elev: #111A24;
	--bg-elev-2: #17222E;
	--text: #EDF2F7;
	--text-2: #C3CDD9;
	--text-3: #8B98A5;
	--line: rgba(195, 205, 217, 0.14);
	--accent-ui: #7FA3F0;
	--ok: #6FCF97;
	--warn: #F2C94C;
	--bad: #EB7A7A;
}

[data-theme="light"] {
	--bg: #F6F8FB;
	--bg-elev: #FFFFFF;
	--bg-elev-2: #EEF2F8;
	--text: #1A232C;
	--text-2: #46535F;
	--text-3: #66737F;
	--line: rgba(56, 69, 79, 0.16);
	--accent-ui: #5E73BB;
	--ok: #1E7B45;
	--warn: #8A6D00;
	--bad: #B23A3A;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
	font-family: var(--font-body);
	font-size: 16px;
	line-height: 1.55;
	color: var(--text);
	background: var(--bg);
	min-height: 100vh;
	display: flex;
	flex-direction: column;
}
h1, h2, h3 { font-family: var(--font-display); letter-spacing: -0.01em; }
h1 { font-size: 1.7rem; margin-bottom: 4px; }
h2 { font-size: 1.15rem; margin-bottom: 12px; }
a { color: var(--accent-ui); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--accent-ui); outline-offset: 2px; }
code, .mono { font-family: var(--font-mono); font-size: 0.9em; }

/* ------------------------------------------------------------ header */
.p-header {
	background: var(--bg-elev);
	border-bottom: 1px solid var(--line);
	position: sticky; top: 0; z-index: 20;
}
.p-header-inner {
	max-width: var(--container);
	margin: 0 auto;
	padding: 0 20px;
	min-height: 64px;
	display: flex; align-items: center; flex-wrap: wrap; gap: 24px;
}
.p-brand { display: flex; flex-direction: column; line-height: 1.15; text-decoration: none; flex-shrink: 0; }
.p-brand:hover { text-decoration: none; }
.p-brand-name { font-family: var(--font-display); font-weight: 700; color: var(--text); font-size: 1rem; }
.p-brand-sub { font-family: var(--font-mono); font-size: 0.68rem; color: var(--accent-ui); text-transform: uppercase; letter-spacing: 0.12em; }

.p-menu { order: 1; flex: 1; display: flex; align-items: center; gap: 24px; }
.p-nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
.p-nav a {
	color: var(--text-2); padding: 7px 12px; border-radius: var(--radius-sm);
	font-size: 0.95rem; text-decoration: none;
}
.p-nav a:hover { color: var(--text); background: var(--bg-elev-2); }
.p-nav a.active { color: var(--text); background: var(--bg-elev-2); font-weight: 600; }

.p-account { display: flex; align-items: center; gap: 14px; flex-shrink: 0; }
.p-user { color: var(--text-3); font-size: 0.9rem; white-space: nowrap; }
.p-logout { font-size: 0.9rem; white-space: nowrap; }

#theme-toggle {
	order: 2; margin-left: auto; flex-shrink: 0;
	background: none; border: 1px solid var(--line); border-radius: 50%;
	width: 34px; height: 34px; padding: 0; cursor: pointer;
	display: inline-flex; align-items: center; justify-content: center;
}
#theme-toggle:hover { border-color: var(--accent-ui); }
#theme-toggle svg { width: 16px; height: 16px; stroke: var(--text); fill: none; stroke-width: 1.5; }
[data-theme="dark"] .icon-moon { display: none; }
[data-theme="light"] .icon-sun { display: none; }

/* hamburger — shown on narrow screens only */
.p-nav-toggle {
	order: 3; flex-shrink: 0; display: none;
	background: none; border: 1px solid var(--line); border-radius: var(--radius-sm);
	width: 38px; height: 38px; padding: 0; cursor: pointer;
	align-items: center; justify-content: center;
}
.p-nav-toggle:hover { border-color: var(--accent-ui); }
.p-nav-toggle svg { width: 20px; height: 20px; stroke: var(--text); fill: none; stroke-width: 1.75; }
.p-nav-toggle .icon-close { display: none; }
.p-nav-toggle[aria-expanded="true"] .icon-menu { display: none; }
.p-nav-toggle[aria-expanded="true"] .icon-close { display: block; }

@media (max-width: 760px) {
	.p-header-inner { gap: 12px; position: relative; }
	.p-nav-toggle { display: inline-flex; }
	/* Menu drops out of the header's flex flow as an overlay so the top
	   bar keeps its full 64px height (brand no longer squishes when open). */
	.p-menu {
		display: none;
		position: absolute; top: 100%; left: 0; right: 0;
		flex-direction: column; align-items: stretch; gap: 0;
		background: var(--bg-elev); border-bottom: 1px solid var(--line);
		padding: 8px 20px 14px;
		box-shadow: 0 10px 24px rgba(12, 18, 25, 0.28);
	}
	.p-menu.open { display: flex; }
	/* scoped to the header menu so page-level .p-nav tab bars stay horizontal */
	.p-menu .p-nav { flex-direction: column; gap: 2px; }
	.p-menu .p-nav a { padding: 11px 12px; font-size: 1rem; }
	.p-account {
		justify-content: space-between;
		margin-top: 8px; padding-top: 12px; border-top: 1px solid var(--line);
	}
	.p-user { font-size: 0.95rem; }
	.p-logout { font-size: 0.95rem; }
}

/* -------------------------------------------------------------- main */
.p-main {
	width: 100%;
	max-width: var(--container);
	margin: 0 auto;
	padding: 28px 20px 48px;
	flex: 1;
}
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head p { color: var(--text-3); font-size: 0.95rem; }
.page-actions { display: flex; gap: 10px; flex-wrap: wrap; }

.card {
	background: var(--bg-elev);
	border: 1px solid var(--line);
	border-radius: var(--radius);
	padding: 20px;
	margin-bottom: 20px;
}
/* heading that starts a new section inside a card */
.card-section { margin-top: 20px; padding-top: 18px; border-top: 1px solid var(--line); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
@media (max-width: 860px) { .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; } }
/* let grid cells shrink below their content width so wide tables scroll
   inside .table-wrap instead of stretching the whole page sideways */
.grid-2 > *, .grid-3 > *, .grid-4 > * { min-width: 0; }

.stat { background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.stat .stat-label { font-family: var(--font-mono); font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-3); }
.stat .stat-value { font-family: var(--font-display); font-size: 1.5rem; font-weight: 700; margin-top: 2px; }
.stat .stat-sub { color: var(--text-3); font-size: 0.85rem; }
.stat-bad .stat-value { color: var(--bad); }
.stat-ok .stat-value { color: var(--ok); }

/* ------------------------------------------------------------- table */
.table-wrap { overflow-x: auto; container-type: inline-size; }
table.list { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
table.list th {
	text-align: left; font-family: var(--font-mono); font-size: 0.7rem;
	text-transform: uppercase; letter-spacing: 0.1em; color: var(--text-3);
	padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap;
}
table.list td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.list tr:last-child td { border-bottom: none; }
table.list td.num, table.list th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.muted { color: var(--text-3); }
.small { font-size: 0.85rem; }
.empty { color: var(--text-3); padding: 18px 4px; }

/* Stacked table: add class "stack-sm" to table.list and data-th="Column
   label" to each cell. Rows become cards, cells become label/value lines,
   and cells with class "actions" become a button row.
   "stack-sub" shows only when stacked; "stack-hide" hides when stacked.
   Stacks when the table's .table-wrap container is too narrow for columns
   (container query — covers half-width cards on desktop AND phones), with
   a viewport fallback for browsers without container query support.
   NOTE: the @container and @media blocks below must be kept identical. */
table.stack-sm .stack-sub { display: none; }
@container (max-width: 640px) {
	table.stack-sm .stack-sub { display: block; }
	table.stack-sm .stack-hide { display: none; }
	table.stack-sm thead { display: none; }
	table.stack-sm, table.stack-sm tbody, table.stack-sm tr { display: block; width: 100%; }
	table.stack-sm tr { padding: 12px 2px; border-bottom: 1px solid var(--line); }
	table.stack-sm tr:last-child { border-bottom: none; }
	table.stack-sm td {
		display: flex; align-items: baseline; flex-wrap: wrap; gap: 8px;
		padding: 3px 0; border-bottom: none; text-align: left; white-space: normal;
	}
	table.stack-sm td[data-th]::before {
		content: attr(data-th); margin-right: auto; flex-shrink: 0;
		font-family: var(--font-mono); font-size: 0.68rem; text-transform: uppercase;
		letter-spacing: 0.1em; color: var(--text-3);
	}
	table.stack-sm td.stack-title { display: block; font-size: 1.02rem; }
	table.stack-sm td[data-th] .small { flex-basis: 100%; text-align: right; }
	table.stack-sm td.actions { justify-content: flex-start; padding-top: 10px; }
	table.stack-sm td.actions .inline-form { display: inline-block; }
}
@media (max-width: 760px) {
	table.stack-sm .stack-sub { display: block; }
	table.stack-sm .stack-hide { display: none; }
	table.stack-sm thead { display: none; }
	table.stack-sm, table.stack-sm tbody, table.stack-sm tr { display: block; width: 100%; }
	table.stack-sm tr { padding: 12px 2px; border-bottom: 1px solid var(--line); }
	table.stack-sm tr:last-child { border-bottom: none; }
	table.stack-sm td {
		display: flex; align-items: baseline; flex-wrap: wrap; gap: 8px;
		padding: 3px 0; border-bottom: none; text-align: left; white-space: normal;
	}
	table.stack-sm td[data-th]::before {
		content: attr(data-th); margin-right: auto; flex-shrink: 0;
		font-family: var(--font-mono); font-size: 0.68rem; text-transform: uppercase;
		letter-spacing: 0.1em; color: var(--text-3);
	}
	table.stack-sm td.stack-title { display: block; font-size: 1.02rem; }
	table.stack-sm td[data-th] .small { flex-basis: 100%; text-align: right; }
	table.stack-sm td.actions { justify-content: flex-start; padding-top: 10px; }
	table.stack-sm td.actions .inline-form { display: inline-block; }
}

/* ------------------------------------------------------------- badges */
.badge {
	display: inline-block; padding: 2px 10px; border-radius: 99px;
	font-family: var(--font-mono); font-size: 0.7rem; text-transform: uppercase;
	letter-spacing: 0.08em; border: 1px solid var(--line); color: var(--text-2);
}
.badge-paid, .badge-active, .badge-completed { color: var(--ok); border-color: currentColor; }
.badge-sent, .badge-proposed { color: var(--accent-ui); border-color: currentColor; }
.badge-overdue, .badge-cancelled { color: var(--bad); border-color: currentColor; }
.badge-draft, .badge-on_hold, .badge-void { color: var(--text-3); }

/* -------------------------------------------------------------- forms */
form .field { margin-bottom: 14px; }
label { display: block; font-size: 0.85rem; color: var(--text-2); margin-bottom: 4px; }
input[type="text"], input[type="email"], input[type="password"], input[type="date"],
input[type="number"], select, textarea {
	width: 100%; padding: 9px 12px; font: inherit; font-size: 0.95rem;
	color: var(--text); background: var(--bg); border: 1px solid var(--line);
	border-radius: var(--radius-sm);
}
input:focus, select:focus, textarea:focus { border-color: var(--accent-ui); outline: none; }
textarea { min-height: 90px; resize: vertical; }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }
.checkline { display: flex; align-items: center; gap: 8px; }
.checkline input { width: auto; }
.form-error { color: var(--bad); font-size: 0.9rem; margin-bottom: 12px; }

.btn {
	display: inline-block; font: inherit; font-size: 0.95rem; font-weight: 600;
	padding: 9px 18px; border-radius: var(--radius-sm); cursor: pointer;
	border: 1px solid transparent; text-decoration: none; transition: opacity 200ms var(--ease-std);
}
.btn:hover { text-decoration: none; opacity: 0.9; }
.btn-primary { background: var(--gradient-cta); color: #fff; }
.btn-ghost { background: none; border-color: var(--line); color: var(--text); }
.btn-ghost:hover { border-color: var(--accent-ui); color: var(--accent-ui); }
.btn-danger { background: none; border-color: var(--bad); color: var(--bad); }
.btn-sm { padding: 5px 12px; font-size: 0.85rem; }
.inline-form { display: inline; }

/* -------------------------------------------------------------- flash */
.flash {
	border-radius: var(--radius-sm); padding: 12px 16px; margin-bottom: 18px;
	border: 1px solid; font-size: 0.95rem;
}
.flash-ok { border-color: var(--ok); color: var(--ok); background: color-mix(in srgb, var(--ok) 8%, transparent); }
.flash-error { border-color: var(--bad); color: var(--bad); background: color-mix(in srgb, var(--bad) 8%, transparent); }
.flash-info { border-color: var(--accent-ui); color: var(--accent-ui); background: color-mix(in srgb, var(--accent-ui) 8%, transparent); }

/* credentials reveal box (shown once after creating/resetting a login) */
.credentials {
	font-family: var(--font-mono); font-size: 0.95rem;
	background: var(--bg); border: 1px dashed var(--accent-ui);
	border-radius: var(--radius-sm); padding: 14px 16px; margin: 10px 0;
}

/* --------------------------------------------------- two-factor setup */
/* The white plate is deliberate: the portal is dark themed and a QR code
   inverted against a dark background will not scan on many readers. */
.totp-qr {
	background: #fff; border-radius: var(--radius-sm);
	padding: 8px; margin: 14px 0; width: fit-content;
}
.totp-qr svg { display: block; max-width: 100%; height: auto; }
.totp-manual { margin: 0 0 4px; }
.totp-manual summary { cursor: pointer; color: var(--text-2); font-size: 0.85rem; }
.totp-manual[open] summary { margin-bottom: 4px; }

/* -------------------------------------------------------------- login */
.login-wrap { min-height: calc(100vh - 130px); display: flex; align-items: center; justify-content: center; }
.login-card { width: 100%; max-width: 400px; }
.login-card .p-brand-sub { display: block; margin-bottom: 18px; }

/* ------------------------------------------------------ invoice detail */
.invoice-head { display: flex; justify-content: space-between; gap: 20px; flex-wrap: wrap; margin-bottom: 18px; }
.invoice-total-row td { font-weight: 700; border-top: 2px solid var(--line); }
/* ---- line-item editor ----
   Scoped as `table.item-editor` so these win over the base `table.list td/th`
   rules (same specificity, later in source) — otherwise the description input
   inherits table.list's 10px padding and vertical-align:top. */
.item-editor { margin-top: 2px; }
table.item-editor th { padding: 0 6px 8px 0; }
table.item-editor th.col-qty, table.item-editor th.col-price { text-align: right; }
table.item-editor th:last-child, table.item-editor td:last-child { padding-right: 0; }
table.item-editor td { padding: 5px 6px 5px 0; border-bottom: none; vertical-align: middle; }
/* first cell flush-left so the description lines up with the fields above it */
table.item-editor th:first-child, table.item-editor td:first-child { padding-left: 0; }
table.item-editor input { margin: 0; }
table.item-editor .col-qty { width: 56px; }
table.item-editor .col-price { width: 116px; }
table.item-editor .col-remove { width: 32px; }
table.item-editor .col-qty input, table.item-editor .col-price input {
	text-align: right; font-variant-numeric: tabular-nums;
}
table.item-editor .col-qty input { padding-left: 8px; padding-right: 8px; }
/* strip number spinners — they crowd the narrow qty/price cells */
table.item-editor input[type="number"] { -moz-appearance: textfield; appearance: textfield; }
table.item-editor input[type="number"]::-webkit-outer-spin-button,
table.item-editor input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* $ affordance inside the unit-price field */
.price-field { position: relative; }
.price-field::before {
	content: "$"; position: absolute; left: 11px; top: 50%; transform: translateY(-50%);
	color: var(--text-3); font-size: 0.9rem; line-height: 1; pointer-events: none;
}
.price-field input { padding-left: 24px; }

/* quiet remove control — no competing box; turns danger on hover */
.item-remove {
	display: inline-flex; align-items: center; justify-content: center;
	width: 30px; height: 30px; padding: 0; border: 0; background: none;
	color: var(--text-3); font-size: 1.2rem; line-height: 1; cursor: pointer;
	border-radius: var(--radius-sm);
	transition: color 150ms var(--ease-std), background 150ms var(--ease-std);
}
.item-remove:hover, .item-remove:focus-visible {
	color: var(--bad); background: color-mix(in srgb, var(--bad) 12%, transparent); outline: none;
}

/* full-width dashed "add line" affordance */
.item-add {
	display: flex; width: 100%; justify-content: center; gap: 6px;
	margin-top: 10px; border-style: dashed; color: var(--text-2);
}
.item-add:hover {
	border-color: var(--accent-ui); color: var(--accent-ui);
	background: color-mix(in srgb, var(--accent-ui) 6%, transparent);
}

/* totals summary — stacked and right-aligned, Total emphasized */
.totals-preview {
	margin: 16px 0 0 auto; max-width: 260px; padding-top: 12px;
	border-top: 1px solid var(--line); font-variant-numeric: tabular-nums;
}
.totals-preview .t-row { display: flex; align-items: baseline; justify-content: space-between; gap: 20px; padding: 3px 0; }
.totals-preview .t-label {
	font-family: var(--font-mono); font-size: 0.68rem; text-transform: uppercase;
	letter-spacing: 0.1em; color: var(--text-3);
}
.totals-preview .t-val { color: var(--text-2); font-size: 0.92rem; }
.totals-preview .t-total { margin-top: 4px; padding-top: 8px; border-top: 1px solid var(--line); }
.totals-preview .t-total .t-label { color: var(--text-2); }
.totals-preview .t-total .t-val { color: var(--text); font-family: var(--font-display); font-weight: 700; font-size: 1.15rem; }

/* --------------------------------------------------------------- modal */
dialog.modal {
	margin: auto;
	width: min(540px, calc(100vw - 40px));
	background: var(--bg-elev);
	color: var(--text);
	border: 1px solid var(--line);
	border-radius: 16px;
	padding: 0;
	box-shadow: 0 24px 64px rgba(12, 18, 25, 0.45), 0 2px 8px rgba(12, 18, 25, 0.25);
}
dialog.modal::backdrop {
	background: rgba(12, 18, 25, 0.55);
	backdrop-filter: blur(7px);
	-webkit-backdrop-filter: blur(7px);
}
dialog.modal[open] { animation: modal-in 220ms var(--ease-std); }
dialog.modal[open]::backdrop { animation: fade-in 220ms var(--ease-std); }
@keyframes modal-in {
	from { opacity: 0; transform: translateY(10px) scale(0.97); }
	to   { opacity: 1; transform: none; }
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
	dialog.modal[open], dialog.modal[open]::backdrop { animation: none; }
}

.modal-head { display: flex; align-items: flex-start; gap: 14px; padding: 24px 24px 0; }
.modal-icon {
	flex-shrink: 0; width: 42px; height: 42px; border-radius: 50%; margin-top: 2px;
	display: inline-flex; align-items: center; justify-content: center;
	color: var(--bad);
	background: color-mix(in srgb, var(--bad) 12%, transparent);
	border: 1px solid color-mix(in srgb, var(--bad) 35%, transparent);
}
.modal-icon svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 1.75; }
.modal-head h2 { margin: 3px 0 4px; font-size: 1.2rem; }
.modal-head p { color: var(--text-2); font-size: 0.92rem; line-height: 1.5; }

.modal-body { padding: 18px 24px 0; }
.impact { border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.impact-group { padding: 12px 16px; }
.impact-group + .impact-group { border-top: 1px solid var(--line); }
.impact-title {
	display: flex; align-items: center; justify-content: space-between; gap: 12px;
	font-family: var(--font-display); font-weight: 600; font-size: 0.92rem;
}
.impact-verb { color: var(--bad); font-weight: 500; font-size: 0.85rem; white-space: nowrap; }
.impact-count {
	flex-shrink: 0; min-width: 26px; text-align: center;
	font-family: var(--font-mono); font-size: 0.72rem; padding: 2px 8px;
	border-radius: 999px; border: 1px solid currentColor; color: var(--bad);
}
.impact-none .impact-count, .impact-none .impact-verb { color: var(--text-3); }
.impact-items { margin: 8px 0 0; padding: 0; list-style: none; }
.impact-items li {
	padding: 4px 0 4px 14px; position: relative;
	color: var(--text-2); font-size: 0.88rem;
}
.impact-items li::before {
	content: ""; position: absolute; left: 0; top: 0.85em;
	width: 5px; height: 5px; border-radius: 50%;
	background: color-mix(in srgb, var(--bad) 65%, transparent);
}
.impact-empty { margin-top: 4px; color: var(--text-3); font-size: 0.85rem; }

.modal-note { margin: 14px 2px 0; color: var(--text-3); font-size: 0.85rem; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; padding: 20px 24px 24px; }
.modal-foot .btn-danger:hover {
	opacity: 1; color: #fff; background: var(--bad); border-color: var(--bad);
}

/* -------------------------------------------------------------- footer */
.p-footer {
	border-top: 1px solid var(--line);
	padding: 18px 20px; display: flex; justify-content: space-between; gap: 12px;
	max-width: var(--container); margin: 0 auto; width: 100%;
	color: var(--text-3); font-size: 0.85rem;
}
.p-footer span:last-child { font-family: var(--font-mono); }

@media print {
	.p-header, .p-footer, .page-actions, .no-print, #theme-toggle, .flash { display: none !important; }
	body { background: #fff; color: #000; }
	.card { border: none; padding: 0; }
	.p-main { padding: 0; }
}
