:root {
    --bg: #f4f5f7;
    --surface: #ffffff;
    --border: #e4e6eb;
    --text: #1c1e21;
    --text-muted: #6b7280;
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --primary-light: #eef2ff;
    --success: #16a34a;
    --success-light: #ecfdf3;
    --warning: #d97706;
    --warning-light: #fffbeb;
    --danger: #dc2626;
    --danger-light: #fef2f2;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 1px 3px rgba(16, 24, 40, 0.06);
    --sidebar-w: 230px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

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

h1 { font-size: 1.4rem; margin: 0 0 1rem; }
h2 { font-size: 1.05rem; margin: 0 0 0.25rem; }

/* ---------- App shell ---------- */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    background: var(--surface);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    padding: 1.25rem 1rem;
    position: sticky;
    top: 0;
    height: 100vh;
}

.brand { display: flex; align-items: center; gap: 0.6rem; padding: 0 0.4rem 1.5rem; }
.brand-mark {
    width: 32px; height: 32px; border-radius: 8px;
    background: var(--primary); color: #fff; font-weight: 700;
    display: flex; align-items: center; justify-content: center; font-size: 0.95rem;
}
.brand-name { font-weight: 700; font-size: 1.05rem; }

.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-link {
    display: flex; align-items: center; gap: 0.6rem;
    padding: 0.55rem 0.7rem; border-radius: 8px;
    color: var(--text); font-size: 0.92rem; font-weight: 500;
}
.nav-link:hover { background: var(--bg); text-decoration: none; }
.nav-link.active { background: var(--primary-light); color: var(--primary-dark); }
.nav-icon { font-size: 1rem; width: 1.2rem; text-align: center; }

