:root {
  --bg: #f1f5f9;
  --panel: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --ok: #16a34a;
  --ok-soft: rgba(22, 163, 74, 0.12);
  --warn: #d97706;
  --warn-soft: rgba(217, 119, 6, 0.14);
  --idle-soft: rgba(100, 116, 139, 0.14);
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.05), 0 10px 30px rgba(15, 23, 42, 0.06);
  --radius: 14px;
}

[data-theme='dark'] {
  --bg: #0b1120;
  --panel: #121b2e;
  --text: #e8edf7;
  --muted: #94a3b8;
  --border: #233149;
  --accent: #3b82f6;
  --accent-hover: #60a5fa;
  --danger: #ef4444;
  --danger-hover: #f87171;
  --ok: #4ade80;
  --ok-soft: rgba(74, 222, 128, 0.14);
  --warn: #fbbf24;
  --warn-soft: rgba(251, 191, 36, 0.16);
  --idle-soft: rgba(148, 163, 184, 0.16);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 30px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 32px 24px 56px;
  background: var(--bg);
  color: var(--text);
  font:
    15px/1.5 -apple-system,
    BlinkMacSystemFont,
    'Segoe UI',
    Roboto,
    Helvetica,
    Arial,
    sans-serif;
  -webkit-font-smoothing: antialiased;
}

.page {
  max-width: 1120px;
  margin: 0 auto;
}

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

.brand h1 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: -0.01em;
}

.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.875rem;
}

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

.live {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
  font: inherit;
  font-size: 0.8rem;
  white-space: nowrap;
  cursor: pointer;
  transition:
    color 0.15s,
    border-color 0.15s;
}

.live:hover {
  color: var(--text);
  border-color: var(--muted);
}

.live .beacon {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 0 var(--ok-soft);
  animation: beacon 2s ease-out infinite;
}

.live .glyph {
  width: 11px;
  height: 11px;
  fill: currentColor;
}
.live .play {
  display: none;
}
.live.paused .pause {
  display: none;
}
.live.paused .play {
  display: block;
}
.live.paused .beacon {
  background: var(--muted);
  animation: none;
}
.live.offline .beacon {
  background: var(--danger);
  animation: none;
}

@keyframes beacon {
  0% {
    box-shadow: 0 0 0 0 var(--ok-soft);
  }
  70% {
    box-shadow: 0 0 0 7px transparent;
  }
  100% {
    box-shadow: 0 0 0 0 transparent;
  }
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--muted);
  cursor: pointer;
  transition:
    color 0.15s,
    border-color 0.15s;
}

.icon-btn:hover {
  color: var(--text);
  border-color: var(--muted);
}
.icon-btn.spinning svg {
  animation: spin 0.7s linear infinite;
}
.icon-btn svg {
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
[data-theme='dark'] .icon-btn .sun {
  display: block;
}
[data-theme='dark'] .icon-btn .moon {
  display: none;
}
[data-theme='light'] .icon-btn .sun {
  display: none;
}
[data-theme='light'] .icon-btn .moon {
  display: block;
}

.summary {
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
  padding: 14px 18px;
  margin-bottom: 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  font-size: 0.875rem;
  color: var(--muted);
}

.summary .count {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--text);
}
.summary .count b {
  font-weight: 600;
}
.summary .swatch {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.summary .swatch.ok {
  background: var(--ok);
}
.summary .swatch.idle {
  background: var(--muted);
}
.summary .swatch.warn {
  background: var(--warn);
}
.summary .spacer {
  flex: 1;
}

.progress {
  width: 100%;
  height: 3px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
  margin-bottom: 22px;
}

.progress span {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent), var(--ok));
  transition: width 0.25s linear;
}

.progress.paused {
  opacity: 0.4;
}

.sections {
  display: grid;
  gap: 28px;
}

.section-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 0 2px 12px;
}

