:root {
  color-scheme: light;
  --bg: #edf1f5;
  --surface: #ffffff;
  --surface-soft: #f7f9fb;
  --surface-alt: #f9fcff;
  --line: #d7dee7;
  --line-strong: #b9c4d0;
  --text: #17212b;
  --muted: #667384;
  --accent: #0f6f63;
  --accent-dark: #0a564d;
  --rapid: #586273;
  --feed: #b63f18;
  --finish: #1269a8;
  --rough: #9a650f;
  --danger: #b42318;
  --warning: #a15c07;
  --ok: #157f3b;
  --tool: #101827;
  --played: #15905d;
  --shadow: 0 16px 40px rgba(29, 39, 54, 0.10);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, Arial, Helvetica, sans-serif;
  font-size: 14px;
  line-height: 1.6;
}

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

button,
.file-button span {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}

button:hover,
.file-button span:hover {
  border-color: var(--line-strong);
  background: var(--surface-soft);
}

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

button.primary,
.primary,
#generateBtn {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

button.primary:hover,
.primary:hover,
#generateBtn:hover {
  background: var(--accent-dark);
  border-color: var(--accent-dark);
}

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(240px, 1.4fr) auto minmax(210px, 300px);
  align-items: center;
  gap: 16px;
  padding: 18px 22px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

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

.brand-mark {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
}

.brand h1 {
  margin: 0;
  font-size: 18px;
  line-height: 1.05;
}

.brand p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

.action-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface-soft);
}

.file-button input {
  display: none;
}

.status-strip {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: end;
  gap: 10px;
}

.status-group {
  display: grid;
  gap: 6px;
  min-width: 130px;
}

.status-label {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  max-width: 190px;
  padding: 0 9px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-pill.ok {
  color: var(--ok);
  border-color: #bde7c9;
  background: #eefaf1;
}

.status-pill.error {
  color: var(--danger);
  border-color: #f4c7c3;
  background: #fff2f0;
}

.status-pill.pending {
  color: var(--warning);
  border-color: #f3d3a1;
  background: #fff7e8;
}

#logoutBtn {
  min-height: 30px;
  padding: 0 10px;
  border-radius: 8px;
  font-size: 12px;
}

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(135deg, rgba(237, 241, 245, 0.94), rgba(226, 236, 239, 0.94)),
    repeating-linear-gradient(90deg, rgba(23, 33, 43, 0.06) 0, rgba(23, 33, 43, 0.06) 1px, transparent 1px, transparent 28px);
}

.auth-gate[hidden] {
  display: none;
}

.auth-card {
  display: grid;
  gap: 14px;
  width: min(100%, 420px);
  padding: 22px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.auth-card-head {
  display: flex;
  align-items: center;
  gap: 14px;
}

.auth-card h2 {
  margin: 0;
  font-size: 18px;
  line-height: 1.15;
}

.auth-card p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.auth-error {
  margin: 0;
  padding: 8px 10px;
  border: 1px solid #f4c7c3;
  border-radius: 6px;
  background: #fff2f0;
  color: var(--danger);
  font-size: 13px;
}

.auth-divider {
  position: relative;
  display: grid;
  place-items: center;
  margin: 4px 0;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.auth-divider::before {
  content: "";
  position: absolute;
  inset-inline: 0;
  top: 50%;
  border-top: 1px solid var(--line);
}

.auth-divider span {
  position: relative;
  padding: 0 10px;
  background: var(--surface);
}

.google-signin-slot {
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px dashed var(--line-strong);
  border-radius: 6px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 6px;
  text-decoration: none;
}

.google-signin-slot[aria-disabled="false"] {
  border-style: solid;
  color: var(--ink);
  font-weight: 600;
  background: linear-gradient(180deg, #ffffff, #f6f8fb);
}

.auth-help {
  margin: -2px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 420px;
  min-height: 0;
  flex: 1;
}

.viewer-panel {
  position: relative;
  min-width: 0;
  min-height: calc(100vh - 92px);
  height: calc(100vh - 92px);
  background: linear-gradient(180deg, #f8fbfd 0%, #edf4f8 40%, #e8eef3 100%);
  overflow: hidden;
}

.viewer-header {
  position: absolute;
  z-index: 2;
  top: 18px;
  left: 18px;
  right: 18px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  pointer-events: none;
}

.metric-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(86px, 1fr));
  gap: 8px;
  max-width: 600px;
  pointer-events: auto;
}

.metric-row div {
  min-width: 0;
  padding: 9px 10px;
  border: 1px solid rgba(215, 222, 231, 0.92);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 8px 22px rgba(20, 31, 44, 0.08);
}

.metric-row span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.2;
}

.metric-row strong {
  display: block;
  margin-top: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
}

.viewer-tools {
  display: flex;
  gap: 8px;
  pointer-events: auto;
}

.viewer-tools button {
  width: 38px;
  min-height: 38px;
  padding: 0;
  font-size: 18px;
}

#plotCanvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: crosshair;
}

