:root {
  --bg: #f3f5f9;
  --card: #ffffff;
  --line: #e5e9f0;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-soft: #e8efff;
  --green: #16a34a;
  --green-soft: #e7f7ec;
  --orange: #d97706;
  --orange-soft: #fdf1dc;
  --red: #dc2626;
  --red-soft: #fdeaea;
  --violet: #7c3aed;
  --violet-soft: #efe9fd;
  --cyan: #0891b2;
  --cyan-soft: #e0f7fa;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

body { padding-bottom: calc(64px + var(--safe-bottom)); }

.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: linear-gradient(135deg, #2563eb, #3b82f6);
  color: #fff;
  padding: 16px 18px 14px;
  padding-top: calc(16px + env(safe-area-inset-top, 0px));
  box-shadow: var(--shadow);
}
.app-header-title { font-size: 18px; font-weight: 700; letter-spacing: 0.5px; }
.app-header-sub { font-size: 12px; opacity: 0.85; margin-top: 2px; }

.app-main { max-width: 560px; margin: 0 auto; padding: 14px 14px 20px; }

.tab-page { display: none; }
.tab-page.active { display: block; }

/* month nav */
.month-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.month-nav-center { display: flex; align-items: center; gap: 8px; }
.month-title { font-size: 17px; font-weight: 700; }
.icon-btn {
  width: 38px; height: 38px;
  border: none; border-radius: 10px;
  background: var(--card);
  color: var(--primary);
  font-size: 24px; line-height: 1;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.text-btn {
  border: 1px solid var(--primary);
  background: var(--primary-soft);
  color: var(--primary);
  border-radius: 8px;
  padding: 3px 10px;
  font-size: 12px;
  cursor: pointer;
}

/* summary cards */
.summary-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
  margin-bottom: 12px;
}
.sum-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 12px;
}
.sum-card .v { font-size: 20px; font-weight: 800; }
.sum-card .k { font-size: 11px; color: var(--muted); }
.sum-card.c-blue .v { color: var(--primary); }
.sum-card.c-green .v { color: var(--green); }
.sum-card.c-orange .v { color: var(--orange); }
.sum-card.c-red .v { color: var(--red); }
.sum-card.c-violet .v { color: var(--violet); }
.sum-card.c-cyan .v { color: var(--cyan); }

/* calendar */
.weekday-row {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}
.calendar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}
.day-cell {
  position: relative;
  aspect-ratio: 1 / 1;
  border-radius: 11px;
  background: var(--card);
  box-shadow: var(--shadow);
  border: 1px solid transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  min-height: 52px;
  overflow: hidden;
}
.day-cell .num { font-size: 15px; font-weight: 600; }
.day-cell .tag {
  font-size: 10px;
  margin-top: 1px;
  padding: 0 5px;
  border-radius: 6px;
  line-height: 1.5;
  white-space: nowrap;
}
.day-cell .ot { font-size: 9px; color: var(--orange); font-weight: 700; }
.day-cell.empty { background: transparent; box-shadow: none; cursor: default; }
.day-cell.today { border-color: var(--primary); }
.day-cell.selected { border-color: var(--primary); background: var(--primary-soft); }
.day-cell.selected::after { content: '✓'; position: absolute; top: 2px; right: 5px; color: var(--primary); font-weight: 800; font-size: 12px; }
.day-cell.st-company .tag { background: var(--primary-soft); color: var(--primary); }
.day-cell.st-trip .tag { background: var(--orange-soft); color: var(--orange); }
.day-cell.st-overtime .tag { background: var(--red-soft); color: var(--red); }
.day-cell.st-travel .tag { background: var(--cyan-soft); color: var(--cyan); }
.day-cell.st-rest .tag { background: var(--green-soft); color: var(--green); }
.day-cell.st-comp .tag { background: var(--violet-soft); color: var(--violet); }
.day-cell.weekend .num { color: var(--orange); }

.legend { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; font-size: 11px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 4px; }
.legend .dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

.sel-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 7px; margin: 8px 0 4px; min-height: 34px; }
.sel-toolbar .sel-count { font-size: 13px; color: var(--muted); font-weight: 600; }

