:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --border: #e5e7eb;
  --border-strong: #d1d5db;
  --text: #111827;
  --text-secondary: #4b5563;
  --text-dim: #6b7280;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --primary-light: #eef2ff;
  --danger: #ef4444;
  --danger-bg: #fef2f2;
  --ok: #22c55e;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.04);
  --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.06), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.08), 0 4px 6px -2px rgba(0, 0, 0, 0.02);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

* {
  box-sizing: border-box;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

/* ---------- 通用 ---------- */
.screen {
  min-height: 100vh;
}

[hidden] {
  display: none !important;
}

.btn {
  appearance: none;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 8px 16px;
  font-size: 14px;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.btn:hover:not(:disabled) {
  background: #f9fafb;
  border-color: #9ca3af;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
  box-shadow: 0 1px 2px rgba(79, 70, 229, 0.2);
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.btn-ghost {
  border-color: transparent;
  background: transparent;
  color: var(--text-dim);
}

.btn-ghost:hover:not(:disabled) {
  background: #f3f4f6;
  color: var(--text);
}

.btn-block {
  width: 100%;
  padding: 10px 16px;
}

.btn-sm {
  padding: 5px 12px;
  font-size: 13px;
}

.icon-btn {
  border: none;
  background: transparent;
  font-size: 24px;
  line-height: 1;
  color: var(--text-dim);
  cursor: pointer;
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  transition: background 0.15s;
}

.icon-btn:hover {
  background: #f3f4f6;
}

.hint {
  font-size: 12px;
  color: var(--text-dim);
}

/* ---------- 登录页 ---------- */
.login-screen {
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 10% 20%, rgba(99, 102, 241, 0.08) 0%, transparent 40%),
    radial-gradient(circle at 90% 80%, rgba(79, 70, 229, 0.08) 0%, transparent 40%),
    var(--bg);
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 36px;
  width: min(400px, 100%);
  text-align: center;
}

.brand-mark.is-large {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  font-size: 18px;
  margin: 0 auto 16px;
}

.login-title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.login-sub {
  margin: 6px 0 24px;
  color: var(--text-dim);
  font-size: 14px;
}

.auth-tabs {
  display: flex;
  gap: 4px;
  background: #f3f4f6;
  border-radius: var(--radius-sm);
  padding: 4px;
  margin-bottom: 20px;
}

.auth-tab {
  flex: 1;
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-dim);
  padding: 8px;
  font-size: 14px;
  font-weight: 500;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
}

.auth-tab.is-active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.auth-form {
  text-align: left;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 14px;
}

.field.is-hidden {
  display: none;
}

.field span {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}

.field input {
  padding: 10px 12px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.field input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}

.form-error {
  background: var(--danger-bg);
  color: var(--danger);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 14px;
}

.login-hint {
  margin: 16px 0 0;
  font-size: 12px;
  color: var(--text-dim);
}

/* ---------- 顶部栏 ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: var(--primary);
  color: #fff;
  font-weight: 700;
  font-size: 12px;
  letter-spacing: 0.5px;
}

.brand-text {
  font-size: 15px;
  font-weight: 700;
}

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

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 4px 4px 12px;
  background: #f3f4f6;
  border-radius: 999px;
}

.user-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- 主布局 ---------- */
.app-main {
  display: grid;
  grid-template-columns: minmax(340px, 420px) 1fr;
  gap: 20px;
  align-items: start;
  padding: 20px 24px 40px;
  max-width: 1500px;
  margin: 0 auto;
}

@media (max-width: 920px) {
  .app-main {
    grid-template-columns: 1fr;
  }
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

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

.panel-head h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
}

/* ---------- 拖放区 ---------- */
.dropzone {
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius);
  background: #fafafa;
  min-height: 220px;
  display: grid;
  place-items: center;
  padding: 18px;
  cursor: pointer;
  transition: all 0.2s ease;
  outline: none;
}

.dropzone:hover,
.dropzone:focus-visible {
  border-color: var(--primary);
  background: var(--primary-light);
}

.dropzone.is-dragover {
  border-color: var(--primary);
  background: #e0e7ff;
}

.dropzone.has-image {
  cursor: default;
  border-style: solid;
  border-color: var(--border);
  background: var(--surface);
}

.dropzone.is-busy {
  pointer-events: none;
  opacity: 0.72;
}

.dropzone-empty {
  text-align: center;
  color: var(--text-dim);
  pointer-events: none;
}

.dz-icon {
  width: 38px;
  height: 38px;
  color: var(--border-strong);
  margin-bottom: 8px;
}

.dz-title {
  margin: 0;
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
}

.dz-sub {
  margin: 4px 0 0;
  font-size: 13px;
}

.preview-wrap {
  max-height: 320px;
  display: grid;
  place-items: center;
}

.preview-img {
  max-width: 100%;
  max-height: 300px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  display: block;
  box-shadow: var(--shadow-sm);
}

/* ---------- 按钮行 ---------- */
.btn-row {
  display: flex;
  gap: 8px;
  margin-top: 14px;
}

/* ---------- 元信息 ---------- */
.file-meta {
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-dim);
  font-family: var(--mono);
  word-break: break-all;
}

.switch {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 13px;
  color: var(--text-dim);
  cursor: pointer;
  user-select: none;
}

.switch input {
  accent-color: var(--primary);
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.alert {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
}

.alert-error {
  background: var(--danger-bg);
  border: 1px solid #fecaca;
  color: var(--danger);
}

/* ---------- 结果区 ---------- */
.tabs {
  display: flex;
  gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 12px;
}

.tab {
  appearance: none;
  border: none;
  background: transparent;
  padding: 8px 14px;
  font-size: 13px;
  font-family: inherit;
  color: var(--text-dim);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  font-weight: 500;
  transition: color 0.15s;
}

.tab:hover {
  color: var(--text);
}

.tab.is-active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.result-toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}

