/* AOF — общий стиль. Цвета только через переменные, тёмная тема ниже. */
:root {
  --bg: #f4f1ea;
  --card: #fffdf8;
  --soft: #efeadf;
  --line: #e2dccd;
  --text: #1d2620;
  --muted: #6b7268;
  --brand: #1f4d3a;
  --brand-2: #2f6b50;
  --brand-soft: #dcebd9;
  --accent: #b8862f;
  --ok: #2e7d4f;
  --warn: #b7791f;
  --bad: #b23b3b;
  --bad-soft: #f6e0dc;
  --ok-soft: #dff0e2;
  --warn-soft: #f7ecd6;
  --shadow: 0 1px 2px rgba(30, 40, 30, .06), 0 4px 16px rgba(30, 40, 30, .05);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121714; --card: #1a211d; --soft: #222b25; --line: #2e3a32; --text: #e7ece6; --muted: #98a399;
    --brand: #7fc29b; --brand-2: #9ad3b0; --brand-soft: #23392d; --accent: #d9a84e;
    --ok: #74c493; --warn: #e0b25a; --bad: #e57d72; --bad-soft: #3a2422; --ok-soft: #1f3528; --warn-soft: #3a3020;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121714; --card: #1a211d; --soft: #222b25; --line: #2e3a32; --text: #e7ece6; --muted: #98a399;
  --brand: #7fc29b; --brand-2: #9ad3b0; --brand-soft: #23392d; --accent: #d9a84e;
  --ok: #74c493; --warn: #e0b25a; --bad: #e57d72; --bad-soft: #3a2422; --ok-soft: #1f3528; --warn-soft: #3a3020;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  display: grid; grid-template-columns: 248px 1fr; min-height: 100vh;
}
a { color: var(--brand-2); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.55rem; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }
h3 { font-size: 1rem; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.ic { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* боковое меню */
.side {
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: 10px;
  padding: 16px 12px; background: var(--card); border-right: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 6px; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand img { border-radius: 10px; }
.brand span { display: flex; flex-direction: column; line-height: 1.15; }
.brand b { font-size: 1.15rem; letter-spacing: .04em; }
.brand small { color: var(--muted); font-size: .78rem; }
.side nav { display: flex; flex-direction: column; gap: 1px; overflow-y: auto; flex: 1; margin: 0 -4px; padding: 0 4px; }
.side nav a {
  display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 10px; color: var(--text);
}
.side nav a:hover { background: var(--soft); text-decoration: none; }
.side nav a.on { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.side .grp { margin: 12px 10px 4px; font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.new-order { justify-content: center; }
.side-user { display: flex; align-items: center; gap: 6px; padding: 8px 6px 0; border-top: 1px solid var(--line); }
.side-user a { display: flex; flex-direction: column; flex: 1; min-width: 0; color: var(--text); line-height: 1.2; }
.side-user a:hover { text-decoration: none; }
.side-user b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.side-foot { display: flex; align-items: center; gap: 10px; padding: 8px 6px 0; border-top: 1px solid var(--line); }
.lang { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.lang a { padding: 3px 9px; font-size: .8rem; color: var(--muted); }
.lang a.on { background: var(--brand); color: var(--card); }
.lang a:hover { text-decoration: none; }
.theme-btn { border: 1px solid var(--line); background: none; color: var(--text); border-radius: 8px; padding: 2px 8px; cursor: pointer; }
.side-foot .muted { margin-left: auto; }

/* основное */
.main { padding: 24px 28px 48px; min-width: 0; display: flex; flex-direction: column; gap: 16px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: 8px; flex-wrap: wrap; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.card.flush { padding: 0; overflow: hidden; }
.card.empty { text-align: center; color: var(--muted); padding: 40px 18px; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.four { grid-template-columns: repeat(4, minmax(0, 1fr)); }

/* плитки с цифрами */
.stat { display: flex; flex-direction: column; gap: 4px; }
.stat .label { color: var(--muted); font-size: .85rem; }
.stat .value { font-size: 1.5rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.stat .sub { color: var(--muted); font-size: .8rem; }

/* кнопки */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; min-height: 38px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--card); color: var(--text);
  font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--soft); text-decoration: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--card); }
.btn.primary:hover { background: var(--brand-2); }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 35%, var(--line)); }
.btn.danger:hover { background: var(--bad-soft); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: .85rem; }
.btn.ghost { border-color: transparent; background: none; }

/* формы */
form.form { display: flex; flex-direction: column; gap: 12px; }
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: .95rem; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--card); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 10px; min-height: 38px; width: 100%;
}
input[type=checkbox] { width: auto; min-height: 0; accent-color: var(--brand); }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--brand) 40%, transparent); outline-offset: 0; border-color: var(--brand); }
textarea { min-height: 70px; resize: vertical; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; }
.filters label { min-width: 140px; }
.filters .grow { flex: 1; min-width: 200px; }
.filters .fold-btn { display: none; }

