:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e2e5eb;
  --text: #1c2430;
  --text-muted: #667085;
  --brand: #2f5fda;
  --brand-dark: #22409c;
  --danger: #d64545;
  --danger-dark: #b23636;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
}

* { box-sizing: border-box; }

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

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

.container {
  width: 100%;
  margin: 0 auto;
  padding: 32px 40px 64px;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-inner {
  width: 100%;
  margin: 0 auto;
  padding: 14px 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  font-size: 1.05rem;
}
.brand span {
  font-weight: 400;
  color: var(--text-muted);
  margin-left: 6px;
  font-size: 0.9rem;
}
.brand .app-version {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.72rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 8px;
  margin-left: 8px;
}

.topnav {
  display: flex;
  align-items: center;
  gap: 18px;
  font-size: 0.92rem;
}
.current-user { color: var(--text-muted); }

.user-menu {
  position: relative;
}
.user-menu summary {
  cursor: pointer;
  list-style: none;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  color: var(--text);
  font-weight: 600;
}
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu summary::after { content: ' ▾'; color: var(--text-muted); }
.user-menu summary:hover { background: #eef0f4; }
.user-menu[open] summary { background: #eef0f4; }

.user-menu-dropdown {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 6px;
  display: flex;
  flex-direction: column;
  min-width: 160px;
  z-index: 10;
}
.user-menu-dropdown a,
.user-menu-dropdown .link-button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border-radius: 6px;
  color: var(--text);
  font-weight: 500;
}
.user-menu-dropdown a:hover,
.user-menu-dropdown .link-button:hover {
  background: #eef0f4;
  text-decoration: none;
}
.user-menu-dropdown .inline-form { display: block; width: 100%; }
.user-menu-dropdown .link-button.danger,
.user-menu-dropdown form .link-button { color: var(--danger); }

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.page-header h1 { margin: 4px 0 0; font-size: 1.5rem; }
.page-actions { display: flex; gap: 10px; }

.back-link {
  display: inline-block;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}

.card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(16, 24, 40, 0.12);
  text-decoration: none;
}
.card h2 { margin: 0 0 6px; font-size: 1.05rem; color: var(--text); }
.card-meta { margin: 0 0 4px; font-size: 0.85rem; color: var(--brand-dark); font-weight: 600; }
.card-status { margin: 0 0 4px; font-size: 0.78rem; font-weight: 700; }
.card-status-online { background: #ecfdf3; border-color: #86efac; }
.card-status-online .card-status { color: #166534; }
.card-status-partial { background: #fffbeb; border-color: #fcd34d; }
.card-status-partial .card-status { color: #92400e; }
.card-status-offline { background: #fef2f2; border-color: #fca5a5; }
.card-status-offline .card-status { color: #b91c1c; }
.card-notes { margin: 0; font-size: 0.85rem; color: var(--text-muted); }

.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 48px 0;
}

.workspace {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 20px;
  align-items: start;
}
@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; }
}

.rb-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 75vh;
  overflow-y: auto;
  padding-right: 4px;
}

.rb-list-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.rb-device-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-left: 22px;
  padding-left: 10px;
  border-left: 2px solid var(--border);
}

.rb-device-item {
  display: flex;
  flex-direction: column;
  gap: 1px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 10px;
  color: var(--text);
}
.rb-device-item:hover { background: #eef1f6; text-decoration: none; }
.rb-device-item strong { font-size: 0.8rem; }
.rb-device-item span { font-size: 0.72rem; color: var(--text-muted); }

.rb-list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid transparent;
  border-radius: 8px;
  padding: 6px 6px 6px 14px;
}
.rb-list-item:hover { background: #eef1f6; }
.rb-list-item.active {
  border-left-color: var(--brand);
  background: #eef1fb;
}
.rb-list-item.rb-status-online {
  background: #ecfdf3;
  border-color: #86efac;
  border-left-color: #22c55e;
}
.rb-list-item.rb-status-offline {
  background: #fef2f2;
  border-color: #fca5a5;
  border-left-color: var(--danger);
}
.rb-list-item.rb-status-online.active { background: #dcfce7; }
.rb-list-item.rb-status-offline.active { background: #fee2e2; }

.rb-list-item-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  min-width: 0;
  color: var(--text);
  padding: 4px 0;
}
.rb-list-item-main:hover { text-decoration: none; }
.rb-list-item-main strong { font-size: 0.95rem; }
.rb-list-item-main span { font-size: 0.82rem; color: var(--text-muted); }

.rb-list-item-edit {
  flex-shrink: 0;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  padding: 5px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
}
.rb-list-item-edit:hover {
  background: var(--surface);
  color: var(--brand);
  text-decoration: none;
}

.rb-detail {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
  min-height: 200px;
}

.rb-detail-empty {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 200px;
  color: var(--text-muted);
  text-align: center;
}

.rb-detail-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.rb-detail-header h2 { margin: 0; font-size: 1.3rem; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 14px;
  margin: 0 0 18px;
}
.detail-grid dt {
  font-size: 0.78rem;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.detail-grid dd { margin: 2px 0 0; font-size: 0.95rem; }

.detail-notes {
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}
.detail-notes h3 { margin: 0 0 6px; font-size: 0.95rem; }
.detail-notes p { margin: 0; color: var(--text-muted); }

.edit-columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  align-items: start;
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}
@media (max-width: 1100px) {
  .edit-columns { grid-template-columns: 1fr; }
}

.wireguard-section h3,
.devices-section h3 { margin: 0 0 10px; font-size: 0.95rem; }
.wireguard-section { min-width: 0; }

.devices-section { min-width: 0; }

.device-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.device-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
}
.device-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.device-main strong { font-size: 0.93rem; }
.device-main span { font-size: 0.82rem; color: var(--text-muted); }

.device-warn {
  align-self: flex-start;
  margin-top: 3px;
  font-size: 0.74rem;
  font-weight: 600;
  color: #92400e;
  background: #fef3c7;
  border: 1px solid #fde68a;
  border-radius: 999px;
  padding: 1px 8px;
}

.hint-text { color: var(--text-muted); font-size: 0.85rem; margin: 0 0 12px; }

.field-label {
  display: block;
  font-size: 0.88rem;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 4px;
}

.lan-block-section {
  border-top: 1px solid var(--border);
  padding-top: 16px;
  margin-top: 4px;
}
.lan-block-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  max-width: 480px;
}
.lan-block-row input { flex: 1; }
.lan-block-remove {
  background: #eef0f4;
  color: var(--text-muted);
  font-weight: 600;
  white-space: nowrap;
}
.lan-block-remove:hover { background: #e3e6ec; color: var(--danger); }

.danger-zone {
  margin-top: 24px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}

textarea.mono {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 0.82rem;
  white-space: pre;
}

.monitoring-placeholder {
  margin-top: 18px;
  padding: 14px 16px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  color: var(--text-muted);
}
.monitoring-placeholder h3 { margin: 0 0 4px; font-size: 0.9rem; color: var(--text); }
.monitoring-placeholder p { margin: 0; font-size: 0.85rem; }

.monitoring-section {
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
}
.monitoring-section h3 { margin: 0 0 12px; font-size: 0.95rem; }

.status-line {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-weight: 700;
  font-size: 0.82rem;
  letter-spacing: 0.04em;
  padding: 5px 13px;
  border-radius: 999px;
  border: 1px solid transparent;
}
.status-badge::before {
  content: '';
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: currentColor;
}
.status-badge.up {
  color: #1c7a3f;
  background: #e9f8ef;
  border-color: #bfe8cd;
}
.status-badge.down {
  color: var(--danger-dark);
  background: #fdecec;
  border-color: #f5c6c6;
}
.status-detail { color: var(--text-muted); font-size: 0.87rem; }

.rb-info-block {
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.rb-info-block h4 {
  margin: 0 0 10px;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
}
.rb-info-block .hint-text { margin: 0; }

.notes-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  color: var(--text-muted);
  margin-bottom: 20px;
}

details.new-form {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  margin-bottom: 20px;
}
details.new-form summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--brand);
}
details.new-form .stacked-form { margin-top: 16px; }

.stacked-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-width: 480px;
}
.grid-form {
  max-width: none;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}
