:root {
  color-scheme: dark;
  --page: #0d1117;
  --surface: #161b22;
  --surface-alt: #1c2330;
  --bar: #010409;
  --ink: #e6edf3;
  --muted: #8b949e;
  --line: #30363d;
  --blue: #4493f8;
  --blue-soft: rgb(56 139 253 / 15%);
  --on-blue: #ffffff;
  --primary: #1f6feb;
  --green: #3fb950;
  --green-soft: rgb(46 160 67 / 15%);
  --amber: #d29922;
  --amber-soft: rgb(187 128 9 / 15%);
  --red: #ff7b72;
  --red-soft: rgb(248 81 73 / 15%);
  --on-red: #ffffff;
  --danger: #da3633;
  --shadow: 0 8px 24px rgb(1 4 9 / 60%);
  --backdrop: rgb(1 4 9 / 70%);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  background: var(--page);
  color: var(--ink);
}

:root[data-theme="light"] {
  color-scheme: light;
  --page: #f6f8fa;
  --surface: #ffffff;
  --surface-alt: #f6f8fa;
  --bar: #0b1f33;
  --ink: #1f2328;
  --muted: #59636e;
  --line: #d0d7de;
  --blue: #0969da;
  --blue-soft: #ddf4ff;
  --on-blue: #ffffff;
  --primary: #0969da;
  --green: #1a7f37;
  --green-soft: #dafbe1;
  --amber: #9a6700;
  --amber-soft: #fff8c5;
  --red: #cf222e;
  --red-soft: #ffebe9;
  --on-red: #ffffff;
  --danger: #cf222e;
  --shadow: 0 8px 24px rgb(140 149 159 / 20%);
  --backdrop: rgb(31 35 40 / 50%);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-height: 100vh; background: var(--page); color: var(--ink); }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.5rem; font-weight: 600; }
h2 { font-size: 1rem; font-weight: 600; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 0.8rem; color: var(--muted); overflow-wrap: anywhere; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

input, select, button, .button { font: inherit; border-radius: 6px; }
input, select { width: 100%; min-width: 0; height: 32px; border: 1px solid var(--line); background: var(--page); color: var(--ink); padding: 0 10px; }
input::placeholder { color: var(--muted); }
button, .button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--line);
  background: var(--surface-alt);
  color: var(--ink);
  font-weight: 500;
  white-space: nowrap;
  text-decoration: none;
  cursor: pointer;
}
button:hover, .button:hover { border-color: var(--muted); }
button:disabled, .button.disabled { cursor: not-allowed; opacity: 0.5; }
.button.disabled { pointer-events: none; }
button:focus-visible, .button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--blue); outline-offset: 1px; }
.primary { border-color: transparent; background: var(--primary); color: var(--on-blue); }
.primary:hover { border-color: transparent; filter: brightness(1.1); }
.secondary { background: var(--surface-alt); }
.danger { color: var(--red); background: var(--surface-alt); }
.danger:hover { border-color: transparent; background: var(--danger); color: var(--on-red); }
.ghost { border-color: transparent; background: transparent; color: inherit; }
.ghost:hover { border-color: transparent; background: rgb(127 127 127 / 18%); }
.small { height: 28px; padding: 0 10px; font-size: 0.85rem; }
.icon { width: 28px; height: 28px; padding: 0; }
.theme-toggle { width: 32px; height: 32px; }
.theme-toggle svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
:root[data-theme="light"] .theme-toggle .icon-sun,
:root:not([data-theme="light"]) .theme-toggle .icon-moon { display: none; }

.app-bar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  height: 56px;
  padding: 0 24px;
  background: var(--bar);
  border-bottom: 1px solid var(--line);
  color: #f0f6fc;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 1rem; }
