:root {
    --bg: #f4f5f7;
    --card: #ffffff;
    --ink: #1d2430;
    --muted: #6b7280;
    --primary: #0b5ed7;
    --primary-dark: #0a53be;
    --border: #d9dde3;
    --overdue: #fde8ea;
    --overdue-ink: #b42318;
    --ok-ink: #1a7f37;
    --radius: 12px;
}

/* Zile până la / peste scadență: verde înainte, roșu după */
.z-ram { color: var(--ok-ink); font-weight: 600; }
.z-dep { color: var(--overdue-ink); font-weight: 700; }

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
    font-size: 17px;
    line-height: 1.4;
    color: var(--ink);
    background: var(--bg);
    padding-bottom: env(safe-area-inset-bottom);
}

/* ---- Top bar ---- */
.topbar {
    display: flex;
    align-items: center;
    gap: .75rem;
    background: var(--primary);
    color: #fff;
    padding: .8rem 1rem;
    padding-top: calc(.8rem + env(safe-area-inset-top));
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar .brand { color: #fff; text-decoration: none; font-weight: 700; font-size: 1.05rem; }
.topbar .spacer { flex: 1; }
.topbar .user { font-size: .85rem; opacity: .9; }
.topbar .logout { color: #fff; text-decoration: none; background: rgba(255,255,255,.18); padding: .35rem .7rem; border-radius: 8px; font-size: .9rem; }

/* ---- Layout ---- */
.container { max-width: 860px; margin: 0 auto; padding: 1rem; }
h1 { font-size: 1.4rem; margin: .3rem 0 .8rem; }
h2 { font-size: 1.1rem; margin: 0 0 .2rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    margin-bottom: 1rem;
    box-shadow: 0 1px 2px rgba(16,24,40,.04);
}

/* ---- Forms ---- */
.form label { display: block; font-weight: 600; margin-bottom: .9rem; font-size: .95rem; }
.form input,
.form select {
    display: block;
    width: 100%;
    height: 52px;
    margin-top: .35rem;
    padding: 0 .85rem;
    font-family: inherit;
    font-size: 1.05rem;
    font-weight: 400;
    line-height: 1.4;
    color: var(--ink);
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #fff;
}
.form select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    padding-right: 2.6rem;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='3' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right .95rem center;
    background-size: 14px;
}

/* checkbox „ține-mă minte" — inline, nu full-width */
.form label.checkbox {
    display: flex;
    align-items: center;
    gap: .6rem;
    font-weight: 400;
}
.form label.checkbox input[type="checkbox"] {
    width: 22px;
    height: 22px;
    margin: 0;
    padding: 0;
    flex: none;
}
input:focus, button:focus { outline: 3px solid rgba(11,94,215,.35); outline-offset: 1px; }

/* ---- Buttons ---- */
.btn {
    display: inline-block;
    text-align: center;
    text-decoration: none;
    border: 1px solid var(--border);
    background: #fff;
    color: var(--ink);
    padding: .85rem 1.1rem;
    border-radius: 10px;
    font-size: 1.05rem;
    font-weight: 600;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
}
.btn.block { display: block; width: 100%; }
.btn.small { padding: .5rem .8rem; font-size: .9rem; }
.btn.big { padding: 1.1rem; font-size: 1.2rem; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:active { background: var(--primary-dark); }
.btn.ghost { background: transparent; }
.btn:disabled, .btn[disabled] { opacity: .45; cursor: not-allowed; }
.btn.danger { background: #dc2626; border-color: #dc2626; color: #fff; }
.btn.danger:active { background: #b91c1c; }
.btn.success { background: #16a34a; border-color: #16a34a; color: #fff; }
.btn.success:active { background: #15803d; }
.btn + .btn, .btn.block + .btn.block { margin-top: .6rem; }

/* ---- Lists ---- */
.list { list-style: none; margin: .5rem 0; padding: 0; }
.list li { margin: 0; }
.list a {
    display: block;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 1rem;
    margin-bottom: .55rem;
    text-decoration: none;
    color: var(--ink);
    font-weight: 600;
}
.list a:active { background: #eef2ff; }

/* ---- Alerts ---- */
.alert { padding: .8rem 1rem; border-radius: 10px; margin-bottom: 1rem; }
.alert.error { background: var(--overdue); color: var(--overdue-ink); }
.alert.success { background: #dcfce7; color: #14532d; }

/* Mini-formularul de trimitere email (din ecranul partenerului) */
.email-form { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; background: #f7f8fa; border: 1px solid var(--border); border-radius: 10px; padding: .55rem .7rem; margin: .4rem 0 .9rem; }
.email-form input[type="email"] { flex: 1 1 220px; min-width: 0; padding: .55rem .7rem; font-size: .95rem; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
.email-form .btn { flex: 0 0 auto; }

/* ---- Tables (restanțe) ---- */
table.restante { width: 100%; border-collapse: collapse; font-size: .95rem; }
table.restante thead th {
    text-align: left;
    background: #f0f2f5;
    padding: .55rem .5rem;
    border-bottom: 2px solid var(--border);
    font-size: .8rem;
    text-transform: uppercase;
    letter-spacing: .02em;
    color: var(--muted);
}
table.restante td { padding: .55rem .5rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.restante .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.restante tr.overdue td { background: var(--overdue); }
table.restante input.suma {
    width: 100%;
    min-width: 90px;
    padding: .6rem;
    font-size: 1rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    text-align: right;
}
.badge { display: inline-block; background: var(--overdue-ink); color: #fff; font-size: .7rem; padding: .1rem .35rem; border-radius: 6px; }
table.restante tfoot .subtotal td { font-weight: 700; background: #f7f8fa; }
.total { text-align: right; font-size: 1.05rem; margin: .8rem 0; }
.grandtotal { font-size: 1.05rem; line-height: 1.8; }
.grandtotal strong { font-size: 1.1rem; }
section.partner h2 a { color: var(--primary); text-decoration: none; }
/* în raportul „Arată tot" tabelul stă deja într-un card de partener — fără chenar dublu */
.partner .erp-wrap { border: none; background: transparent; }
.partner .erp-head { margin-bottom: .5rem; }

/* ---- Comutare desktop / telefon (implicit: desktop) ---- */
.only-mobile { display: none !important; }

/* ---- Ecran restanțe partener: tabel stil ERP (desktop) ---- */
.erp-head { margin-bottom: .8rem; }
.erp-head h1 { margin-bottom: .2rem; }
.erp-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--border); border-radius: 10px; background: #fff; }
table.erp { width: 100%; border-collapse: collapse; font-size: .82rem; table-layout: auto; }
table.erp thead th {
    background: #f0f2f5;
    text-align: right;
    padding: .45rem .4rem;
    border-bottom: 2px solid var(--border);
    font-size: .74rem;
    color: var(--muted);
    white-space: nowrap;
}
table.erp thead th:nth-child(1),
table.erp thead th:nth-child(2),
table.erp thead th:nth-child(3),
table.erp thead th:nth-child(4),
table.erp thead th:nth-child(9) { text-align: left; }
table.erp tbody.inv tr.d td { padding: .4rem .4rem; white-space: nowrap; border-bottom: 1px solid #eee; }
table.erp tbody.inv tr.x td { padding: 0 .4rem .5rem; color: var(--muted); font-size: .78rem; white-space: normal; border-bottom: 1px solid var(--border); }
table.erp .num { text-align: right; font-variant-numeric: tabular-nums; }
table.erp tbody.inv.overdue tr.d td { background: var(--overdue); }
.erp-total { text-align: right; margin: .9rem 0; line-height: 1.7; }
.erp-total strong { font-size: 1.05rem; }

/* ---- Selectare facturi (doar pe telefon) ---- */
.inv-cards .ic[data-nr] { cursor: pointer; -webkit-tap-highlight-color: transparent; position: relative; }
.inv-cards .ic.selected {
    border-color: var(--primary);
    border-width: 2px;
    background: #e3eeff;
    box-shadow: 0 0 0 3px rgba(11,94,215,.35);
}
.inv-cards .ic.selected::after {
    content: "✓";
    position: absolute;
    top: -11px; right: -9px;
    width: 28px; height: 28px;
    background: var(--primary);
    color: #fff;
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-size: 16px; font-weight: 700;
    box-shadow: 0 2px 5px rgba(0,0,0,.3);
}
.select-hint { color: var(--muted); font-size: .82rem; margin: .2rem 0 .6rem; }

/* ---- Modal emitere chitanță ---- */
.modal[hidden] { display: none; }
.modal {
    position: fixed; inset: 0;
    background: rgba(0,0,0,.5);
    display: flex; align-items: flex-end; justify-content: center;
    z-index: 200;
}
.modal-box {
    background: #fff;
    width: 100%; max-width: 620px;
    max-height: 92vh; overflow: auto;
    border-radius: 16px 16px 0 0;
    padding: 1rem 1rem calc(1rem + env(safe-area-inset-bottom));
}
@media (min-width: 641px) {
    .modal { align-items: center; }
    .modal-box { border-radius: 16px; }
}
.modal-box > header { display: flex; justify-content: space-between; align-items: center; font-weight: 700; font-size: 1.15rem; margin-bottom: .6rem; }
.modal-close { background: none; border: none; font-size: 1.4rem; line-height: 1; cursor: pointer; padding: .2rem .4rem; color: var(--muted); }
.modal-distribute { display: flex; gap: .5rem; align-items: flex-end; background: #f0f2f5; padding: .65rem; border-radius: 10px; margin-bottom: .7rem; }
.modal-distribute label { flex: 1; font-weight: 600; font-size: .85rem; }
.modal-distribute input { width: 100%; margin-top: .3rem; padding: .6rem; font-size: 1rem; border: 1px solid var(--border); border-radius: 8px; }
.modal-distribute .btn { white-space: nowrap; }
.modal-invoices { list-style: none; margin: 0 0 .6rem; padding: 0; }
.modal-invoices li { padding: .6rem 0; border-bottom: 1px solid #eee; }
.mi-top { display: grid; grid-template-columns: auto 1fr 1fr auto; gap: .3rem .5rem; align-items: baseline; font-size: .9rem; margin-bottom: .45rem; }
.mi-top .al-l { text-align: left; }
.mi-top .al-c { text-align: center; }
.mi-top .al-r { text-align: right; }
.mi-top .lbl { color: var(--muted); font-size: .78rem; margin-right: .15rem; }
.mi-top b { font-weight: 700; }
.mi-bot { display: flex; gap: .4rem; align-items: center; }
.mi-bot input.suma { flex: 1 1 auto; min-width: 0; padding: .6rem; font-size: 1.05rem; text-align: right; border: 1px solid var(--border); border-radius: 8px; }
.mi-bot .btn { flex: 0 0 auto; }
.modal-foot { display: flex; flex-wrap: nowrap; align-items: center; gap: .4rem; position: sticky; bottom: 0; background: #fff; padding-top: .7rem; border-top: 1px solid var(--border); margin-top: .3rem; }
.modal-foot .btn { white-space: nowrap; padding: .7rem .45rem; font-size: .9rem; }
.modal-foot #cancelEmit { flex: 0 0 auto; }
.modal-foot #emitTotal { flex: 1 1 0; min-width: 0; }
.modal-foot #emitBtn { flex: 1.5 1 0; min-width: 0; }

/* ---- Responsive: tabel -> carduri pe ecrane mici ---- */
@media (max-width: 640px) {
    table.restante, table.restante tbody, table.restante tfoot { display: block; width: 100%; }
    table.restante thead { display: none; }

    table.restante tr {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        border: 1px solid var(--border);
        border-radius: 10px;
        margin-bottom: .7rem;
        padding: .35rem .2rem;
        background: #fff;
    }
    table.restante tr.overdue { border-color: var(--overdue-ink); }
    table.restante tfoot tr.subtotal { font-weight: 700; background: #f7f8fa; }

    table.restante td {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: .75rem;
        border: none;
        padding: .45rem .7rem;
        background: transparent !important;
    }
    table.restante td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--muted);
        font-size: .85rem;
        white-space: nowrap;
    }
    table.restante .num { text-align: right; }

    /* ---- Comutare desktop / telefon ---- */
    .only-desktop { display: none !important; }
    .only-mobile  { display: block !important; }

    /* ---- Card factură pe telefon ---- */
    .inv-cards .ic {
        border: 1px solid var(--border);
        border-radius: 10px;
        margin-bottom: .6rem;
        padding: .55rem .7rem;
        background: #fff;
    }
    .inv-cards .ic.overdue { border-color: var(--overdue-ink); background: var(--overdue); }
    .inv-cards .ic-r1 { display: grid; grid-template-columns: auto auto 1fr auto; gap: .25rem .55rem; align-items: start; }
    .inv-cards .ic-zile { font-size: 1rem; align-self: center; }
    .inv-cards .ic-r2 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: .25rem .4rem; margin-top: .5rem; }
    .inv-cards .al-l { text-align: left; }
    .inv-cards .al-c { text-align: center; }
    .inv-cards .al-r { text-align: right; }
    .inv-cards .f { font-size: .9rem; min-width: 0; }
    .inv-cards .f .lbl { color: var(--muted); margin-right: .2rem; font-size: .8rem; }
    .inv-cards .f.stack { display: flex; flex-direction: column; line-height: 1.2; font-size: .76rem; }
    .inv-cards .f.stack .lbl { margin: 0; }
    .inv-cards .ic-x {
        margin-top: .45rem; padding-top: .4rem;
        border-top: 1px dashed var(--border);
        color: var(--muted); font-size: .8rem;
    }
}

/* ---- Pagina de print ---- */
.print-summary { line-height: 1.7; }
.canvas-wrap { overflow: auto; margin-top: 1rem; border: 1px dashed var(--border); border-radius: 10px; padding: .5rem; background: #fff; }
#canvasPaper { background: #fff; max-width: 100%; }
.overlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,.5);
    display: flex; align-items: center; justify-content: center;
    z-index: 100;
}
.overlay-box { background: #fff; padding: 1.5rem 2rem; border-radius: 12px; font-weight: 700; }
