:root {
  --bg: #F2F3EF;
  --surface: #FFFFFF;
  --surface-2: #E9EBE5;
  --ink: #1A1F26;
  --ink-2: #3C434C;
  --muted: #6A7079;
  --line: #D6D9D2;
  --line-2: #BFC4BC;
  --accent: #2F5BEA;
  --accent-ink: #FFFFFF;
  --accent-soft: #E4EAFC;
  --good: #1E8E5A;
  --good-soft: #DDF3E7;
  --warn: #B8730A;
  --warn-soft: #FBEFD6;
  --bad: #C43D3D;
  --bad-soft: #F9E0E0;
  --seg-cost: #5B6672;
  --seg-over: #8F99A6;
  --seg-tax: #B9C1CB;
  --seg-agent: #D2A45B;
  --shadow: 0 1px 2px rgba(20, 25, 35, .05), 0 10px 28px rgba(20, 25, 35, .08);
  --radius: 10px;
  --focus: 0 0 0 3px var(--accent-soft);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14171C;
    --surface: #1C2026;
    --surface-2: #242930;
    --ink: #E6E8E3;
    --ink-2: #C2C6C0;
    --muted: #8E959F;
    --line: #2D333C;
    --line-2: #3D444E;
    --accent: #7D9BFF;
    --accent-ink: #0F1524;
    --accent-soft: #263255;
    --good: #4CC38A;
    --good-soft: #1B3328;
    --warn: #E0A43A;
    --warn-soft: #3A2E14;
    --bad: #F07373;
    --bad-soft: #3D2020;
    --seg-cost: #9AA5B3;
    --seg-over: #6E7986;
    --seg-tax: #4C5561;
    --seg-agent: #B08A4A;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 28px rgba(0, 0, 0, .35);
    --focus: 0 0 0 3px var(--accent-soft);
  }
}
:root[data-theme="dark"] {
  --bg: #14171C;
  --surface: #1C2026;
  --surface-2: #242930;
  --ink: #E6E8E3;
  --ink-2: #C2C6C0;
  --muted: #8E959F;
  --line: #2D333C;
  --line-2: #3D444E;
  --accent: #7D9BFF;
  --accent-ink: #0F1524;
  --accent-soft: #263255;
  --good: #4CC38A;
  --good-soft: #1B3328;
  --warn: #E0A43A;
  --warn-soft: #3A2E14;
  --bad: #F07373;
  --bad-soft: #3D2020;
  --seg-cost: #9AA5B3;
  --seg-over: #6E7986;
  --seg-tax: #4C5561;
  --seg-agent: #B08A4A;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 10px 28px rgba(0, 0, 0, .35);
  --focus: 0 0 0 3px var(--accent-soft);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 {
  font-family: "IBM Plex Sans Condensed", "Arial Narrow", "Roboto Condensed", system-ui, sans-serif;
  font-weight: 600;
  letter-spacing: -0.01em;
  text-wrap: balance;
  margin: 0;
}
h1 { font-size: clamp(30px, 4vw, 42px); line-height: 1.05; }
h2 { font-size: 20px; line-height: 1.2; }
h3 { font-size: 16px; line-height: 1.3; color: var(--ink-2); }
p { margin: 0; }
.num, input.num, .ladder td, .kv dd, .price-main, .price-sub, .sum {
  font-family: "IBM Plex Mono", ui-monospace, Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
}

.app { max-width: 1180px; margin: 0 auto; padding: 28px 20px 64px; display: flex; flex-direction: column; gap: 22px; }

/* Masthead */
.masthead { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 20px 40px; align-items: end; padding-bottom: 6px; }
.eyebrow { font-size: 12px; font-weight: 500; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-bottom: 8px; }
.lede { color: var(--ink-2); max-width: 62ch; margin-top: 10px; }
.templates { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.templates-label { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-right: 4px; }

/* Buttons and chips */
.btn, .chip {
  font: inherit; cursor: pointer; border-radius: 8px; border: 1px solid var(--line-2);
  background: var(--surface); color: var(--ink); padding: 8px 14px; line-height: 1.2;
  transition: background .15s, border-color .15s, box-shadow .15s;
}
.btn:hover, .chip:hover { border-color: var(--ink-2); }
.btn:focus-visible, .chip:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .link:focus-visible { outline: none; box-shadow: var(--focus); border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 500; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--bad); }
.btn.ghost { border-color: transparent; background: transparent; padding: 6px 8px; color: var(--muted); }
.btn.ghost:hover { color: var(--ink); background: var(--surface-2); }
.chip { border-radius: 999px; padding: 6px 12px; font-size: 14px; }
.chip.small { padding: 4px 10px; font-size: 13px; }
.chip.active { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.link { font: inherit; background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }

/* Layout */
.layout { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 22px; align-items: start; }
.inputs { display: flex; flex-direction: column; gap: 18px; min-width: 0; }
.summary { display: flex; flex-direction: column; gap: 14px; position: sticky; top: 16px; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 20px; }
.panel-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 6px 16px; margin-bottom: 14px; }
.hint { color: var(--muted); font-size: 13px; }
.hint.tight { margin-top: -8px; margin-bottom: 12px; }

/* Fields */
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > span { font-size: 12.5px; font-weight: 500; color: var(--ink-2); }
.field small { font-size: 12px; color: var(--muted); line-height: 1.35; }
input[type="text"], input.num, select, textarea {
  font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line-2); border-radius: 8px;
  padding: 8px 10px; width: 100%; min-width: 0;
}
input.num { text-align: right; }
select { appearance: auto; }
.params { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px 16px; }

