/* ===== Fonts (self-hosted) ===== */
@font-face { font-family: 'Playfair Display'; font-weight: 700; font-style: normal; font-display: swap; src: url('/fonts/playfair-700.woff2') format('woff2'); }
@font-face { font-family: 'Montserrat'; font-weight: 400; font-style: normal; font-display: swap; src: url('/fonts/montserrat-400.woff2') format('woff2'); }
@font-face { font-family: 'Montserrat'; font-weight: 500; font-style: normal; font-display: swap; src: url('/fonts/montserrat-500.woff2') format('woff2'); }
@font-face { font-family: 'Montserrat'; font-weight: 600; font-style: normal; font-display: swap; src: url('/fonts/montserrat-600.woff2') format('woff2'); }
@font-face { font-family: 'Montserrat'; font-weight: 700; font-style: normal; font-display: swap; src: url('/fonts/montserrat-700.woff2') format('woff2'); }

/* ===== MS Finance CRM — Dark Premium Desktop-First ===== */
:root {
  color-scheme: dark;
  --bg: #1a1a1a;
  --surface: #222222;
  --surface-2: #2a2a2a;
  --border: rgba(255,255,255,.08);
  --text: #f0f0f0;
  --muted: #7f7f7f;
  --primary: #C8102E;
  --primary-hover: #a50d25;
  --primary-dim: rgba(200,16,46,.10);
  --accent: #C9A84C;
  --accent-dim: rgba(201,168,76,.12);
  --pos: #00d632;
  --neg: #ff5c5c;
  --warn: #ffb020;
  --radius: 12px;
  --sidebar-w: 260px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Montserrat', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--text); text-decoration: none; }
h1, h2, h3 { font-family: 'Playfair Display', Georgia, serif; font-weight: 700; margin: 0; }
.tnum { font-variant-numeric: tabular-nums; }

/* ===== Layout: Sidebar + Main ===== */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  min-height: 100vh;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 30;
  overflow-y: auto;
}

.sidebar-logo {
  padding: 24px 20px 20px;
  display: flex; align-items: center; gap: 10px;
  border-bottom: 1px solid var(--border);
}
.logo-mark {
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: 14px;
  width: 32px; height: 32px;
  border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  letter-spacing: .02em;
}
.logo-text {
  font-family: 'Playfair Display', serif;
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}

.sidebar-nav { flex: 1; padding: 12px 10px; }
.nav-section {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1.2px;
  color: var(--muted);
  padding: 18px 12px 6px;
  font-weight: 600;
}
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--muted);
  transition: background .12s, color .12s;
  cursor: pointer;
}
.nav-item:hover { background: rgba(255,255,255,.04); color: var(--text); }
.nav-item.active { background: var(--primary-dim); color: var(--primary); font-weight: 600; }
.nav-item .nav-icon { display: flex; flex-shrink: 0; }

.sidebar-footer {
  padding: 14px;
  border-top: 1px solid var(--border);
}
.user-badge {
  display: flex; align-items: center; gap: 8px;
  font-size: 12.5px; color: var(--muted);
  padding: 4px 8px 8px;
}
.sidebar-footer .logout {
  font-size: 12.5px;
  color: var(--muted);
}
.sidebar-footer .logout:hover { color: var(--neg); }

.main {
  flex: 1;
  margin-left: var(--sidebar-w);
  padding: 28px 36px 40px;
  min-height: 100vh;
  max-width: 1600px;
}

/* ===== Page Header ===== */
.page-header {
  display: flex; align-items: center; gap: 16px;
  margin-bottom: 24px;
}
.page-header h1 { font-size: 26px; letter-spacing: -.5px; }
.page-header .spacer { flex: 1; }

/* ===== Cards ===== */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 16px;
}
.card-sm { padding: 14px; }

/* ===== KPI Row ===== */
.kpi-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; margin-bottom: 24px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; }
.kpi .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .8px; font-weight: 600; }
.kpi .value { font-size: 28px; font-weight: 700; margin-top: 4px; letter-spacing: -.5px; font-variant-numeric: tabular-nums; }
.kpi .value.accent { color: var(--accent); }

/* ===== Data Table ===== */
.data-table { width: 100%; border-collapse: collapse; }
.data-table th {
  font-size: 11px; text-transform: uppercase; letter-spacing: .8px;
  color: var(--muted); font-weight: 600;
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.data-table td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
  vertical-align: middle;
}
.data-table tr:hover td { background: rgba(255,255,255,.02); }
.data-table .clickable { cursor: pointer; }
.data-table th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
.data-table th.sortable:hover { color: var(--text); }

.table-scroll { overflow-x: auto; }
.data-table.sticky-head thead th {
  position: sticky;
  top: 58px;
  background: var(--bg);
  z-index: 2;
}

.list-footer {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 16px;
}

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 20px; height: 20px;
  margin-left: 4px; padding: 0;
  border: 0; border-radius: 0;
  background: none; color: var(--muted);
  cursor: pointer; vertical-align: middle;
  appearance: none; -webkit-appearance: none;
  outline: none; box-shadow: none;
  transition: color .12s;
}
.icon-btn:hover { background: none; color: var(--text); }
.icon-btn:focus { outline: none; }

/* ===== Buttons ===== */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 10px 20px;
  font-size: 13.5px; font-weight: 600; font-family: inherit;
  border: 0; border-radius: 10px;
  cursor: pointer; transition: background .15s;
  text-decoration: none;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: transparent; border: 1px solid var(--border); color: var(--text); }
.btn-secondary:hover { background: rgba(255,255,255,.04); }
.btn-accent { background: var(--accent); color: #1a1a1a; }
.btn-sm { padding: 7px 14px; font-size: 12.5px; }
.btn-danger { background: transparent; border: 1px solid var(--neg); color: var(--neg); }
.btn-danger:hover { background: rgba(255,92,92,.1); }
.btn-ghost { background: transparent; color: var(--muted); padding: 6px 10px; }
.btn-ghost:hover { color: var(--text); background: rgba(255,255,255,.04); }

/* ===== Form Fields ===== */
.field { margin-bottom: 16px; }
.field label {
  display: block; font-size: 12px; color: var(--muted);
  font-weight: 500; margin-bottom: 5px; letter-spacing: .3px;
}
.field input, .field select, .field textarea {
  width: 100%; box-sizing: border-box;
  padding: 10px 14px;
  font-size: 14px; font-family: inherit;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  transition: border-color .15s;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--primary);
}
.field textarea { resize: vertical; min-height: 80px; }
.field-row { display: flex; gap: 14px; }
.field-row .field { flex: 1; }

/* ===== Tags / Badges ===== */
.badge {
  display: inline-flex; align-items: center;
  padding: 3px 10px; border-radius: 20px;
  font-size: 11.5px; font-weight: 600;
}
.badge-status { background: rgba(255,255,255,.06); }
.badge-vip { background: var(--accent-dim); color: var(--accent); }
.badge-tag { background: rgba(255,255,255,.06); color: var(--muted); margin: 2px 3px; }
.tag-list { display: flex; flex-wrap: wrap; gap: 4px; }

