/* ---------- Presence bar (avatar stack, top right) ---------- */

.collab-presence {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin-right: 6px;
}

.collab-floating {
  position: fixed;
  top: 14px;
  right: 18px;
  z-index: 40;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
}

.collab-status {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--border-strong);
  flex: 0 0 auto;
}

.collab-status.online {
  background: #2f9e44;
  box-shadow: 0 0 0 3px rgba(47, 158, 68, 0.15);
}

.collab-status.connecting {
  background: #f08c00;
  animation: collab-pulse 1.2s ease-in-out infinite;
}

.collab-status.offline {
  background: var(--danger);
}

@keyframes collab-pulse {
  50% {
    opacity: 0.35;
  }
}

.collab-avatars {
  display: flex;
  align-items: center;
  flex-direction: row;
}

.collab-avatar-wrap {
  position: relative;
  margin-left: -6px;
  cursor: default;
  border-radius: 50%;
  outline: none;
}

.collab-avatar-wrap:first-child {
  margin-left: 0;
}

.collab-avatar-wrap.self,
.collab-avatar-wrap[data-more="true"] {
  cursor: pointer;
}

.collab-avatar-wrap.self {
  margin-left: 6px;
}

.collab-avatar {
  --user-color: #868e96;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--user-color);
  color: #fff;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  overflow: hidden;
  border: 2px solid var(--surface);
  box-shadow: 0 0 0 2px var(--user-color);
  user-select: none;
  transition: transform 0.12s ease;
}

.collab-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.collab-avatar-wrap:hover .collab-avatar,
.collab-avatar-wrap:focus-visible .collab-avatar {
  transform: translateY(-2px);
  z-index: 2;
}

.collab-avatar-more {
  background: var(--surface-subtle);
  color: var(--text);
  box-shadow: 0 0 0 2px var(--border-strong);
}

.collab-avatar.small {
  width: 24px;
  height: 24px;
  font-size: 0.62rem;
  border-width: 1px;
}

.collab-avatar.large {
  width: 46px;
  height: 46px;
  font-size: 1rem;
}

.collab-tooltip {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 50;
  min-width: max-content;
  padding: 6px 9px;
  border-radius: 7px;
  background: #343a40;
  color: #fff;
  font-size: 0.78rem;
  line-height: 1.35;
  opacity: 0;
  pointer-events: none;
  transform: translateY(-3px);
  transition: opacity 0.12s ease, transform 0.12s ease;
}

.collab-tooltip small {
  color: #ced4da;
}

.collab-avatar-wrap:hover .collab-tooltip,
.collab-avatar-wrap:focus-visible .collab-tooltip {
  opacity: 1;
  transform: none;
}

/* ---------- Account menu ---------- */

.collab-menu {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  z-index: 60;
  width: 290px;
  display: grid;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.16);
  text-align: left;
}

.collab-menu[hidden] {
  display: none;
}

.collab-menu-head {
  display: flex;
  align-items: center;
  gap: 12px;
}

.collab-menu-head div {
  display: grid;
}

.collab-menu-head small,
.collab-people small {
  color: var(--text-muted);
}

.collab-menu-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.collab-menu-actions .button,
.collab-menu > .button {
  padding: 0.45rem 0.7rem;
  font-size: 0.86rem;
}

.collab-menu-section {
  margin: 0;
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 0.72rem;
}

.collab-people {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
  max-height: 180px;
  overflow: auto;
}

.collab-people li {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.88rem;
}

.collab-menu-links {
  display: flex;
  gap: 12px;
  font-size: 0.84rem;
}

.collab-menu-error {
  margin: 0;
  color: var(--danger);
  font-size: 0.84rem;
}

/* ---------- Remote cursors ---------- */

.collab-cursor-layer {
  position: fixed;
  inset: 0;
  z-index: 45;
  pointer-events: none;
  overflow: hidden;
}

.collab-cursor {
  --user-color: #868e96;
  position: absolute;
  top: 0;
  left: 0;
  transition: transform 0.06s linear;
  will-change: transform;
}

.collab-cursor[hidden] {
  display: none;
}

.collab-cursor-arrow {
  display: block;
  width: 18px;
  height: 22px;
  fill: var(--user-color);
  stroke: #fff;
  stroke-width: 1.4;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.25));
}

.collab-cursor-label {
  position: absolute;
  top: 18px;
  left: 12px;
  padding: 2px 7px;
  border-radius: 4px 9px 9px 9px;
  background: var(--user-color);
  color: #fff;
  font-size: 0.74rem;
  font-weight: 600;
  white-space: nowrap;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.18);
}

.collab-cursor.clipped {
  opacity: 0.45;
}

/* ---------- Atlas: who is where ---------- */

.presence-layer {
  pointer-events: none;
}

