:root {
    --accent: #0f5c63;
    --accent-dark: #0a4247;
    --ink: #1d2327;
    --muted: #6b7479;
    --line: #e2e6e8;
    --bg: #f5f7f8;
    --card: #fff;
    --ok: #1f7a3d;
    --warn: #9a6400;
    --err: #b3261e;
    --radius: 8px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font: 15px/1.5 "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
    color: var(--ink);
    background: var(--bg);
}

h1 { font-size: 1.5rem; margin: 0 0 .5rem; }
h2 { font-size: 1rem; margin: 0 0 .5rem; color: var(--muted); font-weight: 600; }
a { color: var(--accent); }
.muted { color: var(--muted); }
small { font-size: .85em; }

.wordmark {
    font-weight: 700;
    letter-spacing: .28em;
    color: var(--accent);
    text-decoration: none;
    font-size: 1.15rem;
}
.wordmark-lg { font-size: 2rem; text-align: center; }

/* Top bar */
.topbar {
    display: flex; align-items: center; gap: 2rem;
    padding: .75rem 1.5rem;
    background: var(--card);
    border-bottom: 1px solid var(--line);
}
.mainnav { display: flex; gap: 1rem; flex: 1; }
.mainnav a { text-decoration: none; color: var(--ink); padding: .25rem .5rem; border-radius: 4px; }
.mainnav a.active { background: var(--bg); color: var(--accent); font-weight: 600; }
.userbox { display: flex; align-items: center; gap: 1rem; }
form.inline { display: inline; margin: 0; }
button.link { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font: inherit; text-decoration: underline; }

.container { max-width: 1100px; margin: 2rem auto; padding: 0 1.5rem; }

/* Cards */
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem; margin: 1.5rem 0; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1rem; }
.card.narrow { max-width: 460px; }
.kpi { font-size: 1.75rem; font-weight: 600; margin: 0; }

