:root {
  --bg: #f4f1ea;
  --card: #fffdf8;
  --ink: #1d232b;
  --muted: #737a82;
  --line: #e2ddd2;
  --accent: #e4572e;
  --accent-soft: #fdeee8;
  --green: #2f7d4f;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 24, 28, 0.06), 0 8px 24px rgba(20, 24, 28, 0.05);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: #b3261e; font-size: 12.5px; min-height: 16px; }

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  letter-spacing: -0.01em;
  font-size: 16px;
}

.logo {
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
}

/* ------------------------------------------------------------- login */

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: 100%;
  max-width: 360px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 26px;
  display: grid;
  gap: 12px;
  box-shadow: var(--shadow);
}

.login-card label { display: grid; gap: 6px; font-size: 12.5px; color: var(--muted); }

input, select, textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  font: inherit;
  color: var(--ink);
  outline: none;
}

input:focus, select:focus, textarea:focus { border-color: var(--accent); }

.btn {
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover { background: #f7f4ee; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(0.96); }
.btn.ghost { background: transparent; }

/* ------------------------------------------------------------- app shell */

.app { min-height: 100vh; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}

.search { max-width: 520px; }
.userbox { margin-left: auto; display: flex; align-items: center; gap: 12px; font-size: 13px; }

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  padding: 12px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 57px;
  z-index: 9;
}

.filters input, .filters select { width: auto; min-width: 110px; }
.hints {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 4px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  min-width: 260px;
  max-height: 280px;
  overflow: auto;
  z-index: 20;
}

.hint { padding: 9px 12px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; }
.hint:hover { background: var(--accent-soft); }
.hint .lines { color: var(--muted); font-size: 11.5px; }

/* ------------------------------------------------------------- layout */

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 400px;
  gap: 16px;
  padding: 16px 20px 32px;
  align-items: start;
}

.list-wrap { min-width: 0; }

.list { display: grid; gap: 10px; }

