:root {
  --bg: #f6f5f1;
  --bg-card: #ffffff;
  --bg-hover: #efeee8;
  --bg-soft: #fafaf7;
  --ink: #1a1a1a;
  --ink-2: #4a4a48;
  --ink-3: #8a8a85;
  --line: #e3e1d9;
  --line-2: #d4d2c8;
  --accent: #c2410c;
  --accent-soft: #fef3ec;
  --green: #15803d;
  --green-soft: #f0fdf4;
  --red: #b91c1c;
  --red-soft: #fef2f2;
  --amber: #b45309;
  --amber-soft: #fffbeb;
  --blue: #1e40af;
  --blue-soft: #eff6ff;
  --purple: #7c3aed;
  --purple-soft: #f3e8ff;
  --teal: #0d9488;
  --teal-soft: #ccfbf1;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  font-family: 'IBM Plex Sans', sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.app { display: grid; grid-template-columns: 280px 1fr; height: 100vh; --mod: var(--ink); --mod-soft: var(--bg-hover); }
/* акцент модуля: меняется при переключении разделов */
.app[data-module="debts"]     { --mod: var(--accent); --mod-soft: var(--accent-soft); }
.app[data-module="deals"]     { --mod: var(--purple); --mod-soft: var(--purple-soft); }
.app[data-module="companies"] { --mod: #475569;        --mod-soft: #eef2f6; }

/* ============ SIDEBAR ============ */
.sidebar { background: var(--bg-soft); border-right: 1px solid var(--line); display: flex; flex-direction: column; overflow: hidden; }
.brand { padding: 18px 24px 14px; border-bottom: 1px solid var(--line); }
.brand-title { font-size: 16px; font-weight: 700; letter-spacing: -0.01em; }
.brand-sub { font-size: 11px; color: var(--ink-3); text-transform: uppercase; letter-spacing: 0.08em; margin-top: 2px; }

/* nav switcher */
.nav { padding: 10px 12px; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 4px; }
.nav-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 9px 12px; border-radius: 6px; cursor: pointer; font-size: 13px; font-weight: 500; user-select: none;
}
.nav-item:hover { background: var(--bg-hover); }
.nav-item.active { background: var(--mod); color: white; }
.nav-item.active .nav-count { color: rgba(255,255,255,0.75); }
.nav-count { font-family: 'IBM Plex Mono', monospace; font-size: 11px; color: var(--ink-3); }

.summary { padding: 14px 24px; border-bottom: 1px solid var(--line); }
.summary-row { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
.summary-label { font-size: 11px; color: var(--ink-3); text-transform: uppercase; letter-spacing: 0.06em; }
.summary-value { font-family: 'IBM Plex Mono', monospace; font-size: 13px; font-weight: 500; }
.summary-total { font-family: 'IBM Plex Mono', monospace; font-size: 22px; font-weight: 600; margin-top: 4px; letter-spacing: -0.02em; color: var(--mod); }

.sidebar-scroll { flex: 1; overflow-y: auto; }
.sidebar-section { padding: 16px 24px 8px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-3); font-weight: 600; }
.filter-list { padding: 0 12px 8px; }
.filter-item { display: flex; justify-content: space-between; align-items: center; padding: 8px 12px; border-radius: 6px; cursor: pointer; font-size: 13px; user-select: none; transition: background 0.1s; }
.filter-item:hover { background: var(--bg-hover); }
.filter-item.active { background: var(--mod); color: white; }
.filter-item.active .filter-count { color: rgba(255,255,255,0.75); }
.filter-item.active .cat-pill { filter: none; }
.filter-count { font-family: 'IBM Plex Mono', monospace; font-size: 11px; color: var(--ink-3); }

.sidebar-actions { padding: 14px 24px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }

/* ============ BUTTONS ============ */
.btn { font-family: inherit; font-size: 13px; font-weight: 500; padding: 8px 14px; border: 1px solid var(--line-2); background: white; color: var(--ink); border-radius: 6px; cursor: pointer; transition: all 0.1s; display: inline-flex; align-items: center; gap: 6px; }
.btn:hover { background: var(--bg-hover); border-color: var(--ink-3); }
.btn-primary { background: var(--ink); color: white; border-color: var(--ink); }
.btn-primary:hover { background: #000; border-color: #000; }
.btn-danger { color: var(--red); border-color: #f5c5c5; }
.btn-danger:hover { background: var(--red-soft); border-color: var(--red); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-icon { padding: 6px 8px; }

/* ============ MAIN ============ */
.main { overflow-y: auto; display: flex; flex-direction: column; background: var(--mod-soft); transition: background 0.15s; }
.toolbar { padding: 16px 28px; border-bottom: 1px solid var(--line); border-top: 3px solid var(--mod); background: var(--bg-card); display: flex; align-items: center; gap: 12px; position: sticky; top: 0; z-index: 10; }

/* кнопка добавления и бейдж раздела — в цвете текущего модуля */
#add-btn { background: var(--mod); border-color: var(--mod); color: #fff; }
#add-btn:hover { filter: brightness(0.92); background: var(--mod); border-color: var(--mod); }
.module-tag { display: inline-block; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.08em; color: #fff; background: var(--mod); padding: 3px 10px; border-radius: 5px; margin-bottom: 10px; }
.search-box { flex: 1; max-width: 400px; position: relative; }
.search-box input { width: 100%; padding: 8px 12px 8px 32px; border: 1px solid var(--line-2); border-radius: 6px; font-family: inherit; font-size: 13px; background: var(--bg-soft); }
.search-box input:focus { outline: none; border-color: var(--ink); background: white; }
.search-box::before { content: "⌕"; position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--ink-3); font-size: 16px; }
.toolbar-spacer { flex: 1; }

.view-title { padding: 24px 28px 16px; }
.view-title h1 { font-size: 22px; font-weight: 600; letter-spacing: -0.02em; color: var(--mod); }
.view-title .subtitle { font-size: 13px; color: var(--ink-3); margin-top: 2px; }

/* ============ TABLE ============ */
.table-wrap { padding: 0 28px 28px; }
table { width: 100%; border-collapse: collapse; background: var(--bg-card); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
thead th { text-align: left; padding: 10px 14px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--mod); font-weight: 700; background: var(--mod-soft); border-bottom: 2px solid var(--mod); white-space: nowrap; }
tbody td { padding: 14px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tbody tr { cursor: pointer; transition: background 0.08s; }
tbody tr:hover { background: var(--bg-soft); }
tbody tr:last-child td { border-bottom: none; }
.cell-name { font-weight: 600; color: var(--ink); }
.cell-inn { font-family: 'IBM Plex Mono', monospace; font-size: 11px; color: var(--ink-3); margin-top: 2px; }
.cell-amount { font-family: 'IBM Plex Mono', monospace; font-weight: 600; text-align: right; white-space: nowrap; }

.days-pill { display: inline-block; padding: 2px 8px; border-radius: 4px; font-size: 11px; font-family: 'IBM Plex Mono', monospace; font-weight: 500; }

.cat-pill { display: inline-block; min-width: 22px; height: 22px; padding: 0 8px; border-radius: 4px; text-align: center; line-height: 22px; font-size: 11px; font-weight: 600; white-space: nowrap; }
.cat-work { background: var(--blue-soft); color: var(--blue); }
.cat-plan { background: var(--amber-soft); color: var(--amber); }
.cat-court { background: var(--red-soft); color: var(--red); }
.cat-writeoff { background: #ece6df; color: #7a6a55; }
.cat-none { background: var(--bg-hover); color: var(--ink-3); }
.cat-closed { background: var(--green-soft); color: var(--green); }

/* ============ STAGE PILLS (продажи) ============ */
.stage-pill { display: inline-block; padding: 0 8px; height: 22px; line-height: 22px; border-radius: 4px; font-size: 11px; font-weight: 600; white-space: nowrap; }
.stage-none { background: var(--bg-hover); color: var(--ink-3); }
.stage-negotiation { background: var(--blue-soft); color: var(--blue); }
.stage-proposal { background: var(--amber-soft); color: var(--amber); }
.stage-contract { background: var(--purple-soft); color: var(--purple); }
.stage-work { background: var(--teal-soft); color: var(--teal); }
.stage-won { background: var(--green-soft); color: var(--green); }
.stage-lost { background: #f0f0eb; color: var(--ink-3); }

.badge-loyal { display: inline-block; background: var(--purple-soft); color: var(--purple); font-size: 10px; font-weight: 600; padding: 2px 8px; border-radius: 4px; text-transform: uppercase; letter-spacing: 0.04em; }

.empty-state { padding: 80px 28px; text-align: center; color: var(--ink-3); }
.empty-state h3 { font-size: 16px; color: var(--ink-2); margin-bottom: 8px; font-weight: 600; }
.empty-state p { font-size: 13px; max-width: 420px; margin: 0 auto 20px; }

/* ============ DRAWER ============ */
.drawer-overlay { position: fixed; inset: 0; background: rgba(20,20,20,0.3); z-index: 50; opacity: 0; pointer-events: none; transition: opacity 0.2s; }
.drawer-overlay.open { opacity: 1; pointer-events: auto; }
.drawer { position: fixed; top: 0; right: 0; width: 760px; max-width: 95vw; height: 100vh; background: var(--bg-card); z-index: 51; box-shadow: -4px 0 24px rgba(0,0,0,0.08); transform: translateX(100%); transition: transform 0.25s cubic-bezier(0.2,0.8,0.2,1); display: flex; flex-direction: column; }
.drawer.open { transform: translateX(0); }
.drawer-header { padding: 20px 28px 16px; border-bottom: 1px solid var(--line); display: flex; align-items: flex-start; gap: 16px; }
.drawer-header-main { flex: 1; min-width: 0; }
.drawer-header h2 { font-size: 18px; font-weight: 600; letter-spacing: -0.01em; word-wrap: break-word; }
.drawer-header .meta { font-size: 12px; color: var(--ink-3); font-family: 'IBM Plex Mono', monospace; margin-top: 4px; }
.drawer-close { background: none; border: none; font-size: 22px; color: var(--ink-3); cursor: pointer; padding: 4px 10px; border-radius: 4px; line-height: 1; }
.drawer-close:hover { background: var(--bg-hover); color: var(--ink); }
.drawer-body { flex: 1; overflow-y: auto; padding: 24px 28px 40px; }

.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px 24px; margin-bottom: 28px; }
.detail-grid.full { grid-template-columns: 1fr; }
.field { display: flex; flex-direction: column; }
.field label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-3); font-weight: 600; margin-bottom: 6px; }
.field input, .field select, .field textarea { font-family: inherit; font-size: 13px; padding: 8px 10px; border: 1px solid var(--line-2); border-radius: 5px; background: white; color: var(--ink); width: 100%; }
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--ink); }
.field textarea { resize: vertical; min-height: 60px; }

.section-title { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; font-weight: 700; color: var(--ink-2); padding-bottom: 8px; border-bottom: 1px solid var(--line); margin: 8px 0 16px; display: flex; align-items: center; justify-content: space-between; }

/* tabs внутри карточки контрагента */
.tabs { display: flex; gap: 6px; margin-bottom: 20px; background: var(--bg-soft); padding: 4px; border-radius: 8px; border: 1px solid var(--line); }
.tab { flex: 1; text-align: center; padding: 8px 12px; border-radius: 6px; cursor: pointer; font-size: 13px; font-weight: 500; color: var(--ink-2); user-select: none; }
.tab.active { background: white; color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,0.06); font-weight: 600; }

.mini-row { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg-soft); margin-bottom: 8px; cursor: pointer; }
.mini-row:hover { background: var(--bg-hover); }
.mini-row .mini-amount { font-family: 'IBM Plex Mono', monospace; font-weight: 600; }