.presence-layer .presence-ring {
  fill: none;
  stroke-width: 3;
  pointer-events: none;
}

.presence-layer .presence-tag rect {
  rx: 6;
  ry: 6;
}

.presence-layer .presence-tag text {
  fill: #fff;
  font-size: 11px;
  font-weight: 600;
}

#lock-banner[hidden] {
  display: none;
}

.lock-banner {
  --user-color: #868e96;
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--user-color) 35%, transparent);
  background: color-mix(in srgb, var(--user-color) 9%, var(--surface));
  font-size: 0.88rem;
  line-height: 1.4;
}

.lock-banner.mine {
  --user-color: #2f9e44;
}

.lock-banner strong {
  color: var(--user-color);
}

.editor.readonly input,
.editor.readonly textarea {
  background: var(--surface-subtle);
  cursor: not-allowed;
}

.field.peer-focus {
  position: relative;
}

.field.peer-focus input,
.field.peer-focus textarea {
  border-color: var(--peer-color);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--peer-color) 25%, transparent);
}

.peer-focus-tag {
  position: absolute;
  top: -4px;
  right: 0;
  max-width: 70%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--peer-color);
  color: #fff !important;
  font-size: 0.72rem !important;
  font-weight: 600;
}

.field-block.peer-focus .peer-focus-tag {
  right: 40px;
  top: 4px;
}

.result-card .peer-dots,
.chapter-card .peer-dots {
  display: inline-flex;
  gap: 3px;
  margin-left: 6px;
  vertical-align: middle;
}

.peer-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--user-color);
}

/* ---------- Login page ---------- */

body.auth-page {
  display: grid;
  place-items: center;
  min-height: 100dvh;
  overflow: auto;
  background: var(--surface-subtle);
}

.auth-card {
  width: min(420px, 100%);
  padding: 28px;
}

.auth-card h1 {
  margin: 0;
  font-size: 1.4rem;
  letter-spacing: -0.02em;
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin: 22px 0 16px;
  border-bottom: 1px solid var(--border);
}

.auth-tab {
  appearance: none;
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  padding: 10px 0;
  color: var(--text-muted);
  cursor: pointer;
}

.auth-tab.active {
  border-bottom-color: var(--accent);
  color: var(--text);
  font-weight: 600;
}

.auth-form {
  display: grid;
  gap: 14px;
}

.auth-form .field[hidden] {
  display: none;
}

.auth-form .button {
  margin-top: 4px;
}

.auth-error {
  margin: 0;
  padding: 9px 11px;
  border-radius: 8px;
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 0.88rem;
}

.auth-error[hidden] {
  display: none;
}

.topbar .toolbar {
  align-items: center;
  justify-content: flex-end;
}

/* ---------- Edit history drawer ---------- */

.history-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 42;
  width: min(500px, 100vw);
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  background: var(--surface);
  border-left: 1px solid var(--border-strong);
  box-shadow: -18px 0 48px rgba(0, 0, 0, 0.12);
}

.history-drawer[hidden] {
  display: none;
}

.history-head { grid-row: 1; }
.history-tabs { grid-row: 2; }
.history-filters { grid-row: 3; }
.history-body { grid-row: 4; }
.history-foot { grid-row: 5; }

.history-head {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 20px 8px;
}

.history-head h2 {
  margin: 0;
  font-size: 1.1rem;
}

.history-tabs {
  display: flex;
  gap: 4px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
}

.history-tabs button {
  appearance: none;
  border: 0;
  border-bottom: 2px solid transparent;
  background: none;
  padding: 10px 10px;
  color: var(--text-muted);
  cursor: pointer;
  font: inherit;
  font-size: 0.9rem;
}

.history-tabs button.active {
  border-bottom-color: var(--accent);
  color: var(--text);
  font-weight: 600;
}

.history-filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.history-filters[hidden] {
  display: none;
}

.history-filters input[type="search"] {
  grid-column: 1 / -1;
}

.history-check {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.86rem;
  color: var(--text-muted);
}

.history-check input {
  width: auto;
}

.history-body {
  overflow: auto;
  padding: 4px 16px 16px;
}

.history-foot {
  display: grid;
  gap: 2px;
  padding: 10px 16px 14px;
  border-top: 1px solid var(--border);
  font-size: 0.82rem;
}

.history-foot-note,
.history-muted {
  color: var(--text-muted);
  font-size: 0.82rem;
  margin: 0;
}

.history-day {
  position: sticky;
  top: 0;
  z-index: 1;
  margin: 0;
  padding: 12px 0 6px;
  background: var(--surface);
  color: var(--text-muted);
  font-family: var(--font-mono);
  font-size: 0.74rem;
  font-weight: 500;
}

.history-scope {
  margin: 12px 0 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--surface-subtle);
  color: var(--text-muted);
  font-size: 0.84rem;
}

