/* =========================================================
   Invoicent - Core Stylesheet
   Dark/light-ready SaaS dashboard architecture
   ========================================================= */

:root {
    --color-bg: #f4f6fb;
    --color-surface: #ffffff;
    --color-border: #e3e7ef;
    --color-text: #1f2430;
    --color-text-muted: #6b7280;
    --color-primary: #4f46e5;
    --color-primary-dark: #4338ca;
    --color-primary-light: #eef2ff;
    --color-success: #10b981;
    --color-danger: #ef4444;
    --color-warning: #f59e0b;
    --sidebar-width: 260px;
    --radius: 10px;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
    --shadow-md: 0 4px 14px rgba(15, 23, 42, 0.08);
}

[data-theme="dark"] {
    --color-bg: #10131c;
    --color-surface: #171b26;
    --color-border: #262c3b;
    --color-text: #e7e9ee;
    --color-text-muted: #9aa1b1;
    --color-primary: #6366f1;
    --color-primary-dark: #4f46e5;
    --color-primary-light: #1e2135;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--color-bg);
    color: var(--color-text);
    font-family: 'Segoe UI', Roboto, -apple-system, BlinkMacSystemFont, sans-serif;
    font-size: 15px;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; }

/* ---------- Auth pages (login/signup) ---------- */
.auth-wrapper {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: linear-gradient(135deg, var(--color-primary) 0%, #7c3aed 100%);
}

.auth-card {
    width: 100%;
    max-width: 420px;
    background: var(--color-surface);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    padding: 40px 36px;
}

.auth-card .brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 22px;
    font-weight: 700;
    margin-bottom: 6px;
    color: var(--color-primary);
}

.auth-card p.subtitle {
    color: var(--color-text-muted);
    margin-top: 0;
    margin-bottom: 28px;
}

