:root {
  --bg: #f3faf6;
  --panel: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #d9eadf;
  --green: #059669;
  --green-dark: #047857;
  --green-soft: #ecfdf5;
  --red: #dc2626;
  --red-soft: #fef2f2;
  --yellow: #b45309;
  --yellow-soft: #fffbeb;
  --blue-soft: #eff6ff;
  --shadow: 0 16px 40px rgba(15, 23, 42, 0.08);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-family: Arial, Helvetica, sans-serif; font-size: 14px; }
a { color: inherit; }
.d-shell { min-height: 100vh; display: grid; grid-template-columns: 260px 1fr; }
.d-sidebar { background: #ffffff; border-right: 1px solid var(--border); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; z-index: 10; }
.d-sidebar-head { display: flex; gap: 10px; align-items: center; padding: 18px 14px; border-bottom: 1px solid var(--border); }
.d-brand-icon { width: 38px; height: 38px; border-radius: 14px; background: var(--green); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 900; }
.d-brand-title { font-weight: 900; font-size: 16px; }
.d-brand-subtitle { color: var(--muted); font-size: 12px; margin-top: 2px; }
.d-nav { padding: 12px 10px; display: grid; gap: 6px; }
.d-sidebar-foot { margin-top: auto; padding: 12px 10px; border-top: 1px solid var(--border); }
.d-nav-item { position: relative; display: flex; align-items: center; gap: 10px; min-height: 42px; padding: 10px 12px; border-radius: 14px; text-decoration: none; color: #1f2937; font-weight: 800; }
.d-nav-item:hover, .d-nav-item.active { background: var(--green-soft); color: var(--green-dark); outline: 1px solid #bbf7d0; }
.d-logout { color: var(--red); }
.d-nav-icon { width: 22px; text-align: center; font-weight: 900; }
.d-nav-badge { display: none; position: absolute; right: 5px; top: 4px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px; background: var(--red); color: #fff; border: 2px solid #fff; font-size: 10px; font-weight: 900; align-items: center; justify-content: center; }
.d-nav-badge.is-visible { display: inline-flex; }
.d-main { min-width: 0; }
.d-topbar { height: 70px; display: flex; align-items: center; gap: 14px; padding: 0 22px; border-bottom: 1px solid var(--border); background: rgba(255,255,255,0.78); backdrop-filter: blur(10px); position: sticky; top: 0; z-index: 8; }
.d-burger { width: 40px; height: 40px; border-radius: 12px; border: 1px solid #a7f3d0; background: #fff; color: var(--green-dark); font-weight: 900; font-size: 18px; cursor: pointer; }
.d-topbar-title { font-size: 22px; font-weight: 900; line-height: 1.1; }
.d-topbar-subtitle { color: var(--muted); margin-top: 3px; }
.d-content { padding: 20px; }
body.d-sidebar-collapsed .d-shell { grid-template-columns: 76px 1fr; }
body.d-sidebar-collapsed .d-brand-text, body.d-sidebar-collapsed .d-nav-label { display: none; }
body.d-sidebar-collapsed .d-sidebar-head { justify-content: center; }
body.d-sidebar-collapsed .d-nav-item { justify-content: center; padding-left: 8px; padding-right: 8px; }
body.d-sidebar-collapsed .d-nav-badge { right: 2px; top: 2px; }
.card { background: transparent; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 18px; padding: 16px; box-shadow: var(--shadow); }
.panel-head-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 14px; }
.panel-title { font-size: 20px; font-weight: 900; line-height: 1.2; }
.muted { color: var(--muted); }
.stack { display: grid; }
.gap-12 { gap: 12px; }
.gap-16 { gap: 16px; }
.mt-10 { margin-top: 10px; }
.mt-14 { margin-top: 14px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid-2.compact { gap: 10px; }
.kpi-grid { display: grid; grid-template-columns: repeat(6, minmax(130px, 1fr)); gap: 10px; }
.kpi { border: 1px solid var(--border); border-radius: 16px; padding: 13px; background: #fff; }
.kpi-label { color: var(--muted); font-size: 12px; margin-bottom: 6px; font-weight: 700; }
.kpi-value { font-size: 28px; font-weight: 900; color: var(--green-dark); line-height: 1; }
.kpi-note { color: var(--muted); font-size: 12px; margin-top: 7px; }
.attention-grid { display: grid; grid-template-columns: repeat(4, minmax(160px, 1fr)); gap: 10px; }
.attention-card { text-decoration: none; border: 1px solid var(--border); border-radius: 16px; padding: 14px; background: #fff; display: block; }
.attention-card.good { background: var(--green-soft); border-color: #bbf7d0; }
.attention-card.neutral { background: #fff; }
.attention-card.danger { background: var(--red-soft); border-color: #fecaca; }
.attention-title { font-weight: 900; }
.attention-value { font-size: 30px; font-weight: 900; line-height: 1; margin-top: 8px; color: var(--green-dark); }
.attention-card.danger .attention-value { color: var(--red); }
.attention-note { color: var(--muted); font-size: 12px; margin-top: 8px; line-height: 1.35; }
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; min-width: 860px; }
.table th, .table td { padding: 10px 9px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.table th { color: var(--muted); font-size: 12px; font-weight: 900; background: #f8fffb; }
.actions { text-align: right !important; white-space: nowrap; }
.btn { display: inline-flex; align-items: center; justify-content: center; min-height: 38px; padding: 9px 14px; border-radius: 12px; border: 1px solid var(--green); background: var(--green); color: #fff; font-weight: 900; text-decoration: none; cursor: pointer; }
.btn:hover { background: var(--green-dark); border-color: var(--green-dark); }
.btn-small { min-height: 32px; padding: 6px 10px; font-size: 12px; }
.btn-danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn-danger:hover { background: #b91c1c; border-color: #b91c1c; }
.input { width: 100%; border: 1px solid var(--border); border-radius: 12px; background: #fff; color: var(--text); padding: 10px 12px; font: inherit; outline: none; }
.input:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(5,150,105,0.12); }
.textarea { resize: vertical; min-height: 110px; }
.label { display: block; color: #334155; font-weight: 900; margin-bottom: 6px; }
.check { display: flex; align-items: center; gap: 8px; font-weight: 800; }
.form-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.pill { display: inline-flex; align-items: center; border-radius: 999px; border: 1px solid var(--border); padding: 3px 8px; font-size: 12px; font-weight: 900; background: #fff; color: var(--muted); }
.pill.success, .pill.successful, .pill.success, .pill.active { color: var(--green-dark); background: var(--green-soft); border-color: #bbf7d0; }
.pill.warning, .pill.planned { color: var(--yellow); background: var(--yellow-soft); border-color: #fde68a; }
.pill.danger, .pill.error, .pill.failed { color: var(--red); background: var(--red-soft); border-color: #fecaca; }
.muted-pill { color: var(--muted); background: #f8fafc; }
.thumb { width: 58px; height: 58px; object-fit: cover; border-radius: 12px; border: 1px solid var(--border); background: #f8fafc; }
.clamp-2 { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; max-width: 420px; line-height: 1.35; }
.pagination { display: flex; align-items: center; gap: 8px; margin-top: 14px; }
.page-link, .page-current { border: 1px solid var(--border); border-radius: 12px; padding: 8px 11px; text-decoration: none; background: #fff; font-weight: 800; }
.page-link { color: var(--green-dark); }
.event-list { display: grid; gap: 8px; }
.event-item { position: relative; border: 1px solid var(--border); border-radius: 14px; padding: 10px 48px 10px 12px; background: #fff; }
.event-item.success { background: var(--green-soft); border-color: #bbf7d0; }
.event-item.warning { background: var(--yellow-soft); border-color: #fde68a; }
.event-item.danger { background: var(--red-soft); border-color: #fecaca; }
.event-top { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; color: var(--muted); font-size: 12px; font-weight: 900; margin-bottom: 5px; }
.event-type { display: inline-flex; border: 1px solid var(--border); border-radius: 999px; padding: 2px 7px; background: #fff; }
.event-title { font-size: 15px; font-weight: 900; margin-bottom: 4px; }
.event-description { font-size: 13px; line-height: 1.35; overflow-wrap: anywhere; color: #1f2937; }
.event-open { position: absolute; top: 9px; right: 10px; width: 28px; height: 28px; border-radius: 10px; background: var(--green); color: #fff; text-decoration: none; display: flex; align-items: center; justify-content: center; font-weight: 900; }
.diagnosis { font-weight: 900; }
.diagnosis.good { color: var(--green-dark); }
.diagnosis.warn { color: var(--yellow); }
.diagnosis.bad { color: var(--red); }
.settings-grid { display: grid; gap: 8px; }
.setting-row { display: grid; grid-template-columns: 210px 1fr; gap: 12px; border-bottom: 1px solid var(--border); padding: 10px 0; }
.setting-label { color: var(--muted); font-weight: 900; }
.popup { display: none; position: fixed; inset: 0; z-index: 50; }
.popup.is-open { display: block; }
.popup-backdrop { position: absolute; inset: 0; background: rgba(15,23,42,0.35); }
.popup-panel { position: relative; width: min(760px, calc(100vw - 24px)); max-height: calc(100vh - 40px); overflow-y: auto; margin: 20px auto; background: #fff; border: 1px solid var(--border); border-radius: 18px; padding: 16px; box-shadow: 0 20px 60px rgba(15,23,42,0.22); }
.popup-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 14px; }
.popup-title { font-size: 20px; font-weight: 900; }
.popup-x { width: 34px; height: 34px; border: 1px solid #fecaca; border-radius: 12px; background: var(--red-soft); color: var(--red); font-size: 22px; font-weight: 900; cursor: pointer; }
.alert { border-radius: 12px; padding: 10px 12px; font-weight: 800; }
.alert.danger { background: var(--red-soft); color: var(--red); border: 1px solid #fecaca; }
.login-body { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; background: linear-gradient(145deg, #ecfdf5, #ffffff); }
.login-card { width: min(420px, 100%); background: #fff; border: 1px solid var(--border); border-radius: 22px; box-shadow: var(--shadow); padding: 24px; }
.login-logo { width: 48px; height: 48px; border-radius: 16px; background: var(--green); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 22px; font-weight: 900; }
.login-card h1 { margin: 14px 0 6px; font-size: 24px; }
.login-card p { margin-top: 0; color: var(--muted); }
@media (max-width: 1400px) { .kpi-grid { grid-template-columns: repeat(3, minmax(130px, 1fr)); } .attention-grid { grid-template-columns: repeat(2, minmax(160px, 1fr)); } }
@media (max-width: 900px) { .d-shell { grid-template-columns: 1fr; } .d-sidebar { position: fixed; transform: translateX(-100%); transition: transform .15s ease; width: 260px; } body.d-sidebar-collapsed .d-shell { grid-template-columns: 1fr; } body:not(.d-sidebar-collapsed) .d-sidebar { transform: translateX(0); } .d-content { padding: 14px; } .grid-2, .kpi-grid, .attention-grid { grid-template-columns: 1fr; } .panel-head-row { flex-direction: column; } .setting-row { grid-template-columns: 1fr; } }
.btn-outline { background: #fff; color: var(--green-dark); border-color: #a7f3d0; }
.btn-outline:hover { background: var(--green-soft); color: var(--green-dark); border-color: var(--green); }
.inline-form { display: inline-flex; margin: 0; }
.actions-row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.alert.success { background: var(--green-soft); color: var(--green-dark); border: 1px solid #bbf7d0; }
.telegram-status-grid { display: grid; grid-template-columns: repeat(4, minmax(160px, 1fr)); gap: 10px; }
.telegram-status-card { border: 1px solid var(--border); border-radius: 14px; background: #fff; padding: 12px; }
.status-label { color: var(--muted); font-size: 12px; font-weight: 900; margin-bottom: 6px; }
.status-value { font-size: 15px; font-weight: 900; overflow-wrap: anywhere; }
.danger-zone { display: flex; justify-content: space-between; align-items: center; gap: 14px; border: 1px solid #fecaca; background: var(--red-soft); border-radius: 14px; padding: 12px; }
.danger-zone-title { font-weight: 900; color: var(--red); }
.mt-6 { margin-top: 6px; }
@media (max-width: 1200px) { .telegram-status-grid { grid-template-columns: repeat(2, minmax(160px, 1fr)); } }
@media (max-width: 700px) { .telegram-status-grid { grid-template-columns: 1fr; } .danger-zone { align-items: flex-start; flex-direction: column; } }

.telegram-popup-panel {
  max-width: 620px;
}

.qr-auth-box {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 18px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 16px;
  background: #f8fffb;
}

.qr-auth-img {
  width: 220px;
  height: 220px;
  border-radius: 16px;
  border: 1px solid #bbf7d0;
  background: #fff;
  padding: 10px;
}

.qr-auth-title {
  font-weight: 900;
  font-size: 16px;
  margin-bottom: 8px;
}

.qr-auth-help ol {
  margin: 0;
  padding-left: 18px;
  color: #334155;
  line-height: 1.5;
}

.qr-start-note {
  border: 1px solid #bbf7d0;
  border-radius: 16px;
  padding: 14px;
  background: #f0fdf4;
}

@media (max-width: 760px) {
  .qr-auth-box {
    grid-template-columns: 1fr;
  }

  .qr-auth-img {
    width: 100%;
    max-width: 240px;
    height: auto;
    margin: 0 auto;
  }
}
.pill.dry_run { background: #eef2ff; color: #3730a3; border-color: #c7d2fe; }
.pill.started { background: #fffbeb; color: var(--yellow); border-color: #fde68a; }
.pill.error { background: var(--red-soft); color: var(--red); border-color: #fecaca; }
.pill.success { background: var(--green-soft); color: var(--green-dark); border-color: #bbf7d0; }
.manual-send-note {
  border: 1px solid #bbf7d0;
  border-radius: 16px;
  padding: 14px;
  background: #f0fdf4;
}
.manual-send-title {
  font-weight: 900;
  margin-bottom: 8px;
}

/* patch_04: knowledge, photos, chat rules and matching */
.popup-wide { width: min(1040px, calc(100vw - 24px)); }
.photo-grid { display: grid; grid-template-columns: repeat(5, minmax(160px, 1fr)); gap: 12px; }
.photo-card { border: 1px solid var(--border); border-radius: 16px; overflow: hidden; background: #fff; box-shadow: var(--shadow); }
.photo-preview { width: 100%; aspect-ratio: 1 / 1; background: #f8fafc; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: center; }
.photo-preview img { width: 100%; height: 100%; object-fit: cover; display: block; }
.photo-body { padding: 12px; }
.photo-title { font-weight: 900; line-height: 1.25; }
.photo-edit-preview { width: 160px; height: 160px; object-fit: cover; border-radius: 16px; border: 1px solid var(--border); }
.matching-form { max-width: 760px; }
.rule-summary { display: grid; grid-template-columns: repeat(4, minmax(140px, 1fr)); gap: 8px; border: 1px solid var(--border); border-radius: 14px; background: #f8fffb; padding: 12px; }
.attention-grid.mini { grid-template-columns: repeat(2, minmax(140px, 1fr)); }
.match-score { display: inline-flex; align-items: center; justify-content: center; min-width: 46px; height: 34px; border-radius: 12px; font-weight: 900; border: 1px solid var(--border); background: #fff; }
.match-score.good { color: var(--green-dark); background: var(--green-soft); border-color: #bbf7d0; }
.match-score.warn { color: var(--yellow); background: var(--yellow-soft); border-color: #fde68a; }
.match-score.bad { color: var(--red); background: var(--red-soft); border-color: #fecaca; }
@media (max-width: 1400px) { .photo-grid { grid-template-columns: repeat(4, minmax(150px, 1fr)); } }
@media (max-width: 1000px) { .photo-grid { grid-template-columns: repeat(2, minmax(150px, 1fr)); } .rule-summary { grid-template-columns: 1fr 1fr; } }
@media (max-width: 640px) { .photo-grid { grid-template-columns: 1fr; } .rule-summary { grid-template-columns: 1fr; } }

/* patch_05: simplified chat rules input */
.helper-box {
  border: 1px solid #bbf7d0;
  border-radius: 16px;
  padding: 13px;
  background: #f0fdf4;
}
.helper-title {
  font-weight: 900;
  margin-bottom: 5px;
}
.advanced-box {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: #f8fffb;
  padding: 12px;
}
.advanced-box summary {
  cursor: pointer;
  color: var(--green-dark);
  font-weight: 900;
}
.textarea-tall {
  min-height: 230px;
}

/* patch_07: matching list first, detail on demand */
.selected-row td {
  background: #ecfdf5;
}
.clamp-3 {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* patch_08: compact matching table with icon actions */
.compact-head { align-items: center; }
.compact-table th,
.compact-table td {
  padding: 8px 9px;
  vertical-align: middle;
}
.icon-actions {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
}
.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  border: 1px solid #a7f3d0;
  background: #fff;
  color: var(--green-dark);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  line-height: 1;
}
.icon-btn:hover {
  background: var(--green-soft);
  border-color: var(--green);
}
.inline-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-bottom: 3px;
}
.inline-meta span {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 7px;
  color: var(--green-dark);
  background: #fff;
  font-size: 12px;
  font-weight: 900;
}
.clamp-1 {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 260px;
}
.popup-close-link {
  text-decoration: none;
}
.compact-settings .setting-row {
  grid-template-columns: 130px 1fr;
  padding: 7px 0;
}
.textarea-compact {
  min-height: 72px;
}


/* patch_09: compact popup footer actions */
.popup-panel .popup-form-actions {
  justify-content: flex-end;
  align-items: center;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}
.popup-panel .popup-form-actions .btn {
  min-height: 42px;
}
@media (max-width: 700px) {
  .popup-panel .popup-form-actions {
    justify-content: stretch;
  }
  .popup-panel .popup-form-actions .btn {
    width: 100%;
  }
}
