:root {
    --bg: #f5f6fa;
    --card: #ffffff;
    --text: #1f2a44;
    --muted: #6b7280;
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --border: #e5e7eb;
    --success-bg: #ecfdf5;
    --success-fg: #047857;
    --error-bg: #fef2f2;
    --error-fg: #b91c1c;
    --danger: #dc2626;
    --over-budget: #dc2626;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.container {
    max-width: 1100px;
    margin: 24px auto;
    padding: 0 20px 60px;
}

/* --- Header & footer --- */
.site-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 24px;
    background: #fff;
    border-bottom: 1px solid var(--border);
    box-shadow: 0 1px 2px rgba(0,0,0,.03);
}
.brand {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--text);
}
.site-header nav {
    display: flex;
    align-items: center;
    gap: 14px;
}
.user-chip {
    background: #eef2ff;
    color: var(--primary-dark);
    padding: 4px 10px;
    border-radius: 999px;
    font-size: .9rem;
}
.site-footer {
    text-align: center;
    color: var(--muted);
    padding: 20px;
}

/* --- Forms & buttons --- */
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack label { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; color: var(--muted); }
.stack input, .stack textarea, .stack select {
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 1rem;
    background: #fff;
    color: var(--text);
}
.stack input:focus, .stack textarea:focus, .stack select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, .15);
}
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

button, .btn {
    display: inline-block;
    padding: 10px 16px;
    border: 1px solid var(--border);
    background: #fff;
    color: var(--text);
    border-radius: 8px;
    cursor: pointer;
    font-size: 1rem;
    text-decoration: none;
}
button:hover, .btn:hover { background: #f3f4f6; text-decoration: none; }
button.primary, .btn.primary {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}
button.primary:hover, .btn.primary:hover {
    background: var(--primary-dark);
    border-color: var(--primary-dark);
}
.link-btn {
    background: none;
    border: none;
    padding: 0;
    color: var(--primary);
    cursor: pointer;
    font-size: inherit;
}
.link-btn.danger { color: var(--danger); font-weight: 600; }
.inline { display: inline; }

/* --- Flash messages --- */
.flash {
    padding: 10px 14px;
    border-radius: 8px;
    margin-bottom: 12px;
    font-size: .95rem;
}
.flash-success { background: var(--success-bg); color: var(--success-fg); }
.flash-error { background: var(--error-bg); color: var(--error-fg); }
.flash-warn { background: #fef3c7; color: #92400e; }

/* --- Cards & layout --- */
.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 16px;
    box-shadow: 0 1px 3px rgba(0,0,0,.04);
}
.card.narrow { max-width: 420px; margin: 40px auto; }
.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 14px;
}
.card.tile {
    display: block;
    color: inherit;
    transition: transform .08s ease, box-shadow .08s ease;
}
.card.tile:hover {
    text-decoration: none;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(0,0,0,.06);
}

/* Project tiles with cover image on top */
.project-tile { padding: 0; overflow: hidden; }
.project-tile .project-tile-body { padding: 16px 18px; }
.project-cover {
    width: 100%;
    height: 140px;
    background-size: cover;
    background-position: center;
    background-color: #eef2ff;
}
.project-cover.placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.4rem;
    color: #a5b4fc;
}
.project-tile h3 { margin: 0 0 4px; }
.project-tile .big-number { font-size: 1.3rem; margin: 4px 0; }
.row {
    display: grid;
    grid-template-columns: 1fr 320px;
    gap: 24px;
    margin-bottom: 24px;
}
@media (max-width: 820px) {
    .row { grid-template-columns: 1fr; }
}
.col { min-width: 0; }
.narrow-col { max-width: 420px; }

