:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --text: #1c1f24;
  --muted: #667085;
  --border: #e3e6eb;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --in: #16a34a;
  --out: #dc2626;
  --warn-bg: #fff7e6;
  --warn-text: #92400e;
  --error-bg: #fdecec;
  --error-text: #b42318;
  --ok-bg: #e8f7ee;
  --ok-text: #166534;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --text: #e8eaed;
    --muted: #9aa1ad;
    --border: #2a2e36;
    --primary: #3b82f6;
    --warn-bg: #3a2c10;
    --warn-text: #fcd34d;
    --error-bg: #3b1515;
    --error-text: #fca5a5;
    --ok-bg: #11301d;
    --ok-text: #86efac;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

[hidden] { display: none !important; }

a { color: var(--primary); }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .who { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .who small { display: block; font-weight: 400; color: var(--muted); font-size: 13px; }

main { max-width: 560px; margin: 0 auto; padding: 16px; }
main.wide { max-width: 1280px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}
.card h2 { margin: 0 0 12px; font-size: 18px; }

label { display: block; font-size: 14px; font-weight: 600; margin: 12px 0 6px; }
input, select {
  width: 100%;
  font: inherit;
  font-size: 16px; /* >=16px prevents iOS zoom on focus */
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: 1px; }

button, .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font: inherit;
  font-weight: 600;
  padding: 12px 18px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: var(--primary);
  color: var(--primary-text);
  cursor: pointer;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
button:disabled { opacity: 0.55; cursor: default; }
button.secondary { background: transparent; color: var(--text); border-color: var(--border); }
button.small { padding: 8px 12px; font-size: 14px; }
button.block { width: 100%; }

.alert { padding: 12px 14px; border-radius: 10px; margin: 12px 0; font-size: 15px; }
.alert.error { background: var(--error-bg); color: var(--error-text); }
.alert.warn { background: var(--warn-bg); color: var(--warn-text); }
.alert.ok { background: var(--ok-bg); color: var(--ok-text); }

.muted { color: var(--muted); }
.center { text-align: center; }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-wrap .card { width: 100%; max-width: 380px; }
.brand { text-align: center; margin-bottom: 8px; }
.brand h1 { margin: 8px 0 0; font-size: 22px; }
.brand p { margin: 4px 0 0; }
.login-wrap button[type="submit"] { margin-top: 20px; width: 100%; padding: 14px; }

/* ---------- Employee home ---------- */
.status { text-align: center; padding: 24px 16px; }
.status .label { font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.status .state { font-size: 24px; font-weight: 700; margin: 6px 0 2px; }
.status .state.in { color: var(--in); }
.status .elapsed { font-variant-numeric: tabular-nums; color: var(--muted); }

.punch-btn {
  width: 100%;
  padding: 22px;
  font-size: 22px;
  border-radius: 16px;
  margin-top: 16px;
}
.punch-btn.in { background: var(--in); }
.punch-btn.out { background: var(--out); }

.history-day { border-top: 1px solid var(--border); padding: 12px 0; }
.history-day:first-of-type { border-top: 0; padding-top: 0; }
.history-day .day-head { display: flex; justify-content: space-between; font-weight: 600; margin-bottom: 6px; }
.history-row { display: flex; justify-content: space-between; gap: 8px; font-size: 15px; padding: 3px 0; font-variant-numeric: tabular-nums; }
.history-row .dur { color: var(--muted); }
.tag { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; }
.tag.in { background: var(--ok-bg); color: var(--ok-text); }
.tag.out { background: var(--error-bg); color: var(--error-text); }
.tag.warn { background: var(--warn-bg); color: var(--warn-text); }

/* ---------- Day off / leave ---------- */
:root { --dayoff-bg: #ccfbf1; --dayoff-text: #0f766e; --cl-bg: #f3e8ff; --cl-text: #7e22ce; }
@media (prefers-color-scheme: dark) {
  :root { --dayoff-bg: #134e4a; --dayoff-text: #5eead4; --cl-bg: #2e1065; --cl-text: #d8b4fe; }
}
.tag.day_off { background: var(--dayoff-bg); color: var(--dayoff-text); }
.tag.casual_leave { background: var(--cl-bg); color: var(--cl-text); }
.today-leave { display: inline-block; margin-top: 8px; padding: 4px 12px; border-radius: 999px; background: var(--cl-bg); color: var(--cl-text); font-weight: 600; font-size: 14px; }
.leave-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.leave-actions button { padding: 14px 10px; }
.history-row button { flex-shrink: 0; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 150;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 16px;
  overflow-y: auto;
}
.modal-card { width: 100%; max-width: 420px; margin: auto 0; }
.quick-range { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.quick-range label { margin-top: 0; }
.hint { font-size: 14px; margin: 10px 0 8px; }

.cal { border: 1px solid var(--border); border-radius: 12px; padding: 10px; }
.cal-head { display: flex; align-items: center; justify-content: space-between; font-weight: 600; margin-bottom: 8px; }
.cal-head button { min-width: 40px; font-size: 18px; padding: 4px 10px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-dow { text-align: center; font-size: 12px; color: var(--muted); font-weight: 600; padding: 2px 0; }
.cal-day {
  aspect-ratio: 1;
  padding: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}
.cal-day.today { border: 2px solid var(--primary); }
.cal-day.sel { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.cal-day.day_off { background: var(--dayoff-bg); color: var(--dayoff-text); opacity: 1; }
.cal-day.casual_leave { background: var(--cl-bg); color: var(--cl-text); opacity: 1; }
.cal-day:disabled:not(.day_off):not(.casual_leave) { opacity: 0.3; }
.cal-legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12px; color: var(--muted); margin-top: 8px; }
.cal-legend span { display: inline-flex; align-items: center; gap: 4px; }
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; }
.sw.sel { background: var(--primary); }
.sw.day_off { background: var(--dayoff-text); }
.sw.casual_leave { background: var(--cl-text); }
.lv-summary { margin-top: 12px; font-size: 14px; font-weight: 600; }

/* ---------- Capture overlay ---------- */
.capture {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: #000;
  color: #fff;
  display: flex;
  flex-direction: column;
}
.capture-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  font-weight: 600;
}
.capture-head button { background: rgba(255, 255, 255, 0.15); color: #fff; }
.capture-view { position: relative; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.capture-view video, .capture-view img { width: 100%; height: 100%; object-fit: cover; }
.capture-view video.mirror { transform: scaleX(-1); }
.capture-msg {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  text-align: center;
  background: rgba(0, 0, 0, 0.72);
  font-size: 17px;
}
.capture-msg .big { font-size: 56px; line-height: 1; }
.capture-msg .title { font-size: 22px; font-weight: 700; }
.capture-msg .actions { display: flex; flex-direction: column; gap: 10px; width: 100%; max-width: 320px; }
.capture-msg .actions button { width: 100%; }
.capture-msg .actions button.secondary { color: #fff; border-color: rgba(255, 255, 255, 0.4); }
.capture-foot { padding: 12px 16px; padding-bottom: max(16px, env(safe-area-inset-bottom)); text-align: center; }
.gps-line { font-size: 14px; margin-bottom: 12px; opacity: 0.9; min-height: 1.4em; }
.gps-line.ok { color: #86efac; }
.gps-line.bad { color: #fca5a5; }
.shutter {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  border: 5px solid #fff;
  background: rgba(255, 255, 255, 0.25);
  padding: 0;
}
.shutter:active { background: rgba(255, 255, 255, 0.6); }
.spinner {
  width: 44px;
  height: 44px;
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Admin ---------- */
.tabs { display: flex; gap: 4px; overflow-x: auto; margin-bottom: 16px; }
.tabs button { background: transparent; color: var(--muted); border-radius: 999px; white-space: nowrap; }
.tabs button.active { background: var(--primary); color: var(--primary-text); }

.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; align-items: end; }
.filters label { margin-top: 0; }
.filters .check { display: flex; align-items: center; gap: 8px; font-weight: 500; margin: 0; padding: 12px 0; }
.filters .check input { width: auto; }
.filter-actions { display: flex; gap: 8px; }
.filter-actions button { flex: 1; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--border); border-radius: 10px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 10px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; white-space: nowrap; }
th { background: var(--bg); font-weight: 600; position: sticky; top: 0; }
tr.day-start td { border-top: 2px solid var(--border); }
td.num { font-variant-numeric: tabular-nums; }
.thumb {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  object-fit: cover;
  cursor: zoom-in;
  background: var(--border);
  display: block;
}
.loc small { display: block; color: var(--muted); }
.open-session { color: var(--warn-text); font-weight: 600; }
.summary { display: flex; flex-wrap: wrap; gap: 16px; margin: 12px 0; font-size: 14px; }
.summary b { font-size: 18px; display: block; }

.checks { display: flex; flex-wrap: wrap; gap: 8px 16px; margin: 6px 0 4px; }
.checks label { display: flex; align-items: center; gap: 6px; font-weight: 500; margin: 0; }
.checks input { width: auto; }
.checks .muted { font-weight: 400; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip { background: var(--bg); border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px; font-size: 13px; }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 0 12px; }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.88);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  cursor: zoom-out;
}
.lightbox img { max-width: 100%; max-height: 85vh; border-radius: 8px; }
.lightbox .caption { color: #fff; margin-top: 12px; text-align: center; font-size: 14px; }