.canvas-hud {
  position: absolute;
  left: 14px;
  bottom: 14px;
  display: flex;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  pointer-events: none;
}

.canvas-hud span {
  padding: 6px 8px;
  border: 1px solid rgba(215, 222, 231, 0.92);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.9);
  font-variant-numeric: tabular-nums;
}

.side-panel {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 92px);
  min-height: 0;
  overflow: hidden;
  border-left: 1px solid var(--line);
  background: #e7edf3;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(94px, 1fr));
  gap: 8px;
  padding: 12px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
}

.tab {
  min-height: 40px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: #f4f7fb;
  color: var(--text);
  transition: background 0.2s ease, border-color 0.2s ease;
}

.tab:hover {
  background: #e8f0f5;
}

.tab.active {
  border-color: var(--accent);
  background: #eef8f6;
  color: var(--accent-dark);
  font-weight: 700;
}

.tab-pane {
  flex: 1 1 auto;
  display: none;
  min-height: 0;
  padding: 10px;
  overflow: auto;
}

.tab-pane.active {
  display: grid;
  gap: 10px;
}

.panel {
  background: var(--surface);
  border: 1px solid rgba(215, 222, 231, 0.95);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 10px 28px rgba(10, 18, 30, 0.04);
}

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

.panel-title h2 {
  margin: 0;
  font-size: 14px;
  line-height: 1.2;
}

.panel-title span {
  color: var(--muted);
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.template-library-panel {
  display: grid;
  gap: 10px;
}

.template-library-controls,
.template-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.template-library-controls button,
.template-actions button {
  width: 100%;
}

.template-actions {
  grid-template-columns: 1fr 88px;
}

.profile-resize-panel {
  display: grid;
  gap: 10px;
}

.profile-resize-actions {
  display: grid;
  grid-template-columns: 1fr 92px;
  gap: 8px;
}

.profile-resize-actions button {
  width: 100%;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
}

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding-top: 18px;
}

.checkbox-field input {
  width: auto;
  min-height: 0;
}

.algorithm-field {
  grid-column: 1 / -1;
}

input,
select,
textarea {
  width: 100%;
  min-height: 34px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}

input:focus,
select:focus,
textarea:focus,
button:focus-visible,
.file-button span:focus-visible {
  outline: 2px solid rgba(15, 111, 99, 0.28);
  outline-offset: 1px;
}

input[type="range"] {
  min-height: 24px;
  padding: 0;
  cursor: pointer;
}

input[type="range"]:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.stats-list {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px 12px;
  margin: 0;
  font-size: 13px;
}

