:root {
    --ink: #1c1914;
    --muted: #6b6258;
    --paper: #f3efe6;
    --panel: #fffdf8;
    --line: #e4d9c8;
    --forest: #24362c;
    --forest-2: #1a2720;
    --copper: #c45c26;
    --copper-dark: #9a4318;
    --good: #2f6f4e;
    --danger: #a33b2b;
    --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    min-height: 100%;
    font-family: "Segoe UI", "Avenir Next", system-ui, sans-serif;
    color: var(--ink);
    background: var(--paper);
}

a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; }
[x-cloak] { display: none !important; }

.guest-body {
    min-height: 100vh;
    background:
        radial-gradient(circle at top right, rgba(196, 92, 38, 0.18), transparent 28%),
        linear-gradient(160deg, #24362c 0%, #1a2720 48%, #c45c26 160%);
}

.login-wrap {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 32px 16px;
}

.login-panel {
    width: min(440px, 100%);
    background: var(--panel);
    border-radius: 22px;
    padding: 0;
    overflow: hidden;
    box-shadow: 0 24px 60px rgba(20, 16, 10, 0.28);
}

.login-body {
    padding: 28px 32px 36px;
}

.login-brand {
    padding: 28px 24px 8px;
    text-align: center;
    background: var(--panel);
}

.login-brand .login-logo {
    width: min(300px, 100%);
    margin: 0 auto;
    display: block;
}

.login-panel.wide { width: min(760px, 100%); }
.login-panel a:not(.btn-primary):not(.btn-ghost) { color: var(--copper); font-weight: 700; }

.eyebrow {
    margin: 0 0 8px;
    text-transform: uppercase;
    letter-spacing: 0.14em;
    font-size: 11px;
    color: var(--copper);
    font-weight: 700;
}

h1, h2 { margin: 0 0 8px; letter-spacing: -0.03em; }
.lede, .muted, .welcome { color: var(--muted); }

.login-form {
    display: grid;
    gap: 14px;
    margin-top: 24px;
}

label {
    display: grid;
    gap: 6px;
    font-size: 13px;
    font-weight: 650;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="search"],
input[type="date"],
input[type="tel"],
input[type="url"],
select,
textarea {
    width: 100%;
    min-width: 0;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 12px 14px;
    background: #fff;
    color: var(--ink);
    /* Prevents iOS zoom that breaks edit focus */
    font-size: 16px;
    line-height: 1.35;
    -webkit-appearance: none;
    appearance: none;
}

textarea { min-height: 90px; resize: vertical; }

input:focus,
select:focus,
textarea:focus {
    outline: 2px solid rgba(196, 92, 38, 0.35);
    border-color: var(--copper);
}

input:focus::placeholder,
textarea:focus::placeholder {
    color: transparent;
}

input::placeholder,
textarea::placeholder {
    color: #a89f94;
    opacity: 1;
}

input[type="file"] {
    padding: 10px 12px;
    background: #fff;
    border: 1px dashed var(--line);
}

.logo-preview {
    display: block;
    max-width: 160px;
    max-height: 80px;
    margin-top: 8px;
    object-fit: contain;
    border-radius: 8px;
    border: 1px solid var(--line);
    background: #fff;
    padding: 6px;
}

.remember {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 500;
}

.btn-primary {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    border: 0;
    background: var(--copper);
    color: #fff;
    border-radius: 999px;
    padding: 12px 18px;
    font-weight: 700;
    cursor: pointer;
    text-decoration: none;
}

a.btn-primary,
a.btn-primary:hover,
a.btn-primary:focus {
    color: #fff;
}

.btn-primary:hover { background: var(--copper-dark); }

.error { color: var(--danger); font-size: 13px; font-weight: 600; }

.auth-links {
    display: grid;
    gap: 12px;
    margin-top: 22px;
    justify-items: start;
}

.auth-links p {
    margin: 0;
    color: var(--muted);
    line-height: 1.4;
}

.auth-home {
    display: inline-flex;
    align-items: center;
    min-height: 40px;
    padding: 8px 0;
    color: var(--copper);
    font-weight: 700;
    text-decoration: none;
    white-space: nowrap;
}

.auth-home:hover {
    text-decoration: underline;
}

.auth-actions {
    flex-wrap: wrap;
    align-items: center;
}

.auth-actions .auth-home {
    margin-left: 4px;
}

.app-shell {
    min-height: 100vh;
    display: grid;
    grid-template-columns: 260px 1fr;
}

.sidebar {
    position: sticky;
    top: 0;
    align-self: start;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    background: linear-gradient(180deg, var(--forest) 0%, var(--forest-2) 100%);
    color: #f4eee4;
    padding: 22px 16px;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    gap: 16px;
    overflow: hidden;
    box-sizing: border-box;
}

.brand {
    display: flex;
    gap: 12px;
    align-items: center;
    min-height: 0;
}

.brand-icon {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    object-fit: cover;
    flex: 0 0 42px;
}

.brand-logo,
.login-logo {
    display: block;
    max-width: 100%;
    height: auto;
}

.brand-mark {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    display: grid;
    place-items: center;
    background: var(--copper);
    font-weight: 800;
}

.brand small,
.sidebar-user small,
.nav-link em {
    display: block;
    color: #c8bfb0;
    font-weight: 500;
    font-style: normal;
    font-size: 12px;
}

.nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-height: 0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-right: 2px;
}

