/* ============================================================
   CSS Custom Properties
   ============================================================ */
:root {
	--bg:            #1a1a1a;
	--bg-elevated:   #242424;
	--bg-hover:      #2e2e2e;
	--border:        #333;
	--border-dashed: #4a4a4a;
	--text:          #e8e8e8;
	--text-muted:    #888;
	--text-label:    #aaa;
	--accent:        #77b355;
	--accent-dim:    rgba(119, 179, 85, 0.12);
	--accent-border: rgba(119, 179, 85, 0.4);
	--danger:        #f87171;
	--danger-hover:  #ef4444;

	--space-xs:  4px;
	--space-sm:  8px;
	--space-md:  16px;
	--space-lg:  24px;
	--space-xl:  40px;

	--font:      system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
	--font-mono: "Courier New", Courier, monospace;

	--goal-size: 48px;
	--goal-gap:  6px;
	--radius:    4px;
	--radius-lg: 8px;

	--transition: 120ms ease;
}

/* ============================================================
   Reset & Base
   ============================================================ */
*, *::before, *::after {
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}

[hidden] {
	display: none !important;
}

html {
	font-size: 16px;
	-webkit-text-size-adjust: 100%;
}

body {
	background: var(--bg);
	color: var(--text);
	font-family: var(--font);
	line-height: 1.5;
	min-height: 100vh;
}

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

a:hover, a:focus-visible {
	text-decoration: underline;
}

button {
	cursor: pointer;
	font-family: inherit;
	font-size: inherit;
}

/* ============================================================
   Header
   ============================================================ */
.site-header {
	background: #111;
	border-bottom: 1px solid var(--border);
	position: sticky;
	top: 0;
	z-index: 100;
}

.header-inner {
	max-width: 1200px;
	margin: 0 auto;
	padding: var(--space-md) var(--space-lg);
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: var(--space-md);
}

.header-brand {
	display: flex;
	align-items: baseline;
	gap: var(--space-sm);
}

.header-actions {
	display: flex;
	align-items: center;
	gap: var(--space-sm);
}

.site-logo {
	font-size: 1.25rem;
	font-weight: 700;
	color: var(--text);
	letter-spacing: -0.02em;
	text-decoration: none;
}

.site-logo:hover {
	color: var(--accent);
	text-decoration: none;
}

.site-tagline {
	font-size: 0.8125rem;
	color: var(--text-muted);
}

/* ============================================================
   Buttons
   ============================================================ */
.btn {
	display: inline-flex;
	align-items: center;
	gap: var(--space-xs);
	padding: 6px var(--space-md);
	border: 1px solid transparent;
	border-radius: var(--radius);
	font-size: 0.875rem;
	font-weight: 500;
	line-height: 1.5;
	transition: background var(--transition), color var(--transition), border-color var(--transition);
	white-space: nowrap;
}

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

.btn-primary {
	background: var(--accent-dim);
	color: var(--accent);
	border-color: var(--accent-border);
}

.btn-primary:hover {
	background: var(--accent-border);
	color: var(--text);
	border-color: var(--accent);
}

.btn-secondary {
	background: transparent;
	color: var(--text-muted);
	border-color: var(--border);
}

.btn-secondary:hover {
	background: var(--bg-hover);
	color: var(--text);
	border-color: #555;
}

.btn-danger {
	background: var(--danger);
	color: #111;
	border-color: var(--danger);
}

.btn-danger:hover {
	background: var(--danger-hover);
	border-color: var(--danger-hover);
}

.btn-ghost {
	background: transparent;
	color: var(--text-muted);
	border-color: transparent;
	padding: 4px var(--space-sm);
}

.btn-ghost:hover {
	color: var(--danger);
}

/* ============================================================
   Display / Edit mode
   ============================================================ */

/* In display mode (default): hide edit controls and the New List button */
body:not(.edit-mode) #new-list-btn,
body:not(.edit-mode) .list-actions {
	display: none;
}

/* In display mode: goal squares show tooltips on hover but are not interactive */
body:not(.edit-mode) .goal-square {
	cursor: default;
}

/* Header button visibility by mode:
   - new-list-btn: only visible in edit mode */

/* ============================================================
   Drag and Drop
   ============================================================ */

/* Reorder widget — fixed-size container holding the drag handle and arrow buttons.
   Arrows are positioned absolutely so they never affect surrounding layout. */
.reorder-widget {
	display: none;
	position: relative;
	flex-shrink: 0;
	width: 1.5rem;
	align-self: stretch;
	align-items: center;
	justify-content: center;
}