/* Items table */
.items-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
.items { width: 100%; border-collapse: collapse; min-width: 740px; table-layout: fixed; }
.items th { font-size: 12px; font-weight: 500; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); text-align: left; padding: 0 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.items th.r, .items td.r { text-align: right; }
.items td { padding: 6px 6px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.items td:first-child, .items th:first-child { padding-left: 0; width: 28px; }
.items td:last-child, .items th:last-child { padding-right: 0; width: 32px; }
.items input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--accent); margin: 0; display: block; }
.items .c-name { width: auto; }
.items .c-cat { width: 168px; }
.items .c-qty { width: 72px; }
.items .c-price { width: 104px; }
.items .c-cur { width: 70px; }
.items .c-sum { width: 96px; }
.items .sum { white-space: nowrap; color: var(--ink); }
.items tr.off td { opacity: .5; }
.items tr.off td:first-child { opacity: 1; }
.items tfoot td { border-bottom: 0; padding-top: 10px; font-weight: 500; }
.items tfoot .sum { font-weight: 500; }
.items .x { font: inherit; background: none; border: 0; color: var(--muted); cursor: pointer; width: 28px; height: 28px; border-radius: 6px; line-height: 1; font-size: 18px; }
.items .x:hover { background: var(--bad-soft); color: var(--bad); }
.add-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 14px; }
.quick-label { color: var(--muted); font-size: 13px; margin-left: 6px; }