.section-head h2 {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.section-count {
  color: var(--muted);
  font-size: 0.8rem;
}

.grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.card {
  display: flex;
  flex-direction: column;
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  transition:
    transform 0.15s ease,
    box-shadow 0.15s ease;
}

.card:hover {
  transform: translateY(-2px);
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.identity h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.host {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-top: 3px;
  color: var(--muted);
  font-size: 0.8rem;
  text-decoration: none;
}

.host:hover {
  color: var(--accent);
}
.host svg {
  width: 12px;
  height: 12px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 11px;
  border-radius: 999px;
  background: var(--idle-soft);
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: capitalize;
  white-space: nowrap;
}

.pill .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}

.card[data-state='running'] .pill {
  background: var(--ok-soft);
  color: var(--ok);
}
.card[data-state='pending'] .pill,
.card[data-state='stopping'] .pill,
.card[data-state='shutting-down'] .pill {
  background: var(--warn-soft);
  color: var(--warn);
}
.card[data-state='terminated'] .pill {
  background: rgba(220, 38, 38, 0.14);
  color: var(--danger);
}

.card[data-state='pending'] .pill .dot,
.card[data-state='stopping'] .pill .dot,
.card[data-state='shutting-down'] .pill .dot {
  animation: blink 1.1s ease-in-out infinite;
}

@keyframes blink {
  50% {
    opacity: 0.25;
  }
}

.meta {
  display: grid;
  gap: 10px;
  margin: 18px 0 20px;
}

.meta > div {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.meta dt {
  color: var(--muted);
  font-size: 0.8rem;
}
.meta dd {
  margin: 0;
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.card-foot {
  margin-top: auto;
}

.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 10px 16px;
  border: 0;
  border-radius: 10px;
  color: #fff;
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
  transition:
    background-color 0.15s ease,
    opacity 0.15s ease;
}

.btn.start {
  background: var(--accent);
}
.btn.start:hover {
  background: var(--accent-hover);
}
.btn.stop {
  background: var(--danger);
}
.btn.stop:hover {
  background: var(--danger-hover);
}
.btn.ghost {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
}
.btn.ghost:hover {
  border-color: var(--muted);
}
.btn[aria-disabled='true'] {
  opacity: 0.6;
  pointer-events: none;
}

.spinner {
  display: none;
  width: 13px;
  height: 13px;
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

.btn.is-busy .spinner {
  display: block;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

dialog {
  width: min(420px, calc(100vw - 32px));
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(2, 6, 23, 0.55);
  backdrop-filter: blur(2px);
}

/*
 * Page scroll lock while a modal is open: a wheel or swipe over the dialog, its backdrop or
 * the end of its table must not move the page behind it. `contain` on the dialog and its
 * scroll boxes stops the chaining; the lock covers wheel events outside any scroll box.
 */
html:has(dialog[open]) {
  overflow: hidden;
}
dialog,
dialog .journal-body {
  overscroll-behavior: none;
}
dialog h3 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}
dialog p {
  margin: 0 0 20px;
  color: var(--muted);
  font-size: 0.875rem;
}
.dialog-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}
.dialog-actions .btn {
  width: auto;
  flex: 1;
}

.card-tools {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn.small {
  width: 30px;
  height: 30px;
  border-radius: 8px;
}
.icon-btn.small svg {
  width: 15px;
  height: 15px;
}

/*
 * Capped-height column: header, totals and footer keep their size and the table box takes
 * what is left, so the table is the only thing that scrolls. Keep `[open]`: a display value
 * on a closed dialog would override the browser's display:none and show it.
 */
dialog.wide[open] {
  display: flex;
  flex-direction: column;
  width: min(760px, calc(100vw - 32px));
  max-height: min(820px, calc(100vh - 48px));
  overflow: hidden;
}
/* The all-instances overview: six columns need more room than one instance's journal. */
dialog.wide.usage[open] {
  width: min(1100px, calc(100vw - 32px));
}
dialog.wide > * {
  flex-shrink: 0;
}
dialog.wide > .journal-body {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: 0;
  /* Fallback only: reached when the table is already at its min-height on a tiny screen. */
  overflow-y: auto;
}
.journal-body > * {
  flex-shrink: 0;
}

.journal-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}
dialog .journal-sub {
  margin: 0;
  font-size: 0.8rem;
}
dialog .journal-note {
  margin: 16px 0 0;
  font-size: 0.75rem;
}
.journal-body h4 {
  margin: 20px 0 8px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
dialog .journal-empty {
  margin: 12px 0 0;
}

.totals {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
}
.total {
  display: grid;
  gap: 2px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
}
.total-label {
  color: var(--muted);
  font-size: 0.72rem;
}
.total-time {
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
}
.total-cost {
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/*
 * Header and body are separate tables (views/manager.js `table()`), so only the body scrolls
 * vertically and the header never moves. Both share one fixed-layout colgroup; both boxes
 * reserve the scrollbar gutter, so a classic (non-overlay) scrollbar cannot shift the columns.
 * `overscroll-behavior: none` removes the elastic bounce at either end.
 */
.table-wrap {
  display: flex;
  flex-direction: column;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior: none;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.table-inner {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: 0;
  min-width: 100%;
}
.table-wrap.sessions .table-inner {
  min-width: 520px;
}
.table-wrap.usage .table-inner {
  min-width: 860px;
}
.table-head {
  flex-shrink: 0;
  overflow-y: hidden;
  scrollbar-gutter: stable;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.table-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: none;
  scrollbar-gutter: stable;
}
/* The table body scrolls inside the dialog so the totals and header stay in view. */
.journal-body > .table-wrap.scroll {
  flex: 0 1 auto;
  min-height: 140px;
}
.journal-more {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 0.78rem;
}
/* .btn sets display, which would otherwise override the hidden attribute. */
.journal-more .btn[hidden] {
  display: none;
}
.journal-more .btn {
  width: auto;
  padding: 6px 12px;
  font-size: 0.78rem;
}

.journal-table {
  width: 100%;
  table-layout: fixed;
  border-collapse: collapse;
  font-size: 0.8rem;
}
.journal-table th,
.journal-table td {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  overflow: hidden;
  text-align: left;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.journal-table th {
  border-bottom: 0;
  color: var(--muted);
  font-weight: 600;
  background: var(--bg);
}
.journal-table tr:last-child td {
  border-bottom: 0;
}
/* Headers of numeric columns align with their values. */
.journal-table th.num,
.journal-table .num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.journal-table .is-running {
  color: var(--ok);
  font-weight: 600;
}

.field-label {
  display: block;
  margin: 0 0 6px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}

input[type='password'] {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

input[type='password']:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}

input[type='password'][disabled] {
  opacity: 0.6;
  cursor: not-allowed;
}

.field-error {
  margin: 10px 0 0;
  color: var(--danger);
  font-size: 0.8rem;
}

.gate {
  display: grid;
  place-items: center;
  min-height: calc(100vh - 88px);
}

.login-card {
  width: min(380px, 100%);
  padding: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: var(--shadow);
  text-align: left;
}

.login-card h1 {
  margin: 0 0 4px;
  font-size: 1.2rem;
  letter-spacing: -0.01em;
}

.login-card .gate-hint {
  margin: 0 0 22px;
  color: var(--muted);
  font-size: 0.875rem;
}

.login-card .btn {
  margin-top: 16px;
}

.lock-badge {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 42px;
  margin-bottom: 16px;
  border-radius: 12px;
  background: var(--idle-soft);
  color: var(--accent);
}

.lock-badge svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.toasts {
  position: fixed;
  right: 20px;
  bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 10;
}

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 340px;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 10px;
  background: var(--panel);
  box-shadow: var(--shadow);
  font-size: 0.85rem;
  animation: rise 0.2s ease-out;
}

.toast.error {
  border-left-color: var(--danger);
}
.toast.ok {
  border-left-color: var(--ok);
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
}

/* Two columns need ~724px: keep each card at least 330px wide, or the meta rows wrap. */
@media (max-width: 720px) {
  .grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 560px) {
  body {
    padding: 20px 14px 40px;
  }

  /* Compact totals so the session table keeps most of the dialog's height. */
  dialog.wide[open] {
    padding: 16px;
  }
  .totals {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }
  .total {
    padding: 6px 8px;
  }
  .total-label {
    font-size: 0.66rem;
  }
  .total-time {
    font-size: 0.8rem;
  }
  .total-cost {
    font-size: 0.74rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}
