/* BPPM Mini App — uses Telegram theme variables so it matches the user's
   Telegram theme (light or dark) automatically. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f0f2f5);
  --text: var(--tg-theme-text-color, #111827);
  --hint: var(--tg-theme-hint-color, #6b7280);
  --accent: var(--tg-theme-button-color, #2AABEE);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --ok: #2E9E6B;
  --ok-bg: rgba(46, 158, 107, 0.14);
  --issue: #E2543A;
  --issue-bg: rgba(226, 84, 58, 0.13);
  --na: var(--hint);
  --line: rgba(128, 128, 128, 0.22);
  --radius: 14px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg2);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  padding-bottom: calc(90px + env(safe-area-inset-bottom));
}
#app { max-width: 560px; margin: 0 auto; padding: 12px 12px 0; }
button { font: inherit; border: none; cursor: pointer; }
input, textarea { font: inherit; color: var(--text); }

.center-note { text-align: center; color: var(--hint); padding: 48px 24px; }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; }

/* ---------- header / cards ---------- */
.topbar { display: flex; align-items: center; gap: 10px; padding: 6px 2px 12px; }
.topbar h1 { font-size: 20px; font-weight: 700; margin: 0; flex: 1; }
.topbar .sub { font-size: 13px; color: var(--hint); }
.back-btn {
  background: var(--bg); color: var(--accent);
  border-radius: 10px; padding: 8px 12px; font-weight: 600; font-size: 15px;
}

.card {
  background: var(--bg);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
}

.progress-wrap { margin: 4px 0 12px; }
.progress-label {
  display: flex; justify-content: space-between;
  font-size: 13px; color: var(--hint); margin-bottom: 5px;
}
.progress-label b { color: var(--text); font-variant-numeric: tabular-nums; }
.progress-track {
  height: 8px; border-radius: 5px; background: var(--line); overflow: hidden;
}
.progress-fill {
  height: 100%; border-radius: 5px; background: var(--accent);
  transition: width 0.25s ease;
}
.progress-fill.full { background: var(--ok); }

/* ---------- home menu ---------- */
.menu-card {
  display: flex; align-items: center; gap: 14px;
  width: 100%; text-align: left;
  background: var(--bg);
  border-radius: var(--radius);
  padding: 18px 16px;
  margin-bottom: 10px;
  color: var(--text);
}
.menu-card:active { opacity: 0.7; }
.menu-card.disabled { opacity: 0.55; pointer-events: none; }
.menu-emoji { font-size: 30px; }
.menu-body { flex: 1; display: flex; flex-direction: column; gap: 2px; }
.menu-body b { font-size: 17px; }
.menu-body span { font-size: 13px; color: var(--hint); }
.menu-arrow { font-size: 26px; color: var(--hint); }

/* ---------- home: search + house list ---------- */
.search {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 16px;
  margin-bottom: 10px;
  outline: none;
}
.search:focus { border-color: var(--accent); }

.house-row {
  display: flex; align-items: center; gap: 12px;
  width: 100%; text-align: left;
  background: var(--bg);
  border-radius: var(--radius);
  padding: 13px 14px;
  margin-bottom: 8px;
  color: var(--text);
}
.house-row:active { opacity: 0.7; }
.house-code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-weight: 700; font-size: 14px;
  color: var(--accent);
  min-width: 56px;
}
.house-name { flex: 1; font-size: 15px; }
.chip {
  font-size: 12px; font-weight: 600;
  padding: 4px 10px; border-radius: 99px; white-space: nowrap;
}
.chip.done { background: var(--ok-bg); color: var(--ok); }
.chip.due  { background: var(--line); color: var(--hint); }

/* ---------- checklist ---------- */
.cat-head {
  display: flex; align-items: center; gap: 8px; margin-bottom: 4px;
}
.cat-head h2 { font-size: 16px; font-weight: 700; margin: 0; flex: 1; }
.cat-count { font-size: 13px; color: var(--hint); font-variant-numeric: tabular-nums; }
.cat-done-tick { color: var(--ok); font-weight: 700; font-size: 14px; }

