/* ======== CSS Variables ======== */
:root {
  --bg-deepest: #0d1117;
  --bg-deep: #161b22;
  --bg-medium: #21262d;
  --bg-light: #30363d;
  --text-secondary: #8b949e;
  --text-primary: #c9d1d9;
  --brand-red: #ec4141;
  --brand-green: #1db954;
  --brand-red-hover: #d43838;
  --brand-red-active: #c23030;
  --sidebar-width: 280px;
  --room-width: 320px;
  --radius: 8px;
  --radius-sm: 4px;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  --transition: 0.2s ease;
}

/* ======== Reset & Base ======== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg-deepest);
  color: var(--text-primary);
  overflow: hidden;
}

a { color: inherit; text-decoration: none; }
ul, ol { list-style: none; }
input, button, textarea { font-family: inherit; font-size: inherit; color: inherit; border: none; outline: none; background: none; }
button { cursor: pointer; }

/* ======== Scrollbar ======== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-light); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #484f58; }

/* ======== Layout ======== */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* Navbar */
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
  padding: 0 20px;
  background: var(--bg-deep);
  border-bottom: 1px solid var(--bg-light);
  flex-shrink: 0;
  z-index: 100;
}

.navbar .logo {
  font-size: 20px;
  font-weight: 700;
  color: var(--brand-red);
  letter-spacing: 1px;
}

.navbar .auth-section {
  display: flex;
  align-items: center;
  gap: 10px;
}

.navbar .user-nickname {
  font-size: 14px;
  color: var(--text-primary);
}

.btn {
  padding: 6px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  transition: all var(--transition);
}

.btn-primary {
  background: var(--brand-red);
  color: #fff;
}
.btn-primary:hover { background: var(--brand-red-hover); }
.btn-primary:active { background: var(--brand-red-active); }

.btn-outline {
  border: 1px solid var(--bg-light);
  color: var(--text-primary);
}
.btn-outline:hover { border-color: var(--text-secondary); background: var(--bg-medium); }

.btn-sm { padding: 4px 12px; font-size: 12px; }

/* Main Content */
.main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ======== Sidebar (Left) ======== */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-deep);
  border-right: 1px solid var(--bg-light);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar .search-box {
  padding: 16px;
  border-bottom: 1px solid var(--bg-light);
}

.sidebar .search-box input {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-deepest);
  border: 1px solid var(--bg-light);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 13px;
  transition: border-color var(--transition);
}
.sidebar .search-box input::placeholder { color: var(--text-secondary); }
.sidebar .search-box input:focus { border-color: var(--brand-red); }

.sidebar .search-results {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 0 12px;
  min-height: 0;
}

.sidebar .bookshelf {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 0 12px;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.sidebar .section-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 14px 4px 8px;
}

.book-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all var(--transition);
  margin-bottom: 4px;
}
.book-card:hover {
  background: var(--bg-medium);
  box-shadow: var(--shadow);
}

.book-card img {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  object-fit: cover;
  background: var(--bg-light);
  flex-shrink: 0;
}

.book-card .book-info {
  overflow: hidden;
  flex: 1;
  min-width: 0;
}

