:root {
  --bg-primary: #f4f6f9;
  --bg-card: #ffffff;
  --text-main: #1e293b;
  --text-muted: #64748b;
  --primary-color: #3b82f6;
  --primary-hover: #2563eb;
  --border-color: #e2e8f0;
  --chat-sent: #3b82f6;
  --chat-received: #f1f5f9;
  --success-color: #10b981;
  --error-color: #ef4444;
  --warning-color: #f59e0b;
  --radius: 12px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.1);
  --transition: all 0.2s ease;
}

[data-theme="dark"] {
  --bg-primary: #0f172a;
  --bg-card: #1e293b;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --border-color: #334155;
  --chat-received: #334155;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.4);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.4);
}

* { 
  box-sizing: border-box; 
  margin: 0; 
  padding: 0; 
  font-family: 'Inter', system-ui, -apple-system, sans-serif; 
}

body { 
  background: var(--bg-primary); 
  color: var(--text-main); 
  height: 100vh; 
  overflow: hidden; 
  transition: background 0.3s ease, color 0.3s ease;
}

/* ARM Optimization: Global Loader Overlay */
#global-loader {
  position: fixed; 
  inset: 0; 
  background: rgba(15, 23, 42, 0.85); 
  backdrop-filter: blur(4px);
  display: flex; 
  flex-direction: column; 
  align-items: center; 
  justify-content: center;
  z-index: 9999; 
  color: white; 
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

#global-loader.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.spinner {
  width: 48px; 
  height: 48px; 
  border: 4px solid rgba(255,255,255,0.2);
  border-top-color: #fff; 
  border-radius: 50%; 
  animation: spin 0.8s linear infinite;
}

@keyframes spin { 
  to { transform: rotate(360deg); } 
}

#global-loader p {
  margin-top: 16px;
  font-size: 0.95rem;
  color: rgba(255,255,255,0.8);
}

/* Skeleton Loaders to prevent layout shift */
.skeleton {
  background: linear-gradient(90deg, var(--border-color) 25%, var(--bg-primary) 50%, var(--border-color) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: 6px;
}

@keyframes shimmer { 
  0% { background-position: 200% 0; } 
  100% { background-position: -200% 0; } 
}

.skeleton-text {
  height: 1rem;
  margin-bottom: 0.5rem;
}

.skeleton-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
}

.skeleton-card {
  height: 80px;
  border-radius: 8px;
}

/* View Management */
.view { 
  width: 100vw; 
  height: 100vh; 
  display: flex; 
}

.hidden { 
  display: none !important; 
}

/* Login Container */
.login-card {
  width: 100%;
  max-width: 400px;
  margin: auto;
  padding: 2.5rem;
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border-color);
}

.login-card h2 {
  margin-bottom: 1.5rem;
  text-align: center;
  font-size: 1.5rem;
  font-weight: 600;
}

.form-group { 
  margin-bottom: 1.25rem; 
}

.form-group label { 
  display: block; 
  font-size: 0.875rem; 
  margin-bottom: 0.5rem; 
  font-weight: 500;
  color: var(--text-main);
}

.form-group input {
  width: 100%; 
  padding: 0.875rem 1rem; 
  border: 1px solid var(--border-color); 
  border-radius: 8px; 
  background: var(--bg-primary); 
  color: var(--text-main);
  font-size: 1rem;
  transition: var(--transition);
}

.form-group input:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

.form-group input::placeholder {
  color: var(--text-muted);
}

.btn {
  padding: 0.875rem 1.5rem; 
  border: none; 
  background: var(--primary-color); 
  color: white; 
  border-radius: 8px; 
  cursor: pointer; 
  font-weight: 500;
  font-size: 1rem;
  transition: var(--transition);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.btn:hover:not(:disabled) {
  opacity: 1;
}

.btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--border-color);
  color: var(--text-main);
}

.btn-secondary:hover:not(:disabled) {
  background: var(--border-color);
  opacity: 0.8;
}

.btn-danger {
  background: var(--error-color);
}

.btn-danger:hover:not(:disabled) {
  background: #dc2626;
}

.btn-icon {
  padding: 0.5rem;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-main);
  cursor: pointer;
  transition: var(--transition);
}

.btn-icon:hover {
  background: var(--bg-primary);
  border-color: var(--primary-color);
  color: var(--primary-color);
}

.notice-box {
  margin-top: 1.5rem; 
  padding: 1rem; 
  background: rgba(59, 130, 246, 0.1);
  border-left: 3px solid var(--primary-color); 
  border-radius: 0 8px 8px 0;
  font-size: 0.875rem; 
  color: var(--text-muted);
  line-height: 1.5;
}

.notice-box strong {
  color: var(--text-main);
}

/* Chat Layout */
.app-container { 
  display: flex; 
  width: 100%; 
  height: 100%; 
}