/* Show on list rows in edit mode */
body.edit-mode .list-row .reorder-widget {
	display: flex;
}

/* Always show on goal rows (modal only opens in edit mode) */
.goal-row .reorder-widget {
	display: flex;
}

/* Drag handle — the ⠿ grip icon; centered within the widget */
.drag-handle {
	display: flex;
	cursor: grab;
	color: var(--text-muted);
	user-select: none;
	font-size: 1rem;
	align-items: center;
	justify-content: center;
	width: 100%;
	padding: 0;
	position: relative;
	z-index: 1;
	transition: opacity var(--transition);
}

.drag-handle:active {
	cursor: grabbing;
}

/* Arrow buttons — absolutely positioned within the widget (top / bottom half).
   They never occupy flow space, so no layout shift when they appear. */
.reorder-widget .move-row-btn {
	position: absolute;
	left: 0;
	width: 100%;
	height: 50%;
	padding: 0;
	font-size: 0.5rem;
	display: flex;
	align-items: center;
	justify-content: center;
	opacity: 0;
	pointer-events: none;
	background: var(--bg-elevated);
	border-radius: var(--radius);
	z-index: 2;
	transition: opacity var(--transition);
}

.reorder-widget .move-list-up-btn,
.reorder-widget .move-up-btn {
	top: 0;
}

.reorder-widget .move-list-down-btn,
.reorder-widget .move-down-btn {
	bottom: 0;
}

/* Reveal arrows on hover or keyboard focus-within the widget */
.reorder-widget:hover .move-row-btn,
.reorder-widget:focus-within .move-row-btn {
	opacity: 1;
	pointer-events: auto;
}

/* Fade the handle slightly when arrows are shown */
.reorder-widget:hover .drag-handle,
.reorder-widget:focus-within .drag-handle {
	opacity: 0.3;
}

/* Disabled arrows: dimmed when revealed, invisible otherwise
   (scoped to hover/focus so they don't bleed through at rest) */
.reorder-widget:hover .move-row-btn:disabled,
.reorder-widget:focus-within .move-row-btn:disabled {
	opacity: 0.2;
	pointer-events: none;
}

/* Row being dragged */
.dragging {
	opacity: 0.4;
}

/* Drop target highlight — full-item accent outline centered on the target.
   One unique position per item; no top/bottom ambiguity. */
.drag-over {
	outline: 2px solid var(--accent);
	outline-offset: -2px;
	border-radius: var(--radius);
}

/* ============================================================
   Main / Gallery
   ============================================================ */
#gallery {
	max-width: 1200px;
	margin: 0 auto;
	padding: var(--space-xl) var(--space-lg);
}

.status-message {
	color: var(--text-muted);
	text-align: center;
	padding: var(--space-xl) 0;
}

/* ============================================================
   List Row
   ============================================================ */
.list-row {
	margin-bottom: var(--space-xl);
}

.list-header {
	display: flex;
	align-items: center;
	gap: var(--space-md);
	margin-bottom: var(--space-md);
	flex-wrap: wrap;
}

.list-name {
	font-size: 1.375rem;
	font-weight: 600;
	letter-spacing: -0.02em;
	color: var(--text);
	line-height: 1.2;
}

.list-description {
	font-size: 0.8125rem;
	color: var(--text-muted);
	margin-top: 2px;
}

/* Progress */
.list-progress {
	display: flex;
	align-items: center;
	gap: var(--space-sm);
	flex-shrink: 0;
}

.list-progress-text {
	font-size: 0.8125rem;
	color: var(--text-muted);
	font-variant-numeric: tabular-nums;
	white-space: nowrap;
}

.list-progress-bar {
	width: 80px;
	height: 4px;
	background: var(--border);
	border-radius: 2px;
	overflow: hidden;
	flex-shrink: 0;
}

.list-progress-fill {
	height: 100%;
	background: var(--accent);
	border-radius: 2px;
	transition: width 300ms ease;
	min-width: 0;
}

/* List actions */
.list-actions {
	display: flex;
	align-items: center;
	gap: var(--space-xs);
	margin-left: auto;
}

/* ============================================================
   Goals Grid
   ============================================================ */
.goals-grid {
	display: flex;
	flex-wrap: wrap;
	gap: var(--goal-gap);
}

/* Twemoji <img> elements inside goal squares.
   Drop-shadow follows the SVG alpha channel, creating a subtle halo that
   keeps dark/monochrome emoji (e.g. 🐃) visible on the dark background. */
