:root {
  color-scheme: dark;
  --bg: #0c1418;
  --bg-elevated: #132028;
  --bg-card: #1a2a32;
  --bg-input: #0f1a1f;
  --text: #e8eef0;
  --text-muted: #8a9aa3;
  --accent: #e8a838;
  --accent-strong: #f0b84a;
  --accent-soft: rgba(232, 168, 56, 0.16);
  --teal: #2a6f7a;
  --teal-bright: #3d8f9c;
  --danger: #e07060;
  --danger-soft: rgba(224, 112, 96, 0.14);
  --ok: #6dbf8a;
  --border: rgba(232, 238, 240, 0.08);
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
  --radius: 16px;
  --radius-sm: 10px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --font: "Figtree", "Segoe UI", system-ui, sans-serif;
  --font-display: "Syne", "Figtree", sans-serif;
  --nav-h: 64px;
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #f0f4f5;
  --bg-elevated: #ffffff;
  --bg-card: #e4ecee;
  --bg-input: #ffffff;
  --text: #132028;
  --text-muted: #5a6c74;
  --accent: #c4891a;
  --accent-strong: #a87412;
  --accent-soft: rgba(196, 137, 26, 0.14);
  --teal: #1f5c66;
  --teal-bright: #2a6f7a;
  --danger: #c44a3a;
  --danger-soft: rgba(196, 74, 58, 0.12);
  --ok: #2f8a52;
  --border: rgba(19, 32, 40, 0.1);
  --shadow: 0 12px 32px rgba(19, 32, 40, 0.1);
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    color-scheme: light;
    --bg: #f0f4f5;
    --bg-elevated: #ffffff;
    --bg-card: #e4ecee;
    --bg-input: #ffffff;
    --text: #132028;
    --text-muted: #5a6c74;
    --accent: #c4891a;
    --accent-strong: #a87412;
    --accent-soft: rgba(196, 137, 26, 0.14);
    --teal: #1f5c66;
    --teal-bright: #2a6f7a;
    --danger: #c44a3a;
    --danger-soft: rgba(196, 74, 58, 0.12);
    --ok: #2f8a52;
    --border: rgba(19, 32, 40, 0.1);
    --shadow: 0 12px 32px rgba(19, 32, 40, 0.1);
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(42, 111, 122, 0.28), transparent 55%),
    radial-gradient(900px 500px at 100% 0%, rgba(232, 168, 56, 0.08), transparent 50%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  min-height: 100dvh;
  overflow: hidden;
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  appearance: none;
  border: 0;
  background: none;
  cursor: pointer;
  padding: 0;
}

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

[hidden] {
  display: none !important;
}

code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.9em;
  background: var(--accent-soft);
  color: var(--accent-strong);
  padding: 0.1em 0.35em;
  border-radius: 4px;
}

/* Banners */
.banner {
  position: fixed;
  left: 0;
  right: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px calc(16px + var(--safe-right)) 10px calc(16px + var(--safe-left));
  font-size: 0.875rem;
  font-weight: 500;
}

.banner-offline {
  top: 0;
  padding-top: calc(10px + var(--safe-top));
  background: var(--danger);
  color: #1a0a08;
}

.banner-install {
  bottom: 0;
  padding-bottom: calc(10px + var(--safe-bottom));
  background: var(--bg-elevated);
  border-top: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.banner-btn {
  margin-left: auto;
  background: var(--accent);
  color: #1a1408;
  font-weight: 700;
  padding: 6px 14px;
  border-radius: 999px;
}

.banner-dismiss {
  width: 32px;
  height: 32px;
  font-size: 1.25rem;
  color: var(--text-muted);
}

body.has-offline .app {
  padding-top: calc(44px + var(--safe-top));
}

.app {
  height: 100dvh;
  max-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding:
    calc(12px + var(--safe-top))
    calc(16px + var(--safe-right))
    calc(12px + var(--safe-bottom))
    calc(16px + var(--safe-left));
  overflow: hidden;
}

.app[data-connected="true"] {
  padding-bottom: calc(12px + var(--safe-bottom) + var(--nav-h));
}

.view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* Connect */
.view-connect {
  justify-content: center;
  max-width: 420px;
  width: 100%;
  margin: 0 auto;
  gap: 28px;
}

.connect-hero {
  text-align: center;
}

.brand-mark {
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.brand {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(2rem, 6vw, 2.6rem);
  letter-spacing: -0.03em;
  margin: 16px 0 6px;
  background: linear-gradient(135deg, var(--text) 40%, var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.tagline {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.95rem;
  line-height: 1.45;
}

.connect-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

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

.connect-footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.dot-sep {
  opacity: 0.5;
}

.text-btn {
  color: var(--accent);
  font-weight: 600;
}
.text-btn:hover {
  text-decoration: underline;
}

/* Fields & buttons */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
}

.field input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  color: var(--text);
  font-weight: 500;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.field input:focus {
  border-color: var(--teal-bright);
  box-shadow: 0 0 0 3px rgba(61, 143, 156, 0.25);
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  align-items: end;
}

.field-check {
  padding-bottom: 10px;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
}

.check-row input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 46px;
  padding: 0 18px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.95rem;
  transition: transform 0.12s, opacity 0.12s, background 0.15s;
}
.btn:active {
  transform: scale(0.98);
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-block {
  width: 100%;
}

.btn-primary {
  background: var(--accent);
  color: #1a1408;
}
.btn-primary:hover:not(:disabled) {
  background: var(--accent-strong);
}

.btn-secondary {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
}
.btn-secondary:hover:not(:disabled) {
  border-color: var(--teal-bright);
}

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

.btn-danger-outline {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid transparent;
}
.btn-danger-outline:hover:not(:disabled) {
  border-color: var(--danger);
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--text);
}
.icon-btn:active {
  background: var(--border);
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.topbar-brand img {
  border-radius: 8px;
}

.topbar-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.1rem;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.topbar-sub {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 52vw;
}

.topbar-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

.scroll-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 8px;
}

.section-title {
  font-family: var(--font-display);
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 22px 0 12px;
  color: var(--text);
}

.optional {
  font-family: var(--font);
  font-weight: 500;
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* Status cards */
.status-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  min-height: 78px;
}

.stat-card.wide {
  grid-column: 1 / -1;
}

.stat-card.accent {
  background: linear-gradient(145deg, var(--accent-soft), var(--bg-card));
  border-color: rgba(232, 168, 56, 0.28);
}

.stat-label {
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.stat-value {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
  word-break: break-word;
}

.stat-value.ports {
  font-family: var(--font);
  font-size: 0.9rem;
  font-weight: 600;
  line-height: 1.5;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.port-chip {
  display: inline-block;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--teal-bright);
}

.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.action-row .btn {
  flex: 1 1 auto;
  min-width: 120px;
}

/* Logs */
.log-pane {
  flex: 1;
  min-height: 0;
  margin: 0;
  padding: 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: auto;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.75rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  user-select: text;
  color: var(--ok);
}

/* Settings */
.settings-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 560px;
}

.settings-form .section-title {
  margin-top: 8px;
}

.port-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
}

.port-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}

