/* ============================================================
   裝修管家 · RenovationGuardian PWA
   工地色系:磚橘 + 木質 + 警戒紅
   ============================================================ */

:root {
  --clay: #a8623a;
  --clay-deep: #6b3d1f;
  --clay-soft: #f3e3d4;
  --wood: #8b6f47;
  --wood-soft: #ede1cc;
  --paper: #faf6f0;
  --paper-deep: #f0e8d8;
  --ink: #2b1f12;
  --ink-soft: #6b5942;
  --mute: #9a8771;
  --line: #d9c7b1;
  --warn: #c0461e;
  --warn-soft: #fbe5dc;
  --ok: #4a7a3a;
  --ok-soft: #e3efd9;
  --accent: #3d5a72;
  --shadow: 0 2px 8px rgba(45, 25, 10, .08);
  --shadow-lg: 0 6px 18px rgba(45, 25, 10, .14);
  --radius: 10px;
  --radius-sm: 6px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 15px;
  color: var(--ink);
  background: var(--paper);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

#app {
  max-width: 720px;
  margin: 0 auto;
  min-height: 100vh;
  background: var(--paper);
  padding-bottom: 76px;  /* space for tabbar */
  position: relative;
}

/* ============ Header ============ */
.app-header {
  position: sticky; top: 0; z-index: 50;
  background: linear-gradient(180deg, var(--clay-deep), var(--clay));
  color: var(--paper);
  padding: 14px 16px;
  display: flex; align-items: center; justify-content: space-between;
  box-shadow: var(--shadow-lg);
}
.app-header-title { display: flex; align-items: center; gap: 12px; }
.app-logo { font-size: 28px; }
.app-header h1 { font-size: 18px; margin: 0; font-weight: 600; letter-spacing: .5px; }
.subtitle { font-size: 12px; margin: 2px 0 0; opacity: .85; }
.header-stats { display: flex; gap: 8px; }
.badge-stat {
  background: rgba(255,255,255,.18);
  border: 1px solid rgba(255,255,255,.3);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

/* ============ Page Container ============ */
#pages { position: relative; }
.page { display: none; padding: 16px 16px 8px; animation: fadeIn .25s; }
.page.page-active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

.hero {
  text-align: center;
  padding: 24px 12px 18px;
  border-bottom: 1px dashed var(--line);
  margin-bottom: 16px;
}
.hero-icon { font-size: 44px; }
.hero h2 { margin: 6px 0 4px; color: var(--clay-deep); font-size: 22px; }
.hero p { margin: 0; color: var(--ink-soft); font-size: 13px; }

.page-header { padding: 8px 0 12px; border-bottom: 1px solid var(--line); margin-bottom: 12px; }
.page-header h2 { margin: 0 0 4px; color: var(--clay-deep); font-size: 20px; }
.page-header .muted { margin: 0; font-size: 13px; }

.muted { color: var(--ink-soft); font-size: 13px; margin: 6px 0; }

/* ============ Card ============ */
.card {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 12px; font-size: 16px; color: var(--clay-deep); }
.card-add { border-left: 4px solid var(--clay); }
.card-result { border-left: 4px solid var(--ok); background: linear-gradient(180deg, #fff, var(--ok-soft)); }
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.stat-card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 8px; text-align: center; box-shadow: var(--shadow); }
.stat-card b { display: block; color: var(--clay-deep); font-size: 22px; line-height: 1.2; }
.stat-card span { color: var(--mute); font-size: 11px; }
.empty-state { color: var(--mute); text-align: center; padding: 24px 12px; border: 1px dashed var(--line); border-radius: var(--radius); background: rgba(255,255,255,.6); }
.room-chip { display: inline-flex; align-items: center; gap: 5px; padding: 5px 9px; margin: 0 5px 7px 0; color: var(--clay-deep); background: var(--clay-soft); border-radius: 999px; font-size: 13px; }
.room-chip button { border: 0; background: transparent; color: var(--warn); cursor: pointer; font-size: 16px; line-height: 1; }
.defect-card { background: #fff; border: 1px solid var(--line); border-left: 4px solid var(--warn); border-radius: var(--radius); padding: 12px; margin-bottom: 9px; box-shadow: var(--shadow); }
.defect-card.resolved { border-left-color: var(--ok); opacity: .72; }
.check-row { display: flex; align-items: flex-start; gap: 7px; padding: 8px 0; border-top: 1px dotted var(--line); font-weight: 500; color: var(--ink); }
.check-row input { width: auto; margin-top: 5px; accent-color: var(--ok); }
small { color: var(--mute); font-weight: 400; }
@media (max-width: 420px) { .stat-grid { gap: 5px; } .stat-card { padding: 10px 4px; } .stat-card b { font-size: 19px; } }

/* ============ Form ============ */
label {
  display: block;
  font-size: 12px;
  color: var(--ink-soft);
  margin: 10px 0 4px;
  font-weight: 600;
}
input, select, textarea {
  width: 100%;
  font-family: inherit;
  font-size: 15px;
  color: var(--ink);
  background: var(--paper);
  border: 1.5px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 9px 10px;
  outline: none;
  transition: border-color .15s, background .15s;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--clay);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(168, 98, 58, .12);
}
textarea { resize: vertical; font-family: ui-monospace, "SF Mono", monospace; font-size: 13px; }
.row { display: flex; gap: 10px; margin-top: 6px; }
.col { flex: 1; min-width: 0; }

/* ============ Button ============ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font-family: inherit; font-size: 14px; font-weight: 600;
  padding: 10px 16px;
  border: none; border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all .15s;
  margin-top: 10px;
  min-height: 40px;
}
.btn-primary { background: var(--clay); color: #fff; }
.btn-primary:hover, .btn-primary:active { background: var(--clay-deep); }
.btn-secondary { background: var(--wood); color: #fff; }
.btn-secondary:hover { background: var(--clay-deep); }
.btn-ghost { background: transparent; color: var(--ink-soft); border: 1px solid var(--line); }
.btn-ghost:hover { background: var(--paper-deep); }
.btn-danger { background: var(--warn); color: #fff; }
.btn-danger:hover { background: #952f12; }
.btn-sm { padding: 6px 10px; font-size: 12px; min-height: 32px; margin-top: 0; }

/* ============ Filter Tabs ============ */
.filter-tabs {
  display: flex; gap: 6px;
  margin: 6px 0 14px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.filter-tab {
  flex: 0 0 auto;
  background: #fff;
  border: 1.5px solid var(--line);
  color: var(--ink-soft);
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 13px; font-weight: 600;
  cursor: pointer;
  transition: all .15s;
}
.filter-tab.active { background: var(--clay); color: #fff; border-color: var(--clay); }

/* ============ Quote / Item Card ============ */
.quote-card {
  background: #fff;
  border: 1px solid var(--line);
  border-left: 4px solid var(--clay);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.quote-card .qhead {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 8px;
}
.quote-card .qhead h4 { margin: 0; font-size: 15px; color: var(--clay-deep); }
.quote-card .qhead .qmeta { font-size: 12px; color: var(--mute); }
.quote-card .qtotal {
  font-size: 16px; font-weight: 700;
  color: var(--clay-deep); margin: 6px 0;
}
.quote-card .qitems {
  border-top: 1px dashed var(--line); padding-top: 8px; margin-top: 8px;
}
.quote-card .qitem {
  display: flex; justify-content: space-between;
  font-size: 13px;
  padding: 3px 0;
}
.quote-card .qitem .i-name { color: var(--ink); }
.quote-card .qitem .i-meta { color: var(--mute); font-family: ui-monospace, monospace; font-size: 12px; }
.quote-card .qitem .i-total { font-family: ui-monospace, monospace; font-weight: 600; min-width: 90px; text-align: right; }
.quote-card .qactions { display: flex; gap: 6px; margin-top: 8px; }

/* ============ Alignment Table ============ */
.alignment-summary {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px; margin-bottom: 14px;
}
.align-stat {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px;
  text-align: center;
}
.align-stat .num { font-size: 22px; font-weight: 700; color: var(--clay-deep); }
.align-stat .lbl { font-size: 11px; color: var(--mute); margin-top: 2px; }

.align-row {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 8px;
}
.align-row.has-gap { border-left: 4px solid var(--warn); background: linear-gradient(180deg, #fff, var(--warn-soft)); }
.align-row .ar-name { font-weight: 700; color: var(--clay-deep); font-size: 14px; margin-bottom: 6px; }
.align-row .ar-line {
  display: grid; grid-template-columns: 1fr 70px 80px;
  font-size: 12px; padding: 3px 0;
  border-top: 1px dotted var(--line);
  align-items: center;
}
.align-row .ar-line:first-of-type { border-top: 0; }
.align-row .ar-cname { color: var(--ink-soft); font-weight: 600; }
.align-row .ar-cprice { font-family: ui-monospace, monospace; text-align: right; font-weight: 600; }
.align-row .ar-cdiff { font-family: ui-monospace, monospace; text-align: right; font-size: 11px; }
.align-row .ar-cdiff.pos { color: var(--warn); }
.align-row .ar-cdiff.neg { color: var(--ok); }
.align-row .ar-actions { display: flex; gap: 6px; margin-top: 6px; }
.align-tag {
  display: inline-block; padding: 1px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600;
}
.align-tag.missing { background: var(--warn); color: #fff; }
.align-tag.covered { background: var(--ok); color: #fff; }

/* ============ Contract / Payment ============ */
.pay-card {
  background: #fff; border: 1px solid var(--line);
  border-left: 4px solid var(--wood);
  border-radius: var(--radius); padding: 10px 12px;
  margin-bottom: 8px;
  display: flex; justify-content: space-between; align-items: center;
  gap: 8px;
}
.pay-card.paid { border-left-color: var(--ok); }
.pay-card.overdue { border-left-color: var(--warn); }
.pay-card .pinfo { flex: 1; }
.pay-card .pphase { font-weight: 700; color: var(--clay-deep); }
.pay-card .pmeta { font-size: 12px; color: var(--mute); }
.pay-card .pamount { font-family: ui-monospace, monospace; font-weight: 700; font-size: 16px; color: var(--clay-deep); }
.pay-card .pstatus {
  font-size: 11px; padding: 2px 8px; border-radius: 999px;
  background: var(--paper-deep); color: var(--ink-soft); font-weight: 600;
}
.pay-card.paid .pstatus { background: var(--ok-soft); color: var(--ok); }
.pay-card.overdue .pstatus { background: var(--warn-soft); color: var(--warn); }

.change-card {
  background: #fff; border: 1px solid var(--line);
  border-radius: var(--radius); padding: 10px 12px;
  margin-bottom: 8px;
}
.change-card.add { border-left: 4px solid var(--clay); }
.change-card.deduct { border-left: 4px solid var(--accent); }
.change-card .chtop { display: flex; justify-content: space-between; }
.change-card .chitem { font-weight: 700; color: var(--clay-deep); }
.change-card .chamount { font-family: ui-monospace, monospace; font-weight: 700; }
.change-card.add .chamount { color: var(--clay); }
.change-card.deduct .chamount { color: var(--accent); }
.change-card .chmeta { font-size: 12px; color: var(--mute); margin-top: 2px; }
.change-card .chreason { font-size: 13px; margin-top: 4px; }

/* ============ Rooms & Photos ============ */
.room-section { margin-bottom: 18px; }
.room-head {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--clay-soft);
  border-radius: var(--radius);
  padding: 8px 12px;
  margin-bottom: 8px;
}
.room-head .rname { font-weight: 700; color: var(--clay-deep); }
.room-head .rmeta { font-size: 12px; color: var(--ink-soft); }
.room-head .ractions { display: flex; gap: 4px; }

.photo-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 8px;
}
.photo-card {
  background: #fff; border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
}
.photo-card .pimg {
  width: 100%; aspect-ratio: 1; object-fit: cover; background: var(--paper-deep);
}
.photo-card .pplaceholder {
  width: 100%; aspect-ratio: 1;
  background: var(--paper-deep);
  display: flex; align-items: center; justify-content: center;
  font-size: 32px; color: var(--mute);
}
.photo-card .pinfo {
  padding: 5px 8px;
  font-size: 11px;
  color: var(--ink-soft);
}
.photo-card .pinfo .pdate { font-weight: 700; color: var(--clay-deep); }
.photo-card .pactions {
  display: flex; gap: 4px; padding: 4px 8px 6px;
}
.photo-card .pactions .btn-sm { flex: 1; }

.photo-uploader {
  background: var(--paper-deep);
  border: 2px dashed var(--line);
  border-radius: var(--radius);
  padding: 12px;
  text-align: center;
  margin: 8px 0;
}
.photo-uploader input[type=file] { display: none; }
.photo-uploader label {
  display: inline-block; padding: 8px 16px;
  background: var(--clay); color: #fff;
  border-radius: var(--radius-sm); cursor: pointer;
  font-size: 13px; font-weight: 600;
}
.photo-uploader label:hover { background: var(--clay-deep); }
.photo-uploader .phint { font-size: 11px; color: var(--mute); margin-top: 4px; }

/* ============ Defects ============ */
.defect-card {
  background: #fff; border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 10px;
  border-left: 5px solid var(--warn);
}
.defect-card.minor { border-left-color: #d49a3a; }
.defect-card.major { border-left-color: var(--warn); }
.defect-card.critical { border-left-color: #8b1f0a; background: linear-gradient(180deg, #fff, var(--warn-soft)); }
.defect-card.resolved { opacity: .65; border-left-color: var(--ok); }
.defect-card .dtop { display: flex; justify-content: space-between; align-items: flex-start; }
.defect-card .dcat {
  font-size: 11px; font-weight: 700;
  padding: 2px 8px; border-radius: 999px;
  background: var(--paper-deep); color: var(--ink-soft);
}
.defect-card.critical .dcat { background: var(--warn); color: #fff; }
.defect-card .droom { font-size: 13px; color: var(--clay-deep); font-weight: 700; margin-top: 4px; }
.defect-card .ddesc { margin: 6px 0; font-size: 14px; }
.defect-card .ddue { font-size: 12px; color: var(--mute); margin-bottom: 6px; }
.defect-card .ddue.overdue { color: var(--warn); font-weight: 700; }
.defect-card .dmark {
  display: inline-block; width: 22px; height: 22px;
  background: rgba(192, 70, 30, .7);
  border: 2px solid var(--warn);
  border-radius: 50%;
  color: #fff; font-size: 12px;
  text-align: center; line-height: 18px;
  margin-right: 4px;
}
.defect-card .dactions { display: flex; gap: 6px; margin-top: 8px; }

/* ============ Acceptance ============ */
.acc-card {
  background: #fff; border: 1px solid var(--line);
  border-radius: var(--radius); padding: 12px 14px;
  margin-bottom: 12px;
}
.acc-card.passed { border-left: 5px solid var(--ok); }
.acc-card.failed { border-left: 5px solid var(--warn); }
.acc-card .ahead { display: flex; justify-content: space-between; }
.acc-card .aphase { font-weight: 700; color: var(--clay-deep); font-size: 15px; }
.acc-card .adate { font-size: 12px; color: var(--mute); }
.acc-card .aprogress {
  height: 6px; background: var(--paper-deep); border-radius: 3px; overflow: hidden;
  margin: 8px 0;
}
.acc-card .aprogress .abar {
  height: 100%; background: var(--clay); transition: width .3s;
}
.acc-card.passed .abar { background: var(--ok); }
.acc-card.failed .abar { background: var(--warn); }
.acc-card .aitem {
  display: flex; align-items: flex-start; gap: 8px;
  padding: 6px 0;
  border-top: 1px dotted var(--line);
  font-size: 13px;
}
.acc-card .aitem .acheck {
  flex: 0 0 22px; width: 22px; height: 22px;
  border: 2px solid var(--line); border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; cursor: pointer; user-select: none;
}
.acc-card .aitem.checked .acheck { background: var(--ok); border-color: var(--ok); color: #fff; }
.acc-card .aitem.failed .acheck { background: var(--warn); border-color: var(--warn); color: #fff; }
.acc-card .aitem .atext { flex: 1; }
.acc-card .aitem.failed .atext { text-decoration: line-through; color: var(--mute); }
.acc-card .aactions { display: flex; gap: 6px; margin-top: 8px; }
.acc-sign {
  background: var(--clay-soft); border-radius: var(--radius);
  padding: 8px 12px; margin-top: 8px;
  font-size: 12px; color: var(--clay-deep);
}

/* ============ Overview Cards ============ */
.overview-cards {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}
.overview-card {
  background: linear-gradient(180deg, #fff, var(--clay-soft));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
}
.overview-card .otitle { font-size: 13px; color: var(--clay-deep); font-weight: 700; }
.overview-card .oval { font-size: 22px; font-weight: 700; color: var(--clay-deep); margin-top: 4px; }
.overview-card .olbl { font-size: 11px; color: var(--mute); }
.overview-card.active { background: linear-gradient(180deg, var(--clay-soft), var(--wood-soft)); border-color: var(--clay); }

/* ============ Tab Bar ============ */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 50;
  background: rgba(255,255,255,.96);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
  display: flex; justify-content: space-around;
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  max-width: 720px; margin: 0 auto;
}
.tab {
  flex: 1;
  background: transparent; border: none;
  display: flex; flex-direction: column; align-items: center;
  gap: 2px;
  padding: 4px 2px;
  font-family: inherit;
  color: var(--mute);
  font-size: 11px; font-weight: 600;
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: color .15s;
}
.tab.tab-active { color: var(--clay); }
.tab-icon { font-size: 20px; line-height: 1; }

/* ============ Modal ============ */
.modal { position: fixed; inset: 0; z-index: 100; display: flex; align-items: center; justify-content: center; }
.modal-overlay { position: absolute; inset: 0; background: rgba(0,0,0,.5); }
.modal-content {
  position: relative;
  background: var(--paper);
  border-radius: var(--radius);
  max-width: 90vw; max-height: 86vh; overflow: auto;
  padding: 16px;
  box-shadow: var(--shadow-lg);
}

/* ============ Toast ============ */
.toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff;
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  z-index: 200;
  box-shadow: var(--shadow-lg);
  animation: toastIn .25s;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 12px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ============ Image Modal / Annotation ============ */
.img-modal {
  position: fixed; inset: 0; z-index: 110;
  background: rgba(0,0,0,.92);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 16px;
}
.img-modal img {
  max-width: 100%; max-height: 80vh;
  border-radius: var(--radius-sm);
}
.img-modal .im-controls {
  margin-top: 12px; display: flex; gap: 8px;
}
.img-modal .im-controls .btn { margin: 0; }
.img-modal .im-hint {
  color: #fff; font-size: 13px; margin-bottom: 8px; opacity: .85;
}
.annot-canvas {
  position: relative;
  display: inline-block;
  max-width: 100%;
}
.annot-canvas img {
  max-width: 100%; max-height: 70vh;
  display: block;
  border-radius: var(--radius-sm);
}
.annot-canvas .mark {
  position: absolute;
  width: 28px; height: 28px;
  border: 3px solid var(--warn);
  border-radius: 50%;
  background: rgba(192, 70, 30, .35);
  transform: translate(-50%, -50%);
  cursor: pointer;
  color: #fff; font-weight: 700; font-size: 12px;
  display: flex; align-items: center; justify-content: center;
  pointer-events: auto;
}
.annot-canvas .mark:hover { background: rgba(192, 70, 30, .6); }
.annot-canvas .mark.major { border-color: #d49a3a; background: rgba(212, 154, 58, .35); }
.annot-canvas .mark.critical { border-color: #8b1f0a; background: rgba(139, 31, 10, .55); }

/* ============ Misc ============ */
.empty-state {
  text-align: center; padding: 32px 16px;
  color: var(--mute);
}
.empty-state .es-icon { font-size: 40px; opacity: .5; }
.empty-state p { margin: 8px 0 0; font-size: 13px; }

.kbd {
  display: inline-block;
  padding: 1px 6px;
  background: var(--paper-deep);
  border: 1px solid var(--line);
  border-radius: 4px;
  font-family: ui-monospace, monospace;
  font-size: 11px;
}

@media print {
  .tabbar, .app-header, .btn { display: none !important; }
  .page { display: block !important; }
  #pages { max-width: 100%; }
}
