:root {
  --bg: #f4f6f8;
  --card-bg: #ffffff;
  --border: #e2e5e9;
  --text: #1f2933;
  --text-muted: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --success: #16a34a;
  --warn: #d97706;
  --accent: #7c3aed;
  --accent2: #0891b2;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: linear-gradient(160deg, #eef2ff 0%, #f4f6f8 35%, #f4f6f8 100%);
  color: var(--text);
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 24px;
  background: var(--card-bg);
  border-bottom: 3px solid transparent;
  border-image: linear-gradient(90deg, var(--primary), var(--accent), var(--accent2)) 1;
}

.brand {
  font-weight: 700;
  font-size: 18px;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.topbar-right { display: flex; align-items: center; gap: 16px; }

.lang-switch { display: flex; gap: 4px; }

.lang-btn {
  border: 1px solid var(--border);
  background: transparent;
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-muted);
}

.lang-btn.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

main { padding: 40px 20px; display: flex; justify-content: center; }

.view.hidden, .hidden { display: none !important; }

.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-top: 4px solid var(--primary);
  border-radius: 12px;
  padding: 32px;
  box-shadow: 0 4px 20px rgba(37, 99, 235, 0.06);
}

.card.narrow { width: 100%; max-width: 380px; }
.card.wide { width: 100%; max-width: 1100px; }

.card h1 { margin-top: 0; font-size: 22px; }

form { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }

label { font-size: 13px; color: var(--text-muted); margin-top: 8px; }

input[type=text], input[type=password], input[type=number], select, textarea {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  width: 100%;
  background: var(--card-bg);
  color: var(--text);
}

textarea { font-family: monospace; }

.btn {
  padding: 10px 16px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
}

