:root {
  --bg: #f2f2f7;
  --surface: #ffffff;
  --text: #1c1c1e;
  --text-secondary: #6e6e73;
  --border: #e5e5ea;
  --accent: #007aff;
  --accent-text: #ffffff;
  --danger: #ff3b30;
  --success: #34c759;
  --warning: #ff9500;
  --radius: 12px;
  --nav-height: 64px;
  --header-height: 52px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: "Noto Serif SC", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", "WenQuanYi Micro Hei", "SimSun", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  color: var(--text);
  background: var(--bg);
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea, select {
  font-family: inherit;
  font-size: 16px;
}

button {
  border: none;
  background: var(--accent);
  color: var(--accent-text);
  padding: 12px 16px;
  border-radius: var(--radius);
  min-height: 44px;
  cursor: pointer;
}

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

button.secondary {
  background: var(--surface);
  color: var(--accent);
  border: 1px solid var(--border);
}

button.danger {
  background: var(--danger);
}

input, textarea, select {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  min-height: 44px;
}

textarea {
  min-height: 120px;
  resize: vertical;
}

#app {
  max-width: 430px;
  margin: 0 auto;
  min-height: 100%;
  background: var(--bg);
  position: relative;
}

/* Generic hidden state used by the SPA for banners/buttons/progress. */
.hidden { display: none !important; }
.view.hidden { display: none !important; }

.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  height: var(--header-height);
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
}

.app-header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

main#tab-content {
  padding: 12px 12px calc(var(--nav-height) + 16px);
  overflow-y: auto;
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 430px;
  height: var(--nav-height);
  background: rgba(255,255,255,0.96);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding-bottom: env(safe-area-inset-bottom);
  z-index: 20;
}

.nav-btn {
  flex: 1;
  background: transparent;
  color: var(--text-secondary);
  border-radius: 0;
  padding: 4px;
  font-size: 12px;
  min-height: 44px;
}

.nav-btn.active {
  color: var(--accent);
  font-weight: 600;
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}

.card h2 {
  margin: 0 0 10px 0;
  font-size: 17px;
}

.form-row {
  margin-bottom: 12px;
}

label {
  display: block;
  margin-bottom: 6px;
  font-size: 14px;
  color: var(--text-secondary);
}

.btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 12px 0;
}

.btn-row button { flex: 1; min-width: 64px; }

.file-input-wrap {
  position: relative;
  display: inline-block;
  width: 100%;
}

.file-input-wrap input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  width: 100%;
  height: 100%;
}

.thumb-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 10px;
}

