:root {
  --bg: #f4f1ea;
  --paper: #fffcf7;
  --side: #ebe6dc;
  --ink: #161410;
  --muted: #6f6a62;
  --line: #d9d2c6;
  --accent: #9a3412;
  --pine: #1c3d38;
  --ok: #1f7a4d;
  --bad: #b42318;
  --focus: #1c3d38;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
html { overflow-x: hidden; }
body {
  font: 15px/1.45 "IBM Plex Sans", "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}
.lbl-short { display: none; }
.table-wrap { overflow-x: auto; }

.hidden { display: none !important; }
.muted { color: var(--muted); }
.mono { font-family: "IBM Plex Mono", ui-monospace, monospace; font-size: 13px; }
.kicker {
  margin: 0;
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 500;
}
.howto {
  margin: 0 0 16px;
  padding-left: 18px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}
.howto li { margin-bottom: 6px; }
.howto b { color: var(--ink); font-weight: 600; }
#pushBtn.push-on {
  color: var(--ok);
  border-color: #c9ddd0;
}
.error { color: var(--bad); font-size: 13px; }

.top {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
  padding: 16px 20px 14px;
  padding-left: max(20px, env(safe-area-inset-left));
  padding-right: max(20px, env(safe-area-inset-right));
  border-bottom: 1px solid var(--line);
  background: var(--paper);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand-block { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.brand-block strong { font-size: 20px; font-weight: 600; letter-spacing: -0.02em; }
.current-node { display: none; }
.menu-btn { display: none; }
.nodes-overlay { display: none; }
.drawer-close { display: none; }
.aside-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.top-actions { display: flex; gap: 8px; align-items: center; }

.layout {
  display: grid;
  grid-template-columns: 280px 1fr;
  min-height: calc(100% - 73px);
}
.nodes-wrap {
  background: var(--side);
  border-right: 1px solid var(--line);
  padding: 18px 14px;
  overflow: auto;
}
.aside-label {
  margin: 0 10px 10px;
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}
.node-card {
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  border-left: 2px solid transparent;
  color: inherit;
  border-radius: 0;
  padding: 10px 12px;
  margin-bottom: 2px;
  cursor: pointer;
}
.node-card:hover { background: rgba(255,255,255,.45); }
.node-card.active {
  background: var(--paper);
  border-left-color: var(--accent);
}
.node-card h4 {
  margin: 0 0 2px;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}
.node-card .ip { color: var(--muted); }

.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  font-size: 13px;
  color: var(--muted);
}
.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  display: inline-block;
  background: var(--bad);
  flex-shrink: 0;
}
.dot.ok { background: var(--ok); }

.detail { overflow: auto; padding: 28px 36px 48px; background: var(--bg); }
.empty {
  color: var(--muted);
  margin-top: 48px;
  max-width: 28em;
}

h2 {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.03em;
}
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  margin-bottom: 28px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--line);
}
.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0;
  margin: 18px 0 0;
  border: 1px solid var(--line);
  background: var(--paper);
}
.stat {
  padding: 12px 14px;
  border-right: 1px solid var(--line);
}
.stat:last-child { border-right: 0; }
.stat b { display: block; font-size: 15px; font-weight: 600; color: var(--ink); }
.stat span { font-size: 11px; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }

button, input { font: inherit; }
button {
  background: var(--accent);
  color: #fffaf6;
  border: 0;
  border-radius: 2px;
  padding: 9px 14px;
  cursor: pointer;
  font-weight: 500;
}
button:hover { filter: brightness(1.05); }
button.ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
}
button.link {
  background: none;
  color: var(--pine);
  border: 0;
  padding: 0;
  font-weight: 500;
  text-decoration: underline;
  text-underline-offset: 3px;
}
button.link.danger { color: var(--bad); }
button.danger-outline {
  background: transparent;
  color: var(--bad);
  border: 1px solid #e8c4c0;
}

input {
  width: 100%;
  margin-top: 6px;
  background: var(--paper);
  border: 1px solid var(--line);
  color: var(--ink);
  border-radius: 2px;
  padding: 9px 10px;
}
input:focus {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}
label {
  display: block;
  margin: 0 0 12px;
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 0.04em;
}