.task {
  padding: 11px 0 12px;
  border-top: 1px solid var(--line);
}
.task:first-of-type { border-top: none; }
.task-desc { font-size: 15px; margin-bottom: 3px; }
.task-desc .num { color: var(--hint); }
.freq-badge {
  display: inline-block; font-size: 11px; font-weight: 700;
  color: var(--issue); background: var(--issue-bg);
  border-radius: 6px; padding: 1px 6px; margin-left: 6px;
  vertical-align: 1px;
}
.task-note {
  font-size: 13px; color: var(--issue);
  margin: 2px 0 8px; word-break: break-word;
}
.task-meta {
  font-size: 13px; color: var(--ok);
  margin: 2px 0 8px; word-break: break-word;
}
.seg { display: flex; gap: 6px; margin-top: 8px; }
.seg button {
  flex: 1; min-height: 42px;
  border-radius: 10px; font-weight: 600; font-size: 14px;
  background: var(--bg2); color: var(--hint);
  border: 1px solid var(--line);
}
.seg button.sel-ok    { background: var(--ok-bg);    color: var(--ok);    border-color: var(--ok); }
.seg button.sel-issue { background: var(--issue-bg); color: var(--issue); border-color: var(--issue); }
.seg button.sel-na    { background: var(--line);     color: var(--text);  border-color: var(--hint); }

/* ---------- fixed bottom submit ---------- */
.bottom-bar {
  position: fixed; left: 0; right: 0; bottom: 0;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--bg);
  border-top: 1px solid var(--line);
}
.bottom-bar .inner { max-width: 560px; margin: 0 auto; }
.big-btn {
  width: 100%; min-height: 50px;
  border-radius: 12px; font-size: 16px; font-weight: 700;
  background: var(--accent); color: var(--accent-text);
}
.big-btn:disabled { opacity: 0.45; }
.big-btn.secondary { background: var(--bg2); color: var(--text); }

/* ---------- issue sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45); z-index: 20;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  background: var(--bg);
  border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(16px + env(safe-area-inset-bottom));
  max-width: 560px; margin: 0 auto;
}
.sheet h3 { margin: 0 0 10px; font-size: 16px; }
.sheet textarea, .card textarea {
  width: 100%; min-height: 90px; resize: vertical;
  background: var(--bg2); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 12px; outline: none;
}
.sheet textarea:focus, .card textarea:focus { border-color: var(--accent); }
.fields { margin-top: 10px; }
.field-label {
  display: block; font-size: 13px; font-weight: 600;
  margin: 10px 0 4px;
}
.field-label .unit { color: var(--hint); font-weight: 400; }
/* The acceptable range, beside the field it judges — the same limit the
   printed service sheet carries. */
