:root {
  --bg: #eef2f7;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --line: #dbe3ef;
  --text: #171b26;
  --muted: #667085;
  --blue: #3867f4;
  --blue-dark: #244bd6;
  --blue-soft: #edf2ff;
  --red: #d92d20;
  --red-soft: #fff1f0;
  --green: #079455;
  --green-soft: #ecfdf3;
  --shadow: 0 18px 45px rgba(22, 34, 58, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  letter-spacing: 0;
}

button, input, select, textarea { font: inherit; letter-spacing: 0; }

button {
  border: 0;
  border-radius: 8px;
  min-height: 38px;
  padding: 0 14px;
  background: var(--blue);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 10px 24px rgba(56, 103, 244, 0.2);
}

button:hover { background: var(--blue-dark); }
button:disabled { opacity: 0.6; cursor: not-allowed; }
button.secondary, .secondary {
  background: #fff;
  color: var(--text);
  border: 1px solid var(--line);
  box-shadow: none;
}
button.secondary:hover, .secondary:hover { background: #f8fafc; }
button.active, .side-nav button.active {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}

input, select, textarea {
  width: 100%;
  border: 1px solid #cfd8e6;
  border-radius: 8px;
  background: #fff;
  color: var(--text);
  padding: 10px 12px;
  outline: 0;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 4px rgba(56, 103, 244, 0.12);
}

[hidden] { display: none !important; }

.app-shell {
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 24px 16px;
  background: #fff;
  border-right: 1px solid var(--line);
  box-shadow: 10px 0 30px rgba(22, 34, 58, 0.04);
  overflow-y: auto;
}

.side-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 28px;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: var(--blue);
  color: #fff;
  font-weight: 900;
}

.side-brand strong { display: block; font-size: 20px; line-height: 1.1; }
.side-brand span:last-child { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }

.side-nav {
  display: grid;
  gap: 6px;
}

.side-nav button {
  justify-content: flex-start;
  width: 100%;
  text-align: left;
  color: #667085;
}

.app-main { min-width: 0; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 22px 28px;
  background: rgba(255, 255, 255, 0.9);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(14px);
}

.topbar h1 {
  margin: 0;
  font-size: 28px;
  line-height: 1.15;
}

.topbar p {
  margin: 4px 0 0;
  color: var(--muted);
}

.eyebrow {
  margin: 0 0 4px !important;
  color: var(--blue) !important;
  font-size: 12px;
  font-weight: 850;
  text-transform: uppercase;
}

.top-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.session-badge, .build-badge, .ui-version {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  border-radius: 999px;
  padding: 0 10px;
  background: var(--blue-soft);
  color: var(--blue-dark);
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.ui-version { background: #f1f5f9; color: #64748b; }

.settings-menu { position: relative; }
.settings-menu summary {
  list-style: none;
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 14px;
  background: #fff;
  font-weight: 800;
  cursor: pointer;
}
.settings-menu summary::-webkit-details-marker { display: none; }

.settings-popover {
  position: absolute;
  right: 0;
  top: calc(100% + 10px);
  width: min(420px, calc(100vw - 32px));
  padding: 16px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.settings-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 8px 0;
}

.settings-label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.settings-field {
  display: grid;
  gap: 6px;
  margin: 10px 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.layout {
  display: grid;
  gap: 18px;
  padding: 22px 28px 36px;
}

.login-panel {
  padding: 48px 28px;
}

.login-card {
  display: grid;
  grid-template-columns: minmax(260px, 0.9fr) minmax(300px, 1.1fr);
  gap: 24px;
  max-width: 880px;
  margin: 0 auto;
  padding: 26px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.login-copy h2 { margin: 6px 0; font-size: 28px; }
.login-copy p { margin: 0; color: var(--muted); line-height: 1.5; }
.login-kicker { color: var(--blue); font-size: 12px; font-weight: 850; text-transform: uppercase; }

.login-grid {
  display: grid;
  gap: 12px;
  align-content: start;
}

.login-error {
  grid-column: 1 / -1;
  border: 1px solid #fecaca;
  background: var(--red-soft);
  color: #b42318;
  border-radius: 8px;
  padding: 12px 14px;
  font-weight: 800;
}

.metrics {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}

.metrics article {
  min-height: 104px;
  padding: 18px;
  border-radius: 8px;
  background: #fff;
  border: 1px solid var(--line);
  box-shadow: 0 12px 25px rgba(22, 34, 58, 0.05);
}

.metrics article:first-child {
  background: var(--blue);
  color: #fff;
  border-color: var(--blue);
}

.metrics span {
  display: block;
  color: inherit;
  opacity: 0.72;
  font-size: 12px;
  font-weight: 850;
}

.metrics strong {
  display: block;
  margin-top: 12px;
  font-size: 28px;
  line-height: 1;
}

.panel {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 28px rgba(22, 34, 58, 0.05);
  overflow: hidden;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--line);
}

.panel-head h2 {
  margin: 0;
  font-size: 18px;
}

.panel-head p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.panel-controls, .actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.attention-strip {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  background: #fbfdff;
}

.category-counts {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.category-counts button, .category-counts span {
  min-height: 30px;
  border-radius: 999px;
  padding: 0 10px;
}

.alarm-state {
  color: var(--muted);
  font-size: 12px;
}

.alert-grid, .agent-grid, .customer-grid, .template-list {
  display: grid;
  gap: 12px;
  padding: 16px;
}

.alert-card, .agent-card, .customer-card, .template-card, .card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 14px;
}

.alert-card.open {
  border-color: #fecaca;
  background: #fffafa;
}

.alert-card h3, .agent-card h3, .customer-card h3, .template-card h3 {
  margin: 0 0 6px;
}

.agent-card-head, .agent-meta-grid, .agent-detail-lines, .agent-template-row {
  margin-bottom: 12px;
}

.agent-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.agent-meta-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 8px;
}

.agent-meta-grid div, .agent-template-row {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: #f8fafc;
}

.agent-meta-grid span, .agent-template-row span {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.agent-detail-lines {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.agent-template-row select {
  width: 100%;
  margin-bottom: 6px;
}

.template-form {
  display: grid;
  gap: 12px;
  padding: 16px;
  border-bottom: 1px solid var(--line);
}

.template-form label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.template-form input, .template-form textarea {
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
}

.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
}

.template-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.template-card-head p {
  margin: 0;
  color: var(--muted);
}

.template-counts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}

.template-counts div {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: #f8fafc;
}

.template-counts span {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.muted, small, .small {
  color: var(--muted);
}

.pill, .badge, .status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  border-radius: 999px;
  padding: 0 10px;
  background: var(--blue-soft);
  color: var(--blue-dark);
  font-size: 12px;
  font-weight: 800;
}

.ok, .online, .success { background: var(--green-soft); color: var(--green); }
.danger, .error { background: var(--red-soft); color: var(--red); }

.image-modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: grid;
  grid-template-rows: auto 1fr;
  background: rgba(15, 23, 42, 0.76);
}

.image-modal-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  background: #fff;
}

.image-modal-body {
  display: grid;
  place-items: center;
  padding: 20px;
}

.image-modal img {
  max-width: 100%;
  max-height: 86vh;
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 90;
  max-width: min(420px, calc(100vw - 36px));
  padding: 12px 14px;
  border-radius: 8px;
  background: #111827;
  color: #fff;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

@media (max-width: 1040px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: static;
    height: auto;
    padding: 16px;
  }
  .side-nav {
    grid-auto-flow: column;
    grid-auto-columns: max-content;
    overflow-x: auto;
    padding-bottom: 4px;
  }
  .side-nav button { width: auto; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 720px) {
  .topbar {
    position: static;
    align-items: stretch;
    flex-direction: column;
    padding: 18px;
  }
  .top-actions, .panel-controls, .actions, .attention-strip {
    justify-content: flex-start;
  }
  .layout { padding: 16px; }
  .metrics, .login-card {
    grid-template-columns: 1fr;
  }
  .settings-popover {
    left: 0;
    right: auto;
  }
}
