/* Follows the josiah.tools design system (master.css + round-robin) */

:root {
	--bg: #e8e8e8;
	--surface: #fff;
	--ink: #000;
	--muted: #555;
	--line: #000;
	--yellow: #ffeaa7;
	--green: #55efc4;
	--green-strong: #00b894;
	--red: #fab1a0;
	--blue: #81ecec;
	--btn: #eee;
	--btn-hover: #ddd;
	--btn-active: #ccc;
	--hover: #0000000d;
	--radius-section: 15px;
	--radius-control: 5px;
	--border: solid 2px var(--line);
}

* {
	box-sizing: border-box;
	font-family: "Poppins", sans-serif;
}
::selection {
	background: #000;
	color: #fff;
}

html {
	color-scheme: light;
}
body {
	background: var(--bg);
	color: var(--ink);
	margin: 0;
	-webkit-font-smoothing: antialiased;
}

a {
	color: inherit;
}

:focus-visible {
	outline: solid 3px var(--green-strong);
	outline-offset: 2px;
}

.main {
	margin: 50px auto;
	width: 1100px;
	max-width: 100%;
	padding: 0 20px;
}
.main.narrow {
	width: 640px;
}

h1 {
	margin: 0;
	font-size: 32px;
	font-weight: 700;
	line-height: 1.2;
}
h3.subtitle {
	margin: 4px 0 0;
	font-weight: 400;
	font-size: 16px;
	color: var(--muted);
}

.page-head {
	display: flex;
	align-items: flex-end;
	justify-content: space-between;
	gap: 16px;
	flex-wrap: wrap;
	margin-bottom: 30px;
}
.page-head .actions {
	display: flex;
	gap: 8px;
	flex-wrap: wrap;
}

.section {
	position: relative;
	margin-bottom: 40px;
	padding: 20px;
	border-radius: var(--radius-section);
	border: var(--border);
	background: var(--surface);
}
.section.yellow {
	background: var(--yellow);
}
.section.green {
	background: var(--green);
}
.section.red {
	background: var(--red);
}
.section.blue {
	background: var(--blue);
}

.section-title {
	user-select: none;
	display: flex;
	align-items: baseline;
	gap: 8px;
	margin: 0 0 7px;
}
.section-title h2 {
	display: inline;
	font-size: 21px;
	font-weight: 500;
	margin: 0;
}
.section-title .title-sub {
	font-size: 14px;
	font-weight: 400;
}
.section-title .title-action {
	margin-left: auto;
}

/* Buttons */
button,
.btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	gap: 6px;
	padding: 6px 12px;
	font-size: 15px;
	line-height: 1.5;
	color: var(--ink);
	border: var(--border);
	border-radius: var(--radius-control);
	background: var(--btn);
	cursor: pointer;
	text-decoration: none;
	white-space: nowrap;
}
button:hover,
.btn:hover {
	background: var(--btn-hover);
}
button:active,
.btn:active {
	background: var(--btn-active);
}
button[disabled] {
	opacity: 0.7;
	cursor: not-allowed;
	background: var(--btn);
}
.btn.primary {
	background: var(--green);
	font-weight: 500;
}
.btn.primary:hover {
	background: #3ee0b4;
}
.btn.primary:active {
	background: #2fcfa3;
}
.btn.danger:hover {
	background: var(--red);
}
.btn.ghost {
	background: none;
	border-color: transparent;
}
.btn.ghost:hover {
	background: var(--hover);
}
.btn svg {
	width: 16px;
	height: 16px;
	flex-shrink: 0;
}

