:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #000000);
  --hint: var(--tg-theme-hint-color, #999999);
  --link: var(--tg-theme-link-color, #2481cc);
  --btn-bg: var(--tg-theme-button-color, #2481cc);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #f1f1f1);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  -webkit-tap-highlight-color: transparent;
}

#app { display: flex; flex-direction: column; height: 100vh; }

#header {
  padding: 12px 16px 10px;
  border-bottom: 1px solid var(--secondary-bg);
  background: var(--bg);
  position: sticky; top: 0; z-index: 10;
}
#header h1 { font-size: 18px; font-weight: 700; }

#list-container {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px 80px;
}

#loading { text-align: center; padding: 32px; color: var(--hint); }
.empty   { text-align: center; padding: 32px; color: var(--hint); }

/* ── Countdown banner ── */
#countdown-banner {
  background: linear-gradient(135deg, #f5a623, #e8940f);
  color: #fff;
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 10px;
  cursor: pointer;
}
#countdown-banner.hidden { display: none; }
.countdown-label { font-size: 11px; opacity: 0.85; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; }
.countdown-name  { font-size: 15px; font-weight: 600; margin-top: 2px; }
.countdown-time  { font-size: 22px; font-weight: 700; margin-top: 4px; letter-spacing: -0.5px; }
.countdown-date  { font-size: 12px; opacity: 0.85; margin-top: 2px; }

/* ── Datetime modal ── */
#datetime-modal {
  position: fixed; inset: 0; z-index: 200;
  display: flex; align-items: flex-end;
  background: rgba(0,0,0,0.4);
}
#datetime-modal.hidden { display: none; }
#datetime-sheet {
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  padding: 20px 16px calc(20px + env(safe-area-inset-bottom, 0));
  width: 100%;
}
#datetime-sheet h3 { font-size: 16px; font-weight: 700; margin-bottom: 14px; }
#datetime-input {
  width: 100%;
  padding: 12px;
  border: 1.5px solid var(--secondary-bg);
  border-radius: 12px;
  font-size: 16px;
  background: var(--secondary-bg);
  color: var(--text);
  margin-bottom: 12px;
}
#datetime-confirm {
  width: 100%;
  background: var(--btn-bg);
  color: var(--btn-text);
  border: none; border-radius: 12px;
  padding: 13px; font-size: 15px; font-weight: 600; cursor: pointer;
  margin-bottom: 8px;
}
#datetime-cancel {
  width: 100%;
  background: var(--secondary-bg);
  color: var(--hint);
  border: none; border-radius: 12px;
  padding: 11px; font-size: 15px; cursor: pointer;
}

/* ── Cards ── */
.card {
  background: var(--secondary-bg);
  border-radius: 14px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.1s;
  border-left: 4px solid var(--link);
  overflow: hidden;
}
.card:active { transform: scale(0.98); opacity: 0.85; }

