/* Reset and base styles */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #f0f2f7;
  --surface: #ffffff;
  --surface2: #f8f9fc;
  --border: #e2e6ef;
  --primary: #3b6ef8;
  --primary-hover: #2b5ce8;
  --primary-light: #eef2ff;
  --success: #22c55e;
  --success-hover: #16a34a;
  --success-light: #f0fdf4;
  --warning: #f59e0b;
  --warning-hover: #d97706;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --purple: #8b5cf6;
  --purple-hover: #7c3aed;
  --purple-light: #f5f3ff;
  --text: #1e2a3b;
  --text-muted: #6b7a99;
  --text-light: #9aa5be;
  --shadow: 0 2px 12px rgba(30, 42, 59, 0.08);
  --shadow-md: 0 4px 24px rgba(30, 42, 59, 0.12);
  --radius: 14px;
  --radius-sm: 10px;
  --radius-xs: 6px;
  --font: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Noto Sans SC',
    'Microsoft YaHei', sans-serif;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --header-h: 56px;
  --tab-h: 56px;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  line-height: 1.6;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

/* ===== Header ===== */
.header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 8px rgba(30, 42, 59, 0.06);
  padding-top: var(--safe-top);
}

.header-inner {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  letter-spacing: -0.3px;
}

.brand-icon {
  width: 34px;
  height: 34px;
  background: linear-gradient(135deg, var(--primary), var(--purple));
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 17px;
}

.header-badge {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg);
  padding: 3px 10px;
  border-radius: 20px;
  border: 1px solid var(--border);
  white-space: nowrap;
}

/* ===== Bottom Tab Bar ===== */
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: 0 -2px 12px rgba(30, 42, 59, 0.06);
  padding-bottom: var(--safe-bottom);
  display: flex;
  height: calc(var(--tab-h) + var(--safe-bottom));
}

.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 6px 0;
  cursor: pointer;
  border: none;
  background: transparent;
  font-family: var(--font);
  transition: all 0.2s;
  -webkit-tap-highlight-color: transparent;
  position: relative;
}

.tab-item .tab-icon {
  font-size: 22px;
  line-height: 1;
  transition: transform 0.2s;
}

.tab-item .tab-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  transition: color 0.2s;
}

.tab-item.active .tab-label {
  color: var(--primary);
}

.tab-item.active .tab-icon {
  transform: scale(1.1);
}

.tab-item.active::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 32px;
  height: 3px;
  border-radius: 0 0 3px 3px;
  background: var(--primary);
}

.tab-item.purple-tab.active .tab-label {
  color: var(--purple);
}

.tab-item.purple-tab.active::before {
  background: var(--purple);
}

/* ===== Page Content ===== */
.page-content {
  padding: 16px 14px;
  padding-bottom: calc(var(--tab-h) + var(--safe-bottom) + 24px);
  min-height: calc(100vh - var(--header-h) - var(--tab-h));
  min-height: calc(100dvh - var(--header-h) - var(--tab-h));
}

/* ===== Module Panel ===== */
.module-panel {
  display: none;
  flex-direction: column;
  gap: 16px;
}

.module-panel.active {
  display: flex;
}

/* ===== Card ===== */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.card-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  gap: 12px;
  background: var(--surface2);
}

.card-icon {
  width: 42px;
  height: 42px;
  border-radius: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.card-icon.blue {
  background: #dbeafe;
  color: var(--primary);
}

.card-icon.purple {
  background: #ede9fe;
  color: var(--purple);
}

.card-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.4;
}

.card-desc {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 3px;
  line-height: 1.5;
}

.card-body {
  padding: 16px;
}

/* ===== Steps ===== */
.steps {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding: 0 4px;
}

.step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  color: var(--text-muted);
  flex: 1;
  position: relative;
}

.step-num {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--border);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.3s;
}

.step.active .step-num {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 2px 8px rgba(59, 110, 248, 0.35);
}

.step.done .step-num {
  background: var(--success);
  color: #fff;
  box-shadow: 0 2px 8px rgba(34, 197, 94, 0.35);
}

.step-connector {
  flex: 1;
  height: 2px;
  background: var(--border);
  margin: 0 4px;
  margin-bottom: 18px;
  transition: background 0.3s;
}

.step-connector.done {
  background: var(--success);
}

/* ===== Buttons ===== */
.btn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}

.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 14px 12px;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  font-family: var(--font);
  transition: all 0.15s;
  white-space: nowrap;
  letter-spacing: 0.2px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

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

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

.btn-outline {
  background: var(--surface);
  border: 1.5px solid var(--border);
  color: var(--text-muted);
}

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

.btn-primary {
  background: var(--primary);
  color: #fff;
}

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

.btn-success {
  background: var(--success);
  color: #fff;
}

.btn-success:not(:disabled):active {
  background: var(--success-hover);
}

.btn-warning {
  background: var(--warning);
  color: #fff;
}

.btn-warning:not(:disabled):active {
  background: var(--warning-hover);
}

