:root {
  color-scheme: light;
  --bg: #f4f7f9;
  --panel: #ffffff;
  --ink: #17212b;
  --muted: #687585;
  --line: #dce4eb;
  --accent: #0f8b8d;
  --accent-dark: #0a6f72;
  --up: #16834a;
  --down: #c23b3b;
  --warn: #c78720;
  --shadow: 0 10px 30px rgba(25, 39, 52, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: 260px 1fr;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.sidebar {
  background: #15232f;
  color: #eef7fb;
  padding: 22px;
}

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

.mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: var(--accent);
  font-weight: 800;
}

.brand strong, .brand small { display: block; }
.brand small { color: #9fb3c0; }

nav { display: grid; gap: 8px; }

button {
  border: 0;
  border-radius: 8px;
  cursor: pointer;
  font: inherit;
}

.nav {
  padding: 11px 12px;
  background: transparent;
  color: #c9d7df;
  text-align: left;
}

.nav.active, .nav:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

main {
  min-width: 0;
  padding: 28px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 18px;
  margin-bottom: 22px;
}

h1, h2, p { margin: 0; }
h1 { font-size: 28px; }
h2 { font-size: 18px; margin-bottom: 16px; }
p { color: var(--muted); margin-top: 4px; }

.primary {
  background: var(--accent);
  color: white;
  padding: 10px 14px;
  font-weight: 700;
}

.primary:hover { background: var(--accent-dark); }

.view { display: none; }
.view.active { display: block; }

.summary {
  display: grid;
  grid-template-columns: repeat(4, minmax(140px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.summary div, .panel, .monitor-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.summary div {
  padding: 16px;
}

.summary span {
  display: block;
  font-size: 28px;
  font-weight: 800;
}

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

.monitor-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 16px;
}

.monitor-card {
  padding: 16px;
  min-width: 0;
}

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

.target-name { font-size: 16px; font-weight: 800; }
.target-meta { color: var(--muted); font-size: 12px; margin-top: 2px; word-break: break-word; }

.badge {
  min-width: 72px;
  text-align: center;
  padding: 5px 8px;
  border-radius: 999px;
  font-weight: 800;
  font-size: 12px;
}

.badge.up { background: #dcf7e8; color: var(--up); }
.badge.down { background: #ffe2e2; color: var(--down); }
.badge.wait { background: #fff2d7; color: var(--warn); }

.metrics {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.metric {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
}

.metric strong { display: block; font-size: 16px; }
.metric small { color: var(--muted); }

canvas {
  width: 100%;
  height: 150px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdfe;
}

.panel {
  padding: 18px;
  margin-bottom: 16px;
}

.target-form {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(260px, 2fr) 150px auto;
  gap: 12px;
  align-items: end;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-weight: 700;
}

input, select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 11px;
  color: var(--ink);
  background: white;
  font: inherit;
}

.target-list, .event-list {
  display: grid;
  gap: 10px;
}

.target-row, .event-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}

.ghost {
  background: #eef4f7;
  color: #244050;
  padding: 8px 10px;
}

.danger {
  background: #ffe2e2;
  color: var(--down);
  padding: 8px 10px;
}

.settings-form {
  display: grid;
  gap: 14px;
}

fieldset {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  display: grid;
  gap: 10px;
}

legend {
  padding: 0 8px;
  font-weight: 800;
}

.switch {
  display: flex;
  grid-template-columns: none;
  align-items: center;
  gap: 8px;
  color: var(--ink);
}

.switch input {
  width: 18px;
  height: 18px;
}

.toasts {
  position: fixed;
  right: 18px;
  bottom: 18px;
  display: grid;
  gap: 10px;
  z-index: 20;
}

.toast {
  width: min(360px, calc(100vw - 36px));
  padding: 12px 14px;
  border-radius: 8px;
  color: white;
  box-shadow: var(--shadow);
}

.toast.up { background: var(--up); }
.toast.down { background: var(--down); }

@media (max-width: 900px) {
  body {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: sticky;
    top: 0;
    z-index: 10;
    padding: 14px;
  }

  .brand { margin-bottom: 12px; }
  nav { grid-template-columns: repeat(3, 1fr); }
  .nav { text-align: center; }
  main { padding: 18px; }
  .topbar { align-items: flex-start; flex-direction: column; }
  .summary { grid-template-columns: repeat(2, 1fr); }
  .target-form { grid-template-columns: 1fr; }
  .monitor-grid { grid-template-columns: 1fr; }
}

.toolbar {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
}

.panel.compact {
  padding: 12px 14px;
  margin-bottom: 16px;
}

.toolbar label {
  min-width: 220px;
}

.chart-wrap {
  height: 230px;
  min-height: 230px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdfe;
  padding: 10px;
}

.chart-wrap canvas {
  width: 100% !important;
  height: 100% !important;
  border: 0;
  border-radius: 0;
  background: transparent;
}

@media (max-width: 640px) {
  .toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .toolbar label {
    min-width: 0;
  }

  .chart-wrap {
    height: 210px;
    min-height: 210px;
  }
}

.toast.info { background: #244050; }

.chart-wrap { cursor: zoom-in; }

.detail-button {
  width: 100%;
  margin-top: 10px;
}

.detail-head {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 14px;
}

.detail-head h2 {
  margin-bottom: 2px;
}

.detail-panel {
  min-height: calc(100vh - 210px);
}

.detail-chart-wrap {
  height: calc(100vh - 260px);
  min-height: 460px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdfe;
  padding: 14px;
}

.detail-chart-wrap canvas {
  width: 100% !important;
  height: 100% !important;
  border: 0;
  border-radius: 0;
  background: transparent;
}

@media (max-width: 700px) {
  .detail-head {
    grid-template-columns: 1fr;
  }

  .detail-chart-wrap {
    height: 420px;
    min-height: 420px;
  }
}

.monitor-card {
  position: relative;
  overflow: hidden;
  transition: border-color 180ms ease, box-shadow 180ms ease, transform 180ms ease;
}

.monitor-card.alarm {
  border-color: rgba(194, 59, 59, 0.78);
  animation: alarmPulse 1.8s ease-in-out infinite;
}

.monitor-card.alarm::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(100deg, rgba(194, 59, 59, 0.18), transparent 34%, rgba(255, 255, 255, 0.45) 50%, transparent 66%, rgba(194, 59, 59, 0.14));
  opacity: 0;
  transform: translateX(-70%);
  animation: alarmSweep 2.4s ease-in-out infinite;
}

.monitor-card.alarm .target-name {
  color: var(--down);
}

@keyframes alarmPulse {
  0%, 100% { box-shadow: 0 10px 30px rgba(25, 39, 52, 0.08), 0 0 0 rgba(194, 59, 59, 0); }
  50% { box-shadow: 0 14px 34px rgba(25, 39, 52, 0.11), 0 0 26px rgba(194, 59, 59, 0.35); }
}

@keyframes alarmSweep {
  0%, 22% { opacity: 0; transform: translateX(-70%); }
  42% { opacity: 1; }
  72%, 100% { opacity: 0; transform: translateX(70%); }
}

.toast {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: start;
  transform-origin: right bottom;
  animation: toastIn 260ms cubic-bezier(.2,.9,.2,1) both;
}

.toast strong,
.toast span {
  display: block;
}

.toast strong {
  margin-bottom: 3px;
  font-size: 12px;
  letter-spacing: 0;
}

.toast button {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
  line-height: 1;
}

.toast button:hover {
  background: rgba(255, 255, 255, 0.28);
}

.toast.leaving {
  animation: toastOut 240ms ease-in both;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(34px) translateY(8px) scale(.96); }
  to { opacity: 1; transform: translateX(0) translateY(0) scale(1); }
}

@keyframes toastOut {
  from { opacity: 1; transform: translateX(0) scale(1); }
  to { opacity: 0; transform: translateX(38px) scale(.96); }
}

.detail-chart-wrap.detail-enter {
  animation: detailChartIn 420ms cubic-bezier(.2,.9,.2,1) both;
}

@keyframes detailChartIn {
  from { opacity: 0; transform: translateY(18px) scale(.985); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  .monitor-card.alarm,
  .monitor-card.alarm::before,
  .toast,
  .toast.leaving,
  .detail-chart-wrap.detail-enter {
    animation: none;
  }
}

.summary-card {
  display: block;
  text-align: left;
  padding: 16px;
  color: var(--ink);
}

button.summary-card {
  border: 1px solid var(--line);
  background: var(--panel);
}

.summary-card.active {
  border-color: rgba(15, 139, 141, 0.78);
  box-shadow: 0 10px 30px rgba(25, 39, 52, 0.08), inset 0 0 0 1px rgba(15, 139, 141, 0.18);
}

.summary-card.alarm {
  border-color: rgba(194, 59, 59, 0.82);
  animation: alarmPulse 1.8s ease-in-out infinite;
}

.summary-card.alarm span,
.summary-card.alarm small {
  color: var(--down);
}

.sound-gate {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 14px 16px;
  margin-bottom: 18px;
  border: 1px solid rgba(199, 135, 32, 0.42);
  border-radius: 8px;
  background: #fff8e9;
  box-shadow: var(--shadow);
}

.sound-gate.hidden {
  display: none;
}

.sound-gate strong,
.sound-gate span {
  display: block;
}

.sound-gate span {
  color: var(--muted);
  margin-top: 2px;
}

.sound-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

#soundSettingInfo {
  font-size: 12px;
}

@media (max-width: 700px) {
  .sound-gate {
    align-items: stretch;
    flex-direction: column;
  }
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
}

.section-head h2 {
  margin-bottom: 0;
}

.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f7fafc;
}

.segmented button {
  padding: 7px 11px;
  color: var(--muted);
  background: transparent;
}

.segmented button.active {
  color: #fff;
  background: var(--accent);
}

.target-row {
  position: relative;
  overflow: hidden;
  grid-template-columns: 1fr auto auto auto;
  transition: border-color 180ms ease, box-shadow 180ms ease;
}

.target-row.status-up {
  border-color: rgba(22, 131, 74, 0.72);
  box-shadow: inset 3px 0 0 rgba(22, 131, 74, 0.72);
}

.target-row.status-down {
  border-color: rgba(194, 59, 59, 0.78);
}

.target-row.alarm {
  animation: alarmPulse 1.8s ease-in-out infinite;
}

.target-row.alarm::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(100deg, rgba(194, 59, 59, 0.16), transparent 38%, rgba(255, 255, 255, 0.5) 50%, transparent 62%, rgba(194, 59, 59, 0.12));
  opacity: 0;
  transform: translateX(-72%);
  animation: alarmSweep 2.4s ease-in-out infinite;
}

.target-row > * {
  position: relative;
  z-index: 1;
}

.empty-state,
.empty-cell {
  padding: 16px;
  color: var(--muted);
  text-align: center;
}

.rows-select,
.detail-range {
  min-width: 180px;
}

.table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.event-table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
}

.event-table th,
.event-table td {
  padding: 11px 12px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.event-table th {
  color: var(--muted);
  font-size: 12px;
  background: #f7fafc;
}

.event-table tr:last-child td {
  border-bottom: 0;
}

@media (max-width: 760px) {
  .section-head,
  .detail-head {
    align-items: stretch;
    flex-direction: column;
    display: flex;
  }

  .target-row {
    grid-template-columns: 1fr;
  }

  .segmented {
    width: 100%;
  }

  .segmented button {
    flex: 1;
  }
}

.login-body {
  display: grid;
  grid-template-columns: 1fr;
  place-items: center;
  min-height: 100vh;
  background: var(--bg);
}

.login-shell {
  width: min(440px, calc(100vw - 32px));
  padding: 0;
}

.login-card {
  display: grid;
  gap: 14px;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--shadow);
}

.login-brand {
  margin-bottom: 6px;
  color: var(--ink);
}

.form-error {
  min-height: 20px;
  color: var(--down);
  font-weight: 700;
}

.user-box {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-box span {
  color: var(--muted);
  font-weight: 800;
}

.warning-banner {
  padding: 12px 14px;
  margin-bottom: 18px;
  border: 1px solid rgba(194, 59, 59, 0.42);
  border-radius: 8px;
  color: var(--down);
  background: #ffecec;
  font-weight: 800;
}

.hidden { display: none !important; }

.telegram-bots {
  display: grid;
  gap: 12px;
}

.telegram-bot {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdfe;
}

.secret-input {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.inline-form {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(180px, 1fr) auto;
  gap: 12px;
  align-items: end;
  margin-bottom: 14px;
}

#passwordForm {
  grid-template-columns: minmax(220px, 1fr) auto;
}

.user-list {
  display: grid;
  gap: 8px;
}

.user-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.user-row span {
  color: var(--muted);
}

@media (max-width: 760px) {
  .inline-form,
  #passwordForm {
    grid-template-columns: 1fr;
  }
}

.logo-mark {
  background: #fff;
  overflow: hidden;
}

.logo-mark img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  padding: 4px;
}

.detail-head {
  grid-template-columns: auto minmax(220px, 1fr) minmax(190px, 220px) auto;
}

.detail-head .ghost,
.detail-head .detail-range {
  align-self: end;
}

.event-tools {
  display: grid;
  grid-template-columns: minmax(180px, 1fr) minmax(180px, 1fr) auto;
  gap: 12px;
  align-items: end;
  margin-bottom: 12px;
}

.pagination {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
}

.pagination span {
  color: var(--muted);
  font-weight: 800;
}

button:disabled {
  cursor: not-allowed;
  opacity: .5;
}

@media (max-width: 760px) {
  .event-tools {
    grid-template-columns: 1fr;
  }

  .pagination {
    justify-content: stretch;
  }

  .pagination button {
    flex: 1;
  }
}
