:root {
  --bg: #f2f4f7;
  --card: #ffffff;
  --text: #1a1d21;
  --muted: #6b7280;
  --line: #e2e5ea;
  --accent: #1f6feb;
  --accent-press: #1a5cc4;
  --warn: #c0392b;
  --radius: 14px;
}

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

/* display 를 지정한 요소에도 hidden 속성이 통하도록 */
[hidden] { display: none !important; }

body {
  margin: 0;
  padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo",
               "Malgun Gothic", "Noto Sans KR", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  max-width: 560px;
  margin-inline: auto;
}

.head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin: 4px 0 16px;
}

h1 {
  font-size: 20px;
  margin: 0;
}

/* 자동 저장 상태 표시 */
.autosave {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  opacity: 0;
  transition: opacity .2s;
}

.autosave.show { opacity: 1; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 18px 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,.06);
}

/* --- 상단 탭 --- */
.tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 14px;
  padding: 4px;
  border-radius: 12px;
  background: #e7eaef;
}

.tab {
  flex: 1;
  width: auto;
  padding: 10px 8px;
  border-radius: 9px;
  background: none;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.tab:active { background: rgba(255,255,255,.5); }

.tab.is-active {
  background: #fff;
  color: var(--text);
  box-shadow: 0 1px 2px rgba(0,0,0,.08);
}

.field { margin-bottom: 18px; }
.field:last-of-type { margin-bottom: 22px; }

/* --- 접을 수 있는 업체 설정 --- */
.settings {
  margin-bottom: 14px;
  padding: 0;
}

.settings summary {
  padding: 16px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  list-style: none;
}

.settings summary::-webkit-details-marker { display: none; }

.settings summary::after {
  content: '▾';
  float: right;
  color: var(--muted);
  font-weight: 400;
}

.settings[open] summary::after { content: '▴'; }
.settings[open] summary { border-bottom: 1px solid var(--line); }

.settings-body { padding: 16px 16px 4px; }

.settings-note {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--muted);
}

.optional {
  font-weight: 400;
  color: var(--muted);
}

/* --- 비용 명세 입력 --- */
.group-title {
  margin: 24px 0 14px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  font-size: 14px;
  font-weight: 700;
}

.total-line {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin: -4px 0 18px;
  padding: 12px 14px;
  border-radius: 10px;
  background: #f2f6fd;
  font-size: 14px;
  font-weight: 600;
}

.total-line strong {
  font-size: 18px;
  color: var(--accent);
}

label {
  display: block;
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 7px;
}

.count {
  float: right;
  font-weight: 400;
  color: var(--muted);
}

input[type="text"], input[type="password"], input[type="number"], input[type="date"], input[type="search"],
select, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font: inherit;
  font-size: 16px; /* 16px 미만이면 iOS에서 화면이 확대됨 */
  background: #fff;
  color: inherit;
}

input[type="text"]:focus, input[type="password"]:focus, input[type="number"]:focus, input[type="date"]:focus,
input[type="search"]:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(31,111,235,.15);
}

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

/* 기본 파일 입력은 숨기고 라벨을 버튼처럼 사용 */
input[type="file"] {
  position: absolute;
  width: 1px; height: 1px;
  opacity: 0;
  overflow: hidden;
}

.picker {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  background: #fafbfc;
  cursor: pointer;
  font-size: 15px;
  color: var(--muted);
}

.picker:active { background: #f0f2f5; }
input[type="file"]:focus-visible + .picker { border-color: var(--accent); }

/* 5장을 모두 채우면 더 이상 고를 수 없음을 시각적으로 표시 */
.picker.full {
  cursor: not-allowed;
  opacity: .55;
}

.picker .icon { font-size: 20px; line-height: 1; }

.note {
  margin: 8px 0 0;
  font-size: 13px;
  color: var(--warn);
}

.note:empty { display: none; }

/* --- 폼 안의 작은 미리보기 --- */
.thumbs {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-top: 10px;
}

.thumbs:empty { display: none; }

.thumb { position: relative; }

.thumb img {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #f0f2f5;
}

.thumb .remove {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: 50%;
  background: rgba(0,0,0,.62);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.thumb .remove:active { background: rgba(0,0,0,.85); }

/* 사진마다 딸린 설명 입력칸 */
.thumb .desc {
  margin-top: 6px;
  padding: 9px 10px;
  font-size: 16px; /* iOS 자동 확대 방지 */
  border-radius: 8px;
}

button {
  width: 100%;
  padding: 15px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
}

button:active { background: var(--accent-press); }

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

.secondary:active { background: #eef5ff; }

.report-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 18px;
}

.report-actions .wide { grid-column: 1 / -1; }

.hint {
  margin-top: 10px;
  font-size: 13px;
  color: var(--warn);
  min-height: 18px;
}

/* --- 보고서 --- */
#report { display: none; margin-top: 20px; }
#report.show { display: block; }

/* 진행 단계 표시 */
.steps {
  display: flex;
  gap: 6px;
  margin: 0 0 16px;
  padding: 0;
  list-style: none;
}

.step {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 2px;
  border-radius: 8px;
  background: #f2f4f7;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.step .num {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #cfd4da;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
}

.step.done { color: var(--text); }
.step.done .num { background: #6f9a6f; }

.step.current {
  background: #e8f0fe;
  color: var(--accent);
  font-weight: 700;
}

.step.current .num { background: var(--accent); }

.report-no {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--muted);
  margin-bottom: 4px;
}

.report-no:empty { display: none; }

#report h2 {
  font-size: 18px;
  margin: 0 0 4px;
}

.date {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 16px;
}

.row {
  border-top: 1px solid var(--line);
  padding-top: 14px;
  margin-top: 14px;
}

.row-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  margin-bottom: 6px;
}

