:root {
  --bg: #0b0f14;
  --panel: #131a22;
  --panel-border: #223040;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #4da3ff;
  --green: #3fb950;
  --red: #f85149;
  --amber: #d29922;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
}

header {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 12px 24px 16px;
  border-bottom: 1px solid var(--panel-border);
}

/* Logo (left) and the settings gear (right) share the very top row, above
   everything else in the header -- the gear is deliberately NOT grouped
   with .status-bar below anymore (it used to sit inline with it). */
.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.header-logo {
  height: 40px;
  width: auto;
  display: block;
}

.header-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
}

header h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 600;
}

.status-bar {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  align-items: center;
}

.settings-btn {
  background: transparent;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  color: var(--muted);
  font-size: 16px;
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}

.settings-btn:hover, .settings-btn:focus {
  border-color: var(--accent);
  color: var(--accent);
  outline: none;
}

.settings-subheading {
  margin: 4px 0 2px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  border-top: 1px solid var(--panel-border);
  padding-top: 12px;
}

.settings-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 14px;
  font-size: 13px;
}

.settings-field input {
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  color: var(--text);
  padding: 6px 10px;
  font-size: 13px;
  width: 100%;
}

.settings-field input:focus {
  outline: none;
  border-color: var(--accent);
}

.settings-field-checkbox {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.settings-field-checkbox input {
  width: auto;
  flex: none;
  padding: 0;
}

.settings-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 6px;
}

.settings-actions button {
  background: #1c2530;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  color: var(--text);
  padding: 8px 18px;
  font-size: 13px;
  cursor: pointer;
}

.settings-actions button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.settings-actions button.danger {
  background: var(--red);
  border-color: var(--red);
  color: #fff;
  font-weight: 600;
}

.settings-actions button.danger:hover {
  background: #d63a33;
  border-color: #d63a33;
  color: #fff;
}

.kill-switch-btn {
  border: none;
  cursor: pointer;
  font: inherit;
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

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

.stat .value {
  font-size: 16px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.badge.paper { background: #21384a; color: #4da3ff; }
.badge.sandbox { background: #2a3a1e; color: #a8d66b; }
.badge.live { background: #4a1e1e; color: #ff6b6b; }
.badge.kill-active { background: var(--red); color: #fff; }
.badge.kill-inactive { background: #1c2530; color: var(--muted); }

main {
  padding: 20px 24px 60px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(420px, 1fr));
  gap: 20px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 16px 18px;
  min-width: 0;
}

.panel h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin: 0 0 12px;
}

.panel.full-width { grid-column: 1 / -1; }

.panel-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.panel-header-row h2 {
  margin: 0;
}

.chart-widget-container {
  height: 500px;
  width: 100%;
}

@media (max-width: 640px) {
  .chart-widget-container {
    height: 360px;
  }
}

table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

th, td {
  text-align: left;
  padding: 6px 8px;
  border-bottom: 1px solid var(--panel-border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

th {
  color: var(--muted);
  font-weight: 500;
  font-size: 12px;
}

.table-scroll {
  overflow-x: auto;
}

.pos { color: var(--green); }
.neg { color: var(--red); }
.muted { color: var(--muted); }

.state-pill {
  padding: 2px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
}
.state-discovered, .state-watching { background: #1c2530; color: var(--muted); }
.state-heating_up { background: #3a2c14; color: var(--amber); }
.state-armed { background: #3a2c14; color: #f0a020; }
.state-triggered { background: #1e2f4a; color: var(--accent); }
.state-entered, .state-managing { background: #1e3a24; color: var(--green); }
.state-exited { background: #1c2530; color: var(--muted); }
.state-cooldown { background: #1c2530; color: var(--muted); }
.state-rejected { background: #3a1e1e; color: var(--red); }

.empty-row td {
  color: var(--muted);
  font-style: italic;
  text-align: center;
}

.hint {
  margin: -6px 0 10px;
  color: var(--muted);
  font-size: 12px;
}

.candidate-row {
  cursor: pointer;
}

.candidate-row:hover {
  background: #182230;
}

.candidate-row.selected-row {
  background: #1e2f4a;
}

.link-btn {
  background: transparent;
  border: none;
  color: var(--accent);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
}

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

.inline-form {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.inline-form input {
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  color: var(--text);
  padding: 6px 10px;
  font-size: 13px;
  width: 180px;
}

.inline-form input:focus {
  outline: none;
  border-color: var(--accent);
}

.inline-form button {
  background: #1c2530;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  color: var(--text);
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
}

.inline-form button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.guide-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.guide-btn {
  background: #1c2530;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  color: var(--text);
  padding: 8px 16px;
  font-size: 13px;
  cursor: pointer;
}

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

footer {
  padding: 12px 24px;
  color: var(--muted);
  font-size: 12px;
}

.info-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  margin-left: 3px;
  padding: 0;
  border: 1px solid var(--muted);
  border-radius: 50%;
  background: transparent;
  color: var(--muted);
  font-size: 10px;
  font-style: italic;
  font-family: Georgia, "Times New Roman", serif;
  line-height: 1;
  cursor: pointer;
}

.info-btn:hover, .info-btn:focus {
  border-color: var(--accent);
  color: var(--accent);
  outline: none;
}

.info-modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.info-modal-overlay.open {
  display: flex;
}

.info-modal-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 18px 20px;
  max-width: 560px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}

.info-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.info-modal-header h3 {
  margin: 0;
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text);
}

.info-modal-close {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}

.info-modal-close:hover {
  color: var(--text);
}

.info-modal-card p {
  margin: 0;
  color: var(--text);
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-line;
}

/* ---- Responsive: tablet (narrower than the 420px-min desktop grid can fit two-up) ---- */
@media (max-width: 900px) {
  main {
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    padding: 16px 16px 48px;
    gap: 16px;
  }
}

/* ---- Responsive: phones ---- */
@media (max-width: 640px) {
  header {
    padding: 12px 16px;
  }

  header h1 {
    font-size: 16px;
  }

  .header-logo {
    height: 28px;
  }

  .status-bar {
    gap: 14px;
  }

  .stat .value {
    font-size: 15px;
  }

  /* Single column -- the desktop grid's fixed minmax() would otherwise force
     the whole page to scroll sideways on a phone-width viewport. */
  main {
    grid-template-columns: 1fr;
    padding: 12px 12px 48px;
    gap: 14px;
  }

  .panel {
    padding: 14px;
  }

  th, td {
    padding: 8px 6px;
    font-size: 12.5px;
  }

  /* Bigger tap targets -- 32px is under the ~40px comfortable minimum on touch. */
  .settings-btn {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }

  .kill-switch-btn {
    padding: 6px 14px;
    font-size: 13px;
  }

  .inline-form {
    flex-wrap: wrap;
  }

  .inline-form input {
    width: auto;
    flex: 1 1 160px;
  }

  .inline-form button {
    flex: 1 1 auto;
    padding: 8px 14px;
  }

  .guide-buttons {
    flex-direction: column;
  }

  .guide-btn {
    width: 100%;
    padding: 10px 16px;
    text-align: center;
  }

  .info-modal-overlay {
    padding: 0;
    align-items: flex-end;
  }

  .info-modal-card {
    max-width: none;
    width: 100%;
    max-height: 90vh;
    border-radius: 14px 14px 0 0;
    padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  }

  .settings-actions {
    flex-wrap: wrap;
  }

  .settings-actions button {
    flex: 1 1 auto;
  }
}