.stats-list dt {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stats-list dd {
  margin: 0;
  font-variant-numeric: tabular-nums;
}

.message-list {
  display: grid;
  gap: 7px;
  min-height: 24px;
  font-size: 13px;
}

.message {
  padding: 8px 9px;
  border-radius: 6px;
  background: var(--surface-soft);
  border: 1px solid var(--line);
  line-height: 1.35;
}

.message.error {
  color: var(--danger);
  border-color: #f4c7c3;
  background: #fff2f0;
}

body.dashboard-host .topbar-actions,
body.dashboard-host .viewer-panel,
body.dashboard-host .tab[data-tab="setup"],
body.dashboard-host .tab[data-tab="output"] {
  display: none;
}

body.dashboard-host .topbar {
  grid-template-columns: minmax(240px, 1fr) minmax(210px, 360px);
}

body.dashboard-host .workspace {
  display: block;
  min-height: calc(100vh - 92px);
  background: #e7edf3;
}

body.dashboard-host .side-panel {
  width: min(100%, 980px);
  height: calc(100vh - 92px);
  margin: 0 auto;
  border-left: 0;
}

body.dashboard-host .tabs {
  grid-template-columns: 1fr;
}

body.dashboard-host .tab-pane[data-pane="dashboard"].active {
  display: grid;
  gap: 10px;
}

.dashboard-panel {
  display: grid;
  gap: 10px;
}

.dashboard-panel .panel-title {
  margin-bottom: 0;
}

.dashboard-panel .panel-title button {
  width: 34px;
  min-height: 34px;
  padding: 0;
}

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

.dashboard-metrics div {
  min-width: 0;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-soft);
}

.dashboard-metrics span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.25;
}

.dashboard-metrics strong {
  display: block;
  margin-top: 3px;
  font-size: 16px;
  font-variant-numeric: tabular-nums;
}

