:root {
  color-scheme: light dark;
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-soft: #f8fafb;
  --text: #18202a;
  --muted: #657180;
  --line: #dce2e8;
  --brand: #166b5c;
  --brand-soft: #dff3ed;
  --blue: #245e9b;
  --blue-soft: #e2eefb;
  --amber: #8a5a09;
  --amber-soft: #fff0c7;
  --red: #a2352a;
  --red-soft: #fde8e5;
  --shadow: 0 8px 24px rgba(24, 32, 42, .07);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 280px;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button, input, textarea, select { font: inherit; }
fieldset { min-width: 0; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 62px;
  padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(12px);
}

.topbar div { display: grid; gap: 1px; }
.topbar strong { font-size: 17px; }
.topbar span { color: var(--muted); font-size: 12px; }

.text-command {
  border: 0;
  padding: 8px 0 8px 12px;
  background: transparent;
  color: var(--brand);
  cursor: pointer;
}

.primary-command, .secondary-command {
  min-height: 42px;
  border: 1px solid var(--brand);
  border-radius: 7px;
  padding: 9px 13px;
  cursor: pointer;
}

.primary-command { background: var(--brand); color: #fff; }
.secondary-command { background: transparent; color: var(--brand); }
.primary-command:disabled, .secondary-command:disabled {
  border-color: var(--line);
  background: var(--surface-soft);
  color: var(--muted);
  cursor: not-allowed;
}

main {
  width: min(760px, 100%);
  margin: 0 auto;
  padding: 22px 16px calc(28px + env(safe-area-inset-bottom));
}

h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 6px; font-size: 25px; line-height: 1.2; }
h2 { margin-bottom: 10px; font-size: 16px; }
.muted { color: var(--muted); }
.eyebrow { margin-bottom: 6px; color: var(--brand); font-size: 12px; font-weight: 700; text-transform: uppercase; }

.state-panel {
  padding: 26px 0;
  border-bottom: 1px solid var(--line);
}

.login-panel { max-width: 420px; }
.login-form { display: grid; gap: 10px; margin-top: 24px; }
.login-form label { font-weight: 600; }
.login-form input {
  width: 100%;
  min-height: 44px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  font-family: ui-monospace, monospace;
  font-size: 18px;
  letter-spacing: 0;
}
.login-form button { justify-self: start; min-width: 116px; }
.login-error { min-height: 22px; margin: 0; color: var(--red); }

.page-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 18px;
}

.back-command {
  flex: 0 0 auto;
  border: 0;
  padding: 5px 0;
  background: transparent;
  color: var(--brand);
  cursor: pointer;
}

.company-list { display: grid; gap: 10px; }

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
}

.segment {
  min-height: 42px;
  border: 0;
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
}

.segment + .segment { border-left: 1px solid var(--line); }
.segment.active { background: var(--brand-soft); color: var(--brand); font-weight: 700; }

.onboarding-form { display: grid; gap: 14px; padding: 20px 0; }
.field { display: grid; gap: 6px; }
.field-label { font-weight: 650; }
.field-control {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 10px 11px;
  background: var(--surface);
  color: var(--text);
}
textarea.field-control { min-height: 112px; resize: vertical; }
.readonly-value {
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 10px 11px;
  background: var(--surface-soft);
}
.checkout { display: grid; gap: 14px; }
.checkout h2 { margin-bottom: 0; }
.history-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 7px 12px;
  padding: 13px 0;
  border-top: 1px solid var(--line);
}
.history-row:first-of-type { border-top: 0; }
.history-row > div { display: grid; gap: 3px; }
.history-detail { grid-column: 1 / -1; color: var(--muted); font-size: 13px; }
.command-row { display: flex; flex-wrap: wrap; gap: 10px; }
.notice { padding: 11px 0; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); color: var(--amber); }

.company-row {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  padding: 15px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
  text-align: left;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.company-row.static-row { cursor: default; }

.company-row:focus-visible, .back-command:focus-visible, .text-command:focus-visible,
.primary-command:focus-visible, .secondary-command:focus-visible, .segment:focus-visible,
.field-control:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--brand) 35%, transparent);
  outline-offset: 2px;
}

.company-name { display: block; overflow-wrap: anywhere; font-weight: 700; }
.company-meta { display: flex; flex-wrap: wrap; gap: 6px 12px; margin-top: 7px; color: var(--muted); font-size: 12px; }
.chevron { color: var(--muted); font-size: 22px; line-height: 1; }

.section-band {
  padding: 17px 0;
  border-top: 1px solid var(--line);
}

.section-band:first-of-type { border-top: 0; }

.status-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.status-line h2 { margin: 0; }

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  white-space: nowrap;
}

.badge.covered, .badge.allowed, .badge.active, .badge.success, .badge.paid { background: var(--brand-soft); color: var(--brand); }
.badge.not_connected, .badge.not_started, .badge.pending, .badge.running { background: var(--amber-soft); color: var(--amber); }
.badge.invalid, .badge.error, .badge.blocked, .badge.cancelled { background: var(--red-soft); color: var(--red); }
.badge.observe, .badge.partial, .badge.uncertain { background: var(--blue-soft); color: var(--blue); }

.facts {
  display: grid;
  grid-template-columns: minmax(120px, .8fr) minmax(0, 1.2fr);
  border: 1px solid var(--line);
  border-radius: 7px;
  overflow: hidden;
  background: var(--surface);
}

.facts dt, .facts dd {
  margin: 0;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  overflow-wrap: anywhere;
}

.facts dt { color: var(--muted); background: var(--surface-soft); }
.facts dt:last-of-type, .facts dd:last-of-type { border-bottom: 0; }

.empty {
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: 7px;
  color: var(--muted);
  text-align: center;
}

.selection-list { display: grid; gap: 10px; margin-bottom: 12px; }
.filter-band {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px 14px;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
}
.filter-band legend { padding: 0 5px; font-weight: 700; }
.check-row { display: flex; align-items: center; gap: 9px; min-height: 36px; }
.check-row input { width: 18px; height: 18px; accent-color: var(--brand); }
.check-row.compact { color: var(--muted); }
.settings-grid { display: grid; gap: 10px; margin-bottom: 12px; }
.group-settings { display: grid; gap: 10px; padding: 14px 0; border-top: 1px solid var(--line); }
.group-settings:first-of-type { border-top: 0; }

@media (max-width: 430px) {
  main { padding-top: 18px; }
  h1 { font-size: 22px; }
  .page-heading { display: grid; }
  .page-heading .back-command { justify-self: start; text-align: left; }
  .facts { grid-template-columns: minmax(128px, .9fr) minmax(0, 1.1fr); }
  .facts dt, .facts dd { padding: 9px 10px; }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121619;
    --surface: #1b2126;
    --surface-soft: #222a30;
    --text: #f2f5f7;
    --muted: #a9b4be;
    --line: #34404a;
    --brand: #70d1bb;
    --brand-soft: #183c35;
    --blue: #9bc8f4;
    --blue-soft: #20384f;
    --amber: #f1ca78;
    --amber-soft: #473919;
    --red: #f1a49a;
    --red-soft: #492722;
    --shadow: none;
  }
}
