:root {
  --bg: #f6f5f2;
  --panel: #ffffff;
  --ink: #1b1b1a;
  --muted: #6c6a64;
  --line: #e6e3dc;
  --accent: #ff5a1f;
  --accent-ink: #ffffff;
  --accent-soft: #fff0ea;
  --ok: #1f8a4c;
  --ok-soft: #e7f5ec;
  --warn: #a86500;
  --warn-soft: #fff4dc;
  --bad: #c0362c;
  --bad-soft: #fdecea;
  --blue: #2b62d9;
  --blue-soft: #eaf0fd;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141413; --panel: #1d1d1b; --ink: #f1efe9; --muted: #a09d95; --line: #32312d;
    --accent-soft: #3a2017; --ok-soft: #15301f; --warn-soft: #382a10; --bad-soft: #3a1714; --blue-soft: #18233d;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.5 Inter, system-ui, -apple-system, Segoe UI, Arial, sans-serif; }
a { color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--bad); min-height: 1em; margin: 8px 0 0; }
.small { font-size: 12px; }
h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; }

/* Layout */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.side { border-right: 1px solid var(--line); padding: 20px 14px; display: flex; flex-direction: column; gap: 20px; position: sticky; top: 0; height: 100vh; background: var(--panel); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; }
.brand.big { font-size: 20px; justify-content: center; }
.logo { width: 30px; height: 30px; border-radius: 8px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; }
nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
nav a { text-decoration: none; padding: 8px 12px; border-radius: 8px; color: var(--muted); font-weight: 500; }
nav a:hover { background: var(--bg); color: var(--ink); }
nav a.active { background: var(--accent-soft); color: var(--accent); }
.view { padding: 28px 32px 60px; min-width: 0; }
.head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .side { position: sticky; height: auto; flex-direction: row; align-items: center; padding: 10px 16px; gap: 12px; z-index: 5; border-right: 0; border-bottom: 1px solid var(--line); overflow-x: auto; }
  .side .brand { display: none; }
  nav { flex-direction: row; gap: 4px; }
  nav a { white-space: nowrap; padding: 6px 10px; }
  .view { padding: 18px 16px 60px; }
}