/* Sidebar */
.sidebar {
  width: 340px; 
  border-right: 1px solid var(--border-color); 
  background: var(--bg-card); 
  display: flex; 
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-header { 
  padding: 1.25rem; 
  border-bottom: 1px solid var(--border-color); 
  display: flex; 
  justify-content: space-between; 
  align-items: center;
}

.sidebar-header h3 {
  font-size: 1.125rem;
  font-weight: 600;
}

.sidebar-header small {
  font-size: 0.75rem;
  color: var(--text-muted);
  display: block;
  margin-top: 0.25rem;
}

.teacher-list { 
  overflow-y: auto; 
  flex: 1; 
}

.teacher-card {
  display: flex; 
  align-items: center; 
  gap: 12px; 
  padding: 1rem; 
  cursor: pointer; 
  border-bottom: 1px solid var(--border-color); 
  transition: var(--transition);
}

.teacher-card:hover, 
.teacher-card.active { 
  background: rgba(59, 130, 246, 0.08); 
}

.teacher-card:last-child {
  border-bottom: none;
}

.avatar { 
  width: 48px; 
  height: 48px; 
  border-radius: 50%; 
  object-fit: cover; 
  background: var(--border-color);
  flex-shrink: 0;
}

.teacher-info {
  flex: 1;
  min-width: 0;
}

.teacher-name {
  font-weight: 600;
  font-size: 0.9375rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.teacher-subject {
  font-size: 0.8125rem; 
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.online-indicator { 
  width: 12px; 
  height: 12px; 
  background: var(--success-color); 
  border-radius: 50%; 
  border: 2px solid var(--bg-card); 
  flex-shrink: 0;
  margin-left: auto;
}

/* Main Chat Area */
.chat-main { 
  flex: 1; 
  display: flex; 
  flex-direction: column; 
  background: var(--bg-primary); 
  min-width: 0;
}

.chat-header { 
  padding: 1rem 1.5rem; 
  background: var(--bg-card); 
  border-bottom: 1px solid var(--border-color); 
  display: flex; 
  align-items: center; 
  gap: 12px;
  flex-shrink: 0;
}

.chat-header-placeholder {
  color: var(--text-muted);
  font-size: 1rem;
}

.chat-header-active {
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-header-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--border-color);
}

.chat-header-info {
  flex: 1;
  min-width: 0;
}

.chat-header-name {
  font-weight: 600;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-header-subject {
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.chat-header-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: var(--success-color);
}

.chat-messages { 
  flex: 1; 
  padding: 1.5rem; 
  overflow-y: auto; 
  display: flex; 
  flex-direction: column; 
  gap: 1rem;
  min-height: 0;
}

.msg-bubble { 
  max-width: 70%; 
  padding: 0.875rem 1.125rem; 
  border-radius: 16px; 
  font-size: 0.9375rem; 
  line-height: 1.5;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg-bubble.sent { 
  align-self: flex-end; 
  background: var(--chat-sent); 
  color: white; 
  border-bottom-right-radius: 4px;
}

.msg-bubble.received { 
  align-self: flex-start; 
  background: var(--chat-received); 
  color: var(--text-main); 
  border-bottom-left-radius: 4px;
}

.msg-bubble .msg-time {
  font-size: 0.6875rem;
  opacity: 0.7;
  margin-top: 0.375rem;
  text-align: right;
}

.msg-bubble.received .msg-time {
  text-align: left;
}

.msg-img { 
  max-width: 100%; 
  border-radius: 8px; 
  margin-top: 0.5rem; 
  cursor: pointer;
  transition: var(--transition);
}

.msg-img:hover {
  opacity: 0.9;
}

.msg-file {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem;
  background: rgba(0,0,0,0.05);
  border-radius: 8px;
  margin-top: 0.5rem;
}

.msg-bubble.received .msg-file {
  background: rgba(0,0,0,0.03);
}

.msg-file-icon {
  font-size: 1.5rem;
}

.msg-file-info {
  flex: 1;
  min-width: 0;
}

.msg-file-name {
  font-weight: 500;
  font-size: 0.875rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.msg-file-size {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.typing-indicator {
  padding: 0.5rem 1rem;
  font-size: 0.8125rem;
  color: var(--text-muted);
  font-style: italic;
  height: 20px;
}

/* Chat Footer */
.chat-footer { 
  padding: 1rem 1.5rem; 
  background: var(--bg-card); 
  border-top: 1px solid var(--border-color); 
  flex-shrink: 0;
}

.attachment-preview {
  display: flex; 
  align-items: center; 
  gap: 10px; 
  padding: 0.75rem 1rem; 
  background: var(--bg-primary); 
  border-radius: 8px; 
  margin-bottom: 0.75rem; 
  font-size: 0.875rem;
  border: 1px solid var(--border-color);
  animation: slideDown 0.2s ease;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.attachment-preview.hidden {
  display: none;
}

.attachment-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
  min-width: 0;
}

.attachment-icon {
  font-size: 1.25rem;
}

.attachment-name {
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.attachment-remove {
  background: none;
  border: none;
  color: var(--error-color);
  cursor: pointer;
  font-size: 1.25rem;
  padding: 0.25rem;
  line-height: 1;
  transition: var(--transition);
}

.attachment-remove:hover {
  transform: scale(1.1);
}

.input-row { 
  display: flex; 
  gap: 0.75rem; 
  align-items: center;
}

.input-row input[type="text"] { 
  flex: 1; 
  padding: 0.875rem 1rem; 
  border: 1px solid var(--border-color); 
  border-radius: 8px; 
  background: var(--bg-primary); 
  color: var(--text-main); 
  font-size: 1rem;
  transition: var(--transition);
}

.input-row input[type="text"]:focus {
  outline: none;
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}

.input-row input[type="file"] {
  display: none;
}

/* Responsive */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    box-shadow: var(--shadow-lg);
  }
  
  .sidebar.open {
    transform: translateX(0);
  }
  
  .chat-main {
    width: 100%;
  }
  
  .msg-bubble {
    max-width: 85%;
  }
  
  .login-card {
    margin: 1rem;
    padding: 1.5rem;
  }
}

/* Scrollbar styling */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}