.dashboard-message {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.create-user-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.create-user-form button {
  grid-column: 1 / -1;
}

.dashboard-table-wrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.dashboard-table {
  width: 100%;
  min-width: 560px;
  border-collapse: collapse;
  background: #fff;
  font-size: 12px;
}

.dashboard-table th,
.dashboard-table td {
  padding: 8px 9px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.dashboard-table th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f4f7fb;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.dashboard-table tr:last-child td {
  border-bottom: 0;
}

.user-cell {
  white-space: pre-line;
  overflow-wrap: anywhere;
}

.ok-text {
  color: var(--ok);
  font-weight: 700;
}

.danger-text {
  color: var(--danger);
  font-weight: 700;
}

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

.dashboard-actions button {
  min-height: 30px;
  padding: 0 9px;
  border-radius: 7px;
  font-size: 12px;
}

.audit-event-list {
  display: grid;
  gap: 8px;
  max-height: 420px;
  overflow: auto;
}

.audit-event {
  display: grid;
  gap: 5px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  font-size: 12px;
}

.audit-event.empty {
  color: var(--muted);
}

.audit-event-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.audit-event-head strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.audit-event-head span {
  flex: 0 0 auto;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.audit-event p {
  margin: 0;
  color: var(--muted);
  overflow-wrap: anywhere;
}

.audit-event code {
  display: block;
  max-width: 100%;
  padding: 5px 6px;
  overflow: auto;
  border-radius: 5px;
  background: #eef3f7;
  color: #344153;
  font-size: 11px;
  line-height: 1.35;
}

.move-readout {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.speed-control {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr) 62px;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}

.speed-control span {
  color: var(--text);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.current-line {
  min-height: 78px;
  margin: 0;
  padding: 10px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #0f1720;
  color: #d9e4ee;
  font-family: Consolas, "Courier New", monospace;
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.compact-current-line {
  display: none;
}

.tab-pane[data-pane="output"].active {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

.nc-panel {
  flex: 1;
  min-height: 0;
  max-height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.nc-playback-card {
  flex: 0 0 auto;
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #f8fbfd;
}

.nc-playback-head,
.nc-playback-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.nc-playback-head {
  flex-wrap: wrap;
}

.nc-playback-head strong {
  color: var(--text);
  font-size: 13px;
}

.follow-toggle {
  display: inline-flex;
  grid-auto-flow: column;
  align-items: center;
  gap: 6px;
  min-height: 0;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}

.follow-toggle input {
  width: auto;
  min-height: 0;
  margin: 0;
}

.nc-playback-row {
  display: grid;
  grid-template-columns: 36px minmax(68px, 0.8fr) 36px minmax(0, 1.8fr);
  gap: 6px;
  align-items: center;
}

.nc-playback-row button {
  width: 100%;
  min-height: 34px;
  padding: 0 8px;
}

.nc-playback-tuning {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(120px, 0.8fr);
  gap: 8px;
}

.speed-control.compact {
  grid-template-columns: 42px minmax(0, 1fr) 48px;
  gap: 6px;
  min-width: 0;
}

.speed-control.compact input[type="number"] {
  min-width: 0;
}

.nc-diagnostics {
  flex: 0 0 auto;
  display: grid;
  gap: 5px;
  padding: 8px 10px;
  border: 1px solid #f3d3a1;
  border-radius: 8px;
  background: #fff7e8;
  color: #6e4305;
  font-size: 12px;
  line-height: 1.35;
}

.nc-diagnostics.empty {
  border-color: var(--line);
  background: var(--surface-soft);
  color: var(--muted);
}

.nc-diagnostics.error {
  border-color: #f4c7c3;
  background: #fff2f0;
  color: var(--danger);
}

.nc-diagnostics strong {
  color: inherit;
}

.nc-diagnostic-line {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nc-current-pin {
  flex: 0 0 auto;
  padding: 9px 10px;
  border: 1px solid #e7c36a;
  border-radius: 8px;
  background: #fff7d8;
  color: #5c3f03;
  font-family: Consolas, "Courier New", monospace;
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.nc-current-pin.empty {
  border-color: var(--line);
  background: var(--surface-soft);
  color: var(--muted);
  font-family: inherit;
}

.nc-line-viewer {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 6px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0f1720;
  color: #d9e4ee;
  font-family: Consolas, "Courier New", monospace;
  font-size: 12px;
  line-height: 1.45;
}

.nc-line-viewer.empty {
  display: grid;
  place-items: center;
  min-height: 180px;
  padding: 16px;
  color: #7f8b99;
  text-align: center;
}

.nc-line {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr);
  gap: 10px;
  min-height: 22px;
  padding: 2px 10px 2px 0;
  border-left: 4px solid transparent;
  white-space: pre;
}

.nc-line:nth-child(odd) {
  background: rgba(255, 255, 255, 0.018);
}

.nc-line.active {
  border-left-color: #f5b942;
  background: rgba(245, 185, 66, 0.2);
  color: #fff7d6;
  box-shadow: inset 0 0 0 1px rgba(245, 185, 66, 0.26);
}

.nc-line-number {
  color: #718096;
  text-align: right;
  user-select: none;
}

.nc-line.active .nc-line-number {
  color: #ffd27a;
}

.nc-line-code {
  overflow: visible;
}

@media (max-width: 1120px) {
  .topbar {
    grid-template-columns: 1fr;
  }

  .topbar-actions,
  .status-strip {
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .viewer-panel {
    min-height: 62vh;
    height: 62vh;
  }

  .side-panel {
    height: auto;
    overflow: visible;
    border-left: 0;
    border-top: 1px solid var(--line);
  }

  .tab-pane {
    max-height: none;
  }

  .nc-panel {
    min-height: 420px;
  }

  .nc-line-viewer {
    min-height: 240px;
  }

}

@media (max-width: 640px) {
  .topbar {
    padding: 10px;
  }

  .topbar-actions,
  .action-group {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .topbar-actions button,
  .topbar-actions .file-button span {
    width: 100%;
  }

  .metric-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    max-width: calc(100vw - 86px);
  }

  .metric-row div {
    padding: 7px 8px;
  }

  .metric-row strong {
    font-size: 13px;
  }

  .field-grid {
    grid-template-columns: 1fr;
  }

  .create-user-form,
  .dashboard-metrics {
    grid-template-columns: 1fr;
  }

  .checkbox-field {
    min-height: auto;
    padding-top: 0;
  }
}