.goal-square img.emoji {
	width: 1.6rem;
	height: 1.6rem;
	pointer-events: none;
	filter: drop-shadow(0 0 1px rgba(255, 255, 255, 0.25));
}

/* Name label inside each goal button — hidden in grid layout */
.goal-name-label {
	display: none;
}

/* Individual goal square */
.goal-square {
	width: var(--goal-size);
	height: var(--goal-size);
	border-radius: var(--radius);
	border: 2px dashed var(--border-dashed);
	background: transparent;
	color: transparent;
	font-size: 1.25rem;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: border-color var(--transition), background var(--transition), color var(--transition);
	position: relative;
	flex-shrink: 0;
}

/* ============================================================
   List Layout
   ============================================================ */

/* The goals container becomes a CSS grid with auto-fill columns */
body.layout-list .goals-grid {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
	gap: var(--space-xs);
}

/* Each goal becomes a horizontal row: icon + name */
body.layout-list .goal-square {
	width: 100%;
	height: auto;
	min-height: 40px;
	padding: var(--space-xs) var(--space-sm);
	border-style: solid;
	border-width: 1px;
	justify-content: flex-start;
	gap: var(--space-sm);
	flex-shrink: 1;
	color: var(--text-muted);
}

/* Show the name label */
body.layout-list .goal-name-label {
	display: block;
	font-size: 0.875rem;
	color: var(--text-muted);
	text-align: left;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	min-width: 0;
}

/* Name inherits the accent color for completed items */
body.layout-list .goal-square.completed .goal-name-label {
	color: var(--text);
}

/* Icon wrap: fixed size so names align across complete/incomplete items */
.goal-icon-wrap {
	display: flex;
	align-items: center;
	justify-content: center;
	flex-shrink: 0;
	width: 1.6rem;
	height: 1.6rem;
}

/* Incomplete items in list mode: show a subtle dashed border around the icon slot */
body.layout-list .goal-square:not(.completed) .goal-icon-wrap {
	border: 1.5px dashed var(--border-dashed);
	border-radius: 3px;
}

/* Suppress hover tooltip in list mode since the name is always visible */
body.layout-list .goal-square::after {
	display: none;
}

.goal-square:hover {
	border-color: #666;
	background: var(--bg-hover);
}

.goal-square:focus-visible {
	outline: 2px solid var(--accent);
	outline-offset: 2px;
}

/* Custom tooltip */
.goal-square::after {
	content: attr(data-tooltip);
	position: absolute;
	bottom: calc(100% + 7px);
	left: 50%;
	transform: translateX(-50%);
	background: #2a2a2a;
	border: 1px solid var(--border);
	color: var(--text);
	font-size: 0.75rem;
	line-height: 1.3;
	padding: 4px 8px;
	border-radius: var(--radius);
	white-space: nowrap;
	max-width: 280px;
	pointer-events: none;
	opacity: 0;
	transition: opacity 150ms ease;
	z-index: 50;
}

.goal-square:hover::after,
.goal-square:focus-visible::after {
	opacity: 1;
}

/* Completed state */
.goal-square.completed {
	border: 2px solid rgba(255, 255, 255, 0.25);
	background: rgba(255, 255, 255, 0.12);
	color: var(--text-muted);
}

.goal-square.completed:hover {
	background: rgba(255, 255, 255, 0.2);
}

/* Optimistic toggle feedback */
.goal-square.loading {
	opacity: 0.5;
	pointer-events: none;
}

/* ============================================================
   Modals
   ============================================================ */
.modal {
	position: fixed;
	inset: 0;
	z-index: 200;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: var(--space-md);
}

.modal[hidden] {
	display: none;
}

.modal-backdrop {
	position: absolute;
	inset: 0;
	background: rgba(0, 0, 0, 0.7);
}

.modal-panel {
	position: relative;
	background: var(--bg-elevated);
	border: 1px solid var(--border);
	border-radius: var(--radius-lg);
	padding: var(--space-lg);
	width: 100%;
	max-width: 600px;
	max-height: calc(100vh - 2 * var(--space-md));
	overflow-y: auto;
	z-index: 1;
}

.modal-panel--sm {
	max-width: 360px;
}

.modal-title {
	font-size: 1.125rem;
	font-weight: 600;
	margin-bottom: var(--space-lg);
}

/* ============================================================
   Forms
   ============================================================ */
.form-group {
	margin-bottom: var(--space-md);
}

