/* Mini App styles (spec §5, §5.9). Mobile-first, no desktop layouts.
   `--app-*` are the school's own colours: fills, borders, stripes and icons only. Their values mirror
   `js/palette.js` (checked by tests/js/palette-css.test.mjs). All text uses Telegram theme colours. */

:root,
[data-scheme='light'] {
  color-scheme: light;
  --app-brand: #6A4A9C;
  --app-on-brand: #FFFFFF;
  --app-type-individual: #2E7D32;
  --app-type-spec-english: #B52E72;
  --app-type-pair: #087E80;
  --app-type-group: #BF360C;
  --app-success: #2E7D32;
  --app-danger: #DC3545;
  --app-on-danger: #FFFFFF;

  /* Used only when Telegram gives no theme variable: an old client or the dev stub mode. */
  --ui-fallback-surface: #FFFFFF;
  --ui-fallback-text: #000000;
  --ui-fallback-hint: #6D6D72;
  --ui-fallback-link: #2481CC;
  --ui-fallback-separator: #C8C7CC;
}

[data-scheme='dark'] {
  color-scheme: dark;
  --app-brand: #9B78D4;
  --app-on-brand: #1A1F2E;
  --app-type-individual: #81C784;
  --app-type-spec-english: #F58BC0;
  --app-type-pair: #72D9D1;
  --app-type-group: #FFB74D;
  --app-success: #81C784;
  --app-danger: #EF6B78;
  --app-on-danger: #1A1F2E;

  --ui-fallback-surface: #212121;
  --ui-fallback-text: #FFFFFF;
  --ui-fallback-hint: #AAAAAA;
  --ui-fallback-link: #6AB3F3;
  --ui-fallback-separator: #3A3A3C;
}

/* Telegram surfaces and text with fallbacks. */
:root {
  --ui-page: var(--tg-theme-secondary-bg-color, var(--tg-theme-bg-color, var(--ui-fallback-surface)));
  --ui-card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, var(--ui-fallback-surface)));
  --ui-separator: var(--tg-theme-section-separator-color, var(--ui-fallback-separator));
  --ui-text: var(--tg-theme-text-color, var(--ui-fallback-text));
  --ui-hint: var(--tg-theme-hint-color, var(--ui-fallback-hint));
  --ui-link: var(--tg-theme-link-color, var(--ui-fallback-link));
  --ui-destructive: var(--tg-theme-destructive-text-color, var(--app-danger));

  --ui-gap: 16px;
  --ui-radius: 12px;
  --ui-tap: 44px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  /* No zoom on a double tap, for example on the ±5 buttons. Pinch zoom and scrolling stay. */
  touch-action: manipulation;
}

body {
  display: flex;
  flex-direction: column;
  min-height: var(--tg-viewport-stable-height, 100dvh);
  margin: 0;
  background: var(--ui-page);
  color: var(--ui-text);
  font: 16px/1.4 -apple-system, system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overflow-wrap: anywhere;
}

button {
  margin: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid var(--app-brand);
  outline-offset: 2px;
}

.app {
  display: flex;
  flex: 1;
  flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-right: env(safe-area-inset-right);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
}

/* --- Tabs ------------------------------------------------------------------ */

.tabs {
  display: flex;
  padding: 0 var(--ui-gap);
  border-bottom: 1px solid var(--ui-separator);
}

.tab {
  flex: 1;
  min-height: var(--ui-tap);
  padding: 8px;
  border-bottom: 3px solid transparent;
  color: var(--ui-hint);
  font-weight: 500;
}

.tab[aria-current='page'] {
  border-bottom-color: var(--app-brand);
  color: var(--ui-text);
  font-weight: 600;
}

/* --- Screen ---------------------------------------------------------------- */

.screen {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 12px;
  padding: var(--ui-gap);
}

.screen-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  line-height: 1.25;
}

.hint {
  margin: 0;
  color: var(--ui-hint);
  font-size: 14px;
}

.error-text {
  margin: 0;
  color: var(--ui-destructive);
  font-size: 14px;
}

/* Text action such as «Перенести ›». */
.action {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: var(--ui-tap);
  min-height: var(--ui-tap);
  color: var(--ui-link);
  font-weight: 500;
}

