/* CRM — סקיי תרגומים. Strict black/white/gray per the locked design decision: red is spent
   entirely on "stuck" and "customer asked for a human" — the moment it means anything else
   it stops meaning anything. */

:root {
  --paper: #ffffff;
  --paper-2: #f7f7f6;
  --paper-3: #efefed;
  --ink: #111111;
  --ink-soft: #4a4a48;
  --ink-faint: #86847e;
  --line: #dedcd6;
  --line-soft: #ececea;
  --red: #b3261e;
  --red-bg: #fdecea;
  --red-bright: #e11d2e;

  --serif: "Frank Ruhl Libre", serif;
  --sans: "Assistant", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--paper-2);
  color: var(--ink);
  font-family: var(--sans);
  direction: rtl;
  min-height: 100vh;
}
a { color: inherit; }
button { font-family: inherit; }
h1, h2, h3 { font-family: var(--serif); font-weight: 700; margin: 0; }

/* ── Shell ────────────────────────────────────────────────────────────────── */
/* Added to the iPhone home screen the app launches standalone, with no Safari chrome — the
   page then starts at the physical top of the screen, so without this the header sits under
   the clock and battery. env() resolves to 0 in a normal browser tab, so one rule covers
   both cases. */
header.top {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px;
  padding-top: calc(14px + env(safe-area-inset-top, 0px));
  padding-left: calc(20px + env(safe-area-inset-left, 0px));
  padding-right: calc(20px + env(safe-area-inset-right, 0px));
  background: var(--paper); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
/* Brand sits first in the DOM, so in RTL it lands top-RIGHT — which is what the spec asked
   for. Width/height are set on the <img> itself so the row doesn't reflow once it loads. */
.brand { display: flex; align-items: center; gap: 10px; font-family: var(--serif); font-weight: 700; font-size: 1.3rem; letter-spacing: -0.01em; }
.brand small { display: block; font-family: var(--sans); font-weight: 500; font-size: 0.72rem; color: var(--ink-faint); }
.logo-sm { width: 34px; height: 34px; object-fit: contain; flex: 0 0 auto; }
.logo-lg { width: 72px; height: 72px; object-fit: contain; margin: 0 auto 14px; display: block; }
.top-actions { display: flex; align-items: center; gap: 12px; font-size: 0.88rem; color: var(--ink-soft); }
.top-actions button.link {
  background: none; border: none; color: var(--ink-soft); cursor: pointer; padding: 4px;
  text-decoration: underline; text-underline-offset: 3px;
}

main {
  max-width: 1400px; margin: 0 auto;
  padding: 18px 20px 60px;
  /* Keep the last row of cards clear of the home indicator in standalone mode. */
  padding-bottom: calc(60px + env(safe-area-inset-bottom, 0px));
  padding-left: calc(20px + env(safe-area-inset-left, 0px));
  padding-right: calc(20px + env(safe-area-inset-right, 0px));
}

/* ── Login ────────────────────────────────────────────────────────────────── */
#login-screen {
  min-height: calc(100vh - 60px); display: flex; align-items: center; justify-content: center;
}
.login-box {
  background: var(--paper); border: 1px solid var(--line); border-radius: 14px;
  padding: 34px 30px; width: min(360px, 92vw); text-align: center;
}
.login-box h1 { font-size: 1.5rem; margin-bottom: 6px; }
.login-box p.sub { color: var(--ink-faint); font-size: 0.9rem; margin: 0 0 22px; }
.login-box input {
  width: 100%; padding: 11px 12px; margin-bottom: 12px; border: 1px solid var(--line);
  border-radius: 8px; font-size: 1rem; font-family: inherit; background: var(--paper-2);
}
.login-box button.primary { width: 100%; margin-top: 6px; }
.login-error { color: var(--red); font-size: 0.86rem; margin-top: 10px; min-height: 1.2em; }