.summary-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 20px; }
.summary-card { padding: 12px 14px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg-soft); }
.summary-card .lbl { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-3); font-weight: 600; margin-bottom: 4px; }
.summary-card .val { font-size: 18px; font-weight: 600; font-family: 'IBM Plex Mono', monospace; }

/* TASKS */
.task-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.task { display: grid; grid-template-columns: auto 90px 1fr auto; gap: 10px; align-items: center; padding: 10px 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg-soft); }
.task.overdue { background: var(--red-soft); border-color: #f5c5c5; }
.task.today { background: var(--amber-soft); border-color: #fde68a; }
.task.done { opacity: 0.55; background: var(--bg-soft); }
.task.done .task-text { text-decoration: line-through; }
.task-checkbox { width: 16px; height: 16px; cursor: pointer; accent-color: var(--ink); }
.task-date { font-family: 'IBM Plex Mono', monospace; font-size: 11px; color: var(--ink-2); }
.task-text { font-size: 13px; }
.task-add { display: grid; grid-template-columns: 130px 1fr 110px auto; gap: 8px; margin-top: 8px; }
.task-add input, .task-add select { padding: 7px 9px; font-size: 12px; font-family: inherit; border: 1px solid var(--line-2); border-radius: 5px; }

/* FILES */
.file-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.file-item { display: grid; grid-template-columns: 1fr 2fr auto; gap: 12px; align-items: center; padding: 10px 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg-soft); }
.file-name { font-size: 13px; font-weight: 500; word-break: break-word; }
.file-comment { font-size: 12px; color: var(--ink-2); word-break: break-word; }
.file-add-v2 { display: flex; gap: 8px; align-items: center; padding: 10px; background: var(--bg-soft); border: 1px dashed var(--line-2); border-radius: 6px; flex-wrap: wrap; }
.file-add-v2 input[type="text"] { padding: 7px 9px; font-size: 12px; border: 1px solid var(--line-2); border-radius: 4px; background: white; font-family: inherit; min-width: 150px; }
.file-add-v2 input[type="text"]:focus { outline: none; border-color: var(--ink); }

/* CONTACTS */
.contacts-list { display: flex; flex-direction: column; gap: 10px; margin-bottom: 10px; }
.contact-item { border: 1px solid var(--line); border-radius: 6px; padding: 12px 14px; background: var(--bg-soft); }
.contact-item.primary { border-left: 3px solid var(--accent); background: white; }
.contact-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; margin-bottom: 8px; }
.contact-field { display: flex; flex-direction: column; }
.contact-field label { font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-3); font-weight: 600; margin-bottom: 4px; display: flex; align-items: center; gap: 6px; }
.contact-field input { font-family: inherit; font-size: 13px; padding: 6px 9px; border: 1px solid var(--line-2); border-radius: 4px; background: white; color: var(--ink); }
.contact-field input:focus { outline: none; border-color: var(--ink); }
.primary-badge { font-size: 9px; font-weight: 600; background: var(--accent-soft); color: var(--accent); padding: 1px 6px; border-radius: 3px; text-transform: uppercase; letter-spacing: 0.04em; }
.contact-actions { display: flex; gap: 6px; justify-content: flex-end; margin-top: 8px; }
.contact-add { margin-bottom: 12px; }

