@import url("https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;600&display=swap");

:root {
  --bg: #f8faf6; --fg: #1a2417; --topbar-bg: #fff; --border: #cfd9c6;
  --accent: #4d7c0f; --accent-light: #e3ecd7; --stage-bg: #eef2e8; --count-bg: #dde6d3;
  --card-bg: #fff; --card-shadow: 0 1px 1px rgba(0,0,0,.05);
  --muted: #5b6b53; --danger: #a72828;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; min-width: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--fg); background: var(--bg);
  display: flex; flex-direction: column;
}
a { color: var(--accent); }

.topbar {
  display: flex; align-items: center; gap: 1.5rem;
  padding: .75rem 1.25rem; background: var(--topbar-bg);
  border-bottom: 1px solid var(--border); flex-shrink: 0; position: relative;
}
.brand { font-weight: 700; font-size: 1rem; margin-right: .5rem; }
.nav-toggle {
  display: none; width: 2.25rem; height: 2.25rem; border: 1px solid var(--border);
  border-radius: 6px; background: var(--card-bg); color: var(--fg); font-size: 1.2rem;
  line-height: 1; cursor: pointer;
}
.primary-nav { display: flex; gap: .3rem; margin-right: auto; }
.primary-nav a {
  color: var(--fg); text-decoration: none; font-size: .85rem;
  padding: .4rem .7rem; border-radius: 6px;
}
.primary-nav a:hover, .primary-nav a.active { background: var(--accent-light); color: var(--accent); }
.logout-form button {
  background: none; border: 1px solid var(--border); color: inherit;
  padding: .35rem .75rem; border-radius: 6px; cursor: pointer; font-size: .8rem;
}

.layout { display: flex; flex: 1; min-height: 0; }
.board {
  flex: 1; min-width: 0; overflow-x: hidden; overflow-y: auto; display: flex; flex-direction: column; gap: 1rem; padding: 1.25rem;
  /* forceFallback drag uses mouse events, not native HTML5 DnD, so the
     browser still tries to text-select while dragging unless the whole
     drag surface (not just the card) opts out — this was the cause of
     text getting selected mid-drag and the drop then reverting. */
  user-select: none; -webkit-user-select: none; -ms-user-select: none;
}
.page { flex: 1; overflow-y: auto; padding: 1.5rem 1.75rem; max-width: 980px; }
/* Analytics has a wide data table (7 columns) — the shared 980px reading-width
   constraint above squeezes it into half the screen. This opts that one page
   out without affecting board/settings/search. */
.page-wide { max-width: none; }

/* Document-style pages (deal/search/settings): native page scroll instead of
   the inner .page scroll region, and the reading column centered on wide
   screens. Board/analytics keep the fixed-topbar inner-scroll model. */
body.doc { height: auto; min-height: 100%; }
body.doc .topbar { position: sticky; top: 0; z-index: 10; max-width: 980px; margin-inline: auto; width: 100%; }
body.doc .page { flex: 0 0 auto; overflow: visible; width: 100%; margin-inline: auto; }

.data-table { width: 100%; border-collapse: collapse; font-size: .82rem; margin-bottom: .6rem; table-layout: fixed; }
.data-table th, .data-table td { padding: .5rem .5rem; vertical-align: middle; }
.data-table th {
  text-align: center; color: var(--muted); font-weight: 600;
  white-space: normal; word-break: break-word; line-height: 1.25;
  border-bottom: 1px solid var(--border);
}
.data-table td { text-align: center; border-top: 1px solid var(--border); }
.data-table td.text-cell { text-align: left; }
.data-table th[data-sort] { cursor: pointer; }
.data-table col.col-name { width: 18%; }

