:root {
  color-scheme: light;
  --bg: #F7F8FA;
  --surface: #FFFFFF;
  --surface-subtle: #F1F5F9;
  --ink: #11161D;
  --ink-muted: #475569;
  --line: #E2E8F0;
  --line-strong: #CBD5E1;
  --accent: #0F52BA;
  --accent-hover: #0B3E91;
  --accent-soft: #EEF4FF;
  --ok: #15803D;
  --ok-bg: #DCFCE7;
  --warn: #92400E;
  --warn-bg: #FEF3C7;
  --danger: #B91C1C;
  --danger-bg: #FEE2E2;
  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --text-h: 1.25rem;
  --text-body: 0.9375rem;
  --text-sm: 0.8125rem;
  --radius-sm: 6px;
  --radius-md: 10px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

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

[hidden] {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.skip-link:focus {
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 1000;
  width: auto;
  height: auto;
  margin: 0;
  padding: 10px 16px;
  clip-path: none;
  background: var(--accent);
  color: #FFFFFF;
  border-radius: var(--radius-sm);
  font-weight: 600;
  text-decoration: none;
}

html {
  font-size: 100%;
}

html,
body {
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

body {
  font-size: var(--text-body);
}

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

button {
  cursor: pointer;
}

a {
  color: var(--accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

a:hover {
  color: var(--accent-hover);
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.tabular-nums {
  font-variant-numeric: tabular-nums;
}

/* Header & Brand */
.brand-wordmark {
  font-size: 1.375rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--ink);
  text-decoration: none;
  line-height: 1.1;
}

.brand-wordmark:hover {
  color: var(--ink);
}

.top-header,
.auth-header {
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.auth-header-inner,
.top-header-inner {
  max-width: 1240px;
  margin: 0 auto;
  padding: 0 20px;
  min-height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 24px;
  min-width: 0;
}

.primary-nav {
  display: flex;
  align-items: center;
  gap: 4px;
}

.nav-link {
  display: inline-flex;
  align-items: center;
  min-height: 36px;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  color: var(--ink-muted);
  text-decoration: none;
  font-size: var(--text-body);
  font-weight: 500;
  white-space: nowrap;
  transition: background-color 160ms var(--ease), color 160ms var(--ease);
}

.nav-link:hover {
  background: var(--surface-subtle);
  color: var(--ink);
}

.nav-link[aria-current="page"] {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.org-label {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink);
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-sep {
  color: var(--line-strong);
}

.wallet-balance {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink-muted);
  white-space: nowrap;
}

.btn-ghost.nav-toggle {
  display: none;
}

/* Auth Page */
.auth-stage {
  max-width: 460px;
  margin: 0 auto;
  padding: 40px 20px 64px;
}

.auth-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 28px 24px;
}

.auth-title {
  font-size: 1.625rem;
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.2;
  margin-bottom: 6px;
}

.auth-subtitle {
  font-size: var(--text-body);
  color: var(--ink-muted);
  margin-bottom: 20px;
  max-width: 65ch;
}

.segmented-control {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--surface-subtle);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 3px;
  gap: 4px;
  margin-bottom: 20px;
}

.segmented-tab {
  min-height: 40px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--ink-muted);
  font-size: var(--text-sm);
  font-weight: 600;
  transition: background-color 160ms var(--ease), color 160ms var(--ease);
}

.segmented-tab[aria-selected="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(17, 22, 29, 0.08);
}

.identity-notice {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  font-size: var(--text-sm);
  color: var(--ink-muted);
}

.turnstile-wrap {
  margin-top: 4px;
}

/* Forms & Inputs */
.stack-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.field-group label {
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink);
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="search"],
input[type="url"],
select,
textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
  font-size: max(1rem, 1em);
  color: var(--ink);
  transition: border-color 160ms var(--ease);
}

.auth-body input[type="email"],
.auth-body input[type="password"],
.auth-body input[type="text"] {
  min-height: 48px;
}

input:user-invalid,
select:user-invalid,
textarea:user-invalid,
[aria-invalid="true"] {
  border-color: var(--danger);
}

.password-wrap {
  display: flex;
  align-items: stretch;
  gap: 8px;
}

.password-wrap input {
  flex: 1 1 auto;
  min-width: 0;
}

.btn-toggle-pw {
  min-width: 68px;
  min-height: 48px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-sm);
}

.field-hint {
  font-size: var(--text-sm);
  color: var(--ink-muted);
}