/* NOTES */
.note-add { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.note-add textarea { font-family: inherit; font-size: 13px; padding: 10px 12px; border: 1px solid var(--line-2); border-radius: 6px; resize: vertical; background: white; color: var(--ink); }
.note-add textarea:focus { outline: none; border-color: var(--ink); }
.note-add button { align-self: flex-start; }
.note-list { display: flex; flex-direction: column; gap: 8px; }
.note-item { padding: 10px 12px 12px; border: 1px solid var(--line); border-left: 3px solid var(--accent); border-radius: 4px; background: var(--bg-soft); }
.note-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.note-date { font-family: 'IBM Plex Mono', monospace; font-size: 11px; color: var(--ink-3); }
.note-text { font-size: 13px; color: var(--ink); white-space: pre-wrap; line-height: 1.5; word-wrap: break-word; }

/* HISTORY */
.history-item { padding: 10px 12px; border-left: 2px solid var(--line); margin-left: 6px; margin-bottom: 6px; font-size: 12px; }
.history-date { color: var(--ink-3); font-family: 'IBM Plex Mono', monospace; font-size: 11px; }
.history-text { color: var(--ink-2); margin-top: 2px; }

.drawer-footer { padding: 16px 28px; border-top: 1px solid var(--line); display: flex; justify-content: space-between; background: var(--bg-soft); }

/* ============ MODAL ============ */
.modal-overlay { position: fixed; inset: 0; background: rgba(20,20,20,0.4); display: none; align-items: center; justify-content: center; z-index: 100; }
.modal-overlay.open { display: flex; }
.modal { background: white; border-radius: 10px; width: 600px; max-width: 92vw; max-height: 85vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,0.2); }
.modal-header { padding: 20px 24px 16px; border-bottom: 1px solid var(--line); }
.modal-header h2 { font-size: 17px; font-weight: 600; }
.modal-header p { font-size: 12px; color: var(--ink-3); margin-top: 4px; }
.modal-body { padding: 20px 24px; overflow-y: auto; flex: 1; }
.modal-footer { padding: 14px 24px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
.import-instructions { background: var(--bg-soft); border: 1px solid var(--line); border-radius: 6px; padding: 14px 16px; font-size: 12px; color: var(--ink-2); margin-bottom: 16px; }
.import-instructions code { font-family: 'IBM Plex Mono', monospace; background: white; padding: 1px 5px; border-radius: 3px; border: 1px solid var(--line); font-size: 11px; }

.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--ink); color: white; padding: 10px 18px; border-radius: 6px; font-size: 13px; opacity: 0; transition: all 0.2s; z-index: 200; pointer-events: none; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--ink); }
th.sortable.sorted::after { content: ' ↓'; }
th.sortable.sorted.asc::after { content: ' ↑'; }