.nav-link {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 12px;
    border-radius: 10px;
    color: #efe7d8;
    flex: 0 0 auto;
    line-height: 1.3;
}

.nav-link:hover,
.nav-link.is-active {
    background: rgba(255, 255, 255, 0.08);
}

.nav-link em {
    background: rgba(196, 92, 38, 0.2);
    color: #f0c2a6;
    padding: 2px 7px;
    border-radius: 999px;
}

.sidebar-user {
    border-top: 1px solid rgba(255,255,255,0.08);
    padding-top: 14px;
    display: grid;
    gap: 10px;
    min-height: 0;
    background: linear-gradient(180deg, transparent, rgba(0, 0, 0, 0.12));
}

.link-button {
    background: none;
    border: 0;
    color: #f0c2a6;
    cursor: pointer;
    padding: 0;
    font-weight: 700;
}

.link-button:focus {
    outline: none;
}

.link-button:focus-visible {
    outline: 2px solid rgba(196, 92, 38, 0.45);
    outline-offset: 3px;
    border-radius: 4px;
}

.app-main { min-width: 0; }
.topbar {
    display: flex;
    gap: 14px;
    align-items: center;
    padding: 22px 28px 8px;
}

.topbar h1 { font-size: 28px; }
.topbar p { margin: 0; color: var(--muted); }

.menu-toggle {
    display: none;
    border: 1px solid var(--line);
    background: #fff;
    border-radius: 10px;
    width: 40px;
    height: 40px;
}

.content { padding: 12px 28px 40px; }

.stat-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 14px;
    margin: 8px 0 22px;
}

.stat-card,
.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 18px 18px 16px;
}

.stat-card span {
    color: var(--muted);
    font-size: 13px;
}

.stat-card strong {
    display: block;
    margin-top: 8px;
    font-size: 28px;
    letter-spacing: -0.04em;
}

.panel.highlight {
    border-color: #efc8b1;
    background: #fff6ef;
}

.overlay { display: none; }

.toolbar-dates {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: center;
}

.toolbar-dates input[type="date"] {
    width: auto;
    min-width: 140px;
    flex: 1 1 140px;
}

.data-table .table-input {
    width: 100%;
    min-width: 72px;
    max-width: 120px;
    padding: 8px 10px;
}