.field-error {
  font-size: var(--text-sm);
  color: var(--danger);
  font-weight: 500;
}

.checkbox-row {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: center;
}

.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: var(--text-sm);
  font-weight: 500;
  min-height: 36px;
}

.checkbox-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

/* Buttons */
.btn-primary,
.btn-secondary,
.btn-danger,
.btn-ghost {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-size: var(--text-sm);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  transition: background-color 160ms var(--ease), border-color 160ms var(--ease), color 160ms var(--ease), opacity 160ms var(--ease);
}

.btn-primary {
  background: var(--accent);
  color: #FFFFFF;
  border: 1px solid var(--accent);
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-secondary {
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line-strong);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--surface-subtle);
}

.btn-danger {
  background: var(--danger);
  color: #FFFFFF;
  border: 1px solid var(--danger);
}

.btn-danger:hover:not(:disabled) {
  background: #991B1B;
}

.btn-ghost {
  background: transparent;
  color: var(--ink-muted);
  border: 1px solid transparent;
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-subtle);
  color: var(--ink);
}

.btn-compact {
  min-height: 34px;
  padding: 6px 12px;
}

.btn-full {
  width: 100%;
  min-height: 48px;
  font-size: var(--text-body);
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* Main Workspace */
.app-main {
  max-width: 1240px;
  margin: 0 auto;
  padding: 24px 20px 64px;
}

.view-root {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

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

.view-header h1 {
  font-size: 1.625rem;
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.2;
}

.section-block {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 20px;
}

.section-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.section-head h2 {
  font-size: var(--text-h);
  font-weight: 600;
  letter-spacing: -0.015em;
}

.muted-copy {
  font-size: var(--text-body);
  color: var(--ink-muted);
  max-width: 65ch;
}

.small-copy {
  font-size: var(--text-sm);
  color: var(--ink-muted);
}

/* Summary Strip (no nested cards) */
.summary-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
}

.summary-metric {
  padding: 16px 20px;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.summary-metric:last-child {
  border-right: 0;
}

.metric-label {
  font-size: var(--text-sm);
  color: var(--ink-muted);
  font-weight: 500;
}

.metric-value {
  font-size: 1.375rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

/* Filters Toolbar */
.toolbar-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  margin-bottom: 16px;
}

.toolbar-search {
  flex: 1 1 240px;
  min-width: 0;
}

.toolbar-filter {
  flex: 0 1 180px;
  min-width: 140px;
}

/* Alerts & Toast */
.inline-alert,
.toast-banner {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--danger);
  background: var(--danger-bg);
  color: var(--danger);
  font-size: var(--text-sm);
  font-weight: 500;
  margin-bottom: 16px;
}

.toast-banner[data-tone="ok"] {
  border-color: var(--ok);
  background: var(--ok-bg);
  color: var(--ok);
}

.toast-banner[data-tone="warn"] {
  border-color: var(--warn);
  background: var(--warn-bg);
  color: var(--warn);
}

.toast-banner[data-tone="info"] {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

.secret-reveal {
  padding: 14px;
  border: 1px solid var(--ok);
  background: var(--ok-bg);
  color: var(--ink);
  border-radius: var(--radius-sm);
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}

.secret-code {
  display: block;
  padding: 8px 10px;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  font-size: var(--text-sm);
  word-break: break-all;
  font-variant-numeric: tabular-nums;
}

/* Responsive Data Tables */
.table-wrapper {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  container-type: inline-size;
  background: var(--surface);
}

.section-block .table-wrapper {
  border: 0;
  border-top: 1px solid var(--line);
  border-radius: 0;
}

.data-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  text-align: left;
  font-size: var(--text-sm);
}

.data-table thead th {
  position: sticky;
  inset-block-start: 0;
  z-index: 1;
  background: var(--surface-subtle);
  color: var(--ink-muted);
  font-weight: 600;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}

.data-table tbody th,
.data-table tbody td {
  padding: 12px;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}

.data-table tbody th[scope="row"] {
  position: sticky;
  inset-inline-start: 0;
  background: var(--surface);
  font-weight: 600;
}

.data-table tbody tr:last-child th,
.data-table tbody tr:last-child td {
  border-bottom: 0;
}

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

.spaced-bottom {
  margin-bottom: 12px;
}

.spaced-top {
  margin-top: 16px;
}

.state-cell {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: var(--text-sm);
  font-weight: 600;
}

.state-cell[data-state="running"],
.state-cell[data-state="pass"],
.state-cell[data-state="active"],
.state-cell[data-state="healthy"] {
  background: var(--ok-bg);
  color: var(--ok);
}

.state-cell[data-state="starting"],
.state-cell[data-state="warn"],
.state-cell[data-state="suspended"],
.state-cell[data-state="warning"] {
  background: var(--warn-bg);
  color: var(--warn);
}

.state-cell[data-state="stopped"],
.state-cell[data-state="neutral"] {
  background: var(--surface-subtle);
  color: var(--ink-muted);
}

.state-cell[data-state="fail"],
.state-cell[data-state="critical"],
.state-cell[data-state="error"] {
  background: var(--danger-bg);
  color: var(--danger);
}

@container (width < 640px) {
  .data-table thead {
    display: none;
  }

  .data-table,
  .data-table tbody,
  .data-table tr,
  .data-table th,
  .data-table td {
    display: block;
    width: 100%;
  }

  .data-table tbody tr {
    padding: 12px;
    border-bottom: 1px solid var(--line);
  }

  .data-table tbody tr:last-child {
    border-bottom: 0;
  }

  .data-table tbody th,
  .data-table tbody td {
    padding: 4px 0;
    border-bottom: 0;
    position: static;
  }

  .data-table tbody td[data-label]::before {
    content: attr(data-label) ": ";
    font-weight: 600;
    color: var(--ink-muted);
  }
}

/* Native Dialog */
.app-dialog {
  margin: auto;
  width: min(94vw, 640px);
  max-height: 90dvh;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--ink);
  padding: 0;
  box-shadow: 0 20px 50px rgba(17, 22, 29, 0.2);
}