button.primary {
  background: var(--ink); color: #fff; border: none; border-radius: 8px;
  padding: 11px 18px; font-size: 0.96rem; font-weight: 600; cursor: pointer;
}
button.primary:hover { background: #000; }
button.secondary {
  background: var(--paper); color: var(--ink); border: 1px solid var(--line); border-radius: 8px;
  padding: 9px 16px; font-size: 0.9rem; cursor: pointer;
}
button.secondary:hover { border-color: var(--ink-faint); }
button.danger { background: var(--red); color: #fff; border: none; }
button.danger:hover { background: #8f1e17; }
button[disabled] { opacity: 0.45; cursor: not-allowed; }

/* ── Metrics bar ──────────────────────────────────────────────────────────── */
.metrics { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 22px; }
.metric {
  background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px;
}
.metric .num { font-family: var(--serif); font-size: 1.9rem; font-weight: 700; line-height: 1; }
.metric .label { color: var(--ink-faint); font-size: 0.82rem; margin-top: 6px; }

/* ── Board (desktop) ──────────────────────────────────────────────────────── */
.tabbar { display: none; }
.board { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 10px; }
.column { background: var(--paper-3); border-radius: 12px; padding: 10px; min-height: 200px; display: flex; flex-direction: column; }
.column-head { padding: 6px 6px 10px; }
.column-head .name { font-weight: 700; font-size: 0.86rem; }
.column-head .stats { color: var(--ink-faint); font-size: 0.76rem; margin-top: 2px; }
.column-cards { display: flex; flex-direction: column; gap: 8px; overflow-y: auto; }

.card {
  background: var(--paper); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
  cursor: pointer; text-align: right;
}
.card:hover { border-color: var(--ink-faint); }
.card .name { font-weight: 700; font-size: 0.92rem; }
.card .phone { font-size: 0.82rem; color: var(--ink-soft); margin-top: 2px; }
.card .phone a { text-decoration: none; }
.card .phone a:hover { text-decoration: underline; }
.card .meta { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; font-size: 0.78rem; color: var(--ink-faint); }
.card .price { font-weight: 600; color: var(--ink); }

.card.stuck { border-color: var(--red); }
.card.stuck .age { color: var(--red); font-weight: 700; }
.card.agent {
  border-color: var(--red-bright); border-width: 2px; background: var(--red-bg);
  box-shadow: 0 0 0 1px var(--red-bright);
}
.dot-pulse {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--red-bright);
  margin-left: 6px; animation: pulse 1.1s infinite;
}
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.25; } }

.empty-col { color: var(--ink-faint); font-size: 0.8rem; text-align: center; padding: 20px 4px; }

/* ── Top-level navigation ─────────────────────────────────────────────────── */
.nav-tabs { display: flex; gap: 8px; margin-bottom: 16px; }
.nav-tabs .nav-tab {
  background: var(--paper); border: 1px solid var(--line); border-radius: 999px;
  padding: 8px 18px; font-size: 0.9rem; color: var(--ink-soft); cursor: pointer;
}
.nav-tabs .nav-tab.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.nav-tabs .nav-tab:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