@media (max-width: 960px) {
    .app-shell { grid-template-columns: 1fr; }
    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        width: min(280px, 86vw);
        max-width: 320px;
        height: 100vh;
        height: 100dvh;
        transform: translateX(-105%);
        transition: transform 0.2s ease;
        z-index: 20;
        overflow: hidden;
        -webkit-overflow-scrolling: touch;
    }
    .sidebar.open { transform: translateX(0); }
    .menu-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex: 0 0 44px;
        width: 44px;
        height: 44px;
        font-size: 20px;
    }
    .stat-grid { grid-template-columns: 1fr 1fr; }
    .overlay {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(20, 16, 10, 0.45);
        z-index: 15;
    }
    .content, .topbar { padding-left: 16px; padding-right: 16px; }
    .topbar {
        padding-top: 14px;
        align-items: flex-start;
        gap: 12px;
    }
    .topbar h1 { font-size: 22px; line-height: 1.2; }
    .roles-layout { grid-template-columns: 1fr; }
    .toolbar {
        flex-direction: column;
        align-items: stretch;
    }
    .customer-row {
        align-items: stretch;
    }
    .customer-add-btn {
        width: 100%;
        align-self: stretch;
    }
    .toolbar .btn-primary,
    .toolbar .btn-ghost {
        width: 100%;
    }
    .detail-grid { grid-template-columns: 1fr; }
    .filter-tabs {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 4px;
    }
    .filter-tabs button { flex: 0 0 auto; }
}

.toolbar {
    display: flex;
    gap: 12px;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

.customer-row {
    align-items: flex-end;
    margin-bottom: 0;
}

.customer-row .grow {
    margin: 0;
}

.customer-add-btn {
    flex: 0 0 auto;
    white-space: nowrap;
    min-height: 46px;
    align-self: flex-end;
}

.toolbar input[type="search"] {
    flex: 1;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 12px 14px;
    background: #fff;
}

select {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 12px 14px;
    padding-right: 36px;
    background: #fff url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%236b6258' d='M1.4.6 6 5.2 10.6.6 12 2 6 8 0 2z'/%3E%3C/svg%3E") no-repeat right 14px center;
    background-size: 12px 8px;
}

.flash {
    background: #e7f3ea;
    color: var(--good);
    border-radius: 10px;
    padding: 10px 14px;
    font-weight: 650;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
}

.data-table th {
    text-align: left;
    font-size: 12px;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--muted);
    padding: 0 12px 12px;
}

.data-table td {
    padding: 14px 12px;
    border-top: 1px solid var(--line);
    vertical-align: top;
}

.data-table th:nth-last-child(-n+2),
.data-table td:nth-last-child(-n+2) {
    width: 1%;
    white-space: nowrap;
}

.data-table th:last-child,
.data-table td:last-child {
    text-align: right;
}

.row-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.row-actions .link-button,
.row-actions a {
    color: var(--copper);
    font-weight: 700;
    font-size: 13px;
}

.badge {
    display: inline-flex;
    padding: 3px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 700;
    text-transform: capitalize;
}

