:root {
  --accent: #0f766e;
  --accent-ink: #fff;
  --bg: #f6f7f8;
  --card: #fff;
  --ink: #15191e;
  --muted: #667085;
  --line: #e4e7ec;
  --in: #0f766e;
  --out: #b54708;
  --warn: #b42318;
  --warn-bg: #fef3f2;
  --radius: 14px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --card: #161b22;
    --ink: #e6edf3;
    --muted: #8b949e;
    --line: #2a313a;
    --in: #2dd4bf;
    --out: #fdb022;
    --warn: #f97066;
    --warn-bg: #3a1714;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
body[data-view="capture"] { background: #000; }

svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

main { height: 100%; }
.view {
  display: none;
  min-height: 100%;
  padding: calc(var(--safe-t) + 12px) 16px calc(var(--tabbar-h) + var(--safe-b) + 24px);
}
.view.active { display: block; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--card); color: var(--ink);
  font: inherit; font-weight: 600; cursor: pointer;
}
.btn.small { min-height: 36px; padding: 0 14px; font-size: 14px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--warn); }
.btn.ghost { background: transparent; }
.btn:active { transform: scale(0.97); }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 44px; height: 44px; border-radius: 50%;
  border: 0; background: transparent; color: inherit; font-size: 28px; cursor: pointer;
}