/* ── Document scans ───────────────────────────────────────────────────────── */
.scans { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
.scan {
  width: 84px; height: 84px; padding: 0; border: 1px solid var(--line); border-radius: 8px;
  background: var(--paper-2); cursor: pointer; overflow: hidden; display: flex;
  align-items: center; justify-content: center; flex: 0 0 auto;
}
.scan img { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan:hover { border-color: var(--ink-faint); }
.scan-status { font-size: 0.66rem; color: var(--ink-faint); padding: 4px; text-align: center; line-height: 1.3; }
/* Meta deletes media ~30 days after receipt, so an expired scan is an ordinary state for an
   older order, not an error — it reads as muted, not alarming. Red stays reserved. */
.scan-gone { border-style: dashed; cursor: default; background: var(--paper); }
.scan-pdf .scan-status { font-weight: 700; color: var(--ink); }

/* ── Proactive messaging ──────────────────────────────────────────────────── */
.window { font-size: 0.84rem; padding: 7px 10px; border-radius: 7px; margin-bottom: 10px; }
.window.open { background: var(--paper-3); color: var(--ink-soft); font-variant-numeric: tabular-nums; }
/* A closed window is a constraint, not a failure — it must not compete with a stuck deal
   for attention, so it stays in the neutral palette. */
.window.closed { background: var(--paper-3); color: var(--ink-soft); border-right: 3px solid var(--ink-faint); }
#msg-pick {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 7px;
  font-family: inherit; font-size: 0.9rem; background: var(--paper-2);
}
.msg-preview {
  margin-top: 10px; padding: 10px 12px; background: var(--paper-2); border: 1px solid var(--line-soft);
  border-radius: 8px; font-size: 0.88rem; white-space: pre-wrap; line-height: 1.5; color: var(--ink-soft);
}
#detail .hint { font-size: 0.78rem; color: var(--ink-faint); margin: 8px 0 0; line-height: 1.5; }

/* ── Settings ─────────────────────────────────────────────────────────────── */
#settings-view { display: none; max-width: 760px; }
#settings-view h2 { font-size: 1.4rem; margin-bottom: 14px; }
.set-card { background: var(--paper); border: 1px solid var(--line); border-radius: 12px; padding: 18px 20px; margin-bottom: 16px; }
.set-card h3 { font-size: 1rem; margin-bottom: 6px; }
.set-card .hint { font-size: 0.82rem; color: var(--ink-faint); margin: 0 0 14px; line-height: 1.5; }
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 7px 0; border-top: 1px solid var(--line-soft); }
.set-row:first-of-type { border-top: none; }
.set-row label { font-size: 0.9rem; }
.set-row span { display: flex; gap: 8px; align-items: center; flex: 0 0 auto; }
input.num { width: 76px; text-align: center; font-variant-numeric: tabular-nums; }
#settings-view input, #settings-view textarea, #settings-view select {
  padding: 7px 9px; border: 1px solid var(--line); border-radius: 7px; font-family: inherit;
  font-size: 0.9rem; background: var(--paper-2); min-width: 0;
}
#settings-view input:focus-visible,
#settings-view textarea:focus-visible,
#settings-view select:focus-visible { outline: 2px solid var(--ink); outline-offset: 1px; }
button.sm { padding: 6px 12px; font-size: 0.82rem; }
.table-wrap { overflow-x: auto; }
.set-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.set-table th { text-align: right; font-weight: 600; color: var(--ink-faint); font-size: 0.78rem; padding: 0 6px 8px; }
.set-table td { padding: 5px 6px; border-top: 1px solid var(--line-soft); }
.set-table td.ctr { text-align: center; width: 1%; white-space: nowrap; }
.set-table input { width: 100%; }
/* A disabled product is greyed, not hidden — it still labels historical orders. */
.set-table tr.off td { opacity: 0.45; }
.add-row { display: flex; gap: 8px; margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line-soft); }
.add-row #new-label { flex: 1 1 auto; }
.add-row.col { flex-direction: column; }
.add-row.col button { align-self: flex-start; }

/* ── Canned messages ──────────────────────────────────────────────────────── */
/* A card rather than a table row: the body is a paragraph the operator writes and rereads,
   and a one-line input hides exactly the part that decides whether it reads well. */
.msg-card { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 10px; }
.msg-card.off { opacity: 0.5; }
.msg-head { display: flex; gap: 8px; margin-bottom: 8px; }
.msg-head .m-title { flex: 1 1 auto; }
.msg-card textarea { width: 100%; resize: vertical; font: inherit; line-height: 1.5; }
.msg-foot { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.msg-foot .grow { flex: 1 1 auto; }

/* ── Lost drawer ──────────────────────────────────────────────────────────── */
#lost-view { display: none; }
.lost-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }
.lost-grid .card .reason { font-size: 0.78rem; color: var(--red); margin-top: 4px; }

/* ── Deal detail (modal / sheet) ──────────────────────────────────────────── */
#overlay {
  position: fixed; inset: 0; background: rgba(17, 17, 17, 0.45); display: none;
  align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
