:root {
  --canvas: oklch(1 0 0);
  --surface: oklch(0.975 0.003 38);
  --surface-strong: oklch(0.945 0.006 38);
  --ink: oklch(0.17 0.018 255);
  --ink-soft: oklch(0.37 0.018 255);
  --muted: oklch(0.5 0.012 255);
  --line: oklch(0.87 0.008 255);
  --line-strong: oklch(0.74 0.012 255);
  --rail: oklch(0.12 0.02 255);
  --rail-ink: oklch(0.96 0.004 255);
  --rail-muted: oklch(0.72 0.018 255);
  --primary: oklch(0.62 0.19 38);
  --primary-hover: oklch(0.55 0.2 38);
  --success: oklch(0.48 0.1 185);
  --success-soft: oklch(0.95 0.035 185);
  --danger: oklch(0.54 0.2 25);
  --focus: oklch(0.56 0.16 215);
  --space-xs: 0.25rem;
  --space-sm: 0.5rem;
  --space-md: 0.75rem;
  --space-lg: 1rem;
  --space-xl: 1.5rem;
  --space-2xl: 2.25rem;
  --radius: 0.5rem;
  --radius-sm: 0.375rem;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
  background: var(--canvas);
  font-synthesis: none;
}

* { box-sizing: border-box; }

body { margin: 0; min-width: 20rem; background: var(--canvas); }

[hidden] { display: none !important; }

.login-gate {
  display: grid;
  min-height: 100vh;
  padding: clamp(1.5rem, 6vw, 6rem);
  background: var(--canvas);
  place-items: center;
}

.login-gate-content { width: min(100%, 29rem); }
.login-gate-content h1 { margin-top: var(--space-sm); font-size: clamp(2.25rem, 6vw, 4rem); }
.login-gate-content p:not(.section-label) { max-width: 25rem; margin: 0; color: var(--ink-soft); font-size: 1rem; line-height: 1.6; }
.login-gate-button { display: inline-flex; align-items: center; margin-top: var(--space-xl); text-decoration: none; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

button, textarea { font: inherit; }

button { cursor: pointer; }

button:focus-visible, textarea:focus-visible, a:focus-visible {
  outline: 0.1875rem solid var(--focus);
  outline-offset: 0.1875rem;
}

.skip-link {
  position: fixed;
  z-index: 10;
  top: -4rem;
  left: var(--space-lg);
  padding: var(--space-sm) var(--space-md);
  color: var(--canvas);
  background: var(--ink);
  border-radius: var(--radius-sm);
}

.skip-link:focus { top: var(--space-lg); }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 14.75rem minmax(0, 1fr); }

.automation-rail {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding: 2rem 1rem 1.25rem;
  color: var(--rail-ink);
  background: var(--rail);
}

.rail-brand { font-size: 1.0625rem; font-weight: 700; }

.automation-nav { margin-top: 2.25rem; }

.nav-label, .section-label, .pane-kicker {
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

.nav-label { padding: 0 var(--space-sm) var(--space-sm); color: var(--rail-muted); }

.automation-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
  min-height: 2.75rem;
  padding: 0.625rem var(--space-sm);
  color: var(--rail-ink);
  text-decoration: none;
  border-radius: var(--radius-sm);
  border: 0;
  background: transparent;
  font: inherit;
  text-align: left;
  transition: background-color 180ms ease-out, color 180ms ease-out;
}

.automation-link:hover { background: oklch(0.19 0.02 255); }
.automation-link.is-active { background: oklch(0.22 0.023 255); }

.automation-link.is-active::before {
  width: 0.25rem;
  height: 1.5rem;
  margin-left: -0.5rem;
  content: "";
  background: var(--primary);
  border-radius: 0.125rem;
}

.automation-state { color: oklch(0.79 0.08 185); font-size: 0.75rem; }

.rail-footer { margin-top: auto; }
.rail-note { margin: 0; padding-top: var(--space-xl); color: var(--rail-muted); font-size: 0.8125rem; line-height: 1.5; }
.rail-account { display: flex; align-items: center; justify-content: space-between; gap: var(--space-sm); margin-top: var(--space-lg); padding-top: var(--space-md); border-top: 1px solid oklch(0.29 0.018 255); }
.account-name { min-width: 0; overflow: hidden; color: var(--rail-muted); font-size: 0.8125rem; text-overflow: ellipsis; white-space: nowrap; }
.rail-logout-button { flex: 0 0 auto; padding: 0; color: var(--rail-ink); font: inherit; font-size: 0.8125rem; font-weight: 650; border: 0; background: transparent; }
.rail-logout-button:hover { color: oklch(0.78 0.14 38); }