/* ---------- Capture ---------- */
#view-capture {
  padding: 0;
  display: none;
  height: 100dvh;
  flex-direction: column;
  color: #fff;
  background: #000;
}
#view-capture.active { display: flex; }
.viewfinder { position: relative; flex: 1; overflow: hidden; }
.viewfinder video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.guide {
  position: absolute; inset: 14% 12% 10%;
  border: 2px solid rgba(255, 255, 255, 0.55); border-radius: 18px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, 0.25);
  pointer-events: none;
}
.flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.flash.on { animation: flash 0.35s ease-out; }
@keyframes flash { from { opacity: 0.85; } to { opacity: 0; } }
.cam-msg {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  padding: 24px; text-align: center; background: #111;
}
.cam-msg[hidden] { display: none; }
.cam-msg .btn.ghost { color: #fff; border-color: #444; }
.floating { position: absolute; top: calc(var(--safe-t) + 8px); right: 8px; background: rgba(0, 0, 0, 0.35); color: #fff; }
.floating.left { left: 8px; right: auto; }
.floating.left.second { left: 60px; }
#camToggleBtn .cam-off-icon, #camToggleBtn[aria-pressed="true"] .cam-on-icon { display: none; }
#camToggleBtn[aria-pressed="true"] .cam-off-icon { display: block; }
body.cam-off .guide { display: none; }
body.cam-off .shutter { opacity: 0.35; }
.icon-btn[hidden] { display: none; }

.capture-bar {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; justify-items: center;
  padding: 16px 24px calc(var(--tabbar-h) + var(--safe-b) + 16px);
  background: #000;
}
.capture-bar .icon-btn { width: 52px; height: 52px; background: rgba(255, 255, 255, 0.12); }
.shutter {
  width: 76px; height: 76px; border-radius: 50%;
  border: 4px solid #fff; background: #fff; box-shadow: inset 0 0 0 4px #000;
  cursor: pointer;
}
.shutter:active { transform: scale(0.92); }
.last-thumb {
  width: 52px; height: 52px; border-radius: 10px; border: 2px solid rgba(255, 255, 255, 0.6);
  background: #222 center / cover no-repeat; cursor: pointer;
}
.last-thumb:not([style*="url"]) { visibility: hidden; }

.last-result {
  position: absolute; left: 12px; right: 12px;
  bottom: 12px;
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; border-radius: var(--radius);
  background: rgba(20, 20, 20, 0.88); color: #fff; backdrop-filter: blur(8px);
  font-size: 15px;
}
.last-result[hidden] { display: none; }
.last-result img { width: 44px; height: 44px; border-radius: 8px; object-fit: cover; }
.last-result .grow { flex: 1; min-width: 0; }
.last-result .btn { min-height: 34px; background: rgba(255, 255, 255, 0.15); color: #fff; border: 0; }

/* ---------- Common view chrome ---------- */
.view-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.view-head h1 { font-size: 22px; margin: 0; flex: 1; }
#view-hours .view-head h1 { text-align: center; }
.empty { color: var(--muted); text-align: center; margin-top: 48px; }
.muted { color: var(--muted); font-size: 14px; }

/* ---------- Photos ---------- */
.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 8px; }
.tile {
  position: relative; aspect-ratio: 3 / 4; padding: 0; overflow: hidden;
  border: 0; border-radius: 10px; background: var(--line); cursor: pointer;
}
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile .badge {
  position: absolute; left: 4px; right: 4px; bottom: 4px;
  padding: 3px 6px; border-radius: 6px;
  background: rgba(0, 0, 0, 0.7); color: #fff; font-size: 12px; font-weight: 600; text-align: left;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tile .badge.warn { background: var(--warn); }
.tile .date { position: absolute; top: 4px; left: 4px; padding: 2px 6px; border-radius: 6px; background: rgba(0, 0, 0, 0.55); color: #fff; font-size: 11px; }

/* ---------- Hours ---------- */
.summary { display: grid; grid-template-columns: 1.4fr 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; display: flex; flex-direction: column; }
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-sub { font-size: 12px; color: var(--muted); }
.open-shift { background: var(--card); border: 1px solid var(--in); color: var(--in); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 12px; font-weight: 600; }
.open-shift[hidden] { display: none; }
.hours-actions { display: flex; gap: 8px; margin-bottom: 12px; }
.day-list { display: flex; flex-direction: column; gap: 10px; }
.day { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.day-head { display: flex; justify-content: space-between; align-items: baseline; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.day-head strong { font-variant-numeric: tabular-nums; }
.punch-list { display: flex; flex-direction: column; }
.punch {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 10px 14px; border: 0; border-bottom: 1px solid var(--line);
  background: transparent; color: inherit; font: inherit; text-align: left; cursor: pointer;
}
.punch:last-child { border-bottom: 0; }
.punch .chip { min-width: 44px; text-align: center; padding: 2px 8px; border-radius: 6px; font-size: 13px; font-weight: 700; color: #fff; }
.chip.in { background: var(--in); }
.chip.out { background: var(--out); }
.punch .time { font-variant-numeric: tabular-nums; font-weight: 600; }
.punch .sub { flex: 1; color: var(--muted); font-size: 13px; text-align: right; }
.punch.problem { background: var(--warn-bg); }
.punch.problem .sub { color: var(--warn); }
.shift-line { padding: 4px 14px 4px 68px; font-size: 12px; color: var(--muted); }

/* ---------- Dialogs ---------- */
dialog.sheet {
  width: 100%; max-width: 560px; max-height: 92dvh;
  margin: auto auto 0; padding: 0;
  border: 0; border-radius: 20px 20px 0 0;
  background: var(--card); color: var(--ink);
}
@media (min-width: 600px) { dialog.sheet { margin: auto; border-radius: 20px; } }
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.5); }
.sheet-body { padding: 18px 16px calc(16px + var(--safe-b)); display: flex; flex-direction: column; gap: 12px; }
.sheet-body h2 { margin: 0; font-size: 20px; }
.sheet-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
.photo-full { width: 100%; max-height: 55dvh; object-fit: contain; border-radius: 10px; background: #000; }
.photo-ref { width: 100%; max-height: 38dvh; object-fit: contain; border-radius: 10px; background: #000; }
.photo-ref[hidden] { display: none; }
#photoPunches .punch { border: 1px solid var(--line); border-radius: 10px; margin-bottom: 6px; }
pre { white-space: pre-wrap; font-size: 13px; background: var(--bg); padding: 10px; border-radius: 8px; margin: 8px 0 0; }

.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 14px; }
.field input, .field select {
  font: inherit; font-size: 17px; font-weight: 400;
  padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink);
}
.field small { font-weight: 400; }

.segmented { display: grid; grid-template-columns: 1fr 1fr; gap: 0; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; padding: 10px; text-align: center; font-weight: 700; cursor: pointer; }
.segmented label:first-child input:checked + span { background: var(--in); color: #fff; }
.segmented label:last-child input:checked + span { background: var(--out); color: #fff; }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(3, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: var(--card); border-top: 1px solid var(--line);
}
body[data-view="capture"] .tabbar { background: #000; border-top-color: #222; }
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  border: 0; background: transparent; color: var(--muted); font: inherit; font-size: 11px; font-weight: 600; cursor: pointer;
}
.tab.active { color: var(--accent); }
body[data-view="capture"] .tab { color: #888; }
body[data-view="capture"] .tab.active { color: #fff; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; top: calc(var(--safe-t) + 12px); z-index: 100;
  transform: translate(-50%, -150%); transition: transform 0.25s;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 12px;
  background: #222; color: #fff; font-size: 14px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.3);
}
.toast.show { transform: translate(-50%, 0); }

.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin 0.8s linear infinite; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }
