:root {
  --fallback-bg: #ffffff;
  --fallback-secondary: #f2f3f5;
  --fallback-text: #101114;
  --fallback-hint: #8a8f98;
  --fallback-link: #2481cc;
  --fallback-button: #2481cc;
  --fallback-button-text: #ffffff;
  --fallback-separator: rgba(16, 17, 20, 0.08);
  --fallback-destructive: #e5484d;

  --bg: var(--tg-theme-bg-color, var(--fallback-bg));
  --secondary: var(--tg-theme-secondary-bg-color, var(--fallback-secondary));
  --text: var(--tg-theme-text-color, var(--fallback-text));
  --hint: var(--tg-theme-hint-color, var(--fallback-hint));
  --link: var(--tg-theme-link-color, var(--fallback-link));
  --button: var(--tg-theme-button-color, var(--fallback-button));
  --button-text: var(--tg-theme-button-text-color, var(--fallback-button-text));
  --separator: var(--tg-theme-section-separator-color, var(--fallback-separator));
  --destructive: var(--tg-theme-destructive-text-color, var(--fallback-destructive));

  --card: var(--secondary);
  --radius: 16px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.07);
}

@media (prefers-color-scheme: dark) {
  :root {
    --fallback-bg: #0f1115;
    --fallback-secondary: #191c22;
    --fallback-text: #f2f3f5;
    --fallback-hint: #8d939e;
    --fallback-link: #6ab3f3;
    --fallback-button: #4c9fe0;
    --fallback-button-text: #ffffff;
    --fallback-separator: rgba(255, 255, 255, 0.09);
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

body {
  height: var(--tg-viewport-stable-height, 100dvh);
}

button,
input {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  border: 0;
  background: none;
}

a {
  color: var(--link);
  text-decoration: none;
}

h1, h2, h3, p {
  margin: 0;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 640px;
  margin: 0 auto;
  position: relative;
}

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px 8px;
  flex: 0 0 auto;
}

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 19px;
  color: var(--button-text);
  background: linear-gradient(140deg, var(--button), color-mix(in srgb, var(--button) 65%, #7b61ff));
  box-shadow: 0 6px 16px color-mix(in srgb, var(--button) 30%, transparent);
  flex: 0 0 auto;
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.brand-title {
  font-weight: 700;
  font-size: 17px;
  letter-spacing: -0.2px;
}

.target-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px 3px 0;
  margin-top: 1px;
  color: var(--hint);
  font-size: 13px;
  font-weight: 500;
  max-width: 100%;
  text-align: left;
}

.target-chip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.target-chip:active {
  opacity: 0.6;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  color: var(--text);
  background: var(--secondary);
  flex: 0 0 auto;
  transition: transform 0.15s ease, opacity 0.15s ease;
}

.icon-btn:active {
  transform: scale(0.94);
  opacity: 0.75;
}

.icon-btn.spinning svg {
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------- screens ---------- */

.screens {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 4px 16px calc(82px + var(--safe-bottom));
}

.screen[hidden] {
  display: none;
}

/* ---------- week nav ---------- */

.weekbar {
  display: grid;
  grid-template-columns: 40px 1fr 40px auto;
  align-items: center;
  gap: 6px;
  background: var(--secondary);
  border-radius: 14px;
  padding: 7px;
  margin-top: 6px;
}

.nav-arrow {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  color: var(--text);
  background: color-mix(in srgb, var(--bg) 70%, transparent);
}

.nav-arrow:active {
  opacity: 0.6;
}

.week-caption {
  text-align: center;
  min-width: 0;
}

.week-caption b {
  display: block;
  font-size: 14.5px;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.week-caption span {
  font-size: 11.5px;
  color: var(--hint);
}

.today-pill {
  height: 34px;
  padding: 0 11px;
  border-radius: 10px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--button);
  background: color-mix(in srgb, var(--button) 12%, transparent);
}

.today-pill:active {
  opacity: 0.7;
}

/* ---------- day strip ---------- */

.daystrip {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 5px;
  margin: 12px 0 10px;
}

.day-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 2px 7px;
  border-radius: 13px;
  color: var(--hint);
  transition: background 0.15s ease, color 0.15s ease;
}

.day-btn .dow {
  font-size: 10.5px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.day-btn .dnum {
  font-size: 16.5px;
  font-weight: 650;
  color: var(--text);
  line-height: 1.2;
}

.day-btn .dot {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--button);
  opacity: 0;
  margin-top: 1px;
}

.day-btn.today .dot {
  opacity: 1;
}

.day-btn.active {
  background: var(--button);
  color: var(--button-text);
  box-shadow: 0 6px 16px color-mix(in srgb, var(--button) 28%, transparent);
}

.day-btn.active .dnum,
.day-btn.active .dow {
  color: var(--button-text);
}

.day-btn.active .dot {
  background: var(--button-text);
  opacity: 1;
}

/* ---------- view switch ---------- */

.view-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 4px 0 12px;
}

.segmented {
  display: inline-flex;
  background: var(--secondary);
  padding: 3px;
  border-radius: 11px;
  gap: 2px;
}

.segmented.wide {
  display: flex;
  width: 100%;
  margin-bottom: 12px;
}

.segmented button {
  padding: 6px 14px;
  border-radius: 9px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--hint);
  transition: background 0.15s ease, color 0.15s ease;
  flex: 1;
}

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

