:root {
  --bg: #f4f6fa; --panel: #ffffff; --text: #182033; --muted: #687086; --line: #e3e7ef;
  --primary: #2563eb; --primary-ink: #ffffff; --primary-soft: #e8efff;
  --good: #15803d; --good-soft: #dcf5e4; --bad: #c62828; --bad-soft: #fde4e4;
  --warn: #a15c00; --warn-soft: #fff1d6; --chip: #eef1f6;
  --sidebar: #111827; --sidebar-ink: #cdd3df; --sidebar-active: #1f2a40;
  --radius: 10px; --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e131c; --panel: #161d29; --text: #e6e9ef; --muted: #96a0b3; --line: #273041;
    --primary: #4f86f7; --primary-soft: #1c2a47; --good: #4ade80; --good-soft: #143322;
    --bad: #f87171; --bad-soft: #3a1717; --warn: #fbbf24; --warn-soft: #3a2c10; --chip: #232c3b;
    --sidebar: #0a0e15; --sidebar-active: #1a2333; --shadow: none;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
h1 { font-size: 22px; margin: 0 0 2px; }
h2 { font-size: 15px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 18px 0 8px; }
p { margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.boot { padding: 40px; text-align: center; color: var(--muted); }

/* buttons & inputs */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--panel); color: var(--text);
  padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 500; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn-primary:hover { filter: brightness(1.08); border-color: var(--primary); }
.btn-danger { color: var(--bad); border-color: var(--bad); background: transparent; }
.btn-small { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .6; cursor: wait; }
.icon-btn { border: 0; background: transparent; color: inherit; font-size: 22px; line-height: 1; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.link { background: none; border: 0; padding: 0; font: inherit; color: var(--primary); cursor: pointer; text-decoration: none; }
.link:hover { text-decoration: underline; }
input, select, textarea { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%; }
input:focus, select:focus, textarea:focus, .btn:focus-visible, a:focus-visible, .deal-card:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--primary); }
textarea { resize: vertical; }

