:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #1c2430;
  --muted: #687385;
  --border: #e3e7ee;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --danger-bg: #fee2e2;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

header.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0.6rem 1rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}
header.topbar .brand { font-weight: 700; font-size: 1.05rem; color: var(--text); }
header.topbar nav { display: flex; gap: 0.9rem; flex-wrap: wrap; margin-left: auto; }
header.topbar nav a { color: var(--muted); font-size: 0.92rem; }
header.topbar nav a.active { color: var(--primary); font-weight: 600; }

main.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 1.2rem 1rem 3rem;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
  margin-bottom: 1rem;
}

h1 { font-size: 1.4rem; margin: 0 0 1rem; }
h2 { font-size: 1.15rem; margin: 1.5rem 0 0.7rem; }
h2:first-child { margin-top: 0; }
h3 { font-size: 1rem; margin: 1rem 0 0.5rem; }
h3:first-child { margin-top: 0; }

.grid { display: grid; gap: 1rem; }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 760px) {
  .grid.cols-2, .grid.cols-3 { grid-template-columns: 1fr; }

  /* Compact header: brand on its own line, menu in one scrollable row. */
  header.topbar { display: block; padding: 0.55rem 0.8rem; }
  header.topbar .brand { display: inline-block; margin-bottom: 0.35rem; }
  header.topbar nav {
    display: flex;
    flex-wrap: nowrap;
    gap: 0.9rem;
    margin-left: 0;
    overflow-x: auto;
    padding-bottom: 0.15rem;
  }
  header.topbar nav a { white-space: nowrap; font-size: 0.86rem; }

  /* Secondary table columns are dropped on narrow screens. */
  th.hide-sm, td.hide-sm { display: none; }
  /* Dashboard counters: compact rows instead of three tall cards. */
  .stats { gap: 0.5rem; }
  .stats .card { display: flex; align-items: baseline; gap: 0.6rem; padding: 0.7rem 0.9rem; margin-bottom: 0; }
  .stats .stat { font-size: 1.3rem; }
  .stats .stat-label { font-size: 0.8rem; }
  .table-wrap table th, .table-wrap table td { padding: 0.45rem 0.45rem; }
  /* Access table on a phone: the badge is redundant (the button already says
     «Запретить»/«Разрешить»), so only the protocol and the action remain. */
  table.access th:nth-child(2), table.access td:nth-child(2) { display: none; }
  table.access th:first-child, table.access td:first-child { width: 100%; }
  table.access th, table.access td { padding: 0.45rem 0.3rem; }
  table.access td:last-child button { min-width: 0; }
  /* Compact action buttons so a delete/link button still fits the row. */
  .table-wrap table td:last-child button,
  .table-wrap table td:last-child .btn { padding: 0.2rem 0.45rem; font-size: 0.75rem; }
}

table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
th, td { text-align: left; padding: 0.55rem 0.6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; }
tr:last-child td { border-bottom: none; }
.table-wrap { overflow-x: auto; }
/* Summary tables (dashboard): the first column absorbs the extra width and
   the rest hug their content, otherwise columns drift apart on wide screens. */
table.summary th, table.summary td { white-space: nowrap; }
.server-id-line { margin: 0.15rem 0 0.4rem; }
details.server-details { margin: 0.2rem 0 0.6rem; }
details.server-details summary { cursor: pointer; color: var(--muted); font-size: 0.85rem; user-select: none; }
details.server-details table.summary { margin-top: 0.4rem; }
table.summary th:last-child, table.summary td:last-child { width: 100%; white-space: normal; }

/* Access tables: protocol/status hug their content, the action column sits at
   the right edge and buttons keep a stable width. */
table.access th, table.access td { white-space: nowrap; }
table.access th:nth-child(2), table.access td:nth-child(2) { width: 100%; }
table.access td:last-child button { min-width: 6.5rem; }
/* Values that must stay on one line (IPs, badges next to a name). */
.nowrap { white-space: nowrap; }
.cell-with-badge { display: inline-flex; align-items: center; gap: 0.4rem; flex-wrap: nowrap; }

.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
}
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--danger-bg); color: var(--danger); }
.badge.muted { background: #eef1f5; color: var(--muted); }

button, .btn {
  display: inline-block;
  background: var(--primary);
  color: #fff;
  border: 1px solid var(--primary);
  border-radius: 8px;
  padding: 0.45rem 0.85rem;
  font-size: 0.9rem;
  cursor: pointer;
  text-align: center;
}
button:hover, .btn:hover { background: var(--primary-dark); text-decoration: none; color: #fff; }
button.secondary, .btn.secondary { background: #fff; color: var(--text); border-color: var(--border); }
button.secondary:hover { background: #f1f3f6; }
button.danger, .btn.danger { background: var(--danger); border-color: var(--danger); }
button.danger:hover { background: #b91c1c; }
button.small { padding: 0.25rem 0.6rem; font-size: 0.82rem; }
button:disabled { opacity: 0.55; cursor: not-allowed; }

input, select, textarea {
  width: 100%;
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.92rem;
  background: #fff;
  color: var(--text);
}
label { display: block; font-size: 0.85rem; color: var(--muted); margin: 0.6rem 0 0.25rem; }
input::placeholder, textarea::placeholder { color: #a7b0bd; opacity: 1; }

/* File inputs: make the native control look like the rest of the form. */
input[type="file"] { padding: 0.35rem 0.5rem; background: #fff; }
input[type="file"]::file-selector-button {
  font: inherit;
  margin-right: 0.6rem;
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f6f7f9;
  color: var(--text);
  cursor: pointer;
}
.form-row { margin-bottom: 0.5rem; }
.inline-form { display: inline; }
.toolbar { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-bottom: 1rem; }

.flash { border-radius: 8px; padding: 0.7rem 1rem; margin-bottom: 1rem; font-size: 0.92rem; }
.flash.ok { background: var(--ok-bg); color: var(--ok); }
.flash.err { background: var(--danger-bg); color: var(--danger); }
.flash.warn { background: var(--warn-bg); color: var(--warn); }

.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.85rem; word-break: break-all; }
.pre-wrap { white-space: pre-wrap; word-break: break-word; }

.server-block { display: flex; flex-direction: column; gap: 0.4rem; }
.protocol-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem 0.7rem;
  flex-wrap: wrap;
}
.protocol-row .proto { font-weight: 600; }

.creds-box {
  background: var(--warn-bg);
  border: 1px dashed var(--warn);
  border-radius: 8px;
  padding: 0.9rem;
  margin: 0.8rem 0;
}

.stat { font-size: 1.5rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: 0.82rem; }