.updated {
  font-size: 11px;
  color: var(--hint);
}

/* ---------- content / lessons ---------- */

.content {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 120px;
}

.day-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin: 14px 2px 2px;
}

.day-heading:first-child {
  margin-top: 0;
}

.day-heading b {
  font-size: 13.5px;
  font-weight: 650;
}

.day-heading span {
  font-size: 11.5px;
  color: var(--hint);
}

.lesson {
  display: grid;
  grid-template-columns: 64px 1fr;
  gap: 11px;
  background: var(--secondary);
  border-radius: var(--radius);
  padding: 12px;
  position: relative;
  overflow: hidden;
  animation: rise 0.22s ease both;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(5px); }
  to { opacity: 1; transform: none; }
}

.lesson.current {
  outline: 2px solid var(--button);
  outline-offset: -2px;
}

.lesson.current::before {
  content: "";
  position: absolute;
  inset: 0 auto 0 0;
  width: 3px;
  background: var(--button);
}

.lesson-time {
  display: flex;
  flex-direction: column;
  gap: 1px;
  border-right: 1px solid var(--separator);
  padding-right: 9px;
}

.lesson-time .start {
  font-size: 14.5px;
  font-weight: 650;
  letter-spacing: -0.2px;
}

.lesson-time .end {
  font-size: 11.5px;
  color: var(--hint);
}

.lesson-time .num {
  margin-top: auto;
  font-size: 10.5px;
  color: var(--hint);
}

.lesson-main {
  min-width: 0;
}

.lesson-subject {
  font-size: 14.5px;
  font-weight: 600;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.badges {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 5px;
}

.badge {
  font-size: 10.5px;
  font-weight: 600;
  padding: 2.5px 7px;
  border-radius: 7px;
  color: var(--button);
  background: color-mix(in srgb, var(--button) 12%, transparent);
  white-space: nowrap;
}

.badge.neutral {
  color: var(--hint);
  background: color-mix(in srgb, var(--hint) 12%, transparent);
}

.lesson-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 3px 10px;
  margin-top: 6px;
  font-size: 12px;
  color: var(--hint);
}

.lesson-meta span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  overflow-wrap: anywhere;
}

.lesson-meta .cab {
  font-weight: 600;
  color: var(--text);
  opacity: 0.85;
}

.lesson-meta svg {
  flex: 0 0 auto;
  opacity: 0.75;
}

/* ---------- empty / skeleton ---------- */

.empty {
  text-align: center;
  padding: 38px 16px;
  color: var(--hint);
}

.empty .emoji {
  font-size: 34px;
  display: block;
  margin-bottom: 8px;
}