/* Fields: label and input share one bordered block, like round-robin's .input-block */
.fields {
	display: flex;
	flex-wrap: wrap;
	justify-content: space-between;
	gap: 10px 2%;
}
.input-block {
	width: 49%;
	display: flex;
	flex-wrap: nowrap;
	border: var(--border);
	border-radius: var(--radius-control);
	background: #fff;
	overflow: hidden;
}
.input-block.full {
	width: 100%;
}
.input-block .input-label {
	flex: 0 0 40%;
	display: flex;
	align-items: center;
	padding: 6px 8px;
	font-size: 15px;
	border-right: var(--border);
	background: #fff;
	user-select: none;
}
.input-block.full .input-label {
	flex-basis: 20%;
}
.input-block input,
.input-block select {
	flex: 1 1 auto;
	min-width: 0;
	width: 100%;
	font-size: 15px;
	padding: 6px 8px;
	border: none;
	outline: none;
	background: #fff;
	color: var(--ink);
}
.input-block:focus-within {
	outline: solid 3px var(--green-strong);
	outline-offset: 1px;
}
.input-block.stacked {
	flex-direction: column;
}
.input-block.stacked .input-label {
	flex: none;
	border-right: none;
	border-bottom: var(--border);
}
.input-block textarea {
	width: 100%;
	font-size: 15px;
	padding: 8px;
	border: none;
	outline: none;
	resize: vertical;
	min-height: 64px;
	color: var(--ink);
	line-height: 1.5;
}
input[type="date"] {
	min-height: 36px;
}

.hint {
	font-size: 13px;
	color: var(--muted);
	margin: 6px 0 0;
}

/* Status pills */
.status {
	display: inline-block;
	padding: 1px 10px;
	border: var(--border);
	border-radius: 50px;
	font-size: 13px;
	font-weight: 500;
	background: #fff;
}
.status.paid {
	background: var(--green);
}
.status.unpaid {
	background: var(--yellow);
}
.status.overdue {
	background: var(--red);
}

/* Toast */
.toast {
	position: fixed;
	left: 50%;
	bottom: 24px;
	transform: translate(-50%, 150%);
	padding: 10px 16px;
	background: #000;
	color: #fff;
	border-radius: var(--radius-control);
	font-size: 15px;
	transition: transform 0.2s;
	z-index: 100;
	pointer-events: none;
}
.toast.show {
	transform: translate(-50%, 0);
}

/* Dialog */
dialog {
	border: var(--border);
	border-radius: var(--radius-section);
	padding: 20px;
	width: 520px;
	max-width: calc(100% - 32px);
	background: var(--surface);
}
dialog::backdrop {
	background: #0006;
}
dialog h2 {
	margin: 0 0 14px;
	font-size: 21px;
	font-weight: 500;
}
dialog .fields {
	margin-bottom: 18px;
}
dialog .dialog-actions {
	display: flex;
	justify-content: flex-end;
	gap: 8px;
}

/* ---------- Login ---------- */
.login-wrap {
	min-height: 100vh;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 20px;
}
.login {
	width: 400px;
	max-width: 100%;
	margin: 0;
}
.login h1 {
	margin-bottom: 18px;
}
.login .input-block {
	width: 100%;
	margin-bottom: 10px;
}
.login button {
	width: 100%;
	margin-top: 6px;
}
.form-error {
	min-height: 1.5em;
	margin: 8px 0 0;
	font-size: 14px;
	color: #b3261e;
}

/* ---------- Invoice list ---------- */
.toolbar {
	display: flex;
	gap: 10px;
	flex-wrap: wrap;
	align-items: center;
	margin-bottom: 14px;
}
.filters {
	display: inline-flex;
	border: var(--border);
	border-radius: var(--radius-control);
	overflow: hidden;
}
.filters button {
	border: none;
	border-radius: 0;
	border-right: var(--border);
	background: #fff;
}
.filters button:last-child {
	border-right: none;
}
.filters button:hover {
	background: var(--btn-hover);
}
.filters button[aria-pressed="true"] {
	background: #000;
	color: #fff;
}
.search {
	flex: 1 1 220px;
	max-width: 360px;
	font-size: 15px;
	padding: 6px 10px;
	border: var(--border);
	border-radius: var(--radius-control);
	outline: none;
}
.search:focus-visible {
	outline: solid 3px var(--green-strong);
	outline-offset: 1px;
}