.row-value { white-space: pre-wrap; word-break: break-word; }

/* --- 보고서 상단 업체 정보 --- */
.company {
  padding-bottom: 14px;
  margin-bottom: 14px;
  border-bottom: 2px solid var(--text);
}

.company-name {
  font-size: 16px;
  font-weight: 700;
}

.company-sub {
  font-size: 13px;
  color: var(--muted);
  margin-top: 2px;
}

/* --- 보고서 비용 명세 --- */
.costs { font-size: 15px; }

.cost-line {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 3px 0;
}

.cost-line .name { color: var(--muted); }

.cost-line.total {
  margin-top: 8px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  font-weight: 700;
}

.cost-line.total .name { color: inherit; }
.cost-line.total .value { font-size: 18px; color: var(--accent); }

.pay {
  margin-top: 10px;
  font-size: 14px;
}

.pay .badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  background: #eef1f5;
  font-size: 13px;
  font-weight: 700;
}

.pay .badge.paid { background: #e6f4e6; color: #2f6d2f; }
.pay .badge.unpaid { background: #fdecea; color: var(--warn); }
.pay .method { color: var(--muted); margin-left: 6px; }

.photos {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.photos figure { margin: 0; }

.photos figcaption {
  font-size: 13px;
  margin-top: 6px;
  word-break: break-word;
}

/* 설명을 적지 않은 사진은 '작업 전 / 작업 후'만 흐리게 */
.photos figcaption.plain {
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

.photos img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #f0f2f5;
}

.photos .empty {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  border-radius: 10px;
  border: 1px dashed var(--line);
  color: var(--muted);
  font-size: 13px;
  background: #fafbfc;
}

/* --- 고객 확인 --- */
.check {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  font-size: 15px;
  font-weight: 400;
  cursor: pointer;
}

.check input {
  flex: none;
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--accent);
}

.check input:disabled { cursor: default; }

.sub-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  margin-bottom: 7px;
}

.methods {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 18px;
}

.methods .check { margin-bottom: 6px; }

.method-guide {
  margin: 0 0 14px;
  font-size: 13px;
  line-height: 1.45;
  color: var(--muted);
}

/* --- 손가락 서명 --- */
.link-btn {
  float: right;
  width: auto;
  padding: 0;
  background: none;
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
}

.link-btn:active { background: none; opacity: .6; }

.sign-wrap {
  position: relative;
  margin-bottom: 14px;
}

#signaturePad {
  display: block;
  width: 100%;
  height: 160px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  background: #fff;
  touch-action: none; /* 서명 중 화면이 스크롤되지 않도록 */
  cursor: crosshair;
}

.sign-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 14px;
  pointer-events: none;
}

.confirm-done .sign {
  display: block;
  width: 100%;
  max-width: 260px;
  margin-top: 8px;
  border: 1px solid #cfe3cf;
  border-radius: 8px;
  background: #fff;
}

#confirmName { margin: 4px 0 12px; }

.confirm-done {
  padding: 12px 14px;
  border: 1px solid #cfe3cf;
  border-radius: 10px;
  background: #f1f8f1;
}

.confirm-done .who { font-weight: 700; }

.confirm-done .when,
.confirm-done .items {
  font-size: 13px;
  color: var(--muted);
  margin-top: 3px;
}

/* --- 공유 --- */
.share-hint {
  grid-column: 1 / -1;
  margin: 2px 0 0;
  font-size: 13px;
  color: var(--muted);
}

.share-hint:empty { display: none; }
.share-hint.warn { color: var(--warn); }

#shareFallback {
  grid-column: 1 / -1;
  min-height: 170px;
  background: #fafbfc;
}