/* Panels */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 14px; }
.stats { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin-bottom: 14px; }
.stat .n { font-size: 26px; font-weight: 700; letter-spacing: -.02em; }
.stat .l { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.stat .s { color: var(--muted); font-size: 12px; }
.two { grid-template-columns: 1.4fr 1fr; }
@media (max-width: 900px) { .two { grid-template-columns: 1fr; } }

.banner { padding: 12px 16px; border-radius: var(--radius); margin-bottom: 16px; border: 1px solid; }
.banner.warn { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.banner.ok { background: var(--ok-soft); border-color: transparent; color: var(--ok); }

/* Buttons and inputs */
.btn { appearance: none; border: 1px solid var(--line); background: var(--panel); color: var(--ink); padding: 8px 14px; border-radius: 8px; font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(.95); }
.btn.danger { color: var(--bad); }
.btn.ghost { border-color: transparent; background: transparent; color: var(--muted); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.full { width: 100%; justify-content: center; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea { font: inherit; color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { min-height: 220px; resize: vertical; line-height: 1.55; }
label { display: block; font-weight: 500; font-size: 13px; margin-bottom: 4px; }
.field { margin-bottom: 12px; }
.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
@media (max-width: 600px) { .fields { grid-template-columns: 1fr; } }
input[type=checkbox] { width: auto; }

/* Table */
.tablewrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; background: var(--bg); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--bg); }
td .sub { color: var(--muted); font-size: 12px; }

/* Badges */
.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; font-weight: 500; background: var(--bg); color: var(--muted); white-space: nowrap; }
.b-new { background: var(--blue-soft); color: var(--blue); }
.b-contacted { background: var(--bg); color: var(--ink); }
.b-replied, .b-demo_agreed { background: var(--warn-soft); color: var(--warn); }
.b-demo_sent, .b-contract_sent { background: var(--accent-soft); color: var(--accent); }
.b-won, .b-ok { background: var(--ok-soft); color: var(--ok); }
.b-lost, .b-bad { background: var(--bad-soft); color: var(--bad); }

/* Pipeline */
.board { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(230px, 1fr); gap: 12px; overflow-x: auto; padding-bottom: 12px; }
.col { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; min-height: 300px; }
.col.over { outline: 2px dashed var(--accent); }
.col-h { padding: 12px 14px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; font-weight: 600; }
.col-b { padding: 10px; display: flex; flex-direction: column; gap: 8px; flex: 1; }
.lcard { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px; cursor: grab; }
.lcard:active { cursor: grabbing; }
.lcard .t { font-weight: 600; }
.lcard .m { color: var(--muted); font-size: 12px; }
.lcard select { margin-top: 6px; padding: 4px 6px; font-size: 12px; }

/* Bars chart */
.bars { display: flex; align-items: flex-end; gap: 6px; height: 140px; padding-top: 8px; }
.bars .b { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 4px; min-width: 0; height: 100%; }
.bars .bar { width: 100%; max-width: 28px; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 2px; position: relative; }
.bars .bar .o { position: absolute; bottom: 0; left: 0; right: 0; background: rgba(0,0,0,.18); border-radius: 4px 4px 0 0; }
.bars .d { font-size: 10px; color: var(--muted); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }

.stagebar { display: flex; flex-direction: column; gap: 8px; }
.stagebar .r { display: grid; grid-template-columns: 110px 1fr 36px; gap: 8px; align-items: center; font-size: 13px; }
.stagebar .t { background: var(--bg); border-radius: 99px; height: 8px; overflow: hidden; }
.stagebar .f { background: var(--accent); height: 100%; border-radius: 99px; }

/* Modal & drawer */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 50; display: flex; justify-content: center; align-items: flex-start; padding: 40px 16px; overflow-y: auto; }
.modal { background: var(--panel); border-radius: 14px; width: 100%; max-width: 640px; padding: 22px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal.wide { max-width: 980px; }
.modal-h { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.modal-f { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.overlay.drawer { justify-content: flex-end; padding: 0; align-items: stretch; }
.overlay.drawer .modal { max-width: 560px; border-radius: 0; min-height: 100vh; overflow-y: auto; }
.x { background: none; border: 0; font-size: 22px; cursor: pointer; color: var(--muted); line-height: 1; }

.split { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 760px) { .split { grid-template-columns: 1fr; } }
.preview { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 14px; white-space: pre-wrap; font-size: 14px; min-height: 220px; }
.preview .subj { font-weight: 600; margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid var(--line); white-space: normal; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin: 6px 0 10px; }
.chip { font-size: 11px; padding: 3px 7px; border-radius: 6px; background: var(--bg); border: 1px solid var(--line); cursor: pointer; font-family: ui-monospace, Menlo, monospace; }
.chip:hover { border-color: var(--accent); color: var(--accent); }

.step { display: grid; grid-template-columns: 28px 1fr 120px 32px; gap: 8px; align-items: center; margin-bottom: 8px; }
.step .num { width: 28px; height: 28px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; font-size: 12px; }
.seq-steps { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 10px 0; }
.seq-steps .arrow { color: var(--muted); font-size: 12px; }

.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13px; display: flex; gap: 10px; }
.timeline li:last-child { border-bottom: 0; }
.timeline .when { color: var(--muted); font-size: 12px; white-space: nowrap; min-width: 92px; }
.msg { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin-bottom: 8px; }
.msg summary { cursor: pointer; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.msg pre { white-space: pre-wrap; font: inherit; font-size: 13px; color: var(--muted); margin: 10px 0 0; }

.bulkbar { position: sticky; bottom: 16px; margin-top: 12px; background: var(--ink); color: var(--bg); border-radius: 12px; padding: 10px 14px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; box-shadow: 0 8px 30px rgba(0,0,0,.2); }
.bulkbar select { width: auto; padding: 5px 8px; }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }

.toasts { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.2); max-width: 360px; }
.toast.bad { background: var(--bad); color: #fff; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 32px; width: 100%; max-width: 360px; text-align: center; display: flex; flex-direction: column; gap: 12px; box-shadow: var(--shadow); }
.login-card p { margin: 0; }

dl.kv { display: grid; grid-template-columns: 160px 1fr; gap: 6px 12px; margin: 0; font-size: 13px; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; word-break: break-word; }
code { font-family: ui-monospace, Menlo, monospace; font-size: 12px; background: var(--bg); padding: 1px 5px; border-radius: 4px; }