.book-card .book-info .book-name {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-card .book-info .book-meta {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ======== Player (Center) ======== */
.player-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  overflow-y: auto;
  background: linear-gradient(180deg, #1a0a0a 0%, var(--bg-deepest) 60%);
}

.player-cover {
  width: 300px;
  height: 300px;
  border-radius: 12px;
  object-fit: cover;
  background: var(--bg-medium);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  margin-bottom: 30px;
  transition: transform var(--transition);
}

.player-area .book-title {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 6px;
  text-align: center;
  max-width: 400px;
}

.player-area .chapter-title {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 24px;
  text-align: center;
  max-width: 400px;
}

/* Progress Bar */
.progress-container {
  width: 100%;
  max-width: 500px;
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

.progress-time {
  font-size: 11px;
  color: var(--text-secondary);
  min-width: 38px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.progress-bar-wrapper {
  flex: 1;
  height: 18px;
  display: flex;
  align-items: center;
  cursor: pointer;
  position: relative;
}

.progress-bar-bg {
  width: 100%;
  height: 4px;
  background: var(--bg-light);
  border-radius: 2px;
  position: relative;
  overflow: visible;
}

.progress-bar-fill {
  height: 100%;
  background: var(--brand-red);
  border-radius: 2px;
  transition: width 0.1s linear;
  position: relative;
}

.progress-bar-thumb {
  position: absolute;
  right: -7px;
  top: 50%;
  transform: translateY(-50%);
  width: 14px;
  height: 14px;
  background: var(--brand-red);
  border-radius: 50%;
  box-shadow: 0 0 4px rgba(236, 65, 65, 0.6);
  opacity: 0;
  transition: opacity var(--transition);
}
.progress-bar-wrapper:hover .progress-bar-thumb { opacity: 1; }

/* Controls */
.player-controls {
  display: flex;
  align-items: center;
  gap: 24px;
  margin-bottom: 16px;
}

.control-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all var(--transition);
  color: var(--text-primary);
  font-size: 18px;
}
.control-btn:hover { background: rgba(255,255,255,0.08); }
.control-btn:active { background: rgba(255,255,255,0.14); }

.play-btn {
  width: 56px;
  height: 56px;
  background: var(--brand-red);
  color: #fff;
  font-size: 22px;
  box-shadow: 0 2px 12px rgba(236, 65, 65, 0.4);
}
.play-btn:hover { background: var(--brand-red-hover); transform: scale(1.05); }
.play-btn:active { background: var(--brand-red-active); transform: scale(1); }

.speed-selector {
  font-size: 12px;
  color: var(--text-secondary);
  padding: 4px 10px;
  border: 1px solid var(--bg-light);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  background: var(--bg-deepest);
}
.speed-selector:hover { border-color: var(--text-secondary); }
.speed-selector.active { color: var(--brand-red); border-color: var(--brand-red); }

.speed-options {
  display: flex;
  gap: 4px;
}

/* Chapter List in Player */
.chapter-list-container {
  width: 100%;
  max-width: 500px;
  margin-top: 20px;
  max-height: 250px;
  overflow-y: auto;
  border-top: 1px solid var(--bg-light);
  padding-top: 12px;
}

.chapter-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  font-size: 13px;
}
.chapter-item:hover { background: var(--bg-medium); }
.chapter-item.playing { color: var(--brand-red); font-weight: 500; }

.chapter-item .chap-index {
  font-size: 11px;
  color: var(--text-secondary);
  margin-right: 8px;
}

/* ======== Room Panel (Right) ======== */
.room-panel {
  width: var(--room-width);
  background: var(--bg-deep);
  border-left: 1px solid var(--bg-light);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

.room-panel .room-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--bg-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.room-panel .room-header .room-name {
  font-size: 16px;
  font-weight: 600;
}

.room-panel .room-header .room-code {
  font-size: 11px;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 2px 8px;
  border: 1px solid var(--bg-light);
  border-radius: var(--radius-sm);
  transition: all var(--transition);
}
.room-panel .room-header .room-code:hover { border-color: var(--text-secondary); }

/* No Room State */
.no-room {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 40px 24px;
  text-align: center;
}

.no-room h3 {
  font-size: 16px;
  color: var(--text-secondary);
  font-weight: 400;
}

.room-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.room-form input {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-deepest);
  border: 1px solid var(--bg-light);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  transition: border-color var(--transition);
}
.room-form input::placeholder { color: var(--text-secondary); }
.room-form input:focus { border-color: var(--brand-red); }

.room-form .btn { width: 100%; text-align: center; padding: 10px; font-size: 14px; }

.divider-text {
  font-size: 12px;
  color: var(--text-secondary);
  position: relative;
  width: 100%;
  text-align: center;
}
.divider-text::before, .divider-text::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 35%;
  height: 1px;
  background: var(--bg-light);
}
.divider-text::before { left: 0; }
.divider-text::after { right: 0; }

/* In Room State */
.in-room {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.room-members {
  padding: 10px 16px;
  border-bottom: 1px solid var(--bg-light);
  flex-shrink: 0;
}

.room-members .members-title {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.member-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 0;
  font-size: 13px;
}

.member-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--brand-red);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  flex-shrink: 0;
}
.member-avatar.host { background: #f59e0b; }

.connection-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  padding: 4px 16px;
  color: var(--text-secondary);
}
.connection-status .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--brand-green);
}
.connection-status.disconnected .dot { background: #f85149; }

/* Chat */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.chat-bubble {
  max-width: 85%;
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.5;
  word-break: break-word;
}

.chat-bubble.self {
  align-self: flex-end;
  background: var(--brand-red);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-bubble.other {
  align-self: flex-start;
  background: var(--bg-medium);
  border-bottom-left-radius: 4px;
}

.chat-bubble .msg-sender {
  font-size: 11px;
  font-weight: 600;
  margin-bottom: 2px;
  opacity: 0.8;
}

.chat-bubble .msg-time {
  font-size: 10px;
  opacity: 0.6;
  margin-top: 4px;
  text-align: right;
}

.chat-input-area {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid var(--bg-light);
  flex-shrink: 0;
}

.chat-input-area input {
  flex: 1;
  padding: 8px 12px;
  background: var(--bg-deepest);
  border: 1px solid var(--bg-light);
  border-radius: 20px;
  color: var(--text-primary);
  font-size: 13px;
  transition: border-color var(--transition);
}
.chat-input-area input::placeholder { color: var(--text-secondary); }
.chat-input-area input:focus { border-color: var(--brand-red); }

.chat-input-area button {
  padding: 8px 16px;
  background: var(--bg-light);
  border-radius: 20px;
  font-size: 13px;
  color: var(--text-primary);
  transition: all var(--transition);
}
.chat-input-area button:hover { background: var(--brand-red); color: #fff; }

/* Toast */
.toast-container {
  position: fixed;
  top: 64px;
  right: 20px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  color: #fff;
  animation: toastIn 0.3s ease;
  max-width: 360px;
}
.toast.success { background: #238636; }
.toast.error { background: #da3633; }
.toast.info { background: #1f6feb; }

@keyframes toastIn {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}

/* Auth Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: var(--bg-deep);
  border: 1px solid var(--bg-light);
  border-radius: 12px;
  padding: 28px 24px;
  width: 380px;
  max-width: 90vw;
  box-shadow: 0 16px 48px rgba(0,0,0,0.5);
}

.modal h3 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 20px;
  text-align: center;
}

.modal .form-group {
  margin-bottom: 14px;
}

.modal .form-group label {
  display: block;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 5px;
}

.modal .form-group input {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-deepest);
  border: 1px solid var(--bg-light);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  transition: border-color var(--transition);
}
.modal .form-group input:focus { border-color: var(--brand-red); }

.modal .btn {
  width: 100%;
  text-align: center;
  padding: 10px;
  font-size: 14px;
  margin-top: 6px;
}

.modal .modal-footer {
  margin-top: 16px;
  text-align: center;
  font-size: 12px;
  color: var(--text-secondary);
}

.modal .modal-footer a {
  color: var(--brand-red);
  cursor: pointer;
}
.modal .modal-footer a:hover { text-decoration: underline; }

.modal .close-btn {
  position: absolute;
  top: 12px;
  right: 16px;
  font-size: 20px;
  color: var(--text-secondary);
  cursor: pointer;
}
.modal { position: relative; }

/* ======== Responsive ======== */
@media (max-width: 768px) {
  .main-content {
    flex-direction: column;
  }
  .sidebar {
    width: 100%;
    height: 200px;
    border-right: none;
    border-bottom: 1px solid var(--bg-light);
  }
  .room-panel {
    width: 100%;
    height: 300px;
    border-left: none;
    border-top: 1px solid var(--bg-light);
  }
  .player-area {
    flex: auto;
    padding: 20px;
  }
  .player-cover {
    width: 200px;
    height: 200px;
    margin-bottom: 16px;
  }
  .player-area .book-title {
    font-size: 18px;
  }
}

/* Hidden */
.hidden { display: none !important; }