/* --- 작업 템플릿 --- */
.template-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
}

.template-row button {
  width: auto;
  padding: 12px 14px;
  font-size: 14px;
  white-space: nowrap;
}

.template-note {
  margin: 8px 0 0;
  font-size: 13px;
  line-height: 1.45;
  color: var(--muted);
}

.template-note:empty { display: none; }

.template-panel {
  margin-top: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fafbfc;
}

.template-panel label {
  margin-top: 12px;
  font-size: 13px;
}

.template-panel textarea { min-height: 70px; }

.template-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 12px;
}

.template-actions button {
  padding: 12px;
  font-size: 15px;
}

.template-item {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}

.template-item:last-child { border-bottom: 0; }

.template-item .name {
  font-size: 14px;
  font-weight: 600;
}

.template-item .meta {
  font-size: 12px;
  color: var(--muted);
}

.template-item button {
  width: auto;
  padding: 8px 12px;
  font-size: 13px;
  border-radius: 8px;
}

.template-empty {
  padding: 14px 0 4px;
  font-size: 13px;
  color: var(--muted);
}

/* --- 데이터 백업 --- */
.backup {
  margin-bottom: 20px;
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}

.backup-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.backup-actions .wide { grid-column: 1 / -1; }

/* 숨긴 파일 입력을 대신 여는 라벨 (버튼과 같은 모양) */
.btn-like {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 15px;
  border: 1px solid var(--accent);
  border-radius: 10px;
  background: #fff;
  color: var(--accent);
  font-size: 17px;
  font-weight: 700;
  text-align: center;
  cursor: pointer;
}

.btn-like:active { background: #eef5ff; }
input[type="file"]:focus-visible + .btn-like { box-shadow: 0 0 0 3px rgba(31,111,235,.25); }

.restore-preview {
  margin-top: 14px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fafbfc;
}

.restore-preview .backup-actions { margin-top: 12px; }

/* --- 작업 이력 --- */
.filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.filters .field { margin-bottom: 18px; }

.history-count {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.history-empty {
  margin: 14px 0 0;
  padding: 28px 16px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  text-align: center;
  font-size: 14px;
  color: var(--muted);
  background: #fafbfc;
}

.history-item {
  margin-top: 10px;
  padding: 0;
}

.history-item summary {
  padding: 14px 16px;
  cursor: pointer;
  list-style: none;
}

.history-item summary::-webkit-details-marker { display: none; }

.history-no {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--muted);
}

.history-main {
  margin-top: 3px;
  font-size: 15px;
  font-weight: 700;
}

.history-sub {
  margin-top: 2px;
  font-size: 13px;
  color: var(--muted);
}

.history-amount {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 15px;
  font-weight: 700;
}

.history-detail {
  padding: 0 16px 16px;
  border-top: 1px solid var(--line);
}

.history-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 0;
  border-bottom: 1px solid #f0f2f5;
  font-size: 14px;
}

.history-row:last-of-type { border-bottom: 0; }

.history-row .k {
  flex: none;
  color: var(--muted);
}

.history-row .v {
  text-align: right;
  white-space: pre-wrap;
  word-break: break-word;
}

.history-actions {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  margin-top: 14px;
}

.danger {
  width: auto;
  padding: 15px 18px;
  background: #fff;
  color: var(--warn);
  border: 1px solid #f0c8c3;
}

.danger:active { background: #fdecea; }

@media print {
  body {
    max-width: none;
    padding: 0;
    background: #fff;
  }

  .tabs, #historyView,
  .head, #form, .report-actions, .steps, #companySettings,
  #confirmSubmit, #confirmHint, #methodGuide { display: none !important; }

  /* 이력 탭을 보고 있어도 인쇄물에는 현재 보고서가 나오도록 */
  #writeView { display: block !important; }
  #report {
    display: block !important;
    margin: 0;
    box-shadow: none;
  }

  .photos figure { break-inside: avoid; }
}

/* --- 로그인 게이트 (auth-client.js) --- */
/* 로그인 확인 전(auth-pending)·미로그인(auth-locked)에는 로그인 화면만 보인다.
   JS 가 아예 실행되지 않아도 기본 상태가 auth-pending 이라 앱 내용이 노출되지 않는다. */
body.auth-pending > :not(#loginView),
body.auth-locked > :not(#loginView) { display: none !important; }
body.auth-ready #loginView { display: none; }

.login-card {
  max-width: 400px;
  margin: 15vh auto 0;
}

.login-title {
  font-size: 20px;
  margin: 0 0 6px;
}

.login-status {
  color: var(--muted, #666);
  font-size: 14px;
  margin: 0 0 14px;
}

#loginView .field { margin-bottom: 12px; }

#logoutBtn { margin-left: auto; }
