/* Базовые токены */
:root {
  --clr-primary: #2563EB;
  --clr-primary-dark: #1D4ED8;
  --clr-sidebar: #FFFFFF;
  --clr-bg: #F8FAFC;
  --clr-border: #E2E8F0;
  --clr-text: #0F172A;
  --clr-subtle: #64748B;
  --sidebar-width: 256px;
  --header-height: 56px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; -webkit-tap-highlight-color: transparent; }
body { font-family: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif; color: var(--clr-text); background: var(--clr-bg); font-size: 14px; }
input, select, textarea, button { font-family: inherit; font-size: inherit; }

/* Скроллбары */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: #F1F5F9; }
::-webkit-scrollbar-thumb { background: #CBD5E1; border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: #94A3B8; }

/* ── Layout ─────────────────────────────────────────────────────────────────── */
.app-shell { display: flex; height: 100vh; overflow: hidden; }

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: white;
  border-right: 1px solid var(--clr-border);
  display: flex;
  flex-direction: column;
  transition: transform 0.25s;
  z-index: 40;
}
.sidebar-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.5);
  z-index: 35;
}

.main {
  flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0;
}
.app-header {
  background: white;
  border-bottom: 1px solid var(--clr-border);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  min-height: var(--header-height);
}
.menu-btn {
  display: none;
  background: none; border: none; cursor: pointer;
  width: 40px; height: 40px; border-radius: 8px;
  font-size: 22px; align-items: center; justify-content: center;
}
.menu-btn:hover { background: #F1F5F9; }

.content-area {
  flex: 1; overflow: auto;
  padding: 16px;
}

/* Боковое меню */
.sidebar-item {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 16px; border-radius: 10px;
  color: #475569; font-weight: 500; cursor: pointer;
  transition: all 0.15s;
  -webkit-user-select: none; user-select: none;
}
.sidebar-item:hover { background: #F1F5F9; color: var(--clr-text); }
.sidebar-item.active { background: #EFF6FF; color: var(--clr-primary); }
.sidebar-item .icon { width: 20px; flex-shrink: 0; text-align: center; }

/* ── Календарь ──────────────────────────────────────────────────────────────── */
.cal-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.cal-grid {
  display: grid;
  grid-template-columns: 60px repeat(var(--cols, 3), minmax(180px, 1fr));
  background: white;
  border-radius: 12px;
  border: 1px solid var(--clr-border);
  min-width: max-content;
}
.cal-header {
  padding: 10px 8px;
  border-bottom: 2px solid var(--clr-border);
  background: #F8FAFC;
  font-weight: 600;
  text-align: center;
  font-size: 13px;
  position: sticky; top: 0; z-index: 2;
}
.cal-time {
  border-right: 1px solid var(--clr-border);
  font-size: 11px;
  color: var(--clr-subtle);
  padding: 4px 6px;
  text-align: right;
  background: #FAFBFC;
  position: sticky; left: 0; z-index: 1;
}
.cal-cell {
  border-right: 1px solid var(--clr-border);
  border-bottom: 1px dashed #E2E8F0;
  min-height: 40px;
  height: 40px;
  position: relative;
  cursor: pointer;
  transition: background 0.1s;
}
.cal-cell:hover { background: #F1F5F9; }
.cal-cell:last-child { border-right: none; }

.appt-block {
  position: absolute;
  left: 3px; right: 3px;
  background: #DCFCE7;
  border-left: 3px solid #16A34A;
  border-radius: 6px;
  padding: 4px 6px;
  font-size: 11px;
  line-height: 1.3;
  overflow: hidden;
  cursor: pointer;
  transition: all 0.15s;
}
.appt-block:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0,0,0,0.08); z-index: 3; }
.appt-block.color-blue   { background: #DBEAFE; border-color: #2563EB; }
.appt-block.color-yellow { background: #FEF3C7; border-color: #D97706; }
.appt-block.color-purple { background: #EDE9FE; border-color: #7C3AED; }
.appt-block.color-pink   { background: #FCE7F3; border-color: #DB2777; }
.appt-block.color-red    { background: #FEE2E2; border-color: #DC2626; }

/* Слот времени */
.slot {
  display: inline-block;
  padding: 7px 12px; margin: 3px;
  border: 1px solid #CBD5E1;
  border-radius: 6px;
  font-size: 13px;
  background: white;
  cursor: pointer;
  user-select: none; -webkit-user-select: none;
  min-width: 56px; text-align: center;
}
.slot:hover:not(.booked) { background: #EFF6FF; border-color: var(--clr-primary); }
.slot.selected { background: var(--clr-primary); color: white; border-color: var(--clr-primary); }
.slot.booked { background: #F1F5F9; color: #94A3B8; cursor: not-allowed; text-decoration: line-through; }

/* Модальные окна */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 50; padding: 16px;
}
.modal {
  background: white; border-radius: 16px;
  width: 100%; max-width: 600px; max-height: 90vh;
  overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

/* Кнопки */
.btn { padding: 9px 16px; border-radius: 8px; font-weight: 500; transition: all 0.1s; cursor: pointer; border: none; font-size: 14px; min-height: 38px; }
.btn-primary { background: var(--clr-primary); color: white; }
.btn-primary:hover { background: var(--clr-primary-dark); }
.btn-secondary { background: #F1F5F9; color: var(--clr-text); }
.btn-secondary:hover { background: #E2E8F0; }
.btn-danger { background: #FEE2E2; color: #DC2626; }
.btn-danger:hover { background: #FECACA; }
.btn-success { background: #16A34A; color: white; }
.btn-success:hover { background: #15803D; }

/* Поля ввода */
.input {
  width: 100%; padding: 10px 12px;
  border: 1px solid #CBD5E1; border-radius: 8px;
  font-size: 14px; outline: none; transition: border 0.1s, box-shadow 0.1s;
  background: white;
  min-height: 40px;
}
.input:focus { border-color: var(--clr-primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1); }

/* Таблицы */
.tbl-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; background: white; border-radius: 12px; }
.tbl { width: 100%; border-collapse: collapse; }
.tbl thead { background: #F8FAFC; }
.tbl th { text-align: left; padding: 12px 16px; font-weight: 600; font-size: 13px; color: #475569; border-bottom: 1px solid var(--clr-border); white-space: nowrap; }
.tbl td { padding: 12px 16px; border-bottom: 1px solid #F1F5F9; font-size: 14px; }
.tbl tbody tr { cursor: pointer; }
.tbl tbody tr:hover { background: #F8FAFC; }

/* Бейдж */
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 500; }
.badge-green { background: #DCFCE7; color: #15803D; }
.badge-red   { background: #FEE2E2; color: #B91C1C; }
.badge-gray  { background: #F1F5F9; color: #475569; }
.badge-blue  { background: #DBEAFE; color: #1D4ED8; }

.hidden { display: none !important; }

@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
.animate-fade { animation: fadeIn 0.2s ease-out; }

/* ── Респонсив (мобильные) ──────────────────────────────────────────────────── */
@media (max-width: 900px) {
  .menu-btn { display: inline-flex; }

  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    transform: translateX(-100%);
    box-shadow: 4px 0 16px rgba(0,0,0,0.1);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop.open { display: block; }

  .app-header { padding: 8px 12px; gap: 8px; }
  .content-area { padding: 12px; }

  /* Скрываем переключатель клиники и поиск в шапке на мобиле — теперь в сайдбаре/контенте */
  #quick-search { display: none; }
  #clinic-switcher { max-width: 140px; font-size: 12px; }

  /* Таблицы → карточки */
  .tbl-mobile { background: transparent !important; }
  .tbl-mobile thead { display: none; }
  .tbl-mobile tbody tr {
    display: block;
    background: white;
    border-radius: 12px;
    margin-bottom: 10px;
    padding: 12px 14px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.04);
  }
  .tbl-mobile tbody td {
    display: block;
    padding: 4px 0;
    border-bottom: none;
    font-size: 14px;
  }
  .tbl-mobile tbody td:before {
    content: attr(data-label) ": ";
    font-weight: 600;
    color: #64748B;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-right: 6px;
  }
  .tbl-mobile tbody td:first-child:before { display: none; }
  .tbl-mobile tbody td:first-child {
    font-size: 16px;
    font-weight: 600;
    color: #0F172A;
    margin-bottom: 6px;
    padding-bottom: 6px;
    border-bottom: 1px solid #F1F5F9;
  }

  /* Модалки на весь экран */
  .modal-backdrop { padding: 0; align-items: stretch; }
  .modal {
    border-radius: 0;
    max-width: none;
    max-height: 100vh;
    min-height: 100vh;
  }

  /* Уменьшить размер заголовка страницы */
  #page-title { font-size: 16px; }

  /* Поля во формах в одну колонку на мобиле */
  .grid-2-mobile-1 { grid-template-columns: 1fr !important; }

  /* Календарь компактнее */
  .cal-header { font-size: 11px; padding: 8px 4px; }
  .cal-grid { grid-template-columns: 50px repeat(var(--cols, 3), minmax(140px, 1fr)); }
  .cal-time { font-size: 10px; }
  .appt-block { font-size: 10px; padding: 3px 5px; }
}

/* Очень узкий экран */
@media (max-width: 480px) {
  .app-header { padding: 6px 10px; }
  #page-title { font-size: 15px; }
  .btn { padding: 8px 12px; font-size: 13px; }
  .content-area { padding: 8px; }
}

/* PWA / iOS safe area */
@supports (padding: env(safe-area-inset-bottom)) {
  .sidebar { padding-bottom: env(safe-area-inset-bottom); }
  .content-area { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
}