.form-group label {
	display: block;
	font-size: 0.8125rem;
	color: var(--text-label);
	margin-bottom: var(--space-xs);
}

.label-optional {
	color: var(--text-muted);
	font-weight: 400;
}

.form-group input[type="text"],
.form-group textarea {
	width: 100%;
	background: var(--bg);
	border: 1px solid var(--border);
	border-radius: var(--radius);
	color: var(--text);
	font-family: inherit;
	font-size: 0.9375rem;
	padding: 8px var(--space-sm);
	transition: border-color var(--transition);
}

.form-group textarea {
	resize: vertical;
	line-height: 1.5;
}

.form-group input[type="text"]:focus,
.form-group textarea:focus {
	outline: none;
	border-color: var(--accent);
}

.form-group input[type="text"]::placeholder,
.form-group textarea::placeholder {
	color: var(--text-muted);
}

.form-actions {
	display: flex;
	justify-content: space-between;
	align-items: center;
	gap: var(--space-sm);
	margin-top: var(--space-lg);
}

.form-actions-left,
.form-actions-right {
	display: flex;
	gap: var(--space-sm);
}

/* Paste a List panel */
#paste-list-panel {
	margin-top: var(--space-md);
	display: flex;
	flex-direction: column;
	gap: var(--space-sm);
}

#paste-list-input {
	width: 100%;
	resize: vertical;
	font: inherit;
	font-size: 0.875rem;
	background: var(--bg);
	color: var(--text);
	border: 1px solid var(--border);
	border-radius: var(--radius);
	padding: var(--space-sm);
	box-sizing: border-box;
}

#paste-list-input:focus {
	outline: none;
	border-color: var(--accent);
}

.paste-list-actions {
	display: flex;
	justify-content: flex-end;
	gap: var(--space-sm);
}

/* Autofill status message (errors red by default, success overrides to green) */
.autofill-status {
	font-size: 0.8125rem;
	color: var(--danger);
	margin-top: calc(-1 * var(--space-sm));
	margin-bottom: 0;
}

.autofill-status--success {
	color: var(--accent);
}

/* Spinner shown inside the Autofill button while the request is in flight */
@keyframes autofill-spin {
	to { transform: rotate(360deg); }
}

.autofill-spinner {
	display: inline-block;
	width: 0.875em;
	height: 0.875em;
	border: 2px solid currentColor;
	border-top-color: transparent;
	border-radius: 50%;
	animation: autofill-spin 0.6s linear infinite;
	vertical-align: -0.15em;
}

/* Highlight animation for newly autofilled goal rows */
@keyframes goal-row-highlight {
	0%   { background-color: var(--accent-dim); }
	100% { background-color: transparent; }
}

.goal-row--new {
	border-radius: var(--radius);
	animation: goal-row-highlight 2.5s ease-out forwards;
}

.form-error {
	font-size: 0.8125rem;
	color: var(--danger);
	margin-top: calc(-1 * var(--space-sm));
	margin-bottom: var(--space-sm);
}

/* Turnstile widget spacing */
.cf-turnstile {
	margin-bottom: var(--space-md);
}

/* ============================================================
   Responsive
   ============================================================ */
@media (max-width: 600px) {
	.header-inner {
		padding: var(--space-sm) var(--space-md);
	}

	#gallery {
		padding: var(--space-lg) var(--space-md);
	}

	.list-header {
		gap: var(--space-sm);
	}

	.list-progress-bar {
		display: none;
	}

	:root {
		--goal-size: 40px;
	}
}

/* ============================================================
   Goal rows (Add Goal modal)
   ============================================================ */
#goal-rows {
	margin-bottom: var(--space-sm);
}

.goal-row {
	display: flex;
	align-items: center;
	gap: var(--space-sm);
	margin-bottom: var(--space-sm);
}

/* Emoji trigger button */
.goal-emoji-btn {
	flex-shrink: 0;
	width: 2.5rem;
	height: 2.5rem;
	font-size: 1.25rem;
	line-height: 1;
	padding: 0;
	display: flex;
	align-items: center;
	justify-content: center;
}

/* Twemoji <img> inside the emoji picker trigger button */
.goal-emoji-btn img.emoji {
	width: 1.6rem;
	height: 1.6rem;
	pointer-events: none;
}

/* Goal name input inside a row (mirrors .form-group input[type="text"]) */
.goal-name-input {
	flex: 1;
	background: var(--bg);
	border: 1px solid var(--border);
	border-radius: var(--radius);
	color: var(--text);
	font-family: inherit;
	font-size: 0.9375rem;
	padding: 8px var(--space-sm);
	transition: border-color var(--transition);
	min-width: 0;
}

