:root {
    --bg: #f4f5f7;
    --card: #fff;
    --ink: #1c2230;
    --muted: #6b7280;
    --line: #e2e5ea;
    --brand: #16794a;
    --brand-ink: #fff;
    --danger: #c0392b;
    --radius: 10px;
}
* { box-sizing: border-box; }
body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--ink);
    font-size: 15px;
    line-height: 1.45;
}
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.topbar {
    display: flex; align-items: center; justify-content: space-between;
    background: var(--card); border-bottom: 1px solid var(--line);
    padding: 0 20px; height: 56px; position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; font-size: 16px; color: var(--ink); }
.topbar nav { display: flex; gap: 4px; align-items: center; }
.topbar nav a { padding: 8px 12px; border-radius: 8px; color: var(--muted); font-weight: 500; }
.topbar nav a:hover { background: var(--bg); text-decoration: none; }
.topbar nav a.active { color: var(--brand); background: #eaf5ef; }
.topbar nav a.logout { color: var(--danger); }

.wrap { max-width: 960px; margin: 0 auto; padding: 24px 20px 80px; }

.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; gap: 12px; }
.page-head h1 { font-size: 22px; margin: 0; }
h2.sec { font-size: 15px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 22px 0 10px; border-bottom: 1px solid var(--line); padding-bottom: 6px; }

.btn {
    display: inline-flex; align-items: center; gap: 6px; cursor: pointer;
    border: 1px solid var(--line); background: var(--card); color: var(--ink);
    padding: 9px 16px; border-radius: 8px; font-size: 14px; font-weight: 500;
    text-decoration: none; transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--bg); text-decoration: none; }
