/* Design tokens */
:root {
  --font-sans: 'Inter', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, monospace;

  --bg-root: #0b0f14;
  --bg-surface: #121820;
  --bg-elevated: #1a2230;
  --bg-input: #0f141c;
  --border: rgba(255, 255, 255, 0.07);
  --border-focus: rgba(59, 130, 246, 0.55);

  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;

  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-glow: rgba(59, 130, 246, 0.25);
  --success: #10b981;
  --success-bg: rgba(16, 185, 129, 0.12);
  --error: #f87171;
  --error-bg: rgba(248, 113, 113, 0.12);
  --warning: #fbbf24;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.35);
  --sidebar-width: 300px;
  --header-height: 56px;
  --status-height: 44px;
  --transition: 180ms ease;
}

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

html,
body {
  margin: 0;
  height: 100%;
}

body {
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-primary);
  background: var(--bg-root);
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--accent);
  text-decoration: none;
  transition: color var(--transition);
}

a:hover {
  color: #60a5fa;
}

/* ── App shell ── */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  height: var(--header-height);
  padding: 0 1.25rem;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 10;
}

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

.brand-icon {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, #3b82f6 0%, #6366f1 100%);
  display: grid;
  place-items: center;
  flex-shrink: 0;
  box-shadow: 0 0 20px var(--accent-glow);
}

.brand-icon svg {
  width: 18px;
  height: 18px;
  color: #fff;
}

.brand-text h1 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.brand-text span {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-weight: 400;
}

.header-nav {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-left: auto;
}

.nav-link {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.85rem;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-secondary);
  border: 1px solid transparent;
  transition: background var(--transition), color var(--transition), border-color var(--transition);
}

.nav-link:hover {
  color: var(--text-primary);
  background: var(--bg-elevated);
}

.nav-link.active {
  color: var(--text-primary);
  background: var(--bg-elevated);
  border-color: var(--border);
}

.nav-link svg {
  width: 15px;
  height: 15px;
  opacity: 0.75;
}

/* ── Editor layout ── */
.editor-layout {
  display: flex;
  flex: 1;
  min-height: 0;
  position: relative;
}

.job-panel {
  width: var(--sidebar-width);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  overflow: hidden;
  transition: width var(--transition), margin var(--transition);
}

.job-panel.collapsed {
  width: 0;
  border-right: none;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.1rem 0.75rem;
  border-bottom: 1px solid var(--border);
}

.panel-header h2 {
  margin: 0;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.1rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.panel-toggle {
  display: none;
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  z-index: 5;
  width: 28px;
  height: 48px;
  border: 1px solid var(--border);
  border-left: none;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--bg-elevated);
  color: var(--text-secondary);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), color var(--transition);
}

.panel-toggle:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}

.panel-toggle svg {
  width: 14px;
  height: 14px;
}

.designer-wrap {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: #e8eaed;
  position: relative;
}

#gc-designer-container {
  flex: 1;
  min-height: 0;
}

/* ── Form controls ── */
.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.field label {
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: 0.02em;
}

.field input,
.field textarea,
.field select {
  font-family: inherit;
  font-size: 0.85rem;
  padding: 0.55rem 0.7rem;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  transition: border-color var(--transition), box-shadow var(--transition);
  width: 100%;
}

.field input:focus,
.field textarea:focus,
.field select:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.field textarea {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  min-height: 88px;
  resize: vertical;
  line-height: 1.45;
}

.field-hint {
  font-size: 0.68rem;
  color: var(--text-muted);
  margin-top: -0.15rem;
}

.field-readonly {
  gap: 0.2rem;
}

.field-readonly .field-label {
  font-size: 0.72rem;
  font-weight: 500;
  color: var(--text-secondary);
  letter-spacing: 0.02em;
}

.field-readonly .field-value {
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--text-primary);
}

.sync-status {
  padding: 0.65rem 0.75rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.72rem;
  color: var(--text-secondary);
}

.sync-status .sync-line {
  font-family: var(--font-mono);
  line-height: 1.5;
  word-break: break-all;
}

.field.invalid textarea {
  border-color: rgba(248, 113, 113, 0.5);
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  padding: 0.6rem 1rem;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  transition: background var(--transition), transform 80ms ease, box-shadow var(--transition);
  white-space: nowrap;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn-primary {
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  color: #fff;
  box-shadow: 0 2px 8px var(--accent-glow);
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
  box-shadow: 0 4px 16px var(--accent-glow);
}

.btn-primary:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  padding: 0.4rem 0.6rem;
}

.btn-ghost:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.btn-block {
  width: 100%;
  margin-top: 0.25rem;
}

.btn svg {
  width: 16px;
  height: 16px;
}

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

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

/* ── Status bar ── */
.status-bar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-height: var(--status-height);
  padding: 0 1.25rem;
  font-size: 0.8rem;
  background: var(--bg-surface);
  border-top: 1px solid var(--border);
  color: var(--text-secondary);
  flex-shrink: 0;
  transition: background var(--transition), color var(--transition);
}

.status-bar:empty {
  display: none;
}

.status-bar .status-icon {
  flex-shrink: 0;
  width: 16px;
  height: 16px;
}