.grid-form .full-width { grid-column: 1 / -1; }

label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.88rem;
  color: var(--text-muted);
  font-weight: 600;
}

input[type="text"],
input[type="email"],
input[type="password"],
textarea {
  font: inherit;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  background: #fff;
}
input:focus, textarea:focus {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
}

.password-row {
  display: flex;
  gap: 8px;
}
.password-row input { flex: 1; }

button, .button {
  font: inherit;
  font-weight: 600;
  border: none;
  border-radius: 8px;
  padding: 9px 16px;
  cursor: pointer;
  background: var(--brand);
  color: #fff;
  display: inline-block;
  text-align: center;
}
button:hover, .button:hover { background: var(--brand-dark); text-decoration: none; }

.button.secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
.button.secondary:hover { background: #eef0f4; }

.button.danger, button.danger { background: var(--danger); }
.button.danger:hover, button.danger:hover { background: var(--danger-dark); }

.password-row button {
  background: #eef0f4;
  color: var(--text);
  font-weight: 600;
  white-space: nowrap;
}
.password-row button:hover { background: #dfe3ea; }

.inline-form { display: inline-flex; margin: 0; }

.link-button {
  background: none;
  border: none;
  color: var(--brand);
  font-weight: 600;
  cursor: pointer;
  padding: 0;
}
.link-button:hover { text-decoration: underline; background: none; }
.link-button.danger { color: var(--danger); }

.auth-card {
  max-width: 360px;
  margin: 64px auto 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  box-shadow: var(--shadow);
}
.auth-card h1 { margin-top: 0; font-size: 1.3rem; }

.alert {
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 0.88rem;
}
.alert.error {
  background: #fdecec;
  color: var(--danger-dark);
  border: 1px solid #f5c6c6;
}
.alert.success {
  background: #e9f8ef;
  color: #1c7a3f;
  border: 1px solid #bfe8cd;
}

.account-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 20px;
  align-items: start;
  max-width: 780px;
}
.account-grid h2 { margin: 0 0 16px; font-size: 1.05rem; }
input:disabled { background: #f1f2f5; color: var(--text-muted); }

.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.table th, .table td {
  text-align: left;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.table th { color: var(--text-muted); font-weight: 600; background: #f8f9fb; }
.table tr:last-child td { border-bottom: none; }
.table-actions { text-align: right; }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151c;
    --surface: #1a1e28;
    --border: #2a2f3a;
    --text: #e7e9ee;
    --text-muted: #939aab;
    --brand: #6c8fff;
    --brand-dark: #8aa4ff;
  }
  input[type="text"], input[type="email"], input[type="password"], textarea {
    background: #12151c;
    color: var(--text);
  }
  .button.secondary:hover, .password-row button:hover { background: #262b36; }
  .password-row button { background: #232833; color: var(--text); }
  .table th { background: #1f2430; }
  .alert.error { background: #3a1f22; border-color: #5c2b2f; color: #ff9c9c; }
  .alert.success { background: #1c3324; border-color: #2c4f39; color: #8fe3ac; }
  input:disabled { background: #1c2028; color: var(--text-muted); }
  .rb-list-item:hover { background: #232833; }
  .rb-list-item.active { background: #202940; }
  .rb-list-item.rb-status-online { background: #1c3324; border-color: #2c4f39; border-left-color: #8fe3ac; }
  .rb-list-item.rb-status-offline { background: #3a1f22; border-color: #5c2b2f; border-left-color: #ff9c9c; }
  .rb-list-item.rb-status-online.active { background: #24462f; }
  .rb-list-item.rb-status-offline.active { background: #49252a; }
  .rb-device-item:hover { background: #232833; }
  .user-menu summary:hover, .user-menu[open] summary { background: #232833; }
  .user-menu-dropdown a:hover, .user-menu-dropdown .link-button:hover { background: #232833; }
  .lan-block-remove { background: #232833; color: var(--text-muted); }
  .lan-block-remove:hover { background: #2c323f; color: #ff9c9c; }
  .status-badge.up { color: #8fe3ac; background: #1c3324; border-color: #2c4f39; }
  .status-badge.down { color: #ff9c9c; background: #3a1f22; border-color: #5c2b2f; }
  .device-warn { color: #fcd34d; background: #3a2f14; border-color: #5c4a1f; }
}