.invoice-list {
	padding: 0;
	overflow: hidden;
}
.invoice-row {
	display: grid;
	grid-template-columns: 80px minmax(0, 1fr) 130px 130px 100px;
	gap: 16px;
	align-items: center;
	padding: 14px 20px;
	text-decoration: none;
	border-bottom: var(--border);
}
.invoice-row:last-child {
	border-bottom: none;
}
.invoice-row:hover {
	background: #f5f5f5;
}
.invoice-row .number {
	font-weight: 600;
	font-variant-numeric: tabular-nums;
}
.invoice-row .who {
	min-width: 0;
}
.invoice-row .who strong,
.invoice-row .who span {
	display: block;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}
.invoice-row .who strong {
	font-weight: 500;
}
.invoice-row .who span,
.invoice-row .date {
	font-size: 14px;
	color: var(--muted);
}
.invoice-row .amount {
	text-align: right;
	font-weight: 500;
	font-variant-numeric: tabular-nums;
}
.invoice-row .status-cell {
	text-align: right;
}
.list-empty {
	padding: 40px 20px;
	text-align: center;
}
.list-empty p {
	margin: 0 0 14px;
}

/* ---------- Editor ---------- */
.topbar {
	position: sticky;
	top: 0;
	z-index: 10;
	background: var(--bg);
	border-bottom: var(--border);
}
.topbar-inner {
	width: 1400px;
	max-width: 100%;
	margin: 0 auto;
	padding: 12px 20px;
	display: flex;
	align-items: center;
	gap: 10px;
	flex-wrap: wrap;
}
.topbar h1 {
	font-size: 22px;
	font-weight: 600;
}
.save-status {
	font-size: 14px;
	color: var(--muted);
	margin-right: auto;
}
.save-status.error {
	color: #b3261e;
}
.topbar .actions {
	display: flex;
	gap: 8px;
	flex-wrap: wrap;
}

.editor {
	width: 1400px;
	max-width: 100%;
	margin: 30px auto 50px;
	padding: 0 20px;
	display: grid;
	grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
	gap: 30px;
	align-items: start;
}
.editor .section {
	margin-bottom: 30px;
}

.preview {
	position: sticky;
	top: 90px;
	padding: 12px;
}
.preview .section-title {
	padding: 0 6px;
}
.preview-frame {
	position: relative;
	aspect-ratio: 8.5 / 11;
	max-height: calc(100vh - 170px);
	margin: 0 auto;
	border: var(--border);
	border-radius: var(--radius-control);
	background: #fff;
	overflow: hidden;
}
.preview-frame iframe {
	position: absolute;
	inset: 0;
	width: 100%;
	height: 100%;
	border: none;
}
.preview-frame iframe.loading {
	visibility: hidden;
}
/* Spinner while the PDF is being regenerated. The old PDF stays visible under a veil. */
.preview-frame::before,
.preview-frame::after {
	content: "";
	position: absolute;
	z-index: 2;
	opacity: 0;
	pointer-events: none;
	transition: opacity 0.15s;
}
.preview-frame::before {
	inset: 0;
	background: #ffffffa6;
}
.preview-frame::after {
	top: 50%;
	left: 50%;
	width: 40px;
	height: 40px;
	margin: -20px 0 0 -20px;
	border: solid 4px var(--line);
	border-top-color: var(--blue);
	border-radius: 50%;
	animation: spin 0.7s linear infinite;
}
.preview-frame.busy::before,
.preview-frame.busy::after {
	opacity: 1;
}
@keyframes spin {
	to {
		transform: rotate(360deg);
	}
}

