:root {
  --bg: var(--tg-theme-bg-color, #fff);
  --text: var(--tg-theme-text-color, #111);
  --hint: var(--tg-theme-hint-color, #777);
  --link: var(--tg-theme-link-color, #2481cc);
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #fff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f4f4f5);
  --section-bg: var(--tg-theme-section-bg-color, #fff);
  --header-bg: var(--tg-theme-header-bg-color, #fff);
  --destructive: var(--tg-theme-destructive-text-color, #e3502c);
  --border: color-mix(in srgb, var(--text) 18%, transparent);
  --border-strong: color-mix(in srgb, var(--text) 32%, transparent);
  --shadow: 0 -2px 12px rgba(0, 0, 0, 0.4);
  --ontime: #34a853;
  --late: #f4a02f;
  --missed: #9aa0a6;
  --manual: #f4ce53;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: contain;
}

button {
  font-family: inherit;
  font-size: inherit;
  cursor: pointer;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--header-bg);
  border-bottom: 1px solid var(--border);
  padding: 8px 12px 0;
}

.title-wrap {
  margin-bottom: 8px;
  line-height: 1.2;
}

.title {
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.5px;
}

.subtitle {
  font-size: 12px;
  color: var(--hint);
  margin-top: 2px;
}

.tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
}

.tab {
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  padding: 10px 12px;
  color: var(--hint);
  font-weight: 500;
  white-space: nowrap;
}

.tab.active {
  color: var(--text);
  border-bottom-color: var(--button);
}

#root {
  padding: 12px;
  padding-bottom: 96px;
}

.center {
  text-align: center;
  padding: 24px;
}

.muted {
  color: var(--hint);
}

.row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.spacer {
  flex: 1;
}

.btn {
  background: var(--button);
  color: var(--button-text);
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  font-weight: 500;
}

.btn.secondary {
  background: var(--secondary-bg);
  color: var(--text);
}

.btn.danger {
  background: transparent;
  color: var(--destructive);
}

.schedule-sticky {
  position: sticky;
  top: var(--header-height, 88px);
  z-index: 4;
  background: var(--bg);
  padding-top: 8px;
  margin: -12px -12px 8px;
  padding-left: 12px;
  padding-right: 12px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}

.schedule-sticky > * {
  margin-bottom: 8px;
}

.schedule-sticky > *:last-child {
  margin-bottom: 0;
}

.month-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.month-nav .label {
  flex: 1;
  text-align: center;
  font-weight: 600;
}

.actions {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.actions .btn {
  flex: 1;
  min-width: 120px;
  padding: 10px 8px;
  font-size: 14px;
}

.legend {
  display: flex;
  gap: 12px;
  padding: 8px 0;
  font-size: 12px;
  color: var(--hint);
  flex-wrap: wrap;
}

.legend .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  margin-right: 4px;
  vertical-align: middle;
}

.dot.ontime { background: var(--ontime); }
.dot.late { background: var(--late); }
.dot.missed { background: var(--missed); }
.dot.manual { background: var(--manual); }

.schedule-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13px;
  table-layout: fixed;
}

.schedule-table thead th {
  position: sticky;
  top: 0;
  background: var(--section-bg);
  font-weight: 500;
  font-size: 11px;
  padding: 6px 4px;
  border-bottom: 1px solid var(--border);
  color: var(--hint);
  text-align: center;
  z-index: 1;
}

.schedule-table thead th:not(:last-child),
.schedule-table tbody td:not(:last-child) {
  border-right: 1px solid var(--border-strong);
}

.schedule-table thead th:first-child,
.schedule-table tbody td:first-child {
  border-right: 2px solid var(--border-strong);
}

.schedule-table thead th,
.schedule-table tbody td {
  border-bottom-color: var(--border-strong);
}

.schedule-table tbody td {
  padding: 4px;
  border-bottom: 1px solid var(--border);
  text-align: center;
  vertical-align: middle;
  transition: background 0.1s;
}

.schedule-table tbody tr.row-block-0 td {
  background: color-mix(in srgb, var(--text) 3%, transparent);
}

.schedule-table tbody tr.row-block-1 td {
  background: transparent;
}

.schedule-table tbody tr.weekend-row td {
  background: color-mix(in srgb, var(--destructive) 6%, transparent);
}

.schedule-table .day-cell {
  font-weight: 500;
  color: var(--hint);
  width: 36px;
  font-size: 12px;
}

.schedule-table .day-cell.weekend {
  color: var(--destructive);
  opacity: 0.7;
}

.schedule-table .day-cell.today {
  color: var(--button-text);
  background: var(--button);
  border-radius: 8px;
  opacity: 1;
}

.schedule-table tbody tr.today-row td {
  background: color-mix(in srgb, var(--button) 18%, transparent);
}

.schedule-table tbody tr.today-row td:first-child {
  border-left: 3px solid var(--button);
}

.cell {
  padding: 8px 6px;
  border-radius: 8px;
  background: var(--secondary-bg);
  cursor: pointer;
  position: relative;
  min-height: 40px;
  font-size: 13px;
  line-height: 1.3;
  word-break: break-word;
  text-align: center;
}

.cell.empty {
  color: var(--hint);
  background: transparent;
  border: 1px dashed var(--border);
}

.cell.manual {
  outline: 1.5px solid var(--manual);
  outline-offset: -2px;
}

.cell .status {
  position: absolute;
  top: 2px;
  right: 4px;
  font-size: 10px;
  line-height: 1;
}

.status.ontime { color: var(--ontime); }
.status.late { color: var(--late); }
.status.missed { color: var(--missed); }

.cell.read-only {
  cursor: default;
}

.note-tag {
  display: block;
  font-size: 10px;
  color: var(--hint);
  margin-top: 2px;
  font-style: italic;
}

/* Sheet (bottom modal) */
.sheet {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 10;
}

.sheet.hidden { display: none; }

.sheet-card {
  width: 100%;
  max-width: 560px;
  background: var(--bg);
  border-radius: 16px 16px 0 0;
  padding: 16px;
  box-shadow: var(--shadow);
  max-height: 80vh;
  overflow-y: auto;
}

.sheet-card h3 {
  margin: 0 0 12px;
  font-size: 16px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}

.field label {
  color: var(--hint);
  font-size: 12px;
}

.field input,
.field select,
.field textarea {
  background: var(--secondary-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
}

.field textarea {
  resize: vertical;
  min-height: 60px;
}

.sheet-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.sheet-actions .btn {
  flex: 1;
}

/* Employees */
.emp-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.emp-card {
  background: var(--secondary-bg);
  border-radius: 12px;
  padding: 12px;
}

.emp-card .name {
  font-weight: 500;
}

.emp-card .meta {
  color: var(--hint);
  font-size: 12px;
  margin-top: 4px;
}

.emp-card .actions-row {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.role-pill {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 11px;
  margin-right: 4px;
}

.subtabs {
  display: flex;
  gap: 4px;
  margin-bottom: 12px;
  background: var(--secondary-bg);
  border-radius: 10px;
  padding: 4px;
}

.subtabs button {
  flex: 1;
  background: transparent;
  border: 0;
  padding: 8px;
  border-radius: 8px;
  color: var(--hint);
  font-weight: 500;
}

.subtabs button.active {
  background: var(--bg);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}

/* Stats */
.period-selector {
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.stats-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.stats-table th, .stats-table td {
  padding: 8px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.stats-table th {
  color: var(--hint);
  font-weight: 500;
  font-size: 12px;
}

.stats-table td.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.stats-incident {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  margin-top: 8px;
}

.calendar-cell {
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  background: var(--secondary-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: var(--hint);
}

.calendar-cell.ontime { background: var(--ontime); color: #fff; }
.calendar-cell.late { background: var(--late); color: #fff; }
.calendar-cell.missed { background: var(--missed); color: #fff; }

.error-banner {
  background: var(--destructive);
  color: #fff;
  padding: 10px;
  border-radius: 8px;
  margin-bottom: 12px;
}

.pending-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  background: color-mix(in srgb, var(--button) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--button) 35%, transparent);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.pending-banner .pending-icon { font-size: 18px; }

.pending-banner .pending-text {
  flex: 1;
  font-weight: 500;
  font-size: 13px;
}

.pending-banner .pending-actions {
  display: flex;
  gap: 6px;
}

.btn.small {
  padding: 6px 10px;
  font-size: 12px;
  border-radius: 8px;
}

.btn.primary {
  background: var(--button);
  color: var(--button-text);
  font-weight: 600;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.8);
  color: #fff;
  padding: 8px 16px;
  border-radius: 999px;
  z-index: 20;
  font-size: 13px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}

.toast.show { opacity: 1; }