/* ---- funnel tabs (real links, not JS-only) ---- */
.board-controls { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; position: relative; }
.funnel-tabs { display: flex; gap: .4rem; flex-wrap: wrap; }
.funnel-tab {
  border: 1px solid var(--border); background: var(--card-bg); color: var(--fg);
  padding: .4rem .9rem; border-radius: 8px; font-size: .84rem; text-decoration: none; display: inline-block;
}
.funnel-tab-count { display: inline-block; margin-left: .35rem; padding: .02rem .45rem; border-radius: 6px; background: var(--count-bg); font-size: .7rem; font-family: var(--mono); }
.funnel-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.funnel-tab.active .funnel-tab-count { background: rgba(255,255,255,.25); color: #fff; }

.board-controls-right { display: flex; align-items: center; gap: .6rem; margin-left: auto; }
.filter-toggle { border: 1px solid var(--border); background: var(--card-bg); color: var(--fg); padding: .4rem .9rem; border-radius: 8px; font-size: .84rem; cursor: pointer; }
.filter-toggle.open { border-color: var(--accent); }
.unanswered-toggle { text-decoration: none; white-space: nowrap; }
.unanswered-toggle.has-items { border-color: var(--danger); color: var(--danger); font-weight: 700; }
.unanswered-toggle.open { background: var(--fg); color: var(--card-bg); border-color: var(--fg); }
.lead-card-unanswered { border-left: 3px solid var(--danger); }
.unanswered-badge { background: #fff0f0; color: #a72828; border-color: #e7aaaa; }
.filter-popover { display: none; position: absolute; top: calc(100% + .4rem); right: 0; z-index: 20; background: var(--card-bg); border: 1px solid var(--border); border-radius: 10px; padding: .8rem .9rem; font-size: .84rem; box-shadow: 0 8px 24px rgba(16,24,40,.14); min-width: 200px; }
.filter-popover.open { display: block; }
.filter-popover label { display: block; margin: .35rem 0; }
.filter-popover .filter-note { margin-top: .6rem; color: var(--muted); font-size: .72rem; }
.density-control { display: flex; gap: .25rem; margin-top: .7rem; }
.density-control button {
  flex: 1; border: 1px solid var(--border); border-radius: 5px; padding: .3rem .35rem;
  background: var(--card-bg); color: var(--fg); font-size: .7rem; cursor: pointer;
}
.density-control button.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---- stages / cards ---- */
.funnel-title { font-size: 1rem; font-weight: 700; margin: 0 0 .75rem; color: var(--accent); }
.stages { display: flex; gap: .9rem; overflow-x: auto; max-width: 100%; padding-bottom: .4rem; overscroll-behavior-x: contain; }
.stage { flex: 1 1 260px; min-width: 250px; background: var(--stage-bg); border-radius: 8px; padding: .7rem; display: flex; flex-direction: column; height: 78vh; }
.stage.stage-hidden { display: none; }
.stage-header { display: flex; justify-content: space-between; align-items: center; font-size: .8rem; font-weight: 600; color: var(--muted); padding: 0 .2rem .5rem; }
.stage-count { background: var(--count-bg); border-radius: 6px; padding: .05rem .5rem; font-size: .72rem; font-family: var(--mono); }
.cards {
  overflow-y: auto; display: flex; flex-direction: column; gap: .6rem;
  /* flex:1 + min-height:0 makes this fill the whole column (.stage is now a
     fixed height:78vh, not just max-height) instead of shrinking to fit
     content — an empty stage is now a full-height drop target, not a thin
     160px strip. */
  flex: 1 1 auto; min-height: 0;
  border: 1px dashed var(--border); border-radius: 6px;
}
.cards:has(.lead-card) { border-color: transparent; }

.lead-card {
  position: relative; display: flow-root;
  background: var(--card-bg); border-radius: 6px; padding: .6rem .65rem; box-shadow: var(--card-shadow);
  border: 1px solid var(--border); cursor: grab; font-size: .84rem; line-height: 1.4;
  user-select: none; -webkit-user-select: none; -ms-user-select: none; -webkit-touch-callout: none;
  /* A long unbroken token (a raw URL, an id string) must wrap inside the
     card instead of forcing the whole column wider / triggering horizontal
     scroll — normal word-wrap only breaks at spaces, which URLs don't have. */
  overflow-wrap: anywhere; word-break: break-word;
}
.card-drag-handle {
  display: none; float: right; width: 2.75rem; height: 2.75rem; margin: 0 0 .25rem .45rem;
  place-items: center; border: 1px solid var(--border); border-radius: 5px;
  background: var(--accent-light); color: var(--accent); line-height: 1; cursor: grab;
}
.card-drag-handle svg {
  width: 1.65rem; height: 1.65rem; fill: none; stroke: currentColor;
  stroke-width: 1.2; stroke-linecap: round; stroke-linejoin: round;
}
.lead-card.dragging-ghost { opacity: .35; background: var(--accent-light); border: 1px dashed var(--accent); box-shadow: none; }
.lead-card:active { cursor: grabbing; }
/* The clone that follows the cursor in forceFallback mode (fallbackOnBody
   appends it to <body>, so it must carry its own visible styling, not just
   inherit .lead-card's). Without this it can be effectively invisible. */
.sortable-drag-helper {
  background: var(--card-bg) !important; border: 1px solid var(--accent) !important;
  border-radius: 6px; box-shadow: 0 8px 24px rgba(0,0,0,.25) !important;
  opacity: .95 !important; pointer-events: none; z-index: 9999;
}
.card-link { color: inherit; text-decoration: none; display: block; -webkit-user-drag: none; user-drag: none; }
.account-error-banner {
  display: flex; flex-direction: column; gap: .12rem; margin: 0 0 .55rem;
  padding: .5rem .58rem; border-radius: 5px; background: #730d18; color: #fff;
  font-size: .7rem; line-height: 1.35; box-shadow: inset 0 0 0 1px #9f1827;
}
.account-error-banner strong { font-size: .74rem; }
.card-text { margin-bottom: .35rem; }
.card-tags { display: flex; gap: .35rem; flex-wrap: wrap; margin-bottom: .4rem; }
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 4px; font-size: .66rem; font-weight: 600; background: #e5e7eb; color: #374151; }
.badge.status-целевой { background: #dcfce7; color: #166534; }
.badge.status-творческий { background: #dbeafe; color: #1e40af; }
.badge.status-сомнения { background: #fef3c7; color: #92400e; }
.badge.status-нецелевой { background: #fee2e2; color: #991b1b; }
.tag-placeholder { display: inline-block; padding: .1rem .5rem; border-radius: 4px; font-size: .66rem; border: 1px dashed var(--border); color: var(--muted); }
.tag-chip { display: inline-block; padding: .1rem .5rem; border-radius: 4px; font-size: .66rem; border: 1px solid var(--accent); color: var(--accent); background: var(--accent-light); }
.tag-chip.removable { display: inline-flex; align-items: center; gap: .3rem; }
.tag-chip.removable button { border: none; background: none; color: inherit; cursor: pointer; font-size: .8rem; line-height: 1; padding: 0; }
.tag-add-row { display: flex; gap: .4rem; align-items: center; margin-top: .4rem; }
.card-footer { display: flex; flex-direction: column; align-items: flex-start; gap: .15rem; font-size: .7rem; color: var(--muted); font-family: var(--mono); }
.meta-extra { display: block; font-size: .65rem; }
.load-more { margin-top: .5rem; width: 100%; border: 1px dashed var(--border); background: none; color: var(--accent); padding: .4rem; border-radius: 6px; cursor: pointer; font-size: .78rem; }
.load-more:hover { background: var(--accent-light); }

.board[data-density="compact"] .stage { min-width: 225px; }
.board[data-density="compact"] .lead-card { padding: .48rem .52rem; font-size: .78rem; line-height: 1.32; }
.board[data-density="compact"] .cards { gap: .45rem; }
.board[data-density="dense"] .stage { min-width: 205px; padding: .55rem; }
.board[data-density="dense"] .lead-card { padding: .4rem .45rem; font-size: .72rem; line-height: 1.28; }
.board[data-density="dense"] .cards { gap: .35rem; }
.board[data-density="dense"] .card-footer { font-size: .62rem; }

/* ---- deal page / modal ---- */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(20,25,15,.45); z-index: 50; align-items: flex-start; justify-content: center; padding: 4vh 1rem; overflow-y: auto; }
.modal-overlay.open { display: flex; }
.modal-box { background: var(--bg); border-radius: 10px; max-width: 720px; width: 100%; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-close { position: sticky; top: 0; text-align: right; padding: .5rem .5rem 0; }
.modal-close button { border: none; background: none; font-size: 1.3rem; cursor: pointer; color: var(--muted); padding: .3rem .6rem; }

.deal-content { padding: 1.5rem 1.75rem; }
.deal-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1rem; }
.stage-quick-row { display: flex; flex-wrap: wrap; gap: .4rem; justify-content: flex-end; flex: 1; }
.stage-quick-btn { font-size: .78rem; padding: .3rem .7rem; }
.stage-quick-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; cursor: default; }
.deal-text { font-size: 1rem; line-height: 1.6; white-space: pre-wrap; background: var(--card-bg); border: 1px solid var(--border); border-radius: 8px; padding: 1rem; margin-bottom: 1rem; }
.deal-meta { font-family: var(--mono); font-size: .78rem; color: var(--muted); display: grid; grid-template-columns: auto 1fr; gap: .3rem 1rem; margin-bottom: 1.2rem; }
.deal-meta dt { font-weight: 600; }
.deal-meta dd { margin: 0; }
.deal-section { margin-bottom: 1.2rem; }
.deal-section label { display: block; font-size: .8rem; font-weight: 600; margin-bottom: .4rem; color: var(--muted); }
.deal-section select, .deal-section textarea { width: 100%; border: 1px solid var(--border); border-radius: 6px; padding: .5rem .6rem; font-size: .88rem; font-family: inherit; background: var(--card-bg); color: var(--fg); }
.deal-section textarea { min-height: 90px; resize: vertical; }
.deal-actions { display: flex; gap: .6rem; align-items: center; }
.dialogue-heading { display: flex; align-items: center; justify-content: space-between; margin-bottom: .45rem; }
.dialogue-heading label { margin-bottom: 0; }
.dialogue-heading-status { display: flex; align-items: center; justify-content: flex-end; gap: .45rem; flex-wrap: wrap; }
.dialogue-send-countdown { font-size: .72rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.dialogue-history {
  max-height: 320px; overflow-y: auto; margin-bottom: .65rem; padding: .55rem 0;
  border-top: 1px solid var(--border); border-bottom: 1px solid var(--border);
}
.dialogue-message { max-width: 88%; margin-bottom: .55rem; font-size: .86rem; line-height: 1.45; }
.dialogue-message.ours { margin-left: auto; padding-left: .8rem; border-left: 3px solid var(--accent); }
.dialogue-message.client { padding-right: .8rem; border-right: 3px solid #d97706; }
.dialogue-message-meta { margin-bottom: .15rem; color: var(--muted); font-size: .7rem; font-weight: 600; }
.dialogue-message-text { white-space: pre-wrap; }
.dialogue-panel textarea { margin-bottom: .55rem; }
.btn { border: 1px solid var(--border); background: var(--card-bg); color: var(--fg); padding: .45rem .9rem; border-radius: 6px; cursor: pointer; font-size: .84rem; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-danger { background: #d33; border-color: #d33; color: #fff; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

/* ---- search: view toggle / table / bulk toolbar / pagination ---- */
.view-toggle { display: flex; gap: .5rem; margin: .8rem 0; }
.view-toggle .btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.search-view[hidden] { display: none; }
.bulk-toolbar { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; margin-bottom: .6rem; }
.bulk-toolbar .bulk-count { font-size: .82rem; color: var(--muted); }
.bulk-toolbar select { border: 1px solid var(--border); border-radius: 6px; padding: .4rem .6rem; font-size: .84rem; background: var(--card-bg); color: var(--fg); }
.search-table td.text-cell { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.search-table td a { color: inherit; }
.pagination { display: flex; gap: 1rem; align-items: center; margin: 1rem 0; font-size: .84rem; }
.save-status { font-size: .78rem; color: var(--muted); }

/* ---- settings / analytics / search ---- */
h1.page-title { font-size: 1.15rem; margin: 0 0 1.2rem; }
.settings-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.settings-card { background: var(--card-bg); border: 1px solid var(--border); border-radius: 8px; padding: 1rem; }
.settings-card dt { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.settings-card dd { margin: .2rem 0 0; font-family: var(--mono); font-size: .95rem; }
.settings-disclosure { border-top: 1px solid var(--border); margin-top: 1rem; padding-top: .75rem; }
.settings-disclosure summary { cursor: pointer; font-size: .95rem; font-weight: 600; }
.settings-disclosure-content { padding-top: .65rem; }

.stat-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 1rem; margin-bottom: 1.6rem; }
.stat-tile { background: var(--card-bg); border: 1px solid var(--border); border-radius: 8px; padding: .9rem 1rem; }
.stat-tile .label { font-size: .78rem; color: var(--muted); margin-bottom: .3rem; }
.stat-tile .value { font-size: 1.6rem; font-weight: 600; font-variant-numeric: proportional-nums; }

.funnel-breakdown { margin-bottom: 1.5rem; }
.funnel-breakdown h3 { font-size: .9rem; margin: 0 0 .5rem; color: var(--accent); }
.meter-row { display: grid; grid-template-columns: 130px 1fr 3rem; align-items: center; gap: .6rem; font-size: .78rem; margin-bottom: .35rem; }
.meter-track { background: var(--accent-light); border-radius: 4px; height: .55rem; overflow: hidden; }
.meter-fill { background: var(--accent); height: 100%; }
.meter-value { font-family: var(--mono); text-align: right; font-variant-numeric: tabular-nums; }
.analytics-system-info { clear: both; border-top: 1px solid var(--border); margin-top: 2rem; padding-top: 1.2rem; }
.analytics-system-info h2 { font-size: .95rem; margin: 0 0 .8rem; }
.analytics-system-info .settings-grid { margin-bottom: .7rem; }
.analytics-system-info p { color: var(--muted); font-size: .78rem; margin: 0; }

.search-form { background: var(--card-bg); border: 1px solid var(--border); border-radius: 8px; padding: 1rem 1.2rem; margin-bottom: 1.4rem; }
.search-row { display: flex; gap: 1rem; flex-wrap: wrap; margin-bottom: .8rem; }
.search-row label { font-size: .8rem; font-weight: 600; color: var(--muted); display: block; margin-bottom: .3rem; }
.search-row input[type="text"], .search-row input[type="date"] { border: 1px solid var(--border); border-radius: 6px; padding: .4rem .6rem; font-size: .85rem; }
.checkbox-group { display: flex; gap: .8rem; flex-wrap: wrap; font-size: .82rem; }
.results-list { display: flex; flex-direction: column; gap: .6rem; }
.results-count { font-size: .82rem; color: var(--muted); margin-bottom: .8rem; }

/* ---- login (kept from stage 1) ---- */
.login-page { display: flex; align-items: center; justify-content: center; height: 100vh; }
.login-page form { background: var(--card-bg); border: 1px solid var(--border); padding: 2rem; border-radius: 10px; width: 320px; }
.login-page h1 { font-size: 1.1rem; margin: 0 0 1.2rem; }
.login-page label { display: block; font-size: .85rem; margin: .8rem 0 .3rem; color: var(--muted); }
.login-page input { width: 100%; box-sizing: border-box; padding: .55rem .7rem; border-radius: 8px; border: 1px solid var(--border); font-size: 1rem; }
.login-page button { margin-top: 1.2rem; width: 100%; padding: .6rem; border: none; border-radius: 8px; background: var(--accent); color: #fff; font-size: 1rem; cursor: pointer; }
.login-page .error { color: #b91c1c; font-size: .85rem; margin-top: .8rem; }

@media (max-width: 600px), (orientation: landscape) and (max-width: 900px) and (max-height: 600px) {
  html { height: auto; min-height: 100%; overflow-x: hidden; }
  body {
    display: block; width: 100%; min-height: 100dvh; height: auto; overflow-x: hidden;
  }
  .topbar {
    display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: .5rem;
    width: 100%; padding: max(.55rem, env(safe-area-inset-top)) max(.75rem, env(safe-area-inset-right)) .55rem max(.75rem, env(safe-area-inset-left));
  }
  .brand { min-width: 0; margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .nav-toggle { display: inline-grid; place-items: center; }
  .primary-nav {
    display: none; grid-column: 1 / -1; width: 100%; margin: 0;
    grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .3rem;
  }
  .primary-nav a { min-width: 0; padding: .5rem .6rem; text-align: center; }
  .logout-form { display: none; grid-column: 1 / -1; }
  .logout-form button { width: 100%; }
  .topbar.nav-open .primary-nav { display: grid; }
  .topbar.nav-open .logout-form { display: block; }

  .layout { display: block; min-height: 0; }
  .board {
    width: 100%; min-width: 0; overflow: visible; padding: .55rem;
    padding-left: max(.4rem, env(safe-area-inset-left));
    padding-right: max(.4rem, env(safe-area-inset-right));
  }
  .board-controls { align-items: flex-start; }
  .funnel-tabs { flex: 1 1 100%; gap: .3rem; }
  .funnel-tab { padding: .35rem .55rem; font-size: .76rem; }
  .board-controls-right {
    width: 100%; min-width: 0; margin-left: 0; gap: .35rem; flex-wrap: wrap;
  }
  .board-controls-right .filter-toggle { padding: .35rem .55rem; font-size: .76rem; }
  .filter-popover {
    position: fixed; top: auto; right: .75rem;
    bottom: max(.75rem, env(safe-area-inset-bottom)); max-height: 70dvh; overflow-y: auto;
  }
  .funnel { min-width: 0; }
  .stage {
    flex: 0 0 min(88vw, 310px); min-width: min(88vw, 310px); padding: .55rem;
    height: max(320px, calc(100dvh - 7rem));
    scroll-snap-align: start;
  }
  .stages { width: 100%; max-width: 100%; gap: .55rem; scroll-snap-type: x proximity; }
  .board[data-density="compact"] .stage { flex-basis: min(82vw, 280px); min-width: min(82vw, 280px); }
  .board[data-density="dense"] .stage { flex-basis: min(74vw, 245px); min-width: min(74vw, 245px); }
  .cards { -webkit-overflow-scrolling: touch; overscroll-behavior-y: contain; }
  .modal-overlay {
    padding: max(.5rem, env(safe-area-inset-top)) max(.5rem, env(safe-area-inset-right)) max(.5rem, env(safe-area-inset-bottom)) max(.5rem, env(safe-area-inset-left));
    max-height: 100dvh;
  }
  .modal-box { border-radius: 6px; }
  .deal-content { padding: .85rem; }
  .deal-header { flex-direction: column; gap: .6rem; }
  .stage-quick-row { justify-content: flex-start; }
  .dialogue-message { max-width: 96%; }
  .dialogue-panel .deal-actions { align-items: stretch; flex-direction: column; }
  .page { width: 100%; max-width: 100%; padding: 1rem .85rem; overflow-x: auto; }
}

.touch-input .lead-card, .touch-input .card-link { touch-action: pan-y; }
.touch-input .card-drag-handle {
  display: grid; touch-action: none; -webkit-tap-highlight-color: transparent;
}