.workspace { width: min(100%, 100rem); margin: 0 auto; padding: 3rem clamp(1.25rem, 4vw, 4.5rem) 2rem; }

.workspace-header { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-xl); margin-bottom: var(--space-2xl); }

h1, h2, p { text-wrap: pretty; }
h1 { margin: var(--space-xs) 0 var(--space-sm); font-size: 2rem; line-height: 1.15; letter-spacing: 0; }
h2 { margin: var(--space-xs) 0 0; font-size: 1rem; line-height: 1.3; }

.workspace-subtitle { max-width: 44rem; margin: 0; color: var(--ink-soft); font-size: 1rem; line-height: 1.55; }

.history-tab { flex: 0 0 auto; min-height: 2.5rem; padding: 0.5rem 0.75rem; color: var(--ink-soft); font: inherit; font-size: 0.8125rem; font-weight: 650; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--canvas); }
.history-tab:hover { color: var(--ink); border-color: var(--line-strong); background: var(--surface); }

.workbench { display: grid; grid-template-columns: minmax(0, 1fr) minmax(20rem, 0.86fr); gap: var(--space-lg); }

.input-pane, .result-pane { display: flex; flex-direction: column; min-height: 36rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--canvas); overflow: hidden; }

.pane-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--space-sm); padding: var(--space-lg); border-bottom: 1px solid var(--line); }
.pane-kicker { color: var(--muted); font-size: 0.6875rem; }
.character-count, .result-status { color: var(--muted); font-size: 0.8125rem; white-space: nowrap; }

.script-editor { position: relative; display: flex; flex: 1; min-height: 0; }

textarea {
  width: 100%;
  min-height: 0;
  flex: 1;
  resize: vertical;
  padding: var(--space-lg);
  color: var(--ink);
  font-size: 1rem;
  line-height: 1.65;
  border: 0;
  background: transparent;
}

.input-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 0.65rem;
  color: var(--ink-soft);
  font-size: 0.875rem;
  font-weight: 650;
  background: oklch(1 0 0 / 0.82);
  backdrop-filter: blur(2px);
}

.loading-mark {
  width: 3.25rem;
  height: 3.25rem;
  object-fit: contain;
  filter: drop-shadow(0 0.45rem 0.35rem oklch(0.48 0.15 38 / 0.2));
  animation: mark-bounce 1100ms cubic-bezier(0.22, 0.8, 0.36, 1) infinite;
}

@keyframes mark-bounce {
  0%, 100% { transform: translateY(0) scale(1); }
  42% { transform: translateY(-0.6rem) scale(1.04, 0.96); }
  58% { transform: translateY(0.05rem) scale(0.97, 1.03); }
}

textarea::placeholder { color: oklch(0.5 0.012 255); opacity: 1; }

.input-actions, .result-actions { display: flex; align-items: center; gap: var(--space-sm); padding: var(--space-md) var(--space-lg); border-top: 1px solid var(--line); }
.form-message { flex: 1; min-height: 1.25rem; margin: 0; color: var(--muted); font-size: 0.8125rem; text-align: right; }
.form-message.is-error { color: var(--danger); }

.primary-button, .secondary-button {
  min-height: 2.625rem;
  padding: 0.625rem 0.875rem;
  border-radius: var(--radius-sm);
  font-weight: 650;
  font-size: 0.875rem;
  transition: transform 180ms ease-out, background-color 180ms ease-out, border-color 180ms ease-out;
}

.primary-button { color: var(--canvas); border: 1px solid var(--primary); background: var(--primary); }
.primary-button:hover:not(:disabled) { background: var(--primary-hover); }
.primary-button:active:not(:disabled) { transform: translateY(1px); }
.primary-button:disabled { opacity: 0.58; cursor: not-allowed; }

.secondary-button { color: var(--ink); border: 1px solid var(--line-strong); background: var(--canvas); }
.secondary-button:hover:not(:disabled) { border-color: var(--ink-soft); background: var(--surface); }
.secondary-button:disabled { color: var(--muted); cursor: not-allowed; }

