/* Davomat WebApp — mobil birinchi (360px+), Telegram mavzu ranglari (--tg-theme-*). */

:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --text: var(--tg-theme-text-color, #111418);
  --hint: var(--tg-theme-hint-color, #6b7280);
  --link: var(--tg-theme-link-color, #2481cc);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --section-bg: var(--tg-theme-section-bg-color, var(--bg));
  --danger: var(--tg-theme-destructive-text-color, #e53935);
  --ok: #2e9e4f;
  --warn: #d68a00;
  --border: rgba(127, 127, 127, 0.22);
  --radius: 12px;
  --tap: 44px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: var(--tg-theme-bg-color, #17212b);
    --bg2: var(--tg-theme-secondary-bg-color, #0e1621);
    --text: var(--tg-theme-text-color, #f5f5f5);
    --hint: var(--tg-theme-hint-color, #8d99a6);
    --link: var(--tg-theme-link-color, #6ab3f3);
    --accent: var(--tg-theme-button-color, #5288c1);
    --section-bg: var(--tg-theme-section-bg-color, #17212b);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg2);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body.has-fallback-main { padding-bottom: 84px; }

.app { max-width: 720px; margin: 0 auto; padding: 12px 12px 32px; }

.boot { text-align: center; padding: 80px 0; color: var(--hint); }

h1 { font-size: 22px; margin: 4px 0 2px; line-height: 1.25; }
h2 { font-size: 15px; margin: 0 0 8px; color: var(--hint); text-transform: uppercase; letter-spacing: .03em; font-weight: 600; }
p { margin: 6px 0; }

.muted { color: var(--hint); }
.small { font-size: 13px; }
.center { text-align: center; }
.danger-text { color: var(--danger); }

.screen-header { margin: 4px 4px 14px; }
.screen-header p { margin: 2px 0 0; }

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

.list { display: flex; flex-direction: column; gap: 8px; }

.card {
  background: var(--section-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
}
.section .card { background: var(--bg); }
.card-click { cursor: pointer; }
.card-click:active { opacity: .75; }
.card-row { display: flex; align-items: center; gap: 10px; }
.card-main { flex: 1; min-width: 0; }
.card-title { font-weight: 600; display: flex; align-items: center; gap: 6px; word-break: break-word; }
.card-badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }

.dot-today { width: 10px; height: 10px; border-radius: 50%; background: var(--ok); flex: none; }

.badge {
  display: inline-block;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg2);
  color: var(--text);
  white-space: nowrap;
}
.badge-ok { background: rgba(46, 158, 79, .15); color: var(--ok); }
.badge-warn { background: rgba(214, 138, 0, .15); color: var(--warn); }
.badge-danger { background: rgba(229, 57, 53, .14); color: var(--danger); }
.badge-muted { color: var(--hint); }

/* --- Tugmalar --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: var(--tap);
  padding: 10px 16px;
  border: 0;
  border-radius: 10px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  touch-action: manipulation;
}
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-secondary { background: var(--bg2); color: var(--link); }
.section .btn-secondary { background: var(--bg2); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn-link { background: transparent; color: var(--link); padding: 8px 4px; min-height: 36px; font-weight: 500; }
.btn-block { width: 100%; }
.btn-compact { padding: 8px 10px; font-size: 14px; white-space: nowrap; }
.back-link { margin: 0 0 4px; }

.actions { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }
.actions-row { display: flex; flex-wrap: wrap; gap: 8px; }
.actions-row .btn { flex: 1 1 140px; }

.fallback-main-wrap {
  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(--border);
  z-index: 20;
}
.fallback-main-wrap .btn { max-width: 696px; margin: 0 auto; display: flex; }

/* --- Formalar --- */
.form { display: flex; flex-direction: column; gap: 14px; }
.field label { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.field-hint { font-size: 13px; color: var(--hint); margin-top: 4px; }
.field-error { font-size: 13px; color: var(--danger); margin-top: 4px; }
input[type="text"], input[type="tel"], input[type="date"], input[type="time"], input[type="number"], select, textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
textarea { min-height: 96px; resize: vertical; }
input.invalid, select.invalid, textarea.invalid { border-color: var(--danger); }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; }

.time-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.weekdays { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.weekday {
  display: flex; align-items: center; gap: 10px;
  min-height: var(--tap);
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  cursor: pointer;
}
.weekday input { width: 20px; height: 20px; margin: 0; accent-color: var(--accent); }
.weekday.checked { border-color: var(--accent); }

.photo-picker { display: flex; align-items: center; gap: 12px; }
.photo-preview {
  width: 88px; height: 88px;
  border-radius: 12px;
  background: var(--bg2);
  object-fit: cover;
  flex: none;
  display: flex; align-items: center; justify-content: center;
  font-size: 32px;
  color: var(--hint);
  overflow: hidden;
}
.photo-preview img { width: 100%; height: 100%; object-fit: cover; }
.file-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

/* --- Avatar / rasm --- */
.avatar { border-radius: 10px; object-fit: cover; background: var(--bg2); flex: none; }
.avatar-sm { width: 44px; height: 44px; }
.avatar-lg { width: 132px; height: 132px; border-radius: 16px; }
.zoomable { cursor: zoom-in; }
.photo-viewer {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, .88);
  display: flex; align-items: center; justify-content: center;
  z-index: 50; padding: 16px; cursor: zoom-out;
}
.photo-viewer img { max-width: 100%; max-height: 100%; border-radius: 12px; }

/* --- Davomat --- */
.att-header { margin: 4px 4px 10px; }
.att-header h1 { text-transform: uppercase; }
.att-counters { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 8px; font-weight: 600; }
.att-counters .grow { flex: 1; }
.notice {
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(36, 129, 204, .12);
  margin-bottom: 10px;
  font-size: 14px;
}
.notice-warn { background: rgba(214, 138, 0, .14); }
.notice .actions { margin: 8px 0 0; }

.att-list { display: flex; flex-direction: column; gap: 6px; }
.att-row {
  display: grid;
  grid-template-columns: 28px 44px 1fr 56px 40px;
  align-items: center;
  gap: 8px;
  background: var(--section-bg);
  border-radius: 10px;
  padding: 6px 8px;
}
.att-num { text-align: center; color: var(--hint); font-weight: 600; font-variant-numeric: tabular-nums; }
.att-name { min-width: 0; overflow-wrap: anywhere; line-height: 1.25; }
.att-name .small { display: block; }
.att-status {
  width: 56px; height: 48px;
  font-size: 24px;
  border-radius: 10px;
  border: 0;
  background: var(--bg2);
  cursor: pointer;
  touch-action: manipulation;
}
.att-status.present { background: rgba(46, 158, 79, .16); }
.att-status.absent { background: rgba(229, 57, 53, .16); }
.att-status.late { background: rgba(214, 138, 0, .18); }
.att-status.none { outline: 2px dashed var(--border); }
.att-eye {
  width: 40px; height: 44px;
  border: 0; background: transparent; font-size: 20px; cursor: pointer;
}
.date-row { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.date-row input { flex: 1; }

/* --- O'quvchilar ro'yxati --- */
.student-row { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.student-row:last-child { border-bottom: 0; }
.student-row .num { width: 24px; text-align: center; color: var(--hint); font-weight: 600; }
.student-row .name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.student-row.clickable { cursor: pointer; }
.order-btns { display: flex; gap: 4px; }
.order-btns .btn { min-width: var(--tap); padding: 6px; font-size: 18px; }

/* --- Statistika --- */
.profile-head { display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; margin-bottom: 12px; }
.profile-head h1 { text-transform: uppercase; }
.stats { display: grid; grid-template-columns: 1fr; gap: 2px; }
.stat-row { display: flex; justify-content: space-between; padding: 7px 0; border-bottom: 1px solid var(--border); }
.stat-row:last-child { border-bottom: 0; }
.history-list { display: flex; flex-direction: column; }
.history-item { display: flex; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-variant-numeric: tabular-nums; }
.history-item:last-child { border-bottom: 0; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; }
.kpi { background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 10px; }
.kpi .value { font-size: 22px; font-weight: 700; }
.kpi .label { font-size: 13px; color: var(--hint); }

.segmented { display: flex; background: var(--bg2); border-radius: 10px; padding: 3px; margin-bottom: 12px; }
.segmented button {
  flex: 1; min-height: 38px; border: 0; background: transparent; color: var(--text);
  border-radius: 8px; font: inherit; font-size: 14px; cursor: pointer;
}
.segmented button.active { background: var(--bg); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.12); }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.report { width: 100%; border-collapse: collapse; font-size: 14px; font-variant-numeric: tabular-nums; }
table.report th, table.report td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; }
table.report th:first-child, table.report td:first-child { text-align: left; white-space: normal; }
table.report th { color: var(--hint); font-weight: 600; font-size: 12px; }

.invite-link {
  word-break: break-all;
  background: var(--bg2);
  border-radius: 10px;
  padding: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  user-select: all;
}

/* --- Holatlar --- */
.skeleton { display: flex; flex-direction: column; gap: 10px; padding: 8px 0; }
.skeleton-row {
  height: 58px; border-radius: 10px;
  background: linear-gradient(90deg, var(--bg2) 25%, var(--section-bg) 50%, var(--bg2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.2s infinite linear;
}
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .skeleton-row { animation: none; } }

.empty, .error-state { text-align: center; padding: 28px 12px; color: var(--hint); }
.error-state p { color: var(--text); }
.error-state .btn { margin-top: 10px; }

.spinner {
  width: 28px; height: 28px; margin: 0 auto 12px;
  border: 3px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed;
  left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: #1f2937;
  color: #fff;
  font-size: 14px;
  z-index: 60;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .2);
}
body.has-fallback-main .toast { bottom: 96px; }
.toast-error { background: #b3261e; }

@media (min-width: 600px) {
  .app { padding: 20px 20px 40px; }
  .weekdays { grid-template-columns: repeat(4, 1fr); }
  .stats { grid-template-columns: 1fr 1fr; column-gap: 24px; }
}
