:root {
  color-scheme: dark;
  --bg: #07090c;
  --bg2: #10151c;
  --surface: #121820;
  --surface2: #1a222d;
  --ink: #e8eef5;
  --muted: #8b98a8;
  --accent: #3ecf8e;
  --accent-2: #2fb87a;
  --accent-soft: rgba(62, 207, 142, 0.14);
  --ok: #3ecf8e;
  --warn: #e0b34a;
  --danger: #f07178;
  --border: #243041;
  --border-strong: #3a4d63;
  --shadow: 0 1px 0 rgba(255, 255, 255, 0.03), 0 16px 40px rgba(0, 0, 0, 0.45);
  --radius: 12px;
  --font: "Sora", "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
  --touch: 44px;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-height: 100vh;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(900px 480px at 0% -10%, rgba(62, 207, 142, 0.12), transparent 55%),
    radial-gradient(700px 420px at 100% 0%, rgba(56, 120, 200, 0.1), transparent 50%),
    var(--bg);
  position: relative;
  -webkit-tap-highlight-color: transparent;
}

.bg-grid {
  pointer-events: none;
  position: fixed;
  inset: 0;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.03) 1px, transparent 1px);
  background-size: 28px 28px;
  mask-image: radial-gradient(ellipse at 50% 0%, #000 15%, transparent 70%);
  z-index: 0;
}

.top, .wrap { position: relative; z-index: 1; }

a { color: var(--accent); text-decoration: none; }
a:hover { color: #6bdbab; }

.top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1.1rem;
  border-bottom: 1px solid var(--border);
  background: rgba(7, 9, 12, 0.88);
  backdrop-filter: blur(12px);
  position: sticky;
  top: 0;
  z-index: 30;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  color: var(--ink);
  font-weight: 700;
  letter-spacing: -0.02em;
}
.brand-mark {
  width: 0.65rem;
  height: 0.65rem;
  background: var(--accent);
  transform: rotate(45deg);
}
.brand-text { color: var(--ink); }
.brand-text span { color: var(--accent); font-weight: 500; }
.nav-link { color: var(--muted); font-size: 0.92rem; font-weight: 500; }
.nav-link:hover { color: var(--ink); }

.wrap {
  max-width: 1180px;
  margin: 0 auto;
  padding: 1.25rem 1rem 5.5rem;
}

.page-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 0.85rem;
  margin-bottom: 1rem;
}
.page-head h1 {
  margin: 0;
  font-size: clamp(1.45rem, 4vw, 1.9rem);
  letter-spacing: -0.03em;
  line-height: 1.15;
}
.page-head .muted { margin: 0.3rem 0 0; font-size: 0.9rem; }
.eyebrow {
  margin: 0 0 0.35rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--accent);
}

