@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
    --bg: #f7f6fb;
    --surface: #ffffff;
    --surface-alt: #f1eef8;
    --border: #e6e2f0;
    --text: #1c1830;
    --text-muted: #6f6580;

    --primary: #7c3aed;
    --primary-hover: #6d28d9;
    --primary-soft: #f1ebfe;
    --primary-soft-text: #6d28d9;
    --on-primary: #ffffff;

    --green: #15803d;
    --green-soft: #e7f6ec;
    --red: #dc2626;
    --red-soft: #fdeaea;
    --amber: #b45309;
    --amber-soft: #fdf1de;

    --shadow-sm: 0 1px 2px rgba(30, 15, 60, 0.05), 0 1px 3px rgba(30, 15, 60, 0.06);
    --shadow-md: 0 8px 24px rgba(30, 15, 60, 0.10);
    --radius-sm: 7px;
    --radius-md: 10px;
    --radius-lg: 16px;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --bg: #0c0a13;
        --surface: #16131f;
        --surface-alt: #1e1a2b;
        --border: #2b2538;
        --text: #f1eef7;
        --text-muted: #a79fb8;

        --primary: #9163f5;
        --primary-hover: #a78bfa;
        --primary-soft: #291f42;
        --primary-soft-text: #c7b6fb;
        --on-primary: #ffffff;

        --green: #4ade80;
        --green-soft: #163524;
        --red: #f87171;
        --red-soft: #3a1a1a;
        --amber: #fbbf24;
        --amber-soft: #3a2c0f;

        --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
        --shadow-md: 0 10px 28px rgba(0, 0, 0, 0.5);
    }
}

:root[data-theme="dark"] {
    --bg: #0c0a13;
    --surface: #16131f;
    --surface-alt: #1e1a2b;
    --border: #2b2538;
    --text: #f1eef7;
    --text-muted: #a79fb8;

    --primary: #9163f5;
    --primary-hover: #a78bfa;
    --primary-soft: #291f42;
    --primary-soft-text: #c7b6fb;
    --on-primary: #ffffff;

    --green: #4ade80;
    --green-soft: #163524;
    --red: #f87171;
    --red-soft: #3a1a1a;
    --amber: #fbbf24;
    --amber-soft: #3a2c0f;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 10px 28px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    font-family: 'Inter', -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 21px; font-weight: 700; margin: 0 0 16px; letter-spacing: -0.01em; }
h2 { font-size: 15.5px; font-weight: 600; margin: 0 0 12px; }
h3 { font-size: 13.5px; font-weight: 600; margin: 0 0 8px; }

/* ---- App shell / sidebar ---- */

.app-shell {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: 250px;
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
    z-index: 40;
    transition: transform 0.2s ease;
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 18px 16px;
    border-bottom: 1px solid var(--border);
    min-height: 32px;
}

.sidebar-brand .mark {
    width: 30px;
    height: 30px;
    border-radius: 9px;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
    flex-shrink: 0;
}

.sidebar-brand .name {
    font-weight: 700;
    font-size: 14.5px;
    color: var(--text);
    letter-spacing: -0.01em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-close {
    display: none;
    margin-left: auto;
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px;
}

.sidebar-nav {
    flex: 1;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 9px 12px;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-size: 13.5px;
    font-weight: 500;
}

.sidebar-nav a:hover {
    background: var(--surface-alt);
    color: var(--text);
    text-decoration: none;
}

.sidebar-nav a.active {
    background: var(--primary-soft);
    color: var(--primary-soft-text);
}

.sidebar-nav svg {
    width: 17px;
    height: 17px;
    flex-shrink: 0;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.sidebar-footer {
    padding: 12px;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.sidebar-user {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 8px;
}

.sidebar-user .who { display: flex; flex-direction: column; min-width: 0; }
.sidebar-user .who .n { font-size: 13px; font-weight: 600; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar-user .who .r { font-size: 11px; color: var(--text-muted); text-transform: capitalize; }
.sidebar-user a.logout-link { color: var(--text-muted); display: flex; align-items: center; padding: 4px; border-radius: 6px; }
.sidebar-user a.logout-link:hover { color: var(--red); background: var(--red-soft); text-decoration: none; }
.sidebar-user a.logout-link svg { width: 16px; height: 16px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.theme-toggle {
    display: flex;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 3px;
    gap: 2px;
}

.theme-toggle button {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px 0;
    border: none;
    background: none;
    color: var(--text-muted);
    border-radius: 999px;
    cursor: pointer;
}

.theme-toggle button svg {
    width: 15px;
    height: 15px;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.9;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.theme-toggle button.active {
    background: var(--surface);
    color: var(--primary);
    box-shadow: var(--shadow-sm);
}

.mobile-topbar {
    display: none;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 30;
}

.mobile-topbar button {
    background: none;
    border: none;
    color: var(--text);
    cursor: pointer;
    padding: 4px;
}

.mobile-topbar button svg {
    width: 22px;
    height: 22px;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.mobile-topbar .name { font-weight: 700; font-size: 14.5px; }

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(10, 5, 20, 0.5);
    z-index: 39;
}

.main-column {
    flex: 1;
    min-width: 0;
}

.container {
    max-width: 1220px;
    margin: 0 auto;
    padding: 30px 32px 60px;
}

.page-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 18px;
    flex-wrap: wrap;
    gap: 10px;
}

/* ---- Shared components ---- */

.flash {
    background: var(--green-soft);
    border: 1px solid transparent;
    color: var(--green);
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    margin-bottom: 16px;
    font-size: 13px;
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    padding: 20px;
    margin-bottom: 20px;
}

.stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 14px;
    margin-bottom: 20px;
}

.stat-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    padding: 16px 18px;
}

.stat-box .label {
    font-size: 11.5px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-weight: 600;
}

.stat-box .value {
    font-size: 23px;
    font-weight: 700;
    margin-top: 6px;
    letter-spacing: -0.01em;
}

.stat-box.profit .value { color: var(--green); }
.stat-box.profit.negative .value { color: var(--red); }

table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
}

th, td {
    text-align: left;
    padding: 11px 14px;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
}

th {
    background: var(--surface-alt);
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    font-size: 10.5px;
    letter-spacing: 0.05em;
}

tbody tr:hover { background: var(--surface-alt); }

.table-wrap {
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    overflow-x: auto;
    box-shadow: var(--shadow-sm);
}

.num { text-align: right; font-variant-numeric: tabular-nums; }

.actions-col { text-align: right; white-space: nowrap; }
.actions-col a, .actions-col button.link { margin-left: 12px; font-size: 13px; }

button.link {
    background: none;
    border: none;
    padding: 0;
    color: var(--red);
    cursor: pointer;
    font-size: 13px;
    font-family: inherit;
}
button.link:hover { text-decoration: underline; }

form.inline { display: inline; }

.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px;
}

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 12px; color: var(--text-muted); font-weight: 600; }
.field .hint { font-size: 11px; color: var(--text-muted); }

input, select, textarea {
    padding: 9px 11px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-family: inherit;
    color: var(--text);
    background: var(--surface);
}

input::placeholder, textarea::placeholder { color: var(--text-muted); }

input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-soft);
}

