:root {
    --bg-primary: #f8fafc;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f1f5f9;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --border: #e2e8f0;
    --accent: #22c55e;
    --accent-hover: #16a34a;
    --accent-light: #dcfce7;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
}

[data-theme="dark"] {
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-tertiary: #334155;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --text-muted: #64748b;
    --border: #334155;
    --accent: #22c55e;
    --accent-hover: #4ade80;
    --accent-light: #052e16;
    --danger: #f87171;
    --danger-hover: #ef4444;
    --shadow: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-lg: 0 4px 12px rgba(0,0,0,0.4);
    color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    transition: background 0.2s, color 0.2s;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }

/* Header */
.header {
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 50;
}
.header-inner {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0.75rem 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.logo-link {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text-primary);
}
.logo-link:hover { color: var(--accent); }
.logo-link span { color: var(--accent); }

.nav { display: flex; align-items: center; gap: 0.25rem; }
.nav a, .nav button {
    padding: 0.5rem 0.75rem;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-secondary);
    transition: all 0.15s;
    border: none;
    background: none;
    cursor: pointer;
}
.nav a:hover, .nav button:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}
.nav a.active { background: var(--accent-light); color: var(--accent); }

/* Dark mode toggle */
.theme-toggle {
    width: 36px; height: 36px;
    display: flex; align-items: center; justify-content: center;
    border-radius: 0.5rem;
    font-size: 1.1rem;
}

.nav-toggle {
    display: none;
    width: 38px;
    height: 38px;
    align-items: center;
    justify-content: center;
    border-radius: 0.6rem;
    border: 1px solid var(--border);
    background: var(--bg-secondary);
    color: var(--text-primary);
    cursor: pointer;
    font-size: 1.1rem;
}
.nav-toggle:hover { background: var(--bg-tertiary); }

/* Container */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1.5rem;
}

/* Cards */
.card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 0.75rem;
    box-shadow: var(--shadow);
    padding: 1.5rem;
    margin-bottom: 1.5rem;
}
.card-title {
    font-size: 1.125rem;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--text-primary);
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    border: 1px solid transparent;
    cursor: pointer;
    transition: all 0.15s;
    text-decoration: none;
    white-space: nowrap;
}
.btn-primary {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
}
.btn-primary:hover { background: var(--accent-hover); color: #fff; }
.btn-secondary {
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border-color: var(--border);
}
.btn-secondary:hover { background: var(--border); }
.btn-danger {
    background: var(--danger);
    color: #fff;
    border-color: var(--danger);
}
.btn-danger:hover { background: var(--danger-hover); color: #fff; }
.btn-sm { padding: 0.35rem 0.65rem; font-size: 0.8rem; }
.btn-ghost {
    background: transparent;
    color: var(--text-secondary);
    border-color: transparent;
}
.btn-ghost:hover { background: var(--bg-tertiary); color: var(--text-primary); }

/* Forms */
.form-group { margin-bottom: 1rem; }
.form-label {
    display: block;
    font-size: 0.8125rem;
    font-weight: 500;
    color: var(--text-secondary);
    margin-bottom: 0.35rem;
}
.form-input, .form-select, .form-textarea {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    font-size: 0.875rem;
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: border 0.15s;
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(34,197,94,0.15);
}
.form-textarea { min-height: 80px; resize: vertical; }

.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1rem;
}

/* Table */
.table-wrap { overflow-x: auto; }
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}
th {
    text-align: left;
    padding: 0.65rem 0.75rem;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-bottom: 1px solid var(--border);
}
td {
    padding: 0.65rem 0.75rem;
    border-bottom: 1px solid var(--border);
    color: var(--text-primary);
}
tr:hover td { background: var(--bg-tertiary); }
.text-right { text-align: right; }
.text-center { text-align: center; }
.text-muted { color: var(--text-muted); }
.text-accent { color: var(--accent); font-weight: 600; }