.field-label .accept {
  float: right; font-weight: 700; font-size: 12px;
  color: var(--ok);
}
.value-field {
  width: 100%;
  background: var(--bg2); border: 1px solid var(--line);
  border-radius: 12px; padding: 11px 12px; outline: none;
  font-size: 16px;
}
.value-field:focus { border-color: var(--accent); }
.big-btn.ok { background: var(--ok); color: #fff; }

.photo-req-note {
  font-size: 12.5px; color: var(--hint); margin: 14px 0 6px;
}
.photo-stage {
  background: var(--bg2);
  border: 1px dashed var(--hint);
  border-radius: 12px;
  padding: 8px 10px;
  margin-bottom: 8px;
}
.stage-head { display: flex; align-items: center; gap: 8px; }
.slot-label { font-size: 14px; font-weight: 700; min-width: 4.2em; }
.slot-req { font-size: 12px; color: var(--hint); flex: 1; }
.slot-req.done { color: var(--ok); font-weight: 600; }
.add-photo {
  background: var(--bg); color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 10px; padding: 7px 12px;
  font-weight: 600; font-size: 13px;
}
.add-photo:disabled { opacity: 0.45; pointer-events: none; }
.thumbs { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; margin-bottom: 6px; }
.photo-groups { display: flex; flex-direction: column; gap: 20px; }
.photo-group .section-hint { margin: 0 0 8px; }
.photo-group .thumbs { margin: 0; }
.thumb {
  position: relative;
  width: 56px; height: 56px; flex-shrink: 0;
  border-radius: 10px; overflow: visible;
  background: var(--bg);
  border: 1px solid var(--line);
  display: grid; place-items: center;
}
/* Read-only registry/field-photo thumbs never show a remove badge or
   spinner outside their box, so clip them hard — this guarantees no
   image, regardless of its natural aspect ratio, can ever bleed past
   its 56x56 box into whatever sits below the photo grid. */
.thumb.view { overflow: hidden; }
.thumb img {
  width: 100%; height: 100%;
  object-fit: cover; border-radius: 10px;
}
.thumb-ph { font-size: 22px; }
.thumb-spinner {
  position: absolute; inset: 0;
  display: grid; place-items: center;
}
.thumb-spinner::after {
  content: ""; width: 20px; height: 20px;
  border-radius: 50%;
  border: 2.5px solid var(--line);
  border-top-color: var(--accent);
  animation: thumb-spin 0.7s linear infinite;
}
@keyframes thumb-spin { to { transform: rotate(360deg); } }
.thumb-x {
  position: absolute; top: -7px; right: -7px;
  width: 21px; height: 21px;
  border-radius: 50%;
  background: var(--issue); color: #fff;
  font-size: 11px; line-height: 1;
  display: grid; place-items: center;
  border: 2px solid var(--bg2);
}
.thumb.view { cursor: pointer; }
.sheet-actions {
  display: flex; gap: 8px;
  margin-top: 0; padding-top: 14px;
  border-top: 1px solid var(--line);
}
.sheet-actions .big-btn { flex: 1; }

/* ---------- fullscreen image viewer ---------- */
.viewer-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,0.92);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px;
}
.viewer-img {
  max-width: 92vw; max-height: 68vh;
  object-fit: contain; border-radius: 8px;
  touch-action: pan-y;
}
.viewer-close {
  position: fixed; top: calc(12px + env(safe-area-inset-top)); right: 14px;
  width: 36px; height: 36px; border-radius: 50%;
  background: rgba(255,255,255,0.12); color: #fff;
  font-size: 16px; line-height: 1; display: grid; place-items: center;
  z-index: 51;
}
.viewer-download {
  position: fixed; top: calc(12px + env(safe-area-inset-top)); left: 14px;
  width: 36px; height: 36px; border-radius: 50%;
  background: rgba(255,255,255,0.12); color: #fff;
  font-size: 16px; line-height: 1; display: grid; place-items: center;
  z-index: 51;
}
.viewer-arrow {
  position: fixed; top: 50%; transform: translateY(-50%);
  width: 42px; height: 42px; border-radius: 50%;
  background: rgba(255,255,255,0.12); color: #fff;
  font-size: 26px; line-height: 1; display: grid; place-items: center;
  z-index: 51;
}
.viewer-prev { left: 10px; }
.viewer-next { right: 10px; }
.viewer-caption {
  color: rgba(255,255,255,0.85); font-size: 13px;
  text-align: center; max-width: 88vw; padding: 0 12px;
}
.viewer-count {
  position: fixed; top: calc(14px + env(safe-area-inset-top)); left: 0; right: 0;
  text-align: center; color: rgba(255,255,255,0.7); font-size: 13px;
}

/* ---------- success ---------- */
.success { text-align: center; padding: 44px 20px; }
.success .emoji { font-size: 56px; }
.success h2 { margin: 12px 0 6px; }
.success .counts { color: var(--hint); margin-bottom: 26px; }

.form-field {
  width: 100%; background: var(--bg2); border: 1px solid var(--line);
  border-radius: 12px; padding: 13px 14px; font-size: 16px;
  margin: 4px 0 14px; outline: none;
}
.form-field:focus { border-color: var(--accent); }
.error-note {
  background: var(--issue-bg); color: var(--issue);
  border-radius: 12px; padding: 12px 14px; font-size: 14px; margin-bottom: 10px;
}

