/* ===== Variables ===== */
:root {
  --bg: #F1F5F9;
  --surface: #FFFFFF;
  --border: #E2E8F0;
  --border-hover: #CBD5E1;
  --text: #1E293B;
  --text-secondary: #64748B;
  --text-muted: #94A3B8;
  --primary: #3B82F6;
  --primary-dark: #2563EB;
  --primary-light: #EFF6FF;
  --primary-border: #BFDBFE;
  --danger: #EF4444;
  --danger-light: #FEF2F2;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08), 0 2px 4px rgba(0,0,0,0.04);
  --shadow-lg: 0 20px 40px rgba(0,0,0,0.12);

  /* Category colors */
  --first-lang-bg: #3B82F6;
  --first-lang-light: #EFF6FF;
  --first-lang-border: #93C5FD;
  --second-lang-bg: #10B981;
  --second-lang-light: #ECFDF5;
  --second-lang-border: #6EE7B7;
  --general-bg: #8B5CF6;
  --general-light: #F5F3FF;
  --general-border: #C4B5FD;
  --major-bg: #F59E0B;
  --major-light: #FFFBEB;
  --major-border: #FCD34D;
  --other-bg: #6B7280;
  --other-light: #F9FAFB;
  --other-border: #D1D5DB;
}

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; }
body {
  font-family: 'Noto Sans JP', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100%;
  line-height: 1.5;
}
a { text-decoration: none; color: inherit; }
button { font-family: inherit; cursor: pointer; }

/* ===== Loading ===== */
.loading-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  gap: 16px;
}
.loading-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { color: var(--text-secondary); font-size: 14px; }

/* ===== Login page ===== */
.login-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 24px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 48px 40px;
  text-align: center;
  max-width: 400px;
  width: 100%;
  box-shadow: var(--shadow-lg);
}
.login-logo {
  font-size: 52px;
  margin-bottom: 16px;
  display: block;
}
.login-card h1 {
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 6px;
}
.login-card .login-subtitle {
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 36px;
}
.google-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: white;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 20px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  width: 100%;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}
.google-btn:hover {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.1);
  background: var(--primary-light);
}
.login-note {
  margin-top: 20px;
  font-size: 12px;
  color: var(--text-muted);
}

/* ===== App wrapper ===== */
.app-wrapper {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ===== Header ===== */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 58px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
  gap: 16px;
}
.app-header-left {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-shrink: 0;
}
.app-title {
  font-size: 17px;
  font-weight: 700;
  white-space: nowrap;
}
.app-title span { color: var(--primary); }

.semester-tabs {
  display: flex;
  background: var(--bg);
  border-radius: 8px;
  padding: 3px;
  gap: 2px;
}
.semester-tab {
  padding: 5px 18px;
  border-radius: 6px;
  border: none;
  background: none;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  transition: all 0.15s;
  white-space: nowrap;
}
.semester-tab.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow);
}
.semester-tab:hover:not(.active) { color: var(--text); }

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
.user-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid var(--border);
}
.user-name {
  font-size: 13px;
  font-weight: 500;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.logout-btn {
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: none;
  font-size: 13px;
  color: var(--text-secondary);
  transition: all 0.15s;
  white-space: nowrap;
}
.logout-btn:hover { background: var(--bg); color: var(--text); }

/* ===== Stats bar ===== */
.stats-bar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 24px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  min-height: 42px;
}
.stat-total {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
}
.stat-total strong { color: var(--primary); font-size: 16px; }
.stat-divider {
  width: 1px;
  height: 18px;
  background: var(--border);
  flex-shrink: 0;
}
.stat-cats {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: center;
  flex: 1;
}
.stat-cat {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
}
.stat-cat strong { color: var(--text); }
.stat-cat.muted { opacity: 0.45; }
.legend {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-left: auto;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: var(--text-secondary);
}
.legend-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* ===== Timetable ===== */
.timetable-container {
  padding: 20px 24px;
  overflow-x: auto;
  flex: 1;
}
.timetable {
  display: grid;
  grid-template-columns: 72px repeat(6, minmax(120px, 1fr));
  gap: 4px;
  min-width: 780px;
}
.timetable-corner {
  background: var(--bg);
  position: sticky;
  left: 0;
  z-index: 2;
}
.timetable-header {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 10px 6px;
  text-align: center;
  font-size: 14px;
  font-weight: 700;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.timetable-period-cell {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 8px 4px;
  text-align: center;
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  position: sticky;
  left: 0;
  z-index: 2;
}
.period-num {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}
.period-time {
  font-size: 9px;
  color: var(--text-muted);
  line-height: 1.2;
}

/* Timetable cell */
.timetable-cell {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 8px;
  min-height: 95px;
  cursor: pointer;
  border: 2px solid var(--border);
  transition: border-color 0.15s, box-shadow 0.15s;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.timetable-cell:hover {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.12);
  z-index: 1;
}
.timetable-cell.active {
  border-color: var(--primary) !important;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2) !important;
  z-index: 2;
}