.empty b {
  display: block;
  color: var(--text);
  font-size: 15px;
  margin-bottom: 3px;
}

.empty p {
  font-size: 12.5px;
  max-width: 280px;
  margin: 0 auto;
}

.skeleton {
  background: var(--secondary);
  border-radius: var(--radius);
  height: 84px;
  position: relative;
  overflow: hidden;
}

.skeleton::after {
  content: "";
  position: absolute;
  inset: 0;
  transform: translateX(-100%);
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--bg) 55%, transparent), transparent);
  animation: shimmer 1.2s infinite;
}

@keyframes shimmer {
  to { transform: translateX(100%); }
}

/* ---------- changes ---------- */

.screen-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin: 10px 2px 14px;
}

.screen-head h2 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

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

.small {
  font-size: 12px;
}

.ghost-btn {
  padding: 7px 12px;
  border-radius: 10px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--button);
  background: color-mix(in srgb, var(--button) 10%, transparent);
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
}

.ghost-btn:active {
  opacity: 0.65;
}

.notice {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-radius: 14px;
  font-size: 12.5px;
  font-weight: 500;
  background: color-mix(in srgb, #34c759 12%, transparent);
  color: #248a3d;
}

@media (prefers-color-scheme: dark) {
  .notice {
    color: #6fdd8b;
  }
}

.notice.warn {
  background: color-mix(in srgb, #ff9f0a 14%, transparent);
  color: #a05c00;
}

@media (prefers-color-scheme: dark) {
  .notice.warn {
    color: #ffc061;
  }
}

.notice .big {
  font-size: 20px;
}

.lesson.changed {
  outline: 1.5px dashed color-mix(in srgb, #ff9f0a 75%, transparent);
  outline-offset: -1.5px;
}

/* ---------- settings ---------- */

.card {
  background: var(--secondary);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
}

.card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.card h3 {
  font-size: 14.5px;
  font-weight: 650;
}

.card p {
  margin-top: 3px;
}

.link-btn {
  color: var(--button);
  font-size: 12.5px;
  font-weight: 600;
  padding: 4px 0;
  flex: 0 0 auto;
}

.target-view {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 11px;
  padding: 10px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--bg) 60%, transparent);
}

.target-view .avatar {
  width: 38px;
  height: 38px;
  border-radius: 11px;
  display: grid;
  place-items: center;
  font-size: 19px;
  background: color-mix(in srgb, var(--button) 14%, transparent);
  flex: 0 0 auto;
}

.target-view .name {
  font-weight: 600;
  font-size: 14px;
  overflow-wrap: anywhere;
}

.target-view .role {
  font-size: 11.5px;
  color: var(--hint);
}

.switch {
  position: relative;
  flex: 0 0 auto;
  display: block;
}

.switch input {
  position: absolute;
  opacity: 0;
  width: 1px;
  height: 1px;
}

.switch-track {
  display: block;
  width: 46px;
  height: 28px;
  border-radius: 20px;
  background: color-mix(in srgb, var(--hint) 40%, transparent);
  transition: background 0.18s ease;
  position: relative;
}

.switch-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
  transition: transform 0.18s ease;
}

.switch input:checked + .switch-track {
  background: #34c759;
}

.switch input:checked + .switch-track .switch-thumb {
  transform: translateX(18px);
}

.reminder-controls {
  margin-top: 13px;
  border-top: 1px solid var(--separator);
  padding-top: 12px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
}

.chips:empty {
  display: none;
}

.chip {
  padding: 6px 11px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  background: color-mix(in srgb, var(--bg) 65%, transparent);
  white-space: nowrap;
}

.chip.active {
  color: var(--button-text);
  background: var(--button);
}

.chip:active {
  opacity: 0.7;
}

.time-row {
  display: flex;
  gap: 8px;
}

.time-row input[type="time"] {
  flex: 1;
  min-width: 0;
  padding: 9px 11px;
  border-radius: 11px;
  border: 1px solid var(--separator);
  background: color-mix(in srgb, var(--bg) 65%, transparent);
  color: var(--text);
  font-size: 14px;
}

