/* ---- Theme tokens -------------------------------------------------------
   Dark MCM is the default (:root). Light MCM overrides via [data-theme=light].
   Every colour below is a token so the whole app recolours from here. */
:root,
:root[data-theme="dark"] {
	--bg: #1b2422;
	--surface: #24302d;
	--surface-2: #2e3b38;
	--surface-3: #384542;
	--border: #3a4744;
	--text: #ece3d6;
	--text-muted: #9fb0a8;
	--text-faint: #6f807a;
	--accent: #e9c46a;        /* mustard: headings / labels */
	--primary: #2a9d8f;       /* teal: active / actions */
	--primary-hover: #33b4a4;
	--on-primary: #0f1c1a;
	--value: #a7c957;         /* lime: result numbers */
	--danger: #e0743f;        /* burnt orange: destructive */
	--on-danger: #ffffff;
	--input-bg: #17201e;
	--shadow: rgba(0,0,0,0.3);
	--select-arrow: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='%239fb0a8' d='M6 8L0 0h12z'/></svg>");
}

:root[data-theme="light"] {
	--bg: #f5ecd9;
	--surface: #fffaf0;
	--surface-2: #efe3cd;
	--surface-3: #e6d8bd;
	--border: #ddccac;
	--text: #3a2f28;
	--text-muted: #7a6a55;
	--text-faint: #a89a82;
	--accent: #a9761a;        /* deep mustard for contrast on cream */
	--primary: #1f7a70;
	--primary-hover: #186a61;
	--on-primary: #ffffff;
	--value: #5a8a1f;         /* avocado, readable on light */
	--danger: #cc5803;
	--on-danger: #ffffff;
	--input-bg: #fffdf8;
	--shadow: rgba(60,40,10,0.12);
	--select-arrow: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path fill='%237a6a55' d='M6 8L0 0h12z'/></svg>");
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
	background: var(--bg);
	color: var(--text);
	min-height: 100dvh;
	transition: background 0.2s, color 0.2s;
}

/* ---- App shell: header / main / bottom tab bar ------------------------- */

.app-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 0.5rem;
	padding: calc(0.75rem + env(safe-area-inset-top)) 1rem 0.75rem;
}

.app-header h1 {
	flex: 1;
	text-align: center;
	font-size: 1.25rem;
	font-weight: 600;
	color: var(--accent);
}

.theme-toggle,
.account-btn {
	flex-shrink: 0;
	width: 2.3rem;
	height: 2.3rem;
	border-radius: 50%;
	border: 1px solid var(--border);
	background: var(--surface-2);
	color: var(--text);
	font-size: 1.1rem;
	line-height: 1;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
}

.account-btn { color: var(--text-muted); }
.account-btn.connected { color: var(--primary); border-color: var(--primary); }
.account-btn.offline { color: var(--danger); border-color: var(--danger); }

/* ---- Auth modal ------------------------------------------------------- */

.modal {
	position: fixed;
	inset: 0;
	background: rgba(0,0,0,0.55);
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 1.5rem;
	z-index: 50;
}

.modal[hidden] { display: none; }

.modal-card {
	position: relative;
	background: var(--surface);
	border-radius: 1rem;
	padding: 1.5rem;
	width: 100%;
	max-width: 22rem;
	box-shadow: 0 8px 40px rgba(0,0,0,0.4);
}

.modal-card h2 { font-size: 1.15rem; color: var(--accent); margin-bottom: 0.6rem; }
.modal-card p { font-size: 0.9rem; color: var(--text-muted); line-height: 1.5; margin-bottom: 0.9rem; }
.modal-card input[type="email"] { margin-bottom: 0.75rem; }

.modal-close {
	position: absolute;
	top: 0.6rem;
	right: 0.7rem;
	border: none;
	background: transparent;
	color: var(--text-faint);
	font-size: 1.5rem;
	line-height: 1;
	cursor: pointer;
}

.auth-msg { font-size: 0.85rem; color: var(--text-muted); margin-top: 0.7rem; min-height: 1.2rem; }

.save-btn.secondary { background: var(--surface-2); color: var(--text); }
.save-btn.secondary:hover { background: var(--surface-3); }

.app-main {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 0.5rem 1.25rem calc(5.5rem + env(safe-area-inset-bottom));
}

.view {
	width: 100%;
	max-width: 22rem;
}

.view-head {
	margin: 0.25rem 0.25rem 0.9rem;
}

.view-head h2 {
	font-size: 1.1rem;
	font-weight: 600;
	color: var(--accent);
}