.btn-primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn-primary:hover { background: #12633c; }
.btn-danger { color: var(--danger); border-color: #e6c3bd; }
.btn-danger:hover { background: #fbeae7; }
.btn-sm { padding: 5px 11px; font-size: 13px; }
.btn-block { width: 100%; justify-content: center; }
.btn-doc { background: #eaf5ef; border-color: #c9e6d7; color: var(--brand); }

.flash { padding: 11px 15px; border-radius: 8px; margin-bottom: 16px; font-size: 14px; }
.flash-ok { background: #eaf7f0; color: #15794a; border: 1px solid #c6e8d5; }
.flash-err { background: #fdece9; color: #b03325; border: 1px solid #f2ccc5; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 22px; margin-bottom: 18px; }
.card-soft { background: #f0f6f2; border: 1px solid #d6e8dd; border-radius: var(--radius); padding: 14px 18px; margin-bottom: 18px; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; font-weight: 500; }
label.full { display: block; }
input[type=text], input[type=password], input[type=date], select, input[type=file] {
    display: block; width: 100%; margin-top: 4px;
    padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px;
    font-size: 14px; color: var(--ink); background: #fff; font-family: inherit;
}
input:focus, select:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px #eaf5ef; }
.row { display: flex; gap: 14px; }
.row label { flex: 1; }
.form-actions { display: flex; gap: 10px; margin-top: 18px; }

.grid { width: 100%; border-collapse: collapse; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.grid th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); padding: 11px 14px; border-bottom: 1px solid var(--line); background: #fafbfc; }
.grid td { padding: 12px 14px; border-bottom: 1px solid var(--line); vertical-align: top; }
.grid tr:last-child td { border-bottom: 0; }
.doc-links a { display: inline-block; margin-right: 8px; padding: 3px 9px; background: #eaf5ef; border-radius: 6px; font-size: 13px; }
.doc-links-clean { margin-top: 6px; }
.doc-links-clean a { background: #f1f2f4; color: var(--muted); padding: 2px 8px; font-size: 12px; margin-right: 6px; }
.doc-links-clean .small { margin-right: 4px; }
.doc-table { border-collapse: collapse; margin-bottom: 6px; }
.doc-table td { padding: 3px 8px 3px 0; vertical-align: middle; }
.doc-table .doc-name { font-weight: 600; white-space: nowrap; }

.empty { padding: 40px; text-align: center; background: var(--card); border: 1px dashed var(--line); border-radius: var(--radius); }

.items-edit { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
.items-edit th { font-size: 12px; text-transform: uppercase; color: var(--muted); text-align: left; padding: 6px 6px; }
.items-edit td { padding: 3px 6px 3px 0; vertical-align: middle; }
.items-edit td.rownum { color: var(--muted); text-align: center; }
.items-edit input { margin-top: 0; }
.items-edit .i-sum { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.items-edit tfoot td { padding-top: 10px; font-size: 15px; }
.r { text-align: right; }
.btn-x { border: none; background: #fbeae7; color: var(--danger); width: 28px; height: 28px; border-radius: 6px; cursor: pointer; font-size: 16px; line-height: 1; }
.btn-x:hover { background: #f6d5cf; }

.doc-nums { display: flex; gap: 14px; flex-wrap: wrap; }
.doc-nums fieldset { flex: 1; min-width: 200px; border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px 4px; }
.doc-nums legend { font-size: 12px; text-transform: uppercase; color: var(--muted); padding: 0 6px; }
.doc-nums label { display: inline-block; width: 46%; }

.login-box { max-width: 360px; margin: 8vh auto 0; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 30px; }
.login-box h1 { margin: 0 0 4px; font-size: 22px; }
.login-box form { margin-top: 20px; }

.sign-preview { margin: 8px 0; padding: 10px; background: #fafbfc; border: 1px solid var(--line); border-radius: 8px; display: inline-block; }
.sign-preview img { max-width: 240px; max-height: 140px; display: block; }

@media (max-width: 640px) {
    .row { flex-direction: column; gap: 0; }
    .topbar { padding: 0 10px; height: 52px; }
    .brand { font-size: 13px; }
    .topbar nav { gap: 0; }
    .topbar nav a { padding: 7px 7px; font-size: 13px; }
    .wrap { padding: 16px 12px 60px; }
    .card { padding: 16px 14px; }
    .page-head { flex-wrap: wrap; }
    .page-head h1 { font-size: 20px; }
    .form-actions { flex-wrap: wrap; }
    .form-actions .btn { flex: 1 1 auto; justify-content: center; }

    /* iOS зумит при фокусе на поле с font-size < 16px */
    input[type=text], input[type=password], input[type=date],
    input[type=number], input[type=email], input[type=tel],
    select, textarea, input[type=file], .items-edit input {
        font-size: 16px;
    }

    /* Реквизиты документов — фиксеты в столбик */
    .doc-nums fieldset { min-width: 100%; }

    /* Кнопки формирования документов в карточке сделки — в столбик */
    .doc-actions .doc-table,
    .doc-actions .doc-table tbody,
    .doc-actions .doc-table tr,
    .doc-actions .doc-table td { display: block; width: 100%; }
    .doc-actions .doc-table tr { margin-bottom: 12px; }
    .doc-actions .doc-table td { padding: 3px 0; }
    .doc-actions .doc-table .doc-name { margin-bottom: 4px; }
    .doc-actions .doc-table .btn { display: flex; justify-content: center; }

    /* Списки (сделки, клиенты) — карточками вместо таблицы */
    .grid { border: 0; background: transparent; overflow: visible; }
    .grid thead { display: none; }
    .grid tbody, .grid tr, .grid td { display: block; }
    .grid tr {
        background: var(--card); border: 1px solid var(--line);
        border-radius: var(--radius); padding: 12px 14px; margin-bottom: 12px;
    }
    .grid td { width: auto !important; padding: 4px 0; border: 0; text-align: left !important; }
    .grid td.cell-title { font-size: 16px; font-weight: 600; padding-bottom: 6px; }
    .grid td.cell-title .small { font-weight: 400; display: block; margin-top: 2px; }
    .grid td[data-label]::before {
        content: attr(data-label); display: inline-block; min-width: 84px;
        font-size: 12px; text-transform: uppercase; letter-spacing: .02em;
        color: var(--muted); font-weight: 500;
    }
    .grid td.doc-links::before { display: block; margin-bottom: 4px; }
    .grid td.cell-action { padding-top: 10px; }
    .grid td.cell-action .btn { display: flex; justify-content: center; }

    /* Таблица позиций сделки — каждая строка карточкой */
    #items-table thead { display: none; }
    #items-table, #items-table tbody, #items-table tfoot { display: block; }
    .item-row {
        display: grid;
        grid-template-columns: 1fr 1fr auto;
        grid-template-areas:
            "name  name  name"
            "qty   unit  unit"
            "price sum   del";
        gap: 10px;
        padding: 12px;
        border: 1px solid var(--line); border-radius: var(--radius);
        background: #fafbfc; margin-bottom: 12px;
    }
    .item-row td { display: block; padding: 0; }
    .item-row td.rownum { display: none; }
    .item-row td:nth-child(2) { grid-area: name; }
    .item-row td:nth-child(3) { grid-area: qty; }
    .item-row td:nth-child(4) { grid-area: unit; }
    .item-row td:nth-child(5) { grid-area: price; }
    .item-row td.i-sum { grid-area: sum; align-self: end; text-align: right; }
    .item-row td:last-child { grid-area: del; align-self: end; text-align: right; }
    .item-row input { width: 100%; }
    .item-row td:nth-child(2)::before,
    .item-row td:nth-child(3)::before,
    .item-row td:nth-child(4)::before,
    .item-row td:nth-child(5)::before,
    .item-row td.i-sum::before {
        display: block; font-size: 11px; text-transform: uppercase;
        letter-spacing: .03em; color: var(--muted); margin-bottom: 4px; font-weight: 500;
    }
    .item-row td:nth-child(2)::before { content: "Наименование"; }
    .item-row td:nth-child(3)::before { content: "Кол-во"; }
    .item-row td:nth-child(4)::before { content: "Ед."; }
    .item-row td:nth-child(5)::before { content: "Цена, ₽"; }
    .item-row td.i-sum::before { content: "Сумма"; }
    #items-table tfoot tr {
        display: flex; justify-content: space-between; align-items: center;
        padding: 6px 4px 0; font-size: 15px;
    }
    #items-table tfoot td { display: inline; padding: 0; text-align: left; }
    #items-table tfoot td:empty { display: none; }
    #add-item { width: 100%; justify-content: center; margin-top: 2px; }
}
