:root{
  /* App */
  --bg:#f3f6fb;
  --panel:#ffffff;
  --panel2:#f8fafc;
  --text:#0f172a;
  --muted:#5b6b86;
  --accent:#2563eb;
  --danger:#dc2626;
  --border:rgba(15,23,42,.12);
  --shadow: 0 12px 28px rgba(2,6,23,.10);
  --radius:14px;

  /* Canvas */
  --canvas-bg: #f7f8fb;
  --canvas-border: rgba(2,6,23,.14);
  --canvas-grid: rgba(2,6,23,.06);
  --canvas-text: rgba(2,6,23,.82);
  --canvas-text2: rgba(2,6,23,.58);

  /* overlays */
  --overlay: rgba(2,6,23,.45);

  /* header a bit darker */
  --header-bg: rgba(233,238,246,.92);
  --header-bg2: rgba(233,238,246,.82);
}

*{box-sizing:border-box}

body{
  margin:0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  color:var(--text);
  background:
    radial-gradient(1000px 600px at 15% 0%, rgba(37,99,235,.10), transparent 55%),
    radial-gradient(900px 600px at 100% 0%, rgba(14,165,233,.08), transparent 55%),
    var(--bg);
}

header{
  position: sticky;
  top:0;
  z-index: 20;
  backdrop-filter: blur(10px);
  background: linear-gradient(to bottom, var(--header-bg), var(--header-bg2));
  border-bottom: 1px solid var(--border);
  box-shadow: 0 10px 18px rgba(2,6,23,.06);
}

header.collapsed .toolbar,
header.collapsed .minirow{
  display:none;
}

/* top line */
.topbar{
  display:flex;
  align-items:center;
  gap:10px;
  padding:10px 12px;
}
.brand{
  display:flex;
  align-items:center;
  gap:8px;
  font-family: 'Montserrat', sans-serif;
  font-weight:600;
  letter-spacing:.2px;
  font-size:16px;
}
.badge{
  font-size:11px;
  color:var(--muted);
  padding:2px 7px;
  border:1px solid var(--border);
  border-radius:999px;
  background: rgba(2,6,23,.03);
}

.btn{
  border:1px solid var(--border);
  background: var(--panel);
  color:var(--text);
  padding:8px 10px;
  border-radius: 12px;
  cursor:pointer;
  transition: transform .05s, background .2s, border-color .2s;
  font-size:12px;
  line-height: 1;
  white-space: nowrap;
}
.btn:hover{ background: rgba(2,6,23,.03); }
.btn:active{ transform: translateY(1px); }

.btn.primary{
  color: #fff;
  background: linear-gradient(135deg, rgba(37,99,235,.98), rgba(59,130,246,.88));
  border-color: rgba(37,99,235,.35);
}
.btn.primary:hover{
  background: linear-gradient(135deg, rgba(37,99,235,1), rgba(59,130,246,.95));
}
.btn.danger{
  color: #fff;
  background: linear-gradient(135deg, rgba(220,38,38,.95), rgba(239,68,68,.85));
  border-color: rgba(220,38,38,.35);
}

/* compact toolbar */
.toolbar{
  display:flex;
  flex-wrap: nowrap;
  gap:8px;
  padding: 0 12px 10px 12px;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-width: thin;
}
.toolbar::-webkit-scrollbar{ height: 8px; }
.toolbar::-webkit-scrollbar-thumb{ background: rgba(2,6,23,.18); border-radius: 999px; }
.toolbar::-webkit-scrollbar-track{ background: rgba(2,6,23,.04); }

.control{
  display:flex;
  flex-direction:column;
  gap:4px;
  padding:7px 9px;
  background: var(--panel);
  border:1px solid var(--border);
  border-radius: 12px;
  flex: 0 0 auto;
}
.control label{
  font-size:10.5px;
  color:var(--muted);
  letter-spacing:.2px;
}
.control select, .control input{
  background: #fff;
  border:1px solid rgba(2,6,23,.14);
  color:var(--text);
  border-radius: 10px;
  padding:6px 8px;
  outline:none;
  font-size:12px;
}
.control select:disabled, .control input:disabled{opacity:.6}

