:root {
  --bg: #f4f6f4;
  --surface: #ffffff;
  --surface-2: #fafbfa;
  --border: #dfe4df;
  --border-strong: #c5ccc5;
  --text: #1b241d;
  --muted: #6b756e;
  /* Фирмени цветове на Агро Цар Петрово, взети от логото (#006747). */
  --brand: #006747;
  --accent: #006747;
  --accent-dark: #00513a;
  --accent-soft: #e4efeb;
  --danger: #c0392b;
  --danger-soft: #fdecea;
  --warn: #b7791f;
  --warn-soft: #fdf4e3;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(20,40,25,.06), 0 4px 14px rgba(20,40,25,.06);
  --mono: ui-monospace, SFMono-Regular, Menlo, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* ---------------------------------------------------------------- app shell */
.app { display: flex; flex-direction: column; height: 100vh; }
.topbar {
  display: flex; align-items: center; gap: 20px;
  padding: 0 18px; height: 54px; flex: none;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand {
  display: flex; align-items: center; gap: 10px; font-weight: 650;
  letter-spacing: -.01em; color: var(--text); text-decoration: none; white-space: nowrap;
}
.brand:hover { color: var(--accent-dark); }
.brand .mark { width: 30px; height: 30px; border-radius: 8px; flex: none; display: block; }
.nav { display: flex; gap: 2px; margin-left: 6px; }
.nav a {
  padding: 7px 13px; border-radius: 8px; text-decoration: none;
  color: var(--muted); font-weight: 550; white-space: nowrap;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent-dark); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }

.main { flex: 1; min-height: 0; overflow: auto; }
.page { padding: 22px; max-width: 1500px; margin: 0 auto; }
.page-head { display: flex; align-items: flex-end; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 21px; letter-spacing: -.02em; }
.page-head .sub { color: var(--muted); font-size: 13px; }
.page-head .spacer { margin-left: auto; }

/* -------------------------------------------------------------------- cards */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 18px;
}
.card-head {
  padding: 13px 16px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.card-head h2 { margin: 0; font-size: 14px; font-weight: 650; letter-spacing: -.01em; }
.card-head .spacer { margin-left: auto; }
.card-body { padding: 16px; }
.card-body.tight { padding: 0; }

.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }

/* -------------------------------------------------------------------- stats */
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .label { color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; font-weight: 600; }
.stat .value { font-size: 24px; font-weight: 650; letter-spacing: -.02em; margin-top: 5px; font-variant-numeric: tabular-nums; }
.stat .hint { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* ------------------------------------------------------------------ controls */
button, .btn {
  font: inherit; font-weight: 550; cursor: pointer;
  padding: 7px 13px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
  display: inline-flex; align-items: center; gap: 6px; text-decoration: none;
}
button:hover, .btn:hover { background: var(--surface-2); }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover, .btn.primary:hover { background: var(--accent-dark); }
button.danger { color: var(--danger); border-color: #e8c4bf; background: var(--surface); }
button.danger:hover { background: var(--danger-soft); }
button.ghost { border-color: transparent; background: transparent; color: var(--muted); }
button.ghost:hover { background: var(--surface-2); color: var(--text); }
button.sm, .btn.sm { padding: 4px 9px; font-size: 12.5px; border-radius: 7px; }
button:disabled { opacity: .5; cursor: not-allowed; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  padding: 7px 10px; border: 1px solid var(--border-strong); border-radius: 8px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; min-height: 62px; }
input[type=color] { padding: 2px; height: 34px; cursor: pointer; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 1; min-width: 0; }
.row > .none { flex: none; }
.inline { display: flex; gap: 8px; align-items: center; }
.checkbox { display: flex; align-items: center; gap: 7px; cursor: pointer; }
.checkbox input { width: auto; }

/* ------------------------------------------------------------------- tables */
table { width: 100%; border-collapse: collapse; }
th, td { padding: 9px 14px; text-align: left; border-bottom: 1px solid var(--border); }
th {
  font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--muted); font-weight: 650; background: var(--surface-2);
  position: sticky; top: 0; z-index: 1; white-space: nowrap;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.actions { text-align: right; white-space: nowrap; }
.table-wrap { overflow: auto; max-height: 70vh; }

/* -------------------------------------------------------------------- chips */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--surface-2); border: 1px solid var(--border);
}
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; flex: none; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.pill-low { background: var(--danger-soft); color: var(--danger); border-color: #f0cfc9; }
.pill-ok { background: var(--accent-soft); color: var(--accent-dark); border-color: #cfe4d6; }

/* -------------------------------------------------------------------- modal */
.modal-back {
  position: fixed; inset: 0; background: rgba(20,30,22,.42);
  display: grid; place-items: center; z-index: 3000; padding: 20px;
}
.modal {
  background: var(--surface); border-radius: 14px; width: min(680px, 100%);
  max-height: 88vh; display: flex; flex-direction: column; box-shadow: 0 18px 50px rgba(0,0,0,.28);
}
.modal-head { padding: 15px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; }
.modal-head h3 { margin: 0; font-size: 15px; }
.modal-body { padding: 18px; overflow: auto; }
.modal-foot { padding: 13px 18px; border-top: 1px solid var(--border); display: flex; gap: 9px; justify-content: flex-end; }
.hidden { display: none !important; }

/* ------------------------------------------------------------------- alerts */
.alert { padding: 11px 14px; border-radius: 9px; margin-bottom: 14px; font-size: 13px; border: 1px solid; }
.alert.ok { background: var(--accent-soft); border-color: #cfe4d6; color: var(--accent-dark); }
.alert.err { background: var(--danger-soft); border-color: #f0cfc9; color: var(--danger); }
.alert.warn { background: var(--warn-soft); border-color: #ecdcb8; color: var(--warn); }
.alert ul { margin: 6px 0 0; padding-left: 18px; }

.empty { padding: 40px 20px; text-align: center; color: var(--muted); }
.empty .big { font-size: 30px; margin-bottom: 8px; }

/* ------------------------------------------------------------------- toasts */
#toasts { position: fixed; bottom: 18px; right: 18px; z-index: 5000; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: #22302610; background: #1f2a22; color: #fff; padding: 11px 15px;
  border-radius: 9px; box-shadow: 0 8px 24px rgba(0,0,0,.25); font-size: 13px;
  max-width: 380px; animation: rise .18s ease-out;
}
.toast.err { background: #8c2f24; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

/* --------------------------------------------------------------- map layout */
.map-layout { display: flex; height: 100%; min-height: 0; }
#map { flex: 1; min-width: 0; background: #dfe4df; }
.map-side {
  width: 330px; flex: none; background: var(--surface);
  border-right: 1px solid var(--border); display: flex; flex-direction: column; overflow: hidden;
}
.map-side.right { border-right: none; border-left: 1px solid var(--border); width: 370px; }
.side-head { padding: 13px 15px; border-bottom: 1px solid var(--border); flex: none; }
.side-body { overflow: auto; flex: 1; padding: 13px 15px; }
.side-foot { padding: 11px 15px; border-top: 1px solid var(--border); flex: none; background: var(--surface-2); }
.side-title { font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 650; margin: 0 0 8px; }

.legend-item {
  display: flex; align-items: center; gap: 9px; padding: 7px 9px;
  border-radius: 8px; cursor: pointer; user-select: none; border: 1px solid transparent;
}
.legend-item:hover { background: var(--surface-2); }
.legend-item.off { opacity: .38; }
.legend-item .nm { flex: 1; font-weight: 550; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend-item .qty { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; }
.legend-swatch { display: inline-block; width: 14px; height: 14px; border-radius: 4px; flex: none; border: 1px solid rgba(0,0,0,.15); }

.field-row {
  display: flex; align-items: center; gap: 9px; padding: 7px 9px;
  border-radius: 8px; cursor: pointer; border: 1px solid transparent;
}
.field-row:hover { background: var(--surface-2); }
.field-row.selected { background: var(--accent-soft); border-color: #cfe4d6; }
.field-row .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 550; }
.field-row .ar { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

.field-label {
  background: rgba(255,255,255,.86); border: 1px solid rgba(0,0,0,.18);
  border-radius: 6px; padding: 2px 6px; font-size: 11px; font-weight: 650;
  color: #1b241d; white-space: nowrap; box-shadow: 0 1px 3px rgba(0,0,0,.2);
}
.field-label .a { font-weight: 500; color: #4a554d; }
.leaflet-tooltip.field-label::before { display: none; }

.timeline { border-left: 2px solid var(--border); margin-left: 7px; padding-left: 14px; }
.timeline .ev { position: relative; padding: 9px 0; border-bottom: 1px solid var(--border); }
.timeline .ev:last-child { border-bottom: none; }
.timeline .ev::before {
  content: ''; position: absolute; left: -21px; top: 15px;
  width: 9px; height: 9px; border-radius: 50%; background: var(--accent); border: 2px solid var(--surface);
}
.timeline .ev .d { font-size: 12px; color: var(--muted); }
.timeline .ev .t { font-weight: 600; }

.bar { height: 8px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 999px; }

@media (max-width: 900px) {
  .map-layout { flex-direction: column; }
  .map-side, .map-side.right { width: auto; max-height: 240px; border: none; border-top: 1px solid var(--border); }
  #map { min-height: 320px; }
  .nav a { padding: 7px 9px; }
}

/* ------------------------------------------------------------- expense viz */
.viz {
  --series-materials: #2a78d6;
  --series-labour:    #eb6834;
  --series-machinery: #1baf7a;
  --series-other:     #eda100;
  --series-magnitude: #006747;
  --series-sales:     #1baf7a;
  --series-subsidy:   #4a3aa7;
  /* Печалба/загуба: синьо ↔ оранжево, а не зелено/червено — остава четимо
     и при далтонизъм, а знакът е винаги изписан и в текста. */
  --series-profit:    #2a78d6;
  --series-loss:      #eb6834;
}
.split-bar {
  display: flex; gap: 2px; height: 34px; border-radius: 6px; overflow: hidden;
  background: var(--surface-2); margin-bottom: 12px;
}
.split-bar > i { display: block; height: 100%; }
.split-bar > i:first-child { border-radius: 6px 0 0 6px; }
.split-bar > i:last-child { border-radius: 0 6px 6px 0; }
.split-legend { display: flex; flex-wrap: wrap; gap: 16px; }
.split-legend .li { display: flex; align-items: center; gap: 7px; font-size: 13px; }
.split-legend .li b { font-variant-numeric: tabular-nums; }
.split-legend .sw { width: 11px; height: 11px; border-radius: 3px; flex: none; }

.rank { display: flex; flex-direction: column; gap: 9px; }
.rank .r { display: grid; grid-template-columns: 1fr auto; gap: 3px 12px; align-items: baseline; cursor: default; }
.rank .r .nm { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank .r .vl { font-variant-numeric: tabular-nums; font-weight: 600; }
.rank .r .track { grid-column: 1 / -1; height: 9px; border-radius: 5px; background: var(--surface-2); overflow: hidden; }
.rank .r .track > i { display: block; height: 100%; border-radius: 5px; }
.rank .r .sub { grid-column: 1 / -1; font-size: 12px; color: var(--muted); margin-top: -1px; }
.rank .r:hover .nm { color: var(--accent-dark); }

#vizTip {
  position: fixed; z-index: 6000; pointer-events: none; opacity: 0;
  transition: opacity .1s; background: #1f2a22; color: #fff; font-size: 12.5px;
  padding: 7px 10px; border-radius: 7px; box-shadow: 0 6px 18px rgba(0,0,0,.28); max-width: 260px;
}
#vizTip .h { font-weight: 650; margin-bottom: 2px; }

/* ---------------------------------------------------------- login & setup */
.auth-page { display: grid; place-items: center; min-height: 100vh; background: var(--bg); padding: 20px; }
.auth-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: var(--shadow); padding: 26px; width: min(400px, 100%);
}
.auth-card h1 { font-size: 19px; margin: 14px 0 16px; letter-spacing: -.02em; }
.auth-logo { display: block; width: 100%; max-width: 250px; height: auto; margin: 2px auto 4px; }

/* ------------------------------------------------------------- user menu */
.usermenu { position: relative; }
.usermenu .avatar {
  width: 21px; height: 21px; border-radius: 50%; background: var(--accent);
  color: #fff; display: grid; place-items: center; font-size: 11px; font-weight: 700;
}
.usermenu-pop {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 2500;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0,0,0,.15); min-width: 200px; padding: 4px; display: flex; flex-direction: column;
}
.usermenu-pop > button, .usermenu-pop > a {
  justify-content: flex-start; width: 100%; border-radius: 7px; text-align: left;
}

/* --------------------------------------------------- финансов блок в панела */
.moneyline { display: flex; justify-content: space-between; gap: 10px; padding: 4px 0; font-size: 13px; }
.moneyline b { font-variant-numeric: tabular-nums; }
.moneyline.total { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 7px; font-weight: 600; }

/* -------------------------------------------------- двупосочни (±) стълбове */
.divrow { display: grid; grid-template-columns: 1fr auto; gap: 3px 12px; align-items: baseline; }
.divrow .nm { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.divrow .vl { font-variant-numeric: tabular-nums; font-weight: 650; }
.divrow .sub { grid-column: 1 / -1; font-size: 12px; color: var(--muted); }
.divtrack {
  grid-column: 1 / -1; position: relative; height: 11px;
  background: var(--surface-2); border-radius: 5px; overflow: hidden;
}
.divtrack::before {
  content: ''; position: absolute; left: 50%; top: 0; bottom: 0;
  width: 1px; background: var(--border-strong);
}
.divtrack.nozero::before { display: none; }   /* няма загуба — няма нулева линия */
.divtrack > i { position: absolute; top: 0; bottom: 0; border-radius: 3px; }

/* Ниви, които още носят автоматично име */
.field-row .nm.unnamed { color: var(--muted); font-style: italic; font-weight: 500; }

/* ------------------------------------------- избор на артикул с търсене */
.picker { position: relative; }
.picker-list {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 40;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: 9px;
  box-shadow: 0 10px 28px rgba(0,0,0,.16); max-height: 260px; overflow: auto;
}
.picker-list .opt { padding: 7px 11px; cursor: pointer; border-bottom: 1px solid var(--border); }
.picker-list .opt:last-child { border-bottom: none; }
.picker-list .opt:hover, .picker-list .opt.active { background: var(--accent-soft); }
.picker-list .opt .nm { font-weight: 550; }
.picker-list .opt .meta { font-size: 12px; color: var(--muted); }
.picker-list .none { padding: 10px 11px; color: var(--muted); font-size: 13px; }

/* ------------------------------------------------ избор на много ниви */
.fieldpick { border: 1px solid var(--border-strong); border-radius: 9px; overflow: hidden; }
.fieldpick-head { display: flex; gap: 8px; padding: 8px; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.fieldpick-list { max-height: 220px; overflow: auto; }
.fieldpick-list label {
  display: flex; align-items: center; gap: 9px; padding: 6px 11px; cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.fieldpick-list label:last-child { border-bottom: none; }
.fieldpick-list label:hover { background: var(--surface-2); }
.fieldpick-list input { width: auto; }
.fieldpick-list .nm { flex: 1; }
.fieldpick-list .ar { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.fieldpick-foot { padding: 8px 11px; background: var(--surface-2); border-top: 1px solid var(--border); font-size: 13px; }

/* -------------------------------- преглед на внос: таблица + карта един до друг */
.review { display: flex; gap: 16px; align-items: flex-start; }
.review-left { flex: 1; min-width: 0; }
.review-right { width: 400px; flex: none; position: sticky; top: 0; }
.review-map {
  height: 340px; border-radius: 10px; border: 1px solid var(--border-strong);
  background: var(--surface-2); overflow: hidden;
}
.review-legend { display: flex; flex-wrap: wrap; gap: 12px; margin-top: 8px; font-size: 12.5px; }
.review-legend .li { display: flex; align-items: center; gap: 6px; }
.review-legend .sw { width: 16px; height: 10px; border-radius: 2px; flex: none; }
.review-hint { color: var(--muted); font-size: 12.5px; margin-top: 8px; }
.review tbody tr { cursor: pointer; }
.review tbody tr.picked { background: var(--accent-soft); }
.review tbody tr.picked td:first-child { box-shadow: inset 3px 0 0 var(--accent); }
@media (max-width: 1000px) {
  .review { flex-direction: column; }
  .review-right { width: auto; align-self: stretch; position: static; }
}

/* ---------------------------------------- помощник за сеитбена норма */
.seed-calc {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  margin: 4px 0 0 2px; padding: 7px 10px; font-size: 13px;
  background: var(--accent-soft); border-radius: 8px;
}
.seed-calc input.seeds-m2 { width: 170px; }
.seed-calc .calc-out { font-variant-numeric: tabular-nums; }

/* показателите в панела на нивата — по две на ред */
.qual-row { flex-wrap: wrap; }
.qual-row > label { flex: 1 1 calc(50% - 5px); min-width: 120px; }