.history-empty {
  margin: 28px 4px;
  color: var(--text-muted);
  text-align: center;
}

.history-empty.error {
  color: var(--danger);
}

.history-entry {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 10px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.history-entry.is-reverted .history-summary,
.history-entry.is-reverted .history-diff {
  opacity: 0.55;
}

.history-entry.is-reverted .history-summary {
  text-decoration: line-through;
}

.history-system {
  background: var(--surface-subtle);
  color: var(--text-muted);
  box-shadow: 0 0 0 2px var(--border-strong);
}

.history-main {
  display: grid;
  gap: 5px;
  min-width: 0;
}

.history-line {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.88rem;
}

.history-line time {
  color: var(--text-muted);
  font-size: 0.78rem;
  white-space: nowrap;
}

.history-summary {
  margin: 0;
  font-size: 0.9rem;
  line-height: 1.4;
}

.history-node {
  justify-self: start;
  appearance: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-subtle);
  padding: 2px 7px;
  color: var(--accent-strong);
  font-family: var(--font-mono);
  font-size: 0.74rem;
  cursor: pointer;
  text-align: left;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-node:hover {
  border-color: var(--accent);
}

.history-name {
  color: var(--text-muted);
  font-size: 0.84rem;
}

.history-diff {
  padding: 7px 9px;
  border-radius: 8px;
  background: var(--surface-subtle);
  font-size: 0.85rem;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.history-diff.small {
  margin-top: 4px;
  padding: 5px 7px;
  font-size: 0.8rem;
}

.history-diff.collapsed {
  max-height: 7.5em;
  overflow: hidden;
  mask-image: linear-gradient(#000 70%, transparent);
}

.history-diff del {
  background: rgba(220, 50, 47, 0.13);
  color: #a61e1b;
  text-decoration: line-through;
}

.history-diff ins {
  background: rgba(47, 158, 68, 0.15);
  color: #1e6b2e;
  text-decoration: none;
}

.history-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.history-tag {
  display: inline-flex;
  padding: 1px 8px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.74rem;
}

.history-tag.undo {
  border-color: rgba(82, 173, 200, 0.4);
  color: var(--accent-strong);
}

.history-tag.reverted {
  border-color: rgba(203, 75, 22, 0.3);
  color: var(--warning);
}

.history-tag.online {
  border-color: rgba(47, 158, 68, 0.35);
  color: #2f9e44;
}

.history-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.history-actions .button {
  padding: 0.32rem 0.6rem;
  font-size: 0.8rem;
  text-decoration: none;
}

.history-more {
  width: 100%;
  margin-top: 12px;
}

.history-person {
  display: grid;
  gap: 10px;
  margin-top: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
}

.history-person:hover,
.history-person:focus-visible {
  border-color: var(--accent);
}

.history-person-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.history-stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  margin: 0;
}

.history-stats div {
  padding: 6px;
  border-radius: 8px;
  background: var(--surface-subtle);
  text-align: center;
}

.history-stats dt {
  color: var(--text-muted);
  font-size: 0.7rem;
}

.history-stats dd {
  margin: 0;
  font-weight: 600;
}

.history-modal {
  z-index: 60;
}

.history-modal-card {
  width: min(640px, 100%);
  grid-template-rows: auto minmax(0, 1fr) auto;
}

.history-modal-body {
  display: grid;
  gap: 10px;
  overflow: auto;
  min-height: 0;
  align-content: start;
}

.history-alert {
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 0.88rem;
}

.history-alert p {
  margin: 4px 0 0;
}

.history-alert.warn {
  background: var(--warning-soft);
  color: var(--warning);
}

.history-alert.error {
  background: var(--danger-soft);
  color: var(--danger);
}

.history-counts {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.history-count {
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--surface-subtle);
  border: 1px solid var(--border-strong);
  font-size: 0.8rem;
}

.history-count.add {
  color: #2f9e44;
}

.history-count.remove {
  color: var(--danger);
}

.history-preview {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: 0.86rem;
}

.history-preview li {
  display: grid;
  grid-template-columns: 22px minmax(0, 1fr);
  gap: 6px;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.history-preview-icon {
  font-weight: 700;
  text-align: center;
}

.history-preview li.add .history-preview-icon {
  color: #2f9e44;
}

.history-preview li.remove .history-preview-icon {
  color: var(--danger);
}

.link-button {
  appearance: none;
  border: 0;
  background: none;
  padding: 0;
  justify-self: start;
  color: var(--accent-strong);
  text-decoration: underline;
  cursor: pointer;
  font: inherit;
  font-size: 0.8rem;
}

#history-button[aria-expanded="true"] {
  border-color: var(--accent);
  color: var(--accent-strong);
}

@media (max-width: 560px) {
  .history-filters {
    grid-template-columns: 1fr;
  }

  .history-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