.goal-name-input:focus {
	outline: none;
	border-color: var(--accent);
}

.goal-name-input::placeholder {
	color: var(--text-muted);
}

/* Remove-row button */
.remove-row-btn {
	flex-shrink: 0;
	width: 2rem;
	height: 2rem;
	font-size: 0.875rem;
	padding: 0;
	display: flex;
	align-items: center;
	justify-content: center;
}

/* Move up / move down buttons */
.move-row-btn {
	flex-shrink: 0;
	width: 1.75rem;
	height: 2rem;
	font-size: 0.625rem;
	padding: 0;
	display: flex;
	align-items: center;
	justify-content: center;
}

.move-row-btn:disabled {
	opacity: 0.2;
	cursor: default;
}

/* "Add another" button */
#add-goal-row {
	font-size: 0.8125rem;
	padding: 4px var(--space-sm);
	margin-bottom: var(--space-md);
}

/* ============================================================
   Emoji picker
   ============================================================ */
#emoji-picker-wrap {
	position: fixed;
	z-index: 300;
	border-radius: var(--radius-lg);
	/* Shadow to lift it above everything */
	box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
}

#emoji-picker-wrap[hidden] {
	display: none;
}

/* Theme emoji-picker-element to match the app's dark palette */
emoji-picker {
	--background:           #242424;
	--border-color:         #333;
	--border-radius:        8px;
	--input-border-color:   #444;
	--input-border-radius:  4px;
	--input-font-color:     #e8e8e8;
	--input-placeholder-color: #888;
	--outline-color:        #77b355;
	--category-emoji-padding: 0.25rem;
	--indicator-color:      #77b355;
	--search-background:    #1a1a1a;
	--button-hover-background: rgba(119, 179, 85, 0.12);
	--button-active-background: rgba(119, 179, 85, 0.2);
	width: 360px;
	max-height: 380px;
}

/* ============================================================
   Light Mode
   ============================================================ */
body.light-mode {
	--bg:            #f7f7f7;
	--bg-elevated:   #ffffff;
	--bg-hover:      #eeeeee;
	--border:        #ddd;
	--border-dashed: #bbb;
	--text:          #111;
	--text-muted:    #777;
	--text-label:    #555;
	--danger:        #dc2626;
	--danger-hover:  #b91c1c;
}

body.light-mode .site-header {
	background: #ffffff;
	border-bottom-color: #e5e5e5;
}

/* Completed squares: invert the rgba values for a light background */
body.light-mode .goal-square.completed {
	border: 2px solid rgba(0, 0, 0, 0.15);
	background: rgba(0, 0, 0, 0.06);
}

body.light-mode .goal-square.completed:hover {
	background: rgba(0, 0, 0, 0.1);
}

/* Drop-shadow on emoji should darken on a light background */
body.light-mode .goal-square img.emoji {
	filter: drop-shadow(0 0 1px rgba(0, 0, 0, 0.25));
}

/* Tooltip */
body.light-mode .goal-square::after {
	background: #ffffff;
	border-color: #ddd;
	color: #111;
}

/* Emoji picker */
body.light-mode emoji-picker {
	--background:                #ffffff;
	--border-color:              #ddd;
	--input-border-color:        #ccc;
	--input-font-color:          #111;
	--input-placeholder-color:   #999;
	--search-background:         #f7f7f7;
	--button-hover-background:   rgba(119, 179, 85, 0.12);
	--button-active-background:  rgba(119, 179, 85, 0.2);
}

/* ============================================================
   Catalog view
   ============================================================ */

/* Accessible visually-hidden utility used for the search label */
.sr-only {
	position: absolute;
	width: 1px;
	height: 1px;
	padding: 0;
	margin: -1px;
	overflow: hidden;
	clip: rect(0, 0, 0, 0);
	white-space: nowrap;
	border: 0;
}

/* ============================================================
   Print
   ============================================================ */
@media print {
	.site-header,
	.list-actions,
	.modal,
	#edit-toggle-btn,
	#layout-toggle-btn,
	#theme-toggle-btn {
		display: none !important;
	}

	body {
		background: #fff;
		color: #000;
	}

	.list-row {
		break-inside: avoid;
	}

	.goal-square {
		border: 2px dashed #ccc;
		color: transparent;
	}

	.goal-square.completed {
		border: 2px solid #bbb;
		background: #f0f0f0;
		color: #555;
	}
}