/* Price panel */
.price-panel { box-shadow: var(--shadow); border-color: transparent; }
.label { font-size: 12px; font-weight: 500; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.price-main { font-size: 40px; font-weight: 500; line-height: 1.1; margin-top: 4px; letter-spacing: -0.02em; }
.price-sub { font-size: 17px; color: var(--ink-2); margin-top: 2px; }
.price-note { color: var(--muted); font-size: 13px; margin-top: 8px; }
.status { display: inline-flex; align-items: center; gap: 8px; margin-top: 12px; padding: 5px 10px 5px 8px; border-radius: 999px; font-size: 13px; font-weight: 500; background: var(--surface-2); color: var(--ink-2); }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.good { background: var(--good-soft); color: var(--good); }
.status.good::before { background: var(--good); }
.status.warn { background: var(--warn-soft); color: var(--warn); }
.status.warn::before { background: var(--warn); }
.status.bad { background: var(--bad-soft); color: var(--bad); }
.status.bad::before { background: var(--bad); }

.discount { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
.discount-head { display: flex; justify-content: space-between; align-items: baseline; font-size: 13px; font-weight: 500; color: var(--ink-2); }
.discount-head output { font-size: 18px; color: var(--ink); }
input[type="range"] { width: 100%; accent-color: var(--accent); margin: 10px 0 0; }

.breakdown { margin-top: 16px; }
.bar { display: flex; height: 14px; border-radius: 4px; overflow: hidden; background: var(--surface-2); }
.bar span { display: block; height: 100%; min-width: 0; }
.seg-cost { background: var(--seg-cost); }
.seg-over { background: var(--seg-over); }
.seg-tax { background: var(--seg-tax); }
.seg-agent { background: var(--seg-agent); }
.seg-profit { background: var(--accent); }
.seg-loss { background: var(--bad); }
.legend { list-style: none; margin: 10px 0 0; padding: 0; display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; font-size: 13px; }
.legend li { display: contents; }
.legend .k { display: flex; align-items: center; gap: 8px; color: var(--ink-2); }
.legend .k::before { content: ""; width: 10px; height: 10px; border-radius: 2px; background: var(--sw, var(--muted)); flex: none; }
.legend .v { text-align: right; color: var(--ink); }

.kv { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; margin: 16px 0 0; padding-top: 14px; border-top: 1px solid var(--line); }
.kv div { display: contents; }
.kv dt { color: var(--ink-2); font-size: 13.5px; }
.kv dd { margin: 0; text-align: right; font-weight: 500; }
.kv dd.good { color: var(--good); }
.kv dd.bad { color: var(--bad); }

.ladder { width: 100%; border-collapse: collapse; font-size: 13.5px; margin-top: 10px; }
.ladder th { font-size: 11.5px; font-weight: 500; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); text-align: right; padding: 0 6px 6px; border-bottom: 1px solid var(--line); }
.ladder th:first-child, .ladder td:first-child { text-align: left; padding-left: 0; }
.ladder td { text-align: right; padding: 5px 6px; border-bottom: 1px solid var(--line); }
.ladder td:last-child, .ladder th:last-child { padding-right: 0; }
.ladder tr:last-child td { border-bottom: 0; }
.ladder tr.now td { background: var(--accent-soft); }
.ladder tr.now td:first-child { border-radius: 6px 0 0 6px; }
.ladder tr.now td:last-child { border-radius: 0 6px 6px 0; }
.ladder .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.dot.good { background: var(--good); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); }
.ladder .rub { display: block; font-size: 11.5px; color: var(--muted); }