.card[data-status=""]             { border-left-color: var(--link); }
.card[data-status="Заплановано"]  { border-left-color: #f5a623; }
.card[data-status="Проведено"]    { border-left-color: #28a745; }
.card[data-status="Відмова"]      { border-left-color: #dc3545; }
.card[data-status="Переглянуто"]  { border-left-color: var(--hint); }

.card-body { padding: 12px 12px 10px 12px; }

.card-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 5px;
}
.card-row:last-child { margin-bottom: 0; }

.card-main {
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 7px;
}

.card-icon { font-size: 13px; flex-shrink: 0; }

.card-name {
  font-weight: 600;
  font-size: 15px;
  flex: 1;
  line-height: 1.3;
}

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

.card-sub {
  color: var(--hint);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.card-footer-row {
  margin-top: 4px;
  justify-content: space-between;
}

.card-chevron {
  color: var(--hint);
  font-size: 18px;
  line-height: 1;
  opacity: 0.5;
}

.status-badge {
  font-size: 11px;
  padding: 3px 9px;
  border-radius: 100px;
  white-space: nowrap;
  flex-shrink: 0;
  font-weight: 500;
}
.status-new     { background: rgba(36,129,204,0.15);  color: var(--link); }
.status-planned { background: rgba(245,166,35,0.2);   color: #c07800; }
.status-done    { background: rgba(40,167,69,0.2);    color: #28a745; }
.status-refused { background: rgba(220,53,69,0.2);    color: #dc3545; }
.status-viewed  { background: rgba(153,153,153,0.15); color: var(--hint); }

/* ── Tab bar ── */
#tab-bar {
  display: flex;
  background: var(--bg);
  border-top: 1px solid var(--secondary-bg);
  position: fixed;
  bottom: 0; left: 0; right: 0;
  padding: 6px 0 env(safe-area-inset-bottom, 4px);
  z-index: 10;
}

.tab {
  flex: 1;
  border: none;
  background: none;
  color: var(--hint);
  font-size: 10px;
  padding: 4px 2px 2px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  position: relative;
  transition: color 0.15s;
}

.tab::before {
  content: '';
  position: absolute;
  top: 0; left: 20%; right: 20%;
  height: 2px;
  border-radius: 0 0 2px 2px;
  background: var(--btn-bg);
  opacity: 0;
  transition: opacity 0.15s;
}

.tab.active { color: var(--btn-bg); }
.tab.active::before { opacity: 1; }

.tab-icon { font-size: 18px; line-height: 1; }
.tab-label { font-size: 10px; font-weight: 500; }

.badge {
  position: absolute;
  top: 2px;
  right: calc(50% - 18px);
  background: var(--btn-bg);
  color: var(--btn-text);
  border-radius: 100px;
  font-size: 9px;
  font-weight: 700;
  padding: 1px 4px;
  min-width: 14px;
  text-align: center;
  line-height: 1.4;
}
.badge:empty    { display: none; }
.badge-danger   { background: #dc3545; }

/* ── Detail panel ── */
#detail-panel {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg);
  z-index: 100;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}
#detail-panel.hidden { display: none; }

#back-btn {
  border: none;
  background: var(--bg);
  color: var(--link);
  font-size: 16px;
  padding: 12px 16px;
  cursor: pointer;
  text-align: left;
  position: sticky; top: 0;
  width: 100%;
  font-weight: 500;
}

#detail-content { padding: 0 16px 16px; flex: 1; }

.detail-title { font-size: 18px; font-weight: 700; margin-bottom: 14px; }
.detail-field { margin-bottom: 9px; line-height: 1.5; }

.detail-questions { margin: 12px 0; }
.detail-questions strong { display: block; margin-bottom: 6px; font-size: 13px; color: var(--hint); }
.detail-questions ul { padding-left: 16px; }
.detail-questions li { margin-bottom: 5px; line-height: 1.5; }

.detail-ai {
  background: var(--secondary-bg);
  border-radius: 10px;
  padding: 12px;
  margin-top: 10px;
  font-size: 13px;
  line-height: 1.6;
}
.detail-ai strong { display: block; margin-bottom: 5px; font-size: 12px; color: var(--hint); font-weight: 600; }
.detail-time { color: var(--hint); font-size: 12px; margin-top: 14px; }

#status-actions {
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0));
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-top: 1px solid var(--secondary-bg);
  position: sticky; bottom: 0;
  background: var(--bg);
}
.status-btn {
  background: var(--btn-bg);
  color: var(--btn-text);
  border: none;
  border-radius: 12px;
  padding: 13px;
  font-size: 15px;
  cursor: pointer;
  font-weight: 600;
}
.status-btn:active  { opacity: 0.8; }
.status-btn.cancel  { background: var(--secondary-bg); color: var(--hint); font-weight: 500; }
.status-btn:disabled { opacity: 0.4; cursor: not-allowed; }