/* widths tuned */
.w-session{ width: 170px; }
.w-mach{ width: 150px; }
.w-scep{ width: 190px; }
.w-run{ width: 220px; }
.w-dt{ width: 165px; }

.actions{
  display:flex;
  gap:8px;
  align-items:stretch;
  flex: 0 0 auto;
}
.toolbarLoadOverlay{
  position:absolute;
  inset:0;
  z-index:40;
  display:flex;
  align-items:center;
  justify-content:center;
  padding:14px;
  background:rgba(241,245,249,.62);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  pointer-events:auto;
}
.toolbarLoadOverlay[hidden]{ display:none !important; }
.toolbarLoadOverlayCard{
  display:inline-flex;
  align-items:center;
  gap:10px;
  min-height:44px;
  padding:10px 16px;
  border:1px solid rgba(37,99,235,.20);
  border-radius:14px;
  background:rgba(255,255,255,.90);
  color:var(--accent);
  box-shadow:0 14px 28px rgba(15,23,42,.10);
  white-space:nowrap;
}
.toolbarLoadSpinner{
  width:15px;
  height:15px;
  flex:0 0 15px;
  border-radius:999px;
  border:2px solid rgba(37,99,235,.22);
  border-top-color:var(--accent);
  animation:toolbarSpin .8s linear infinite;
}
.toolbarLoadText{
  font-size:13px;
  font-weight:700;
}
header.headerLoading > :not(#toolbarLoadState){
  user-select:none;
}
#btnBuild[data-scope-loading="1"]{
  opacity:.72;
  cursor:progress;
}
@keyframes toolbarSpin{ to { transform: rotate(360deg); } }

/* second row */
.minirow{
  display:flex;
  flex-wrap: nowrap;
  align-items:center;
  gap:8px;
  padding: 0 12px 12px 12px;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-width: thin;
}
.minirow::-webkit-scrollbar{ height: 8px; }
.minirow::-webkit-scrollbar-thumb{ background: rgba(2,6,23,.18); border-radius: 999px; }
.minirow::-webkit-scrollbar-track{ background: rgba(2,6,23,.04); }

.minirow .chip{
  flex: 0 0 auto;
  display:flex;
  align-items:center;
  gap:8px;
  padding:6px 10px;
  min-height: 34px;
  border:1px solid var(--border);
  border-radius:999px;
  background: var(--panel);
}
.minirow .chip label{font-size:11px;color:var(--muted);display:flex;align-items:center;gap:8px;}
.minirow select{
  background: #fff;
  border:1px solid rgba(2,6,23,.14);
  color:var(--text);
  border-radius: 10px;
  padding:5px 7px;
  font-size:12px;
}

.zoomGroup{
  margin-left:auto;
  display:flex;
  flex: 0 0 auto;
  gap:8px;
  align-items:center;
}

.iconBtn{
  width:34px;
  height:34px;
  padding:0;
  border-radius:999px;
  display:inline-flex;
  align-items:center;
  justify-content:center;
}
.iconBtn svg{ width:18px; height:18px; opacity:.95; }

