:root {
  color-scheme: light;
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-muted: #f2f4f7;
  --text: #111827;
  --muted: #667085;
  --line: #d9dee7;
  --line-soft: #e7ebf0;
  --accent: #1d4ed8;
  --accent-dark: #153e9f;
  --success: #087443;
  --danger: #b42318;
  --shadow: 0 18px 50px rgba(17, 24, 39, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  font-family: Inter, "PingFang SC", "Microsoft YaHei", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

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

.app-shell {
  min-height: 100vh;
  padding: 20px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(320px, 420px) minmax(0, 1fr);
  gap: 16px;
  max-width: 1500px;
  margin: 0 auto;
  min-height: calc(100vh - 40px);
}

.panel,
.preview-area {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.input-panel {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.app-header,
.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
}

.header-actions {
  display: inline-flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
}

.topbar-actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 24px;
  line-height: 1.1;
  letter-spacing: 0;
}

.app-header p,
.empty-state span,
.drop-zone span {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.status-pill {
  border: 1px solid var(--line);
  background: var(--surface-muted);
  color: var(--muted);
  padding: 6px 9px;
  border-radius: 999px;
  font-size: 12px;
  white-space: nowrap;
}

.user-pill,
.header-link,
.text-button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  padding: 6px 9px;
  border-radius: 6px;
  font-size: 12px;
  line-height: 1.2;
  text-decoration: none;
  white-space: nowrap;
}

.text-button {
  cursor: pointer;
}

.header-link:hover,
.text-button:hover {
  background: var(--surface-muted);
}

.hidden {
  display: none !important;
}

.status-pill.ok {
  color: var(--success);
  border-color: #a6d8bf;
  background: #f0f8f4;
}

.status-pill.bad {
  color: var(--danger);
  border-color: #f3b6b0;
  background: #fff4f2;
}

.field-label,
.form-grid span {
  color: #344054;
  font-size: 12px;
  font-weight: 700;
}

textarea {
  width: 100%;
  min-height: 190px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 13px;
  color: var(--text);
  background: #fff;
  line-height: 1.55;
}

input {
  width: 100%;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
  color: var(--text);
}

.formula-preview {
  min-height: 92px;
  max-height: 190px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfd;
  padding: 12px;
}

.formula-preview-body {
  color: var(--text);
  font-size: 14px;
  line-height: 1.65;
  overflow-wrap: anywhere;
}

.formula-placeholder {
  color: var(--muted);
}

textarea:focus,
input:focus,
select:focus,
button:focus-visible,
.drop-zone:focus-visible {
  outline: 3px solid rgba(29, 78, 216, 0.18);
  outline-offset: 1px;
  border-color: var(--accent);
}

.drop-zone {
  min-height: 86px;
  border: 1px dashed #b9c2d0;
  border-radius: 8px;
  background: #fbfcfd;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 12px;
  align-items: center;
  padding: 14px;
}

.drop-zone.dragging {
  border-color: var(--accent);
  background: #f6f8ff;
}

.drop-zone strong {
  display: block;
  font-size: 14px;
  margin-bottom: 2px;
}

select {
  height: 42px;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
  color: var(--text);
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.form-grid label {
  display: grid;
  gap: 7px;
}

.actions {
  display: grid;
  grid-template-columns: 1fr 1.3fr;
  gap: 10px;
  margin-top: auto;
}

.primary-button,
.secondary-button,
.ghost-button,
.icon-button,
.danger-button,
.download-button {
  border: 1px solid var(--line);
  border-radius: 6px;
  height: 42px;
  padding: 0 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  text-decoration: none;
  font-weight: 700;
  font-size: 14px;
}

.icon-button {
  width: 42px;
  padding: 0;
  background: #fff;
  color: var(--text);
}

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

.primary-button:hover {
  background: #000;
}

.secondary-button,
.ghost-button,
.download-button {
  background: #fff;
  color: var(--text);
}

.secondary-button:hover,
.ghost-button:hover,
.icon-button:hover,
.danger-button:hover,
.download-button:hover {
  background: var(--surface-muted);
}

.danger-button {
  height: 34px;
  padding: 0 10px;
  color: var(--danger);
  border-color: #fac8c3;
  background: #fff;
}

button:disabled,
.download-button.disabled {
  opacity: 0.45;
  pointer-events: none;
}

svg {
  width: 17px;
  height: 17px;
  fill: currentColor;
  flex: 0 0 auto;
}

.preview-area {
  min-width: 0;
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr);
  gap: 14px;
  padding: 18px;
}

.eyebrow {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  margin-bottom: 5px;
}

h2 {
  font-size: 22px;
  letter-spacing: 0;
}

.timeline {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.timeline-item {
  border: 1px solid var(--line-soft);
  background: #fbfcfd;
  color: var(--muted);
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 13px;
  font-weight: 700;
}

.timeline-item.active {
  color: var(--accent);
  border-color: #b8c7f6;
  background: #f5f7ff;
}

.timeline-item.done {
  color: var(--success);
  border-color: #bfe5d0;
  background: #f4fbf7;
}

.timeline-item.error {
  color: var(--danger);
  border-color: #fac8c3;
  background: #fff6f5;
}

.generation-stream {
  height: 44px;
  min-height: 44px;
  overflow: hidden;
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  background: #fbfcfd;
  display: grid;
  grid-template-rows: 1fr 1fr;
  align-items: center;
  padding: 3px 12px;
}

.generation-stream-line {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 18px;
  font-size: 13px;
}

.generation-stream-line.previous {
  color: var(--muted);
  opacity: 0.48;
  filter: blur(1.4px);
}

.generation-stream-line.current {
  color: var(--text);
  font-weight: 700;
}

.generation-stream.is-rolling .generation-stream-line.previous {
  animation: generation-prev-line 420ms ease-out;
}

.generation-stream.is-rolling .generation-stream-line.current {
  animation: generation-current-line 420ms ease-out;
}

@keyframes generation-prev-line {
  from {
    opacity: 0.9;
    filter: blur(0);
    transform: translateY(18px);
  }
  to {
    opacity: 0.48;
    filter: blur(1.4px);
    transform: translateY(0);
  }
}

@keyframes generation-current-line {
  from {
    opacity: 0;
    transform: translateY(18px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.iframe-shell {
  position: relative;
  min-height: 480px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

iframe {
  width: 100%;
  height: 100%;
  min-height: 100%;
  border: 0;
  display: none;
  background: #fff;
}

iframe.ready {
  display: block;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 6px;
  text-align: center;
  padding: 24px;
}

.empty-state.hidden {
  display: none;
}

.history-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(17, 24, 39, 0.24);
  z-index: 20;
}

.history-panel {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 21;
  width: min(420px, 100vw);
  height: 100vh;
  background: var(--surface);
  border-left: 1px solid var(--line);
  box-shadow: -18px 0 50px rgba(17, 24, 39, 0.14);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.history-panel.hidden,
.history-backdrop.hidden {
  display: none;
}

.history-panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 18px;
  border-bottom: 1px solid var(--line-soft);
}

h3 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0;
}

.history-list {
  overflow: auto;
  padding: 10px;
}

.history-item {
  width: 100%;
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  display: grid;
  gap: 5px;
  padding: 11px 12px;
  text-align: left;
  cursor: pointer;
}

.history-item + .history-item {
  margin-top: 8px;
}

.history-item:hover {
  background: #fbfcfd;
  border-color: var(--line);
}

.history-item-main {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 700;
  font-size: 14px;
}

.history-item-meta,
.history-empty {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.history-empty {
  padding: 16px 8px;
}

.auth-shell,
.admin-shell {
  min-height: 100vh;
  padding: 20px;
  display: grid;
  place-items: center;
}

.auth-panel {
  width: min(430px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 24px;
  display: grid;
  gap: 18px;
}

.auth-panel p {
  color: var(--muted);
  margin-top: 8px;
  line-height: 1.5;
}

.auth-form,
.admin-card {
  display: grid;
  gap: 12px;
}

.auth-form label,
.admin-card label {
  display: grid;
  gap: 7px;
  color: #344054;
  font-size: 12px;
  font-weight: 700;
}

.form-message {
  min-height: 18px;
  color: var(--danger);
  font-size: 13px;
  line-height: 1.4;
}

.admin-shell {
  place-items: stretch;
  max-width: 1120px;
  margin: 0 auto;
}

.admin-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 16px;
}

.admin-grid {
  display: grid;
  grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
  gap: 16px;
}

.admin-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 18px;
  align-content: start;
}

.admin-card h2 {
  font-size: 18px;
}

.user-list {
  display: grid;
  gap: 8px;
}

.user-row {
  border: 1px solid var(--line-soft);
  border-radius: 6px;
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

.user-row div {
  min-width: 0;
  display: grid;
  gap: 3px;
}

.user-row strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-row span {
  color: var(--muted);
  font-size: 12px;
}

@media (max-width: 960px) {
  .app-shell {
    padding: 12px;
  }

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

  .actions,
  .form-grid,
  .timeline {
    grid-template-columns: 1fr;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .app-header,
  .admin-header {
    align-items: stretch;
    flex-direction: column;
  }

  .topbar-actions {
    justify-content: flex-end;
  }

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

  .iframe-shell {
    min-height: 520px;
  }
}