/* --- Lesson type colour ---------------------------------------------------- */
/* Sets `--type-color` for a card stripe and a type chip. Without a modifier (lesson_type is null) it is neutral. */

.card,
.type-chip {
  --type-color: var(--ui-hint);
}

.type-individual {
  --type-color: var(--app-type-individual);
}

.type-spec-english {
  --type-color: var(--app-type-spec-english);
}

.type-pair {
  --type-color: var(--app-type-pair);
}

.type-group {
  --type-color: var(--app-type-group);
}

/* --- Card ------------------------------------------------------------------ */

.card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: flex-start;
  padding: 12px var(--ui-gap) 12px calc(var(--ui-gap) + 4px);
  overflow: hidden;
  border: 1px solid var(--ui-separator);
  border-radius: var(--ui-radius);
  background: var(--ui-card);
}

.card::before {
  position: absolute;
  inset: 0 auto 0 0;
  width: 4px;
  background: var(--type-color);
  content: '';
}

.card-title {
  margin: 0;
  font-weight: 600;
}

.card--tappable {
  cursor: pointer;
}

/* A lesson that cannot be rescheduled: the date and the type are dimmed, the reason keeps its full contrast. */
.card--inactive {
  border-style: dashed;
  background: none;
}

.card--inactive::before,
.card--inactive .card-title,
.card--inactive .card-row {
  opacity: 0.5;
}

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

.card-text {
  margin: 0;
}

.comment {
  white-space: pre-wrap;
}

/* The title of a request row is its control: a button that looks like the title. */
.card-link {
  padding: 0;
  text-align: left;
}

.card .action {
  align-self: flex-end;
  margin: -6px 0 -10px;
}

/* «Зараз / Буде». */
.details {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  margin: 0;
}

.details dt {
  color: var(--ui-hint);
}

.details dd {
  margin: 0;
  font-weight: 600;
}

/* --- Lists ----------------------------------------------------------------- */

.list,
.list-footer,
.error-block {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.error-block {
  align-items: flex-start;
}

.empty {
  margin: auto;
  padding: 24px 8px;
  color: var(--ui-hint);
  text-align: center;
}

/* One-off message above a screen, for example «Запит не знайдено». */
.notice {
  margin: var(--ui-gap) var(--ui-gap) 0;
  color: var(--ui-destructive);
  font-size: 14px;
}

/* Secondary button inside the content: «Спробувати ще раз», «Показати ще». */
.button {
  min-height: var(--ui-tap);
  padding: 8px 16px;
  border: 1px solid var(--ui-separator);
  border-radius: 10px;
  background: var(--ui-card);
  color: var(--ui-link);
  font-weight: 500;
}

.button:disabled {
  cursor: progress;
  opacity: 0.6;
}

/* --- Type chip and status badge -------------------------------------------- */

.type-chip,
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  color: var(--ui-text);
  font-size: 13px;
  line-height: 1.4;
}

.type-chip {
  border: 1px solid var(--type-color);
  background: color-mix(in srgb, var(--type-color) 13%, var(--ui-card));
}

.type-chip .fas {
  color: var(--type-color);
}

.badge {
  --status-color: var(--ui-hint);
  background: color-mix(in srgb, var(--status-color) 13%, var(--ui-card));
}

.badge .fas {
  color: var(--status-color);
}

.badge--pending {
  --status-color: var(--app-brand);
}

.badge--approved {
  --status-color: var(--app-success);
}

.badge--rejected {
  --status-color: var(--app-danger);
}

/* --- Day choice: calendar (spec §5.3, §5.9) -------------------------------- */

.screen-body {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 12px;
  align-items: flex-start;
}

.screen-body > .calendar,
.screen-body > .skeleton-list,
.screen-body > .empty {
  align-self: stretch;
}

.calendar {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.calendar-month {
  margin: 8px 0 0;
  font-size: 16px;
  font-weight: 600;
}

.calendar-week {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  justify-items: center;
}

.calendar-weekdays {
  color: var(--ui-hint);
  font-size: 13px;
}

.day {
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--ui-tap);
  max-width: 100%;
  height: var(--ui-tap);
  border-radius: 10px;
  font-variant-numeric: tabular-nums;
}

.day--off {
  color: var(--ui-hint);
}

.day--on {
  border: 1px solid var(--app-brand);
  background: var(--ui-card);
  color: var(--ui-text);
  font-weight: 600;
}