.property {
  display: grid;
  grid-template-columns: 120px 1fr auto;
  gap: 14px;
  align-items: center;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.property:hover { border-color: #c9c2b4; box-shadow: var(--shadow); }
.property.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }

.thumb {
  width: 120px;
  height: 84px;
  border-radius: 9px;
  background: #e9e4d9 center/cover no-repeat;
  display: grid;
  place-items: center;
  color: #b5ad9e;
  font-size: 11px;
}

.p-title { font-weight: 650; margin-bottom: 3px; }
.p-sub { color: var(--muted); font-size: 12.5px; }
.p-meta { color: var(--muted); font-size: 12.5px; margin-top: 4px; }
.p-price { font-weight: 700; font-size: 16px; text-align: right; white-space: nowrap; }
.p-price small { display: block; color: var(--muted); font-weight: 500; font-size: 11.5px; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: #efeae0;
  font-size: 11px;
  color: #5c564b;
  margin-left: 6px;
}

.badge.rent { background: #e6f1ff; color: #1f5fa8; }
.badge.sale { background: #eaf7ee; color: var(--green); }

.pager { display: flex; gap: 12px; align-items: center; justify-content: center; padding: 18px; }

/* ------------------------------------------------------------- panel */

.panel {
  position: sticky;
  top: 120px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  max-height: calc(100vh - 130px);
  overflow: auto;
  box-shadow: var(--shadow);
}

.empty { color: var(--muted); text-align: center; padding: 40px 10px; font-size: 13px; }

.panel h2 { margin: 0 0 4px; font-size: 18px; letter-spacing: -0.01em; }
.panel h3 { margin: 16px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }

.kv { display: grid; grid-template-columns: 130px 1fr; gap: 6px 10px; font-size: 13px; }
.kv div:nth-child(odd) { color: var(--muted); }

.photos { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-top: 8px; }
.photo { aspect-ratio: 4 / 3; border-radius: 8px; background: #eee center/cover no-repeat; position: relative; }
.photo .del {
  position: absolute; top: 4px; right: 4px; width: 22px; height: 22px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,.55); color: #fff; cursor: pointer; font-size: 13px; line-height: 1;
}

.panel-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }

/* ------------------------------------------------------------- modal */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(20, 24, 28, 0.45);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 100;
}

.modal-card {
  width: 100%;
  max-width: 720px;
  max-height: 92vh;
  overflow: auto;
  background: var(--card);
  border-radius: 16px;
  padding: 20px;
  display: grid;
  gap: 12px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}

.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { margin: 0; font-size: 18px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.grid2 label, .modal-card > label { display: grid; gap: 5px; font-size: 12px; color: var(--muted); }
.modal-actions { display: flex; align-items: center; gap: 12px; }

/* ------------------------------------------------------------- mobile */

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .panel { position: static; max-height: none; }
  .property { grid-template-columns: 84px 1fr; }
  .thumb { width: 84px; height: 64px; }
  .p-price { grid-column: 1 / -1; text-align: left; }
  .grid2 { grid-template-columns: 1fr; }
  .filters { top: 0; position: static; }
}


/* ------------------------------------------------------------- таблица */

.table-wrap {
  overflow: auto;
  max-height: calc(100vh - 210px);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

table.grid {
  border-collapse: separate;
  border-spacing: 0;
  width: fit-content;
  max-width: 100%;
  table-layout: auto;
  font-size: 12px;
}

table.grid th,
table.grid td {
  white-space: normal;
  overflow-wrap: anywhere;
}

table.grid td.num,
table.grid td.id,
table.grid td.phone {
  white-space: nowrap;
}

table.grid th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #f8f5ee;
  text-align: left;
  font-weight: 600;
  color: #5d5648;
  padding: 8px 8px;
  border-bottom: 1px solid var(--line);
  font-size: 11px;
}

table.grid td {
  padding: 6px 8px;
  border-bottom: 1px solid #f0ece3;
  vertical-align: middle;
}

table.grid tbody tr { cursor: pointer; }
table.grid tbody tr:hover { background: #fbf8f2; }
table.grid tbody tr.active { background: var(--accent-soft); }

table.grid td.num { color: var(--muted); }
table.grid td.note { max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
table.grid td.id { color: var(--muted); font-variant-numeric: tabular-nums; }
table.grid td.phone { font-variant-numeric: tabular-nums; }

.star, .alt, .photo-btn {
  border: 0;
  background: transparent;
  cursor: pointer;
  font: inherit;
  padding: 2px 4px;
  color: var(--muted);
}

.star { font-size: 30px; line-height: 1; padding: 0 2px; }
.star.on { color: #d9a400; }
.alt { font-size: 10px; border: 1px solid var(--line); border-radius: 5px; padding: 1px 5px; }
.photo-btn { font-size: 12px; }

.small-btn { padding: 5px 10px; font-size: 12px; }

/* ------------------------------------------------------------- карточка */

.note-input { width: 100%; font-size: 12.5px; resize: vertical; }

.pre { white-space: pre-wrap; }

.tasks { display: grid; gap: 4px; }
.task {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  padding: 3px 0;
}
.task.done .task-title { text-decoration: line-through; color: var(--muted); }
.task-title { flex: 1; }
.task-date { color: var(--muted); font-variant-numeric: tabular-nums; }
.task-del { border: 0; background: transparent; color: var(--muted); cursor: pointer; }

.task-add { display: flex; gap: 6px; margin-top: 8px; }
.task-add input[type=date] { width: 140px; }

.history { display: grid; gap: 3px; max-height: 220px; overflow: auto; }
.hist { display: flex; gap: 8px; font-size: 11.5px; align-items: baseline; }
.hist-date { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

.orig { margin-top: 14px; }
.orig summary { cursor: pointer; font-size: 12px; color: var(--muted); }
.orig-json {
  max-height: 240px;
  overflow: auto;
  background: #fbf8f2;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  font-size: 10.5px;
}

.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px 14px; }

@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; }
  .grid3 { grid-template-columns: 1fr 1fr; }
}


table.grid th[data-sort] { cursor: pointer; user-select: none; }
table.grid th[data-sort]:hover { background: #f1ece1; }
table.grid th.sorted { color: var(--accent); }


/* тонкие разделители ячеек */
table.grid th,
table.grid td {
  border-right: 1px solid #f0ece3;
}

table.grid th:last-child,
table.grid td:last-child {
  border-right: 0;
}

table.grid th {
  border-bottom: 1px solid #e2ddd2;
}

table.grid td {
  border-bottom: 1px solid #f4f1ea;
}


/* массовые операции */
.bulk-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  margin-bottom: 8px;
  background: var(--accent-soft);
  border: 1px solid #f3cbbd;
  border-radius: var(--radius);
}

.bulk-bar select { width: auto; min-width: 130px; }
.bulk-bar #bulkCount { font-weight: 600; margin-right: 4px; }

table.grid td.check, table.grid th.check {
  width: 34px;
  text-align: center;
  padding-left: 4px;
  padding-right: 4px;
}

table.grid input[type="checkbox"] {
  width: 16px;
  height: 16px;
  cursor: pointer;
}


/* резиновые столбцы: сжимаются, но не растягиваются */
.layout { grid-template-columns: 1fr; }

.panel {
  position: fixed;
  top: 108px;
  right: 16px;
  bottom: 16px;
  width: 430px;
  max-height: none;
  z-index: 60;
  display: none;
}
.panel.open { display: block; }

.panel-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}

table.grid {
  width: fit-content;
  max-width: 100%;
  font-size: 11px;
}

table.grid th,
table.grid td {
  white-space: normal;
  overflow-wrap: break-word;
  min-width: 48px;
  padding: 4px 5px;
}

table.grid td.num,
table.grid td.id,
table.grid td.phone {
  white-space: nowrap;
}


/* карта дома */
.map-col { width: 44px; text-align: center; }

.map-btn {
  border: 0;
  background: transparent;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 4px;
}

.map-btn:hover { transform: scale(1.15); }

.map-card { max-width: 900px; width: 100%; }

.map-frame iframe {
  width: 100%;
  height: 60vh;
  border: 0;
  border-radius: 10px;
  background: #eee;
}

.map-actions { display: flex; align-items: center; gap: 12px; }


/* телефоны */
.phone-cell { min-width: 120px; }
.phone-num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.phone-btn {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 7px;
  padding: 2px 7px;
  font-size: 11px;
  cursor: pointer;
  margin-top: 2px;
}
.phone-btn:disabled { opacity: 0.6; cursor: default; }


.map-frame { position: relative; }

.map-pin {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 74px;
  transform: translate(-50%, -100%);
  pointer-events: none;
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.35));
  z-index: 2;
}


