:root {
  --bg: #f4f6fa;
  --surface: #fff;
  --ink: #18212f;
  --muted: #5d6b82;
  --line: #e1e6ef;
  --accent: #1f4fd8;
  --accent-ink: #fff;
  --accent-soft: #e8eefc;
  --danger: #c0262d;
  --danger-soft: #fdeceb;
  --ok: #17803d;
  --ok-soft: #e6f5ec;
  --warn: #9a6200;
  --warn-soft: #fff3d6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20, 30, 50, 0.06), 0 2px 8px rgba(20, 30, 50, 0.04);
  font-family: Inter, system-ui, -apple-system, 'Segoe UI', Roboto, 'Noto Sans', sans-serif;
  color: var(--ink);
  line-height: 1.45;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141d;
    --surface: #171e2a;
    --ink: #e8edf6;
    --muted: #9aa8bf;
    --line: #2a3446;
    --accent: #6d97ff;
    --accent-ink: #0b1220;
    --accent-soft: #1d2a47;
    --danger: #ff7a80;
    --danger-soft: #3a1d20;
    --ok: #52d08a;
    --ok-soft: #14301f;
    --warn: #f0b84a;
    --warn-soft: #3a2d0f;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); font-size: 15px; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.05rem; margin-bottom: 0.75rem; }
h3 { font-size: 0.95rem; }
p { margin: 0 0 0.75rem; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.skip { position: absolute; left: -999px; top: 0; background: var(--surface); padding: 0.5rem 1rem; z-index: 100; }
.skip:focus { left: 0; }

.muted { color: var(--muted); }
.small { font-size: 0.82rem; }
.strong { font-weight: 600; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.overdue { color: var(--danger); font-weight: 600; }
.pre { white-space: pre-wrap; }
.right { text-align: right; }
.hp { position: absolute; left: -9999px; width: 1px; height: 1px; opacity: 0; }

/* ---------- shell ---------- */
.topbar { display: flex; align-items: center; gap: 1rem; padding: 0.6rem 1.25rem; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.brand { display: flex; align-items: center; gap: 0.5rem; font-weight: 700; color: var(--ink); font-size: 1.05rem; }
.brand:hover { text-decoration: none; }
.brand.big { font-size: 1.4rem; margin-bottom: 0.5rem; }
.logo { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); font-weight: 700; }
.nav { display: flex; gap: 0.15rem; overflow-x: auto; flex: 1; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }
.nav a { color: var(--muted); padding: 0.4rem 0.75rem; border-radius: 8px; white-space: nowrap; font-weight: 500; }
.nav a:hover { background: var(--bg); text-decoration: none; color: var(--ink); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.user { display: flex; align-items: center; gap: 0.75rem; color: var(--muted); font-size: 0.9rem; white-space: nowrap; }
.main { max-width: 1280px; margin: 0 auto; padding: 1.25rem; outline: none; }
.loading, .empty { color: var(--muted); padding: 2rem; text-align: center; }
.empty.big { padding: 3.5rem 1rem; background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); }
.error-panel { background: var(--danger-soft); border: 1px solid var(--danger); border-radius: var(--radius); padding: 1.25rem; }
.crumb { margin: 0 0 0.5rem; font-size: 0.9rem; }

.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 0.75rem; margin-bottom: 1rem; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; }
.row.end { justify-content: flex-end; }
.row.between { justify-content: space-between; }
.row.top { align-items: flex-start; margin-top: 0.75rem; }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); margin-bottom: 1rem; min-width: 0; }
.panel.narrow { max-width: 520px; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; align-items: start; }
.grid2.tall { grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); }
@media (max-width: 860px) { .grid2, .grid2.tall { grid-template-columns: 1fr; } }