/* Forms */
label { display: block; margin-bottom: 1rem; font-weight: 500; }
input[type=email], input[type=password], input[type=text] {
    display: block; width: 100%; margin-top: .3rem;
    padding: .55rem .7rem; font: inherit;
    border: 1px solid #c9d0d3; border-radius: 6px; background: #fff;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }

.btn { display: inline-block; padding: .6rem 1.2rem; border-radius: 6px; border: 1px solid transparent; font: inherit; font-weight: 600; cursor: pointer; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }

/* Alerts & badges */
.alert { padding: .65rem .9rem; border-radius: 6px; margin-bottom: 1rem; border: 1px solid; }
.alert-error { color: var(--err); background: #fdecea; border-color: #f5c2bd; }
.alert-success { color: var(--ok); background: #e8f5ec; border-color: #b9dfc5; }
.alert-info { color: var(--accent-dark); background: #e7f2f3; border-color: #b8d8db; }
.badge { display: inline-block; padding: .05rem .45rem; border-radius: 999px; font-size: .78rem; font-weight: 600; }
.badge-warn { background: #fff3dc; color: var(--warn); }

/* Login */
body.auth { display: flex; min-height: 100vh; align-items: center; justify-content: center; }
.auth-card {
    width: 100%; max-width: 380px;
    background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
    padding: 2rem;
}
.auth-card > .muted { text-align: center; margin-top: 0; margin-bottom: 1.5rem; }
.auth-card .btn { width: 100%; }

@media (max-width: 640px) {
    .topbar { flex-wrap: wrap; gap: .75rem; }
    .userbox { width: 100%; justify-content: space-between; }
}

/* ---- Phase 1: pages, tables, forms ---- */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.page-head h1 { margin: 0; }
.page-head h1 small { font-weight: 400; font-size: .9rem; margin-left: .4rem; }
h3 { font-size: .95rem; margin: 0 0 .5rem; }

.card.flush { padding: 0; overflow-x: auto; }
.empty { padding: 1.25rem; color: var(--muted); margin: 0; }

.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: .6rem .9rem; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { font-size: .82rem; color: var(--muted); font-weight: 600; background: #fafbfb; }
.table tbody tr:hover { background: #fafcfc; }
.table tr.inactive td { color: var(--muted); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table .center { text-align: center; }
.table .actions { text-align: right; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.table.matrix td:first-child { min-width: 260px; }
.table.translations textarea { width: 100%; min-width: 180px; font: inherit; font-size: .9rem; padding: .35rem .5rem; border: 1px solid #c9d0d3; border-radius: 6px; resize: vertical; }
.table.translations .col-key { width: 170px; }
.table.audit pre { white-space: pre-wrap; font-size: .8rem; margin: .4rem 0 0; max-width: 520px; }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 1.25rem; }
.form-grid .span-2 { grid-column: span 2; }
.lang-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1.25rem; }
select, textarea, input[type=number], input[type=search], input[type=text], input[type=email], input[type=color] { font: inherit; }
label select, label textarea, label input[type=number], label input[type=color], label input[type=file] {
    display: block; width: 100%; margin-top: .3rem;
    padding: .5rem .6rem; border: 1px solid #c9d0d3; border-radius: 6px; background: #fff;
}
label input[type=color] { height: 2.4rem; padding: .2rem; max-width: 120px; }
label small { display: block; font-weight: 400; margin-top: .2rem; }
label.check { display: flex; align-items: center; gap: .5rem; font-weight: 500; }
label.check small { display: inline; margin: 0; }
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
fieldset:disabled input, fieldset:disabled select, fieldset:disabled textarea { background: #f3f5f6; color: var(--ink); }
.form-actions { margin: 1rem 0 2rem; display: flex; gap: .75rem; }
.sticky-actions { position: sticky; bottom: 0; background: var(--bg); padding: .75rem 0; margin: 0; }
.row-form { display: flex; gap: .5rem; }
.row-form input { margin: 0; }
.upload-form { margin-top: 1rem; }
.logo-preview { max-height: 80px; max-width: 320px; border: 1px dashed var(--line); padding: .5rem; background: #fff; }

.filter-bar { display: flex; gap: .5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.filter-bar input[type=search] { flex: 1; min-width: 220px; padding: .5rem .7rem; border: 1px solid #c9d0d3; border-radius: 6px; }
.filter-bar select { padding: .5rem .6rem; border: 1px solid #c9d0d3; border-radius: 6px; background: #fff; }

.btn-secondary { background: #fff; color: var(--accent); border-color: #b8d8db; }
.btn-secondary:hover { background: #e7f2f3; }
a.btn { text-decoration: none; }

.alert-warning { color: var(--warn); background: #fff6e5; border-color: #f1d59c; }
.alert-password { color: var(--ink); background: #fffbe6; border-color: #e8d57a; font-family: Consolas, monospace; }
.badge { background: #eceff1; color: var(--muted); }
.badge-ok { background: #e8f5ec; color: var(--ok); }
.badge-info { background: #e7f2f3; color: var(--accent-dark); }

/* Admin dropdown */
.dropdown { position: relative; }
.dropdown summary { list-style: none; cursor: pointer; padding: .25rem .5rem; border-radius: 4px; }
.dropdown summary::-webkit-details-marker { display: none; }
.dropdown summary::after { content: ' ▾'; font-size: .75em; }
.dropdown summary.active { background: var(--bg); color: var(--accent); font-weight: 600; }
.dropdown-menu {
    position: absolute; top: calc(100% + .4rem); left: 0; z-index: 10; min-width: 220px;
    background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
    box-shadow: 0 6px 20px rgba(0,0,0,.08); padding: .35rem;
}
.dropdown-menu a { display: block; padding: .45rem .7rem; border-radius: 4px; text-decoration: none; color: var(--ink); }
.dropdown-menu a:hover { background: var(--bg); color: var(--accent); }

@media (max-width: 800px) {
    .form-grid, .lang-grid { grid-template-columns: 1fr; }
    .form-grid .span-2 { grid-column: auto; }
}

[hidden] { display: none !important; }

/* ---- Phase 2: documents ---- */
.form-grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); margin: 0 0 1rem; }
.grid.two .card { margin: 0; }
.action-bar { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; }
.btn-issue { background: #1f7a3d; color: #fff; }
.btn-issue:hover { background: #17602f; }
.danger, button.link.danger { color: var(--err); }
.danger-zone { margin: -1rem 0 2rem; }

.lines-editor th, .lines-editor td { padding: .45rem .4rem; vertical-align: top; }
.lines-editor input[type=text], .lines-editor select, .lines-editor textarea {
    width: 100%; font: inherit; font-size: .9rem; padding: .4rem .5rem; margin: 0 0 .3rem;
    border: 1px solid #c9d0d3; border-radius: 6px; background: #fff; resize: none; overflow: hidden;
}
.lines-editor .line-text { min-width: 280px; width: 42%; }
.lines-editor .line-title { font-weight: 600; }
.lines-editor .num-input { text-align: right; min-width: 70px; }
.lines-editor .num-input.narrow { min-width: 50px; }
.lines-editor .line-net { padding-top: .8rem; white-space: nowrap; font-variant-numeric: tabular-nums; }
.lines-editor .line-net.invalid { color: var(--err); font-weight: 700; }
.lines-editor .line-move { width: 28px; }
.icon-btn { border: 0; background: none; cursor: pointer; color: var(--muted); padding: .15rem .25rem; font-size: .8rem; line-height: 1; display: block; }
.icon-btn:hover { color: var(--accent); }
.icon-btn.danger:hover { color: var(--err); }
#doc-form .second-lang { display: none; border-left: 3px solid #b8d8db; padding-left: .4rem; }
#doc-form.bilingual .second-lang { display: block; }
#doc-form.no-vat .vat-cell { display: none; }
.lines-actions { display: flex; gap: .5rem; margin: .75rem 0; flex-wrap: wrap; }
.lines-actions select { padding: .5rem .6rem; border: 1px solid #b8d8db; border-radius: 6px; background: #fff; color: var(--accent); font: inherit; }
.totals-box { margin-left: auto; max-width: 360px; border-top: 1px solid var(--line); padding-top: .75rem; }
.totals-box > div, #t-vat-groups > div { display: flex; justify-content: space-between; padding: .2rem 0; font-variant-numeric: tabular-nums; }
.totals-box .grand { border-top: 2px solid var(--ink); margin-top: .3rem; padding-top: .4rem; font-size: 1.1rem; }

table.kv td { padding: .25rem .75rem .25rem 0; vertical-align: top; }
table.kv td:first-child { color: var(--muted); white-space: nowrap; }
code.hash { font-size: .78rem; word-break: break-all; }
.table tfoot td { border-bottom: 0; }
.table tfoot tr.grand td { font-weight: 700; font-size: 1.05rem; border-top: 2px solid var(--ink); }

.status-draft { background: #eceff1; color: #455a64; }
.status-issued { background: #e7f2f3; color: var(--accent-dark); }
.status-sent { background: #e3ecfa; color: #1f4e99; }
.status-paid, .status-accepted { background: #e8f5ec; color: var(--ok); }
.status-partially_paid { background: #fff3dc; color: var(--warn); }
.status-rejected, .status-cancelled, .status-expired { background: #fdecea; color: var(--err); }
.status-converted { background: #f1e8fa; color: #6a3d9a; }

@media (max-width: 900px) {
    .form-grid.three, .grid.two { grid-template-columns: 1fr; }
}
label input[type=date] { display: block; width: 100%; margin-top: .3rem; padding: .5rem .6rem; border: 1px solid #c9d0d3; border-radius: 6px; background: #fff; font: inherit; }
.lines-editor select[name$="[unit]"] { min-width: 88px; }
.lines-editor select[name$="[vat_rate]"] { min-width: 72px; }
.topbar { flex-wrap: wrap; }
#t-vat-groups { display: block; padding: 0; }

/* ---- Phase 3 ---- */
.action-bar.flush { margin: 0; }
.check-offset { margin-top: 1.6rem; }
.tabs { display: flex; gap: .25rem; margin-bottom: 1rem; border-bottom: 1px solid var(--line); }
.tabs a { padding: .5rem 1rem; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.lines-editor tr.is-commission .std-only > * { visibility: hidden; }
.commission-box { margin-top: .4rem; padding: .6rem; background: #f4f8f8; border: 1px solid #d5e6e8; border-radius: 6px; }
.commission-grid { display: grid; grid-template-columns: 110px 170px 1fr; gap: .5rem; }
.commission-grid label { margin: 0; font-size: .85rem; }
.commission-grid input, .commission-grid select { margin-top: .2rem; }
.commission-base[readonly] { background: #eef3f3; color: var(--muted); }
.channels { margin: .4rem 0 .2rem; }
.channel-row { display: grid; grid-template-columns: 1fr 140px 24px; gap: .4rem; align-items: start; }
.credit-actions summary { cursor: pointer; }
.credit-form { margin-top: 1rem; max-width: 640px; }
.credit-form label.check { margin-bottom: .5rem; font-weight: 400; }
.balance { display: flex; gap: 2rem; flex-wrap: wrap; margin-bottom: 1rem; font-variant-numeric: tabular-nums; }
.balance div { display: flex; flex-direction: column; }
.balance span { color: var(--muted); font-size: .85rem; }
.balance .grand strong { font-size: 1.2rem; color: var(--accent-dark); }
.payment-form { display: flex; gap: .75rem; align-items: flex-end; flex-wrap: wrap; margin-top: 1rem; }
.payment-form label { margin: 0; }
.payment-form label.grow { flex: 1; min-width: 180px; }
.payment-form input, .payment-form select { display: block; margin-top: .3rem; padding: .5rem .6rem; border: 1px solid #c9d0d3; border-radius: 6px; font: inherit; background: #fff; }
.due-box { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; }
.due-box form { margin-left: auto; }
span.danger { color: var(--err); }
.lines-editor tr.is-commission td.line-text { width: auto; }
.lines-editor tr.is-commission .line-title, .lines-editor tr.is-commission textarea, .commission-box { max-width: 720px; }

/* ---- Phase 4 ---- */
.print-frame { position: fixed; right: 0; bottom: 0; width: 0; height: 0; border: 0; visibility: hidden; }
.expense-layout { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.expense-layout .card { margin: 0; }
.tax-table tr.month td:first-child { padding-left: 1.6rem; }
.tax-table tr.quarter td { background: #f4f8f8; font-weight: 700; border-bottom: 2px solid var(--line); }
.tax-table tr.year td { background: var(--accent); color: #fff; font-weight: 700; }
.tax-table tr.current td { background: #fffbe6; }
.tax-table td.positive { color: var(--ok); }
.tax-table td.reserve { font-weight: 700; color: var(--accent-dark); }
.tax-table tr.year td.reserve { color: #fff; }
hr { border: 0; border-top: 1px solid var(--line); margin: 1.25rem 0; }
.row-form select { padding: .5rem .6rem; border: 1px solid #c9d0d3; border-radius: 6px; font: inherit; background: #fff; }
.row-form { flex-wrap: wrap; margin-bottom: .75rem; }
pre { background: #f4f7f8; padding: .75rem; border-radius: 6px; overflow-x: auto; }
.kpi-sub { font-size: .9rem; color: var(--muted); margin: .2rem 0 0; }
@media (max-width: 900px) { .expense-layout { grid-template-columns: 1fr; } }
.small-note { text-align: center; font-size: .85rem; margin: .75rem 0 0; }
.codes { columns: 2; list-style: none; padding: 0; font-size: 1.05rem; }
.codes li { padding: .2rem 0; }
.codes-box { border-color: #e8d57a; background: #fffdf0; }
.qr-setup { margin: .75rem 0; }
.qr-setup img { border: 1px solid var(--line); border-radius: 6px; }
.steps li { margin-bottom: 1rem; }
.stack-form { max-width: 320px; margin-top: .75rem; }
details > summary { cursor: pointer; margin: .5rem 0; color: var(--accent); }
@media print {
    .topbar, .page-head a, .action-bar, form, .btn, .alert:not(.codes-box) { display: none !important; }
    body { background: #fff; }
    .card { border: 0; }
}
.suggested { background: #fff7d1 !important; border-color: #e8c84a !important; }
.extract-status { margin: -.5rem 0 1rem; padding: .45rem .7rem; border-radius: 6px; font-size: .9rem; background: #eef3f3; color: var(--muted); }
.extract-status.ok { background: #fff7d1; color: #6b5300; }
.signature-layout { display: grid; grid-template-columns: 320px 1fr; gap: 1.5rem; align-items: start; }
.signature-frame { width: 100%; min-height: 260px; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
.preview-langs { margin: 0 0 .5rem; font-size: .9rem; }
.mail-form textarea { font-family: inherit; }
@media (max-width: 900px) { .signature-layout { grid-template-columns: 1fr; } }
.help ol, .help ul { padding-left: 1.3rem; }
.help li { margin-bottom: .45rem; }
.help-toc { font-size: .9rem; line-height: 1.9; }
.suggested-sample { background: #fff7d1; padding: 0 .3rem; border-radius: 3px; }
@media print { .help-toc { display: none; } .help .card { page-break-inside: avoid; } }

/* ---- Navigation: desktop + mobile menu ---- */
.topbar { flex-wrap: nowrap; position: relative; z-index: 20; }
.nav-panel { display: flex; align-items: center; gap: 1rem; flex: 1; min-width: 0; }
.mainnav { flex-wrap: nowrap; gap: .1rem; }
.mainnav > a, .mainnav .dropdown summary { font-size: .95rem; padding: .3rem .4rem; white-space: nowrap; }
.userbox { white-space: nowrap; gap: .8rem; font-size: .92rem; }
@media (min-width: 1321px) { .topbar { gap: 1.5rem; } }
.nav-toggle { display: none; }

@media (max-width: 1280px) {
    .user-name small { display: none; }
}

@media (max-width: 1320px) {
    .topbar { flex-wrap: wrap; justify-content: space-between; gap: 0; padding: .6rem 1rem; }
    .nav-toggle {
        display: inline-flex; align-items: center; gap: .5rem; background: none; border: 1px solid var(--line);
        border-radius: 6px; padding: .45rem .8rem; font: inherit; font-weight: 600; color: var(--ink); cursor: pointer;
    }
    .nav-toggle-icon, .nav-toggle-icon::before, .nav-toggle-icon::after {
        display: block; width: 18px; height: 2px; background: currentColor; border-radius: 1px; position: relative; transition: transform .15s;
    }
    .nav-toggle-icon::before, .nav-toggle-icon::after { content: ''; position: absolute; left: 0; }
    .nav-toggle-icon::before { top: -6px; }
    .nav-toggle-icon::after { top: 6px; }
    .topbar.open .nav-toggle-icon { background: transparent; }
    .topbar.open .nav-toggle-icon::before { transform: translateY(6px) rotate(45deg); }
    .topbar.open .nav-toggle-icon::after { transform: translateY(-6px) rotate(-45deg); }

    .nav-panel { display: none; flex: 0 0 100%; width: 100%; flex-direction: column; align-items: stretch; gap: 0; padding: .5rem 0 .25rem; }
    .topbar.open .nav-panel { display: flex; }
    .mainnav { flex-direction: column; gap: 0; }
    .mainnav > a, .dropdown summary { display: block; padding: .8rem .75rem; border-radius: 6px; font-size: 1.05rem; }
    .mainnav > a + a, .mainnav > .dropdown { border-top: 1px solid var(--line); }
    .dropdown-menu { position: static; box-shadow: none; border: 0; min-width: 0; padding: 0 0 .5rem .75rem; }
    .dropdown-menu a { padding: .65rem .75rem; }
    .userbox { flex-direction: column; align-items: stretch; gap: 0; border-top: 2px solid var(--line); margin-top: .5rem; padding-top: .25rem; white-space: normal; }
    .userbox > * { padding: .7rem .75rem; }
    .userbox .user-name { color: var(--muted); font-size: .9rem; padding-bottom: .2rem; }
    .userbox .user-name small { display: inline; }
    .userbox a { text-decoration: none; color: var(--ink); font-size: 1.05rem; }
    .userbox form.inline { display: block; }
    .userbox button.link { font-size: 1.05rem; text-decoration: none; color: var(--err); padding: 0; }
}

@media (max-width: 700px) {
    .container { margin: 1rem auto; padding: 0 .75rem; }
    h1 { font-size: 1.3rem; }
    .card { padding: 1rem; }
    /* wide tables scroll inside their card instead of moving the whole page */
    .card > table.table, .card.flush > table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .lines-editor .line-text { min-width: 220px; }
    .filter-bar > * { flex: 1 1 100%; }
    .action-bar .btn, .action-bar form { flex: 1 1 auto; }
    .action-bar form .btn { width: 100%; }
    .form-actions { flex-wrap: wrap; }
    .totals-box { max-width: none; }
}
