:root {
  --bg:      var(--tg-theme-bg-color, #ffffff);
  --bg2:     var(--tg-theme-secondary-bg-color, #f4f5f7);
  --text:    var(--tg-theme-text-color, #1c1c1e);
  --hint:    var(--tg-theme-hint-color, #8e8e93);
  --btn:     var(--tg-theme-button-color, #007aff);
  --btn-txt: var(--tg-theme-button-text-color, #ffffff);
  --err:     #ff3b30;
  --success: #34c759;
  --warning: #ff9500;
  --r:       14px;
  --r-sm:    10px;
  
  /* Premium Design Variables */
  --border-color: rgba(127, 127, 127, 0.12);
  --card-bg: var(--tg-theme-bg-color, #ffffff);
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.03);
  --shadow-md: 0 6px 18px rgba(0, 0, 0, 0.05);
  --glow-color: rgba(0, 122, 255, 0.12);
}

/* Detect dark mode from telegram background color to tweak gradients and borders */
@media (prefers-color-scheme: dark) {
  :root {
    --border-color: rgba(255, 255, 255, 0.08);
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 6px 18px rgba(0, 0, 0, 0.3);
  }
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

/* Progress bar */
.progress {
  height: 4px;
  background: var(--border-color);
  position: sticky;
  top: 0;
  z-index: 100;
}
.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--btn), #5856d6);
  border-radius: 0 2px 2px 0;
  transition: width .3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Sections */
.header {
  padding: 24px 20px 8px;
}
.header h2 {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.4px;
}
.header p {
  font-size: 13px;
  color: var(--hint);
  margin-top: 4px;
}
.section {
  display: none;
  padding-bottom: 130px;
  animation: fadeIn 0.3s ease;
}
.section.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Groups and Controls */
.group {
  padding: 16px 20px 0;
}
label {
  display: block;
  font-size: 11px;
  color: var(--hint);
  margin-bottom: 6px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
}
.required::after {
  content: ' *';
  color: var(--err);
}

input, select, textarea {
  width: 100%;
  padding: 13px 16px;
  background: var(--bg2);
  border: 1px solid var(--border-color);
  border-radius: var(--r);
  color: var(--text);
  font-size: 15px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}
input[type="date"] {
  -webkit-appearance: auto;
  appearance: auto;
}
input:focus, select:focus {
  border-color: var(--btn);
  background: var(--bg);
  box-shadow: 0 0 0 3px var(--glow-color);
}
input.err, select.err {
  border-color: var(--err);
  box-shadow: 0 0 0 3px rgba(255, 59, 48, 0.12);
}

/* Error Messages with smooth transition */
.err-msg {
  font-size: 12px;
  color: var(--err);
  margin-top: 0;
  max-height: 0;
  opacity: 0;
  overflow: hidden;
  transition: max-height 0.25s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.2s ease, margin-top 0.2s ease;
}
.err-msg.show {
  max-height: 48px;
  opacity: 1;
  margin-top: 6px;
}

.hint {
  font-size: 12px;
  color: var(--hint);
  margin-top: 6px;
  line-height: 1.4;
}

/* Radio buttons & Checkboxes */
.radio-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.radio-row input {
  display: none;
}
.radio-row label {
  padding: 12px 16px;
  border-radius: var(--r);
  background: var(--bg2);
  cursor: pointer;
  color: var(--text);
  font-size: 14px;
  text-transform: none;
  letter-spacing: 0;
  font-weight: 500;
  border: 1px solid var(--border-color);
  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease, transform 0.1s ease;
  box-shadow: var(--shadow-sm);
}
.radio-row label:active {
  transform: scale(0.98);
}
.radio-row.two label {
  flex: 1;
  text-align: center;
}
.radio-row input:checked + label {
  background: var(--btn);
  color: var(--btn-txt);
  border-color: var(--btn);
}
.radio-row input:disabled + label {
  opacity: .5;
  cursor: default;
}
.radio-row.err label {
  border-color: var(--err);
}

/* Premium Radio Cards */
.radio-cards {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 6px;
}
.radio-card {
  position: relative;
}
.radio-card input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}
.radio-card-label {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  background: var(--bg2);
  border: 1px solid var(--border-color);
  border-radius: var(--r);
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease, transform 0.1s ease;
  box-shadow: var(--shadow-sm);
}
.radio-card-label:active {
  transform: scale(0.99);
}
.radio-card-icon {
  font-size: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  background: rgba(0, 122, 255, 0.06);
  border-radius: 12px;
  transition: background 0.2s ease, transform 0.2s ease;
}
.radio-card-content {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
}
.radio-card-title {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  transition: color 0.2s ease;
}
.radio-card-desc {
  font-size: 12px;
  color: var(--hint);
  line-height: 1.35;
}
.radio-card-dot {
  width: 20px;
  height: 20px;
  border: 2px solid var(--border-color);
  border-radius: 50%;
  position: relative;
  transition: border-color 0.2s ease, background-color 0.2s ease;
  flex-shrink: 0;
}
.radio-card-dot::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0);
  width: 10px;
  height: 10px;
  background-color: #ffffff;
  border-radius: 50%;
  transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.2);
}

/* Checked state */
.radio-card input[type="radio"]:checked + .radio-card-label {
  border-color: var(--btn);
  background: var(--bg);
  box-shadow: 0 0 0 3px var(--glow-color), var(--shadow-md);
}
.radio-card input[type="radio"]:checked + .radio-card-label .radio-card-icon {
  background: rgba(0, 122, 255, 0.15);
  transform: scale(1.05);
}
.radio-card input[type="radio"]:checked + .radio-card-label .radio-card-title {
  color: var(--btn);
}
.radio-card input[type="radio"]:checked + .radio-card-label .radio-card-dot {
  border-color: var(--btn);
  background-color: var(--btn);
}
.radio-card input[type="radio"]:checked + .radio-card-label .radio-card-dot::after {
  transform: translate(-50%, -50%) scale(1);
}

/* Disabled state */
.radio-card input[type="radio"]:disabled + .radio-card-label {
  opacity: 0.6;
  cursor: default;
  pointer-events: none;
}

/* Error state */
.radio-cards.err .radio-card-label {
  border-color: var(--err);
}

.divider {
  height: 1px;
  background: var(--border-color);
  margin: 20px 0 0;
}
.sub-label {
  padding: 16px 20px 0;
  font-size: 11px;
  color: var(--hint);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

/* Navigation Bar */
.nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border-color);
  padding: 12px 20px calc(12px + env(safe-area-inset-bottom, 12px));
  display: flex;
  gap: 12px;
  z-index: 90;
}

/* Adjust navigation bar background in dark theme */
@media (prefers-color-scheme: dark) {
  .nav {
    background: rgba(28, 28, 30, 0.85);
  }
}

button {
  font-family: inherit;
}

.btn-back {
  width: 90px;
  padding: 14px;
  background: var(--bg2);
  border: 1px solid var(--border-color);
  border-radius: var(--r);
  color: var(--text);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.1s ease, background 0.15s ease;
}
.btn-back:active {
  transform: scale(0.97);
}

.btn-next {
  flex: 1;
  padding: 14px;
  background: linear-gradient(135deg, var(--btn), #0056b3);
  border: none;
  border-radius: var(--r);
  color: var(--btn-txt);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, opacity 0.15s ease;
  box-shadow: 0 4px 12px rgba(0, 122, 255, 0.2);
}
.btn-next:active {
  transform: scale(0.97);
}
.btn-next:disabled {
  opacity: .5;
  cursor: default;
  box-shadow: none;
}

/* Start screen */
.start {
  padding: 40px 24px 24px;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100svh;
}
.start-logo {
  font-size: 38px;
  width: 72px;
  height: 72px;
  background: linear-gradient(135deg, rgba(36, 129, 204, 0.08), rgba(36, 129, 204, 0.02));
  border: 1px solid rgba(36, 129, 204, 0.15);
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 24px;
  box-shadow: var(--shadow-sm);
}
.start-title {
  font-size: 22px;
  font-weight: 700;
  text-align: center;
  line-height: 1.3;
  margin-bottom: 12px;
  letter-spacing: -0.5px;
}
.start-sub {
  font-size: 14px;
  color: var(--hint);
  text-align: center;
  line-height: 1.5;
  margin: 0 auto 28px;
  max-width: 320px;
}
.start-note {
  font-size: 12px;
  color: var(--hint);
  text-align: center;
  line-height: 1.5;
  margin: 28px auto 0;
  max-width: 320px;
  padding: 12px;
  border-radius: var(--r-sm);
  border: 1px dashed var(--border-color);
  background: rgba(127, 127, 127, 0.02);
}

.btn-start {
  width: 100%;
  padding: 15px;
  background: linear-gradient(135deg, var(--btn), #0056b3);
  border: none;
  border-radius: var(--r);
  color: var(--btn-txt);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 24px;
  transition: transform 0.1s ease;
  box-shadow: 0 4px 12px rgba(0, 122, 255, 0.2);
}
.btn-start:active {
  transform: scale(0.97);
}

/* Applicant lists & cards */
.applicant-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 20px;
}
.applicant-list:empty {
  display: none;
}
.applicant-card {
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--r);
  padding: 16px;
  box-shadow: var(--shadow-sm);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.applicant-card:active {
  transform: scale(0.99);
}
.applicant-card-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 6px;
}
.applicant-card-meta {
  font-size: 13px;
  color: var(--hint);
  line-height: 1.5;
  margin-bottom: 14px;
}
.applicant-actions {
  display: flex;
  gap: 8px;
}
.btn-small {
  flex: 1;
  padding: 10px 12px;
  border: none;
  border-radius: var(--r-sm);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease, border-color 0.15s ease;
}
.btn-small:active {
  transform: scale(0.97);
}
.btn-small.primary {
  background: var(--btn);
  color: var(--btn-txt);
  box-shadow: 0 2px 6px rgba(0, 122, 255, 0.15);
}
.btn-small.danger {
  background: transparent;
  color: var(--err);
  border: 1px solid rgba(255, 59, 48, 0.3);
}
.btn-small.danger:active {
  background: rgba(255, 59, 48, 0.05);
}

/* Periods selection */
.add-period-btn {
  margin-top: 10px;
  width: 100%;
  background: var(--bg2);
  color: var(--btn);
  border: 1px dashed var(--btn);
  padding: 12px;
  font-weight: 600;
  border-radius: var(--r);
  transition: background 0.2s ease, transform 0.1s ease;
}
.add-period-btn:active {
  background: rgba(0, 122, 255, 0.05);
  transform: scale(0.98);
}
.period-row {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
  align-items: center;
  animation: slideDown 0.2s ease;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.period-col {
  flex: 1;
  display: flex;
  flex-direction: column;
}
.period-col label {
  font-size: 9px;
  margin-bottom: 3px;
  padding: 0 4px;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.period-col input {
  padding: 10px 12px;
  border-radius: var(--r-sm);
}
.period-del {
  background: rgba(255, 59, 48, 0.08);
  color: var(--err);
  border: none;
  width: 38px;
  height: 38px;
  border-radius: var(--r-sm);
  font-size: 20px;
  font-weight: 500;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 12px;
  transition: transform 0.1s ease, background 0.15s ease;
}
.period-del:active {
  transform: scale(0.9);
  background: rgba(255, 59, 48, 0.15);
}
.period-placeholder {
  width: 38px;
  margin-top: 12px;
}

/* Queue preview style */
.queue-preview {
  margin-top: 12px;
  background: var(--bg2);
  border: 1px solid var(--border-color);
  border-radius: var(--r);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  transition: background 0.2s ease;
}
.queue-preview summary {
  padding: 12px 16px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  outline: none;
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.queue-preview summary::-webkit-details-marker {
  display: none;
}
.queue-preview summary::after {
  content: '→';
  color: var(--btn);
  font-weight: 700;
  font-size: 14px;
  transition: transform 0.2s ease;
}
.queue-preview[open] summary::after {
  transform: rotate(90deg);
}
.queue-preview-body {
  padding: 0 16px 14px;
  color: var(--hint);
  font-size: 13px;
  line-height: 1.5;
  border-top: 1px solid var(--border-color);
  animation: fadeIn 0.2s ease;
}
.queue-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid rgba(127, 127, 127, 0.08);
  color: var(--text);
  font-size: 14px;
}
.queue-row:last-of-type {
  border-bottom: none;
}
.queue-row span:last-child {
  font-weight: 700;
  color: var(--btn);
}

/* Days Buffer segment & scale selector */
.days-picker {
  display: grid;
  grid-template-columns: 46px 1fr 46px;
  gap: 8px;
  align-items: center;
}
.days-picker input {
  text-align: center;
  font-size: 22px;
  font-weight: 700;
  padding: 9px 12px;
  border: 1px solid var(--border-color);
  background: var(--bg2);
}
.days-step {
  height: 46px;
  border: 1px solid var(--border-color);
  border-radius: var(--r);
  background: var(--bg2);
  color: var(--text);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease;
  box-shadow: var(--shadow-sm);
}
.days-step:active {
  transform: translateY(1px) scale(0.95);
  background: rgba(127, 127, 127, 0.05);
}

.days-scale {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  margin-top: 12px;
  background: var(--bg2);
  padding: 4px;
  border-radius: var(--r);
  border: 1px solid var(--border-color);
}
.days-scale button {
  height: 32px;
  border: none;
  border-radius: calc(var(--r) - 4px);
  background: transparent;
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease, font-weight 0.2s ease, box-shadow 0.2s ease;
}
.days-scale button.active {
  background: var(--btn);
  color: var(--btn-txt);
  font-weight: 700;
  box-shadow: var(--shadow-sm);
}

/* Done and Error screens */
.done {
  padding: 64px 24px;
  text-align: center;
  animation: fadeIn 0.3s ease;
}
.done .icon {
  font-size: 64px;
  width: 96px;
  height: 96px;
  background: rgba(52, 199, 89, 0.1);
  color: var(--success);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 24px;
}
.done h2 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 10px;
}
.done p {
  color: var(--hint);
  line-height: 1.5;
  max-width: 280px;
  margin: 0 auto;
}

.telegram-required {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 80vh;
  padding: 24px;
  text-align: center;
}
.telegram-required-icon {
  font-size: 48px;
  margin-bottom: 16px;
}
.telegram-required h2 {
  margin-bottom: 8px;
  font-weight: 700;
}
.telegram-required p {
  color: var(--hint);
  max-width: 320px;
  line-height: 1.5;
}
.telegram-required a {
  margin-top: 20px;
  display: inline-block;
  padding: 14px 28px;
  background: linear-gradient(135deg, var(--btn), #0056b3);
  color: var(--btn-txt);
  border-radius: var(--r);
  text-decoration: none;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(0, 122, 255, 0.2);
}

@media (min-height: 640px) {
  .btn-start { margin-top: auto; }
}

@media (max-height: 639px) {
  .start { padding-top: 24px; }
  .start-logo { font-size: 32px; width: 60px; height: 60px; margin-bottom: 14px; }
  .start-sub { margin-bottom: 20px; }
  .start-note { margin-top: 20px; }
}

/* Maintenance Cards */
.maintenance-card {
  background: rgba(255, 59, 48, 0.05);
  border-left: 4px solid var(--err);
  border-top: 1px solid rgba(255, 59, 48, 0.1);
  border-right: 1px solid rgba(255, 59, 48, 0.1);
  border-bottom: 1px solid rgba(255, 59, 48, 0.1);
  color: var(--text);
  padding: 14px 16px;
  border-radius: var(--r);
  font-size: 13px;
  line-height: 1.5;
  text-align: left;
  margin: 16px 0;
  box-shadow: var(--shadow-sm);
}
.maintenance-card-title {
  font-weight: 700;
  color: var(--err);
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 8px;
}
