/* 记工系统 v2.8 SPA — 移动端 + NAS 浏览器适配 */

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --success: #16a34a;
  --warn: #d97706;
  --border: #e2e8f0;
  --bg: #f1f5f9;
  --surface: #fff;
  --text: #0f172a;
  --muted: #64748b;
  --nav-h: 56px;
  --header-h: 52px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html { font-size: 16px; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  padding-top: calc(var(--nav-h) + 8px);
  padding-bottom: 16px;
}

[x-cloak] { display: none !important; }

/* 顶栏 */
.app-header {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: var(--header-h);
}

.app-header > div:last-child,
.app-header-actions { display: flex; gap: 6px; align-items: center; }

.app-title { font-size: 17px; font-weight: 700; margin: 0; white-space: nowrap; }
.app-user { font-size: 12px; color: var(--muted); }
.role-tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  background: #dbeafe;
  color: #1e40af;
}

/* 主内容 */
.app-main {
  max-width: 960px;
  margin: 0 auto;
  padding: 12px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 12px;
}

.panel-title { font-size: 16px; font-weight: 700; margin: 0 0 4px; }
.panel-desc { font-size: 13px; color: var(--muted); margin: 0 0 12px; }

/* 顶部 Tab 导航（记工录入/记工本/款号价格/工资结算/客户结算/数据概览） */
.top-nav {
  display: flex;
  gap: 4px;
  flex-wrap: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 0 12px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.top-nav .nav-item {
  flex: 1 1 0;
  min-width: 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 48px;
  padding: 6px 4px;
  border: none;
  border-radius: 8px;
  background: none;
  cursor: pointer;
  font-size: 11px;
  color: var(--muted);
  touch-action: manipulation;
  white-space: nowrap;
}

.top-nav .nav-item .icon { font-size: 18px; line-height: 1; }
.top-nav .nav-item.active {
  color: var(--primary);
  font-weight: 600;
  background: #eff6ff;
}

/* 不使用底部 Tab 栏；保留 .bottom-nav 隐藏规则以兼容旧缓存 */
.bottom-nav { display: none !important; }

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: var(--nav-h);
  padding: 6px 4px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 11px;
  color: var(--muted);
  touch-action: manipulation;
}

.nav-item .icon { font-size: 20px; line-height: 1; }
.nav-item.active { color: var(--primary); font-weight: 600; }

/* 兼容旧 desktop-tabs 类名 */
.desktop-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.tab-btn {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
  font-size: 14px;
  touch-action: manipulation;
}

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

@media (min-width: 768px) {
  body { padding-top: 8px; padding-bottom: 16px; }
  .top-nav .nav-item {
    flex-direction: row;
    gap: 6px;
    min-height: 40px;
    font-size: 13px;
    padding: 8px 12px;
  }
  .top-nav .nav-item .icon { font-size: 16px; }
}

@media (max-width: 767px) {
  body { padding-top: 8px; padding-bottom: calc(12px + var(--safe-b)); }
}

/* 表单 */
.form-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

@media (min-width: 480px) {
  .form-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (min-width: 768px) {
  .form-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
}

label.field-label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 4px;
}

input, select, textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px; /* 防止 iOS 缩放 */
  background: var(--surface);
  min-height: 44px;
}

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

/* 按钮 */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  cursor: pointer;
  touch-action: manipulation;
  white-space: nowrap;
}

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: var(--primary-dark); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-warn { background: var(--warn); color: #fff; }
.btn-ghost {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
}
.btn-block { width: 100%; }
.btn-sm { min-height: 36px; padding: 6px 12px; font-size: 13px; }
.btn:disabled { opacity: 0.5; pointer-events: none; }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }

/* 表格（移动端横向滚动） */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 -4px;
}

table { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 520px; }
th, td { padding: 10px 8px; border-bottom: 1px solid var(--border); text-align: left; }
th { background: #f8fafc; font-weight: 600; white-space: nowrap; position: sticky; top: 0; }

/* 卡片列表 */
.list-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 8px;
  background: #fafafa;
}

.list-card header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 6px;
  flex-wrap: wrap;
}

.list-card .meta { font-size: 13px; color: var(--muted); }
.list-card .amount { font-size: 15px; font-weight: 700; color: var(--primary); }

