/* The web app at /app. main.css is the health page's. */

:root {
	--bg: #f8fafc;
	--surface: #ffffff;
	--text: #0f172a;
	--muted: #64748b;
	--border: #e2e8f0;
	--accent: #2563eb;
	--accent-text: #ffffff;
	--error: #b91c1c;
	--alert-bg: #fef3c7;
	--alert-text: #78350f;
	color-scheme: light dark;
}

/* Dark follows the system unless light is pinned, or is pinned itself
   (#87): theme.js sets data-theme. The two dark lists must match. */
@media (prefers-color-scheme: dark) {
	:root:not([data-theme="light"]) {
		--bg: #0f172a;
		--surface: #1e293b;
		--text: #e2e8f0;
		--muted: #94a3b8;
		--border: #334155;
		--accent: #60a5fa;
		--accent-text: #0f172a;
		--error: #fca5a5;
		--alert-bg: #422006;
		--alert-text: #fde68a;
	}
}

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

:root[data-theme="dark"] {
	--bg: #0f172a;
	--surface: #1e293b;
	--text: #e2e8f0;
	--muted: #94a3b8;
	--border: #334155;
	--accent: #60a5fa;
	--accent-text: #0f172a;
	--error: #fca5a5;
	--alert-bg: #422006;
	--alert-text: #fde68a;
	color-scheme: dark;
}

*,
*::before,
*::after {
	box-sizing: border-box;
}

body.web {
	margin: 0;
	min-height: 100vh;
	display: flex;
	flex-direction: column;
	background: var(--bg);
	color: var(--text);
	font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
	font-size: 1rem;
	line-height: 1.5;
	/* Notched phones in landscape (viewport-fit=cover in app.html). */
	padding-left: env(safe-area-inset-left);
	padding-right: env(safe-area-inset-right);
}

.app-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 1rem;
	padding: 0.5rem 1rem;
	padding-top: calc(0.5rem + env(safe-area-inset-top));
	border-bottom: 1px solid var(--border);
	background: var(--surface);
}

.app-name {
	font-weight: 700;
	font-size: 1.25rem;
}

.header-actions {
	display: flex;
	align-items: center;
	gap: 0.5rem;
}

.logout-form {
	margin: 0;
}

.theme-toggle {
	padding: 0;
	font-size: 1.25rem;
	line-height: 1;
}

.button {
	min-height: 44px;
	min-width: 44px;
	padding: 0 1rem;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: var(--surface);
	color: var(--text);
	font: inherit;
	cursor: pointer;
}

a.button {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	text-decoration: none;
}

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

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

.button:disabled {
	opacity: 0.6;
	cursor: default;
}

.button:focus-visible,
.keypad button:focus-visible,
.pin-field:focus-visible {
	outline: 3px solid var(--accent);
	outline-offset: 2px;
}

.pin-alert {
	margin: 0;
	padding: 0.75rem 1rem;
	background: var(--alert-bg);
	color: var(--alert-text);
	font-weight: 600;
}

.app-footer {
	padding: 0.75rem 1rem;
	padding-bottom: calc(0.75rem + env(safe-area-inset-bottom));
	border-top: 1px solid var(--border);
	color: var(--muted);
	font-size: 0.875rem;
	text-align: center;
}

/* A button that looks like a link, tall enough to tap. */
.link-button {
	display: inline-flex;
	align-items: center;
	min-height: 44px;
	margin-left: 0.5rem;
	padding: 0 0.5rem;
	border: none;
	background: none;
	color: var(--accent);
	font: inherit;
	text-decoration: underline;
	cursor: pointer;
}

.link-button[hidden] {
	display: none;
}

.link-button:focus-visible {
	outline: 3px solid var(--accent);
	outline-offset: 2px;
}

/* The keyboard shortcuts overlay (#52). The dialog has no padding, so a
   click on it is a click on the backdrop. */
.shortcuts {
	width: min(32rem, calc(100vw - 2rem));
	max-height: calc(100vh - 2rem);
	padding: 0;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: var(--surface);
	color: var(--text);
}

