:root {
    --bg: #f3f6f8;
    --surface: #ffffff;
    --surface-2: #f7f9fb;
    --border: #dde3e8;
    --text: #1f2d3a;
    --muted: #64748b;
    --primary: #1d6fa5;
    --primary-hover: #185c89;
    --primary-soft: #e6f1f8;
    --success: #1f8a57;
    --success-hover: #19734a;
    --success-soft: #e5f4ec;
    --warn: #b7791f;
    --warn-soft: #fdf3e1;
    --danger: #c0392b;
    --danger-hover: #a53125;
    --danger-soft: #fbe9e7;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(16, 38, 58, .06), 0 1px 3px rgba(16, 38, 58, .08);
    color-scheme: light;
}

* { box-sizing: border-box; }

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

h1 { font-size: 1.6rem; margin: 0 0 .25rem; }
h2 { font-size: 1.15rem; margin: 0 0 1rem; }
h3 { font-size: 1rem; }
a { color: var(--primary); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.nowrap { white-space: nowrap; }
.center { text-align: center; }
.inline { display: inline; margin: 0; }

/* ------------------------------------------------ layout */
.container { max-width: 1100px; margin: 0 auto; padding: 24px 16px 60px; }
.container.wide { max-width: 1600px; }

.topbar {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 0 20px;
    min-height: 60px;
    background: #123b5a;
    color: #fff;
    flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; font-weight: 600; }
.nav { display: flex; align-items: center; gap: 4px; flex: 1; }
.nav a {
    color: #d7e6f2;
    text-decoration: none;
    padding: 8px 12px;
    border-radius: 6px;
}
.nav a:hover { background: rgba(255, 255, 255, .08); color: #fff; }
.nav a.active { background: rgba(255, 255, 255, .15); color: #fff; }
.nav-spacer { flex: 1; }
.user-link { display: flex; flex-direction: column; line-height: 1.2; text-align: right; }
.user-link small { color: #9fc0da; font-size: .75rem; }
.nav-toggle, .nav-toggle-label { display: none; }

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

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 20px;
}
.card.flush { padding: 0; overflow: hidden; }
.card.empty { text-align: center; padding: 48px 20px; }
.error-code { font-size: 3rem; font-weight: 700; color: var(--primary); margin: 0; }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 20px; }
.grid-2 .card { margin: 0; }

/* ------------------------------------------------ stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat {
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 4px solid var(--primary);
    border-radius: var(--radius);
    padding: 14px 16px;
    display: flex;
    flex-direction: column;
}
.stat.good { border-left-color: var(--success); }
.stat.warn { border-left-color: var(--warn); }
.stat-value { font-size: 1.6rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: .85rem; }

/* ------------------------------------------------ forms */
label { display: block; font-weight: 600; font-size: .9rem; margin-bottom: 14px; }
label.check { display: inline-flex; align-items: center; gap: 6px; margin: 0; font-weight: 500; }
input[type=text], input[type=password], input[type=email], input[type=number], input[type=tel],
input[type=search], select {
    display: block;
    width: 100%;
    margin-top: 5px;
    padding: 9px 11px;
    font: inherit;
    font-weight: 400;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
}
input:focus, select:focus { outline: 2px solid var(--primary-soft); border-color: var(--primary); }
input:disabled { background: var(--surface-2); color: var(--muted); }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--success); cursor: pointer; }
input[type=checkbox]:disabled { cursor: not-allowed; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
.span-2 { grid-column: 1 / -1; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px; padding: 14px 16px; }
.toolbar input[type=search] { max-width: 320px; margin: 0; flex: 1 1 220px; }
.toolbar select { width: auto; margin: 0; display: inline-block; }
.toolbar label { margin: 0; }
.invoice-form { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-left: auto; }

.inline-form { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.inline-form label { margin: 0; flex: 1 1 180px; }

/* ------------------------------------------------ buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 9px 16px;
    font: inherit;
    font-weight: 600;
    font-size: .9rem;
    line-height: 1.2;
    color: #fff;
    background: var(--primary);
    border: 1px solid transparent;
    border-radius: 6px;
    text-decoration: none;
    cursor: pointer;
    white-space: nowrap;
}
.btn:hover { background: var(--primary-hover); }
.btn-success { background: var(--success); }
.btn-success:hover { background: var(--success-hover); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: var(--danger-hover); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--surface-2); }
.btn-ghost { background: transparent; color: #d7e6f2; border-color: rgba(255, 255, 255, .25); }
.btn-ghost:hover { background: rgba(255, 255, 255, .1); color: #fff; }
.btn-small { padding: 5px 10px; font-size: .8rem; }
.btn-block { width: 100%; }
.btn-primary { background: var(--primary); }

/* ------------------------------------------------ tables */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; background: var(--surface); }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { background: var(--surface-2); font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); white-space: nowrap; }
th.center, td.center { text-align: center; }
th a { color: inherit; text-decoration: none; }
th a::after { content: " ↕"; opacity: .5; }
th a.sorted { color: var(--primary); }
th a.sorted::after { content: " ↓"; opacity: 1; }
tbody tr:hover { background: #fafcfd; }
tbody tr:last-child td { border-bottom: 0; }
tr.inactive td { opacity: .55; }
table.members td { font-size: .9rem; }
td input[type=text], td select { margin: 0; min-width: 130px; padding: 6px 8px; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.row-actions form { margin: 0; }

.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    font-size: .75rem;
    font-weight: 600;
    background: var(--surface-2);
    color: var(--muted);
    white-space: nowrap;
}
.badge-main { background: var(--primary-soft); color: var(--primary); }
.badge-extra { background: var(--warn-soft); color: var(--warn); }

/* ------------------------------------------------ alerts & toast */
.alert { padding: 12px 16px; border-radius: 8px; margin-bottom: 16px; border: 1px solid transparent; }
.alert-success { background: var(--success-soft); color: #14603c; border-color: #bfe3cf; }
.alert-error { background: var(--danger-soft); color: #8c2a1f; border-color: #f2c4bd; }
.alert-warning, .alert-message { background: var(--warn-soft); color: #7a5214; border-color: #f0d9a8; }
.alert-password { background: #fff8db; color: #5c4600; border: 2px dashed #e0b400; font-weight: 600; }

.toast {
    position: fixed;
    left: 50%;
    bottom: 24px;
    transform: translate(-50%, 20px);
    background: #1f2d3a;
    color: #fff;
    padding: 10px 18px;
    border-radius: 8px;
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s, transform .25s;
    z-index: 200;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }

/* ------------------------------------------------ dialog */
.dialog {
    width: min(680px, calc(100vw - 32px));
    border: 0;
    border-radius: 12px;
    padding: 0;
    box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
}
.dialog::backdrop { background: rgba(15, 30, 45, .5); }
.dialog form#member-form { padding: 22px 24px; }
.dialog-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.dialog-head h2 { margin: 0; }
.dialog-foot { display: flex; gap: 8px; margin-top: 8px; }
.close-btn { background: none; border: 0; font-size: 1.6rem; line-height: 1; cursor: pointer; color: var(--muted); }

/* ------------------------------------------------ auth pages */
.auth-page { background: linear-gradient(160deg, #123b5a 0%, #1d6fa5 55%, #5fa8c9 100%); min-height: 100vh; }
.auth-wrap { display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; padding: 24px 16px; }
.auth-wrap .alert { width: 100%; max-width: 400px; }
.auth-card { width: 100%; max-width: 400px; padding: 32px; }
.auth-card h1 { font-size: 1.35rem; }
.auth-logo { display: block; margin-bottom: 12px; }

/* ------------------------------------------------ invoices */
.email-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 28px;
    max-width: 800px;
    box-shadow: var(--shadow);
}
.email-header { background: var(--surface-2); border-bottom: 1px solid var(--border); padding: 14px 24px; font-size: .9rem; color: var(--muted); border-radius: var(--radius) var(--radius) 0 0; }
.email-header strong { color: var(--text); display: inline-block; width: 60px; }
.email-body { padding: 20px 24px; line-height: 1.6; }
.invoice-box { background: var(--bg); border-left: 4px solid var(--success); padding: 16px 20px; margin: 20px 0; border-radius: 0 6px 6px 0; }
.invoice-box h3 { margin: 0 0 8px; color: var(--success); }
.invoice-box hr { border: 0; border-top: 1px solid var(--border); margin: 14px 0; }
table.plain { background: none; width: auto; }
table.plain td { border: 0; padding: 3px 24px 3px 0; }
table.plain td:first-child { font-weight: 600; }
tbody tr:hover table.plain, table.plain tr:hover { background: none; }
.ocr { font-family: "Courier New", Courier, monospace; font-weight: 700; font-size: 1.15em; color: #b34700; letter-spacing: 1px; }

/* ------------------------------------------------ responsive */
@media (max-width: 860px) {
    .nav-toggle-label { display: block; margin-left: auto; font-size: 1.5rem; cursor: pointer; padding: 6px 10px; }
    .nav { display: none; flex-basis: 100%; flex-direction: column; align-items: stretch; padding-bottom: 12px; }
    .nav-toggle:checked ~ .nav { display: flex; }
    .user-link { text-align: left; }
    .invoice-form { margin-left: 0; }
}
@media (max-width: 560px) {
    .form-grid { grid-template-columns: 1fr; }
    .auth-card { padding: 24px; }
}

/* ------------------------------------------------ print */
@media print {
    body { background: #fff; }
    .no-print, .topbar, .alert, .toast { display: none !important; }
    .container { padding: 0; max-width: none; }
    .email-card { box-shadow: none; border: 0; page-break-after: always; max-width: none; }
    .email-header { background: none; }
}