/* ---------- login ---------- */
.login-wrap { padding-top: 8vh; }
.login-brand { text-align: center; margin-bottom: 22px; }
.login-brand .login-emoji { font-size: 48px; }
.login-brand h1 { margin: 6px 0 2px; font-size: 28px; letter-spacing: 0.04em; }
.login-brand .sub { margin: 0; font-size: 13px; color: var(--hint); }
.login-hint {
  font-size: 12.5px; color: var(--hint);
  text-align: center; margin: 14px 0 2px;
}
.signout-link {
  display: block; margin: 18px auto 0;
  background: none; color: var(--hint);
  font-size: 13px; text-decoration: underline;
}

/* ---------- asset registry ---------- */
.asset-row {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 0;
  border-top: 1px solid var(--line);
}
.asset-row:first-child { border-top: none; }
.asset-row.inactive .asset-label { color: var(--hint); text-decoration: line-through; }
.asset-label { flex: 1; font-size: 15px; font-weight: 600; }
.asset-btn {
  background: var(--bg2); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 10px; font-size: 14px;
}
.sheet-asset { color: var(--hint); font-weight: 400; font-size: 14px; }

.section-hint {
  font-size: 13px; color: var(--hint);
  margin: 2px 4px 10px;
  text-transform: uppercase; letter-spacing: 0.06em; font-weight: 600;
}

.region-line {
  display: flex; align-items: center; gap: 8px;
  font-size: 14px; font-weight: 600;
  margin: 0 4px 12px;
}
.region-change {
  background: none; color: var(--accent);
  font-size: 13px; font-weight: 600; text-decoration: underline;
  padding: 0;
}

.asset-main {
  flex: 1;
  display: flex; align-items: center; gap: 8px;
  background: none; text-align: left; padding: 0;
  color: var(--text);
}
.asset-main .asset-label { flex: 1; }
.chip.bad { background: var(--issue-bg); color: var(--issue); }
.chip.info { background: rgba(42, 171, 238, 0.15); color: var(--accent); }
.info-row {
  display: flex; justify-content: space-between; gap: 12px;
  padding: 7px 0; border-top: 1px solid var(--line);
  font-size: 14px;
}
.info-row span { color: var(--hint); }
.info-row b { text-align: right; word-break: break-word; }