.filters {
  display: grid;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
  padding: 0.85rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 0.75rem;
  align-items: center;
}
.filter-row-label {
  flex: 0 0 4.5rem;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  flex: 1;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.4rem 0.7rem;
  min-height: 36px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface2);
  color: var(--ink);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease, color 0.12s ease;
}
.chip:hover { border-color: var(--border-strong); }
.chip.is-active {
  background: var(--accent-soft);
  border-color: rgba(62, 207, 142, 0.55);
  color: #b8f5d4;
}
.chip-n {
  font-family: var(--mono);
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--muted);
  background: rgba(255,255,255,0.06);
  padding: 0.1rem 0.35rem;
  border-radius: 5px;
}
.chip.is-active .chip-n { color: #9be8c4; background: rgba(62, 207, 142, 0.15); }
.chip-online.is-active { background: rgba(61, 255, 154, 0.15); border-color: #3DFF9A; color: #3DFF9A; }
.chip-recent.is-active { background: rgba(255, 176, 32, 0.15); border-color: #FFB020; color: #FFB020; }
.chip-offline.is-active { background: rgba(198, 40, 40, 0.2); border-color: #ff6b6b; color: #ff8a8a; }
.filter-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  align-items: center;
  justify-content: space-between;
  padding-top: 0.35rem;
  border-top: 1px solid var(--border);
}

.stats {
  display: none;
}
.stat {
  display: none;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.85rem;
}

.search {
  position: relative;
  display: flex;
  align-items: center;
  flex: 1;
  min-width: min(100%, 240px);
  max-width: 560px;
}
.search-icon {
  position: absolute;
  left: 0.85rem;
  color: var(--muted);
  font-size: 1rem;
  pointer-events: none;
}
.search input[type="search"] {
  width: 100%;
  min-height: var(--touch);
  padding: 0.7rem 2.6rem 0.7rem 2.2rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface2);
  color: var(--ink);
  font-size: 16px; /* avoid iOS zoom */
}
.search input:focus {
  outline: 2px solid rgba(62, 207, 142, 0.3);
  border-color: var(--accent);
}
.search-kbd {
  position: absolute;
  right: 0.7rem;
  font-family: var(--mono);
  font-size: 0.72rem;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 0.1rem 0.35rem;
  background: var(--surface);
}
.search-clear {
  position: absolute;
  right: 0.55rem;
  color: var(--muted);
  font-size: 1.35rem;
  line-height: 1;
  border: 0;
  background: transparent;
  cursor: pointer;
  padding: 0.35rem 0.45rem;
  border-radius: 6px;
  min-width: 36px;
  min-height: 36px;
}
.search-clear:hover { background: var(--bg2); color: var(--ink); }
.search:has(.search-clear:not(.is-hidden)) .search-kbd { display: none; }
.result-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

input, select, button, textarea { font: inherit; color: var(--ink); }
input, select {
  width: 100%;
  padding: 0.7rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface2);
  color: var(--ink);
  font-size: 16px;
}
select.w-auto { width: auto; }
label {
  display: grid;
  gap: 0.35rem;
  font-size: 0.86rem;
  color: var(--muted);
  font-weight: 500;
}
.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.55rem 0.9rem;
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface2);
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
  font-weight: 500;
  transition: background 0.12s ease, border-color 0.12s ease;
}
.btn:hover { border-color: var(--border-strong); }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #06140e;
  font-weight: 600;
}
.btn.primary:hover { background: var(--accent-2); border-color: var(--accent-2); color: #04100b; }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; }
.btn.small { padding: 0.4rem 0.6rem; font-size: 0.82rem; min-height: 36px; }
.btn.warn { border-color: rgba(224, 179, 74, 0.45); color: var(--warn); background: rgba(224, 179, 74, 0.1); }
.btn.danger { border-color: rgba(240, 113, 120, 0.45); color: var(--danger); background: rgba(240, 113, 120, 0.1); }

.icon-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  min-width: 40px;
  min-height: 40px;
}
.icon-btn:hover { background: var(--surface2); color: var(--ink); }

