/* ===== Reset & Base ===== */
* { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Microsoft JhengHei', sans-serif;
  background: #f0f2f5;
  color: #333;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-tap-highlight-color: transparent;
}

a { color: #1a73e8; text-decoration: none; }
a:hover { text-decoration: underline; }

/* ===== Header ===== */
header {
  background: linear-gradient(135deg, #1a237e, #283593);
  color: white;
  padding: 12px 16px 0;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 8px;
}
header h1 { font-size: 1.1rem; font-weight: 600; }
header h1 a { color: white; text-decoration: none; }
#user-info { display: flex; align-items: center; gap: 8px; font-size: 0.85rem; }
#user-name-display { font-weight: 500; }

nav {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  padding-bottom: 0;
  -webkit-overflow-scrolling: touch;
}
.nav-btn {
  background: rgba(255,255,255,0.1);
  border: none;
  color: rgba(255,255,255,0.8);
  padding: 8px 14px;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
  border-radius: 6px 6px 0 0;
  transition: background 0.2s;
}
.nav-btn:hover, .nav-btn.active {
  background: rgba(255,255,255,0.25);
  color: white;
}

/* ===== Main ===== */
main {
  flex: 1;
  padding: 12px;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
}

/* ===== Pages ===== */
.page { display: none; }
.page.active { display: block; }

/* ===== Cards ===== */
.card {
  background: white;
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}
.card h2 {
  font-size: 1.15rem;
  color: #1a237e;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 2px solid #e8eaf6;
}
.card h3 {
  font-size: 1rem;
  color: #333;
  margin: 12px 0 8px;
}

/* ===== Forms ===== */
.form-group {
  margin-bottom: 12px;
}
.form-group label {
  display: block;
  font-size: 0.85rem;
  font-weight: 500;
  margin-bottom: 4px;
  color: #555;
}
.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 0.95rem;
  font-family: inherit;
  transition: border-color 0.2s;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  outline: none;
  border-color: #1a73e8;
  box-shadow: 0 0 0 2px rgba(26,115,232,0.15);
}
.form-row {
  display: flex;
  gap: 12px;
}
.form-row .form-group { flex: 1; }
.form-group.inline {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-right: 16px;
}
.form-group.inline label { margin-bottom: 0; }
.form-group.inline select { width: auto; }

/* ===== Buttons ===== */
.btn {
  display: inline-block;
  padding: 8px 16px;
  border: none;
  border-radius: 6px;
  font-size: 0.9rem;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}
.btn:active { transform: scale(0.97); }
.btn-primary {
  background: #1a73e8;
  color: white;
}
.btn-primary:hover { background: #1557b0; }
.btn-block { width: 100%; }
.btn-sm { padding: 5px 10px; font-size: 0.8rem; }
.form-actions { margin-top: 16px; }

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: #323232;
  color: white;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 0.9rem;
  z-index: 9999;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  animation: slideUp 0.3s ease;
}
.toast.error { background: #d32f2f; }
.toast.success { background: #388e3c; }
@keyframes slideUp { from { transform: translateX(-50%) translateY(20px); opacity:0; } to { transform: translateX(-50%) translateY(0); opacity:1; } }

/* ===== Grid Legend ===== */
.grid-legend {
  display: flex;
  gap: 12px;
  margin-bottom: 8px;
  font-size: 0.8rem;
}
.legend-item { display: flex; align-items: center; gap: 4px; }
.legend-color { width: 14px; height: 14px; border-radius: 3px; display: inline-block; }

/* ===== Slot Modal ===== */
.modal-sm { max-width: 300px; }
.modal-actions { display: flex; gap: 8px; margin-top: 12px; }
.modal-actions .btn { flex: 1; }
.btn-danger { background: #d32f2f; color: white; }
.btn-danger:hover { background: #b71c1c; }

/* ===== Chart ===== */
.chart-container {
  position: relative;
  width: 100%;
  max-height: 250px;
  margin-bottom: 12px;
}
.stats-summary {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
.stat-card {
  flex: 1;
  min-width: 100px;
  background: #e8eaf6;
  border-radius: 8px;
  padding: 10px;
  text-align: center;
}
.stat-card .stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  color: #1a237e;
}
.stat-card .stat-label {
  font-size: 0.75rem;
  color: #666;
}

/* ===== Instructions ===== */
.instructions h3 {
  color: #1a237e;
  margin-top: 12px;
}
.instructions ol {
  padding-left: 20px;
  margin-bottom: 8px;
}
.instructions li {
  margin-bottom: 4px;
  font-size: 0.9rem;
  line-height: 1.5;
}

/* ===== Availability Grid ===== */
.week-nav {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  font-size: 0.9rem;
}
.availability-settings {
  display: flex;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.grid-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 -16px;
  padding: 0 16px;
}
.time-grid {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  user-select: none;
  -webkit-user-select: none;
  min-width: 500px;
}
.time-grid th {
  padding: 4px 2px;
  font-size: 0.7rem;
  text-align: center;
  background: #e8eaf6;
  border: 1px solid #c5cae9;
  color: #333;
  position: sticky;
  top: 0;
  z-index: 2;
}
.time-grid th .date-label {
  display: block;
  font-size: 0.65rem;
  color: #666;
  font-weight: normal;
}
.time-grid td {
  height: 22px;
  border: 1px solid #e0e0e0;
  font-size: 0.65rem;
  text-align: center;
  cursor: pointer;
  transition: background 0.1s;
  position: relative;
}
.time-grid td:first-child {
  width: 50px;
  min-width: 50px;
  cursor: default;
  background: #fafafa;
  font-weight: 500;
  font-size: 0.7rem;
  color: #555;
}
.time-grid td.selected {
  background: #bbdefb;
}
.time-grid td.saved {
  background: #a5d6a7;
}
.time-grid td.saved.online {
  background: #80cbc4;
}
.time-grid td.saved.in_person {
  background: #ffcc80;
}
.time-grid td.has-lesson {
  background: #ef9a9a;
  cursor: default;
}
.time-grid td.has-lesson::after {
  content: attr(data-lesson-info);
  position: absolute;
  font-size: 0.55rem;
  left: 2px;
  top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: calc(100% - 4px);
}
.time-grid td:hover:not(:first-child):not(.has-lesson) {
  background: #e3f2fd;
}
.time-grid .time-label {
  font-size: 0.7rem;
}

/* ===== Lesson Page ===== */
.step-section {
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid #eee;
}
.step-section:last-of-type { border-bottom: none; }

.autocomplete-wrapper { position: relative; }
.suggestions-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: white;
  border: 1px solid #ddd;
  border-radius: 0 0 6px 6px;
  max-height: 200px;
  overflow-y: auto;
  z-index: 50;
  box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.suggestion-item {
  padding: 8px 12px;
  cursor: pointer;
  font-size: 0.9rem;
  border-bottom: 1px solid #f5f5f5;
}
.suggestion-item:hover { background: #e3f2fd; }

.lesson-content-options { display: flex; flex-direction: column; gap: 6px; }
.radio-label {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: background 0.2s;
}
.radio-label:hover { background: #f5f5f5; }
.radio-label input[type="radio"] { margin: 0; }
.content-link {
  margin-left: auto;
  font-size: 0.8rem;
  color: #1a73e8;
}
.location-options { display: flex; gap: 12px; flex-wrap: wrap; }

/* Matching members */
#matching-members .hint { color: #999; font-size: 0.85rem; font-style: italic; }
.member-check-list { display: flex; flex-direction: column; gap: 4px; }
.member-check-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 8px;
  border-radius: 6px;
  background: #fafafa;
  border: 1px solid #eee;
}
.member-check-item input[type="checkbox"] {
  margin-top: 3px;
  flex-shrink: 0;
}
.member-check-item .member-info-brief {
  font-size: 0.9rem;
  font-weight: 500;
}
.member-check-item .member-avail-info {
  font-size: 0.75rem;
  color: #666;
}
.member-detail-card {
  background: #e8eaf6;
  border-radius: 6px;
  padding: 10px;
  margin-top: 6px;
  font-size: 0.85rem;
  display: none;
}
.member-detail-card.show { display: block; }
.member-detail-card p { margin-bottom: 4px; }

/* ===== Weekly View ===== */
.tabs {
  display: flex;
  gap: 2px;
  margin-bottom: 12px;
}
.tab-btn {
  flex: 1;
  padding: 8px;
  border: 1px solid #ddd;
  background: #f5f5f5;
  cursor: pointer;
  font-size: 0.85rem;
  font-family: inherit;
  border-radius: 6px 6px 0 0;
  transition: background 0.2s;
}
.tab-btn.active {
  background: white;
  border-bottom-color: white;
  font-weight: 600;
  color: #1a237e;
}
.tab-content { display: none; }
.tab-content.active { display: block; }

.weekly-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}
.weekly-table th {
  background: #e8eaf6;
  padding: 8px 6px;
  text-align: left;
  font-weight: 600;
  border-bottom: 2px solid #c5cae9;
  white-space: nowrap;
}
.weekly-table td {
  padding: 8px 6px;
  border-bottom: 1px solid #eee;
  vertical-align: top;
}
.weekly-table tr:hover td { background: #f5f5f5; }

.member-link {
  color: #1a73e8;
  cursor: pointer;
  text-decoration: underline;
}
.member-link:hover { color: #1557b0; }

/* ===== Modal ===== */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-content {
  background: white;
  border-radius: 12px;
  padding: 20px;
  max-width: 400px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  position: relative;
}
.modal-close {
  position: absolute;
  top: 10px;
  right: 14px;
  font-size: 1.5rem;
  cursor: pointer;
  color: #999;
}
.modal-close:hover { color: #333; }

/* ===== Footer ===== */
footer {
  text-align: center;
  padding: 12px;
  font-size: 0.75rem;
  color: #999;
}

/* ===== Utility ===== */
.text-center { text-align: center; }
.mt-1 { margin-top: 8px; }
.hidden { display: none !important; }

/* ===== My Lessons Section ===== */
.my-lesson-card {
  background: #fff3e0;
  border: 1px solid #ffcc80;
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 8px;
  font-size: 0.85rem;
}
.my-lesson-card .lesson-tag {
  display: inline-block;
  background: #e65100;
  color: white;
  font-size: 0.7rem;
  padding: 2px 6px;
  border-radius: 4px;
  margin-bottom: 4px;
}

/* ===== Members grid in weekly view ===== */
.members-avail-grid {
  overflow-x: auto;
}
.members-avail-grid table {
  min-width: 600px;
}

/* ===== Responsive ===== */
@media (max-width: 600px) {
  header { padding: 8px 12px 0; }
  header h1 { font-size: 0.95rem; }
  main { padding: 8px; }
  .card { padding: 12px; }
  .form-row { flex-direction: column; gap: 0; }
  .nav-btn { padding: 6px 10px; font-size: 0.8rem; }
  .time-grid th { font-size: 0.6rem; }
  .time-grid td { height: 20px; font-size: 0.6rem; }
  .time-grid td:first-child { width: 42px; min-width: 42px; font-size: 0.6rem; }
  .weekly-table { font-size: 0.78rem; }
  .weekly-table th, .weekly-table td { padding: 6px 4px; }
}
@media (min-width: 768px) {
  main { padding: 16px; }
  .card { padding: 20px; }
  .home-container { display: grid; grid-template-columns: 1fr; gap: 16px; }
}
@media (min-width: 1024px) {
  header h1 { font-size: 1.3rem; }
}