.dist {
  display: inline-block; margin-left: 6px;
  font-size: 11.5px; font-weight: 700; color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.sheet-scroll { max-height: 86vh; overflow-y: auto; }
.thumb.view { width: 64px; height: 64px; }

.slot-label.wide { min-width: 0; flex: 1; font-size: 13px; }

.record-link {
  display: block; width: 100%;
  background: var(--bg); color: var(--accent);
  border-radius: 12px; padding: 12px 14px;
  font-weight: 600; font-size: 14px; text-align: left;
  margin-bottom: 10px;
}
.record-head { display: flex; flex-direction: column; gap: 2px; }
.record-head span { font-size: 13px; color: var(--hint); }
.done-note { color: var(--ok); font-weight: 600; }
.thumb.view.sm { width: 44px; height: 44px; }

.last-maint {
  font-size: 13px; color: var(--hint);
  margin: 0 4px 10px;
}
.last-maint b { color: var(--text); }

/* ---------- emergency work ---------- */
.ew-report-btn { margin-bottom: 14px; }
.chip.warn { background: rgba(226, 140, 58, 0.16); color: #e28c3a; }
.ew-row {
  display: block; width: 100%; text-align: left;
  background: var(--bg); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 8px; color: var(--text);
}
.ew-row:active { opacity: 0.7; }
.ew-row-top {
  display: flex; align-items: center; gap: 8px;
}
.ew-row-top b {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 14px; color: var(--accent); flex: 1;
}
.ew-pr { font-size: 12.5px; font-weight: 600; }
.ew-row-mid { font-size: 13.5px; margin-top: 4px; }
.ew-row-type { font-size: 12.5px; color: var(--hint); margin-top: 2px; }
.ew-row-sum {
  font-size: 13.5px; color: var(--hint); margin-top: 3px;
  display: -webkit-box; -webkit-line-clamp: 2;
  -webkit-box-orient: vertical; overflow: hidden;
}
.ew-row-meta { font-size: 12px; color: var(--hint); margin-top: 4px; }

.ew-wt { display: flex; flex-direction: column; gap: 6px; }
.ew-wt-btn {
  text-align: left; font-size: 14.5px; font-weight: 600;
  background: var(--bg2); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px;
  padding: 11px 12px;
}
.ew-wt-btn.sel {
  background: var(--issue-bg); color: var(--issue);
  border-color: var(--issue);
}
.ew-check {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 4px; border-top: 1px solid var(--line);
  font-size: 14.5px;
}
.ew-check:first-of-type { border-top: none; }
.ew-check input {
  width: 20px; height: 20px; accent-color: var(--accent); flex: none;
}

.ew-prio { display: flex; gap: 6px; }
.ew-prio button {
  flex: 1; display: flex; flex-direction: column; gap: 2px;
  align-items: center; padding: 10px 4px;
  background: var(--bg2); color: var(--hint);
  border: 1px solid var(--line); border-radius: 12px;
}
.ew-prio button b { font-size: 13.5px; }
.ew-prio button span { font-size: 10.5px; }
.ew-prio button.sel {
  background: var(--issue-bg); color: var(--issue); border-color: var(--issue);
}

.ew-steps {
  display: flex; gap: 2px; margin: 10px 0 12px;
}
.ew-step {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; gap: 4px; position: relative;
}
.ew-step .dot {
  width: 24px; height: 24px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700;
  background: var(--line); color: var(--hint);
}
.ew-step.done .dot { background: var(--ok-bg); color: var(--ok); }
.ew-step.cur .dot { background: var(--accent); color: #fff; }
.ew-step .lbl {
  font-size: 10px; text-align: center; color: var(--hint); line-height: 1.2;
}
.ew-step.cur .lbl { color: var(--text); font-weight: 700; }

.ew-tags { display: flex; flex-wrap: wrap; gap: 6px; }
.ew-tag {
  font-size: 12px; font-weight: 600;
  background: var(--issue-bg); color: var(--issue);
  border-radius: 8px; padding: 3px 8px;
}
.ew-text {
  font-size: 14px; background: var(--bg2);
  border-radius: 10px; padding: 9px 11px;
  white-space: pre-wrap; word-break: break-word;
}
select.form-field { -webkit-appearance: none; appearance: none; }

/* ---------- AI-assisted emergency reporting ---------- */
.ew-chat {
  display: flex; flex-direction: column; gap: 8px;
  min-height: 220px; max-height: 46vh; overflow-y: auto;
  padding: 4px 2px 8px;
}
.ew-bubble {
  max-width: 86%; padding: 9px 12px; border-radius: 14px;
  font-size: 14.5px; line-height: 1.4; white-space: pre-wrap;
  word-break: break-word;
}
.ew-bubble.assistant {
  align-self: flex-start; background: var(--bg2);
  border-bottom-left-radius: 4px;
}
.ew-bubble.user {
  align-self: flex-end; background: var(--accent);
  color: var(--accent-text); border-bottom-right-radius: 4px;
}
.ew-bubble.typing { color: var(--hint); letter-spacing: 3px; }
.ew-chat-input { display: flex; gap: 8px; align-items: flex-end; }
.ew-chat-input textarea { flex: 1; min-height: 48px; }
.ew-chat-input .big-btn { width: auto; padding: 0 18px; min-height: 48px; }

.ai-banner {
  background: rgba(42, 171, 238, 0.12);
  border: 1px solid rgba(42, 171, 238, 0.35);
  color: var(--text);
  border-radius: 12px; padding: 10px 12px;
  font-size: 13.5px; margin-bottom: 10px;
}

/* Sign-in with S-Water credentials (first open on a phone). */
.signin-error {
  margin: 8px 0 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--issue-bg);
  color: var(--issue);
  font-size: 14px;
}
#signin-form .field-label { margin-top: 12px; }
#signin-form .form-field { width: 100%; }