.badge-good,
.badge-active { background: #e7f3ea; color: var(--good); }
.badge-muted,
.badge-suspended,
.badge-deactivated { background: #efe8dc; color: var(--muted); }
.badge-pending { background: #fff3d6; color: #8a6a12; }
.badge-rejected { background: #f8e4e0; color: var(--danger); }

.filter-tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 16px;
}

.filter-tabs button {
    border: 1px solid var(--line);
    background: #fff;
    border-radius: 999px;
    padding: 8px 14px;
    font-weight: 700;
    cursor: pointer;
}

.filter-tabs button.is-active {
    background: var(--forest);
    border-color: var(--forest);
    color: #fff;
}

.detail-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 14px 18px;
    margin-top: 18px;
}

.detail-grid dt {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
}

.detail-grid dd { margin: 4px 0 0; font-weight: 650; }

.table-wrap { overflow-x: auto; }
.data-table a { color: var(--copper); font-weight: 700; }
.panel + .panel { margin-top: 16px; }

.btn-ghost {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
    border-radius: 999px;
    padding: 12px 18px;
    font-weight: 700;
}

.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-top: 18px;
}

.form-grid .toolbar,
.form-grid .full { grid-column: 1 / -1; }

.roles-layout {
    display: grid;
    grid-template-columns: 240px 1fr;
    gap: 16px;
}

.role-list { display: grid; gap: 8px; align-content: start; }

.role-item {
    display: grid;
    gap: 4px;
    text-align: left;
    border: 1px solid transparent;
    background: #f7f1e6;
    border-radius: 12px;
    padding: 12px;
    cursor: pointer;
    color: inherit;
}

.role-item.is-active {
    border-color: var(--copper);
    background: #fff6ef;
}

.perm-group {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 12px 14px;
    margin: 14px 0;
}

.perm-group legend {
    padding: 0 6px;
    font-weight: 700;
}

@media (max-width: 720px) {
    .form-grid { grid-template-columns: 1fr; }
    .login-body { padding: 22px 18px 28px; }
    .login-brand { padding: 22px 16px 4px; }
    .stat-grid { grid-template-columns: 1fr; }
    .stat-card strong { font-size: 24px; }
    .content { padding-bottom: 28px; }
    .panel, .stat-card { padding: 14px; }
    .invoice-sheet { margin: 12px; padding: 16px; }
    .inv-top { grid-template-columns: 1fr; }
    .inv-title { grid-column: 1; text-align: left; margin-top: 0; }
    .inv-meta { flex-direction: column; gap: 4px; }
    .pay-grid { grid-template-columns: 1fr; }
    .totals input { width: 100%; max-width: 140px; }
    .btn-primary,
    .btn-ghost {
        min-height: 44px;
        width: 100%;
    }
    .form-grid .toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    /* Stack editable line tables on phones so qty/price stay editable */
    .stack-table thead { display: none; }
    .stack-table,
    .stack-table tbody,
    .stack-table tr,
    .stack-table td {
        display: block;
        width: 100%;
    }
    .stack-table tr {
        background: #fff;
        border: 1px solid var(--line);
        border-radius: 12px;
        padding: 12px;
        margin-bottom: 10px;
    }
    .stack-table td {
        display: grid;
        grid-template-columns: 100px minmax(0, 1fr);
        gap: 8px;
        align-items: center;
        padding: 8px 0;
        border-top: 0;
        text-align: left !important;
        white-space: normal;
        width: 100% !important;
    }
    .stack-table td::before {
        content: attr(data-label);
        font-size: 11px;
        font-weight: 700;
        letter-spacing: 0.06em;
        text-transform: uppercase;
        color: var(--muted);
    }
    .stack-table td:first-child {
        display: block;
        padding-top: 0;
        padding-bottom: 10px;
        border-bottom: 1px solid var(--line);
        margin-bottom: 4px;
    }
    .stack-table td:first-child::before { display: none; }
    .stack-table td:last-child {
        display: flex;
        justify-content: flex-end;
        grid-template-columns: none;
        padding-bottom: 0;
    }
    .stack-table td:last-child::before { display: none; }
    .stack-table .table-input {
        max-width: none;
        width: 100%;
    }
    .data-table th:nth-last-child(-n+2),
    .data-table td:nth-last-child(-n+2) {
        white-space: normal;
    }
    .row-actions {
        justify-content: flex-end;
    }
    .stack-table tr.empty-row {
        border: 0;
        padding: 0;
        background: transparent;
        margin: 0;
    }
    .stack-table tr.empty-row td {
        display: block;
        padding: 12px;
        border: 0;
    }
    .stack-table tr.empty-row td::before {
        display: none;
    }
}

.plan-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 14px;
    margin-top: 16px;
}

.plan-card {
    border: 1px solid var(--line);
    border-radius: 14px;
    background: #fff;
    padding: 16px;
    display: grid;
    gap: 8px;
    align-content: start;
}

.plan-card.is-current {
    border-color: #efc8b1;
    background: #fff6ef;
}

.plan-card strong {
    font-size: 28px;
    letter-spacing: -0.04em;
}

.plan-benefits {
    margin: 8px 0 0;
    padding-left: 18px;
    color: var(--muted);
    font-size: 14px;
}

.plan-benefits li { margin: 4px 0; }

select:disabled {
    background-color: #f3efe6;
    color: var(--ink);
    cursor: not-allowed;
    opacity: 1;
}

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