/* Line items */
.items-head,
.item {
	display: grid;
	grid-template-columns: 22px minmax(0, 1fr) 70px 100px 100px 26px;
	gap: 8px;
	align-items: start;
}
.items-head {
	font-size: 13px;
	color: var(--muted);
	padding: 0 0 4px;
}
.items-head .num,
.item .amount {
	text-align: right;
}
.item {
	padding: 6px 0;
	border-radius: var(--radius-control);
}
.item.dragging {
	opacity: 0.4;
}
.item.drop-before {
	box-shadow: 0 -3px 0 var(--green-strong);
}
.item.drop-after {
	box-shadow: 0 3px 0 var(--green-strong);
}
.item textarea,
.item input {
	width: 100%;
	font-size: 15px;
	padding: 5px 8px;
	border: var(--border);
	border-radius: var(--radius-control);
	outline: none;
	background: #fff;
	color: var(--ink);
	line-height: 1.5;
}
.item textarea {
	resize: none;
	overflow: hidden;
	min-height: 38px;
	display: block;
}
.item input {
	text-align: right;
	font-variant-numeric: tabular-nums;
}
.item textarea:focus-visible,
.item input:focus-visible {
	outline: solid 3px var(--green-strong);
	outline-offset: 1px;
}
.item .amount {
	padding-top: 8px;
	font-variant-numeric: tabular-nums;
	font-weight: 500;
}
.drag-handle,
.remove-item {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 38px;
	color: var(--muted);
}
.drag-handle {
	cursor: grab;
}
.remove-item {
	padding: 0;
	border: none;
	background: none;
}
.remove-item:hover {
	background: none;
	color: var(--ink);
}
.remove-item svg {
	height: 17px;
	width: 17px;
	transition: transform 0.1s;
}
.remove-item:hover svg {
	transform: rotate(-90deg);
}
.add-item {
	margin-top: 8px;
}

.totals {
	margin-top: 16px;
	padding-top: 14px;
	border-top: var(--border);
	display: grid;
	grid-template-columns: 1fr auto;
	gap: 10px 20px;
	align-items: center;
}
.totals .input-block {
	width: 260px;
	max-width: 100%;
}
.totals dl {
	margin: 0;
	display: grid;
	grid-template-columns: auto auto;
	gap: 2px 20px;
	text-align: right;
	font-variant-numeric: tabular-nums;
}
.totals dt {
	color: var(--muted);
}
.totals dd {
	margin: 0;
	font-weight: 500;
}
.totals dt.balance,
.totals dd.balance {
	color: var(--ink);
	font-weight: 600;
	font-size: 18px;
}

.payment-summary p {
	margin: 0 0 12px;
}
.payment-summary .notes {
	white-space: pre-wrap;
	padding: 10px 12px;
	background: #fff8;
	border: var(--border);
	border-radius: var(--radius-control);
}
.payment-summary .actions {
	display: flex;
	gap: 8px;
	flex-wrap: wrap;
}

.mobile-preview {
	display: none;
}

/* ---------- Settings ---------- */
.settings .section {
	margin-bottom: 30px;
}
.color-field input[type="color"] {
	padding: 2px 4px;
	height: 38px;
	cursor: pointer;
}

/* ---------- Responsive ---------- */
@media only screen and (max-width: 940px) {
	.main {
		width: 640px;
	}
	.editor {
		grid-template-columns: minmax(0, 1fr);
		width: 700px;
	}
	.preview {
		display: none;
	}
	.mobile-preview {
		display: inline-flex;
	}
	.invoice-row {
		grid-template-columns: 60px minmax(0, 1fr) auto;
		grid-template-areas: "num who amount" "num date status";
		gap: 2px 12px;
	}
	.invoice-row .number {
		grid-area: num;
		align-self: start;
	}
	.invoice-row .who {
		grid-area: who;
	}
	.invoice-row .date {
		grid-area: date;
	}
	.invoice-row .amount {
		grid-area: amount;
	}
	.invoice-row .status-cell {
		grid-area: status;
	}
}

@media only screen and (max-width: 540px) {
	.main {
		margin: 30px auto;
		padding: 0 16px;
	}
	.editor,
	.topbar-inner {
		padding-left: 16px;
		padding-right: 16px;
	}
	.section {
		padding: 16px;
	}
	.input-block,
	.input-block.full {
		width: 100%;
	}
	.input-block .input-label,
	.input-block.full .input-label {
		flex-basis: 38%;
	}
	.items-head {
		display: none;
	}
	.item {
		grid-template-columns: 22px minmax(0, 1fr) minmax(0, 1fr) 26px;
		grid-template-areas: "handle desc desc remove" "handle qty rate remove" "handle amount amount remove";
		padding: 10px 0;
		border-bottom: dashed 2px #0003;
		border-radius: 0;
	}
	.item .drag-handle {
		grid-area: handle;
	}
	.item .desc {
		grid-area: desc;
	}
	.item .qty {
		grid-area: qty;
	}
	.item .rate {
		grid-area: rate;
	}
	.item .amount {
		grid-area: amount;
		padding-top: 0;
	}
	.item .remove-item {
		grid-area: remove;
	}
	.item input::placeholder {
		color: #888;
	}
	.totals {
		grid-template-columns: 1fr;
	}
	.totals .input-block {
		width: 100%;
	}
	.topbar h1 {
		font-size: 18px;
	}
}