.shortcuts::backdrop {
	background: rgb(15 23 42 / 0.5);
}

.shortcuts-body {
	padding: 1rem;
}

.shortcuts h2 {
	margin: 0 0 0.75rem;
	font-size: 1.25rem;
}

.shortcuts table {
	width: 100%;
	border-collapse: collapse;
}

.shortcuts td,
.shortcuts th {
	padding: 0.375rem 0.5rem 0.375rem 0;
	border-top: 1px solid var(--border);
	text-align: left;
	vertical-align: top;
}

.shortcuts th {
	padding-top: 1rem;
	border-top: none;
}

.shortcuts td:first-child {
	white-space: nowrap;
}

.shortcuts kbd {
	display: inline-block;
	padding: 0 0.375rem;
	border: 1px solid var(--border);
	border-radius: 4px;
	background: var(--bg);
	font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
	font-size: 0.875em;
}

.shortcuts form {
	margin: 1rem 0 0;
	text-align: right;
}

/* The import dialog (#117) uses the overlay's look; its form is the body. */
.shortcuts .import-form {
	margin: 0;
	text-align: left;
}

.import-form input {
	font: inherit;
	font-size: 1rem;
}

.import-form input[type="file"] {
	max-width: 100%;
}

.dialog-buttons {
	display: flex;
	justify-content: flex-end;
	gap: 0.5rem;
}

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

/* Two columns: the note list and the note, each scrolling on its own. Phones
   show one of them at a time. */
.app-main {
	flex: 1;
	display: grid;
	grid-template-columns: minmax(14rem, 20rem) minmax(0, 1fr);
	grid-template-rows: minmax(0, 1fr);
	min-width: 0;
	min-height: 0;
}

body.web:has(.app-main) {
	height: 100vh;
	height: 100dvh;
}

.note-list {
	border-right: 1px solid var(--border);
	padding: 0.5rem;
	min-width: 0;
	overflow-y: auto;
}

.note-list ul,
.backlinks ul {
	margin: 0;
	padding: 0;
	list-style: none;
}

.note-list a,
.backlinks a {
	display: flex;
	align-items: center;
	min-height: 44px;
	padding: 0.25rem 0.75rem;
	border-radius: 8px;
	color: var(--text);
	text-decoration: none;
	overflow-wrap: anywhere;
}

.note-list a:hover,
.backlinks a:hover {
	background: var(--border);
}

.note-list a[aria-current="page"] {
	background: var(--accent);
	color: var(--accent-text);
	font-weight: 600;
}

.note-list a:focus-visible,
.backlinks a:focus-visible,
.note-body a:focus-visible {
	outline: 3px solid var(--accent);
	outline-offset: 2px;
}

/* The note pane's top bar. From 700 px up only its actions show, as a row
   above the note; the back link, the title, New and the ⋯ menu are for
   phones (wide screens have New above the list). */
.note-bar {
	display: contents;
}

.note-bar .back-link,
.note-bar-title,
.note-actions .phone-only,
.more-menu {
	display: none;
}

.list-actions,
.note-actions {
	display: flex;
	flex-wrap: wrap;
	gap: 0.5rem;
	margin-bottom: 0.5rem;
}

/* Search and project filter above the list. */
.list-filter {
	display: flex;
	flex-wrap: wrap;
	gap: 0.5rem;
	margin-bottom: 0.5rem;
}

.search-field,
.project-select {
	flex: 1 1 8rem;
	min-width: 0;
	min-height: 44px;
	padding: 0 0.75rem;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: var(--surface);
	color: var(--text);
	/* 16px or more, so phones don't zoom in on focus. */
	font: inherit;
	font-size: 1rem;
}

.search-field:focus-visible,
.project-select:focus-visible {
	outline: 3px solid var(--accent);
	outline-offset: 2px;
}

.list-filter [hidden] {
	display: none;
}

.note-items > .muted {
	padding: 0 0.5rem;
}

.note-view {
	padding: 1rem;
	min-width: 0;
	overflow-y: auto;
	overflow-wrap: anywhere;
}

