*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f1117;
  --bg-card: #1a1d27;
  --bg-card-hover: #222632;
  --border: #2a2e3a;
  --text: #e4e6ed;
  --text-muted: #6b7280;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --green: #22c55e;
  --yellow: #eab308;
  --red: #ef4444;
  --orange: #f97316;
  --radius: 10px;
  --shadow: 0 2px 8px rgba(0,0,0,.3);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
}

.app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px 40px;
}

/* Header */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 16px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--accent);
}

.logo h1 {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
}

.status-badge {
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 20px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-muted);
  font-weight: 500;
}

.status-badge.ok {
  background: rgba(34, 197, 94, 0.15);
  border-color: var(--green);
  color: var(--green);
}

.status-badge.warn {
  background: rgba(234, 179, 8, 0.15);
  border-color: var(--yellow);
  color: var(--yellow);
}

.status-badge.error {
  background: rgba(239, 68, 68, 0.15);
  border-color: var(--red);
  color: var(--red);
}

nav {
  display: flex;
  gap: 4px;
  background: var(--bg-card);
  border-radius: var(--radius);
  padding: 4px;
}

.tab-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 8px 18px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all .15s;
}

.tab-btn:hover {
  color: var(--text);
}

.tab-btn.active {
  background: var(--accent);
  color: #fff;
}

/* Tabs */
.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}

/* Toolbar */
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 12px;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 12px;
}

.last-update {
  font-size: 13px;
  color: var(--text-muted);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  transition: all .15s;
  font-weight: 500;
}

.btn:hover {
  background: var(--bg-card-hover);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

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

.btn-danger {
  border-color: var(--red);
  color: var(--red);
}

.btn-danger:hover {
  background: rgba(239, 68, 68, 0.15);
}

.btn-lg {
  padding: 12px 28px;
  font-size: 15px;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Cards */
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
  margin-bottom: 32px;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  transition: border-color .15s;
}

.card:hover {
  border-color: var(--accent);
}

.card.placeholder {
  color: var(--text-muted);
  text-align: center;
  padding: 40px;
  grid-column: 1 / -1;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 14px;
}

.card-server {
  font-weight: 600;
  font-size: 15px;
  word-break: break-all;
}

.card-stratum {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 12px;
  background: rgba(59, 130, 246, 0.15);
  color: var(--accent);
  white-space: nowrap;
}

.card-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.metric {
  display: flex;
  flex-direction: column;
}

.metric-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.metric-value {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.metric-value.positive { color: var(--green); }
.metric-value.negative { color: var(--red); }
.metric-value.neutral { color: var(--yellow); }

/* Section titles */
.section-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 14px;
  color: var(--text);
}

/* Tables */
.diffs-table-wrap, .history-table-wrap {
  overflow-x: auto;
  margin-bottom: 32px;
}

.diffs-table, .history-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.diffs-table th, .history-table th {
  text-align: left;
  padding: 10px 14px;
  background: var(--bg-card);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border);
}

.diffs-table td, .history-table td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}

.diffs-table tbody tr:hover, .history-table tbody tr:hover {
  background: var(--bg-card);
}

.diff-bar {
  display: inline-block;
  height: 6px;
  border-radius: 3px;
  background: var(--accent);
  margin-left: 8px;
  vertical-align: middle;
  min-width: 2px;
}

.muted {
  color: var(--text-muted);
  text-align: center;
}

/* Failed list */
.failed-list {
  margin-bottom: 32px;
}

.failed-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.2);
  border-radius: 8px;
  margin-bottom: 8px;
  font-size: 14px;
}

.failed-icon {
  color: var(--red);
  font-weight: 700;
}

/* Chart */
.chart-container {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  height: 320px;
  position: relative;
  margin-bottom: 32px;
}

canvas {
  width: 100% !important;
  height: 100% !important;
}

/* GrandMaster label */
.gm-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  padding: 6px 14px;
  background: rgba(59, 130, 246, 0.1);
  border: 1px solid rgba(59, 130, 246, 0.3);
  border-radius: 8px;
}

.chart-tall {
  height: 380px;
}

.stat-good { color: var(--green); }
.stat-warn { color: var(--yellow); }
.stat-bad { color: var(--red); }

/* РСТВ */
.rstv-header { margin-bottom: 20px; }

.rstv-status-row {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

.hidden { display: none !important; }

.cn0-bar {
  display: inline-block;
  height: 10px;
  border-radius: 4px;
  vertical-align: middle;
  min-width: 2px;
}

.sat-system-gps { color: #3b82f6; }
.sat-system-glo { color: #ef4444; }
.sat-system-gal { color: #f97316; }
.sat-system-bds { color: #eab308; }

/* Settings */
.settings-form {
  max-width: 600px;
}

.form-group {
  margin-bottom: 24px;
}

.form-group label {
  display: block;
  font-weight: 600;
  margin-bottom: 6px;
  font-size: 14px;
}

.form-group small {
  display: block;
  color: var(--text-muted);
  font-size: 12px;
  margin-top: 4px;
}

.form-group input[type="number"],
.form-group input[type="text"],
select {
  width: 100%;
  max-width: 300px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color .15s;
}

.form-group input:focus, select:focus {
  border-color: var(--accent);
}

.checkbox-label {
  display: flex !important;
  align-items: center;
  gap: 10px;
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

/* Server list in settings */
.server-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}

.server-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
}

.server-item button {
  background: none;
  border: none;
  color: var(--red);
  cursor: pointer;
  font-size: 18px;
  padding: 0 4px;
  line-height: 1;
}

.server-item button:hover {
  opacity: 0.7;
}

.add-server {
  display: flex;
  gap: 8px;
}

.add-server input {
  flex: 1;
  max-width: 300px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-card);
  color: var(--text);
  font-size: 14px;
  outline: none;
}

.add-server input:focus {
  border-color: var(--accent);
}

.save-status {
  margin-left: 14px;
  font-size: 14px;
  color: var(--green);
}

/* History detail */
.history-detail {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-top: 16px;
}

.history-detail.hidden {
  display: none;
}

.detail-btn {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 13px;
  text-decoration: underline;
}

.detail-btn:hover {
  color: var(--accent-hover);
}

/* РСТВ device panels (multi-device) */
.rstv-device-panel {
  margin-bottom: 32px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--border);
}

.rstv-device-panel:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

/* РСТВ device list in settings */
.rstv-device-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 14px;
}

.rstv-device-item {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}

.rstv-device-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.rstv-device-header button {
  background: none;
  border: none;
  color: var(--red);
  cursor: pointer;
  font-size: 20px;
  padding: 0 4px;
  line-height: 1;
}

.rstv-device-header button:hover {
  opacity: 0.7;
}

.rstv-device-fields {
  display: grid;
  grid-template-columns: 1fr 80px 1fr;
  gap: 8px;
}

.rstv-device-fields input {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  outline: none;
}

.rstv-device-fields input:focus {
  border-color: var(--accent);
}

/* GM offset cells in history table */
.gm-offset-cell {
  white-space: nowrap;
}

.gm-offset-item {
  display: inline-block;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.rstv-hist-block {
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  padding: 2px 0;
  line-height: 1.4;
}
.rstv-hist-block b {
  font-weight: 600;
}
.rstv-hist-block.stat-good {
  color: var(--green);
}
.rstv-hist-block.stat-bad {
  color: var(--red);
}

/* Responsive */
@media (max-width: 768px) {
  header {
    flex-direction: column;
    align-items: flex-start;
  }

  .cards {
    grid-template-columns: 1fr;
  }

  .toolbar {
    flex-direction: column;
    align-items: flex-start;
  }
}
