:root {
  --bg: #0f1419; --card: #182028; --line: #263039; --fg: #e6edf3;
  --muted: #8b98a5; --accent: #2ea3f2; --ok: #3fb950; --warn: #d29922; --bad: #f85149;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: 14px/1.5 -apple-system, "Segoe UI", Roboto, sans-serif;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
.err { color: var(--bad); margin-top: 8px; min-height: 18px; }
.grow { flex: 1; }

header {
  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; flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 18px; color: var(--accent); }
.stats { color: var(--muted); font-size: 13px; }
.tabs { display: flex; gap: 4px; margin-left: auto; }
.tabs button {
  background: transparent; border: 1px solid transparent; color: var(--muted);
  padding: 7px 14px; border-radius: 8px; cursor: pointer; font-size: 14px;
}
.tabs button:hover { color: var(--fg); }
.tabs button.active { background: var(--bg); color: var(--fg); border-color: var(--line); }

main { padding: 20px; max-width: 1400px; margin: 0 auto; }

button {
  background: #223040; color: var(--fg); border: 1px solid var(--line);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 13px;
}
button:hover { background: #2b3c4f; }
button.primary { background: var(--accent); border-color: var(--accent); color: #06121c; font-weight: 600; }
button.primary:hover { filter: brightness(1.12); }
button.danger { background: #3a1d1d; border-color: #5c2b2b; color: #ff9a92; }
button.ghost { background: transparent; color: var(--muted); }
button:disabled { opacity: .5; cursor: not-allowed; }

input, textarea, select {
  background: #0d1319; color: var(--fg); border: 1px solid var(--line);
  padding: 8px 10px; border-radius: 8px; font: inherit; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); }
textarea { resize: vertical; font-family: ui-monospace, monospace; font-size: 13px; }

.bar { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.bar select, .bar input { width: auto; min-width: 160px; }
.row { display: flex; gap: 10px; align-items: center; margin: 10px 0; flex-wrap: wrap; }
.row select, .row input { width: auto; }
.row label { display: flex; gap: 6px; align-items: center; color: var(--muted); font-size: 13px; }
.chk { display: flex; gap: 6px; align-items: center; cursor: pointer; white-space: nowrap; }
.chk input, .grid input[type=checkbox] { width: auto; }

.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 12px; padding: 16px; margin-bottom: 16px;
}
.card h3 { margin: 0 0 8px; font-size: 15px; }
.card textarea, .card input:not([type=checkbox]) { margin-bottom: 8px; }

.notice {
  background: #1c2a17; border: 1px solid #2d4423; color: #a5d68a;
  padding: 10px 14px; border-radius: 8px; margin-bottom: 14px;
}

.grid { width: 100%; border-collapse: collapse; background: var(--card); border-radius: 12px; overflow: hidden; }
.grid th, .grid td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--line); }
.grid th { background: #131b22; color: var(--muted); font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: .4px; }
.grid tr:last-child td { border-bottom: none; }
.grid tr:hover td { background: #1c252e; }
.grid td select, .grid td input { width: 100%; min-width: 120px; }

.pill { padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.pill.running { background: #12301c; color: var(--ok); }
.pill.stopped { background: #2a2a2a; color: var(--muted); }
.pill.invalid, .pill.error { background: #3a1d1d; color: var(--bad); }
.pill.new { background: #2d2611; color: var(--warn); }

.login { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.login-card { width: 340px; text-align: center; }
.login-card h1 { color: var(--accent); margin: 0 0 4px; }
.login-card input { margin: 16px 0 10px; }
.login-card button { width: 100%; }

.gcard { background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 16px; margin-bottom: 14px; }
.gcard h3 { margin: 0 0 12px; display: flex; align-items: center; gap: 10px; }
.gcard .fields { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.gcard label { display: block; color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.chkgroup { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
.chkgroup .chk {
  background: #0d1319; border: 1px solid var(--line);
  padding: 9px 14px; border-radius: 8px; min-height: 44px;
}
.chkgroup .chk:has(input:checked) { border-color: var(--accent); color: var(--accent); }

.howto {
  background: #14202b; border: 1px solid var(--line); border-left: 3px solid var(--accent);
  border-radius: 12px; padding: 14px 16px; margin-bottom: 16px;
}
.howto p { margin: 6px 0 0; }

.step h3 { display: flex; align-items: center; gap: 10px; font-size: 16px; margin-bottom: 6px; }
.step .num {
  display: inline-flex; align-items: center; justify-content: center;
  width: 26px; height: 26px; border-radius: 50%; flex: 0 0 26px;
  background: var(--accent); color: #06121c; font-size: 14px; font-weight: 700;
}
.step p.muted { margin: 0 0 10px; }

.modes { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.mode {
  display: flex; align-items: flex-start; gap: 12px; cursor: pointer;
  background: #0d1319; border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px;
}
.mode:has(input:checked) { border-color: var(--accent); background: #101d29; }
.mode input { width: auto; margin-top: 2px; flex: 0 0 auto; }
.mode span { display: flex; flex-direction: column; gap: 2px; }
.mode i { color: var(--muted); font-style: normal; font-size: 13px; }

.rule {
  display: flex; align-items: center; gap: 12px;
  background: #0d1319; border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 14px; margin-bottom: 8px;
}
.ruleadd { display: flex; gap: 8px; flex-wrap: wrap; }
.ruleadd input { flex: 1 1 200px; width: auto; }

.savebar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 4px 0 8px; margin-bottom: 8px;
}
button.big { font-size: 15px; padding: 12px 22px; }

.pickgroup { display: flex; align-items: center; gap: 10px; color: var(--muted); }
.pickgroup select { min-width: 220px; font-weight: 600; color: var(--fg); }

.castcard b { display: block; margin-bottom: 4px; }
.castcard button { margin-top: 10px; }

.fl { display: block; margin-bottom: 12px; color: var(--muted); font-size: 13px; }
.fl i { font-style: normal; }
.fl input, .fl textarea { margin-top: 5px; color: var(--fg); font-size: 14px; }

h4 { margin: 18px 0 8px; font-size: 14px; }
.btnedit { display: flex; flex-direction: column; gap: 8px; }
.btwrap { display: flex; flex-direction: column; gap: 4px; }
.btrow.bad { border-color: #7a3a1f; }
.bthint { color: #e0a06a; font-size: 12px; padding-left: 12px; }
.btrow {
  display: flex; align-items: center; gap: 8px;
  background: #0d1319; border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 10px;
}
.btrow input, .btrow select { width: auto; margin: 0; }
.bt-text { flex: 1 1 160px; }
.bt-kind { flex: 0 0 190px; }
.bt-url, .bt-step { flex: 1 1 200px; min-width: 0; }
.btrow button { flex: 0 0 auto; padding: 8px 12px; }
.addbtn { align-self: flex-start; }

.botname { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.botname input { width: auto; }
.bulkhint { display: none; }

@media (max-width: 860px) {
  main { padding: 12px 12px 16px; }

  /* Крупные поля: меньше 16px — и iOS зумит страницу при фокусе */
  input, textarea, select, button { font-size: 16px; }
  button { padding: 11px 14px; min-height: 44px; }
  .grid td button { min-height: 40px; }

  header { padding: 10px 12px; gap: 10px; }
  .brand { font-size: 16px; }
  .stats { font-size: 12px; width: 100%; order: 3; }
  .tabs {
    width: 100%; margin-left: 0; order: 4;
    overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 2px;
  }
  .tabs::-webkit-scrollbar { display: none; }
  .tabs button { flex: 1 0 auto; padding: 10px 16px; min-height: 44px; }
  #logoutBtn { margin-left: auto; min-height: 40px; padding: 8px 12px; }

  .bar select, .bar input { width: 100%; min-width: 0; }
  .row { gap: 8px; }
  .row select, .row input { width: 100%; }
  .row label { width: 100%; justify-content: space-between; }
  .chkgroup .chk { width: auto; justify-content: flex-start; gap: 10px; }
  #castMsg { display: block; width: 100%; margin-top: 8px; }
  #castSend { width: 100%; }

  /* Таблица не влезает в узкий экран — разворачиваем каждую строку в карточку */
  .grid { background: transparent; }
  .grid thead { display: none; }
  .grid, .grid tbody, .grid tr, .grid td { display: block; width: 100%; }
  .grid tr {
    background: var(--card); border: 1px solid var(--line);
    border-radius: 12px; margin-bottom: 12px; padding: 12px 14px;
  }
  .grid tr:hover td { background: transparent; }
  .grid td {
    border: none; padding: 7px 0;
    display: flex; align-items: center; justify-content: space-between; gap: 14px;
  }
  .grid td::before {
    content: attr(data-label); color: var(--muted);
    font-size: 12px; text-transform: uppercase; letter-spacing: .4px; flex: 0 0 auto;
  }
  .grid td[data-label=""]::before { display: none; }
  .grid td select, .grid td input { min-width: 0; max-width: 60%; }
  .grid td:first-child { border-bottom: 1px solid var(--line); margin-bottom: 4px; padding-bottom: 10px; }
  .grid td:last-child { justify-content: flex-end; }

  /* Массовые действия уезжают вниз экрана и всплывают, когда что-то выбрано */
  .bulkhint { display: block; width: 100%; font-size: 12px; margin-bottom: 2px; }
  .bulkbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    margin: 0; padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
    background: var(--card); border-top: 1px solid var(--line);
    box-shadow: 0 -8px 24px rgba(0,0,0,.45);
    display: none; gap: 6px;
  }
  body.has-selection .bulkbar { display: flex; }
  body.has-selection main { padding-bottom: 230px; }
  .bulkbar button { flex: 1 1 calc(50% - 3px); padding: 10px 8px; }
  .bulkbar select { flex: 1 1 58%; }
  #bulkAssign { flex: 1 1 38%; }
  #bulkNames { flex: 1 1 100%; }

  .pickgroup { width: 100%; flex-direction: column; align-items: stretch; gap: 6px; }
  .pickgroup select { width: 100%; min-width: 0; }
  #newGroupBtn { width: 100%; }
  .savebar button.big { width: 100%; }
  .savebar { flex-direction: column; align-items: stretch; }
  .ruleadd input { flex: 1 1 100%; }
  .btrow { flex-wrap: wrap; }
  .btrow input, .btrow select { flex: 1 1 100%; }
  .btrow button { position: absolute; right: 14px; margin-top: -4px; }
  .btrow { position: relative; padding-right: 46px; }
  #addStep { width: 100%; }
  .notice { display: flex; flex-direction: column; gap: 10px; align-items: stretch; }

  .topbar { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 8px 0; }
  .gcard .fields { grid-template-columns: 1fr; }
  .card { padding: 14px; }
}

@media (max-width: 380px) {
  .bulkbar button { flex: 1 1 100%; }
}