/* A rendered note. Wide parts scroll inside themselves, not the page. */
.note-body {
	max-width: 48rem;
}

.note-body h1 {
	margin-top: 0;
}

.note-body a {
	color: var(--accent);
}

/* A [[link]] to a note that doesn't exist. */
.note-body a.missing {
	color: var(--error);
	text-decoration-style: dashed;
}

.note-body pre,
.note-body table {
	display: block;
	max-width: 100%;
	overflow-x: auto;
}

.note-body pre {
	padding: 0.75rem;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: var(--surface);
	overflow-wrap: normal;
}

.note-body code {
	font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
	font-size: 0.9em;
}

.note-body table {
	border-collapse: collapse;
	overflow-wrap: normal;
}

.note-body th,
.note-body td {
	padding: 0.25rem 0.75rem;
	border: 1px solid var(--border);
}

.note-body img {
	max-width: 100%;
	height: auto;
}

.note-body ul:has(> li > input[type="checkbox"]) {
	padding-left: 0.5rem;
}

.note-body li:has(> input[type="checkbox"]) {
	list-style: none;
}

/* Raw HTML in a note, shown as text. */
.note-body .raw-html {
	white-space: pre-wrap;
	font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
	font-size: 0.9em;
}

.backlinks {
	max-width: 48rem;
	margin-top: 2rem;
	padding-top: 1rem;
	border-top: 1px solid var(--border);
}

.backlinks h2 {
	margin: 0 0 0.5rem;
	font-size: 1rem;
	color: var(--muted);
}

/* The note editor. */
.editor {
	display: flex;
	flex-direction: column;
	gap: 0.75rem;
	max-width: 48rem;
}

.editor [hidden] {
	display: none;
}

.editor-text {
	width: 100%;
	min-height: 50vh;
	padding: 0.75rem;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: var(--surface);
	color: var(--text);
	font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
	/* 16px or more, so phones don't zoom in on focus. */
	font-size: 1rem;
	line-height: 1.5;
	resize: vertical;
}

.editor-text:focus-visible {
	outline: 3px solid var(--accent);
	outline-offset: 2px;
}

/* The Markdown toolbar and the Write / Preview tabs above the textarea. */
.md-bar {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	justify-content: space-between;
	gap: 0.5rem;
}

.md-tabs,
.md-toolbar,
.md-group {
	display: flex;
	flex-wrap: wrap;
	gap: 0.25rem;
}

.md-toolbar {
	gap: 0.75rem;
}

.md-tab,
.md-button {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	min-height: 44px;
	min-width: 44px;
	border: 1px solid transparent;
	border-radius: 8px;
	background: none;
	color: var(--muted);
	font: inherit;
	cursor: pointer;
	touch-action: manipulation;
}

.md-tab {
	padding: 0 1rem;
}

.md-tab[aria-selected="true"] {
	border-color: var(--border);
	background: var(--surface);
	color: var(--text);
	font-weight: 600;
}

.md-button svg {
	fill: currentColor;
}

.md-button:hover,
.md-button:active {
	background: var(--border);
	color: var(--text);
}

.md-tab:focus-visible,
.md-button:focus-visible {
	outline: 3px solid var(--accent);
	outline-offset: 2px;
}

.editor-preview {
	min-height: 50vh;
	padding: 0.75rem;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: var(--surface);
	max-width: none;
}

.editor-text.draft {
	min-height: 12rem;
	color: var(--muted);
}

.editor-buttons {
	display: flex;
	flex-wrap: wrap;
	gap: 0.5rem;
}

.editor-error {
	margin: 0;
	color: var(--error);
	font-weight: 600;
}

.session-ended {
	padding: 0.75rem 1rem;
	border-radius: 8px;
	background: var(--alert-bg);
	color: var(--alert-text);
	font-weight: 600;
}

.session-ended a {
	display: inline-flex;
	align-items: center;
	min-height: 44px;
	color: inherit;
}

/* The rename form and the delete confirmation. */
.note-form {
	display: flex;
	flex-direction: column;
	gap: 0.75rem;
	max-width: 32rem;
}

.note-form h1 {
	margin: 0;
	font-size: 1.5rem;
}