.check { display: grid; grid-template-columns: 1fr 88px; gap: 8px; margin-top: 10px; }
.check-out { margin-top: 10px; font-size: 14px; color: var(--ink-2); min-height: 1.5em; }
.check-out strong { color: var(--ink); font-weight: 500; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions .btn { font-size: 14px; padding: 8px 10px; white-space: nowrap; }
.actions .primary { grid-column: 1 / -1; }

/* History */
.history-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.history-list li { display: grid; grid-template-columns: minmax(0, 1fr) auto auto auto; gap: 4px 16px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.history-list li:first-child { border-top: 0; }
.h-name { font-weight: 500; }
.h-meta { color: var(--muted); font-size: 13px; }
.h-price { text-align: right; white-space: nowrap; }
.h-price .m { display: block; font-size: 12.5px; color: var(--muted); }
.empty { color: var(--muted); }

/* Dialog */
dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--ink); padding: 0; width: min(640px, 92vw); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(10, 12, 16, .45); }
.dlg { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.dlg textarea { min-height: 220px; font-family: "IBM Plex Mono", ui-monospace, Menlo, monospace; font-size: 13px; resize: vertical; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.dlg-err { color: var(--bad); font-size: 13px; }

/* Toast */
.toast { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px; display: flex; gap: 14px; align-items: center; box-shadow: var(--shadow); font-size: 14px; z-index: 10; max-width: 92vw; }
.toast .link { color: inherit; font-weight: 500; }

.foot { color: var(--muted); font-size: 12.5px; max-width: 70ch; }

/* Floating price strip: mobile only, shows while the price panel is off-screen */
.mobile-bar { display: none; }

/* Single column: every panel becomes its own grid item and gets a mobile order */
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; gap: 14px; }
  .inputs, .summary { display: contents; }
  .price-panel { order: 1; }
  .items-panel { order: 2; }
  .params-panel { order: 3; }
  .ladder-panel { order: 4; }
  .check-panel { order: 5; }
  .actions { order: 6; }
  .foot { order: 7; }
  .masthead { grid-template-columns: 1fr; align-items: start; gap: 14px; }

  .mobile-bar {
    display: flex; position: fixed; top: 0; left: 0; right: 0; z-index: 20;
    align-items: baseline; gap: 10px; padding: 10px 16px;
    background: var(--surface); color: var(--ink); border: 0; border-bottom: 1px solid var(--line);
    box-shadow: var(--shadow); font: inherit; text-align: left; cursor: pointer;
  }
  .mobile-bar:focus-visible { outline: none; box-shadow: inset 0 0 0 3px var(--accent-soft); }
  .mb-label { font-size: 11px; font-weight: 500; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
  .mb-price { font-size: 20px; font-weight: 500; }
  .mb-rub { font-size: 13px; color: var(--muted); }
  .mb-status { margin-left: auto; font-size: 12.5px; font-weight: 500; padding: 3px 9px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); white-space: nowrap; }
  .mb-status.good { background: var(--good-soft); color: var(--good); }
  .mb-status.warn { background: var(--warn-soft); color: var(--warn); }
  .mb-status.bad { background: var(--bad-soft); color: var(--bad); }
}