/* Empty cell */
.timetable-cell.empty .add-hint {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  color: var(--text-muted);
  transition: color 0.15s;
}
.timetable-cell.empty:hover .add-hint { color: var(--primary); }
.add-icon { font-size: 22px; line-height: 1; }

/* Filled cell */
.course-card-cell {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 3px;
  align-items: flex-start;
}
.course-cat-badge {
  font-size: 9px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 99px;
  color: white;
  letter-spacing: 0.3px;
}
.course-cell-name {
  font-size: 11px;
  font-weight: 700;
  line-height: 1.3;
  color: var(--text);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.course-cell-class {
  font-size: 10px;
  color: var(--text-secondary);
}
.course-cell-instructor {
  font-size: 10px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.course-cell-room {
  font-size: 10px;
  color: var(--text-muted);
}

/* Category cell colors */
.timetable-cell[data-category="第一外国語"] {
  background: var(--first-lang-light);
  border-color: var(--first-lang-border);
}
.timetable-cell[data-category="第二外国語"] {
  background: var(--second-lang-light);
  border-color: var(--second-lang-border);
}
.timetable-cell[data-category="共通教養"] {
  background: var(--general-light);
  border-color: var(--general-border);
}
.timetable-cell[data-category="専門"] {
  background: var(--major-light);
  border-color: var(--major-border);
}

/* ===== Side panel ===== */
.side-panel-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.side-panel-overlay.open {
  opacity: 1;
  pointer-events: all;
}
.side-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 420px;
  height: 100vh;
  background: var(--surface);
  border-left: 1px solid var(--border);
  transform: translateX(100%);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
  z-index: 201;
  box-shadow: var(--shadow-lg);
}
.side-panel.open { transform: translateX(0); }

@media (max-width: 480px) {
  .side-panel { width: 100%; }
}

.side-panel-header {
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
}
.side-panel-title { font-size: 18px; font-weight: 700; }
.side-panel-subtitle { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }
.side-panel-close {
  width: 32px;
  height: 32px;
  border: none;
  background: var(--bg);
  border-radius: 6px;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--text-secondary);
  transition: background 0.15s, color 0.15s;
}
.side-panel-close:hover { background: var(--border); color: var(--text); }

.side-panel-search {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.search-input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.search-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.side-panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Section labels */
.section-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--text-muted);
  padding: 0 2px;
}
.section-divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

/* Course item */
.course-item {
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}
.course-item:hover {
  border-color: var(--primary);
  background: var(--primary-light);
  box-shadow: 0 0 0 1px rgba(59, 130, 246, 0.1);
}
.course-item.selected {
  border-color: var(--primary);
  background: var(--primary-light);
}
.course-item.selected .select-indicator { display: flex; }

.course-item-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.course-item-name {
  font-size: 14px;
  font-weight: 700;
  line-height: 1.3;
  flex: 1;
}
.select-indicator {
  display: none;
  width: 20px;
  height: 20px;
  background: var(--primary);
  border-radius: 50%;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 11px;
  flex-shrink: 0;
  font-weight: 700;
}

.course-item-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 8px;
}
.badge {
  font-size: 10px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 99px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-secondary);
  white-space: nowrap;
}
.badge.cat {
  color: white;
  border: none;
}
.badge.auto {
  background: #FEF3C7;
  color: #92400E;
  border-color: #FDE68A;
}

.course-item-details {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 12px;
}
.detail-row {
  display: flex;
  gap: 6px;
  color: var(--text-secondary);
}
.detail-label {
  color: var(--text-muted);
  flex-shrink: 0;
  width: 2.5em;
}

.syllabus-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 10px;
  font-size: 12px;
  font-weight: 600;
  color: var(--primary);
  padding: 5px 12px;
  border: 1px solid var(--primary-border);
  border-radius: var(--radius-sm);
  background: var(--primary-light);
  transition: background 0.15s, border-color 0.15s;
  text-decoration: none;
}
.syllabus-link:hover {
  background: #DBEAFE;
  border-color: var(--primary);
}

.deselect-btn {
  width: 100%;
  margin-top: 8px;
  padding: 7px;
  border: 1px solid #FCA5A5;
  background: #FFF5F5;
  color: var(--danger);
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  font-family: inherit;
  transition: background 0.15s;
}
.deselect-btn:hover { background: #FEE2E2; }

/* Empty state */
.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
}
.empty-state-icon { font-size: 40px; margin-bottom: 12px; }
.empty-state-text { font-size: 14px; }