/* --- Typography helpers --- */
h1 { margin-top: 0; }
h2 { margin-top: 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.big-number { font-size: 1.6rem; font-weight: 700; margin: 6px 0; }
.right { text-align: right; }
.crumbs { color: var(--muted); font-size: .9rem; margin-bottom: 8px; }
.over-budget { color: var(--over-budget); font-weight: 700; }

/* --- Table --- */
table.data {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
}
table.data th, table.data td {
    padding: 10px 12px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
table.data th { background: #f9fafb; font-size: .85rem; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
table.data tr:last-child td { border-bottom: none; }
table.data tfoot td { background: #f9fafb; }

/* --- Project page --- */
.project-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}
.project-header-main {
    display: flex;
    gap: 16px;
    align-items: flex-start;
    flex: 1;
    min-width: 0;
}
.project-hero {
    width: 120px;
    height: 120px;
    object-fit: cover;
    border-radius: 12px;
    border: 1px solid var(--border);
    cursor: zoom-in;
}
.project-header .project-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

/* Dashboard: household sections */
.household-section { margin-bottom: 32px; }
.household-section-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    margin-bottom: 10px;
    gap: 12px;
    flex-wrap: wrap;
}
.household-section-header h2 { margin: 0; }
.household-section-header h2 a { color: inherit; }

/* Line-item row thumbnails */
.thumb-cell { width: 60px; }
.item-thumb {
    width: 48px;
    height: 48px;
    object-fit: cover;
    border-radius: 6px;
    border: 1px solid var(--border);
    cursor: zoom-in;
    display: block;
}
.item-thumb.receipt-thumb-inline { opacity: 0.9; }
.row-actions { white-space: nowrap; }
.row-actions .link-btn { font-size: 1rem; margin-right: 4px; }

/* Edit form previews */
.image-preview {
    margin: 6px 0 10px;
    padding: 10px;
    border: 1px dashed var(--border);
    border-radius: 8px;
    background: #f9fafb;
}
.image-preview img {
    max-width: 220px;
    max-height: 220px;
    border-radius: 8px;
    display: block;
    margin-bottom: 8px;
    cursor: zoom-in;
}
.actions-row {
    display: flex;
    gap: 10px;
    align-items: center;
}
label.checkbox {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: .9rem;
    color: var(--text);
}
.summary {
    display: flex;
    gap: 24px;
    flex-wrap: wrap;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px 20px;
    margin-bottom: 24px;
}
.stat { display: flex; flex-direction: column; }

.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    background: #f3f4f6;
    color: var(--muted);
    font-size: .75rem;
    text-transform: uppercase;
    letter-spacing: .05em;
}
.badge-ocr { background: #eef2ff; color: var(--primary-dark); }

.admin-badge {
    display: inline-block;
    background: #fef3c7;
    color: #92400e;
    padding: 1px 7px;
    border-radius: 999px;
    font-size: .7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: .05em;
    vertical-align: middle;
    margin-left: 4px;
}
.admin-link { font-weight: 600; }

.badge-ok   { background: #d1fae5; color: #065f46; }
.badge-warn { background: #fef3c7; color: #92400e; }

.smtp-status-head {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
}
.smtp-status-head h2 { margin: 0; }
.smtp-dl {
    display: grid;
    grid-template-columns: 110px 1fr;
    gap: 4px 12px;
    margin: 8px 0 16px;
    font-size: .95rem;
}
.smtp-dl dt { color: var(--muted); }
.smtp-dl dd { margin: 0; }
.smtp-test-form {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    flex-wrap: wrap;
}
.smtp-test-form label { flex: 1; min-width: 220px; }
.smtp-test-form input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 1rem;
}

.ocr-preview { margin-top: 14px; }
.receipt-thumb {
    max-width: 100%;
    border-radius: 8px;
    border: 1px solid var(--border);
    margin: 8px 0;
}
.ocr-preview pre {
    background: #0f172a;
    color: #e2e8f0;
    padding: 10px;
    border-radius: 8px;
    overflow: auto;
    font-size: .8rem;
    max-height: 220px;
}

ul.plain { list-style: none; padding: 0; }
ul.plain li { padding: 6px 0; border-bottom: 1px solid var(--border); }
ul.plain li:last-child { border-bottom: none; }

ul.members-list li {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

h3.section-sub {
    margin: 18px 0 8px;
    font-size: .85rem;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--muted);
    border-top: 1px solid var(--border);
    padding-top: 14px;
}

.user-picker {
    max-height: 240px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #fff;
    padding: 4px 0;
    margin-bottom: 10px;
}
.user-picker-row {
    display: flex;
    gap: 10px;
    align-items: flex-start;
    padding: 8px 12px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    font-size: .95rem;
    color: var(--text);
}
.user-picker-row:last-child { border-bottom: none; }
.user-picker-row:hover { background: #f3f4f6; }
.user-picker-row.header {
    background: #f9fafb;
    position: sticky;
    top: 0;
    z-index: 1;
}
.user-picker-row input[type="checkbox"] {
    margin-top: 3px;
    flex: 0 0 auto;
}
.user-picker-row span { flex: 1; min-width: 0; }

/* --- Print view --- */
.print-header { margin-bottom: 16px; }
table.data.print { border-radius: 0; border-color: #000; }
table.data.print th { background: #eee; color: #000; }

/* --- Processing overlay (OCR lock screen) --- */
body.is-locked { overflow: hidden; }

/* Native <dialog> styling. We override the browser defaults so the backdrop
   covers the viewport with a blur and the card is centered. */
.processing-overlay {
    border: none;
    padding: 0;
    background: transparent;
    max-width: 100vw;
    max-height: 100vh;
    overflow: visible;
    color: inherit;
}
.processing-overlay::backdrop {
    background: rgba(15, 23, 42, 0.55);
    -webkit-backdrop-filter: blur(6px);
    backdrop-filter: blur(6px);
    animation: fadeIn .2s ease-out;
}
.processing-overlay[open] {
    animation: fadeIn .2s ease-out;
}

.processing-card {
    background: #fff;
    border-radius: 16px;
    padding: 28px 32px;
    max-width: 420px;
    width: 100%;
    text-align: center;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}
.processing-card h2 { margin: 12px 0 6px; }
.processing-card p  { margin: 4px 0; }

.processing-thumb {
    display: block;
    max-width: 220px;
    max-height: 220px;
    margin: 14px auto 10px;
    border-radius: 10px;
    border: 1px solid var(--border);
    object-fit: cover;
}

.spinner {
    width: 54px;
    height: 54px;
    margin: 0 auto;
    border: 5px solid rgba(79, 70, 229, 0.18);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.9s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}
@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
    .spinner { animation-duration: 2.5s; }
    .processing-overlay { animation: none; }
}

/* --- Lightbox (click-to-zoom image popup) --- */
.lightbox {
    border: none;
    padding: 0;
    background: transparent;
    max-width: 100vw;
    max-height: 100vh;
    overflow: visible;
    color: #fff;
}
.lightbox::backdrop {
    background: rgba(0, 0, 0, 0.85);
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
}
.lightbox[open] {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    animation: fadeIn .15s ease-out;
}
.lightbox img {
    max-width: 92vw;
    max-height: 85vh;
    border-radius: 8px;
    box-shadow: 0 10px 40px rgba(0,0,0,.5);
    background: #fff;
}
.lightbox-caption {
    margin-top: 10px;
    color: #e5e7eb;
    text-align: center;
    font-size: .95rem;
    max-width: 92vw;
}
.lightbox-close {
    position: fixed;
    top: 16px;
    right: 20px;
    background: rgba(255,255,255,.15);
    color: #fff;
    border: 1px solid rgba(255,255,255,.35);
    border-radius: 999px;
    width: 40px;
    height: 40px;
    font-size: 1.2rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}
.lightbox-close:hover { background: rgba(255,255,255,.25); }