/* ============ LOGIN ============ */
.login-overlay { position: fixed; inset: 0; background: var(--bg); display: flex; align-items: center; justify-content: center; z-index: 500; }
.login-card { background: var(--bg-card); border: 1px solid var(--line); border-radius: 12px; padding: 32px; width: 360px; max-width: 92vw; box-shadow: 0 10px 40px rgba(0,0,0,0.08); }
.login-card h1 { font-size: 20px; font-weight: 700; margin-bottom: 4px; }
.login-card p { font-size: 13px; color: var(--ink-3); margin-bottom: 20px; }
.login-card input { width: 100%; padding: 10px 12px; border: 1px solid var(--line-2); border-radius: 6px; font-family: inherit; font-size: 14px; margin-bottom: 12px; }
.login-card input:focus { outline: none; border-color: var(--ink); }
.login-card .btn { width: 100%; justify-content: center; }
.login-error { color: var(--red); font-size: 12px; margin-bottom: 12px; display: none; }
.login-error.show { display: block; }
.hidden { display: none !important; }

/* ============ PICK COMPANY ============ */
.pick-results { display: flex; flex-direction: column; gap: 6px; max-height: 360px; overflow-y: auto; }
.pick-item { padding: 10px 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg-soft); cursor: pointer; }
.pick-item:hover { background: var(--bg-hover); border-color: var(--ink-3); }
.pick-item.pick-new { border-style: dashed; background: white; }

/* ============ DRAWER ACCENT ПО ТИПУ СУЩНОСТИ ============ */
#drawer { border-top: 5px solid var(--md, var(--ink)); }
#drawer[data-dtype="debt"]    { --md: var(--accent); --md-soft: var(--accent-soft); }
#drawer[data-dtype="deal"]    { --md: var(--purple); --md-soft: var(--purple-soft); }
#drawer[data-dtype="company"] { --md: #475569;        --md-soft: #eef2f6; }
#drawer .drawer-header { background: var(--md-soft); }
#drawer .drawer-header h2 { color: var(--md); }
#drawer .section-title { color: var(--md); border-bottom-color: color-mix(in srgb, var(--md) 35%, var(--line)); }