.result-pane { background: var(--surface); }
.result-content { flex: 1; padding: var(--space-lg); overflow: auto; }
.result-content:focus { outline: none; }
.result-empty { display: grid; align-content: center; min-height: 100%; color: var(--muted); text-align: center; }
.result-empty p { margin: 0 0 var(--space-sm); color: var(--ink-soft); font-weight: 650; }
.result-empty span { max-width: 26rem; margin: 0 auto; font-size: 0.875rem; line-height: 1.55; }
.result-copy { margin: 0; white-space: pre-wrap; font-size: 1rem; line-height: 1.68; }
.result-error { margin: 0; color: var(--danger); line-height: 1.6; }
.result-status { padding: 0.25rem 0.5rem; border-radius: 999px; background: var(--surface-strong); }
.result-status[data-state="ready"] { color: oklch(0.31 0.08 185); background: var(--success-soft); }
.result-status[data-state="working"] { color: var(--ink-soft); background: var(--surface-strong); }
.result-status[data-state="blocked"] { color: var(--danger); background: oklch(0.96 0.025 25); }

.workflow-strip {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--space-lg);
  margin-top: 1.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--line);
}
.stage {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-md);
  color: var(--muted);
  text-align: left;
}
.stage-number {
  display: grid;
  flex: 0 0 auto;
  place-items: center;
  width: 2rem;
  height: 2rem;
  padding: 0;
  color: var(--ink-soft);
  font-size: 0.75rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  background: var(--canvas);
}
.stage strong, .stage > div span { display: block; }
.stage strong { color: var(--ink); font-size: 0.875rem; }
.stage div span { margin-top: 0.1875rem; font-size: 0.8125rem; line-height: 1.4; }
.stage.is-active .stage-number { color: var(--canvas); border-color: var(--primary); background: var(--primary); }
.stage.is-complete .stage-number { color: var(--canvas); border-color: var(--success); background: var(--success); }
.stage.is-active .stage-number {
  box-shadow: 0 0 0 0.3125rem oklch(0.62 0.19 38 / 0.13), 0 0 1.5rem oklch(0.62 0.19 38 / 0.32);
}
.stage.is-active strong { color: var(--primary-hover); }

.history-view { width: min(100%, 72rem); }
.run-history { display: grid; grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr)); gap: var(--space-sm); }
.history-empty { grid-column: 1 / -1; margin: 0; color: var(--muted); font-size: 0.875rem; }
.history-item { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: var(--space-sm); width: 100%; padding: var(--space-md); color: var(--ink); text-align: left; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--canvas); }
button.history-item:hover { border-color: var(--line-strong); background: var(--surface); }
.history-item-copy { min-width: 0; }
.history-item-copy strong, .history-item-copy span { display: block; }
.history-item-copy strong { overflow: hidden; font-size: 0.8125rem; text-overflow: ellipsis; white-space: nowrap; }
.history-item-copy span { margin-top: 0.1875rem; color: var(--muted); font-size: 0.75rem; }
.history-badge { align-self: center; padding: 0.2rem 0.4rem; color: var(--ink-soft); font-size: 0.6875rem; font-weight: 700; border-radius: 999px; background: var(--surface-strong); }
.history-badge.is-ready, .history-badge.is-fixed { color: oklch(0.31 0.08 185); background: var(--success-soft); }
.history-badge.is-blocked { color: var(--danger); background: oklch(0.96 0.025 25); }

@media (max-width: 66rem) {
  .app-shell { grid-template-columns: 12.25rem minmax(0, 1fr); }
  .workspace { padding: 2rem 1.5rem; }
  .workbench { grid-template-columns: 1fr; }
  .input-pane, .result-pane { min-height: 28rem; }
}

@media (max-width: 44rem) {
  .app-shell { display: block; }
  .automation-rail { min-height: auto; padding: 1rem; }
  .rail-brand { display: inline-block; }
  .automation-nav { margin-top: 1rem; }
  .nav-label, .rail-note { display: none; }
  .automation-link { max-width: 15rem; }
  .workspace { padding: 1.5rem 1rem; }
  .workspace-header { display: block; margin-bottom: 1.5rem; }
  .history-tab { margin-top: 1rem; }
  h1 { font-size: 1.75rem; }
  .input-pane, .result-pane { min-height: 25rem; }
  .workflow-strip { grid-template-columns: 1fr; gap: var(--space-md); }
  .stage { flex-direction: row; align-items: flex-start; justify-content: flex-start; text-align: left; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}