.note-form [hidden] {
	display: none;
}

.text-field {
	width: 100%;
	min-height: 44px;
	padding: 0 0.75rem;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: var(--surface);
	color: var(--text);
	font: inherit;
	font-size: 1rem;
}

.text-field:focus-visible {
	outline: 3px solid var(--accent);
	outline-offset: 2px;
}

.delete-question {
	margin: 0;
	font-weight: 600;
	overflow-wrap: anywhere;
}

/* The share pane's QR code (#83). It stays black on white in dark mode,
   which is what phone cameras read best. */
.share .qr {
	width: 100%;
	max-width: 16rem;
	height: auto;
	border-radius: 8px;
}

.share form {
	margin: 0;
}

/* The note view's revision line (#110). */
.note-meta {
	margin: 0 0 0.75rem;
	font-size: 0.875rem;
}

/* A note's history pane (#110): the versions, newest first, the banner of
   the one on show, and its text. */
.history {
	max-width: 48rem;
}

.history h1 {
	margin: 0 0 0.5rem;
	font-size: 1.5rem;
}

.history-list {
	margin: 0 0 1rem;
	padding: 0;
	list-style: none;
	max-height: 40vh;
	overflow-y: auto;
	border: 1px solid var(--border);
	border-radius: 8px;
}

.history-list li + li {
	border-top: 1px solid var(--border);
}

.history-list a {
	display: flex;
	flex-wrap: wrap;
	align-items: center;
	gap: 0.25rem 0.75rem;
	min-height: 44px;
	padding: 0.25rem 0.75rem;
	color: var(--text);
	text-decoration: none;
}

.history-list a:hover {
	background: var(--border);
}

.history-list a[aria-current="true"] {
	background: var(--accent);
	color: var(--accent-text);
	font-weight: 600;
}

.history-list a:focus-visible {
	outline: 3px solid var(--accent);
	outline-offset: -3px;
}