.btn-purple {
  background: var(--purple);
  color: #fff;
}

.btn-purple:not(:disabled):active {
  background: var(--purple-hover);
}

.btn-full {
  grid-column: 1 / -1;
}

/* ===== Upload Zone ===== */
.upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 28px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
  background: var(--surface2);
  margin-bottom: 16px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.upload-zone:active,
.upload-zone.drag-over {
  border-color: var(--primary);
  background: var(--primary-light);
  color: var(--primary);
}

.upload-zone .upload-icon {
  font-size: 36px;
  margin-bottom: 8px;
  display: block;
}

.upload-zone .upload-hint {
  font-size: 12px;
  color: var(--text-light);
  margin-top: 6px;
}

.upload-zone.has-file {
  border-color: var(--success);
  background: var(--success-light);
  color: var(--success);
  border-style: solid;
}

/* ===== Messages ===== */
.msg {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 12px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  line-height: 1.5;
}

.msg.error {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #dc2626;
}

.msg.success {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: #16a34a;
}

.msg.info {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  color: #2563eb;
}

.msg.warning {
  background: #fffbeb;
  border: 1px solid #fde68a;
  color: #d97706;
}

.msg-icon {
  flex-shrink: 0;
  font-size: 15px;
  margin-top: 1px;
}

.msg-close {
  margin-left: auto;
  cursor: pointer;
  font-size: 15px;
  opacity: 0.6;
  padding: 0 4px;
  -webkit-tap-highlight-color: transparent;
}

.msg-close:active {
  opacity: 1;
}

/* ===== Preview Section ===== */
.preview-section {
  margin-top: 4px;
}

.preview-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.badge {
  background: var(--border);
  color: var(--text-muted);
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 600;
}

.badge.blue {
  background: #dbeafe;
  color: var(--primary);
}

.badge.green {
  background: #dcfce7;
  color: var(--success);
}

/* ===== Type Legend ===== */
.type-legend {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  color: var(--text-muted);
}

.legend-cell {
  position: relative;
  width: 24px;
  height: 20px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  overflow: hidden;
  flex-shrink: 0;
}

.legend-cell.is-text::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 8px 8px 0 0;
  border-color: #1daa4e transparent transparent transparent;
}

.legend-cell.is-num {
  text-align: center;
  font-size: 9px;
  font-weight: 700;
  color: #d97706;
  line-height: 20px;
}

/* ===== Table ===== */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius-xs);
  border: 1px solid var(--border);
  margin: 0 -4px;
  padding: 0 4px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  min-width: 480px;
}

thead tr {
  background: var(--surface2);
}

thead th {
  padding: 10px 10px;
  text-align: left;
  font-weight: 600;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  font-size: 12px;
}

tbody tr:nth-child(even) {
  background: var(--surface2);
}

tbody tr:active {
  background: #f0f4ff;
}

tbody td {
  padding: 9px 10px;
  color: var(--text);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

tbody tr:last-child td {
  border-bottom: none;
}

td.cell-text {
  position: relative;
}

td.cell-text::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 7px 7px 0 0;
  border-color: #1daa4e transparent transparent transparent;
  pointer-events: none;
}

td.cell-num {
  text-align: center;
  color: #d97706;
  font-weight: 700;
}

td.cell-empty {
  color: var(--text-light);
  font-style: italic;
}

.table-more {
  font-size: 11px;
  color: var(--text-light);
  text-align: center;
  padding: 8px;
  background: var(--surface2);
  border-top: 1px solid var(--border);
}

/* ===== Mapping Info ===== */
.mapping-info {
  font-size: 12px;
  color: var(--text-muted);
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  margin-bottom: 12px;
  line-height: 1.5;
}

.mapping-info strong {
  color: var(--warning);
}

/* ===== Stats Bar ===== */
.stats-bar {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.stat {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
}

.stat strong {
  color: var(--text);
  font-size: 18px;
  font-weight: 700;
}

/* ===== Divider ===== */
.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 16px 0;
}

/* ===== Hidden file input ===== */
input[type='file'] {
  display: none;
}

/* ===== Spin animation ===== */
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.spin {
  display: inline-block;
  animation: spin 0.8s linear infinite;
}

/* ===== Desktop enhancement ===== */
@media (min-width: 768px) {
  .page-content {
    max-width: 720px;
    margin: 0 auto;
    padding: 24px 20px;
    padding-bottom: calc(var(--tab-h) + var(--safe-bottom) + 32px);
  }

  .btn-grid {
    grid-template-columns: 1fr 1fr;
    max-width: 500px;
  }

  .tab-bar {
    max-width: 720px;
    left: 50%;
    transform: translateX(-50%);
    border-radius: var(--radius) var(--radius) 0 0;
  }
}

@media (min-width: 1024px) {
  .page-content {
    max-width: 800px;
  }

  .tab-bar {
    max-width: 800px;
  }

  .btn-grid {
    grid-template-columns: repeat(4, 1fr);
    max-width: none;
  }

  .btn-full {
    grid-column: auto;
  }
}