.search-results {
    list-style: none;
    margin: 10px 0 0;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 12px;
    overflow: hidden;
}
.search-results button {
    width: 100%;
    text-align: left;
    background: #fff;
    border: 0;
    border-bottom: 1px solid var(--line);
    padding: 10px 12px;
    cursor: pointer;
    display: grid;
    gap: 2px;
}
.search-results button:hover { background: #fff6ef; }
.bill-screen {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(260px, 300px);
    gap: 16px;
    align-items: start;
    width: 100%;
    max-width: 100%;
}
.bill-main {
    min-width: 0;
    max-width: 100%;
}
.bill-side {
    position: sticky;
    top: 16px;
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
}
.totals { display: grid; gap: 8px; margin: 16px 0; }
.totals dt,
.totals dd { margin: 0; }
.totals > div {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: center;
    min-width: 0;
}
.totals .grand { font-size: 18px; font-weight: 800; }
.totals input {
    width: 110px;
    min-width: 0;
    padding: 8px 10px;
}
.pay-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 10px;
    margin: 12px 0;
    min-width: 0;
}
.pay-grid label {
    min-width: 0;
}
.pay-grid input {
    width: 100%;
    min-width: 0;
}
.toolbar.stacked { flex-direction: column; align-items: stretch; }
.invoice-sheet {
    max-width: 820px;
    margin: 24px auto;
    background: #fff;
    padding: 22px 24px 28px;
    border: 1px solid #222;
    color: #111;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 13px;
    line-height: 1.45;
}
.invoice-sheet.inv h1,
.invoice-sheet.inv h2,
.invoice-sheet.inv strong {
    color: #111;
}
.inv-top {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: start;
    gap: 12px;
    margin-bottom: 10px;
}
.inv-brand {
    display: flex;
    align-items: center;
    min-width: 0;
}
.inv-logo {
    width: 88px;
    height: 88px;
    object-fit: contain;
    flex: 0 0 auto;
}
.inv-title {
    margin: 8px 0 0;
    text-align: center;
    font-size: 1.55rem;
    font-weight: 700;
    letter-spacing: 0.01em;
    grid-column: 2;
}
.inv-meta {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    margin: 8px 0 12px;
    font-weight: 600;
}
.inv-table {
    width: 100%;
    border-collapse: collapse;
    margin: 0 0 16px;
}
.inv-table th,
.inv-table td {
    border: 1px solid #333;
    padding: 8px 10px;
    vertical-align: top;
}
.inv-table th {
    background: #f2f2f2;
    font-weight: 700;
    text-align: left;
}
.inv-parties td {
    width: 50%;
}
.inv-num { width: 36px; text-align: center; }
.inv-qty { width: 64px; text-align: center; }
.inv-amt { text-align: right; white-space: nowrap; }
.inv-sku {
    display: block;
    color: #555;
    font-size: 11px;
    margin-top: 2px;
}
.inv-empty {
    text-align: center;
    color: #666;
    padding: 18px 10px;
}
.inv-grand td {
    font-weight: 700;
    background: #fafafa;
}
.inv-payable td {
    font-weight: 700;
}
.inv-section-title {
    margin: 4px 0 8px;
    text-align: center;
    font-size: 1rem;
    font-weight: 700;
}
.inv-breakdown td:first-child,
.inv-pay td:first-child {
    width: 70%;
}
.inv-words {
    margin: 4px 0 14px;
    font-weight: 600;
}
.inv-terms {
    margin: 0 0 28px;
}
.inv-terms ol {
    margin: 6px 0 0;
    padding-left: 18px;
}
.inv-terms li {
    margin: 4px 0;
}
.inv-sign {
    margin: 36px 0 0;
    font-weight: 600;
}
.inv-actions {
    display: flex;
    gap: 12px;
    align-items: center;
    margin-top: 18px;
}
.print-body { background: #fff; }
@media print {
    .no-print { display: none !important; }
    .invoice-sheet {
        border: 1px solid #222;
        margin: 0;
        max-width: none;
        padding: 12px;
    }
}
@media (max-width: 720px) {
    .inv-top {
        grid-template-columns: 1fr;
    }
    .inv-title {
        grid-column: 1;
        text-align: left;
        margin-top: 0;
    }
}

@media (max-width: 960px) {
    .bill-screen {
        display: flex;
        flex-direction: column;
        gap: 16px;
        width: 100%;
        max-width: 100%;
    }
    .bill-main,
    .bill-side {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        position: static;
    }
    .bill-side {
        order: 2;
    }
    .bill-main {
        order: 1;
    }
    .customer-row {
        flex-direction: column;
        align-items: stretch;
    }
    .customer-add-btn {
        width: 100%;
    }
    .pay-grid {
        grid-template-columns: 1fr;
    }
}