.port-item:has(input:checked) {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.port-item input {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
}

.settings-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 16px;
  padding-bottom: 24px;
}

/* Devices */
.device-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.device-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  text-align: left;
  width: 100%;
  transition: border-color 0.15s;
}

.device-card:hover {
  border-color: var(--teal-bright);
}

.device-card.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.device-card-body {
  flex: 1;
  min-width: 0;
}

.device-card-name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1rem;
}

.device-card-url {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.device-delete {
  color: var(--danger);
  padding: 8px;
  border-radius: 8px;
  font-size: 0.8rem;
  font-weight: 700;
}
.device-delete:hover {
  background: var(--danger-soft);
}

.empty-hint {
  text-align: center;
  color: var(--text-muted);
  padding: 32px 16px;
}

/* Bottom nav */
.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  height: calc(var(--nav-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: color-mix(in srgb, var(--bg-elevated) 92%, transparent);
  backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--text-muted);
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.nav-item.active {
  color: var(--accent);
}

.nav-item:active {
  background: var(--border);
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-bottom) + var(--nav-h));
  transform: translateX(-50%);
  z-index: 60;
  max-width: min(420px, calc(100vw - 32px));
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  border-radius: 999px;
  padding: 12px 20px;
  font-size: 0.9rem;
  font-weight: 600;
  text-align: center;
}

.app:not([data-connected="true"]) ~ .toast,
body:has(.app:not([data-connected="true"])) .toast {
  bottom: calc(24px + var(--safe-bottom));
}

/* Desktop / tablet */
@media (min-width: 768px) {
  .app {
    max-width: 960px;
    margin: 0 auto;
  }

  .app[data-connected="true"] {
    padding-bottom: calc(12px + var(--safe-bottom));
    padding-left: calc(16px + var(--safe-left) + 88px);
  }

  .bottom-nav {
    left: auto;
    right: auto;
    top: 50%;
    bottom: auto;
    transform: translateY(-50%);
    width: 72px;
    height: auto;
    grid-template-columns: 1fr;
    gap: 4px;
    padding: 12px 8px;
    margin-left: calc(16px + var(--safe-left));
    border-radius: var(--radius);
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
    /* Position relative to viewport left of centered app */
    position: fixed;
    left: max(12px, calc(50vw - 480px - 88px));
  }

  .status-grid {
    grid-template-columns: repeat(4, 1fr);
  }

  .stat-card.wide {
    grid-column: span 2;
  }

  .topbar-sub {
    max-width: 40vw;
  }
}

@media (min-width: 1100px) {
  .status-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

.settings-lede {
  color: var(--text-muted);
  font-size: 0.92rem;
  line-height: 1.45;
  margin: -0.25rem 0 1rem;
}

.notif-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 1rem 1.05rem 1.1rem;
  margin-bottom: 1rem;
}

.notif-card-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.35rem;
}

.notif-card-head strong {
  font-family: "Syne", sans-serif;
  font-weight: 700;
}

.notif-badge {
  font-size: 0.75rem;
  color: var(--text-muted);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
}

.notif-badge.ok {
  color: var(--ok, #81c995);
  border-color: color-mix(in srgb, var(--ok, #81c995) 40%, transparent);
}

.notif-badge.warn {
  color: var(--accent);
}

.notif-badge.bad {
  color: var(--danger);
}

.notif-actions {
  margin-top: 0.5rem;
  gap: 0.5rem;
}

.optional {
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text-muted);
}