/* ---------- buttons & forms ---------- */
.btn { font: inherit; font-weight: 500; padding: 0.5rem 0.95rem; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem; text-decoration: none; }
.btn:hover { background: var(--bg); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.small { padding: 0.25rem 0.6rem; font-size: 0.85rem; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.icon-btn { background: none; border: 0; font-size: 1.4rem; line-height: 1; color: var(--muted); cursor: pointer; padding: 0.15rem 0.5rem; border-radius: 6px; }
.icon-btn:hover { background: var(--bg); color: var(--ink); }
input, select, textarea { font: inherit; color: inherit; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 0.5rem 0.65rem; width: 100%; min-width: 0; }
input[type='checkbox'] { width: auto; }
textarea { resize: vertical; }
select { width: auto; max-width: 100%; }
.field { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.88rem; }
.field > span { color: var(--muted); font-weight: 500; }
.field small { color: var(--muted); }
.field select { width: 100%; }
.form { display: flex; flex-direction: column; gap: 0.8rem; }
.form.two-col { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.8rem 1rem; }
.form.two-col .span2 { grid-column: 1 / -1; }
@media (max-width: 640px) { .form.two-col { grid-template-columns: 1fr; } }
.form-error { color: var(--danger); margin: 0; min-height: 1.2em; font-size: 0.9rem; }
.toolbar { display: flex; flex-wrap: wrap; gap: 0.75rem; align-items: center; margin-bottom: 1rem; }
.toolbar input[type='search'] { max-width: 340px; }
.check { display: flex; gap: 0.35rem; align-items: center; color: var(--muted); }
.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin-left: auto; }
.seg { font: inherit; border: 0; background: var(--surface); padding: 0.4rem 0.9rem; cursor: pointer; color: var(--muted); }
.seg.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; }
th { text-align: left; font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); font-weight: 600; padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
th.num { text-align: right; }
td { padding: 0.6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
tr.inactive td { opacity: 0.55; }
td.actions { white-space: nowrap; text-align: right; }
table.items td input, table.items td select { padding: 0.35rem 0.45rem; }
table.items td:nth-child(1) { min-width: 130px; max-width: 190px; }
table.items td:nth-child(2) { min-width: 170px; }
table.items td.num input { text-align: right; width: 86px; }
table.items td.actions { white-space: nowrap; }

/* ---------- badges ---------- */
.badge { display: inline-block; padding: 0.1rem 0.55rem; border-radius: 999px; font-size: 0.78rem; font-weight: 600; background: var(--bg); color: var(--muted); border: 1px solid var(--line); }
.badge.new, .badge.draft, .badge.confirmed, .badge.type-note { background: var(--bg); }
.badge.contacted, .badge.qualified, .badge.sent, .badge.processing, .badge.type-call, .badge.type-email, .badge.type-meeting { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.badge.demo_given, .badge.negotiation, .badge.dispatched { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.won, .badge.accepted, .badge.delivered, .badge.type-admin { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.lost, .badge.rejected, .badge.cancelled { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge.expired { background: var(--bg); color: var(--muted); }

/* ---------- dashboard ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); }
.kpi-label { color: var(--muted); font-size: 0.82rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.kpi-value { font-size: 1.7rem; font-weight: 700; margin: 0.2rem 0; font-variant-numeric: tabular-nums; }
.kpi-sub { color: var(--muted); font-size: 0.85rem; }
.bars { display: flex; flex-direction: column; gap: 0.45rem; }
.bar-row { display: grid; grid-template-columns: 90px 1fr 32px 90px; align-items: center; gap: 0.6rem; color: var(--ink); }
.bar-row:hover { text-decoration: none; background: var(--bg); border-radius: 6px; }
.bar-label { font-size: 0.88rem; }
.bar-track { background: var(--bg); border-radius: 6px; height: 14px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 6px; min-width: 2px; }
.bar-fill.won { background: var(--ok); }
.bar-fill.lost { background: var(--danger); }
.bar-fill.demo_given, .bar-fill.negotiation { background: var(--warn); }
.bar-count { text-align: right; font-weight: 600; }
.bar-value { text-align: right; color: var(--muted); font-size: 0.85rem; }
.mini-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 0.75rem; }
.mini { display: flex; flex-direction: column; gap: 0.2rem; align-items: flex-start; padding: 0.7rem; border: 1px solid var(--line); border-radius: 8px; color: var(--ink); }
.mini:hover { background: var(--bg); text-decoration: none; }
.mini strong { font-size: 1.3rem; }
.feed { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.7rem; }

/* ---------- pipeline board ---------- */
.board { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(235px, 1fr); gap: 0.75rem; overflow-x: auto; padding-bottom: 0.75rem; align-items: start; }
.column { background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.6rem; min-height: 140px; border-top: 3px solid var(--muted); }
.column.contacted, .column.qualified { border-top-color: var(--accent); }
.column.demo_given, .column.negotiation { border-top-color: var(--warn); }
.column.won { border-top-color: var(--ok); }
.column.lost { border-top-color: var(--danger); }
.column.over { outline: 2px dashed var(--accent); outline-offset: -4px; }
.column header { display: flex; align-items: center; gap: 0.4rem; margin-bottom: 0.6rem; }
.column .count { background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 0 0.5rem; font-size: 0.78rem; font-weight: 600; }
.column .sum { margin-left: auto; }
.cards { display: flex; flex-direction: column; gap: 0.5rem; }
.lead-card { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 0.6rem 0.7rem; box-shadow: var(--shadow); cursor: grab; }
.lead-card.dragging { opacity: 0.45; }
.lead-name { font-weight: 600; color: var(--ink); display: block; }
.lead-meta { display: flex; align-items: center; gap: 0.5rem; margin-top: 0.35rem; font-size: 0.85rem; }
.lead-meta .follow { color: var(--muted); }
.lead-meta .follow.overdue { color: var(--danger); font-weight: 600; }
.avatar { margin-left: auto; width: 24px; height: 24px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: inline-grid; place-items: center; font-size: 0.7rem; font-weight: 700; }
select.move { width: 100%; margin-top: 0.5rem; padding: 0.2rem 0.4rem; font-size: 0.8rem; color: var(--muted); }

/* ---------- detail pages ---------- */
.kvs { margin: 0; display: grid; gap: 0.55rem; }
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 0.5rem; }
.kv dt { color: var(--muted); font-size: 0.88rem; }
.kv dd { margin: 0; overflow-wrap: anywhere; white-space: pre-wrap; }
.timeline { list-style: none; margin: 1rem 0 0; padding: 0; display: flex; flex-direction: column; gap: 0.8rem; }
.timeline li { border-left: 3px solid var(--line); padding-left: 0.8rem; }
.timeline li.stage { border-left-color: var(--warn); }
.timeline li.call, .timeline li.email, .timeline li.meeting { border-left-color: var(--accent); }
.tl-head { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.2rem; }
.tl-body { white-space: pre-wrap; overflow-wrap: anywhere; }

/* ---------- quotation doc ---------- */
.doc-meta { display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.totals { display: flex; flex-direction: column; gap: 0.3rem; min-width: 260px; margin-left: auto; }
.totals > div { display: flex; justify-content: space-between; gap: 2rem; font-variant-numeric: tabular-nums; }
.totals .grand { border-top: 2px solid var(--ink); padding-top: 0.35rem; margin-top: 0.2rem; font-weight: 700; font-size: 1.1rem; }
.totals-wrap { display: flex; flex-direction: column; gap: 0.75rem; min-width: 260px; }
.terms { margin-top: 1.25rem; border-top: 1px solid var(--line); padding-top: 0.75rem; }

/* ---------- modal & toasts ---------- */
.overlay { position: fixed; inset: 0; background: rgba(10, 15, 25, 0.5); display: grid; place-items: start center; padding: 6vh 1rem 1rem; overflow-y: auto; z-index: 50; }
.modal { background: var(--surface); border-radius: 12px; width: min(460px, 100%); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35); }
.modal.wide { width: min(720px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 0.9rem 1.1rem; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; }
.modal-body { padding: 1.1rem; }
.toasts { position: fixed; bottom: 1rem; right: 1rem; display: flex; flex-direction: column; gap: 0.5rem; z-index: 60; }
.toast { background: var(--ink); color: var(--bg); padding: 0.6rem 0.9rem; border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3); max-width: 340px; }
.toast.err { background: var(--danger); color: #fff; }

/* ---------- login / public form ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.login-card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 1.75rem; width: min(400px, 100%); box-shadow: var(--shadow); }
body.public .login-card { width: min(480px, 100%); }

/* ---------- print ---------- */
.print-page .sheet { background: #fff; color: #111; max-width: 800px; margin: 1rem auto; padding: 2rem; border: 1px solid var(--line); }
.sheet-head { display: flex; justify-content: space-between; gap: 1rem; border-bottom: 2px solid #111; padding-bottom: 0.75rem; margin-bottom: 1rem; }
.sheet-head h1 { font-size: 1.4rem; }
.sheet-head p { margin: 0.15rem 0; }
.print-page .sheet th, .print-page .sheet .muted, .print-page .sheet .small { color: #444; }
.print-page .sheet td, .print-page .sheet th { border-color: #ccc; }
.print-page .sheet .totals .grand { border-top-color: #111; }
.sheet-foot { margin-top: 2rem; text-align: center; }
@media print {
  body { background: #fff; }
  .topbar, .no-print, .skip, .toasts { display: none !important; }
  .main { padding: 0; max-width: none; }
  .print-page .sheet { border: 0; margin: 0; padding: 0; max-width: none; }
  a { color: inherit; }
}
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; }
  .nav { order: 3; flex-basis: 100%; }
  .main { padding: 0.9rem; }
  .bar-row { grid-template-columns: 70px 1fr 28px; }
  .bar-value { display: none; }
  .kv { grid-template-columns: 100px 1fr; }
}

/* ---------- v0.2: lead cards, follow-up time, permissions ---------- */
.lead-firm { font-size: 0.84rem; color: var(--muted); }
.lead-phone { font-size: 0.84rem; font-weight: 500; }
.inline-fields { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 0.5rem; align-items: center; }
.perm-box { display: flex; flex-direction: column; gap: 0.5rem; margin: 0.25rem 0 0.5rem; }
.perm-row { display: grid; grid-template-columns: 8rem 1fr; gap: 0.5rem; align-items: center; font-size: 0.9rem; }
.perm-row select { width: 100%; }
.perm-check { display: flex; gap: 0.5rem; align-items: flex-start; font-size: 0.88rem; }
.perm-check input { margin-top: 0.2rem; }
.overdue { color: var(--danger); font-weight: 600; }
@media (max-width: 520px) { .inline-fields { grid-template-columns: 1fr; } .perm-row { grid-template-columns: 1fr; } }

.lead-product { font-size: 0.82rem; color: var(--accent); font-weight: 500; }
.time-input { display: inline-flex; align-items: center; gap: 0.25rem; }
.time-input select { width: auto; min-width: 3.4rem; }

/* ---------- quotation in the company's own layout ---------- */
.qdoc { font-size: 0.95rem; line-height: 1.45; color: #111; }
.qdoc .strong, .qdoc strong { font-weight: 700; }
.qdoc-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1rem; }
.qdoc-logo { height: 64px; width: auto; max-width: 45%; }
.q-lbl { display: inline-block; min-width: 4.2rem; }
.qdoc-to { margin: 0.5rem 0 0.75rem; }
.qitems { width: 100%; border-collapse: collapse; }
.qitems th { text-align: left; text-decoration: underline; padding: 0.3rem 0.4rem; font-weight: 700; white-space: nowrap; }
.qitems th.num, .qitems td.num { text-align: right; white-space: nowrap; }
.qitems td { padding: 0.4rem; vertical-align: top; border: 0; }
.q-n { font-weight: 700; width: 2.6rem; }
.q-detail { font-size: 0.92rem; }
.qtotals { margin: 0.5rem 0 0 auto; width: min(18rem, 100%); }
.qtotals div { display: flex; justify-content: space-between; padding: 0.1rem 0.4rem; }
.qtotals .grand { font-weight: 700; border-top: 1px solid #888; margin-top: 0.2rem; padding-top: 0.25rem; }
.qinc { margin: 1rem 0 0.5rem 1rem; }
.qinc-title { font-weight: 700; text-decoration: underline; margin-bottom: 0.2rem; }
.qinc ul { margin: 0; padding-left: 1.4rem; list-style: square; }
.qterms { margin-top: 1rem; }
.qterms table { border-collapse: collapse; }
.qterms td { padding: 0.05rem 0.4rem 0.05rem 0; vertical-align: top; border: 0; }
.qclose { display: flex; justify-content: space-between; gap: 1rem; margin-top: 1.25rem; flex-wrap: wrap; }
.q-sign { margin-top: 1.25rem; }
.qbank { border: 1px solid #333; padding: 0.5rem 0.75rem; min-width: 14rem; align-self: flex-start; }
.qfoot { display: flex; align-items: center; gap: 1rem; margin-top: 1.5rem; padding-top: 0.75rem; font-size: 0.85rem; text-align: center; }
.qfoot > div { flex: 1; }
.qfoot-logo { height: 44px; width: auto; }
@media (max-width: 520px) { .qdoc-top { flex-direction: column-reverse; } }
.inline-fields.three { grid-template-columns: 1fr 1fr 1fr; }
.line-extra td { background: var(--bg); }
.line-extra textarea { width: 100%; }

.stack { display: flex; flex-direction: column; gap: 1rem; align-items: stretch; }
.qpanel { max-width: 860px; width: 100%; }
