* { box-sizing: border-box; }
[hidden] { display: none !important; }

:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --line: #e5e8ef;
  --text: #1f2937;
  --muted: #8a94a6;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --pending: #94a3b8;
  --allocated: #2563eb;
  --shipped: #d97706;
  --settled: #059669;
  --danger: #dc2626;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  font-size: 14px;
}

/* ---------------- 顶栏 ---------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 38px; height: 38px; flex: 0 0 38px;
  display: grid; place-items: center;
  background: var(--primary);
  color: #fff;
  border-radius: 10px;
  font-weight: 700;
  font-size: 18px;
}
.brand-text h1 { margin: 0; font-size: 17px; font-weight: 600; }
.brand-text p { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.topbar-right .inline { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }
.topbar-right input {
  width: 110px; padding: 6px 10px; border: 1px solid var(--line);
  border-radius: 8px; font-size: 13px; color: var(--text);
}

/* ---------------- Tabs ---------------- */
.tabs {
  display: flex; gap: 4px; padding: 10px 24px 0;
  background: #fff; border-bottom: 1px solid var(--line);
  position: sticky; top: 67px; z-index: 19;
  overflow-x: auto;
}
.tab {
  border: none; background: transparent; cursor: pointer;
  padding: 9px 16px; border-radius: 8px 8px 0 0;
  font-size: 14px; color: var(--muted); font-family: inherit;
  border-bottom: 2px solid transparent;
}
.tab:hover { color: var(--text); background: #f8fafc; }
.tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

/* ---------------- 布局 ---------------- */
main { padding: 20px 24px 60px; max-width: 1280px; margin: 0 auto; }
.panel { display: none; }
.panel.active { display: block; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 18px 20px;
  margin-bottom: 18px;
  box-shadow: 0 1px 2px rgba(16, 24, 40, .04);
}
.card h2 {
  margin: 0 0 14px;
  font-size: 15px;
  font-weight: 600;
  padding-left: 10px;
  border-left: 3px solid var(--primary);
}

/* ---------------- 表单 ---------------- */
label { display: block; font-size: 13px; color: #4b5563; margin-bottom: 6px; }
label.full { margin-top: 4px; }
input, select, textarea {
  width: 100%;
  padding: 8px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: #fff;
  outline: none;
}
textarea { resize: vertical; font-family: ui-monospace, Menlo, Consolas, monospace; letter-spacing: .3px; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, .12); }
input[disabled] { background: #f3f4f6; color: var(--muted); }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 12px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 14px; margin-bottom: 12px; }
@media (max-width: 820px) { .grid2, .grid3 { grid-template-columns: 1fr; } }

.inline { display: flex; align-items: center; gap: 8px; }
.inline-narrow { max-width: 260px; }
.inline-num { display: flex; gap: 8px; align-items: center; }
.inline-num input { width: 80px; }
.grow { flex: 1; }
.hidden { display: none !important; }

/* ---------------- 按钮 ---------------- */
.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
  transition: .15s;
  white-space: nowrap;
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.ghost { background: #fff; border-color: var(--line); color: var(--text); }
.btn.ghost:hover { border-color: var(--primary); color: var(--primary); }
.btn.link { background: transparent; color: var(--primary); text-decoration: underline; padding: 8px 6px; }
.btn.danger { background: #fff; border-color: #fecaca; color: var(--danger); }
.btn.danger:hover { background: #fef2f2; }
.btn.small { padding: 6px 12px; font-size: 12px; }
.btn.tiny { padding: 4px 10px; font-size: 12px; margin-right: 6px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btnrow { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 12px; }

/* ---------------- 预览区 ---------------- */
.preview {
  margin-top: 14px; padding: 14px 16px;
  background: #f8fafc; border: 1px dashed var(--line); border-radius: 10px;
}
.preview .summary { font-size: 13px; margin-bottom: 10px; }
.preview .summary b { color: var(--primary); font-size: 15px; }
.warn { color: #b45309; background: #fffbeb; border: 1px solid #fde68a; padding: 8px 12px; border-radius: 8px; margin-top: 10px; font-size: 13px; }

/* ---------------- 表格 ---------------- */
.tablewrap { overflow: auto; border: 1px solid var(--line); border-radius: 10px; }
.tablewrap.tall { max-height: 460px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  position: sticky; top: 0; background: #f8fafc;
  text-align: left; padding: 10px 12px;
  font-weight: 600; color: #4b5563;
  border-bottom: 1px solid var(--line);
  white-space: nowrap; z-index: 1;
}
tbody td { padding: 9px 12px; border-bottom: 1px solid #f1f3f7; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f8fafc; }
tbody tr.locked { background: #fafbfc; }
tbody tr.locked:hover { background: #f5f6f8; }
.mono { font-family: ui-monospace, Menlo, Consolas, monospace; letter-spacing: .3px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.colck { width: 40px; }
input[type="checkbox"] { width: 16px; height: 16px; cursor: pointer; accent-color: var(--primary); }
input[type="checkbox"]:disabled { cursor: not-allowed; }

/* ---------------- 来源前缀展开 ---------------- */
.pxcell { white-space: nowrap; }
.px-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); background: #fff; color: #4b5563;
  border-radius: 18px; padding: 3px 10px; cursor: pointer;
  font-family: inherit; font-size: 12px;
}
.px-toggle:hover { border-color: var(--primary); color: var(--primary); background: #eff6ff; }
.px-toggle .caret { font-size: 10px; line-height: 1; color: var(--muted); transition: transform .15s; }
.px-toggle.open { border-color: var(--primary); color: var(--primary); background: #eff6ff; }
.px-toggle.open .caret { transform: rotate(180deg); color: var(--primary); }

tr.detail-row > td { background: #f8fafc; padding: 4px 12px 12px; }
tr.detail-row:hover > td { background: #f8fafc; }
.pxdetail { display: grid; gap: 8px; }
.pxgroup { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.pxgroup .chips { gap: 6px; }
.pxname { font-size: 12px; font-weight: 600; color: #4b5563; }
.pxname .muted { font-weight: 400; }

/* ---------------- 状态标签 ---------------- */
.pill {
  display: inline-block; padding: 2px 9px; border-radius: 20px;
  font-size: 12px; line-height: 18px; border: 1px solid transparent;
}
.pill-pending { background: #f1f5f9; color: #64748b; border-color: #e2e8f0; }
.pill-allocated { background: #eff6ff; color: var(--allocated); border-color: #bfdbfe; }
.pill-shipped { background: #fffbeb; color: var(--shipped); border-color: #fde68a; }
.pill-settled { background: #ecfdf5; color: var(--settled); border-color: #a7f3d0; }

/* ---------------- 分配页 ---------------- */
.modebar {
  display: flex; align-items: center; gap: 20px; flex-wrap: wrap;
  padding: 10px 14px; background: #f8fafc;
  border: 1px solid var(--line); border-radius: 10px; margin-bottom: 12px;
}
.radio { display: flex; align-items: center; gap: 7px; margin: 0; font-size: 13px; cursor: pointer; }
.radio input { width: auto; }
.chipbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.chip-label { font-size: 13px; color: var(--muted); }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  border: 1px solid var(--line); background: #fff; border-radius: 20px;
  padding: 4px 12px; font-size: 12px; cursor: pointer; font-family: inherit; color: #4b5563;
}
.chip:hover { border-color: var(--primary); color: var(--primary); background: #eff6ff; }
.chip.source { cursor: default; }
.chip.source:hover { border-color: var(--line); color: #4b5563; background: #fff; }
.search { width: 180px; padding: 6px 10px; font-size: 13px; }

.actionsbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; margin-top: 14px; flex-wrap: wrap;
}
#selStat { font-size: 14px; font-weight: 600; color: var(--primary); }

/* ---------------- 查询结果 ---------------- */
.query-result { margin-top: 14px; }
.qsum {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 10px 14px; margin-bottom: 12px;
  background: #f8fafc; border: 1px solid var(--line); border-radius: 10px;
  font-size: 13px;
}
.qsum b { color: var(--primary); font-size: 15px; }
.qsum .tagitem { cursor: default; }
.qgroup { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin-bottom: 12px; }
.qgroup h3 { margin: 0 0 8px; font-size: 13px; color: #4b5563; font-weight: 600; }
.taglist { display: flex; flex-wrap: wrap; gap: 6px; }
.tagitem {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 12px; padding: 3px 8px; border-radius: 6px;
  background: #f1f5f9; border: 1px solid #e2e8f0;
}
.tagitem.allocated { background: #eff6ff; border-color: #bfdbfe; color: var(--allocated); }
.tagitem.shipped { background: #fffbeb; border-color: #fde68a; color: var(--shipped); }
.tagitem.settled { background: #ecfdf5; border-color: #a7f3d0; color: var(--settled); }

/* ---------------- 登录层 ---------------- */
.login-mask {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background: linear-gradient(160deg, #eef2ff 0%, #f4f6fa 55%);
}
.login-mask[hidden] { display: none; }
.login-box {
  width: 100%; max-width: 350px;
  display: flex; flex-direction: column; gap: 13px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 26px 24px 24px;
  box-shadow: 0 18px 44px rgba(15, 23, 42, .12);
}
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 2px; }
.login-brand h2 { margin: 0; font-size: 16.5px; font-weight: 600; }
.login-brand p { margin: 3px 0 0; font-size: 12px; color: var(--muted); }
.login-box input { font-size: 15px; }
.login-err { margin: 0; min-height: 17px; font-size: 12.5px; color: var(--danger); }
.login-box .btn { width: 100%; padding: 11px; font-size: 15px; }

/* ---------------- 顶栏：当前用户 ---------------- */
.topbar-right { display: flex; align-items: center; gap: 10px; }
.whoami { font-size: 13px; white-space: nowrap; }
.whoami .muted { color: var(--muted); font-size: 12px; margin-right: 4px; }

@media (max-width: 640px) {
  /* 窄屏顶栏只留名字，避免把页签挤歪 */
  .whoami .muted { display: none; }
  .whoami { font-size: 12px; }
  .topbar-right { gap: 8px; }
  .login-box { padding: 22px 18px; }
}

/* ---------------- Toast ---------------- */
.toast {
  position: fixed; left: 50%; bottom: 32px;
  transform: translate(-50%, 20px);
  background: #1f2937; color: #fff;
  padding: 11px 20px; border-radius: 10px; font-size: 13px;
  opacity: 0; pointer-events: none; transition: .22s; z-index: 99;
  max-width: 70vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.warn { background: #b45309; }
.toast.err { background: #b91c1c; }

/* ---------------- 手机适配（≤640px，不影响桌面） ---------------- */
@media (max-width: 640px) {
  /* 顶栏与页签：顶栏变矮，粘性页签的 top 要跟着改，否则会错位露缝 */
  .topbar { padding: 10px 12px; gap: 10px; }
  .logo { width: 32px; height: 32px; flex-basis: 32px; font-size: 16px; border-radius: 9px; }
  .brand-text h1 { font-size: 15px; white-space: nowrap; }
  .brand-text p { display: none; }
  .topbar-right .inline { gap: 6px; font-size: 12px; }
  /* 顶栏输入框保持小尺寸，否则会把顶栏撑高、让页签的粘性定位错位 */
  .topbar-right input { width: 76px; font-size: 13px; padding: 6px 8px; }
  .tabs { top: 53px; padding: 8px 12px 0; }
  .tab { padding: 8px 10px; font-size: 13px; }

  /* 内容区留白 */
  main { padding: 12px 12px 48px; }
  .card { padding: 14px; border-radius: 10px; }
  .card h2 { font-size: 14px; margin-bottom: 10px; }

  /* 表单：字号 16px，避免 iOS 聚焦输入框时整页被放大 */
  input, select, textarea { font-size: 16px; padding: 9px 10px; }
  .search { width: 100%; }
  .inline-narrow { max-width: none; width: 100%; }
  .inline-num input { width: 72px; }
  label { font-size: 12px; }

  /* 按钮与勾选框：加大点按区域 */
  .btn { padding: 10px 14px; }
  .btn.tiny { padding: 8px 12px; font-size: 12px; margin-right: 8px; }
  .btnrow { gap: 8px; }
  .btnrow .btn { flex: 1 1 auto; }
  input[type="checkbox"] { width: 20px; height: 20px; }
  .colck { width: 32px; }

  /* 表格 */
  thead th { padding: 9px 10px; }
  tbody td { padding: 10px; }
  .tablewrap.tall { max-height: 62vh; }

  /* 车次表的「操作」列钉右已移到 ≤768px 的卡片块与 769–1100px 的表格块，这里不再处理 */

  /* 来源前缀展开明细：窄屏改竖排，前缀名独占一行 */
  .pxgroup { flex-direction: column; align-items: flex-start; gap: 6px; }
  .px-toggle { padding: 5px 10px; }
  .pxname { white-space: nowrap; }

  /* 分配区、查询结果、提示 */
  .modebar { gap: 10px; padding: 10px; }
  .chip { padding: 6px 12px; }
  .chipbar { gap: 8px; }
  .actionsbar { gap: 8px; }
  .qgroup { padding: 10px 12px; }
  .toast { max-width: 88vw; bottom: 18px; }
}

/* ---------------- 窄屏：车次列表转卡片（10 列不再横向滚动） ---------------- */
@media (max-width: 768px) {
  #carTable thead { display: none; }
  #carTable, #carTable tbody { display: block; width: 100%; }
  .tablewrap.tall { max-height: none; overflow: visible; }

  /* 每行一个卡片，4 列网格排布 */
  #carTable tr:not(.detail-row) {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px 6px;
    padding: 12px;
    margin-bottom: 10px;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 10px;
  }
  #carTable tr:not(.detail-row):hover { background: #fff; }

  #carTable tbody tr:not(.detail-row) > td { display: block; padding: 0; border: none; }
  #carTable tbody tr:not(.detail-row) > td::before {
    content: attr(data-label);
    display: block;
    font-size: 11px;
    color: var(--muted);
    margin-bottom: 3px;
  }

  /* 首行：车次号（左）+ 车牌（右） */
  #carTable tbody tr:not(.detail-row) > td:nth-child(1) { grid-column: 1 / 3; font-weight: 600; color: #4b5563; }
  #carTable tbody tr:not(.detail-row) > td:nth-child(2) { grid-column: 3 / 5; text-align: right; font-size: 15px; font-weight: 600; }
  /* 次行：证号（左）+ 总头数（右） */
  #carTable tbody tr:not(.detail-row) > td:nth-child(3) { grid-column: 1 / 3; }
  #carTable tbody tr:not(.detail-row) > td:nth-child(5) { grid-column: 3 / 5; text-align: right; }
  /* 来源前缀：整行，放展开按钮 */
  #carTable tbody tr:not(.detail-row) > td:nth-child(4) { grid-column: 1 / 5; }
  #carTable td.pxcell { white-space: normal; }

  /* 四个状态：一行四格，各按状态配色 */
  #carTable tbody tr:not(.detail-row) > td:nth-child(n+6):nth-child(-n+9) {
    grid-column: span 1;
    text-align: center;
    font-weight: 600;
    padding: 6px 2px;
    background: #f8fafc;
    border-radius: 8px;
  }
  #carTable tbody tr:not(.detail-row) > td:nth-child(n+6):nth-child(-n+9)::before { margin-bottom: 1px; }
  #carTable tbody tr:not(.detail-row) > td:nth-child(6) { color: #b45309; }
  #carTable tbody tr:not(.detail-row) > td:nth-child(7) { color: var(--allocated); }
  #carTable tbody tr:not(.detail-row) > td:nth-child(8) { color: var(--shipped); }
  #carTable tbody tr:not(.detail-row) > td:nth-child(9) { color: var(--settled); }

  /* 操作按钮：整行等宽，随手可点（覆盖「钉右列」那套） */
  #carTable tbody tr:not(.detail-row) > td:nth-child(10) {
    grid-column: 1 / 5;
    display: flex;
    gap: 8px;
    position: static;
    background: transparent;
    box-shadow: none;
  }
  #carTable tbody tr:not(.detail-row) > td:nth-child(10)::before { content: none; }
  #carTable tbody tr:not(.detail-row) > td:nth-child(10) .btn { flex: 1 1 0; margin-right: 0; text-align: center; }

  /* 展开明细：与上方卡片连成一体 */
  #carTable tr.detail-row { display: block; margin: -10px 0 10px; }
  #carTable tr.detail-row > td {
    display: block;
    padding: 10px 12px;
    background: #f8fafc;
    border: 1px solid var(--line);
    border-top: none;
    border-radius: 0 0 10px 10px;
  }
  #carTable tr.detail-row.hidden { display: none; }
  #carTable tr:not(.detail-row):has(+ tr.detail-row:not(.hidden)) { margin-bottom: 0; border-radius: 10px 10px 0 0; }
  #carTable tr:not(.detail-row):has(+ tr.detail-row:not(.hidden)) + tr.detail-row { margin-top: 0; }

  /* 空态不要被当成卡片 */
  #carTable tr.empty-row { display: block; padding: 16px 12px; text-align: center; }
  #carTable tbody tr.empty-row > td { display: block; }
  #carTable tbody tr.empty-row > td::before { content: none; }
}

/* ---------------- 表格态窄窗口（769-1100px）：车次表仍横滑，把操作列钉在右侧 ---------------- */
@media (min-width: 769px) and (max-width: 1100px) {
  #carTable thead th:last-child,
  #carTable tbody tr:not(.detail-row) > td:last-child {
    position: sticky;
    right: 0;
    background: #fff;
  }
  #carTable thead th:last-child { background: #f8fafc; z-index: 3; }
  #carTable tbody tr:hover > td:last-child { background: #f8fafc; }
  #carTable tr.detail-row > td:last-child { position: static; }
}