.thumb-item {
  aspect-ratio: 1;
  background: var(--bg);
  border-radius: 8px;
  overflow: hidden;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.thumb-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.thumb-item .error-badge {
  position: absolute;
  bottom: 4px;
  left: 4px;
  right: 4px;
  background: rgba(255,59,48,0.9);
  color: white;
  font-size: 10px;
  padding: 2px 4px;
  border-radius: 4px;
  text-align: center;
}

.progress-bar {
  width: 100%;
  height: 8px;
  background: var(--border);
  border-radius: 4px;
  overflow: hidden;
  margin: 10px 0;
}

.progress-bar > div {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.2s ease;
}

.essay-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.essay-item .meta {
  display: flex;
  gap: 8px;
  align-items: center;
  font-size: 13px;
  color: var(--text-secondary);
}

.chip {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 12px;
  font-size: 12px;
  background: var(--border);
}

.chip.status-ocr_done { background: #e0f2fe; color: #0369a1; }
.chip.status-transcription_confirmed { background: #dcfce7; color: #166534; }
.chip.status-manual_edited { background: #e0f2fe; color: #0369a1; }
.chip.status-review_done { background: #dcfce7; color: #166534; }
.chip.status-review_demo { background: #fef3c7; color: #92400e; }
.chip.status-error { background: #fee2e2; color: #991b1b; }

.label-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 6px 0;
}

.label-positive {
  background: #dcfce7;
  color: #166534;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 13px;
}

.label-negative {
  background: #fee2e2;
  color: #991b1b;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 13px;
}

.paragraph-block {
  margin-bottom: 12px;
}

.paragraph-block .para-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.paragraph-block .para-actions button {
  flex: none;
  padding: 6px 10px;
  font-size: 13px;
  min-height: 32px;
}

.essay-select {
  margin-right: 10px;
  width: 20px;
  height: 20px;
}

.toolbar {
  position: sticky;
  top: var(--header-height);
  z-index: 9;
  background: rgba(242,242,247,0.96);
  padding: 8px 0;
  margin-bottom: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}

.toolbar button {
  flex: 1;
  font-size: 13px;
  padding: 10px 4px;
  min-height: 40px;
  min-width: 64px;
}

.page-order-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 10px 0;
}

.page-order-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: var(--bg);
  border-radius: 8px;
}

.page-order-item img {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 4px;
}

.page-order-item span {
  flex: 1;
  font-size: 14px;
}

.page-order-item button {
  padding: 6px 10px;
  font-size: 12px;
  min-height: 32px;
}

.paragraph-block .para-text {
  font-size: 15px;
  line-height: 1.7;
  margin: 6px 0;
  padding: 10px;
  background: var(--bg);
  border-radius: 8px;
}

.comment-item {
  background: #f0f9ff;
  border-left: 3px solid var(--accent);
  padding: 8px;
  margin: 4px 0;
  border-radius: 0 8px 8px 0;
  font-size: 14px;
}

.dialog {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

.dialog.hidden { display: none; }

.dialog-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.4);
}

.dialog-box {
  position: relative;
  background: var(--surface);
  width: calc(100% - 40px);
  max-width: 360px;
  border-radius: var(--radius);
  padding: 18px;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 16px;
}

.demo-banner {
  background: #fff7ed;
  border: 1px solid #fed7aa;
  color: #9a3412;
  padding: 10px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 12px;
}

.empty-state {
  text-align: center;
  color: var(--text-secondary);
  padding: 40px 20px;
}

/* Transcription confirmation UI */
.warn-text { color: #b45309; font-size: 13px; }
.ok-text { color: #15803d; font-size: 13px; }

.page-strip {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.page-thumb {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  text-decoration: none;
  color: var(--text-secondary);
  font-size: 12px;
}

.page-thumb img {
  width: 100%;
  height: 84px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border, #e5e5ea);
  background: #f2f2f7;
}

.uncertain-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.uncertain-chip {
  background: #fef3c7;
  color: #92400e;
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 12px;
}

.readiness-item { font-size: 13px; margin-bottom: 6px; }
.readiness-item .ok-text, .readiness-item .warn-text { display: block; }

@media (max-width: 374px) {
  :root { font-size: 15px; }
  .thumb-grid { grid-template-columns: repeat(2, 1fr); }
  .page-strip { grid-template-columns: repeat(2, 1fr); }
}

@media (min-width: 390px) {
  .thumb-grid { grid-template-columns: repeat(4, 1fr); }
}

/* Remote / reconnecting status banner (390px mobile first). */
.connection-banner {
  position: sticky;
  top: 0;
  z-index: 60;
  padding: 8px 14px;
  font-size: 13px;
  line-height: 1.4;
  text-align: center;
  color: #7a4b00;
  background: #fff4d6;
  border-bottom: 1px solid #f0d79a;
}
.connection-banner.offline {
  color: #7a1f16;
  background: #ffe1dd;
  border-bottom-color: #f3b3aa;
}
.connection-banner.auth {
  color: #123c6b;
  background: #e2eefc;
  border-bottom-color: #b6d3f5;
}
.connection-banner button {
  margin-left: 8px;
  padding: 4px 10px;
  font-size: 12px;
  border-radius: 8px;
  background: var(--accent);
}
.connection-banner.hidden { display: none; }

/* Upload progress counts line. */
.upload-counts { font-size: 13px; color: var(--text-secondary); margin: 6px 0; }
.upload-counts .fail { color: var(--danger); }