.primary-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 12px 16px;
  border-radius: 13px;
  background: var(--button);
  color: var(--button-text);
  font-weight: 650;
  font-size: 14px;
  text-align: center;
}

.primary-btn.compact {
  padding: 9px 16px;
  font-size: 13px;
  flex: 0 0 auto;
}

.primary-btn:active {
  opacity: 0.8;
}

.about p {
  margin-top: 6px;
}

/* ---------- tabbar ---------- */

.tabbar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  gap: 4px;
  padding: 6px 10px calc(6px + var(--safe-bottom));
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-top: 1px solid var(--separator);
  z-index: 20;
}

.tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 10px;
  border-radius: 11px;
  color: var(--hint);
  font-size: 10px;
  font-weight: 600;
  position: relative;
  min-width: 76px;
  transition: color 0.15s ease;
}

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

.tab:active {
  opacity: 0.6;
}

.tab-dot {
  position: absolute;
  top: 5px;
  right: 22px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #ff3b30;
  border: 1.5px solid var(--bg);
}

/* ---------- gate ---------- */

.gate {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
  z-index: 100;
}

.gate[hidden] {
  display: none;
}

.gate-card {
  max-width: 340px;
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.gate-icon {
  width: 64px;
  height: 64px;
  border-radius: 20px;
  display: grid;
  place-items: center;
  font-size: 30px;
  background: var(--secondary);
  margin-bottom: 4px;
}

.gate-card h1 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.gate-card p {
  color: var(--hint);
  font-size: 13.5px;
  max-width: 290px;
}

.gate-card .primary-btn {
  width: 100%;
  margin-top: 8px;
}

.gate-card .primary-btn[hidden],
.gate-card .ghost-btn[hidden] {
  display: none;
}

/* ---------- target sheet ---------- */

.sheet {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.sheet[hidden] {
  display: none;
}

.sheet-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.42);
  animation: fade 0.2s ease both;
}

@keyframes fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

.sheet-panel {
  position: relative;
  width: 100%;
  max-width: 640px;
  max-height: 88%;
  background: var(--bg);
  border-radius: 22px 22px 0 0;
  padding: 8px 16px calc(16px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  animation: slideUp 0.26s cubic-bezier(0.2, 0.9, 0.3, 1) both;
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.18);
}

@keyframes slideUp {
  from { transform: translateY(30px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.sheet-handle {
  width: 38px;
  height: 4px;
  border-radius: 3px;
  background: var(--separator);
  margin: 4px auto 10px;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.sheet-head h2 {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.2px;
}

.sheet-head .icon-btn {
  background: var(--secondary);
}

.search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  border-radius: 12px;
  background: var(--secondary);
  margin-bottom: 10px;
  color: var(--hint);
}

.search-wrap input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  padding: 11px 0;
  font-size: 14px;
  outline: none;
}

.search-wrap:focus-within {
  box-shadow: 0 0 0 1.5px var(--button);
}

.target-list {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding-bottom: 6px;
  min-height: 120px;
}

.target-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 12px;
  border-radius: 12px;
  background: var(--secondary);
  text-align: left;
  font-size: 14px;
  font-weight: 500;
}

.target-item:active {
  opacity: 0.7;
}

.target-item .sub {
  font-size: 11px;
  color: var(--hint);
  font-weight: 500;
}

.target-item .check {
  color: var(--button);
  font-weight: 700;
  flex: 0 0 auto;
}

.target-empty {
  text-align: center;
  color: var(--hint);
  padding: 24px 0;
  font-size: 13px;
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(88px + var(--safe-bottom));
  transform: translate(-50%, 12px);
  background: rgba(20, 22, 26, 0.92);
  color: #fff;
  font-size: 12.5px;
  font-weight: 500;
  padding: 9px 14px;
  border-radius: 11px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 200;
  max-width: 85vw;
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (min-width: 640px) {
  .screens {
    padding-left: 24px;
    padding-right: 24px;
  }
  .topbar {
    padding-left: 24px;
    padding-right: 24px;
  }
}