textarea { resize: vertical; min-height: 60px; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--primary);
    color: var(--on-primary);
    border: none;
    padding: 9px 18px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
}
.btn:hover { background: var(--primary-hover); text-decoration: none; color: var(--on-primary); }
.btn.secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn.secondary:hover { background: var(--surface-alt); color: var(--text); }
.btn.danger { background: var(--red); }
.btn.danger:hover { filter: brightness(0.9); }

.form-actions { margin-top: 16px; display: flex; gap: 10px; }

.filters {
    display: flex;
    gap: 12px;
    align-items: flex-end;
    flex-wrap: wrap;
    margin-bottom: 16px;
}

.badge {
    display: inline-block;
    padding: 3px 9px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 600;
    background: var(--surface-alt);
    color: var(--text-muted);
}
.badge.owner { background: var(--amber-soft); color: var(--amber); }
.badge.staff { background: var(--primary-soft); color: var(--primary-soft-text); }

.muted { color: var(--text-muted); }
.empty { padding: 34px; text-align: center; color: var(--text-muted); }

.owner-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 16px;
}

/* ---- Login ---- */

.login-wrap {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    background-image: radial-gradient(circle at 15% 15%, var(--primary-soft), transparent 45%),
        radial-gradient(circle at 85% 85%, var(--primary-soft), transparent 45%);
    padding: 20px;
}

.login-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    padding: 34px 32px;
    width: 340px;
}

.login-box .mark {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    background: var(--primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 18px;
    margin: 0 auto 16px;
}

.login-box h1 {
    text-align: center;
    margin-bottom: 22px;
    font-size: 18px;
}

.login-box .field { margin-bottom: 14px; }
.login-box .btn { width: 100%; margin-top: 8px; padding: 10px 18px; }

.error-box {
    background: var(--red-soft);
    border: 1px solid transparent;
    color: var(--red);
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    margin-bottom: 14px;
    font-size: 13px;
}

.invoice-items-table input { width: 100%; }
.invoice-items-table td { vertical-align: top; }
.invoice-total-row td { font-weight: 600; }

.line-item-remove { color: var(--red); cursor: pointer; font-size: 13px; }

.tabs {
    display: flex;
    gap: 4px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 20px;
}
.tabs a {
    padding: 10px 14px;
    font-size: 13px;
    color: var(--text-muted);
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    font-weight: 500;
}
.tabs a.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    font-weight: 600;
}
.tabs a:hover { text-decoration: none; color: var(--text); }

@media (max-width: 900px) {
    .mobile-topbar { display: flex; }
    .sidebar {
        position: fixed;
        left: 0;
        top: 0;
        transform: translateX(-100%);
        box-shadow: var(--shadow-md);
    }
    body.nav-open .sidebar { transform: translateX(0); }
    body.nav-open .sidebar-backdrop { display: block; }
    .sidebar-close { display: block; }
    .container { padding: 22px 18px 50px; }
}

@media print {
    .topbar, .no-print { display: none !important; }
}