.settings-view { padding: 18px 20px 40px; }
.settings-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.settings-head h2 { margin: 0; }
.settings-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  max-width: 760px;
  display: grid;
  gap: 14px;
  box-shadow: var(--shadow);
}
td.editing { padding: 2px !important; }
td.editing input, td.editing select { min-width: 84px; padding: 4px 6px; font-size: 12px; }
.ok { color: var(--green); font-size: 12.5px; }


.agents-card { max-width: 1100px; }
.agents-table { min-width: 820px; width: 100%; font-size: 12px; }
.agents-table td { padding: 6px 8px; }
.agent-limit { width: 84px; padding: 3px 6px; font-size: 12px; }


.usage {
  border: 0;
  background: transparent;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  color: var(--accent);
  padding: 2px 6px;
  border-radius: 6px;
}
.usage:hover { background: var(--accent-soft); }

.agent-opens td { background: #fbf8f2; }
.opens-list { display: grid; gap: 4px; padding: 4px 2px; }
.opens-title { font-weight: 600; font-size: 12px; margin-bottom: 4px; }
.open-item { display: flex; gap: 12px; font-size: 12px; flex-wrap: wrap; }
.open-time { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }


.report { padding: 16px 22px 30px; }
.report-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.report-head h1 { font-size: 18px; margin: 0; }
.report-grid { min-width: 900px; }


.report-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  margin-bottom: 8px;
  background: var(--accent-soft);
  border: 1px solid #f3cbbd;
  border-radius: var(--radius);
  font-size: 13px;
}
.banner-link { margin-left: auto; color: var(--accent); font-weight: 600; }


