:root {
  color-scheme: light dark;
  --bg: var(--tg-theme-bg-color, #f4f6f8);
  --panel: var(--tg-theme-secondary-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #111827);
  --muted: var(--tg-theme-hint-color, #6b7280);
  --link: var(--tg-theme-link-color, #2271d1);
  --button: var(--tg-theme-button-color, #2271d1);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --line: rgba(116, 116, 128, 0.18);
  --danger: #c2410c;
  --success: #0f8f5f;
  --shadow: 0 20px 50px rgba(15, 23, 42, 0.18);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  min-height: 100vh;
  padding: max(14px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(18px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}

.topbar,
.monthbar,
.day-head,
.sheet-head,
.form-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar {
  max-width: 1180px;
  margin: 0 auto 14px;
}

.app-title {
  font-size: 22px;
  font-weight: 750;
}

.app-subtitle,
.day-meta {
  color: var(--muted);
  font-size: 13px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(340px, 1fr) minmax(320px, 0.9fr);
  gap: 14px;
  max-width: 1180px;
  margin: 0 auto;
  align-items: start;
}

.calendar-pane,
.agenda-pane,
.inspector,
.sheet {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.calendar-pane,
.agenda-pane,
.inspector {
  padding: 14px;
}

.month-title,
.day-title,
.sheet-title {
  font-size: 18px;
  font-weight: 700;
}

.icon-button {
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  background: transparent;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  line-height: 1;
}

.primary-button,
.secondary-button,
.danger-button,
.success-button {
  min-height: 38px;
  border: 0;
  border-radius: 8px;
  padding: 0 14px;
  font-weight: 650;
}

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

.secondary-button {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--text);
}

.danger-button {
  background: rgba(194, 65, 12, 0.12);
  color: var(--danger);
}

.success-button {
  background: rgba(15, 143, 95, 0.13);
  color: var(--success);
}

.filters,
.type-switch {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 4px;
  margin: 14px 0;
  background: rgba(116, 116, 128, 0.12);
  border-radius: 8px;
}

.type-switch {
  grid-template-columns: repeat(3, 1fr);
  margin-top: 0;
}

.filter,
.type-option {
  min-width: 0;
  min-height: 34px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  font-weight: 650;
}

.filter.active,
.type-option.active {
  background: var(--panel);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.weekdays,
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 6px;
}

.weekdays {
  color: var(--muted);
  font-size: 12px;
  text-align: center;
  margin-bottom: 6px;
}

.day-cell {
  position: relative;
  min-height: 76px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  padding: 7px;
  text-align: left;
  overflow: hidden;
}

.day-cell.other {
  opacity: 0.38;
}

.day-cell.selected {
  border-color: var(--button);
  box-shadow: inset 0 0 0 1px var(--button);
}

.day-number {
  display: inline-flex;
  min-width: 24px;
  height: 24px;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  font-weight: 700;
}

.day-cell.today .day-number {
  background: var(--button);
  color: var(--button-text);
}

.day-counts {
  display: flex;
  gap: 3px;
  margin-top: 10px;
}

.dot {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--muted);
}

.dot.event {
  background: #2563eb;
}

.dot.task {
  background: #0f8f5f;
}

.dot.note {
  background: #9333ea;
}

.agenda-list {
  display: grid;
  gap: 8px;
  margin-top: 14px;
}

.agenda-item,
.inspector-row {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  padding: 10px;
}

.agenda-item {
  width: 100%;
  text-align: left;
  color: var(--text);
}

.agenda-item.active {
  border-color: var(--button);
}

.item-line {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: baseline;
}

.item-title {
  min-width: 0;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.item-time,
.item-note,
.empty-state {
  color: var(--muted);
  font-size: 13px;
}

.inspector {
  display: grid;
  gap: 10px;
}

.inspector-title {
  font-size: 18px;
  font-weight: 750;
  overflow-wrap: anywhere;
}

.inspector-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.28);
  z-index: 10;
}

.sheet {
  position: fixed;
  z-index: 11;
  left: 50%;
  bottom: max(14px, env(safe-area-inset-bottom));
  width: min(720px, calc(100vw - 20px));
  max-height: min(90vh, 860px);
  transform: translateX(-50%);
  box-shadow: var(--shadow);
  overflow: auto;
}

.item-form {
  display: grid;
  gap: 12px;
  padding: 14px;
}

.field,
.check-row {
  display: grid;
  gap: 6px;
}

.field span,
.field-caption {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.field input,
.field textarea,
.field select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  background: var(--bg);
  color: var(--text);
}

.field textarea {
  resize: vertical;
}

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

.check-row {
  grid-template-columns: 20px 1fr;
  align-items: center;
  color: var(--text);
}

.check-row input {
  width: 18px;
  height: 18px;
}

.alert-box {
  display: grid;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}

.custom-alert {
  margin-top: 4px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: min(440px, calc(100vw - 28px));
  background: #111827;
  color: white;
  border-radius: 8px;
  padding: 10px 14px;
  z-index: 20;
  box-shadow: var(--shadow);
}

[hidden] {
  display: none !important;
}

.locked-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
}

.locked-panel {
  width: min(420px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  padding: 20px;
  text-align: center;
}

.locked-title {
  font-size: 20px;
  font-weight: 750;
  margin-bottom: 8px;
}

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

@media (max-width: 860px) {
  .app-shell {
    padding-left: 10px;
    padding-right: 10px;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .calendar-pane,
  .agenda-pane,
  .inspector {
    padding: 12px;
  }

  .day-cell {
    min-height: 54px;
    padding: 5px;
  }

  .day-counts {
    margin-top: 6px;
  }

  .filters {
    grid-template-columns: repeat(2, 1fr);
  }

  .field-grid {
    grid-template-columns: 1fr;
  }
}