.empty-state,
.loading-state {
  text-align: center;
  padding: 64px 16px;
  color: var(--text-dim);
}

.empty-icon {
  font-size: 42px;
  margin-bottom: 10px;
  opacity: 0.7;
}

.empty-state p,
.loading-state p {
  margin: 0;
}

.empty-sub,
.loading-sub {
  font-size: 13px;
  margin-top: 6px !important;
}

.spinner {
  width: 28px;
  height: 28px;
  margin: 0 auto 14px;
  border: 2.5px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.tab-panel {
  display: none;
}

.tab-panel.is-active {
  display: block;
}

.result-body {
  min-height: 300px;
}

/* ---------- Markdown 渲染 ---------- */
.markdown-body {
  max-width: none;
  font-size: 14.5px;
  line-height: 1.75;
  word-wrap: break-word;
  color: var(--text);
}

.markdown-body > *:first-child {
  margin-top: 0;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  margin: 1.4em 0 0.6em;
  line-height: 1.35;
  font-weight: 700;
  color: var(--text);
}

.markdown-body h1 {
  font-size: 1.55em;
  padding-bottom: 0.3em;
  border-bottom: 1px solid var(--border);
}

.markdown-body h2 {
  font-size: 1.3em;
  padding-bottom: 0.25em;
  border-bottom: 1px solid var(--border);
}

.markdown-body h3 {
  font-size: 1.12em;
}

.markdown-body p {
  margin: 0.75em 0;
}

.markdown-body ul,
.markdown-body ol {
  padding-left: 1.7em;
  margin: 0.7em 0;
}

.markdown-body li {
  margin: 0.3em 0;
}

.markdown-body blockquote {
  margin: 0.9em 0;
  padding: 0.3em 1em;
  border-left: 3px solid var(--border-strong);
  color: var(--text-secondary);
  background: #f9fafb;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.markdown-body code {
  background: #f3f4f6;
  padding: 0.15em 0.4em;
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 0.88em;
}

.markdown-body pre {
  background: #f9fafb;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  overflow-x: auto;
}

.markdown-body pre code {
  background: none;
  padding: 0;
  font-size: 0.85em;
  line-height: 1.6;
}

.markdown-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 1em 0;
  font-size: 13.5px;
  display: block;
  overflow-x: auto;
}

.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border-strong);
  padding: 8px 12px;
  text-align: left;
}

.markdown-body th {
  background: #f9fafb;
  font-weight: 600;
}

.markdown-body img {
  max-width: 100%;
}

.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.6em 0;
}

.raw-markdown {
  background: #f9fafb;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 16px;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  max-height: 640px;
  overflow: auto;
  width: 100%;
  resize: vertical;
}

.raw-markdown:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.12);
}

/* ---------- 版面标注 ---------- */
.layout-stage {
  position: relative;
  display: inline-block;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: auto;
  background: #fafafa;
  padding: 8px;
}

.layout-stage img {
  display: block;
  max-width: 100%;
}

.layout-box {
  position: absolute;
  border: 2px solid;
  border-radius: 3px;
  cursor: pointer;
  transition: background 0.12s;
}

.layout-box:hover,
.layout-box.is-active {
  background: rgba(79, 70, 229, 0.14);
}

.b-text {
  border-color: #4f46e5;
}

.b-table {
  border-color: #22c55e;
}

.b-formula {
  border-color: #9333ea;
}

.b-image {
  border-color: #f59e0b;
}

.layout-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin-top: 12px;
  font-size: 12px;
  color: var(--text-dim);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.swatch {
  width: 11px;
  height: 11px;
  border-radius: 2px;
  border: 2px solid;
  display: inline-block;
}

.sw-text {
  border-color: #4f46e5;
}

.sw-table {
  border-color: #22c55e;
}

.sw-formula {
  border-color: #9333ea;
}

.sw-image {
  border-color: #f59e0b;
}

.block-tip {
  margin-top: 12px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #f9fafb;
  font-size: 13px;
  max-height: 180px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}

.block-tip[hidden] {
  display: none;
}

/* ---------- 历史抽屉 ---------- */
.drawer {
  position: fixed;
  inset: 0;
  z-index: 40;
}

.drawer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(17, 24, 39, 0.35);
  opacity: 0;
  animation: fadeIn 0.2s forwards;
}

.drawer-panel {
  position: absolute;
  top: 0;
  right: 0;
  width: min(420px, 90vw);
  height: 100%;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  animation: slideIn 0.25s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes fadeIn {
  to {
    opacity: 1;
  }
}

@keyframes slideIn {
  to {
    transform: translateX(0);
  }
}

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

.drawer-head h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
}

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.history-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin-bottom: 10px;
  cursor: pointer;
  transition: all 0.15s;
}

.history-item:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.history-thumb {
  width: 64px;
  height: 64px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #f3f4f6;
  flex-shrink: 0;
}

.history-body {
  flex: 1;
  min-width: 0;
}

.history-title {
  margin: 0 0 4px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-meta {
  margin: 0;
  font-size: 11px;
  color: var(--text-dim);
}

.history-delete {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px 6px;
  font-size: 13px;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.15s;
}

.history-item:hover .history-delete {
  opacity: 1;
}

.history-delete:hover {
  background: var(--danger-bg);
  color: var(--danger);
}

.empty-history {
  text-align: center;
  padding: 40px 16px;
  color: var(--text-dim);
}

.history-foot {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: center;
}

/* ---------- toast ---------- */
.toast {
  position: fixed;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  color: #fff;
  background: var(--text);
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}

.toast.is-visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
