:root {
  --bg: #f4f6f9;
  --bg-elev: #eef1f6;
  --bg-elev2: #e7ebf1;
  --panel: #ffffff;
  --border: #e0e4ea;
  --border-soft: #eef1f5;
  --text: #1a1e27;
  --text-dim: #5b6472;
  --text-faint: #8b94a3;
  --accent: #3b7bff;
  --accent-2: #7c5cff;
  --accent-grad: linear-gradient(135deg, #3b7bff 0%, #7c5cff 100%);
  --green: #0fa968;
  --red: #e5484d;
  --amber: #d98a00;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 12px 34px rgba(24, 39, 75, 0.12);
  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

.hidden { display: none !important; }
.muted { color: var(--text-dim); font-size: 13px; }

/* ---------- buttons ---------- */
.btn {
  font-family: inherit;
  font-weight: 600;
  font-size: 14px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  cursor: pointer;
  transition: all .15s ease;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
  background: var(--bg-elev2);
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn-primary { background: var(--accent-grad); color: #fff; border: none; box-shadow: 0 4px 18px rgba(79,140,255,.3); }
.btn-primary:hover { box-shadow: 0 6px 24px rgba(79,140,255,.45); }
.btn-ghost { background: transparent; border: 1px solid var(--border); color: var(--text-dim); }
.btn-ghost:hover { color: var(--text); border-color: #c3cad4; background: var(--bg-elev2); }
.btn-danger { background: transparent; border: 1px solid rgba(248,113,113,.35); color: var(--red); }
.btn-danger:hover { background: rgba(248,113,113,.12); }
.btn-sm { padding: 7px 12px; font-size: 13px; }
.btn-block { width: 100%; justify-content: center; padding: 12px; font-size: 15px; }
.btn:disabled { opacity: .5; cursor: not-allowed; transform: none; }

/* ---------- login ---------- */
.login-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(1200px 600px at 70% -10%, rgba(124,92,255,.18), transparent 60%),
    radial-gradient(900px 500px at 10% 110%, rgba(79,140,255,.15), transparent 55%),
    var(--bg);
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 36px 32px;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 14px; margin-bottom: 28px; }
.brand-mark {
  width: 46px; height: 46px;
  border-radius: 12px;
  background: var(--accent-grad);
  display: grid; place-items: center;
  font-weight: 800; font-size: 24px; color: #fff;
  box-shadow: 0 6px 20px rgba(124,92,255,.35);
}
.brand-mark.sm { width: 30px; height: 30px; font-size: 16px; border-radius: 8px; }
.brand h1 { font-size: 18px; font-weight: 700; letter-spacing: -.2px; }
.brand p { font-size: 13px; color: var(--text-dim); margin-top: 2px; }

.form { display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 7px; }
.field span { font-size: 13px; color: var(--text-dim); font-weight: 500; }
.field input, .field select, .field textarea {
  font-family: inherit;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 11px 13px;
  color: var(--text);
  font-size: 14px;
  transition: border-color .15s, box-shadow .15s;
  width: 100%;
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79,140,255,.15);
}
.form-error {
  background: rgba(248,113,113,.1);
  border: 1px solid rgba(248,113,113,.3);
  color: var(--red);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
}
.login-foot { text-align: center; color: var(--text-faint); font-size: 12px; margin-top: 22px; }

/* ---------- topbar ---------- */
.topbar {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 32px;
  border-bottom: 1px solid var(--border);
  background: rgba(255,255,255,.75);
  backdrop-filter: blur(12px);
  position: sticky; top: 0; z-index: 50;
}
.topbar-left { display: flex; align-items: center; gap: 12px; }
.topbar-title { font-weight: 700; font-size: 15px; }
.crumb { color: var(--text-dim); font-size: 14px; }
.crumb::before { content: "/"; margin-right: 12px; color: var(--text-faint); }
.topbar-right { display: flex; align-items: center; gap: 14px; }
.whoami { color: var(--text-dim); font-size: 13px; }

.main { width: 100%; margin: 0 auto; padding: 28px 32px 60px; }

/* ---------- page head ---------- */
.page-head {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 16px; margin-bottom: 22px; flex-wrap: wrap;
}
.page-head h2 { font-size: 22px; font-weight: 700; letter-spacing: -.3px; }
.page-head-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.search input {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  color: var(--text);
  font-size: 14px;
  width: 260px;
  font-family: inherit;
}
.search input:focus { outline: none; border-color: var(--accent); }
.toggle { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-dim); cursor: pointer; user-select: none; }
.toggle input { width: 16px; height: 16px; accent-color: var(--accent); }

/* ---------- clients grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}
.client-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  transition: all .18s ease;
  position: relative;
  overflow: hidden;
}
.client-card.active { cursor: pointer; }
.client-card.active:hover {
  border-color: #b7c6ff;
  transform: translateY(-3px);
  box-shadow: 0 12px 32px rgba(0,0,0,.4);
}
.client-card.active::before {
  content: "";
  position: absolute; left: 0; top: 0; bottom: 0; width: 3px;
  background: var(--accent-grad);
}
.client-card.disabled { opacity: .55; }
.cc-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 12px; }
.cc-name { font-weight: 700; font-size: 15px; line-height: 1.35; }
.cc-id { color: var(--text-faint); font-size: 12px; margin-top: 3px; }
.cc-rows { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.cc-row { display: flex; gap: 8px; font-size: 13px; color: var(--text-dim); align-items: center; }
.cc-row svg { flex-shrink: 0; opacity: .6; }
.cc-foot { display: flex; justify-content: space-between; align-items: center; padding-top: 12px; border-top: 1px solid var(--border-soft); }
.cc-date { font-size: 12px; color: var(--text-faint); }

.badge {
  font-size: 11px; font-weight: 600;
  padding: 4px 9px; border-radius: 20px;
  display: inline-flex; align-items: center; gap: 5px;
}
.badge-green { background: rgba(52,211,153,.13); color: var(--green); }
.badge-gray { background: rgba(102,112,133,.15); color: var(--text-faint); }
.badge-blue { background: rgba(79,140,255,.13); color: var(--accent); }
.badge-amber { background: rgba(251,191,36,.13); color: var(--amber); }
.badge-red { background: rgba(248,113,113,.13); color: var(--red); }
.dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

/* ---------- detail ---------- */
.detail-top { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.detail-title h2 { font-size: 22px; font-weight: 700; }
.detail-title a { color: var(--accent); font-size: 13px; text-decoration: none; }
.detail-title a:hover { text-decoration: underline; }

.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 26px; }
.stat {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
}
.stat .lbl { font-size: 12px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .5px; font-weight: 600; }
.stat .val { font-size: 26px; font-weight: 700; margin-top: 8px; letter-spacing: -.5px; }
.stat .sub { font-size: 12px; color: var(--text-faint); margin-top: 4px; }
.stat.action { display: flex; flex-direction: column; justify-content: space-between; }
.stat .stat-head { display: flex; justify-content: space-between; align-items: start; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.panel-head h3 { font-size: 16px; font-weight: 700; }
.panel-head .muted { margin-top: 2px; }

/* ---------- table ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
thead th {
  text-align: left; padding: 12px 16px;
  color: var(--text-dim); font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: .4px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  background: var(--bg-elev);
}
tbody td { padding: 13px 16px; border-bottom: 1px solid var(--border-soft); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: rgba(20,40,90,.03); }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; color: var(--text-dim); }
.printer-cell { color: var(--text-dim); font-size: 12.5px; }
.printer-cell.empty { color: var(--text-faint); font-style: italic; }
.row-actions { display: flex; gap: 8px; justify-content: flex-end; }
.icon-btn {
  background: var(--bg-elev2); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 10px; cursor: pointer;
  color: var(--text-dim); font-size: 12px; font-weight: 600;
  transition: all .15s; display: inline-flex; gap: 5px; align-items: center;
}
.icon-btn:hover { color: var(--text); border-color: #c3cad4; }
.icon-btn.disabled, .icon-btn:disabled { opacity: .4; cursor: not-allowed; }
.icon-btn.disabled:hover, .icon-btn:disabled:hover { color: var(--text-dim); border-color: var(--border); }

/* clients table */
.clients-table tbody tr:hover { background: rgba(20,40,90,.03); }
.tunnel-link {
  color: var(--accent); text-decoration: none; font-size: 12.5px;
  max-width: 280px; display: inline-block; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; vertical-align: middle;
}
.tunnel-link:hover { text-decoration: underline; }
.icon-btn.danger:hover { color: var(--red); border-color: rgba(248,113,113,.4); background: rgba(248,113,113,.08); }

/* ---------- modal ---------- */
.modal-root { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.6); backdrop-filter: blur(3px); }
.modal {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  width: 100%;
  max-width: 480px;
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
  animation: modalIn .18s ease;
}
.modal.wide { max-width: 640px; }
@keyframes modalIn { from { opacity: 0; transform: translateY(12px) scale(.98); } to { opacity: 1; transform: none; } }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 20px 24px; border-bottom: 1px solid var(--border); }
.modal-head h3 { font-size: 17px; font-weight: 700; }
.modal-head p { font-size: 13px; color: var(--text-dim); margin-top: 3px; }
.modal-x { background: none; border: none; color: var(--text-faint); font-size: 22px; cursor: pointer; line-height: 1; padding: 0 4px; }
.modal-x:hover { color: var(--text); }
.modal-body { padding: 22px 24px; display: flex; flex-direction: column; gap: 16px; }
.modal-foot { padding: 16px 24px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }

/* ---------- misc ---------- */
.loading { text-align: center; padding: 60px 20px; color: var(--text-dim); }
.spinner {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid var(--border); border-top-color: var(--accent);
  animation: spin .7s linear infinite; margin: 0 auto 14px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.empty-state { text-align: center; padding: 50px 20px; color: var(--text-faint); }
.empty-state h4 { color: var(--text-dim); font-size: 15px; margin-bottom: 6px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg-elev2); border: 1px solid var(--border);
  padding: 12px 20px; border-radius: 12px; font-size: 14px;
  box-shadow: var(--shadow); z-index: 200; max-width: 90vw;
  animation: toastIn .2s ease;
}
.toast.ok { border-color: rgba(52,211,153,.4); }
.toast.err { border-color: rgba(248,113,113,.4); }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

.inline-spin { width: 15px; height: 15px; border: 2px solid rgba(255,255,255,.35); border-top-color:#fff; border-radius:50%; display:inline-block; animation: spin .7s linear infinite; }

@media (max-width: 620px) {
  .search input { width: 100%; }
  .form-grid { grid-template-columns: 1fr; }
  .main { padding: 20px 16px 50px; }
}