/* auth */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-card { width: 100%; max-width: 400px; background: var(--panel); border-radius: 14px; padding: 28px; box-shadow: var(--shadow); border: 1px solid var(--line); }
.auth-card h1 { margin-top: 18px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; }
.brand.big { font-size: 18px; }
.logo { width: 30px; height: 30px; border-radius: 8px; background: var(--primary); color: #fff; display: grid; place-items: center; font-weight: 800; }

/* shell */
.shell { display: flex; min-height: 100vh; }
.sidebar { width: 236px; flex: none; background: var(--sidebar); color: var(--sidebar-ink); display: flex; flex-direction: column; padding: 18px 12px; position: sticky; top: 0; height: 100vh; }
.sidebar .brand { color: #fff; padding: 0 8px 18px; }
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
.nav a { color: var(--sidebar-ink); text-decoration: none; padding: 9px 10px; border-radius: 8px; display: flex; gap: 10px; align-items: center; }
.nav a:hover { background: var(--sidebar-active); color: #fff; }
.nav a.active { background: var(--primary); color: #fff; }
.nav-icon { width: 18px; text-align: center; opacity: .85; }
.me { border-top: 1px solid #2a3346; padding: 14px 8px 0; display: flex; flex-direction: column; gap: 6px; }
.me strong { color: #fff; }
.me .muted { color: #8b94a7; }
.me-actions { display: flex; gap: 16px; }
.me .link { color: #9dbbff; }
.main { flex: 1; min-width: 0; }
.page { padding: 26px 28px 60px; max-width: 1400px; }
.mobile-bar { display: none; }

.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head p { max-width: 640px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.toolbar { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.toolbar > * { width: auto; min-width: 170px; }
.toolbar input[type=search] { flex: 1; max-width: 360px; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card.narrow { max-width: 460px; }
.roles-help p { margin: 0; color: var(--muted); }
.roles-help strong { color: var(--text); }

/* stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat-label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; font-weight: 600; }
.stat-value { font-size: 24px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.stat-sub { color: var(--muted); font-size: 12px; }
.stat-good .stat-value { color: var(--good); }
.stat-bad .stat-value { color: var(--bad); }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); gap: 16px; align-items: start; }
.grid .card { margin: 0; }

/* tables */
.table-wrap { padding: 0; overflow-x: auto; }
.table-wrap h2 { padding: 16px 18px 0; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: var(--panel); white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--primary-soft); }
tr.done td { opacity: .55; }
tr.done td strong { text-decoration: line-through; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 28px; }
.check-cell { width: 40px; }
.clamp { display: -webkit-box; -webkit-line-clamp: 1; line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden; max-width: 420px; }
table.mini td { padding: 6px 4px; border: 0; }
.w-bar { width: 45%; }
.bar { display: block; height: 8px; background: var(--chip); border-radius: 4px; overflow: hidden; }
.bar-fill { display: block; height: 100%; background: var(--primary); border-radius: 4px; }
.bar-fill.good { background: var(--good); }
.bar-fill.bad { background: var(--bad); }
.inline-select { width: auto; padding: 4px 8px; font-size: 13px; }

/* lists */
.list { list-style: none; margin: 0; padding: 0; }
.list li { padding: 8px 0; border-bottom: 1px solid var(--line); display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
.list li:last-child { border-bottom: 0; }
.right { margin-left: auto; }
.activity li { font-size: 13px; }

/* badges */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--chip); color: var(--muted); white-space: nowrap; }
.badge-p-low, .badge-p-medium, .badge-p-high { text-transform: capitalize; }
.badge-good, .badge-s-won, .badge-o-delivered { background: var(--good-soft); color: var(--good); }
.badge-bad, .badge-s-lost, .badge-o-cancelled, .badge-p-high { background: var(--bad-soft); color: var(--bad); }
.badge-warn, .badge-p-medium, .badge-o-pending { background: var(--warn-soft); color: var(--warn); }
.badge-s-proposal, .badge-s-negotiation, .badge-o-processing, .badge-o-dispatched, .badge-sales, .badge-r-sales { background: var(--primary-soft); color: var(--primary); }
.badge-ops, .badge-r-operations { background: #efe6ff; color: #6d28d9; }
.badge-r-admin { background: var(--bad-soft); color: var(--bad); }
.badge-r-manager { background: var(--warn-soft); color: var(--warn); }
@media (prefers-color-scheme: dark) { .badge-ops, .badge-r-operations { background: #2b1f47; color: #c4a8ff; } }

/* pipeline board */
.board { display: grid; grid-template-columns: repeat(6, minmax(220px, 1fr)); gap: 12px; overflow-x: auto; padding-bottom: 8px; }
.column { background: var(--chip); border-radius: var(--radius); padding: 10px; min-height: 200px; display: flex; flex-direction: column; gap: 8px; border-top: 3px solid var(--muted); }
.col-new { border-top-color: #94a3b8; } .col-contacted { border-top-color: #38bdf8; } .col-proposal { border-top-color: #6366f1; }
.col-negotiation { border-top-color: #f59e0b; } .col-won { border-top-color: var(--good); } .col-lost { border-top-color: var(--bad); }
.column-head { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; padding: 2px 2px 4px; flex-wrap: wrap; }
.deal-card { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px; cursor: pointer; display: flex; flex-direction: column; gap: 6px; box-shadow: var(--shadow); }
.deal-card:hover { border-color: var(--primary); }
.deal-title { font-weight: 600; }
.deal-meta { display: flex; justify-content: space-between; gap: 6px; }
.empty-col { text-align: center; padding: 20px 0; }

/* modal */
.overlay { position: fixed; inset: 0; background: rgba(10, 14, 22, .55); display: grid; place-items: center; padding: 16px; z-index: 50; overflow-y: auto; }
.modal { background: var(--panel); border-radius: 14px; width: 100%; max-width: 560px; box-shadow: 0 20px 50px rgba(0, 0, 0, .3); max-height: calc(100vh - 32px); display: flex; flex-direction: column; }
.modal.wide { max-width: 760px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 17px; overflow-wrap: anywhere; }
.modal-body { padding: 18px 20px 20px; overflow-y: auto; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 14px; }
.field { display: flex; flex-direction: column; gap: 4px; }
.field.full, .form-grid .full { grid-column: 1 / -1; }
.field label { font-weight: 600; font-size: 13px; }
.field.check { flex-direction: row-reverse; justify-content: flex-end; align-items: center; gap: 8px; grid-column: 1 / -1; }
.req { color: var(--bad); }
.hint { color: var(--muted); font-size: 12px; }
.form-error { grid-column: 1 / -1; color: var(--bad); font-weight: 500; min-height: 0; }
.form-error:empty { display: none; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.form-grid .form-actions { margin-top: 4px; }
.details { display: grid; grid-template-columns: 150px 1fr; gap: 8px 12px; margin: 0; }
.details dt { color: var(--muted); }
.details dd { margin: 0; overflow-wrap: anywhere; }
.pre { white-space: pre-wrap; }
.notes-panel { border-top: 1px solid var(--line); margin-top: 18px; }
.note-form { display: flex; gap: 8px; align-items: flex-end; margin-bottom: 10px; }
.notes { list-style: none; margin: 0; padding: 0; }
.notes li { padding: 10px 0; border-bottom: 1px solid var(--line); }
.note-meta { font-size: 12px; color: var(--muted); }
.note-body { white-space: pre-wrap; overflow-wrap: anywhere; }

/* toast */
#toast { position: fixed; bottom: 20px; left: 50%; transform: translate(-50%, 20px); background: #111827; color: #fff; padding: 10px 18px; border-radius: 8px; opacity: 0; pointer-events: none; transition: .2s; z-index: 100; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--bad); }

/* mobile */
@media (max-width: 860px) {
  .sidebar { position: fixed; left: 0; top: 0; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  body.nav-open .sidebar { transform: none; box-shadow: 0 0 0 100vmax rgba(10, 14, 22, .45); }
  .mobile-bar { display: flex; align-items: center; gap: 8px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 30; }
  .page { padding: 18px 16px 50px; }
  .grid { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .details { grid-template-columns: 1fr; gap: 2px; }
  .details dd { margin-bottom: 8px; }
  .toolbar > * { flex: 1 1 150px; min-width: 0; }
  .board { grid-template-columns: repeat(6, 78vw); }
}

/* types, timeline, reports */
.badge-type { background: #e6f6f4; color: #0f766e; text-transform: none; }
@media (prefers-color-scheme: dark) { .badge-type { background: #12302d; color: #5eead4; } }
.good-text { color: var(--good); font-weight: 600; }
.bad-text { color: var(--bad); font-weight: 600; }
.timeline { list-style: none; margin: 0; padding: 0 0 0 14px; border-left: 2px solid var(--line); }
.timeline li { position: relative; padding: 6px 0 10px 10px; }
.timeline li::before { content: ''; position: absolute; left: -20px; top: 11px; width: 10px; height: 10px; border-radius: 50%; background: var(--primary); border: 2px solid var(--panel); }
.tl-when { font-size: 12px; color: var(--muted); }
.section-title { font-size: 18px; margin: 22px 0 12px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head h2 { margin: 0; }
.card .table-wrap { margin: 0 -18px -16px; }
.inline-label { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 13px; color: var(--muted); }
.inline-label input { width: auto; }
.details + h3 { margin-top: 16px; }