.tag-chip {
  display: inline-flex; align-items: center;
  padding: 3px 12px; border-radius: 20px;
  font-size: 12px; font-weight: 600;
  margin: 2px 3px; cursor: pointer;
  background: rgba(255,255,255,.06); color: var(--muted);
  border: 1px solid transparent;
  transition: background .12s, color .12s;
  user-select: none;
}
.tag-chip:hover { background: rgba(255,255,255,.1); color: var(--text); }
.tag-chip.active { background: var(--primary); color: #fff; }

.color-palette-panel {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  margin: 4px 0 10px;
}
.color-palette { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }
.color-swatch {
  width: 26px; height: 26px; border-radius: 50%;
  cursor: pointer; box-sizing: border-box;
  border: 2px solid transparent;
  transition: transform .1s, border-color .12s;
}
.color-swatch:hover { transform: scale(1.1); }
.color-swatch.active { border-color: #fff; box-shadow: 0 0 0 2px var(--bg); }

/* ===== Tabs ===== */
.tabs {
  display: flex; gap: 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
}
.tab {
  padding: 10px 18px;
  font-size: 13.5px; font-weight: 500;
  color: var(--muted);
  border-bottom: 2px solid transparent;
  cursor: pointer; transition: color .12s;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* ===== Empty state ===== */
.empty { text-align: center; padding: 60px 20px; color: var(--muted); }
.empty .ic { margin-bottom: 12px; opacity: .5; }
.empty p { font-size: 15px; margin: 0 0 16px; }

/* ===== Search / Filter Bar ===== */
.filter-bar {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 18px;
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding: 10px 0;
}
.filter-bar .search-input {
  flex: 1; max-width: 380px;
  padding: 9px 14px 9px 36px;
  font-size: 13.5px; font-family: inherit;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%237f7f7f' stroke-width='2'%3E%3Ccircle cx='11' cy='11' r='8'/%3E%3Cpath d='m21 21-4.3-4.3'/%3E%3C/svg%3E") 12px center no-repeat;
  color: var(--text);
}
.filter-bar select {
  padding: 9px 14px; font-size: 13px; font-family: inherit;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
}
.checkbox-label {
  display: flex; align-items: center; gap: 6px;
  font-size: 13px; color: var(--muted);
  white-space: nowrap; cursor: pointer;
}
.checkbox-label input { cursor: pointer; }

.totals-bar {
  display: flex; gap: 24px;
  padding: 10px 16px;
  margin-bottom: 16px;
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 13.5px; color: var(--muted);
}
.totals-bar strong { color: var(--text); font-weight: 600; }

/* ===== Client Card Layout ===== */
.client-header {
  display: flex; align-items: center; gap: 18px;
  margin-bottom: 24px;
}
.client-avatar {
  width: 64px; height: 64px; border-radius: 50%;
  background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 24px; font-weight: 700;
  flex-shrink: 0;
  overflow: hidden;
}
.client-avatar img { width: 100%; height: 100%; object-fit: cover; }
.client-info h2 { font-size: 22px; margin-bottom: 4px; }
.client-info .meta { display: flex; flex-wrap: wrap; gap: 14px; color: var(--muted); font-size: 13px; }
.client-info .meta span { display: flex; align-items: center; gap: 5px; }

/* ===== Two-col layout ===== */
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.three-col { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }

/* ===== Chat ===== */
.chat-messages {
  display: flex; flex-direction: column; gap: 8px;
  max-height: 500px; overflow-y: auto;
  padding: 12px;
}
.chat-msg {
  max-width: 70%;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 13.5px;
  line-height: 1.4;
}
.chat-msg.outgoing {
  align-self: flex-end;
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.chat-msg.incoming {
  align-self: flex-start;
  background: var(--surface-2);
  color: var(--text);
  border-bottom-left-radius: 4px;
}
.chat-msg .time { font-size: 10.5px; color: rgba(255,255,255,.5); margin-top: 4px; }
.chat-input {
  display: flex; gap: 10px; padding: 12px 0 0;
  border-top: 1px solid var(--border);
}
.chat-input input {
  flex: 1; padding: 10px 14px; font-size: 14px; font-family: inherit;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text);
}

/* ===== Events List ===== */
.event-row {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.event-row:last-child { border-bottom: 0; }
.event-date { font-size: 12px; color: var(--muted); min-width: 70px; font-weight: 600; }
.event-icon { color: var(--accent); display: flex; }
.event-text { flex: 1; font-size: 13.5px; }
.event-client { color: var(--muted); font-size: 12.5px; }

/* ===== Currency Badge (shared with бухгалтерия) ===== */
.ccyb {
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 50%; vertical-align: middle;
  font-weight: 700; text-transform: uppercase;
}

/* ===== Misc ===== */
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.accent { color: var(--accent); }
.muted { color: var(--muted); }
.text-right { text-align: right; }
.text-sm { font-size: 12.5px; }
.mt-8 { margin-top: 8px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }
.gap-8 { gap: 8px; }
.flex { display: flex; }
.items-center { align-items: center; }
.flex-1 { flex: 1; }

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,.1); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,.2); }

/* ===== Responsive ===== */
@media (max-width: 960px) {
  .sidebar { width: 220px; }
  .main { margin-left: 220px; padding: 20px; }
  .two-col, .three-col { grid-template-columns: 1fr; }
}