.sidebar-footer { border-top: 1px solid var(--border); padding-top: 0.9rem; margin-top: 0.9rem; }
.user-chip { padding: 0 0.4rem 0.6rem; }
.user-name { font-weight: 600; font-size: 0.88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-email { font-size: 0.78rem; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.main { flex: 1; min-width: 0; }

.topbar {
    display: none;
    align-items: center;
    gap: 0.75rem;
    padding: 0.8rem 1rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}
.menu-toggle {
    background: none; border: none; font-size: 1.2rem; cursor: pointer; padding: 0.2rem 0.4rem;
}
.topbar-title { font-weight: 600; }

.content { padding: 1.75rem 2rem; max-width: 1100px; }

/* ---------- Guest (login/register) ---------- */
.guest-shell {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
}
.guest-card {
    width: 100%;
    max-width: 380px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 2rem;
}
.guest-brand { display: flex; align-items: center; gap: 0.6rem; margin-bottom: 1.5rem; justify-content: center; }
.guest-card h1 { text-align: center; font-size: 1.25rem; }
.guest-switch { text-align: center; margin-top: 1rem; font-size: 0.9rem; color: var(--text-muted); }
.guest-alert { max-width: 380px; width: 100%; margin-bottom: 1rem; }

/* ---------- Forms ---------- */
.form { display: flex; flex-direction: column; gap: 1rem; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.form-grid .span-2 { grid-column: span 2; }

label { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.85rem; font-weight: 600; color: var(--text); }
label.checkbox-label { flex-direction: row; align-items: center; font-weight: 500; gap: 0.5rem; }

input[type=text], input[type=email], input[type=password], input[type=date], input[type=search], input[type=file], select, textarea {
    font-family: inherit;
    font-size: 0.92rem;
    font-weight: 400;
    padding: 0.55rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    width: 100%;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}
textarea { resize: vertical; font-family: inherit; }
small { font-weight: 400; color: var(--text-muted); }

.form-actions { display: flex; gap: 0.6rem; margin-top: 0.25rem; }

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    padding: 0.55rem 1rem;
    border-radius: 8px;
    border: 1px solid transparent;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.12s ease, border-color 0.12s ease;
}
.btn:hover { text-decoration: none; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: var(--danger-light); color: var(--danger); }
.btn-danger:hover { background: #fde2e2; }
.btn-block { width: 100%; }
.btn-icon {
    background: none; border: none; color: var(--text-muted); cursor: pointer;
    font-size: 0.9rem; padding: 0.2rem 0.4rem; border-radius: 6px;
}
.btn-icon:hover { background: var(--danger-light); color: var(--danger); }

/* ---------- Cards / layout ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1.25rem;
    margin-bottom: 1.25rem;
}
.card-form h2 { margin-bottom: 0.2rem; }
.hint { font-size: 0.85rem; color: var(--text-muted); margin: 0 0 1rem; }

.settings-grid { display: grid; grid-template-columns: 1fr; gap: 1.25rem; max-width: 640px; }

.page-header {
    display: flex; align-items: center; justify-content: space-between;
    gap: 1rem; margin-bottom: 1.25rem; flex-wrap: wrap;
}
.back-link { font-size: 0.88rem; font-weight: 600; color: var(--text-muted); }
.invoice-actions, .page-header-actions { display: flex; gap: 0.5rem; }
.invoice-number-preview { font-size: 0.85rem; color: var(--text-muted); font-weight: 600; }

.search-form { display: flex; gap: 0.5rem; }
.search-form input, .filters-form select { max-width: 220px; }

.empty-state {
    text-align: center; padding: 3rem 1rem; background: var(--surface);
    border: 1px dashed var(--border); border-radius: var(--radius); color: var(--text-muted);
}
.empty-state p { margin-bottom: 1rem; }

/* ---------- Table ---------- */
.table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.table th {
    text-align: left; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.03em;
    color: var(--text-muted); padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--border);
}
.table td { padding: 0.65rem 0.6rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:last-child td { border-bottom: none; }
.row-link { color: var(--text); font-weight: 600; }
.row-link:hover { color: var(--primary); }
.table-actions { text-align: right; font-size: 0.85rem; }

/* ---------- Badges ---------- */
.badge {
    display: inline-block; padding: 0.2rem 0.6rem; border-radius: 999px;
    font-size: 0.75rem; font-weight: 600;
}
.badge-draft { background: #f1f2f4; color: #52525b; }
.badge-sent { background: #eff6ff; color: #1d4ed8; }
.badge-paid { background: var(--success-light); color: var(--success); }
.badge-overdue { background: var(--danger-light); color: var(--danger); }
.badge-cancelled { background: #f1f2f4; color: #9ca3af; }

.badge-select {
    border: none; border-radius: 999px; font-size: 0.8rem; font-weight: 600;
    padding: 0.35rem 0.8rem; cursor: pointer;
}

/* ---------- Stats ---------- */
.stats-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1rem; margin-bottom: 1.5rem; }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); }
.stat-label { font-size: 0.78rem; color: var(--text-muted); margin-bottom: 0.3rem; }
.stat-value { font-size: 1.25rem; font-weight: 700; }
.stat-success .stat-value { color: var(--success); }
.stat-warning .stat-value { color: var(--warning); }

.catalog-picker { margin-bottom: 0.75rem; }
.catalog-picker select { max-width: 320px; }

/* ---------- Invoice items table ---------- */
.items-table input { padding: 0.4rem 0.5rem; font-size: 0.88rem; }
.items-table .col-desc { width: 34%; }
.items-table .col-unit { width: 10%; }
.items-table .col-num { width: 12%; }
.items-table .col-del { width: 2%; }
.item-line-total { text-align: right; font-weight: 600; white-space: nowrap; }
.items-table td { padding: 0.4rem 0.35rem; }

.totals-box {
    margin-top: 1rem; margin-left: auto; width: 100%; max-width: 280px;
    display: flex; flex-direction: column; gap: 0.35rem;
}
.totals-line { display: flex; justify-content: space-between; font-size: 0.9rem; color: var(--text-muted); }
.totals-grand { font-size: 1.05rem; color: var(--text); padding-top: 0.5rem; border-top: 1px solid var(--border); }

/* ---------- Invoice view ---------- */
.invoice-view-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 1.25rem; gap: 1rem; flex-wrap: wrap; }
.invoice-view-title { font-size: 1.2rem; font-weight: 700; }
.invoice-view-sub { color: var(--text-muted); font-size: 0.9rem; margin-top: 0.2rem; }
.invoice-notes { margin-top: 1.25rem; font-size: 0.9rem; color: var(--text-muted); }

.logo-preview img { max-height: 60px; margin-top: 0.4rem; }

.hidden-form { display: none; }
.muted-hint { color: #c7c9cf; cursor: not-allowed; font-size: 0.85rem; padding: 0.2rem 0.4rem; }

/* ---------- Alerts ---------- */
.alert {
    padding: 0.7rem 1rem; border-radius: 8px; font-size: 0.88rem; margin-bottom: 1rem;
}
.alert-success { background: var(--success-light); color: var(--success); }
.alert-error { background: var(--danger-light); color: var(--danger); }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
    .sidebar { position: fixed; left: 0; top: 0; z-index: 30; transform: translateX(-100%); transition: transform 0.18s ease; box-shadow: 0 0 0 9999px rgba(0,0,0,0); }
    .sidebar.open { transform: translateX(0); box-shadow: 0 0 0 9999px rgba(0,0,0,0.25); }
    .topbar { display: flex; }
    .content { padding: 1.25rem; }
    .form-grid { grid-template-columns: 1fr; }
    .form-grid .span-2 { grid-column: span 1; }
    .stats-row { grid-template-columns: repeat(2, 1fr); }
    .items-table { display: block; overflow-x: auto; }
}