.stack { display: grid; gap: 0.9rem; }
.row { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.row.center { justify-content: center; }
.wrap-gap { gap: 0.65rem; }
.inline { display: inline-flex; margin: 0; }

.table-wrap {
  overflow: auto;
  max-height: min(70vh, 820px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
table.clients { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.clients th, .clients td {
  padding: 0.72rem 0.9rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.clients th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #161d27;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.74rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.clients tbody tr { transition: background 0.12s ease; }
.clients tbody tr:hover { background: rgba(62, 207, 142, 0.05); }
.clients tbody tr.is-hidden { display: none; }
.clients tbody tr.is-clickable { cursor: pointer; }
.clients tbody tr.is-clickable:hover { background: rgba(62, 207, 142, 0.07); }
.th-actions { min-width: 280px; }

.client-name {
  font-weight: 600;
  color: var(--ink);
  letter-spacing: -0.01em;
  word-break: break-word;
}
.client-name:hover { color: var(--accent); }
.note-line {
  margin-top: 0.18rem;
  font-size: 0.78rem;
  color: var(--muted);
  max-width: 340px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ip { font-family: var(--mono); font-size: 0.82rem; color: #b7c4d4; }
.date-cell { font-variant-numeric: tabular-nums; color: var(--muted); font-weight: 600; }
.date-cell.is-expiring { color: #FFB020; }
.date-cell.is-expired { color: #ff6b6b; }
.meta dd.is-expiring { color: #FFB020; font-weight: 600; }
.meta dd.is-expired { color: #ff6b6b; font-weight: 600; }
.tiny { font-size: 0.8em; font-weight: 400; }
.actions { display: flex; flex-wrap: wrap; gap: 0.35rem; align-items: center; }
.mobile-meta { display: none; }

code { font-family: var(--mono); font-size: 0.85em; }
.break { word-break: break-all; }
.muted { color: var(--muted); }
.empty { padding: 2.5rem 1rem; text-align: center; color: var(--muted); }
.is-hidden { display: none !important; }

.link-pill {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.28rem 0.55rem 0.28rem 0.4rem;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  line-height: 1;
  white-space: nowrap;
  vertical-align: middle;
}
.link-mark {
  width: 0.7rem;
  height: 0.7rem;
  flex-shrink: 0;
  background: currentColor;
}
/* Shapes differ so status is clear even without color */
.link-online .link-mark { border-radius: 50%; }          /* круг */
.link-recent .link-mark { clip-path: polygon(50% 0, 100% 100%, 0 100%); } /* треугольник */
.link-offline .link-mark { border-radius: 2px; }         /* квадрат */

.link-online {
  color: #04140c;
  background: #3DFF9A;
  border-color: #1aff7a;
}
.link-recent {
  color: #1a1200;
  background: #FFB020;
  border-color: #ff9900;
}
.link-offline {
  color: #fff5f5;
  background: #C62828;
  border-color: #ff5252;
}

.link-label {
  font-size: 0.74rem;
  font-weight: 600;
}
.link-label.link-online { color: #3DFF9A; }
.link-label.link-recent { color: #FFB020; }
.link-label.link-offline { color: #ff6b6b; }

.name-with-dot {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
}
.mobile-only-dot { display: none; }
.th-link { width: 7.5rem; }
.td-link { width: 7.5rem; white-space: nowrap; }

.badge {
  display: inline-block;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  font-size: 0.74rem;
  font-weight: 600;
}
.badge-active { background: rgba(62, 207, 142, 0.16); color: #7ee0b0; }
.badge-expiring { background: rgba(224, 179, 74, 0.16); color: #f0d078; }
.badge-expired { background: rgba(240, 113, 120, 0.16); color: #f5a0a5; }
.badge-disabled { background: rgba(139, 152, 168, 0.16); color: #a8b6c6; }

.flashes { list-style: none; padding: 0; margin: 0 0 1rem; }
.flash {
  padding: 0.7rem 0.95rem;
  border-radius: 8px;
  margin-bottom: 0.4rem;
  animation: fadeUp 0.28s ease;
}
.flash-ok { background: rgba(62, 207, 142, 0.12); border: 1px solid rgba(62, 207, 142, 0.35); }
.flash-error { background: rgba(240, 113, 120, 0.12); border: 1px solid rgba(240, 113, 120, 0.35); }

.login-box {
  max-width: 400px;
  margin: 3.5rem auto;
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.login-box h1 { margin: 0 0 0.35rem; letter-spacing: -0.03em; }

dialog.sheet {
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  background: var(--surface);
  color: var(--ink);
  padding: 1.1rem 1.1rem calc(1.25rem + env(safe-area-inset-bottom));
  width: min(440px, 100vw);
  max-width: 100vw;
  box-shadow: var(--shadow);
  margin: auto auto 0;
}
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.65); }
.sheet-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.35rem;
}
.sheet-head h2 { margin: 0; font-size: 1.15rem; letter-spacing: -0.02em; }
.qr-sheet { text-align: center; }

.qr-box {
  margin: 0.5rem auto 1rem;
  text-align: center;
  padding: 1rem;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: #222;
  width: fit-content;
  max-width: 100%;
}
.qr-box img { display: block; margin: 0 auto; border-radius: 4px; max-width: min(280px, 70vw); height: auto; }
.qr-box figcaption { margin-top: 0.55rem; font-size: 0.85rem; color: var(--muted); }

.back { margin: 0 0 1rem; }
.detail {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 1.5rem;
  align-items: start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}
.detail-title {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.65rem;
  margin-bottom: 0.35rem;
}
.detail-title h1 {
  margin: 0;
  font-size: clamp(1.25rem, 4vw, 1.75rem);
  letter-spacing: -0.03em;
  word-break: break-word;
}
.meta {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.85rem;
  margin: 1.2rem 0 1.4rem;
  padding: 1rem;
  background: var(--bg2);
  border-radius: 10px;
}
.meta > div { display: grid; gap: 0.2rem; }
.meta .full { grid-column: 1 / -1; }
.meta dt {
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}
.meta dd { margin: 0; }
.action-groups { display: grid; gap: 1rem; }
.action-group {
  display: grid;
  gap: 0.45rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--border);
}
.group-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  font-weight: 600;
}
.detail-qr { justify-self: center; }

/* Floating create button on mobile */
.fab-create {
  display: none;
}

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

@media (max-width: 900px) {
  .detail { grid-template-columns: 1fr; }
  .meta { grid-template-columns: 1fr 1fr; }
  .th-actions { min-width: 0; }
  .filter-row-label { flex-basis: 100%; }
}

/* Mobile card layout */
@media (max-width: 760px) {
  .wrap { padding: 0.85rem 0.75rem 6rem; }

  .page-head {
    flex-direction: column;
    align-items: stretch;
    gap: 0.65rem;
    margin-bottom: 0.75rem;
  }
  .page-head .btn.primary { display: none; } /* use FAB */

  .fab-create {
    display: flex;
    position: fixed;
    right: 1rem;
    bottom: calc(1rem + env(safe-area-inset-bottom));
    z-index: 40;
    min-width: 56px;
    min-height: 56px;
    padding: 0 1.15rem;
    border-radius: 16px;
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
  }

  .filters { padding: 0.75rem; gap: 0.65rem; }
  .filter-chips { gap: 0.35rem; }
  .chip { padding: 0.35rem 0.55rem; font-size: 0.78rem; }

  .toolbar {
    position: sticky;
    top: 52px;
    z-index: 25;
    margin: 0 -0.75rem 0.75rem;
    padding: 0.55rem 0.75rem;
    background: rgba(7, 9, 12, 0.92);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
  }
  .search { max-width: none; width: 100%; }
  .search-kbd { display: none; }
  .result-meta { width: 100%; font-size: 0.8rem; }

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

  .table-wrap {
    max-height: none;
    overflow: visible;
    border: 0;
    background: transparent;
    box-shadow: none;
    border-radius: 0;
  }

  table.clients,
  .clients tbody {
    display: block;
    width: 100%;
  }
  .clients thead { display: none; }
  .clients tbody tr.client-row {
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
    margin-bottom: 0.65rem;
    padding: 0.9rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
  }
  .clients tbody tr.client-row.is-hidden { display: none; }
  .clients td {
    border: 0;
    padding: 0;
    width: 100%;
  }

  /* On mobile show name cell with meta; hide link/status/date cols (duplicated in meta) */
  .clients td:nth-child(1),
  .clients td:nth-child(3),
  .clients td:nth-child(4) {
    display: none;
  }
  .mobile-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.4rem 0.55rem;
    margin-top: 0.4rem;
  }
  .note-line {
    max-width: 100%;
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  dialog.sheet {
    width: 100vw;
    max-width: 100vw;
    border-radius: 16px 16px 0 0;
  }

  .detail { padding: 1rem; }
  .action-group .row .btn,
  .action-group form,
  .action-group a.btn {
    flex: 1 1 calc(50% - 0.4rem);
    min-height: var(--touch);
  }
  .action-group select {
    min-height: var(--touch);
    flex: 1 1 100%;
  }
}

@media (min-width: 761px) {
  dialog.sheet {
    border-radius: var(--radius);
    margin: auto;
  }
}