/* buttons */
.btn {
  border: none;
  border-radius: 11px;
  padding: 11px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity .12s;
}
.btn:active { opacity: .7; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-secondary { background: #fff; color: var(--primary); box-shadow: var(--shadow); }
.btn-danger { background: var(--red-soft); color: var(--red); }
.btn-sm { padding: 7px 13px; font-size: 13px; }
.btn-row { display: flex; gap: 10px; margin-top: 10px; }
.btn-row .btn { flex: 1; }

.hint { font-size: 12px; color: var(--muted); margin: 8px 2px 0; }

/* section head */
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.section-head h2 { margin: 0; font-size: 18px; }

/* cards */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  margin-bottom: 12px;
}
.card h3 { margin: 0 0 10px; font-size: 15px; }
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.card-head h3 { margin: 0; }

/* list */
.list { display: flex; flex-direction: column; gap: 8px; }
.list-item {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 11px 12px;
}
.list-item .li-main { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.list-item .li-title { font-weight: 600; }
.list-item .li-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.list-item .li-amount { font-weight: 700; white-space: nowrap; }
.list-item .li-amount.pos { color: var(--green); }
.list-item .li-amount.neg { color: var(--red); }
.li-actions { display: flex; gap: 6px; margin-top: 8px; }
.li-actions .mini {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 7px;
  font-size: 12px;
  padding: 3px 9px;
  cursor: pointer;
}
.badge {
  font-size: 11px;
  padding: 1px 7px;
  border-radius: 20px;
  white-space: nowrap;
}
.badge.paid { background: var(--green-soft); color: var(--green); }
.badge.unpaid { background: var(--red-soft); color: var(--red); }

.filter-row { margin-bottom: 10px; }
.filter-row input {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 14px;
  background: #fff;
}

.empty {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  padding: 22px 8px;
}

/* field */
.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 11px;
}
.field span { font-size: 12px; color: var(--muted); }
.field input, .field select, .field textarea {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 9px 11px;
  font-size: 14px;
  background: #fff;
  font-family: inherit;
}
.field textarea { resize: vertical; min-height: 56px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field-row-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }

/* segmented */
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 6px; }
.seg button {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 9px;
  padding: 9px 4px;
  font-size: 13px;
  cursor: pointer;
  color: var(--text);
}
.seg button.on { background: var(--primary); color: #fff; border-color: var(--primary); font-weight: 600; }

/* modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.modal {
  background: #fff;
  width: 100%;
  max-width: 560px;
  max-height: 88vh;
  overflow-y: auto;
  border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(20px + var(--safe-bottom));
  animation: slideUp .18s ease;
}
@keyframes slideUp { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
.modal h3 { margin: 0 0 14px; font-size: 17px; }
.modal-actions { display: flex; gap: 10px; margin-top: 16px; }
.modal-actions .btn { flex: 1; }
.modal-close {
  position: absolute;
  top: 12px; right: 14px;
  border: none; background: #f1f3f7; border-radius: 50%;
  width: 30px; height: 30px; font-size: 18px; cursor: pointer;
}

/* attachments */
.attach-thumbs { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.attach-thumb { position: relative; width: 64px; height: 64px; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); cursor: pointer; }
.attach-thumb img { width: 100%; height: 100%; object-fit: cover; }
.attach-thumb .file-ico { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; font-size: 24px; background: var(--primary-soft); }
.attach-thumb .del {
  position: absolute; top: 0; right: 0;
  background: rgba(0,0,0,.55); color: #fff;
  border: none; width: 18px; height: 18px; font-size: 12px; line-height: 1;
  cursor: pointer;
}

/* tabbar */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  display: flex;
  background: #fff;
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
  max-width: 560px;
  margin: 0 auto;
}
.tab {
  flex: 1;
  border: none;
  background: none;
  padding: 8px 0 7px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  font-size: 11px;
  color: var(--muted);
  cursor: pointer;
}
.tab .tab-ico { font-size: 21px; line-height: 1.2; }
.tab.active { color: var(--primary); font-weight: 600; }

/* toast */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(80px + var(--safe-bottom));
  transform: translateX(-50%) translateY(20px);
  background: rgba(17, 24, 39, 0.92);
  color: #fff;
  padding: 9px 16px;
  border-radius: 22px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: all .25s;
  z-index: 100;
  max-width: 86vw;
  text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

code { background: #f1f3f7; padding: 1px 5px; border-radius: 4px; font-size: 12px; }
.about ol { margin: 0; padding-left: 18px; font-size: 13px; color: var(--muted); }
.about li { margin-bottom: 6px; }

@media (min-width: 640px) {
  .app-main { padding: 18px 20px 24px; }
  .day-cell { min-height: 62px; }
}

/* cost center editor */
.cc-list { display: flex; flex-direction: column; gap: 6px; }
.cc-row { display: flex; gap: 8px; align-items: center; }
.cc-row input {
  flex: 1;
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 10px;
  font-size: 14px;
  min-width: 0;
}
.cc-row .mini {
  flex-shrink: 0;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 7px;
  font-size: 12px;
  padding: 6px 10px;
  cursor: pointer;
  color: var(--red);
}

/* lightbox for attachments */
.lightbox-backdrop {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 18px;
}
.lightbox-img { max-width: 100%; max-height: 78vh; object-fit: contain; border-radius: 8px; }
.lightbox-frame { width: 100%; height: 78vh; border: none; border-radius: 8px; background: #fff; }
.lightbox-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  color: #fff;
  width: 100%;
  max-width: 520px;
}
.lightbox-name { flex: 1; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.sparkline { display: block; width: 100%; height: 92px; margin: 6px 0 4px; }