/* Cost items as cards instead of a wide table */
@media (max-width: 800px) {
  .items-wrap { margin: 0; padding: 0; overflow: visible; }
  .items { min-width: 0; display: block; }
  .items thead { display: none; }
  .items tbody, .items tfoot { display: block; }
  .items tbody tr {
    display: grid; align-items: center; gap: 6px;
    grid-template-columns: 24px 64px minmax(0, 1fr) 64px 84px;
    grid-template-areas: "on name name name x" "on cat cat cat cat" "on qty price cur sum";
    padding: 10px 0; border-bottom: 1px solid var(--line);
  }
  .items td[class] { display: block; padding: 0; border: 0; width: auto; }
  .items td.c-on { grid-area: on; align-self: start; }
  .items td.c-on input { margin-top: 11px; }
  .items td.c-name { grid-area: name; }
  .items td.c-cat { grid-area: cat; }
  .items td.c-qty { grid-area: qty; }
  .items td.c-price { grid-area: price; }
  .items td.c-cur { grid-area: cur; }
  .items td.c-sum { grid-area: sum; text-align: right; font-weight: 500; }
  .items td.c-sum::before { content: "= "; color: var(--muted); font-weight: 400; }
  .items td.c-x { grid-area: x; justify-self: end; }
  .items tfoot tr { display: flex; justify-content: space-between; align-items: baseline; padding-top: 12px; }
  .items tfoot td { display: block; padding: 0; border: 0; width: auto; }
  .items tfoot td:empty { display: none; }
}
@media (max-width: 640px) {
  .app { padding: 18px 14px 56px; }
  .lede { font-size: 14px; }
  .params { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .panel { padding: 16px; }
  .history-list li { grid-template-columns: minmax(0, 1fr) auto; }
  .history-list .h-price { grid-column: 2; }
  .price-main { font-size: 34px; }
  input[type="text"], input.num, select, textarea { font-size: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* ===== Хостинг-версия: шапка, вход, админка ===== */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 15; }
.brand { font-family: "IBM Plex Sans Condensed", "Arial Narrow", system-ui, sans-serif; font-weight: 600; font-size: 17px; color: var(--ink); text-decoration: none; letter-spacing: -0.01em; }
.brand span { color: var(--accent); }
.topbar-right { display: flex; align-items: center; gap: 12px; min-width: 0; }
.toplink { color: var(--ink-2); text-decoration: none; font-size: 14px; padding: 4px 8px; border-radius: 6px; }
.toplink:hover { background: var(--surface-2); color: var(--ink); }
.toplink.active { color: var(--ink); font-weight: 500; }
.topuser { color: var(--muted); font-size: 13px; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inline { display: inline; margin: 0; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.flashes { max-width: 1180px; margin: 14px auto 0; padding: 0 20px; display: flex; flex-direction: column; gap: 8px; }
.flash { padding: 9px 12px; border-radius: 8px; font-size: 14px; background: var(--surface-2); color: var(--ink-2); }
.flash.ok { background: var(--good-soft); color: var(--good); }
.flash.error { background: var(--bad-soft); color: var(--bad); }
.muted { color: var(--muted); }
/* Плавающая цена на мобильном: под шапкой, не поверх неё */
@media (max-width: 960px) { .mobile-bar { top: 45px; } }

/* Вход */
.login-wrap { min-height: calc(100vh - 40px); display: flex; align-items: center; justify-content: center; padding: 24px 16px; }
.login { width: min(440px, 100%); box-shadow: var(--shadow); border-color: transparent; padding: 28px 28px 24px; }
.login h1 { font-size: 34px; margin-bottom: 8px; }
.login .lede { margin-top: 4px; margin-bottom: 18px; }
.login-form { display: flex; flex-direction: column; gap: 14px; }
.login a { color: var(--accent); }
.login .flash { margin-bottom: 12px; }
input[type="email"] { font: inherit; color: var(--ink); background: var(--surface); border: 1px solid var(--line-2); border-radius: 8px; padding: 9px 10px; width: 100%; }

/* Админка */
.admin .masthead { grid-template-columns: 1fr; }
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
.admin-table { width: 100%; border-collapse: collapse; font-size: 14px; min-width: 620px; }
.admin-table th { font-size: 11.5px; font-weight: 500; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); text-align: left; padding: 0 8px 8px 0; border-bottom: 1px solid var(--line); white-space: nowrap; }
.admin-table td { padding: 8px 8px 8px 0; border-bottom: 1px solid var(--line); vertical-align: middle; }
.admin-table th.r, .admin-table td.r { text-align: right; }
.admin-table tr.off td { opacity: .5; }
.admin-table a { color: var(--accent); }
.row-form { display: inline-flex; gap: 6px; align-items: center; }
.row-form input[type="text"] { width: 160px; padding: 5px 8px; font-size: 14px; }
.row-form select { width: auto; padding: 5px 8px; font-size: 14px; }
.add-user { display: grid; grid-template-columns: 1.4fr 1.4fr .7fr auto; gap: 12px; align-items: end; margin-top: 16px; }
.settings-form .params { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.check-field .checks { display: flex; gap: 16px; padding-top: 8px; font-size: 14px; }
.check-field .checks label { display: flex; gap: 6px; align-items: center; }
.check-field input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }
.form-actions { margin-top: 16px; }
code { font-family: "IBM Plex Mono", ui-monospace, Menlo, monospace; font-size: 12px; background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

@media (max-width: 640px) {
  .topbar { padding: 8px 12px; }
  .topuser { display: none; }
  .add-user { grid-template-columns: 1fr; }
  .settings-form .params { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .table-wrap { margin: 0 -16px; padding: 0 16px; }
  .login { padding: 22px 18px; }
}
