* { margin: 0; padding: 0; box-sizing: border-box; }
html { height: 100%; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; display: flex; flex-direction: column; height: 100%; background: #f5f7fa; color: #333; }

/* Tabs */
.tab-bar { display: flex; background: #2c7a7b; }
.tab-btn { padding: 12px 28px; background: none; border: none; color: rgba(255,255,255,0.7); font-size: 15px; font-weight: 500; cursor: pointer; border-bottom: 3px solid transparent; transition: all 0.15s; }
.tab-btn:hover { color: #fff; background: rgba(255,255,255,0.1); }
.tab-btn.active { color: #fff; border-bottom-color: #fff; background: rgba(255,255,255,0.1); }
.tab-content { display: none; flex: 1; overflow: hidden; min-height: 0; }
.tab-content.active { display: flex; }

/* Sidebar */
.sidebar { width: 280px; background: #fff; border-right: 1px solid #e0e4e8; display: flex; flex-direction: column; transition: width 0.2s, opacity 0.2s; }
.sidebar-header { padding: 20px; border-bottom: 1px solid #e0e4e8; }
.sidebar-header h2 { font-size: 18px; color: #2c7a7b; margin-bottom: 12px; }
.sidebar-header button { width: 100%; padding: 8px; background: #2c7a7b; color: #fff; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; }
.sidebar-header button:hover { background: #285e61; }
.sidebar-title-label { display: none; }
.patient-list { flex: 1; overflow-y: auto; }
.patient-item { padding: 12px 20px; cursor: pointer; border-bottom: 1px solid #f0f0f0; transition: background 0.15s; }
.patient-item:hover { background: #e6fffa; }
.patient-item.active { background: #e6fffa; border-left: 3px solid #2c7a7b; }

.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.main-header { padding: 20px 30px; background: #fff; border-bottom: 1px solid #e0e4e8; display: flex; justify-content: space-between; align-items: center; position: sticky; top: 0; z-index: 10; }
.main-header h1 { font-size: 22px; color: #2c7a7b; }
.header-left { display: flex; align-items: center; gap: 8px; }
.header-section-label { font-size: 16px; color: #2c7a7b; font-weight: 600; background: none; border: none; cursor: pointer; padding: 4px 8px; border-radius: 6px; transition: background 0.2s; }
.header-section-label:hover { background: #e6fffa; }
.header-separator { color: #cbd5e0; font-size: 18px; }
.header-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.main-content { flex: 1; padding: 30px; overflow-y: auto; }
.placeholder { display: flex; align-items: center; justify-content: center; height: 100%; color: #a0aec0; font-size: 18px; }

.info-row { display: flex; gap: 20px; margin-bottom: 20px; }
.info-card { background: #fff; padding: 16px; border-radius: 8px; flex: 1; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.info-card label { font-size: 12px; color: #718096; text-transform: uppercase; letter-spacing: 0.5px; }
.info-card input { width: 100%; border: 1px solid #e0e4e8; border-radius: 4px; padding: 6px 8px; margin-top: 4px; font-size: 14px; font-family: inherit; }
.save-btn { padding: 6px 16px; background: #2c7a7b; color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 13px; margin-top: 8px; }
.delete-btn { padding: 6px 16px; background: #e53e3e; color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 13px; }

.sessions-header { display: flex; justify-content: space-between; align-items: center; margin: 20px 0 12px; }
.sessions-header h3 { color: #2d3748; }
.add-session-btn { padding: 6px 14px; background: #38a169; color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 13px; }
.session-item { background: #fff; padding: 14px 18px; border-radius: 8px; margin-bottom: 8px; display: flex; justify-content: space-between; align-items: center; box-shadow: 0 1px 3px rgba(0,0,0,0.06); }
.session-date { font-weight: 600; color: #2c7a7b; }
.session-actions { display: flex; gap: 12px; align-items: center; }
.session-link { color: #3182ce; cursor: pointer; text-decoration: underline; font-size: 13px; }
.session-link:hover { color: #2b6cb0; }
.session-delete { color: #e53e3e; cursor: pointer; font-size: 13px; }
.session-delete:hover { color: #c53030; }
.consent-btn { padding: 6px 14px; background: #805ad5; color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 13px; }

/* Modal */
.modal-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.4); display: flex; align-items: center; justify-content: center; z-index: 100; }
.modal { background: #fff; padding: 24px; border-radius: 10px; width: 520px; max-width: 90%; max-height: 90vh; overflow-y: auto; }
.modal h3 { margin-bottom: 16px; color: #2d3748; }
.modal label { font-size: 12px; color: #718096; text-transform: uppercase; letter-spacing: 0.5px; display: block; margin-bottom: 4px; }
.modal input, .modal textarea, .modal select { width: 100%; border: 1px solid #e0e4e8; border-radius: 4px; padding: 8px 10px; margin-bottom: 12px; font-size: 14px; font-family: inherit; }
.modal textarea { min-height: 120px; resize: vertical; }
.modal .row { display: flex; gap: 12px; align-items: flex-end; }
.modal .row > div { flex: 1; }
.modal .row .btn-add-machine { flex: 0; padding: 8px 12px; background: #38a169; color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; font-weight: bold; margin-bottom: 12px; }
.modal .row .btn-add-machine:hover { background: #2f855a; }
.machine-row-remove { flex: 0; padding: 8px 10px; background: #e53e3e; color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; margin-bottom: 12px; }
.machine-row-remove:hover { background: #c53030; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.modal-actions button { padding: 8px 18px; border: none; border-radius: 4px; cursor: pointer; font-size: 14px; }
.btn-primary { background: #2c7a7b; color: #fff; }
.btn-cancel { background: #e2e8f0; color: #4a5568; }

/* Calendar */
.calendar-container { flex: 1; padding: 20px 20px 0 20px; display: flex; flex-direction: column; overflow: hidden; min-height: 0; background: #f5f7fa; }
.calendar-nav { display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 12px; flex-shrink: 0; }
.calendar-nav button { padding: 8px 16px; background: #2c7a7b; color: #fff; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; }
.calendar-nav button:hover { background: #285e61; }
.calendar-nav h2 { font-size: 20px; color: #2d3748; min-width: 220px; text-align: center; }
.calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); grid-template-rows: auto repeat(6, 1fr); gap: 2px; background: #e0e4e8; border-radius: 8px 8px 0 0; overflow: hidden; flex: 1; min-height: 0; }
.cal-header { background: #2c7a7b; color: #fff; padding: 8px; text-align: center; font-weight: 600; font-size: 13px; }
.cal-day { background: #fff; padding: 6px; overflow-y: auto; }
.cal-day.other-month { background: #f9fafb; }
.cal-day.today { background: #e6fffa; }
.cal-day-number { font-size: 13px; font-weight: 600; color: #4a5568; margin-bottom: 4px; }
.cal-day.other-month .cal-day-number { color: #cbd5e0; }
.cal-day.today .cal-day-number { color: #2c7a7b; }
.cal-appt { padding: 3px 6px; margin-bottom: 3px; border-radius: 3px; font-size: 11px; cursor: default; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; justify-content: space-between; align-items: center; }
.cal-appt.status-citado { background: #ebf8ff; border-left: 3px solid #3182ce; color: #2a4365; }
.cal-appt.status-confirmado { background: #c6f6d5; border-left: 3px solid #38a169; color: #22543d; }
.cal-appt.status-cancelado { background: #fed7d7; border-left: 3px solid #e53e3e; color: #742a2a; text-decoration: line-through; }
.cal-appt-del { color: #e53e3e; cursor: pointer; margin-left: 4px; font-weight: bold; font-size: 13px; }
.cal-appt-del:hover { color: #c53030; }
.add-appt-btn { padding: 8px 16px; background: #d69e2e; color: #fff; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; }
.add-appt-btn:hover { background: #b7791f; }
.view-toggle { padding: 8px 16px; background: #4a5568; color: #fff; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; }
.view-toggle:hover { background: #2d3748; }
.cal-grid-week { grid-template-rows: auto 1fr !important; height: 100%; }

/* Week time-grid view */
.week-appt { position: absolute; border-radius: 4px; padding: 2px 5px; font-size: 10px; overflow: hidden; cursor: grab; z-index: 2; display: flex; flex-direction: column; justify-content: flex-start; align-items: flex-start; line-height: 1.3; box-sizing: border-box; transition: opacity 0.15s; text-align: left; }
.week-appt:active { cursor: grabbing; opacity: 0.7; }
.week-day-col.drag-over { background: #e6fffa !important; }
.week-appt .appt-time { font-weight: 700; font-size: 11px; }
.week-appt .appt-name { font-size: 10px; font-weight: 400; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.week-appt .appt-del { position: absolute; top: 2px; right: 4px; color: #e53e3e; cursor: pointer; font-weight: bold; font-size: 11px; }

/* Drag time preview */
.drag-time-preview { position: fixed; background: #2d3748; color: #fff; padding: 4px 10px; border-radius: 4px; font-size: 12px; font-weight: 600; pointer-events: none; z-index: 9999; white-space: nowrap; box-shadow: 0 2px 8px rgba(0,0,0,0.2); display: none; }

/* Toggle buttons */
.modal .toggle-row { display: flex; gap: 8px; margin-bottom: 12px; }
.modal .toggle-btn { flex: 1; padding: 10px; border: 2px solid #e0e4e8; border-radius: 6px; background: #fff; cursor: pointer; font-size: 14px; text-align: center; color: #4a5568; transition: all 0.15s; }
.modal .toggle-btn.selected { border-color: #2c7a7b; background: #e6fffa; color: #2c7a7b; font-weight: 600; }

/* Autocomplete */
.autocomplete-wrapper { position: relative; }
.autocomplete-list { position: absolute; top: 100%; left: 0; right: 0; background: #fff; border: 1px solid #e0e4e8; border-top: none; border-radius: 0 0 4px 4px; max-height: 150px; overflow-y: auto; z-index: 10; margin-top: -12px; }
.autocomplete-item { padding: 8px 10px; cursor: pointer; font-size: 14px; }
.autocomplete-item:hover { background: #e6fffa; }

/* Specialists table */
.spec-table { width: 100%; border-collapse: collapse; background: #fff; border-radius: 8px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.spec-table th { background: #2c7a7b; color: #fff; padding: 12px 16px; text-align: left; font-size: 13px; text-transform: uppercase; letter-spacing: 0.5px; }
.spec-table td { padding: 12px 16px; border-bottom: 1px solid #f0f0f0; font-size: 14px; }
.spec-table tr:hover td { background: #f7fafa; }
.spec-table .spec-actions { display: flex; gap: 10px; }
.spec-edit { color: #3182ce; cursor: pointer; font-size: 13px; }
.spec-edit:hover { color: #2b6cb0; }
.spec-del { color: #e53e3e; cursor: pointer; font-size: 13px; }
.spec-del:hover { color: #c53030; }
.spec-badge { display: inline-block; padding: 2px 8px; background: #e6fffa; color: #2c7a7b; border-radius: 4px; font-size: 12px; font-weight: 500; }

/* ========== RESPONSIVE / MOBILE ========== */
@media (max-width: 768px) {
  /* Tab bar: fixed bottom nav */
  .tab-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 50;
    flex-direction: row;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: 0;
    box-shadow: 0 -2px 10px rgba(0,0,0,0.15);
  }
  .tab-bar > div:first-child {
    display: flex;
    flex: 1;
    width: 100%;
    min-width: 0;
    overflow-x: hidden;
    scrollbar-width: none;
  }
  .tab-bar > div:first-child::-webkit-scrollbar { display: none; }
  .tab-btn {
    flex: 1 1 0;
    min-width: 0;
    padding: 8px 4px;
    font-size: 10px;
    white-space: nowrap;
    text-align: center;
    border-bottom: none;
    border-top: 3px solid transparent;
  }
  .tab-btn.active { border-bottom: none; border-top-color: #fff; }
  #userBar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 50;
    background: #2c7a7b;
    padding: 8px 12px !important;
    gap: 8px !important;
    font-size: 12px;
    justify-content: flex-end;
    box-shadow: 0 2px 6px rgba(0,0,0,0.1);
  }
  #userBar button { padding: 4px 8px !important; font-size: 11px !important; }

  /* Body padding to account for fixed bars */
  body { padding-top: 40px; padding-bottom: 52px; }
  .tab-content { min-height: calc(100vh - 92px); }

  /* Pacientes: sidebar full-width overlay or stacked */
  .tab-content#tab-pacientes { flex-direction: column; }
  .sidebar {
    width: 100% !important;
    max-height: 45vh;
    border-right: none;
    border-bottom: 1px solid #e0e4e8;
  }
  .patient-list { max-height: 25vh; }
  .main-header { padding: 12px 16px; flex-direction: column; align-items: flex-start; gap: 8px; }
  .main-header h1 { font-size: 18px; }
  .header-left { order: 1; width: 100%; }
  .header-section-label { display: inline-block; background: #2c7a7b !important; color: #fff !important; padding: 6px 12px !important; border-radius: 6px !important; font-size: 14px !important; }
  .header-separator { display: none; }
  .header-right { order: 0; width: 100%; }
  .main-content { padding: 16px; }

  /* Info cards: stack vertically */
  .info-row { flex-direction: column; gap: 10px; margin-bottom: 12px; }
  .info-card { padding: 12px; }

  /* Sessions */
  .sessions-header { flex-direction: column; align-items: flex-start; gap: 8px; margin: 14px 0 8px; }
  .session-item { flex-direction: column; align-items: flex-start; gap: 6px; padding: 10px 14px; }
  .session-actions { width: 100%; justify-content: flex-start; }

  /* Calendar */
  .calendar-container { padding: 10px 8px 0; }
  .calendar-nav { flex-wrap: wrap; gap: 8px; justify-content: center; }
  .calendar-nav h2 { font-size: 16px; min-width: unset; width: 100%; text-align: center; order: -1; }
  .calendar-nav button, .calendar-nav select { font-size: 12px; padding: 6px 10px; }
  .add-appt-btn { font-size: 12px; padding: 6px 10px; }
  .view-toggle { font-size: 12px; padding: 6px 10px; }
  .cal-header { padding: 6px 2px; font-size: 11px; }
  .cal-day { padding: 3px; }
  .cal-day-number { font-size: 11px; }
  .cal-appt { font-size: 9px; padding: 2px 4px; }
  .week-appt { font-size: 9px; padding: 2px 3px; }
  .week-appt .appt-time { font-size: 11px; font-weight: 800; white-space: nowrap; line-height: 1.2; }
  .week-appt .appt-name { display: block; font-size: 8px; font-weight: 400; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.2; }
  .week-appt .appt-del { top: 1px; right: 2px; font-size: 9px; }

  /* Specialists / Machines / Users tabs */
  #tab-especialistas > div:first-child,
  #tab-maquinaria > div:first-child,
  #tab-usuarios > div:first-child,
  #tab-reportes > div:first-child,
  #tab-historico > div:first-child {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 14px 16px;
  }
  #tab-especialistas > div:first-child h1,
  #tab-maquinaria > div:first-child h1,
  #tab-usuarios > div:first-child h1,
  #tab-reportes > div:first-child h1,
  #tab-historico > div:first-child h1 {
    font-size: 18px;
  }
  #tab-especialistas > div:first-child > div,
  #tab-maquinaria > div:first-child > div,
  #tab-usuarios > div:first-child > div {
    flex-wrap: wrap;
  }
  #specFilter, #machineFilter { width: 100% !important; }

  /* Reports filters */
  #tab-reportes > div:first-child > div { flex-direction: column; align-items: stretch; }
  #reportSpecFilter, #reportMode, #reportMonth, #reportYear,
  #reportDateFrom, #reportDateTo { width: 100%; }

  /* Tables */
  .spec-table { font-size: 13px; display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .spec-table th, .spec-table td { padding: 8px 10px; white-space: nowrap; }

  /* Modal */
  .modal { width: 95%; padding: 16px; max-height: 85vh; }
  .modal h3 { font-size: 16px; }
  .modal .row { flex-direction: column; gap: 6px; }
  .modal .row .btn-add-machine,
  .machine-row-remove { margin-bottom: 6px; }
  .modal-actions { flex-direction: column; }
  .modal-actions button { width: 100%; text-align: center; }

  /* RGPD signature canvas responsive */
  #rgpdSignatureCanvas { width: 100% !important; height: auto !important; max-width: 460px; }

  /* Placeholder */
  .placeholder { font-size: 15px; padding: 20px; text-align: center; }
}

/* Extra small screens (phones < 400px) */
@media (max-width: 400px) {
  .tab-btn { padding: 6px 8px; font-size: 9px; }
  .sidebar-header { padding: 12px; }
  .sidebar-header h2 { font-size: 16px; margin-bottom: 8px; }
  .main-header { padding: 10px 12px; }
  .main-content { padding: 10px; }
  .calendar-nav { gap: 6px; }
  .calendar-nav h2 { font-size: 14px; }
}