.send-wrap { display: flex; align-items: center; gap: 3px; flex-wrap: wrap; }
.ch {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 6px;
  font-size: 10px;
  font-weight: 700;
  padding: 3px 5px;
  cursor: pointer;
  color: var(--muted);
}
.ch.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.send-btn {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 6px;
  padding: 3px 7px;
  cursor: pointer;
  font-size: 12px;
}
.send-btn.sent { background: #e6f7ec; border-color: #2f7d4f; color: #2f7d4f; }
.messenger-channels { display: grid; gap: 8px; }
.channel-row { display: grid; grid-template-columns: 180px 1fr 1fr; gap: 8px; align-items: center; }
.channel-title { display: flex; align-items: center; gap: 8px; font-size: 13px; }


.send-cell { min-width: 66px; }
.ch { white-space: nowrap; font-size: 9px; padding: 2px 4px; }


.filter-check { display: flex; align-items: center; gap: 6px; font-size: 12.5px; }
.filter-check input { width: 15px; height: 15px; }

.send-cell.unanswered {
  background: #fff3c4;
  box-shadow: inset 0 0 0 2px #f2c200, 0 0 10px rgba(242, 194, 0, 0.45);
}

.modes { display: flex; gap: 2px; margin-top: 3px; }
.mode {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 5px;
  font-size: 9px;
  padding: 2px 4px;
  cursor: pointer;
  color: var(--muted);
}
.mode.on { background: #eef4ff; border-color: #4a7bd0; color: #2b5bb0; }

.personal-agent { border-bottom: 1px solid var(--line); padding: 6px 0; }
.personal-name { font-weight: 600; font-size: 13px; margin-bottom: 4px; }


table.grid.dragging { user-select: none; }
table.grid tbody tr.selected-row { background: #e8f0fe; }
table.grid tbody tr.selected-row:hover { background: #dfe9fb; }


.map-view {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: #fff;
  display: flex;
  flex-direction: column;
}
.map-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--card);
  flex-wrap: wrap;
}
.map-toolbar #mapInfo { margin-left: auto; }
#mapCanvas { flex: 1; min-height: 0; }
.ruler-label {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 11px;
  font-weight: 600;
  color: #2b5bb0;
  white-space: nowrap;
}

.globe-icon { font-size: 18px; line-height: 1; display: inline-block; }

.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

#ipList { width: 100%; max-width: 520px; font-family: inherit; font-size: 13px; padding: 8px; border: 1px solid var(--line); border-radius: 8px; }


/* Резиновые колонки: минимум по содержимому, длинное — с красным уголком */
#grid { table-layout: auto; width: 100%; }
#grid thead th { white-space: nowrap; }
#gridBody td {
    max-width: 190px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
#gridBody td.check,
#gridBody td.map-col,
#gridBody td.send-cell,
#gridBody td.phone-cell { max-width: none; overflow: visible; }
#gridBody td.truncated { position: relative; cursor: help; }
#gridBody td.truncated::after {
    content: "";
    position: absolute;
    right: 0;
    bottom: 0;
    width: 0;
    height: 0;
    border-style: solid;
    border-width: 0 0 9px 9px;
    border-color: transparent transparent #e53935 transparent;
    pointer-events: none;
}
.cell-tip {
    position: fixed;
    z-index: 300;
    max-width: 460px;
    max-height: 280px;
    overflow: auto;
    background: #1f2937;
    color: #fff;
    padding: 8px 10px;
    border-radius: 8px;
    font-size: 12px;
    line-height: 1.45;
    box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
    white-space: pre-wrap;
    pointer-events: none;
}