.row {
  display: grid;
  grid-template-columns: 1fr 1.2fr 90px 1fr auto;
  gap: 12px;
  align-items: end;
}
.country-pick { position: relative; margin-top: 6px; }
.country-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--line);
  text-align: left;
  font-weight: 400;
  padding: 8px 10px;
}
.country-menu {
  background: #fffcf7;
  border: 1px solid var(--line);
  box-shadow: 0 16px 40px rgba(22, 20, 16, 0.18);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.country-search {
  margin: 10px 10px 6px;
  width: calc(100% - 20px);
}
.country-opts { overflow: auto; flex: 1; padding-bottom: 6px; }
.country-opt {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  background: transparent;
  color: inherit;
  border: 0;
  border-radius: 0;
  text-align: left;
  font-weight: 400;
  padding: 8px 12px;
}
.country-opt:hover { background: #f3eee4; }
.flag, .country-view .flag {
  width: 20px;
  height: 15px;
  object-fit: cover;
  border-radius: 2px;
  flex-shrink: 0;
  box-shadow: 0 0 0 1px rgba(0,0,0,.08);
}
.country-view { display: inline-flex; align-items: center; gap: 8px; }
.icon-btn {
  width: 38px;
  padding: 8px 0;
  font-size: 16px;
  line-height: 1;
}
.route-row {
  grid-template-columns: 110px 1.2fr 90px 1.3fr 1fr auto;
}
.add-form {
  margin-bottom: 28px;
}
.add-form .hint { margin-top: 10px; }

table {
  width: 100%;
  border-collapse: collapse;
  background: var(--paper);
  border: 1px solid var(--line);
}
th, td {
  text-align: left;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  vertical-align: middle;
}
th {
  color: var(--muted);
  font-weight: 500;
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  background: #f7f3ec;
}
tr:last-child td { border-bottom: 0; }
.cell-actions { white-space: nowrap; display: flex; gap: 14px; }

.st {
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.04em;
}
.st.up { color: var(--ok); }
.st.down { color: var(--bad); }
.st.off { color: var(--muted); }

.login-body {
  display: grid;
  place-items: center;
  background: var(--bg);
}
.login-card {
  width: min(360px, calc(100vw - 32px));
  background: var(--paper);
  border: 1px solid var(--line);
  padding: 36px 32px;
}
.login-card h1 {
  margin: 6px 0 4px;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.03em;
}
.lede { margin: 0 0 24px; color: var(--muted); }
.login-card button { width: 100%; margin-top: 8px; }

dialog { border: 0; background: transparent; padding: 0; overflow: visible; }
dialog::backdrop { background: rgba(22, 20, 16, 0.45); }
.modal {
  width: 400px;
  background: var(--paper);
  border: 1px solid var(--line);
  padding: 28px;
  overflow: visible;
}
.modal-wide { width: 560px; }
.modal h3 {
  margin: 4px 0 18px;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.02em;
}
.actions { display: flex; gap: 8px; margin-top: 8px; }
#pushDialog .actions { flex-wrap: wrap; }

.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  background: var(--ink);
  color: #f4f1ea;
  padding: 12px 16px;
  max-width: 420px;
  font-size: 13px;
}

.monitor { margin: 0 0 28px; }
.monitor-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.period { display: flex; gap: 4px; }
.period button {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  padding: 6px 10px;
  font-weight: 500;
  font-size: 12px;
}
.period button.on {
  background: var(--ink);
  color: #f4f1ea;
  border-color: var(--ink);
}
.charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.chart-card {
  background: var(--paper);
  border: 1px solid var(--line);
  padding: 10px 10px 8px;
  position: relative;
  min-width: 0;
}
.chart-card canvas {
  display: block;
  width: 100%;
  height: 132px;
}
.chart-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 8px;
  padding: 0 4px;
}
.chart-now { color: var(--muted); font-size: 12px; }
.chart-tip {
  position: absolute;
  z-index: 2;
  background: var(--ink);
  color: #f4f1ea;
  padding: 8px 10px;
  font-size: 12px;
  pointer-events: none;
  min-width: 110px;
  line-height: 1.4;
}
.swatch {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin: 0 4px 0 8px;
  vertical-align: middle;
}
.swatch.in { background: #1c3d38; }
.swatch.out { background: #9a3412; }
.settings-row {
  grid-template-columns: 1fr 1.1fr 1.4fr 1.4fr 90px auto;
}

@media (max-width: 980px) {
  .layout {
    grid-template-columns: 1fr;
    min-height: auto;
  }
  .top { z-index: 22; }
  .menu-btn {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 44px;
    height: 44px;
    padding: 10px;
    flex-shrink: 0;
    background: var(--paper);
    color: var(--ink);
    border: 1px solid var(--line);
  }
  .menu-btn span {
    display: block;
    height: 2px;
    background: currentColor;
    border-radius: 1px;
  }
  .current-node {
    display: block;
    font-size: 12px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 42vw;
  }
  .current-node.hidden { display: none; }
  .nodes-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(22, 20, 16, 0.4);
    z-index: 20;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease;
  }
  body.nodes-open { overflow: hidden; }
  body.nodes-open .nodes-overlay {
    opacity: 1;
    pointer-events: auto;
  }
  .nodes-wrap {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(320px, 86vw);
    z-index: 23;
    border-right: 1px solid var(--line);
    border-bottom: 0;
    padding: 16px 12px;
    padding-top: max(16px, env(safe-area-inset-top));
    transform: translateX(-105%);
    transition: transform .22s ease;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }
  body.nodes-open .nodes-wrap { transform: translateX(0); }
  .drawer-close { display: inline-flex; padding: 6px 10px; font-size: 13px; }
  .aside-label { margin: 0 4px; }
  #nodes { display: block; }
  .node-card { width: 100%; min-width: 0; }
  .detail { padding: 18px 16px 40px; }
  .toolbar {
    flex-direction: column;
    gap: 14px;
  }
  .toolbar > div:first-child { width: 100%; min-width: 0; }
  .toolbar .actions { flex-wrap: wrap; }
  .row,
  .route-row,
  .settings-row { grid-template-columns: 1fr; }
  .route-add { width: 100%; }
  .charts { grid-template-columns: 1fr; }
  .stats {
    width: 100%;
    grid-template-columns: 1fr 1fr;
  }
  .stat {
    text-align: center;
    border-right: 1px solid var(--line);
    border-bottom: 0;
  }
  .stat:nth-child(2n) { border-right: 0; }
  .stat:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .monitor-bar { flex-wrap: wrap; gap: 8px; }
  .period { width: 100%; }
  .period button { flex: 1; }
  .chart-card canvas { height: 120px; }
  h2 { font-size: 22px; }
}

@media (max-width: 760px) {
  .top {
    align-items: center;
    padding: 12px 12px;
    padding-top: max(12px, env(safe-area-inset-top));
  }
  .brand-block strong { font-size: 17px; }
  .top-actions { gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
  .top-actions button { padding: 8px 10px; font-size: 13px; }
  .lbl-full { display: none; }
  .lbl-short { display: inline; }
  .empty { margin-top: 24px; }
  input, .country-btn, button, .country-search {
    font-size: 16px;
  }
  .modal,
  .modal-wide {
    width: calc(100vw - 24px);
    max-width: 100%;
    max-height: min(88vh, 720px);
    overflow: auto;
    padding: 22px 18px 20px;
  }
  dialog { margin: auto; }
  .actions { flex-wrap: wrap; }
  .actions button { flex: 1; min-height: 44px; }
  .toast {
    left: 12px;
    right: 12px;
    bottom: max(12px, env(safe-area-inset-bottom));
    max-width: none;
  }
  .login-body { padding: 24px 16px; }
  .login-card { padding: 28px 20px; }
  table, thead, tbody, th, td, tr { display: block; }
  thead { display: none; }
  .table-wrap { overflow: visible; }
  table { border: 0; background: transparent; }
  tbody tr {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    align-items: start;
    background: var(--paper);
    border: 1px solid var(--line);
    margin-bottom: 8px;
    padding: 8px 10px 6px;
  }
  tbody td {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: flex-start;
    gap: 2px;
    border-bottom: 0;
    padding: 4px 4px;
    font-size: 12px;
    min-width: 0;
  }
  tbody td .mono { font-size: 12px; word-break: break-all; }
  tbody td::before {
    content: attr(data-label);
    color: var(--muted);
    font-size: 9px;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    flex: none;
    width: auto;
  }
  tbody td:nth-child(1),
  tbody td:nth-child(2) {
    grid-column: 1 / -1;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    padding: 3px 4px;
  }
  tbody td:nth-child(1)::before,
  tbody td:nth-child(2)::before {
    flex: 0 0 44px;
  }
  tbody td.empty-note { display: none; }
  tbody td.cell-actions {
    grid-column: 1 / -1;
    flex-direction: row;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 10px;
    padding: 6px 4px 2px;
  }
  tbody td.cell-actions::before { display: none; }
  tbody td.cell-actions .link { font-size: 12px; }
  .country-opt { min-height: 44px; }
}