/* 状态 */
.hint {
  font-size: 12px;
  color: var(--muted);
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-radius: 8px;
  padding: 8px 10px;
  margin-bottom: 10px;
}

/* 款号工价：工序列表 */
.process-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.process-row {
  display: grid;
  grid-template-columns: 1fr 96px auto;
  gap: 8px;
  align-items: center;
}

.process-row input {
  width: 100%;
  min-height: 40px;
}

.msg-error { color: var(--danger); font-size: 13px; margin-top: 6px; }
.msg-success { color: var(--success); font-size: 13px; margin-top: 6px; }
.empty { text-align: center; color: var(--muted); padding: 32px 12px; font-size: 14px; }

/* HTMX 加载条 */
.global-indicator {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 200;
  pointer-events: none;
}

.htmx-indicator { opacity: 0; transition: opacity 0.2s; }
.htmx-request .htmx-indicator, .htmx-request.htmx-indicator { opacity: 1; }

.loading-bar {
  height: 3px;
  background: linear-gradient(90deg, var(--primary), #60a5fa);
  width: 0;
  transition: width 0.25s;
}
.htmx-request .loading-bar { width: 100%; }

/* 弹窗 */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,0.45);
  z-index: 300;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}

@media (min-width: 480px) {
  .overlay { align-items: center; padding: 16px; }
}

.dialog {
  background: var(--surface);
  border-radius: 16px 16px 0 0;
  padding: 20px;
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  overflow-y: auto;
}

@media (min-width: 480px) {
  .dialog { border-radius: 14px; }
}

.dialog-lg { max-width: 640px; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; flex-wrap: wrap; }

/* Toast */
.toast {
  position: fixed;
  top: calc(var(--header-h) + 8px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 400;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 14px;
  color: #fff;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  max-width: 90vw;
  text-align: center;
}

.toast.ok { background: var(--success); }
.toast.err { background: var(--danger); }

.no-perm { opacity: 0.45; pointer-events: none; }

/* 登录页 */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  padding-bottom: calc(20px + var(--safe-b));
}

.login-box {
  width: 100%;
  max-width: 400px;
}

.login-logo {
  font-size: 22px;
  font-weight: 700;
  margin: 0 0 4px;
  text-align: center;
}

.login-sub {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  margin: 0 0 20px;
}

.login-hint {
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  margin: 14px 0 0;
}

.backup-status { font-size: 14px; line-height: 1.8; }
.backup-status .ok { color: var(--success); font-weight: 600; }
.backup-status .bad { color: var(--danger); font-weight: 600; }

/* 概览统计卡片 */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

@media (min-width: 768px) {
  .stat-grid { grid-template-columns: repeat(3, 1fr); }
}

.stat-box {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  text-align: center;
}

.stat-label { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.stat-value { font-size: 18px; font-weight: 700; color: var(--primary); }
.sub-title { font-size: 14px; font-weight: 600; margin: 16px 0 8px; color: var(--muted); }

.settings-dialog { max-width: 720px; }
.settings-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 12px 0; }

.upload-box {
  margin-top: 6px;
  padding: 10px;
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: #f8fafc;
}
.upload-box input[type="file"] { width: 100%; font-size: 13px; }

.dialog-image {
  max-width: min(1100px, 98vw);
  width: 98vw;
  max-height: 96vh;
}
.image-viewer {
  margin-top: 8px;
  max-height: 85vh;
  overflow: auto;
  text-align: center;
  background: #0f172a;
  border-radius: 10px;
}
.image-viewer img {
  max-width: 100%;
  max-height: 82vh;
  object-fit: contain;
  display: inline-block;
}
.image-overlay {
  background: rgba(15, 23, 42, 0.88);
  align-items: stretch;
  padding: 8px;
}
.image-overlay .dialog {
  background: #fff;
  margin: auto;
  width: 100%;
}

.worker-cards {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.worker-card {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid #e5e7eb;
  background: #fff;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.15s;
}
.worker-card:hover {
  background: #f3f4f6;
}
.worker-card.active {
  background: #2563eb;
  color: #fff;
  border-color: #2563eb;
}
.worker-card .worker-name {
  font-weight: 500;
}
.worker-card .worker-count {
  font-size: 12px;
  opacity: 0.7;
}
