:root {
  --bg: #ffffff;
  --fg: #1e1e1e;
  --accent: #4f6bed;
  --accent-hover: #3d5bd9;
  --surface: #f5f5f5;
  --border: #e0e0e0;
  --toolbar-bg: #fafafa;
  --error: #dc2626;
  --success: #16a34a;
  --muted: #6b7280;
}

.theme-dark {
  --bg: #282c34;
  --fg: #abb2bf;
  --accent: #61afef;
  --accent-hover: #4d9de0;
  --surface: #21252b;
  --border: #3e4451;
  --toolbar-bg: #21252b;
  --error: #e06c75;
  --success: #98c379;
  --muted: #5c6370;
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  transition: background-color 0.2s, color 0.2s;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Toolbar */
.toolbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: var(--toolbar-bg);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  flex-wrap: wrap;
  transition: background-color 0.2s, border-color 0.2s;
}

.logo {
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--fg);
  text-decoration: none;
}
.logo:hover { text-decoration: none; color: var(--accent); }

.toolbar-select {
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--fg);
  font-size: 0.85rem;
  cursor: pointer;
}

.toolbar-btn {
  padding: 4px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--fg);
  font-size: 0.85rem;
  cursor: pointer;
  transition: background-color 0.15s;
}
.toolbar-btn:hover { background: var(--border); }
.toolbar-btn.copied { background: var(--success); color: #fff; border-color: var(--success); }

.toolbar-info {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  font-size: 0.85rem;
  color: var(--muted);
}

.toolbar-link {
  font-size: 0.85rem;
  color: var(--accent);
}

.toolbar-user {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--fg);
}

.room-code {
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 0.8rem;
  background: var(--surface);
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid var(--border);
}

/* Status dots */
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}
.status-dot.connected-webrtc { background: #16a34a; }
.status-dot.connected-relay { background: #15803d; }
.status-dot.reconnecting { background: #eab308; }
.status-dot.disconnected { background: var(--error); }

.status-text { font-size: 0.8rem; }

.user-count-wrapper { font-size: 0.8rem; }

/* Info button and dropdown */
.info-wrapper {
  position: relative;
}

.info-btn {
  font-size: 0.9rem;
  padding: 2px 6px;
  line-height: 1;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
}
.info-btn:hover { color: var(--accent); }

.info-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  min-width: 200px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  z-index: 20;
}

.info-dropdown-item {
  margin-bottom: 10px;
}

.info-label {
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  display: block;
  margin-bottom: 4px;
}

.info-dropdown-footer {
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

/* Guest dropdown */
.guest-wrapper {
  position: relative;
}

.guest-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 0;
  min-width: 160px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  z-index: 20;
}

.guest-dropdown-item {
  display: block;
  width: 100%;
  padding: 8px 14px;
  font-size: 0.85rem;
  color: var(--fg);
  background: none;
  border: none;
  text-align: left;
  cursor: pointer;
  text-decoration: none;
}
.guest-dropdown-item:hover {
  background: var(--border);
  text-decoration: none;
}

/* Chat button */
.chat-btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.chat-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: var(--error);
  color: #fff;
  font-size: 0.65rem;
  padding: 1px 5px;
  border-radius: 10px;
  min-width: 16px;
  text-align: center;
}

/* Chat panel */
.chat-panel {
  position: fixed;
  top: 48px;
  right: 0;
  width: 320px;
  height: calc(100vh - 48px);
  background: var(--surface);
  border-left: 1px solid var(--border);
  flex-direction: column;
  z-index: 15;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.95rem;
}

.chat-close {
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  color: var(--muted);
  padding: 0 4px;
}
.chat-close:hover { color: var(--fg); }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 10px 14px;
}

.chat-message { margin-bottom: 12px; }

.chat-message-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 2px;
}

.chat-message-user {
  font-weight: 600;
  font-size: 0.8rem;
}

.chat-message-time {
  font-size: 0.7rem;
  color: var(--muted);
}

.chat-message-body {
  font-size: 0.85rem;
  line-height: 1.4;
  word-break: break-word;
}

.chat-input-wrapper {
  display: flex;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
}

.chat-input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  font-size: 0.85rem;
}
.chat-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Editor */
#editor {
  height: calc(100vh - 48px);
  transition: width 0.2s;
}
.chat-open #editor {
  width: calc(100% - 320px);
}
#editor .cm-editor { height: 100%; }
#editor .cm-scroller { overflow: auto; }

/* Cursor presence */
.cm-ySelectionInfo {
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
  opacity: 0.9;
}

/* Auth container */
.auth-container {
  max-width: 400px;
  margin: 80px auto;
  padding: 0 20px;
}

.auth-container h1 {
  text-align: center;
  margin-bottom: 32px;
}

.auth-container h2 {
  margin-top: 0;
  font-size: 1.3rem;
}

.auth-container form {
  background: var(--surface);
  padding: 24px;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.auth-container input {
  width: 100%;
  padding: 10px 12px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--fg);
  font-size: 0.95rem;
}
.auth-container input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.auth-container p {
  text-align: center;
  font-size: 0.9rem;
  color: var(--muted);
  margin-top: 16px;
}

.auth-footer {
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 32px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.auth-error {
  background: #fef2f2;
  color: var(--error);
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 0.9rem;
  margin-bottom: 16px;
  border: 1px solid #fecaca;
}
.theme-dark .auth-error {
  background: #2d1b1b;
  border-color: #5c2020;
}

/* Primary button */
.btn-primary {
  width: 100%;
  padding: 10px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 0.15s;
}
.btn-primary:hover { background: var(--accent-hover); }

/* Dashboard */
.dashboard-container {
  max-width: 800px;
  margin: 24px auto;
  padding: 0 20px;
}

.dashboard-container h1 {
  font-size: 1.5rem;
  margin-bottom: 16px;
}

.dashboard-container .btn-primary {
  width: auto;
  padding: 8px 20px;
  margin-bottom: 24px;
}

.doc-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 12px;
  transition: box-shadow 0.15s, border-color 0.15s;
}
.doc-card:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border-color: var(--accent);
}

.doc-info { flex: 1; }

.doc-title {
  margin: 0 0 4px;
  font-size: 1.05rem;
}

.doc-meta {
  font-size: 0.8rem;
  color: var(--muted);
}

.doc-persist {
  display: inline-block;
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 12px;
  margin-left: 8px;
  background: var(--border);
  color: var(--muted);
}
.doc-persist.active {
  background: #dbeafe;
  color: var(--accent);
}
.theme-dark .doc-persist.active {
  background: #1e3a5f;
}

.doc-actions {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.doc-actions button {
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--fg);
  font-size: 0.8rem;
  cursor: pointer;
  transition: background-color 0.15s;
}
.doc-actions button:hover { background: var(--surface); }

.btn-delete:hover { color: var(--error); border-color: var(--error); }

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 48px 0;
  font-size: 1rem;
}

/* Focus states */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Responsive */
@media (max-width: 768px) {
  .toolbar {
    gap: 8px;
    padding: 8px 12px;
  }

  .toolbar-info { margin-left: 0; width: 100%; justify-content: space-between; }

  .toolbar-select { flex: 1; }

  .doc-card { flex-direction: column; align-items: stretch; gap: 12px; }

  .doc-actions { flex-wrap: wrap; }

  .chat-panel { width: 100%; }
  .chat-open #editor { width: 0; overflow: hidden; }
}