/* floating zoom always visible */
.floatingZoom{
  position: fixed;
  right: 14px;
  bottom: 14px;
  z-index: 900;
  display:flex;
  gap:10px;
  align-items:center;
  padding: 10px 10px;
  border-radius: 999px;
  background: rgba(255,255,255,.92);
  border: 1px solid var(--border);
  box-shadow: 0 14px 32px rgba(2,6,23,.14);
  backdrop-filter: blur(10px);
}
.floatingZoom .btn{ background:#fff; }
.floatingZoom .iconBtn{ width:40px; height:40px; }
.floatingZoom .iconBtn svg{ width:20px; height:20px; }
.floatingZoom .mid{
  display:flex;
  align-items:center;
  gap:8px;
  padding: 0 6px;
  color: rgba(2,6,23,.72);
  font-size:12px;
  user-select:none;
}
.floatingZoom .sep{
  width:1px; height:18px;
  background: rgba(2,6,23,.18);
}

/* main / canvas */
main{ padding: 10px 12px 18px 12px; }
canvas{
  display:block;
  width: calc(100vw - 24px);
  margin: 0 auto;
  border-radius: var(--radius);
  border:1px solid var(--canvas-border);
  background: var(--canvas-bg);
  box-shadow: var(--shadow);
  /* важно для мобильных: горизонтальный "drag" по canvas, но вертикальный скролл страницы сохраняем */
  touch-action: pan-y;
}

/* overlay */
.overlay{
  position: fixed; inset:0;
  display:none;
  align-items:center;
  justify-content:center;
  background: var(--overlay);
  z-index: 1000;
}
.overlay.show{ display:flex; }

.modal{
  width:min(900px, 92vw);
  max-height: 88vh;
  overflow:auto;
  background: var(--panel);
  border:1px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 18px 40px rgba(0,0,0,.20);
  padding: 16px;
}
.modal h2{
  margin:0 0 10px 0;
  font-size:16px;
  color: var(--text);
}
.modal .top{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  margin-bottom: 10px;
}
.modal .close{
  border:1px solid var(--border);
  background: #fff;
  color: var(--text);
  padding:8px 10px;
  border-radius: 12px;
  cursor:pointer;
  font-size:12px;
}
.grid{
  display:grid;
  grid-template-columns: 1fr 1fr;
  gap:10px;
}
@media (max-width: 780px){
  .grid{ grid-template-columns: 1fr; }
}

.list{
  border:1px solid var(--border);
  background: var(--panel2);
  border-radius: 14px;
  padding: 10px;
}
.search{
  width:100%;
  padding:10px 12px;
  border-radius: 12px;
  border:1px solid rgba(2,6,23,.14);
  background: #fff;
  color: var(--text);
  outline:none;
  margin-bottom: 10px;
}
.param{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  padding:8px 10px;
  border-radius: 12px;
}
.param:hover{ background: rgba(2,6,23,.04); }
.param small{ color: var(--muted); }
.param input{ transform: scale(1.1); accent-color: var(--accent); }

#loadingOverlay .modal{ width:min(420px, 92vw); text-align:center; }
.loadingTrack{ display:flex; justify-content:center; gap:8px; margin:14px 0 8px; }
.metroCar{ width:30px; height:20px; border-radius: 6px; background: var(--accent); animation: metroMove 1s infinite linear; opacity:.9; }
.metroCar:nth-child(2){ animation-delay:.18s; }
.metroCar:nth-child(3){ animation-delay:.36s; }
@keyframes metroMove{ 0%{ transform: translateX(0); opacity:.55;} 50%{ transform: translateX(18px); opacity:1;} 100%{ transform: translateX(36px); opacity:.55;} }

.loginbox{
  display:flex; flex-direction:column; gap:10px;
  width:min(420px, 92vw);
}
.loginbox input{
  padding:10px 12px;
  border-radius: 12px;
  border:1px solid rgba(2,6,23,.14);
  background: #fff;
  color:var(--text);
  outline:none;
}
.loginbox .hint{ color:var(--muted); font-size:12px; }
.error{
  color:#7f1d1d;
  background: rgba(220,38,38,.10);
  border:1px solid rgba(220,38,38,.25);
  padding:8px 10px;
  border-radius: 12px;
  display:none;
}
.error.show{display:block;}

/* ===== Tabs inside modal ===== */
.tabs{
  display:flex;
  gap:8px;
  padding: 6px;
  background: var(--panel2);
  border:1px solid var(--border);
  border-radius: 14px;
  margin-bottom: 10px;
}
.tabBtn{
  border:1px solid transparent;
  background: transparent;
  color: var(--muted);
  padding:8px 10px;
  border-radius: 12px;
  cursor:pointer;
  font-size:12px;
}
.tabBtn.active{
  background: #fff;
  border-color: rgba(2,6,23,.12);
  color: var(--text);
}
.tabPanel{ display:none; }
.tabPanel.show{ display:block; }

/* ===== Order UI ===== */
.orderHint{
  color: var(--muted);
  font-size:12px;
  margin: 8px 0 12px 0;
}
.orderGrid{
  display:grid;
  grid-template-columns: 1fr 1fr;
  gap:10px;
}
@media (max-width: 900px){
  .orderGrid{ grid-template-columns: 1fr; }
}
.orderColTitle{
  margin:0 0 8px 0;
  font-size:14px;
  color: var(--muted);
}
.orderList{
  border:1px solid var(--border);
  background: var(--panel2);
  border-radius: 14px;
  padding: 10px;
  min-height: 120px;
}
.orderItem{
  display:flex;
  align-items:center;
  gap:8px;
  padding:8px 10px;
  background:#fff;
  border:1px solid rgba(2,6,23,.10);
  border-radius: 12px;
  margin-bottom: 8px;
}
.orderItem:last-child{ margin-bottom: 0; }
.orderItem.dragging{ opacity: .6; }
.orderHandle{
  cursor: grab;
  color: rgba(2,6,23,.50);
  font-weight:700;
  user-select:none;
}
.orderName{ flex:1; font-size:13px; }
.orderBtns{
  display:flex;
  gap:6px;
}
.smallBtn{
  width:28px;
  height:28px;
  border-radius: 10px;
  border:1px solid rgba(2,6,23,.12);
  background: #fff;
  cursor:pointer;
  line-height: 1;
}
.smallBtn:hover{ background: rgba(2,6,23,.03); }
.smallBtn:active{ transform: translateY(1px); }
.orderFooter{
  display:flex;
  gap:10px;
  flex-wrap: wrap;
  margin-top: 12px;
  align-items:center;
}
.status{
  color: var(--muted);
  font-size:12px;
}
.status.ok{ color: #166534; }
.status.bad{ color: #7f1d1d; }

/* ===== Rules UI ===== */
.rulesGrid{
  display:grid;
  grid-template-columns: 290px 1fr;
  gap:10px;
}
@media (max-width: 900px){
  .rulesGrid{ grid-template-columns: 1fr; }
}
.rulesListBox{
  border:1px solid var(--border);
  background: var(--panel2);
  border-radius: 14px;
  padding: 10px;
}
.ruleRow{
  display:flex;
  align-items:center;
  gap:8px;
  padding:8px 10px;
  border-radius: 12px;
  cursor:pointer;
  border:1px solid transparent;
  background: rgba(255,255,255,.55);
  margin-bottom: 8px;
}
.ruleRow:last-child{ margin-bottom: 0; }
.ruleRow:hover{ background: rgba(255,255,255,.8); }
.ruleRow.active{
  background:#fff;
  border-color: rgba(2,6,23,.12);
}
.ruleDot{
  width:10px; height:10px; border-radius:999px; border:1px solid rgba(2,6,23,.18);
  flex: 0 0 auto;
}
.ruleMeta{
  display:flex;
  flex-direction:column;
  gap:2px;
  min-width:0;
}
.ruleMeta b{
  font-size:12.5px;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.ruleMeta small{
  color: var(--muted);
  font-size:11px;
  white-space:nowrap;
  overflow:hidden;
  text-overflow:ellipsis;
}
.rulePill{
  margin-left:auto;
  font-size:10px;
  color: var(--muted);
  border:1px solid rgba(2,6,23,.12);
  background: rgba(2,6,23,.03);
  padding:2px 8px;
  border-radius:999px;
  flex: 0 0 auto;
}
.rulesEditor{
  border:1px solid var(--border);
  background: var(--panel2);
  border-radius: 14px;
  padding: 10px;
}
.rulesForm{
  display:grid;
  grid-template-columns: 1fr 1fr;
  gap:10px;
}
@media (max-width: 900px){
  .rulesForm{ grid-template-columns: 1fr; }
}
.frow{
  display:flex;
  flex-direction:column;
  gap:4px;
}
.frow label{
  font-size:11px;
  color: var(--muted);
}
.frow input, .frow select{
  background:#fff;
  border:1px solid rgba(2,6,23,.14);
  color: var(--text);
  border-radius: 10px;
  padding:8px 10px;
  outline:none;
  font-size:12px;
}
.frow .rowInline{
  display:flex; gap:8px; align-items:center;
}
.frow .rowInline input[type="checkbox"]{ transform: scale(1.1); accent-color: var(--accent); }

.exprWrap{
  margin-top:10px;
  background:#fff;
  border:1px solid rgba(2,6,23,.12);
  border-radius: 14px;
  padding: 10px;
}
.exprTitle{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  margin-bottom: 8px;
}
.exprTitle b{ font-size:13px; }
.exprGroup{
  border:1px dashed rgba(2,6,23,.22);
  border-radius: 12px;
  padding: 10px;
  margin-bottom: 10px;
  background: rgba(248,250,252,.6);
}
.exprGroup:last-child{ margin-bottom: 0; }
.exprGroupHeader{
  display:flex;
  align-items:center;
  gap:8px;
  flex-wrap:wrap;
  margin-bottom: 8px;
}
.exprGroupHeader .pill{
  font-size:11px;
  color: var(--muted);
  border:1px solid rgba(2,6,23,.12);
  background:#fff;
  padding:3px 8px;
  border-radius:999px;
}
.exprItems{
  display:flex;
  flex-direction:column;
  gap:8px;
}
.exprItem{
  background:#fff;
  border:1px solid rgba(2,6,23,.10);
  border-radius: 12px;
  padding: 8px;
}
.exprRow{
  display:flex;
  gap:6px;
  flex-wrap:wrap;
  align-items:center;
}
.exprRow select, .exprRow input{
  background:#fff;
  border:1px solid rgba(2,6,23,.14);
  border-radius:10px;
  padding:6px 8px;
  font-size:12px;
  outline:none;
}
.exprRow input[type="number"]{ width:120px; }
.exprRow .tiny{
  font-size:11px;
  color: var(--muted);
  padding: 0 4px;
}
.exprOps{
  display:flex;
  flex-direction:column;
  gap:6px;
  margin-top: 6px;
  padding-left: 14px;
}
.exprOpRow{
  display:flex;
  gap:6px;
  flex-wrap:wrap;
  align-items:center;
}

.uiTogglesList{
  margin-bottom: 10px;
}

.messagesSection{
  margin: 14px auto 0;
  width: calc(100vw - 24px);
}

.messagesCard{
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.messagesHeader{
  display:flex;
  flex-direction:column;
  gap:8px;
  padding:16px 18px 12px;
  border-bottom: 1px solid rgba(2,6,23,.08);
  background: linear-gradient(to bottom, rgba(255,255,255,.96), rgba(248,250,252,.96));
}
.messagesHeaderTop{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:12px;
  flex-wrap:wrap;
}
.messagesTitleRow{
  display:flex;
  align-items:center;
  gap:10px;
  min-width:0;
}
.messagesHeader h2{
  margin:0;
  font-size:16px;
}
.messagesHeader p{
  margin:0;
  font-size:12px;
  color:var(--muted);
}
.messagesCount{
  min-width:44px;
  height:28px;
  padding:0 10px;
  border-radius:999px;
  display:inline-flex;
  align-items:center;
  justify-content:center;
  background: linear-gradient(180deg, rgba(37,99,235,.12), rgba(37,99,235,.08));
  color: var(--accent);
  border:1px solid rgba(37,99,235,.16);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.45);
  font-size:12px;
  font-weight:800;
  font-variant-numeric: tabular-nums;
}

.messagesTableWrap{
  overflow:auto;
}

.messagesToolbar{
  display:flex;
  align-items:center;
  gap:10px;
  flex-wrap:wrap;
  justify-content:flex-end;
}
.messagesPageSizeLabel{
  display:inline-flex;
  align-items:center;
  gap:8px;
  font-size:12px;
  color:var(--muted);
}
.messagesPageSize{
  height:32px;
  border:1px solid var(--border);
  border-radius:10px;
  background:#fff;
  padding:0 10px;
  font:inherit;
  color:var(--text);
}
.messagesPager{
  display:flex;
  align-items:center;
  gap:8px;
}
.messagesPager .btn{
  min-width:36px;
  height:32px;
  padding:0 10px;
}
.messagesPager .btn:disabled{
  opacity:.45;
  cursor:default;
}
.messagesPageInfo{
  min-width:76px;
  text-align:center;
  font-size:12px;
  font-weight:700;
  color:var(--muted);
  font-variant-numeric: tabular-nums;
}

.messagesTable{
  width:100%;
  border-collapse:separate;
  border-spacing:0;
  min-width:860px;
}

.messagesTable thead th{
  position:sticky;
  top:0;
  z-index:1;
  background: #f8fafc;
  color: var(--muted);
  font-size:12px;
  font-weight:700;
  text-align:left;
  padding:12px 14px;
  border-bottom:1px solid rgba(2,6,23,.10);
  cursor:pointer;
  user-select:none;
  white-space:nowrap;
}
.messagesTable thead th.active{
  color: var(--text);
}
.messagesTable thead th .sortMark{
  display:inline-block;
  min-width:12px;
  margin-left:6px;
  color: rgba(2,6,23,.42);
}
.messagesTable thead th.active .sortMark{
  color: var(--accent);
}

.messagesTable tbody td{
  padding:12px 14px;
  border-bottom:1px solid rgba(2,6,23,.08);
  vertical-align:top;
  font-size:13px;
}
.messagesTable tbody tr:hover{
  background: rgba(37,99,235,.035);
}
.messagesTable tbody tr.state-open{
  background-image: linear-gradient(to right, rgba(220,38,38,.02), transparent 24%);
}
.messagesTable tbody tr.state-solved{
  background-image: linear-gradient(to right, rgba(34,197,94,.02), transparent 24%);
}

.msgCatBadge{
  min-width:28px;
  height:28px;
  padding:0 9px;
  border-radius:999px;
  display:inline-flex;
  align-items:center;
  justify-content:center;
  font-size:12px;
  font-weight:700;
  border:1px solid rgba(2,6,23,.12);
  background:#fff;
}
.cat-А .msgCatBadge{ color:#b91c1c; background: rgba(239,68,68,.10); border-color: rgba(239,68,68,.16); }
.cat-Б .msgCatBadge{ color:#92400e; background: rgba(245,158,11,.12); border-color: rgba(245,158,11,.18); }
.cat-В .msgCatBadge{ color:#1d4ed8; background: rgba(59,130,246,.10); border-color: rgba(59,130,246,.16); }

.msgState{
  display:inline-flex;
  align-items:center;
  justify-content:center;
  min-height:28px;
  padding:0 10px;
  border-radius:999px;
  font-size:12px;
  font-weight:700;
  border:1px solid rgba(2,6,23,.12);
  white-space:nowrap;
}
.msgState.open{
  color:#b91c1c;
  background: rgba(239,68,68,.10);
  border-color: rgba(239,68,68,.16);
}
.msgState.solved{
  color:#166534;
  background: rgba(34,197,94,.12);
  border-color: rgba(34,197,94,.18);
}

.msgTextMain{
  color: var(--text);
  font-weight:600;
  line-height:1.35;
}
.msgTextMeta{
  margin-top:4px;
  color: var(--muted);
  font-size:12px;
}
.monoCell{
  font-variant-numeric: tabular-nums;
  white-space:nowrap;
}

@media (max-width: 780px){
  .messagesHeaderTop{
    align-items:flex-start;
  }
  .messagesToolbar{
    justify-content:flex-start;
  }
}

.messagesTable tbody tr{
  transition: background-color .18s ease, box-shadow .18s ease, transform .05s ease;
}
.messagesTable tbody tr.msgRow{
  cursor:pointer;
}
.messagesTable tbody tr.msgRow:active{
  transform: translateY(1px);
}
.messagesTable tbody tr.msgRow.active{
  background: rgba(37,99,235,.08);
  box-shadow: inset 3px 0 0 rgba(37,99,235,.75);
}
