:root {
  --bg: #f2f4f8;
  --surface: #ffffff;
  --border: #d9dee7;
  --text: #1d2430;
  --muted: #6b7686;
  --primary: #1f5eff;
  --primary-dark: #1848c4;
  --danger: #d64545;
  --success: #1e9e5a;
  --won: #e5f6ec;
  --lost: #fbeaea;
  --shadow: 0 1px 3px rgba(20, 30, 50, .12);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  background: #111c33;
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 40;
  flex-wrap: wrap;
}
.brand { font-size: 17px; font-weight: 700; letter-spacing: .3px; }
.brand span { color: #7aa2ff; }
.topbar nav { display: flex; gap: 4px; flex: 1; }
.tab-btn {
  background: none; border: none; color: #b9c3d8;
  padding: 8px 12px; border-radius: 6px; cursor: pointer; font-size: 14px;
}
.tab-btn:hover { background: rgba(255,255,255,.08); color: #fff; }
.tab-btn.active { background: rgba(255,255,255,.14); color: #fff; font-weight: 600; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.scope-select {
  background: #1d2b4d; color: #dbe2f0; border: 1px solid rgba(255,255,255,.25);
  border-radius: 6px; padding: 7px 8px; font-size: 13px; max-width: 190px;
}
.user-chip {
  display: flex; align-items: center; gap: 7px; padding: 5px 10px;
  background: rgba(255,255,255,.1); border-radius: 20px; font-size: 13px;
  cursor: pointer;
}
.user-chip:hover { background: rgba(255,255,255,.18); }
.user-dot { width: 12px; height: 12px; border-radius: 50%; background: #7aa2ff; display: inline-block; }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid transparent; border-radius: 6px;
  padding: 8px 14px; font-size: 13px; cursor: pointer; font-weight: 600;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-ghost:hover { background: #eef1f6; }
.btn-danger { background: #fff; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.topbar .btn-ghost { background: transparent; color: #dbe2f0; border-color: rgba(255,255,255,.35); }
.topbar .btn-ghost:hover { background: rgba(255,255,255,.1); }

/* ---------- views ---------- */
main { padding: 18px 20px; }
.view { display: none; }
.view.active { display: block; }
.view-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 14px;
}
.view-head h2 { margin: 0; font-size: 18px; }
.view-head input[type=search] {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px;
  width: 300px; font-size: 13px;
}

/* ---------- dashboard ---------- */
.kpi-row { display: flex; gap: 14px; margin-bottom: 16px; flex-wrap: wrap; }
.kpi-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 20px; flex: 1; min-width: 200px; box-shadow: var(--shadow);
}
.kpi-label { font-size: 11.5px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); font-weight: 700; }
.kpi-value { font-size: 28px; font-weight: 800; margin-top: 4px; }
.kpi-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

.dash-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 16px;
}
@media (max-width: 1100px) { .dash-grid { grid-template-columns: 1fr; } }
.dash-panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px; box-shadow: var(--shadow); min-width: 0;
}
.dash-panel h3 { margin: 0 0 8px; font-size: 14px; }
.chart { width: 100%; height: 320px; }