.tab-bar {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	display: flex;
	background: var(--surface);
	border-top: 1px solid var(--border);
	padding-bottom: env(safe-area-inset-bottom);
	z-index: 10;
}

.tab {
	flex: 1;
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 0.15rem;
	padding: 0.6rem 0.4rem 0.7rem;
	border: none;
	background: transparent;
	color: var(--text-muted);
	cursor: pointer;
	transition: color 0.2s;
}

.tab-icon { font-size: 1.25rem; line-height: 1; }
.tab-label { font-size: 0.7rem; font-weight: 500; }
.tab.active { color: var(--primary); }

/* ---- Cards + form controls -------------------------------------------- */

.card {
	background: var(--surface);
	border-radius: 1rem;
	padding: 1.5rem;
	width: 100%;
	box-shadow: 0 4px 24px var(--shadow);
}

.field { margin-bottom: 1.25rem; }
.field:last-child { margin-bottom: 0; }

label {
	display: block;
	font-size: 0.85rem;
	color: var(--text-muted);
	margin-bottom: 0.35rem;
}

.label-hint { color: var(--text-faint); font-weight: 400; }

input[type="number"],
input[type="text"] {
	width: 100%;
	padding: 0.75rem;
	border: 1px solid var(--border);
	border-radius: 0.5rem;
	background: var(--input-bg);
	color: var(--text);
	font-size: 1.1rem;
	outline: none;
	transition: border-color 0.2s;
	-moz-appearance: textfield;
}

input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; }

input::placeholder { color: var(--text-faint); }

input:focus { border-color: var(--primary); }

select {
	width: 100%;
	padding: 0.75rem;
	border: 1px solid var(--border);
	border-radius: 0.5rem;
	background: var(--input-bg);
	color: var(--text);
	font-size: 1.1rem;
	outline: none;
	cursor: pointer;
	appearance: none;
	-webkit-appearance: none;
	background-image: var(--select-arrow);
	background-repeat: no-repeat;
	background-position: right 0.9rem center;
}

select:focus { border-color: var(--primary); }

.unit-toggle {
	display: flex;
	background: var(--surface-2);
	border-radius: 0.5rem;
	overflow: hidden;
	margin-bottom: 0.6rem;
}

.unit-toggle.compact button { padding: 0.5rem; font-size: 0.9rem; }

.unit-toggle button {
	flex: 1;
	padding: 0.6rem;
	border: none;
	background: transparent;
	color: var(--text-muted);
	font-size: 0.95rem;
	font-weight: 500;
	cursor: pointer;
	transition: background 0.2s, color 0.2s;
}

.unit-toggle button.active {
	background: var(--primary);
	color: var(--on-primary);
}

.gauge-row {
	display: flex;
	align-items: center;
	gap: 0.5rem;
}

.gauge-row input { flex: 1; min-width: 0; }
.gauge-word { color: var(--text-faint); font-size: 0.85rem; }
.unit-suffix { color: var(--text-muted); font-size: 0.9rem; min-width: 1.5rem; }

/* ---- Result / preview box --------------------------------------------- */

.result {
	background: var(--surface-2);
	border-radius: 0.75rem;
	padding: 1.1rem;
	text-align: center;
	min-height: 4.5rem;
	display: flex;
	flex-direction: column;
	justify-content: center;
	gap: 0.15rem;
	margin-top: 0.5rem;
}

.result-value {
	font-size: 1.9rem;
	font-weight: 700;
	color: var(--value);
	line-height: 1.15;
}

.result-unit { font-size: 0.9rem; font-weight: 500; color: var(--text-muted); }
.result-detail { font-size: 0.85rem; color: var(--text-muted); }
.result-detail strong { color: var(--value); font-weight: 700; }
.result-empty { color: var(--text-faint); font-size: 0.95rem; }
.result-saved { color: var(--primary); font-size: 0.95rem; font-weight: 600; }

.preview-needle {
	font-size: 0.85rem;
	color: var(--text-muted);
	margin-bottom: 0.75rem;
}

.preview-needle + .preview-stats { padding-top: 0.75rem; border-top: 1px solid var(--border); }

.preview-stats {
	display: flex;
	justify-content: center;
}

.preview-stats .stat {
	flex: 1;
	padding: 0 0.5rem;
}

.preview-stats .stat + .stat { border-left: 1px solid var(--border); }

.stat-value {
	font-size: 1.8rem;
	font-weight: 700;
	color: var(--value);
	line-height: 1.1;
}