.btn-primary { background: var(--primary); color: white; margin-top: 12px; }
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary { background: #eef2ff; color: var(--primary); }
.btn-accent { background: #f3e8ff; color: var(--accent); }
.btn-accent2 { background: #e0f7fa; color: var(--accent2); }

.btn-ghost { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-cancel { background: #fca5a5; color: #7f1d1d; }
.btn-cancel:hover { background: #f87171; }

.btn-small {
  padding: 0 12px;
  font-size: 12px;
  margin-right: 6px;
  height: 38px;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
}

.row-actions {
  position: relative;
  display: inline-block;
}
.row-actions-btn {
  font-size: 18px;
  line-height: 1;
  padding: 6px 10px;
}
.row-actions-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  z-index: 50;
  display: flex;
  flex-direction: column;
  min-width: 170px;
  padding: 4px;
}
.row-menu-item {
  text-align: left;
  width: 100%;
  border: none;
  background: none;
  padding: 8px 10px;
  font-size: 13px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
}
.row-menu-item:hover {
  background: var(--bg);
}
.row-menu-item-danger {
  color: var(--danger);
}

.btn-download { background: #14b8a6; color: white; }
.btn-revoke { background: var(--warn); color: white; }
.btn-warn { background: var(--warn); color: white; }
.btn-danger { background: var(--danger); color: white; }
.btn-routes { background: var(--primary); color: white; }

.error { color: var(--danger); font-size: 13px; min-height: 18px; }

.info { color: var(--success); font-size: 14px; margin: 12px 0; }

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

.tab-bar {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 20px;
}

.tab-btn {
  border: none;
  background: transparent;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}

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

.tab-btn.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.tab-panel.hidden { display: none; }

.box-table th:nth-child(1), .box-table td:nth-child(1) { width: 34%; }

.box-name {
  font-weight: 600;
  color: var(--primary);
  text-decoration: underline;
  cursor: default;
  margin-bottom: 6px;
}

.box-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.box-btn {
  margin-right: 0;
  color: white;
}

.box-btn-online { background: var(--success); }
.box-btn-warn { background: var(--warn); }
.box-btn-offline { background: #9ca3af; }

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

.dashboard-actions { display: flex; gap: 10px; align-items: center; }

.settings-section {
  padding: 14px 0;
  border-bottom: 1px solid var(--border);
}
.settings-section:last-of-type {
  border-bottom: none;
}
.settings-menu-btn {
  width: 100%;
  text-align: left;
}
.settings-system-buttons {
  display: flex;
  gap: 10px;
  margin-top: 8px;
}
.username-view {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.username-edit-form {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.username-edit-form input {
  padding: 6px 8px;
  font-size: 13px;
}
.username-edit-actions {
  display: flex;
  gap: 6px;
}
.username-edit-actions .btn { margin-top: 0; }

#searchInput { min-width: 220px; }

.refresh-countdown {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

.client-table { width: 100%; border-collapse: collapse; }

.table-scroll {
  overflow-x: auto;
  border-radius: 8px;
}
.client-table th, .client-table td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  vertical-align: top;
}
.client-table th { color: var(--text-muted); font-weight: 600; font-size: 13px; }

.routes-cell {
  max-width: 180px;
  line-height: 1.6;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge-active { background: #dcfce7; color: var(--success); }
.badge-revoked { background: #fee2e2; color: var(--danger); }
.badge-expired { background: #fef3c7; color: var(--warn); }

.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.dot-on { background: var(--success); }
.dot-off { background: #d1d5db; }
.dot-warn { background: var(--warn); }

.switch {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 20px;
  vertical-align: middle;
}
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
.switch .slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #d1d5db;
  transition: 0.2s;
  border-radius: 999px;
}
.switch .slider::before {
  position: absolute;
  content: "";
  height: 14px;
  width: 14px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: 0.2s;
  border-radius: 50%;
}
.switch input:checked + .slider {
  background-color: var(--success);
}
.switch input:checked + .slider::before {
  transform: translateX(16px);
}
.switch input:disabled + .slider {
  opacity: 0.6;
  cursor: not-allowed;
}

.toast {
  padding: 10px 14px;
  border-radius: 8px;
  background: #dcfce7;
  color: var(--success);
  font-size: 14px;
  margin-bottom: 16px;
}
.toast.error { background: #fee2e2; color: var(--danger); }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-content {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 420px;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-content h2 { margin-top: 0; font-size: 18px; }

.modal-content-wide {
  max-width: 880px;
}
.modal-content-wide h3 {
  font-size: 15px;
  margin-top: 20px;
  margin-bottom: 4px;
  border-top: 1px solid var(--border);
  padding-top: 16px;
}
.modal-content-wide select {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  width: 100%;
}
.modal-content-wide .client-table {
  margin-bottom: 8px;
  table-layout: fixed;
}
.modal-content-wide .client-table th,
.modal-content-wide .client-table td {
  font-size: 13px;
  padding: 8px 10px;
  overflow-wrap: break-word;
}
#usersTable th:nth-child(1), #usersTable td:nth-child(1) { width: 18%; }
#usersTable th:nth-child(2), #usersTable td:nth-child(2) { width: 20%; }
#usersTable th:nth-child(3), #usersTable td:nth-child(3) { width: 19%; }
#usersTable th:nth-child(4), #usersTable td:nth-child(4) { width: 21%; }
#usersTable th:nth-child(5), #usersTable td:nth-child(5) { width: 22%; }
.user-role-select,
.user-totp-select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  width: auto;
  min-width: 140px;
  height: 38px;
  padding: 8px 30px 8px 10px;
  font-size: 13px;
  line-height: 1.2;
  box-sizing: border-box;
  background-image: url("data:image/svg+xml;charset=UTF-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='%236b7280'%3E%3Cpath fill-rule='evenodd' d='M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z' clip-rule='evenodd'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 16px;
}

.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.modal-actions .btn { margin-top: 0; }

.hint { color: var(--text-muted); font-size: 13px; }

.qr-image {
  display: block;
  width: 200px;
  height: 200px;
  margin: 12px auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  background: white;
}

.checkbox-label {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 14px;
  color: var(--text);
  margin-top: 14px;
  cursor: pointer;
}
.checkbox-label input[type=checkbox] {
  margin-top: 3px;
}

.radio-label {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 14px;
  color: var(--text);
  margin-top: 10px;
  cursor: pointer;
}
.radio-label input[type=radio] {
  margin-top: 3px;
}

.checkbox-list {
  margin-top: 10px;
  margin-bottom: 4px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 220px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.checkbox-list .checkbox-label {
  margin-top: 0;
}

.plain-list {
  margin: 10px 0;
  padding: 10px 12px 10px 28px;
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 220px;
  overflow-y: auto;
  font-size: 14px;
}
.plain-list li {
  padding: 2px 0;
}

.password-field {
  position: relative;
}
.password-field input {
  padding-right: 38px;
}
.password-toggle-btn {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.password-toggle-btn:hover {
  color: var(--text);
}