/* ---------- Sidebar shell ---------- */
body.with-nav {
	display: grid;
	grid-template-columns: 220px minmax(0, 1fr);
	min-height: 100vh;
}
.sidebar {
	position: sticky;
	top: 0;
	height: 100vh;
	display: flex;
	flex-direction: column;
	padding: 44px 14px 20px;
	border-right: var(--border);
	background: var(--bg);
	z-index: 20;
}
.brand {
	font-weight: 700;
	font-size: 19px;
	text-decoration: none;
	padding: 0 10px 22px;
	letter-spacing: -0.01em;
}
.nav-links {
	list-style: none;
	margin: 0;
	padding: 0;
	display: flex;
	flex-direction: column;
	gap: 6px;
}
.nav-links a {
	display: flex;
	align-items: center;
	gap: 10px;
	padding: 7px 10px;
	border: solid 2px transparent;
	border-radius: var(--radius-control);
	text-decoration: none;
	font-weight: 500;
	white-space: nowrap;
}
.nav-links a:hover {
	background: var(--hover);
}
.nav-links a[aria-current="page"] {
	border-color: var(--line);
}
.nav-links a.nav-yellow[aria-current="page"] {
	background: var(--yellow);
}
.nav-links a.nav-green[aria-current="page"] {
	background: var(--green);
}
.nav-links a.nav-red[aria-current="page"] {
	background: var(--red);
}
.nav-links a.nav-blue[aria-current="page"] {
	background: var(--blue);
}
.nav-links svg {
	width: 20px;
	height: 20px;
	flex-shrink: 0;
	fill: none;
	stroke: currentColor;
	stroke-width: 2;
	stroke-linecap: round;
	stroke-linejoin: round;
}
.nav-logout {
	margin-top: auto;
	justify-content: flex-start;
	padding-left: 10px;
	font-weight: 500;
}

/* ---------- Income and expenses ---------- */
.toolbar .year-select {
	width: auto;
}
.toolbar .year-select .input-label {
	flex: none;
}
.toolbar .year-select select {
	width: auto;
	padding-right: 4px;
}
.toolbar .save-status {
	margin: 0 0 0 auto;
}