.app-dialog[open] {
  display: flex;
  flex-direction: column;
}

.app-dialog::backdrop {
  background: rgba(17, 22, 29, 0.45);
}

.dialog-shell {
  display: flex;
  flex-direction: column;
  max-height: 90dvh;
}

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
}

.dialog-head h2 {
  font-size: var(--text-h);
  font-weight: 600;
}

.dialog-body {
  padding: 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
  padding-top: 8px;
}

/* Stream Player inside Dialog */
.stream-stage {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 220px;
  gap: 16px;
  align-items: start;
}

.stream-screen {
  position: relative;
  background: #11161D;
  border-radius: var(--radius-sm);
  aspect-ratio: 9 / 19;
  max-height: 60dvh;
  margin: 0 auto;
  width: 100%;
  max-width: 300px;
  overflow: hidden;
  border: 1px solid var(--line-strong);
}

.stream-video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  touch-action: none;
  cursor: crosshair;
}

.stream-controls {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.kv-list {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px 16px;
  font-size: var(--text-sm);
}

.kv-list dt {
  color: var(--ink-muted);
  font-weight: 500;
}

.kv-list dd {
  font-weight: 600;
  word-break: break-word;
}

.danger-zone {
  border-color: var(--danger);
}

/* Responsive Breakpoints */
@media (max-width: 820px) {
  .summary-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .summary-metric:nth-child(2n) {
    border-right: 0;
  }

  .summary-metric:nth-child(-n+2) {
    border-bottom: 1px solid var(--line);
  }
}

@media (max-width: 720px) {
  .top-header-inner {
    flex-wrap: wrap;
    padding: 10px 16px;
  }

  .header-left {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 10px;
  }

  .btn-ghost.nav-toggle {
    display: inline-flex;
    min-height: 36px;
    padding: 6px 12px;
    border: 1px solid var(--line-strong);
  }

  .primary-nav {
    display: none;
    width: 100%;
    flex-wrap: wrap;
    padding-top: 6px;
    border-top: 1px solid var(--line);
  }

  .primary-nav[data-open="true"] {
    display: flex;
  }

  .header-right {
    width: 100%;
    justify-content: space-between;
    padding-top: 8px;
    border-top: 1px solid var(--line);
  }

  .stream-stage {
    grid-template-columns: 1fr;
  }
}

@media (pointer: coarse) {
  button,
  .nav-link,
  input,
  select {
    min-height: 44px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}

@media (forced-colors: active) {
  .auth-panel,
  .section-block,
  .summary-strip,
  .table-wrapper,
  .app-dialog,
  .btn-primary,
  .btn-secondary,
  .btn-danger {
    border: 1px solid CanvasText;
  }
}