.day--selected {
  background: var(--app-brand);
  color: var(--app-on-brand);
}

/* --- Time choice: chips, stepper, preview scale (spec §5.4, §5.9) ----------- */

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

/* Four time chips fill a row from edge to edge; an incomplete last row is centred. */
.chips:not(.chips--hours) > .chip {
  flex: 0 0 calc((100% - 3 * 8px) / 4);
  min-width: 0;
  padding-right: 4px;
  padding-left: 4px;
}

/* Hours of a wide window: one row with horizontal scrolling. */
.chips--hours {
  flex-wrap: nowrap;
  justify-content: flex-start;
  margin: 0 calc(-1 * var(--ui-gap));
  padding: 0 var(--ui-gap);
  overflow-x: auto;
  scrollbar-width: none;
}

/* Centred while the hours fit; when they do not, the row starts at the left edge and scrolls. */
.chips--hours > :first-child {
  margin-left: auto;
}

.chips--hours > :last-child {
  margin-right: auto;
}

.chips--hours::-webkit-scrollbar {
  display: none;
}

.chip {
  flex: none;
  min-width: 64px;
  min-height: var(--ui-tap);
  padding: 8px 12px;
  border: 1px solid var(--ui-separator);
  border-radius: 10px;
  background: var(--ui-card);
  color: var(--ui-text);
  font-variant-numeric: tabular-nums;
}

.chips--hours .chip {
  min-width: var(--ui-tap);
}

/* The hour whose minutes are shown: marked, but it is not the chosen time. */
.chip--current {
  border-color: var(--app-brand);
  background: color-mix(in srgb, var(--app-brand) 13%, var(--ui-card));
  font-weight: 600;
}

.chip--selected {
  border-color: var(--app-brand);
  background: var(--app-brand);
  color: var(--app-on-brand);
  font-weight: 600;
}

/* Caption of a group of controls: «Година», «Початок заняття», «Уточнити час». */
.field-label {
  margin: 4px 0 -4px;
  color: var(--ui-hint);
  font-size: 14px;
}

.adjust {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.stepper {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: center;
}

.stepper-button {
  min-width: 72px;
  min-height: var(--ui-tap);
  padding: 8px 12px;
  border: 1px solid var(--ui-separator);
  border-radius: 10px;
  background: var(--ui-card);
  color: var(--ui-link);
  font-weight: 500;
}

.stepper-button:disabled {
  color: var(--ui-hint);
  cursor: default;
  opacity: 0.5;
}

.stepper-value {
  min-width: 72px;
  font-size: 22px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.timeline-ticks {
  position: relative;
  height: 18px;
  color: var(--ui-hint);
  font-size: 12px;
}

.timeline-tick {
  position: absolute;
  transform: translateX(-50%);
  /* A tick at the right edge has no room left of its own: without this its digits wrap. */
  white-space: nowrap;
}

.timeline-track {
  position: relative;
  height: 28px;
  overflow: hidden;
  border-radius: 6px;
  background: var(--ui-card);
}

.timeline-free,
.timeline-selected,
.timeline-original {
  position: absolute;
  top: 0;
  bottom: 0;
  border-radius: 6px;
}

.timeline-free {
  background: color-mix(in srgb, var(--app-brand) 18%, var(--ui-card));
}

.timeline-selected {
  background: var(--app-brand);
}

.timeline-original {
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border: 1px dashed var(--ui-hint);
  color: var(--ui-hint);
  font-size: 11px;
  white-space: nowrap;
}

/* A card without the lesson type stripe. */
.card--plain {
  padding-left: var(--ui-gap);
}

.card--plain::before {
  content: none;
}

/* --- Service screen -------------------------------------------------------- */

.service {
  max-width: 320px;
  margin: auto;
  padding: 24px 8px;
  text-align: center;
}

/* --- Skeleton -------------------------------------------------------------- */

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

.skeleton {
  height: 76px;
  border-radius: var(--ui-radius);
  background: color-mix(in srgb, var(--ui-hint) 16%, var(--ui-card));
  animation: skeleton-pulse 1s ease-in-out infinite alternate;
}

@keyframes skeleton-pulse {
  to {
    opacity: 0.45;
  }
}

@media (prefers-reduced-motion: reduce) {
  .skeleton {
    animation: none;
  }
}