.rev-badge {
	min-width: 2.5rem;
	font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.reason-badge,
.current-badge {
	padding: 0 0.5rem;
	border: 1px solid currentColor;
	border-radius: 999px;
	font-size: 0.8125rem;
}

.current-badge {
	font-weight: 600;
}

.history-banner {
	display: flex;
	flex-direction: column;
	gap: 0.5rem;
	margin-bottom: 1rem;
	padding: 0.75rem 1rem;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: var(--surface);
}

.history-banner p {
	margin: 0;
	font-weight: 600;
}

.history-banner [hidden] {
	display: none;
}

.history-banner form {
	margin: 0;
}

/* A shared note's public page: just the note. */
.shared {
	flex: 1;
	width: 100%;
	max-width: 48rem;
	margin: 0 auto;
	padding: 1.5rem 1rem;
	overflow-wrap: anywhere;
}

.draft-label {
	color: var(--muted);
	font-weight: 600;
}

/* Phones (#65): one pane at a time, the note pane with a top bar, and the
   editor's actions in a bar stuck to the bottom of the screen. */
@media (width < 700px) {
	body.web:has(.app-main) {
		height: auto;
	}

	.app-main {
		grid-template-columns: minmax(0, 1fr);
		grid-template-rows: none;
	}

	.app-main.has-note .note-list,
	.app-main:not(.has-note) .note-view {
		display: none;
	}

	.note-list {
		padding: 0.5rem 1rem;
		border-right: none;
		overflow-y: visible;
	}

	.note-list ul,
	.backlinks ul {
		display: flex;
		flex-direction: column;
		gap: 0.5rem;
	}

	.note-view {
		padding: 0 1rem 1rem;
		overflow-y: visible;
	}

	.note-bar {
		position: sticky;
		top: 0;
		z-index: 1;
		display: flex;
		align-items: center;
		gap: 0.5rem;
		margin: 0 -1rem 1rem;
		padding: 0.5rem 1rem;
		border-bottom: 1px solid var(--border);
		background: var(--bg);
	}

	.note-bar .back-link {
		display: inline-flex;
		flex: none;
		padding: 0 0.75rem;
	}

	.note-bar-title {
		display: block;
		flex: 1;
		min-width: 0;
		overflow: hidden;
		font-weight: 600;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.note-bar .note-actions {
		flex: none;
		flex-wrap: nowrap;
		margin: 0;
	}

	.note-actions .wide-only {
		display: none;
	}

	.note-actions .phone-only {
		display: inline-flex;
	}

	.more-menu {
		display: block;
		position: relative;
	}

	.more-menu summary {
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0;
		font-size: 1.25rem;
		list-style: none;
	}

	.more-menu summary::-webkit-details-marker {
		display: none;
	}

	.more-menu summary:focus-visible {
		outline: 3px solid var(--accent);
		outline-offset: 2px;
	}

	.more-items {
		position: absolute;
		top: calc(100% + 0.5rem);
		right: 0;
		display: flex;
		flex-direction: column;
		gap: 0.5rem;
		min-width: 10rem;
		padding: 0.5rem;
		border: 1px solid var(--border);
		border-radius: 8px;
		background: var(--surface);
		box-shadow: 0 4px 16px rgb(15 23 42 / 0.2);
	}

	/* [[links]] and #tags: taller to tap, without spreading the lines. */
	.note-body a {
		padding-block: 0.8125rem;
	}

	/* The textarea fills the screen between the bars and grows with the
	   text (app.js does it where field-sizing is missing). 22rem is
	   roughly the header, the top bar, the tabs, the toolbar and the
	   action bar together. */
	.editor-text {
		min-height: max(10rem, calc(100dvh - 22rem));
		field-sizing: content;
		resize: none;
	}

	.editor-preview {
		min-height: max(10rem, calc(100dvh - 22rem));
	}

	/* Write / Preview tabs on top, then the toolbar on one row that
	   scrolls sideways. */
	.md-bar {
		flex-direction: column;
		flex-wrap: nowrap;
		align-items: stretch;
	}

	.md-tabs,
	.md-group {
		flex-wrap: nowrap;
		gap: 0.5rem;
	}

	.md-toolbar {
		flex-wrap: nowrap;
		overflow-x: auto;
	}

	.md-group {
		flex: none;
	}

	/* Save, Done and Cancel stay on screen, above the on-screen keyboard
	   (interactive-widget in app.html) and the home indicator. Rename
	   isn't offered here: it is under ⋯ on the note's page. */
	.editor-actions {
		position: sticky;
		bottom: 0;
		z-index: 1;
		flex-wrap: nowrap;
		margin: 0 -1rem;
		padding: 0.5rem 1rem;
		padding-bottom: calc(0.5rem + env(safe-area-inset-bottom));
		border-top: 1px solid var(--border);
		background: var(--bg);
	}

	.editor-actions .button {
		flex: 1;
	}

	.editor-actions [data-rename] {
		display: none;
	}
}

/* PIN screen */
.login {
	flex: 1;
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 2rem 1rem;
}

.login h1 {
	margin: 0 0 1rem;
	font-size: 1.5rem;
}

.pin-form {
	display: flex;
	flex-direction: column;
	gap: 1rem;
	width: 100%;
	max-width: 20rem;
}

.pin-field {
	width: 100%;
	min-height: 56px;
	padding: 0 1rem;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: var(--surface);
	color: var(--text);
	font-size: 1.75rem;
	letter-spacing: 0.3em;
	text-align: center;
}

.pin-error {
	margin: 0;
	color: var(--error);
	font-weight: 600;
	text-align: center;
}

.keypad {
	display: grid;
	grid-template-columns: repeat(3, 1fr);
	gap: 0.5rem;
}

.keypad[hidden] {
	display: none;
}

.keypad button {
	min-height: 64px;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: var(--surface);
	color: var(--text);
	font: inherit;
	font-size: 1.5rem;
	cursor: pointer;
	touch-action: manipulation;
}

.keypad button:active {
	background: var(--border);
}

.pin-submit {
	min-height: 56px;
	border-color: var(--accent);
	background: var(--accent);
	color: var(--accent-text);
	font-weight: 600;
}