.status-bar.success {
  background: var(--success-bg);
  color: #6ee7b7;
}

.status-bar.error {
  background: var(--error-bg);
  color: var(--error);
}

.status-bar.loading {
  color: var(--text-secondary);
}

.status-bar a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.status-dismiss {
  margin-left: auto;
  background: none;
  border: none;
  color: inherit;
  opacity: 0.6;
  cursor: pointer;
  padding: 0.25rem;
  line-height: 0;
  border-radius: 4px;
}

.status-dismiss:hover {
  opacity: 1;
  background: rgba(255, 255, 255, 0.06);
}

/* ── Loading overlay ── */
.designer-loading {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  background: var(--bg-root);
  z-index: 4;
  transition: opacity 300ms ease;
}

.designer-loading.hidden {
  opacity: 0;
  pointer-events: none;
}

.designer-loading .spinner {
  width: 32px;
  height: 32px;
  border-width: 3px;
  border-color: var(--border);
  border-top-color: var(--accent);
}

.designer-loading p {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* ── Results page ── */
.page-content {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 1.75rem 2rem;
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.page-header h1 {
  margin: 0 0 0.25rem;
  font-size: 1.35rem;
  font-weight: 600;
  letter-spacing: -0.03em;
}

.page-header p {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.page-actions {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.stat-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 0.85rem 1rem;
}

.stat-card .label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.stat-card .value {
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: -0.03em;
}

.results-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

.results-table {
  width: 100%;
  border-collapse: collapse;
}

.results-table th,
.results-table td {
  padding: 0.85rem 1.1rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.results-table th {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  background: var(--bg-elevated);
}

.results-table tr:last-child td {
  border-bottom: none;
}

.results-table tbody tr {
  transition: background var(--transition);
}

.results-table tbody tr:hover {
  background: rgba(255, 255, 255, 0.02);
}

.cell-mono {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 500;
  background: var(--bg-elevated);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.outputs-grid {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.output-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.3rem 0.55rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.78rem;
}

.output-chip .key {
  font-weight: 600;
  color: var(--accent);
}

.output-chip .val {
  font-family: var(--font-mono);
  color: var(--text-primary);
}

.stat-mono {
  font-family: var(--font-mono);
  font-size: 0.82rem;
}

.results-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  padding: 1rem;
}

.run-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-elevated);
  padding: 1rem;
}

.run-card-header {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.85rem;
}

.run-title {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  color: var(--text-primary);
}

.run-meta {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  margin-top: 0.35rem;
  color: var(--text-muted);
  font-size: 0.78rem;
}

.run-empty {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.85rem;
}

.sheet-grid-list {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.sheet-grid-section {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.sheet-grid-title {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.sheet-grid-wrap {
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.sheet-grid {
  border-collapse: collapse;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  min-width: 100%;
}

.sheet-grid th,
.sheet-grid td {
  border: 1px solid var(--border);
  padding: 0.35rem 0.55rem;
  white-space: nowrap;
  min-width: 3rem;
}

.sheet-grid thead th {
  background: var(--bg-input);
  color: var(--text-muted);
  font-weight: 600;
  position: sticky;
  top: 0;
  z-index: 1;
}

.sheet-grid-corner,
.sheet-grid-row-header {
  background: var(--bg-input);
  color: var(--text-muted);
  font-weight: 600;
  position: sticky;
  left: 0;
  z-index: 1;
}

.sheet-grid tbody td {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.sheet-grid tbody td:empty {
  background: rgba(255, 255, 255, 0.01);
}

.empty-state,
.error-state {
  text-align: center;
  padding: 3.5rem 2rem;
}

.empty-state .icon,
.error-state .icon {
  width: 48px;
  height: 48px;
  margin: 0 auto 1rem;
  color: var(--text-muted);
  opacity: 0.5;
}

.empty-state h2,
.error-state h2 {
  margin: 0 0 0.5rem;
  font-size: 1rem;
  font-weight: 600;
}

.empty-state p,
.error-state p {
  margin: 0 0 1.25rem;
  color: var(--text-muted);
  font-size: 0.85rem;
  max-width: 360px;
  margin-left: auto;
  margin-right: auto;
}

.error-state {
  color: var(--error);
}

.error-state p {
  color: var(--error);
  opacity: 0.85;
}

.skeleton {
  background: linear-gradient(
    90deg,
    var(--bg-elevated) 25%,
    rgba(255, 255, 255, 0.04) 50%,
    var(--bg-elevated) 75%
  );
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: var(--radius-sm);
  height: 48px;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.skeleton-rows {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 1rem;
}

/* ── Responsive ── */
@media (max-width: 900px) {
  .job-panel {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 6;
    box-shadow: var(--shadow-md);
  }

  .job-panel.collapsed {
    width: 0;
    margin-left: calc(-1 * var(--sidebar-width));
  }

  .panel-toggle {
    display: flex;
  }

  .page-content {
    padding: 1rem;
  }

  .results-table {
    display: block;
    overflow-x: auto;
  }
}

@media (max-width: 600px) {
  .header-nav .nav-label {
    display: none;
  }

  .page-header {
    flex-direction: column;
  }
}