.rep-legend { display: flex; flex-direction: column; gap: 8px; }
.rep-row {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px; cursor: pointer;
}
.rep-row:hover { background: #f6f8fb; }
.rep-row .user-dot { width: 14px; height: 14px; flex-shrink: 0; }
.rep-row .rep-name { font-weight: 600; flex: 1; }
.rep-row .rep-stats { font-size: 12.5px; color: var(--muted); }
.rep-bar-track { height: 6px; background: #eef1f6; border-radius: 3px; overflow: hidden; margin-top: 4px; }
.rep-bar { height: 100%; border-radius: 3px; }

/* ---------- kanban board ---------- */
.board {
  display: flex; gap: 12px; align-items: flex-start;
  overflow-x: auto; padding-bottom: 12px;
  min-height: calc(100vh - 130px);
}
.stage-col {
  background: #e8ecf3; border-radius: 10px;
  min-width: 265px; width: 265px; flex-shrink: 0;
  display: flex; flex-direction: column;
  max-height: calc(100vh - 130px);
}
.stage-col.won  { background: var(--won); }
.stage-col.lost { background: var(--lost); }
.stage-head {
  padding: 10px 12px 6px;
  display: flex; justify-content: space-between; align-items: baseline;
}
.stage-head .stage-name { font-weight: 700; font-size: 13px; text-transform: uppercase; letter-spacing: .4px; }
.stage-head .stage-total { font-size: 12px; color: var(--muted); }
.stage-cards {
  padding: 4px 8px 10px; overflow-y: auto; flex: 1; min-height: 60px;
  display: flex; flex-direction: column; gap: 8px;
}
.stage-cards.drag-over { outline: 2px dashed var(--primary); outline-offset: -4px; border-radius: 8px; }

.deal-card {
  background: var(--surface); border: 1px solid var(--border);
  border-left: 4px solid #b9c3d8;
  border-radius: 8px; padding: 10px 12px; cursor: grab;
  box-shadow: var(--shadow);
  touch-action: none; user-select: none;
}
.deal-card:active { cursor: grabbing; }
.deal-card.dragging { opacity: .45; }
.drag-ghost {
  position: fixed; pointer-events: none; z-index: 90;
  opacity: .9; transform: rotate(2deg);
  box-shadow: 0 10px 30px rgba(10,18,35,.35);
}
.deal-card .deal-title { font-weight: 600; margin-bottom: 4px; }
.deal-card .deal-meta { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; }
.deal-card .deal-value { font-weight: 700; color: var(--text); }
.deal-card .deal-badges { margin-top: 6px; display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.badge {
  font-size: 10.5px; padding: 2px 7px; border-radius: 10px; font-weight: 600;
}
.badge-rep { background: #eef1f6; color: var(--muted); display: inline-flex; align-items: center; gap: 5px; }
.badge-rep .user-dot { width: 8px; height: 8px; }
.badge-type { background: #eef4ff; color: var(--primary); }
.badge-synced { background: var(--won); color: var(--success); }
.badge-syncerr { background: var(--lost); color: var(--danger); }

/* ---------- tables ---------- */
.data-table {
  width: 100%; border-collapse: collapse; background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; overflow: hidden;
  box-shadow: var(--shadow);
}
.data-table th, .data-table td {
  text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.data-table th { background: #f6f8fb; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); }
.data-table tbody tr:hover { background: #f6f8fb; }
.data-table tbody tr { cursor: pointer; }
.data-table .status-success { color: var(--success); font-weight: 700; }
.data-table .status-error { color: var(--danger); font-weight: 700; }
.dash-panel .data-table { box-shadow: none; }

/* ---------- settings ---------- */
.settings-form {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 20px; max-width: 560px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 12px;
}
.settings-form label { display: flex; flex-direction: column; gap: 4px; font-weight: 600; font-size: 13px; }
.settings-form input[type=url], .settings-form input[type=text], .settings-form input[type=password] {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px;
}
.checkbox-label { flex-direction: row !important; align-items: center; gap: 8px !important; }
.settings-actions { display: flex; align-items: center; gap: 10px; margin-top: 4px; }
.status-msg { font-size: 13px; font-weight: 600; }
.status-msg.ok { color: var(--success); }
.status-msg.err { color: var(--danger); }
.hint { color: var(--muted); font-weight: 400; font-size: 12px; }

/* ---------- login ---------- */
.login-body {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: #111c33;
}
.login-card {
  background: var(--surface); border-radius: 14px; padding: 30px 34px;
  width: 360px; max-width: 92vw; box-shadow: 0 16px 50px rgba(0,0,0,.4);
}
.login-card .brand span { color: var(--primary); }
.login-form { display: flex; flex-direction: column; gap: 12px; }
.login-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; }
.login-form input {
  padding: 9px 11px; border: 1px solid var(--border); border-radius: 6px; font-size: 14px;
}
.login-error {
  background: var(--lost); color: var(--danger); border-radius: 6px;
  padding: 8px 12px; font-size: 13px; margin-bottom: 12px; font-weight: 600;
}
.login-notice {
  background: var(--won); color: var(--success); border-radius: 6px;
  padding: 8px 12px; font-size: 13px; margin-bottom: 12px; font-weight: 600;
}
.login-title { margin: 0 0 8px; font-size: 17px; }
.login-links { margin: 12px 0 0; font-size: 13px; }
.login-links a { color: var(--primary); text-decoration: none; font-weight: 600; }
.login-links a:hover { text-decoration: underline; }
.settings-form h4 { margin: 10px 0 0; padding-top: 14px; border-top: 1px solid var(--border); font-size: 14px; }
.settings-form input[type=number], .settings-form input[type=email] {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font-size: 13px;
}
.reset-link-box {
  grid-column: span 2; background: var(--bg); border: 1px dashed var(--border); border-radius: 6px;
  padding: 8px 10px; font-size: 12px; word-break: break-all;
}

/* ---------- modals ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 22, 36, .5);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 40px 16px; z-index: 50; overflow-y: auto;
}
.modal {
  background: var(--surface); border-radius: 12px; width: 640px; max-width: 100%;
  box-shadow: 0 12px 40px rgba(10, 18, 35, .3); padding: 18px 22px 22px;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.modal-head h3 { margin: 0; font-size: 17px; }
.modal-close {
  background: none; border: none; font-size: 24px; line-height: 1;
  cursor: pointer; color: var(--muted);
}
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; font-weight: 600; }
.form-grid .span2 { grid-column: span 2; }
.form-grid input, .form-grid select, .form-grid textarea {
  padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px;
  font-size: 13px; font-family: inherit;
}
.form-grid input[type=color] { padding: 2px; height: 34px; }
.modal h4 { margin: 16px 0 8px; font-size: 13px; }
.lines-table { width: 100%; border-collapse: collapse; margin-bottom: 6px; }
.lines-table th { text-align: left; font-size: 11px; color: var(--muted); text-transform: uppercase; padding: 4px 6px; }
.lines-table td { padding: 3px 4px; }
.lines-table input { width: 100%; padding: 6px 8px; border: 1px solid var(--border); border-radius: 5px; font-size: 13px; }
.lines-table .line-remove { background: none; border: none; color: var(--danger); cursor: pointer; font-size: 16px; }
.modal-actions {
  display: flex; justify-content: flex-end; align-items: center; gap: 10px; margin-top: 18px;
}
.modal-actions .hint { margin-right: auto; }

.note-add { display: flex; gap: 6px; margin-bottom: 8px; }
.note-add input { padding: 6px 8px; border: 1px solid var(--border); border-radius: 5px; font-size: 13px; }
#note-body { flex: 1; }
.notes-list { list-style: none; margin: 0; padding: 0; max-height: 160px; overflow-y: auto; }
.notes-list li { padding: 6px 8px; border-bottom: 1px solid var(--border); font-size: 12.5px; }
.notes-list .note-meta { color: var(--muted); font-size: 11px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: #1d2430; color: #fff; padding: 10px 18px; border-radius: 8px;
  font-size: 13px; z-index: 80; box-shadow: 0 6px 20px rgba(0,0,0,.3);
  max-width: 90vw;
}
.toast.err { background: var(--danger); }
.toast.ok { background: var(--success); }
.hidden { display: none !important; }