/* Items table (invoice lines) */
.items-table input, .items-table select {
    padding: 0.4rem 0.5rem;
    border: 1px solid var(--border);
    border-radius: 0.375rem;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 0.8125rem;
}
.items-table input:focus, .items-table select:focus {
    outline: none;
    border-color: var(--accent);
}
.items-table .col-desc { width: 40%; }
.items-table .col-qty { width: 10%; }
.items-table .col-unit { width: 12%; }
.items-table .col-price { width: 15%; }
.items-table .col-sum { width: 15%; }
.items-table .col-action { width: 8%; }

/* Totals */
.totals {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 0.35rem;
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid var(--border);
}
.totals-row {
    display: flex;
    gap: 2rem;
    font-size: 0.875rem;
}
.totals-row.total {
    font-weight: 700;
    font-size: 1rem;
    color: var(--accent);
}
.totals-label { color: var(--text-secondary); min-width: 120px; text-align: right; }

/* Invoice view */
.invoice-view {
    background: #fff;
    color: #0f172a;
    padding: 2.5rem;
    max-width: 800px;
    margin: 0 auto;
    border-radius: 0.75rem;
    box-shadow: var(--shadow-lg);
}
.invoice-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 2rem;
    padding-bottom: 1.5rem;
    border-bottom: 2px solid #22c55e;
}
.invoice-company { font-size: 0.8125rem; color: #475569; line-height: 1.6; }
.invoice-company strong { font-size: 1rem; color: #0f172a; }
.invoice-meta { text-align: right; }
.invoice-meta h2 { font-size: 1.5rem; color: #22c55e; margin-bottom: 0.5rem; }
.invoice-meta p { font-size: 0.8125rem; color: #475569; }
.invoice-parties {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
}
.invoice-party h4 {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #94a3b8;
    margin-bottom: 0.5rem;
}
.invoice-party p { font-size: 0.8125rem; color: #475569; line-height: 1.6; }
.invoice-party p strong { color: #0f172a; }
.invoice-table { margin-bottom: 1.5rem; }
.invoice-table th {
    background: #f1f5f9;
    color: #475569;
    padding: 0.5rem 0.75rem;
}
.invoice-table td {
    padding: 0.5rem 0.75rem;
    border-bottom: 1px solid #e2e8f0;
    color: #334155;
}
.invoice-totals {
    margin-left: auto;
    width: 280px;
}
.invoice-totals tr td { border: none; padding: 0.35rem 0.75rem; }
.invoice-totals .grand-total td {
    font-weight: 700;
    font-size: 1.1rem;
    color: #22c55e;
    border-top: 2px solid #22c55e;
    padding-top: 0.5rem;
}
.invoice-notes {
    margin-top: 2rem;
    padding-top: 1rem;
    border-top: 1px solid #e2e8f0;
    font-size: 0.8125rem;
    color: #64748b;
}

/* Badge */
.badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 9999px;
    font-size: 0.7rem;
    font-weight: 600;
}
.badge-green { background: #dcfce7; color: #16a34a; }

/* Toolbar */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
}
.search-box {
    position: relative;
    flex: 1;
    max-width: 350px;
}
.search-box input {
    width: 100%;
    padding: 0.5rem 0.75rem 0.5rem 2.25rem;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    background: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 0.875rem;
}
.search-box input:focus { outline: none; border-color: var(--accent); }
.search-icon {
    position: absolute;
    left: 0.7rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    font-size: 0.875rem;
}

/* Empty state */
.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--text-muted);
}
.empty-state svg { margin-bottom: 1rem; opacity: 0.5; }
.empty-state p { margin-bottom: 1rem; }

/* Alerts */
.alert {
    padding: 0.75rem 1rem;
    border-radius: 0.5rem;
    margin-bottom: 1rem;
    font-size: 0.875rem;
}
.alert-success { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.alert-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }

/* Action bar (view/edit buttons) */
.action-bar {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    align-items: center;
    margin-bottom: 1rem;
}
.action-bar form { margin: 0; }

/* Invoice items controls */
.items-controls {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    flex-wrap: wrap;
}
.items-controls label { white-space: nowrap; }

/* Print styles */
@media print {
    .header, .toolbar, .no-print { display: none !important; }
    body { background: #fff; }
    .container { max-width: 100%; padding: 0; }
    .invoice-view { box-shadow: none; padding: 0; }
}

/* Responsive */
@media (max-width: 768px) {
    .form-row { grid-template-columns: 1fr; }
    .invoice-parties { grid-template-columns: 1fr; }
    .header-inner { flex-wrap: wrap; gap: 0.5rem; }
    .nav-toggle { display: inline-flex; }
    .nav {
        width: 100%;
        display: none;
        flex-direction: column;
        align-items: stretch;
        gap: 0.25rem;
        padding-top: 0.25rem;
    }
    .nav.is-open { display: flex; }
    .nav a, .nav button { width: 100%; justify-content: flex-start; padding: 0.65rem 0.85rem; }
    .nav form { width: 100%; }
    .nav form button { width: 100%; justify-content: flex-start; }
    .toolbar { flex-direction: column; align-items: stretch; }
    .search-box { max-width: 100%; }
    .items-controls { flex-direction: column; align-items: stretch; width: 100%; }
    .items-controls label { white-space: normal; }
    .items-controls .form-select { width: 100% !important; }
    .items-controls .btn { width: 100%; justify-content: center; }
}

@media (max-width: 768px) {
    table.table-card thead { display: none; }
    table.table-card,
    table.table-card tbody,
    table.table-card tr,
    table.table-card td {
        display: block;
        width: 100%;
    }
    table.table-card tr {
        border: 1px solid var(--border);
        border-radius: 0.75rem;
        overflow: hidden;
        margin-bottom: 0.75rem;
        background: var(--bg-secondary);
        box-shadow: var(--shadow);
    }
    table.table-card td {
        display: flex;
        justify-content: space-between;
        gap: 1rem;
        padding: 0.65rem 0.75rem;
        border-bottom: 1px solid var(--border);
        align-items: baseline;
    }
    table.table-card td:last-child { border-bottom: none; }
    table.table-card td::before {
        content: attr(data-label);
        flex: 0 0 auto;
        font-weight: 700;
        color: var(--text-secondary);
        font-size: 0.72rem;
        text-transform: uppercase;
        letter-spacing: 0.04em;
    }
    table.table-card td.td-actions {
        justify-content: flex-start;
        flex-wrap: wrap;
        gap: 0.5rem;
        align-items: center;
    }
    table.table-card td.td-actions::before { content: ""; }
    table.table-card tr:hover td { background: transparent; }

    table.items-table.table-card td {
        flex-direction: column;
        align-items: stretch;
    }
    table.items-table.table-card td::before { margin-bottom: 0.35rem; }
    table.items-table.table-card td.td-actions {
        flex-direction: row;
        align-items: center;
    }
    table.items-table.table-card td.td-actions::before { display: none; }

    .invoice-view table.table-card tr {
        background: #fff;
        border-color: #e2e8f0;
        box-shadow: none;
    }
    .invoice-view table.table-card td {
        border-bottom-color: #e2e8f0;
        color: #334155;
    }
    .invoice-view table.table-card td::before { color: #94a3b8; }
}

@media (max-width: 480px) {
    .container { padding: 1rem; }
    .card { padding: 1rem; }
    .toolbar .btn { width: 100%; justify-content: center; }
    .btn { padding: 0.5rem 0.8rem; }
    .btn-sm { padding: 0.3rem 0.55rem; font-size: 0.78rem; }
    .action-bar { flex-direction: column; align-items: stretch; }
    .action-bar .btn { width: 100%; justify-content: center; }
    table.table-card td { padding: 0.55rem 0.65rem; }
    table.table-card td.td-actions { gap: 0.35rem; }
    table.table-card td.td-actions .btn { padding: 0.28rem 0.5rem; font-size: 0.78rem; }
}