/* ===== Mobile timetable ===== */
.mobile-timetable {
  display: flex;
  flex-direction: column;
  gap: 0;
  height: 100%;
}
.mobile-day-tabs {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.mobile-day-tabs::-webkit-scrollbar { display: none; }
.mobile-day-tab {
  flex: 1;
  min-width: 48px;
  padding: 10px 4px 8px;
  border: none;
  background: none;
  font-size: 15px;
  font-weight: 700;
  color: var(--text-muted);
  position: relative;
  transition: color 0.15s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.mobile-day-tab.active {
  color: var(--primary);
  border-bottom: 2px solid var(--primary);
}
.mobile-day-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--primary);
}
.mobile-day-tab:not(.active) .mobile-day-dot { background: var(--text-muted); opacity: 0.5; }

.mobile-periods {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 16px;
}
.mobile-period-row {
  display: flex;
  gap: 10px;
  align-items: stretch;
  cursor: pointer;
}
.mobile-period-label {
  width: 56px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  padding: 8px 4px;
}
.mobile-period-num {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}
.mobile-period-time {
  font-size: 9px;
  color: var(--text-muted);
  text-align: center;
  line-height: 1.3;
  margin-top: 2px;
}
.mobile-period-content {
  flex: 1;
  border-radius: var(--radius-sm);
  border: 2px solid var(--border);
  background: var(--surface);
  min-height: 72px;
  display: flex;
  align-items: center;
  padding: 10px 12px;
  transition: border-color 0.15s, background 0.15s;
}
.mobile-period-row:hover .mobile-period-content {
  border-color: var(--primary);
}
.mobile-period-row.active .mobile-period-content {
  border-color: var(--primary) !important;
  box-shadow: 0 0 0 3px rgba(59,130,246,0.15);
}
.mobile-course-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 100%;
}
.mobile-course-name {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.3;
}
.mobile-course-meta {
  font-size: 12px;
  color: var(--text-secondary);
}
.mobile-empty {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
  width: 100%;
}
.mobile-plus { font-size: 20px; }
.mobile-empty-text { font-size: 13px; }

/* ===== Mobile responsive header ===== */
@media (max-width: 767px) {
  .app-header { padding: 0 14px; height: 52px; gap: 10px; }
  .app-title { font-size: 15px; }
  .user-name { display: none; }
  .logout-btn { font-size: 12px; padding: 4px 9px; }
  .settings-btn .settings-summary { display: none; }
  .settings-btn { padding: 5px 8px; }
  .stats-bar { padding: 6px 14px; gap: 8px; }
  .legend { display: none; }
  .stat-total strong { font-size: 14px; }
  .timetable-container { padding: 0; }
  .side-panel { width: 100%; }
  .modal { max-height: calc(100vh - 20px); }
}

/* ===== Error banner */
.error-banner {
  background: var(--danger-light);
  color: var(--danger);
  padding: 10px 24px;
  font-size: 13px;
  border-bottom: 1px solid #FECACA;
}

/* ===== Settings button ===== */
.settings-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: none;
  font-size: 12px;
  color: var(--text-secondary);
  transition: all 0.15s;
  white-space: nowrap;
  max-width: 220px;
  overflow: hidden;
}
.settings-btn:hover { background: var(--bg); color: var(--text); border-color: var(--primary); }
.settings-summary {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  z-index: 300;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.modal-overlay.open {
  opacity: 1;
  pointer-events: all;
}
.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.96);
  width: 480px;
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 40px);
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  z-index: 301;
  display: flex;
  flex-direction: column;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  overflow: hidden;
}
.modal.open {
  opacity: 1;
  pointer-events: all;
  transform: translate(-50%, -50%) scale(1);
}

.modal-header {
  padding: 20px 24px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
}
.modal-title { font-size: 16px; font-weight: 700; }
.modal-subtitle { font-size: 13px; color: var(--text-secondary); margin-top: 3px; }

.modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
}
.settings-grid {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.settings-field {}
.settings-label {
  display: block;
  font-size: 13px;
  font-weight: 700;
  margin-bottom: 8px;
  color: var(--text);
}
.label-note {
  font-weight: 400;
  color: var(--text-muted);
  font-size: 12px;
}
.class-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.class-btn {
  width: 40px;
  height: 40px;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  font-size: 15px;
  font-weight: 700;
  color: var(--text-secondary);
  transition: all 0.15s;
}
.class-btn:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-light); }
.class-btn.active {
  border-color: var(--primary);
  background: var(--primary);
  color: white;
}

.student-id-input {
  width: 160px;
  padding: 10px 14px;
  border: 2px solid var(--border);
  border-radius: 8px;
  font-size: 18px;
  font-weight: 700;
  font-family: inherit;
  letter-spacing: 4px;
  background: var(--bg);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}
.student-id-input:focus { border-color: var(--primary); }

.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  flex-shrink: 0;
}
.modal-error {
  font-size: 12px;
  color: var(--danger);
  flex: 1;
}
.save-btn {
  padding: 10px 28px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 700;
  font-family: inherit;
  transition: background 0.15s;
}
.save-btn:hover { background: var(--primary-dark); }
.save-btn:disabled { opacity: 0.6; cursor: not-allowed; }