.brand-mark { width: 20px; height: 20px; border-radius: 5px; background: linear-gradient(135deg, #2f81f7, #3fb950); }
.header-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }
.identity { margin-right: 8px; color: #c9d1d9; font-size: 0.85rem; }

main { width: min(1440px, calc(100% - 48px)); margin: 0 auto; padding: 24px 0 64px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.subtitle { margin-top: 4px; color: var(--muted); }

.panel { border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat { display: grid; gap: 2px; padding: 14px 16px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.stat-label { color: var(--muted); font-size: 0.8rem; }
.stat-value { font-size: 1.5rem; font-weight: 600; }
.stat-value.is-ready { color: var(--green); }
.stat-value.is-progress { color: var(--blue); }
.stat-value.is-error { color: var(--red); }

.section-heading { display: flex; align-items: center; gap: 8px; padding: 14px 16px 0; }
.count { border-radius: 999px; background: var(--surface-alt); border: 1px solid var(--line); padding: 0 8px; font-size: 0.75rem; font-weight: 600; }
.fleet-toolbar { display: grid; grid-template-columns: minmax(240px, 2fr) repeat(3, minmax(140px, 1fr)); gap: 8px; padding: 12px 16px; }
#status { padding: 0 16px 10px; color: var(--muted); font-size: 0.85rem; }
.error { color: var(--red) !important; }

.table-wrap { position: relative; overflow-x: auto; border-top: 1px solid var(--line); }
.fleet-table { width: 100%; border-collapse: collapse; }
.fleet-table th { padding: 8px 12px; background: var(--surface-alt); color: var(--muted); font-size: 0.75rem; font-weight: 600; text-align: left; text-transform: uppercase; letter-spacing: 0.04em; border-bottom: 1px solid var(--line); white-space: nowrap; }
.fleet-table td { padding: 10px 12px; vertical-align: middle; border-bottom: 1px solid var(--line); }
.robot-group:last-child .robot-row td, .robot-group:last-child .detail-row td { border-bottom: 0; }
.robot-row:hover td { background: rgb(127 127 127 / 6%); }
.robot-row.is-open td { border-bottom-color: transparent; }
.col-expand { width: 44px; padding-right: 0 !important; }
.col-actions { width: 1%; text-align: right; }
.robot-name { font-weight: 600; overflow-wrap: anywhere; }
.release-id { max-width: 240px; overflow-wrap: anywhere; font-size: 0.85rem; }
.expires-at { white-space: nowrap; font-size: 0.85rem; }
.access { color: var(--muted); font-size: 0.8rem; }
.actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 6px; }
.col-actions .actions { flex-wrap: nowrap; }

.chevron { width: 8px; height: 8px; border-right: 2px solid currentColor; border-bottom: 2px solid currentColor; transform: rotate(-45deg); transition: transform 120ms ease; }
.expand[aria-expanded="true"] .chevron { transform: rotate(45deg); }

.state { display: inline-flex; align-items: center; gap: 6px; border-radius: 999px; padding: 2px 10px; background: var(--blue-soft); color: var(--blue); font-size: 0.8rem; font-weight: 500; white-space: nowrap; }
.state::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.state.is-ready { background: var(--green-soft); color: var(--green); }
.state.is-error { background: var(--red-soft); color: var(--red); }
.state.is-ending { background: var(--amber-soft); color: var(--amber); }

.detail-row td { padding-top: 0; background: var(--surface-alt); }
.robot-detail { display: flex; flex-wrap: wrap; gap: 16px 32px; padding: 14px 0 6px 44px; }
.robot-detail dl { flex: 2 1 480px; }
.share-block { flex: 1 1 280px; }
dl { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px 24px; margin: 0; }
dl div { display: grid; align-content: start; gap: 2px; }
dt { color: var(--muted); font-size: 0.75rem; }
dd { margin: 0; overflow-wrap: anywhere; }
.error-message { color: var(--red); }
.summary-note { color: var(--muted); }
.share-block { display: grid; align-content: start; gap: 8px; }
.share-form { display: flex; flex-wrap: wrap; gap: 6px; }
.share-form input { flex: 2 1 180px; }
.share-form select { flex: 1 1 110px; width: auto; }
.share-list { display: flex; flex-wrap: wrap; gap: 6px; color: var(--muted); font-size: 0.8rem; }
.share-list span { border: 1px solid var(--line); border-radius: 999px; padding: 1px 8px; }

.sign-in { width: min(400px, calc(100% - 32px)); margin: 15vh auto 0; padding: 32px; display: grid; gap: 12px; box-shadow: var(--shadow); }
.sign-in .brand { color: var(--muted); font-size: 0.85rem; }
#sign-in-message { color: var(--muted); }
body:not(.needs-auth) #sign-in { display: none; }
body.needs-auth #app { display: none; }

dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--ink); padding: 0; box-shadow: var(--shadow); }
dialog::backdrop { background: var(--backdrop); }
.form-dialog { width: min(480px, calc(100% - 32px)); }
.form-dialog form { display: grid; gap: 14px; padding: 20px; }
.form-dialog header { display: grid; gap: 4px; }
.form-dialog header p { color: var(--muted); }
.form-dialog label { display: grid; gap: 6px; font-weight: 500; }
.form-dialog footer { display: flex; justify-content: flex-end; gap: 8px; padding-top: 6px; }
.form-status { min-height: 1.2em; color: var(--muted); font-size: 0.85rem; }
#robot-viewer { width: min(1400px, calc(100% - 32px)); height: min(900px, calc(100% - 32px)); padding: 16px; }
#robot-viewer[open] { display: grid; grid-template-rows: auto 1fr; gap: 12px; }
#robot-viewer header { display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: 12px; }
.viewer-help { color: var(--muted); font-size: 0.85rem; }
#robot-viewer iframe { width: 100%; height: 100%; border: 1px solid var(--line); border-radius: 6px; background: #ffffff; }

@media (max-width: 1100px) {
  .release-id, .fleet-table th:nth-child(6) { display: none; }
}

@media (max-width: 820px) {
  main { width: calc(100% - 32px); }
  .app-bar { padding: 0 16px; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fleet-toolbar { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fleet-toolbar .search-field { grid-column: 1 / -1; }
  .page-head { flex-wrap: wrap; align-items: flex-start; }
  .col-actions .actions { flex-wrap: wrap; }
  .robot-detail { padding-left: 0; }
}