.ledger-table {
	padding: 0;
	overflow: hidden;
}
.ledger-row {
	display: grid;
	grid-template-columns: 158px minmax(0, 1.4fr) minmax(0, 1fr) 140px 40px;
	grid-template-areas:
		"date f0 f1 amount remove"
		". note note note .";
	align-items: start;
	border-bottom: var(--border);
}
.ledger-row.income {
	grid-template-columns: 150px minmax(0, 1fr) minmax(0, 2fr) minmax(0, 0.7fr) 120px 40px;
	grid-template-areas:
		"date f0 f1 f2 amount remove"
		". note note note note .";
}
.ledger-head.income {
	grid-template-areas: "date f0 f1 f2 amount remove";
}
.ledger-row:last-child {
	border-bottom: none;
}
.ledger-head {
	grid-template-areas: "date f0 f1 amount remove";
	font-size: 14px;
	font-weight: 500;
	padding: 8px 0;
}
.ledger-head > span {
	padding: 0 12px;
}
.ledger-head .num {
	text-align: right;
}
.ledger-head.expenses {
	background: var(--red);
}
.ledger-head.income {
	background: var(--green);
}
.ledger-row .date {
	grid-area: date;
}
.ledger-row .field-0 {
	grid-area: f0;
}
.ledger-row .field-1 {
	grid-area: f1;
}
.ledger-row .field-2 {
	grid-area: f2;
}
.ledger-row .amount {
	grid-area: amount;
}
.ledger-row .remove-item {
	grid-area: remove;
}
.ledger-row .note-cell {
	grid-area: note;
}
.ledger-row .cell {
	display: flex;
	align-items: center;
	min-width: 0;
}
.ledger-row textarea {
	display: block;
	resize: none;
	overflow: hidden;
	line-height: 1.5;
}
.ledger-row input,
.ledger-row textarea {
	width: 100%;
	min-width: 0;
	font-size: 15px;
	padding: 10px 12px;
	border: none;
	border-radius: var(--radius-control);
	background: transparent;
	color: var(--ink);
	outline: none;
}
.ledger-row input:hover,
.ledger-row textarea:hover {
	background: var(--hover);
}
.ledger-row textarea:focus-visible,
.ledger-row input:focus-visible {
	background: #fff;
	box-shadow: inset 0 0 0 3px var(--green-strong);
}
.ledger-row .amount input {
	text-align: right;
	font-variant-numeric: tabular-nums;
	font-weight: 500;
	padding-left: 2px;
}
.ledger-row .currency {
	padding-left: 12px;
	color: var(--muted);
}
.ledger-row input.note {
	font-size: 14px;
	color: var(--muted);
	padding-top: 0;
	padding-bottom: 8px;
}
.ledger-row input.note:focus-visible {
	padding-top: 4px;
	padding-bottom: 4px;
	color: var(--ink);
}
.ledger-row textarea::placeholder,
.ledger-row input::placeholder {
	color: #999;
}
.ledger-row input[type="date"] {
	min-height: 0;
}
.ledger-row .remove-item {
	height: 42px;
}
.ledger-row .static {
	padding: 10px 12px;
	font-size: 15px;
	gap: 8px;
	flex-wrap: wrap;
}
.ledger-row .amount.static {
	justify-content: flex-end;
	font-weight: 500;
	font-variant-numeric: tabular-nums;
}
.ledger-row .note-cell.static {
	padding-top: 0;
	font-size: 14px;
	color: var(--muted);
}
.ledger-row.from-invoice {
	background: #fffbea;
}
.invoice-chip {
	display: inline-block;
	padding: 0 8px;
	border: var(--border);
	border-radius: 50px;
	background: var(--yellow);
	font-size: 13px;
	font-weight: 500;
	text-decoration: none;
	white-space: nowrap;
}
.invoice-chip:hover {
	background: #ffe083;
}
.ledger + .hint,
.ledger-table + .hint {
	margin-top: -26px;
}

@media only screen and (max-width: 940px) {
	body.with-nav {
		display: block;
	}
	.sidebar {
		height: auto;
		flex-direction: row;
		align-items: center;
		gap: 8px;
		padding: 10px 16px;
		border-right: none;
		border-bottom: var(--border);
		overflow-x: auto;
	}
	.brand {
		padding: 0 8px 0 0;
		font-size: 16px;
	}
	.nav-links {
		flex-direction: row;
		gap: 4px;
	}
	.nav-links a {
		padding: 5px 9px;
	}
	.nav-logout {
		margin: 0 0 0 auto;
	}
}

@media only screen and (max-width: 700px) {
	.brand,
	.nav-links svg {
		display: none;
	}
	.ledger-head {
		display: none;
	}
	.ledger-row,
	.ledger-row.income {
		grid-template-columns: minmax(0, 1fr) 130px 40px;
		grid-template-areas:
			"date amount remove"
			"f0 f0 f0"
			"f1 f1 f1"
			"note note note";
		padding: 6px 0;
	}
	.ledger-row.income {
		grid-template-areas:
			"date amount remove"
			"f0 f0 f0"
			"f1 f1 f1"
			"f2 f2 f2"
			"note note note";
	}
	.ledger-row input.note {
		padding-top: 10px;
	}
}

@media (prefers-reduced-motion: reduce) {
	* {
		transition: none !important;
	}
}
