:root {
  color-scheme: light dark;
  --bg: #0f1419;
  --card: #1a2332;
  --text: #e7ecf3;
  --muted: #9aa7b8;
  --accent: #3b82f6;
  --danger: #ef4444;
  --border: #2a3648;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
}

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

a, button {
  -webkit-tap-highlight-color: transparent;
}

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

.container {
  max-width: 720px;
  margin: 0 auto;
  padding: 1rem;
}

.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  margin-bottom: 1rem;
  position: sticky;
  top: 0;
  z-index: 10;
}

.topbar h1 {
  margin: 0.25rem 0;
  font-size: 1.5rem;
  word-break: break-word;
}
.breadcrumb { margin: 0; font-size: 0.9rem; }
.muted { color: var(--muted); }

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

.grid {
  display: grid;
  gap: 0.75rem;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
}

.server-card {
  display: block;
  padding: 1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: #121a24;
  color: inherit;
}

.server-card:hover {
  border-color: var(--accent);
  text-decoration: none;
}

.server-title { font-weight: 600; }
.server-meta { color: var(--muted); font-size: 0.9rem; margin-top: 0.35rem; }

.server-status {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.5rem;
  font-size: 0.9rem;
}

.server-status--header {
  margin: 0.25rem 0;
}

.status-dot {
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-dot--active { background: #4ade80; box-shadow: 0 0 6px #4ade8080; }
.status-dot--offline { background: #f87171; box-shadow: 0 0 6px #f8717180; }
.status-dot--unknown { background: #9aa7b8; }

.status-label { font-weight: 600; }
.status-label:has(+ .muted) { color: var(--text); }

.server-card--active { border-color: #166534; }
.server-card--offline { border-color: #7f1d1d; opacity: 0.92; }

.device-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.device-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  flex-wrap: wrap;
  border-bottom: 1px solid var(--border);
  padding: 0.5rem 0;
}

.device-list li:last-child { border-bottom: none; }

.device-list li a:first-child {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: inherit;
  text-decoration: none;
}

.device-list li a:first-child span:first-of-type {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.device-actions {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
}

.inline-delete {
  display: inline;
  margin: 0;
}

.inline-delete button {
  cursor: pointer;
}

.badge {
  font-size: 0.75rem;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  margin-left: auto;
}

.badge.online {
  background: #14532d;
  color: #86efac;
}

.inline-form {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

input[type="text"],
input[type="password"] {
  flex: 1;
  min-width: 180px;
  padding: 0.6rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #0f1419;
  color: var(--text);
}

button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem 1rem;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: white;
  cursor: pointer;
  font-size: 0.95rem;
  min-height: 44px;
}

.btn.primary { text-align: center; }
.btn.danger, button.danger { background: var(--danger); }

.actions {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  align-items: center;
}

.qr-image {
  display: block;
  max-width: 280px;
  width: 100%;
  margin: 0 auto;
  background: white;
  padding: 0.75rem;
  border-radius: 8px;
}

.hints ul { margin: 0.5rem 0 0; padding-left: 1.2rem; }
.footer-note { text-align: center; color: var(--muted); }
.message { font-size: 1.1rem; }
.error { color: #f87171; margin: 0.5rem 0; }

.login-page {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card h1 {
  margin: 0 0 0.5rem;
  font-size: 1.75rem;
  line-height: 1.2;
}

.login-card {
  width: 100%;
  max-width: 400px;
}

.login-form input[type="text"],
.login-form input[type="password"] {
  width: 100%;
  min-width: 0;
  flex: none;
  font-size: 16px;
  min-height: 48px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 1rem;
}

.login-form label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.9rem;
  color: var(--muted);
}

.login-form button {
  width: 100%;
  margin-top: 0.5rem;
  min-height: 48px;
  font-size: 1rem;
}

.card-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.btn.small {
  padding: 0.4rem 0.75rem;
  font-size: 0.85rem;
}

.hint {
  font-size: 0.9rem;
  line-height: 1.45;
}

.server-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 1rem;
}

.server-tab {
  padding: 0.5rem 0.85rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  font-size: 0.9rem;
}

.server-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.server-panel {
  display: none;
}

.server-panel.active {
  display: block;
}

code {
  background: #0f1419;
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f7fb;
    --card: #ffffff;
    --text: #111827;
    --muted: #6b7280;
    --border: #e5e7eb;
  }
  .server-card { background: #f9fafb; }
  input[type="text"],
  input[type="password"] { background: #fff; }
  code { background: #f3f4f6; }
}

@media (max-width: 640px) {
  .container {
    padding: 0.75rem;
  }

  .topbar {
    margin-bottom: 0.75rem;
    padding-top: env(safe-area-inset-top, 0px);
  }

  .topbar h1 {
    font-size: 1.25rem;
  }

  .card {
    padding: 0.875rem 1rem;
    border-radius: 10px;
    margin-bottom: 0.75rem;
  }

  .card h2 {
    font-size: 1.1rem;
    margin-top: 0;
  }

  .card h3 {
    font-size: 1rem;
  }

  .grid {
    grid-template-columns: 1fr;
    gap: 0.5rem;
  }

  .server-card {
    padding: 0.875rem;
  }

  .inline-form {
    flex-direction: column;
    align-items: stretch;
  }

  .inline-form input[type="text"],
  .inline-form button {
    width: 100%;
    min-width: 0;
    font-size: 16px;
    min-height: 48px;
  }

  .card-header-row {
    flex-direction: column;
    align-items: stretch;
  }

  .card-header-row .btn {
    width: 100%;
    text-align: center;
  }

  .actions {
    flex-direction: column;
    align-items: stretch;
  }

  .actions > .btn,
  .actions > a.btn,
  .actions > form,
  .actions > span.muted {
    width: 100%;
  }

  .actions > span.muted {
    text-align: center;
  }

  .actions form {
    display: block;
  }

  .actions form button {
    width: 100%;
  }

  .device-list li {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
    padding: 0.75rem 0;
  }

  .device-list li a:first-child span:first-of-type {
    white-space: normal;
  }

  .device-list .btn.small {
    width: 100%;
  }

  .server-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    margin-left: -0.75rem;
    margin-right: -0.75rem;
    padding: 0 0.75rem 0.25rem;
    gap: 0.4rem;
  }

  .server-tabs::-webkit-scrollbar {
    display: none;
  }

  .server-tab {
    flex-shrink: 0;
    min-height: 44px;
    white-space: nowrap;
  }

  .login-page {
    align-items: flex-start;
    padding-top: max(1.5rem, env(safe-area-inset-top, 0px));
    padding-bottom: max(1rem, env(safe-area-inset-bottom, 0px));
  }

  .login-page.container {
    padding-left: max(0.75rem, env(safe-area-inset-left, 0px));
    padding-right: max(0.75rem, env(safe-area-inset-right, 0px));
  }

  .login-card {
    margin-top: 0.5rem;
  }

  .login-card h1 {
    font-size: 1.5rem;
  }

  .qr-image {
    max-width: min(260px, calc(100vw - 4rem));
  }

  .hint ul,
  .hints ul {
    padding-left: 1rem;
    margin-bottom: 0;
  }

  .footer-note {
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
}

@media (max-width: 380px) {
  .login-card h1 {
    font-size: 1.35rem;
  }

  .topbar h1 {
    font-size: 1.15rem;
  }
}