/* таблицы */
.table-wrap, .card.flush.table-wrap { overflow-x: auto; }
table.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { padding: 8px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.table th { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; background: var(--soft); position: sticky; top: 0; }
.table tbody tr:hover { background: color-mix(in srgb, var(--soft) 60%, transparent); }
.table tfoot td { font-weight: 650; background: var(--soft); }
.table td.num, .table th.num { text-align: right; }
.table input { min-height: 32px; padding: 4px 8px; }
.row-link { cursor: pointer; }

/* значки */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: .78rem; font-weight: 600; background: var(--soft); color: var(--muted); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.brand { background: var(--brand-soft); color: var(--brand); }

.flash { padding: 10px 14px; border-radius: 10px; background: var(--ok-soft); color: var(--ok); border: 1px solid color-mix(in srgb, var(--ok) 25%, transparent); }
.flash.error { background: var(--bad-soft); color: var(--bad); border-color: color-mix(in srgb, var(--bad) 25%, transparent); }
.flash.warn { background: var(--warn-soft); color: var(--warn); }

.pager { display: flex; gap: 6px; align-items: center; justify-content: flex-end; padding: 10px 14px; }

/* строки заказа в форме */
.lines .line { display: grid; grid-template-columns: minmax(0, 1fr) 110px 120px 120px 40px; gap: 8px; align-items: center; }
.lines .line + .line { margin-top: 6px; }
.lines .head { font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.totals { display: grid; grid-template-columns: auto auto; gap: 4px 24px; justify-content: end; font-variant-numeric: tabular-nums; }
.totals .grand { font-size: 1.15rem; font-weight: 700; }

/* телефон и планшет: верхняя панель, меню-шторка, нижние вкладки (на компьютере скрыты) */
.topbar, .bottombar, .side-backdrop { display: none; }
@media (max-width: 1000px) {
  body { display: block; padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  .topbar {
    position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 10px;
    padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top));
    background: var(--card); border-bottom: 1px solid var(--line);
  }
  .tb-menu { border: 0; background: none; color: var(--text); padding: 6px; border-radius: 10px; cursor: pointer; }
  .tb-menu .ic { width: 24px; height: 24px; }
  .tb-brand { display: flex; align-items: center; gap: 8px; color: var(--text); }
  .tb-brand:hover { text-decoration: none; }
  .tb-brand img { border-radius: 8px; }
  .tb-page { flex: 1; min-width: 0; color: var(--muted); font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .side {
    position: fixed; z-index: 40; top: 0; left: 0; bottom: 0; width: min(300px, 86vw); height: auto;
    transform: translateX(-105%); transition: transform .22s ease; box-shadow: 0 0 40px rgba(0, 0, 0, .35);
    padding-top: calc(16px + env(safe-area-inset-top));
  }
  body.menu-open .side { transform: none; }
  body.menu-open .side-backdrop { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, .45); }
  body.menu-open { overflow: hidden; }
  .side .new-order { display: none; }
  .bottombar {
    position: fixed; z-index: 20; left: 0; right: 0; bottom: 0; display: flex;
    background: var(--card); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
  }
  .bottombar a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 2px 9px; color: var(--muted); font-size: .72rem; }
  .bottombar a:hover { text-decoration: none; }
  .bottombar a.on { color: var(--brand); font-weight: 600; }
  .bottombar .ic { width: 22px; height: 22px; }
  .grid.two, .grid.three, .grid.four { grid-template-columns: 1fr 1fr; }
  .main { padding: 14px 12px 20px; }
  .lines .line { grid-template-columns: 1fr 80px 90px; }
  .lines .line .amt, .lines .head .amt { display: none; }
}

@media (max-width: 600px) {
  .grid.two, .grid.three { grid-template-columns: 1fr; }
  .grid.four { grid-template-columns: 1fr 1fr; gap: 10px; }
  .grid.three:has(> .stat) { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .grid.three > .stat { padding: 10px; }
  .grid.three > .stat .value { font-size: 1rem; }
  .grid.three > .stat .label, .grid.three > .stat .sub { font-size: .72rem; }
  .filters.folded > label:not(:first-of-type) { display: none; }
  .filters .fold-btn { display: inline-flex; }
  .card { padding: 14px; }
  .stat .value { font-size: 1.25rem; }
  h1 { font-size: 1.3rem; }
  .page-head .actions { width: 100%; }

  /* таблицы → карточки (класс cards ставит app.js, подписи — data-label из шапки) */
  table.cards, table.cards > tbody, table.cards > tfoot { display: block; width: 100%; }
  table.cards > thead { display: none; }
  table.cards.cap-head > thead, table.cards.cap-head > thead > tr { display: block; }
  table.cards.cap-head > thead th { display: block; position: static; }
  table.cards.cap-head > thead th:empty { display: none; }
  table.cards tr {
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px 12px;
    padding: 12px 14px; border-bottom: 1px solid var(--line);
  }
  table.cards td { display: block; padding: 0; border: 0; text-align: left; min-width: 0; overflow-wrap: anywhere; }
  table.cards td:first-child, table.cards td:not([data-label]) { grid-column: 1 / -1; }
  table.cards td:first-child { font-size: 1rem; }
  table.cards td[data-label]::before {
    content: attr(data-label); display: block; margin-bottom: 1px;
    font-size: .68rem; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--muted);
  }
  table.cards td.num { text-align: left; }
  table.cards.cards-num tr { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px 8px; }
  table.cards td:empty, table.cards td.c-empty { display: none; }
  table.cards tr:not(.open) td.c-more { display: none; }
  table.cards tr.open .c-toggle { background: var(--soft); }
  table.cards .c-toggle::after { content: "▾"; margin-left: 4px; }
  table.cards tr.open .c-toggle::after { content: "▴"; }
  table.cards > tfoot tr { background: var(--soft); }
  table.cards td input:not([type=checkbox]):not([type=radio]), table.cards td select { width: 100%; min-width: 0; max-width: 100%; }
  table.cards td[data-label]::before { white-space: normal; overflow-wrap: break-word; line-height: 1.25; }
  table.cards.cards-num tr { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media print {
  body { display: block; background: #fff; color: #000; padding: 0; }
  .side, .topbar, .bottombar, .side-backdrop, .no-print, .flash { display: none !important; }
  .main { padding: 0; }
  .card { box-shadow: none; border: 0; padding: 0; }
}