.form-group { margin-bottom: 18px; }
.form-group label {
    display: block;
    font-weight: 600;
    font-size: 13px;
    margin-bottom: 6px;
    color: var(--color-text);
}
.form-control {
    width: 100%;
    padding: 11px 14px;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    background: var(--color-bg);
    color: var(--color-text);
    font-size: 14px;
    transition: border-color .15s ease, box-shadow .15s ease;
}
.form-control:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px var(--color-primary-light);
}
textarea.form-control { resize: vertical; min-height: 80px; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 11px 20px;
    border-radius: 8px;
    border: none;
    font-weight: 600;
    font-size: 14px;
    transition: transform .05s ease, background .15s ease, box-shadow .15s ease;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover { background: var(--color-primary-dark); }
.btn-block { width: 100%; }
.btn-outline { background: transparent; border: 1px solid var(--color-border); color: var(--color-text); }
.btn-outline:hover { border-color: var(--color-primary); color: var(--color-primary); }
.btn-danger { background: var(--color-danger); color: #fff; }
.btn-success { background: var(--color-success); color: #fff; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-icon { padding: 8px 10px; }

.form-error { color: var(--color-danger); font-size: 13px; margin-top: 4px; }
.alert {
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 18px;
    font-size: 14px;
}
.alert-error { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; }
.alert-success { background: #ecfdf5; color: #047857; border: 1px solid #a7f3d0; }
.alert-warning { background: #fffbeb; color: #92400e; border: 1px solid #fde68a; }

.auth-footer-link { text-align: center; margin-top: 20px; font-size: 14px; color: var(--color-text-muted); }
.auth-footer-link a { color: var(--color-primary); font-weight: 600; }

/* ---------- App shell (sidebar + main) ---------- */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-width);
    background: var(--color-surface);
    border-right: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0; bottom: 0; left: 0;
    z-index: 40;
    transition: transform .2s ease;
}
.sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 18px;
    color: var(--color-primary);
    padding: 22px 22px 18px;
}
.sidebar-business {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 22px 20px;
    border-bottom: 1px solid var(--color-border);
}
.sidebar-logo { width: 34px; height: 34px; border-radius: 8px; object-fit: cover; }
.sidebar-logo--placeholder {
    display: flex; align-items: center; justify-content: center;
    background: var(--color-primary-light); color: var(--color-primary);
}
.sidebar-business-name { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.sidebar-nav { display: flex; flex-direction: column; padding: 14px 12px; gap: 2px; flex: 1; }
.sidebar-nav a {
    display: flex; align-items: center; gap: 12px;
    padding: 11px 14px;
    border-radius: 8px;
    color: var(--color-text-muted);
    font-weight: 500;
    font-size: 14px;
}
.sidebar-nav a i { width: 18px; text-align: center; }
.sidebar-nav a:hover { background: var(--color-primary-light); color: var(--color-primary); }
.sidebar-nav a.active { background: var(--color-primary); color: #fff; }
.sidebar-logout { margin-top: auto; color: var(--color-danger) !important; }

.sidebar-toggle {
    display: none;
    position: fixed; top: 14px; left: 14px; z-index: 50;
    background: var(--color-surface); border: 1px solid var(--color-border);
    color: var(--color-text);
    border-radius: 8px; padding: 8px 11px; box-shadow: var(--shadow-sm);
}

.main-content {
    margin-left: var(--sidebar-width);
    flex: 1;
    padding: 28px 32px 60px;
    min-width: 0;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 26px;
    gap: 16px;
    flex-wrap: wrap;
}
.topbar h1 { font-size: 22px; margin: 0; }
.topbar .actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* ---------- Cards / stats ---------- */
.card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-sm);
    padding: 22px;
}
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 18px;
    margin-bottom: 26px;
}
.stat-card { display: flex; align-items: center; gap: 16px; }
.stat-icon {
    width: 46px; height: 46px; border-radius: 10px;
    display: flex; align-items: center; justify-content: center;
    background: var(--color-primary-light); color: var(--color-primary); font-size: 18px;
}
.stat-value { font-size: 22px; font-weight: 700; }
.stat-label { font-size: 13px; color: var(--color-text-muted); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table.data-table { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data-table th, table.data-table td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--color-border); }
table.data-table th { color: var(--color-text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
table.data-table tbody tr:hover { background: var(--color-primary-light); }
.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge-success { background: #ecfdf5; color: #047857; }
.badge-warning { background: #fffbeb; color: #92400e; }
.badge-muted { background: var(--color-border); color: var(--color-text-muted); }

.filters-bar { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 18px; }
.filters-bar input, .filters-bar select { min-width: 160px; }

/* ---------- Invoice builder ---------- */
.invoice-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; }
@media (max-width: 900px) { .invoice-grid { grid-template-columns: 1fr; } }

.line-items-table { width: 100%; border-collapse: collapse; margin-top: 10px; }
.line-items-table th { text-align: left; font-size: 12px; color: var(--color-text-muted); padding: 6px 8px; }
.line-items-table td { padding: 6px 8px; }
.line-items-table tbody tr:first-child td { padding-top: 16px; }
.line-items-table input { width: 100%; padding: 8px 10px; }
.line-items-table td.col-desc { width: 40%; }
.line-items-table td.col-qty, .line-items-table td.col-rate { width: 15%; }
.line-items-table td.col-amount { width: 18%; }
.line-items-table td.col-action { width: 5%; text-align: center; }
.remove-row-btn { color: var(--color-danger); background: none; border: none; font-size: 15px; }

.totals-box { margin-top: 14px; margin-left: auto; width: 260px; }
.totals-row { display: flex; justify-content: space-between; padding: 6px 0; font-size: 14px; }
.totals-row.grand-total { font-weight: 700; font-size: 16px; border-top: 1px solid var(--color-border); margin-top: 6px; padding-top: 10px; }

/* ---------- A5 preview ---------- */
.a5-preview-outer { background: #e5e7eb; padding: 24px; border-radius: var(--radius); display: flex; flex-direction: column; align-items: center; gap: 16px; }
.a5-page {
    width: 420px;
    min-height: 595px;
    background: #fff;
    color: #1a1a1a;
    box-shadow: 0 2px 10px rgba(0,0,0,.15);
    padding: 28px;
    font-size: 12px;
}
.a5-page + .a5-page { margin-top: 12px; }
.a5-header { display: flex; justify-content: space-between; gap: 10px; border-bottom: 2px solid #4f46e5; padding-bottom: 10px; margin-bottom: 10px; }
.a5-header img { max-height: 46px; max-width: 110px; object-fit: contain; }
.a5-parties { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 12px; font-size: 11px; }
.a5-parties h4 { margin: 0 0 4px; font-size: 11px; text-transform: uppercase; color: #6b7280; }
.a5-table { width: 100%; border-collapse: collapse; font-size: 11px; }
.a5-table th, .a5-table td { border-bottom: 1px solid #e5e7eb; padding: 5px 4px; text-align: left; }
.a5-table th { background: #f3f4f6; padding-bottom: 8px; }
.a5-table tbody tr:first-child td { padding-top: 8px; }
.a5-totals { margin-top: 10px; margin-left: auto; width: 55%; font-size: 11px; }
.a5-terms-note { margin-top: 14px; font-size: 10px; color: #374151; }
.a5-terms-note h4 { margin: 0 0 3px; font-size: 10px; text-transform: uppercase; color: #6b7280; }
.a5-overflow-note { font-size: 10px; color: #b91c1c; text-align: center; margin-top: 8px; }

/* ---------- Misc ---------- */
.empty-state { text-align: center; padding: 60px 20px; color: var(--color-text-muted); }
.empty-state i { font-size: 40px; margin-bottom: 14px; color: var(--color-border); }
.logo-upload-preview { width: 90px; height: 90px; border-radius: 10px; object-fit: cover; border: 1px solid var(--color-border); }
.toast-container { position: fixed; top: 20px; right: 20px; z-index: 100; display: flex; flex-direction: column; gap: 10px; }
.toast { padding: 12px 18px; border-radius: 8px; box-shadow: var(--shadow-md); font-size: 14px; color: #fff; animation: slideIn .2s ease; }
.toast-success { background: var(--color-success); }
.toast-error { background: var(--color-danger); }
@keyframes slideIn { from { transform: translateX(30px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
.modal-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: none; align-items: center; justify-content: center; z-index: 60; }
.modal-backdrop.open { display: flex; }
.modal { background: var(--color-surface); border-radius: var(--radius); padding: 26px; width: 100%; max-width: 420px; box-shadow: var(--shadow-md); }

/* ---------- Theme toggle switch (settings.php) ---------- */
.theme-switch {
    position: relative;
    width: 56px;
    height: 30px;
    border-radius: 999px;
    border: 1px solid var(--color-border);
    background: var(--color-primary-light);
    padding: 0;
    flex-shrink: 0;
    transition: background .2s ease;
}
.theme-switch-thumb {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--color-surface);
    box-shadow: var(--shadow-sm);
    transition: transform .2s ease;
}
.theme-switch[aria-pressed="true"] { background: var(--color-primary); }
.theme-switch[aria-pressed="true"] .theme-switch-thumb { transform: translateX(26px); }
.theme-switch-icon {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    font-size: 12px;
    pointer-events: none;
}
.theme-switch-icon-sun { left: 7px; color: var(--color-warning); }
.theme-switch-icon-moon { right: 7px; color: #fff; opacity: .85; }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
    .sidebar { transform: translateX(-100%); }
    .sidebar.open { transform: translateX(0); }
    .main-content { margin-left: 0; padding: 70px 18px 40px; }
    .sidebar-toggle { display: inline-flex; align-items: center; justify-content: center; }
}

/* Invoice line items: below ~640px a 4-column table + delete button has no
   room to breathe, so each row becomes a stacked card with full-width
   fields and visible labels instead of squeezed table columns. */
@media (max-width: 640px) {
    .line-items-table thead { display: none; }
    .line-items-table, .line-items-table tbody, .line-items-table tr, .line-items-table td {
        display: block;
        width: 100%;
    }
    /* These need to match (or beat) the specificity of the desktop
       column-width rules above (.line-items-table td.col-desc etc.),
       otherwise those percentage widths (15%/18%/40%) keep winning here
       and each cell stays tiny even though display:block was applied. */
    .line-items-table td.col-desc,
    .line-items-table td.col-qty,
    .line-items-table td.col-rate,
    .line-items-table td.col-amount {
        width: 100%;
    }
    .line-items-table tbody tr:first-child td { padding-top: 6px; }
    .line-items-table tr {
        position: relative;
        border: 1px solid var(--color-border);
        border-radius: 10px;
        padding: 14px 44px 14px 14px;
        margin-bottom: 12px;
        background: var(--color-surface);
    }
    .line-items-table td {
        padding: 6px 0;
    }
    .line-items-table td[data-label]::before {
        content: attr(data-label);
        display: block;
        font-size: 11px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: .03em;
        color: var(--color-text-muted);
        margin-bottom: 4px;
    }
    .line-items-table td.col-desc input,
    .line-items-table td.col-qty input,
    .line-items-table td.col-rate input,
    .line-items-table td.col-amount input {
        width: 100%;
        font-size: 16px; /* prevents iOS auto-zoom on focus */
        padding: 10px 12px;
    }
    .line-items-table td.col-action {
        position: absolute;
        top: 10px;
        right: 8px;
        padding: 0;
        width: auto;
    }
    .line-items-table .remove-row-btn { font-size: 20px; }
}