.stat-unit {
	font-size: 0.8rem;
	font-weight: 500;
	color: var(--text-muted);
	margin-top: 0.1rem;
}

.stat-label {
	font-size: 0.72rem;
	color: var(--text-faint);
	margin-top: 0.15rem;
}

.save-btn {
	width: 100%;
	margin-top: 1.25rem;
	padding: 0.8rem;
	border: none;
	border-radius: 0.5rem;
	background: var(--primary);
	color: var(--on-primary);
	font-size: 0.95rem;
	font-weight: 600;
	cursor: pointer;
	transition: background 0.2s;
}

.save-btn:hover { background: var(--primary-hover); }
.save-btn:disabled { background: var(--surface-2); color: var(--text-faint); cursor: default; }

/* ---- Swatches view: projects + swatches ------------------------------- */

.empty {
	color: var(--text-faint);
	font-size: 0.95rem;
	text-align: center;
	line-height: 1.5;
	padding: 2rem 0;
}

.project {
	background: var(--surface);
	border-radius: 0.9rem;
	padding: 1rem 1.1rem;
	margin-bottom: 0.75rem;
	box-shadow: 0 2px 12px var(--shadow);
}

.project-header {
	display: flex;
	align-items: center;
	gap: 0.5rem;
	margin-bottom: 0.4rem;
}

.project-name {
	flex: 1;
	min-width: 0;
	font-size: 1.05rem;
	font-weight: 600;
	color: var(--text);
	cursor: pointer;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.project-name.unnamed { color: var(--text-faint); font-weight: 400; font-style: italic; }

.project-name-input {
	flex: 1;
	min-width: 0;
	padding: 0.4rem 0.5rem;
	border: 1px solid var(--primary);
	border-radius: 0.4rem;
	background: var(--input-bg);
	color: var(--text);
	font-size: 1rem;
	outline: none;
}

.project-actions { display: flex; gap: 0.3rem; flex-shrink: 0; }

.icon-btn {
	border: none;
	background: var(--surface-2);
	color: var(--text-muted);
	width: 1.9rem;
	height: 1.9rem;
	border-radius: 0.5rem;
	font-size: 1rem;
	line-height: 1;
	cursor: pointer;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: background 0.2s, color 0.2s;
}

.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.icon-btn.delete:hover { background: var(--danger); color: var(--on-danger); }

.swatch {
	display: flex;
	align-items: flex-start;
	gap: 0.6rem;
	padding: 0.6rem 0;
	border-top: 1px solid var(--border);
}

.swatch-main { flex: 1; min-width: 0; }
.swatch-role {
	font-size: 0.8rem;
	font-weight: 600;
	color: var(--accent);
	text-transform: uppercase;
	letter-spacing: 0.03em;
	margin-bottom: 0.2rem;
}
.swatch-detail { font-size: 0.85rem; color: var(--text-muted); margin-top: 0.1rem; }
.swatch-detail strong { color: var(--value); font-weight: 700; }
.swatch-detail.muted { color: var(--text-faint); }
.empty-swatches { padding: 0.4rem 0 0.1rem; }

/* ---- Reference view --------------------------------------------------- */

.ref-item {
	width: 100%;
	text-align: left;
	padding: 0.9rem 1rem;
	margin-top: 0.6rem;
	border: 1px solid var(--border);
	border-radius: 0.75rem;
	background: var(--surface);
	color: var(--text);
	font-size: 0.95rem;
	font-weight: 500;
	cursor: pointer;
}

.view-head + .ref-item { margin-top: 0; }

.ref-item::after { content: " ▾"; color: var(--text-faint); }
.ref-item.open::after { content: " ▴"; }

.ref-table {
	margin-top: 0.5rem;
	border-radius: 0.75rem;
	overflow: hidden;
	background: var(--surface);
}

.ref-table table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }

.ref-table th {
	text-align: left;
	color: var(--text-muted);
	font-weight: 500;
	font-size: 0.8rem;
	padding: 0.5rem 0.9rem;
	border-bottom: 1px solid var(--border);
}

.ref-table td { padding: 0.45rem 0.9rem; color: var(--text); border-bottom: 1px solid var(--border); }
.ref-table tr:last-child td { border-bottom: none; }
.ref-table td.mm { color: var(--value); font-weight: 600; }
.ref-table td.none { color: var(--text-faint); }
.ref-table td.num { color: var(--value); font-weight: 700; }
.ref-table td.name { color: var(--text); font-weight: 600; }
.ref-table td.aka { color: var(--text-muted); font-size: 0.8rem; }