#overlay.open { display: flex; }
#detail {
  background: var(--paper); border-radius: 16px; width: min(640px, 100%); max-height: 88vh;
  overflow-y: auto; padding: 22px 24px 28px; position: relative;
}
#detail .close {
  position: absolute; top: 14px; left: 14px; background: none; border: none; font-size: 1.3rem;
  cursor: pointer; color: var(--ink-faint); line-height: 1;
}
#detail h2 { font-size: 1.35rem; margin-bottom: 2px; }
#detail .sub-id { color: var(--ink-faint); font-size: 0.85rem; margin-bottom: 16px; }
#detail .section { margin-top: 18px; padding-top: 14px; border-top: 1px solid var(--line-soft); }
#detail .section h3 { font-size: 0.86rem; color: var(--ink-faint); font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; margin-bottom: 10px; }
#detail .row { display: flex; justify-content: space-between; gap: 12px; font-size: 0.92rem; padding: 4px 0; }
#detail .row .k { color: var(--ink-faint); }
#detail .row .v { font-weight: 600; text-align: left; }
#detail .timeline-item { display: flex; gap: 10px; font-size: 0.86rem; padding: 3px 0; }
#detail .timeline-item .t { color: var(--ink-faint); min-width: 130px; }
#detail .doc-block { border: 1px solid var(--line-soft); border-radius: 8px; padding: 10px 12px; margin-bottom: 8px; }
#detail .doc-block .dtype { font-weight: 700; font-size: 0.9rem; }
#detail .doc-block .field { font-size: 0.82rem; color: var(--ink-soft); }
#detail textarea {
  width: 100%; min-height: 70px; border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
  font-family: inherit; font-size: 0.9rem; resize: vertical;
}
#detail .actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
#detail .call-links { display: flex; gap: 10px; margin-top: 4px; }
#detail .call-links a { font-size: 0.86rem; text-decoration: underline; text-underline-offset: 3px; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 0.74rem; background: var(--paper-3); color: var(--ink-soft); }
.badge.red { background: var(--red-bg); color: var(--red); }

/* ── Toasts ───────────────────────────────────────────────────────────────── */
#toast {
  position: fixed; bottom: 18px; right: 18px; background: var(--ink); color: #fff;
  padding: 12px 18px; border-radius: 10px; font-size: 0.88rem; z-index: 100; display: none;
  max-width: 320px;
}
#toast.show { display: block; }
#toast.error { background: var(--red); }

/* ── Agent-request popup ──────────────────────────────────────────────────── */
#agent-popup {
  position: fixed; inset: 0; background: rgba(17, 17, 17, 0.55); display: none;
  align-items: center; justify-content: center; z-index: 90;
}
#agent-popup.open { display: flex; }
#agent-popup .box {
  background: #fff; border: 2px solid var(--red-bright); border-radius: 14px; padding: 26px 28px;
  width: min(360px, 90vw); text-align: center;
}
#agent-popup h2 { color: var(--red-bright); font-size: 1.2rem; }

/* ── Mobile ───────────────────────────────────────────────────────────────── */
@media (max-width: 860px) {
  .metrics { grid-template-columns: repeat(2, 1fr); }
  .board { display: block; }
  .tabbar {
    display: flex; overflow-x: auto; gap: 6px; margin-bottom: 12px; padding-bottom: 4px;
    -webkit-overflow-scrolling: touch;
  }
  .tabbar button {
    flex: 0 0 auto; background: var(--paper); border: 1px solid var(--line); border-radius: 999px;
    padding: 8px 14px; font-size: 0.82rem; color: var(--ink-soft); cursor: pointer; white-space: nowrap;
  }
  .tabbar button.active { background: var(--ink); color: #fff; border-color: var(--ink); }
  .tabbar button .count { opacity: 0.7; margin-right: 4px; }
  .column { display: none; min-height: 0; }
  .column.active { display: flex; }
  .column-head { display: none; }
}

/* ── Internal notes log ──────────────────────────────────────────────────────
   A shared log, not a single overwritable box: every entry keeps its author and
   time, so two people working the same deal can see who said what. */
.note-log { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.note { border-inline-start: 2px solid var(--line); padding-inline-start: 10px; }
.note-meta { font-size: 12px; color: var(--ink-faint); display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.note-edited { font-style: italic; }
.note-text { font-size: 14px; white-space: pre-wrap; margin-top: 2px; }
/* Scoped to the note row on purpose — .top-actions button.link is the header's own style
   and must not inherit this one. */
.note-meta button.link { background: none; border: 0; padding: 0; color: var(--ink-faint); text-decoration: underline; cursor: pointer; font: inherit; font-size: 12px; margin-inline-start: auto; }
.note-meta button.link:hover { color: var(--ink); }
