:root {
  --blue: #2563eb;
  --blue-dark: #1d4ed8;
  --red: #dc2626;
  --orange: #d97706;
  --bg: #f4f5f7;
  --border: #e2e4e9;
  --text: #1f2430;
  --muted: #767c8c;
  --hover-tint: #dbeafe;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: 40px;
}

header {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 14px 16px;
  background: white;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-left { display: flex; align-items: center; }
.title-wrap { position: relative; display: inline-block; padding-right: 50px; }
header h1 { font-size: 18px; margin: 0; white-space: nowrap; }

.conn-status {
  position: absolute;
  top: -8px;
  right: 0;
  font-size: 10px;
  padding: 2px 7px;
  border-radius: 999px;
  background: #eee;
  color: var(--muted);
  white-space: nowrap;
}
.conn-status.online { background: #dcfce7; color: #15803d; }
.conn-status.offline { background: #fef3c7; color: #92400e; }

.presence-list {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
  flex: 1;
}
.presence-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 10.5px;
  background: #eef2ff;
  color: #3730a3;
  padding: 3px 8px;
  border-radius: 999px;
}
.presence-pill.me { font-weight: 700; box-shadow: inset 0 0 0 1.5px currentColor; }
.presence-pill .dot {
  width: 6px; height: 6px; border-radius: 50%;
  display: inline-block;
}
.presence-pill .dot.online { background: #22c55e; }
.presence-pill .dot.idle { background: #f59e0b; }
.presence-pill .session-count {
  font-size: 9px;
  opacity: 0.75;
  font-weight: 400;
}

/* custom tooltip, replaces native title/alt on presence pills */
.custom-tooltip {
  position: fixed;
  z-index: 60;
  background: #111827;
  color: white;
  font-size: 12px;
  padding: 5px 9px;
  border-radius: 6px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.25);
  pointer-events: none;
  white-space: nowrap;
}
.custom-tooltip::after {
  content: '';
  position: absolute;
  left: 50%;
  top: 100%;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-top-color: #111827;
}
.custom-tooltip.hidden { display: none; }

.header-right { margin-left: auto; display: flex; gap: 8px; align-items: center; }

.btn {
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
}
.btn.small, .btn.icon-btn {
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.btn.small { padding: 0 12px; font-size: 12px; }
.btn.icon-btn { padding: 0 10px; font-size: 15px; background: #f1f2f5; color: var(--text); }
.btn.icon-btn:hover { background: var(--hover-tint); }
.btn.danger { background: #fee2e2; color: var(--red); }
.btn.danger:hover { background: #fecaca; }
.btn.danger.armed { background: var(--red); color: white; }

main {
  max-width: 720px;
  margin: 20px auto;
  padding: 0 16px;
}

.rows { display: flex; flex-direction: column; gap: 6px; }

.row {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 6px;
  background: white;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 8px;
  transition: background 0.15s, box-shadow 0.15s;
}
.row:hover { background: #f7f8fa; }
.row.sortable-ghost { opacity: 0.4; }

.drag-handle {
  cursor: grab;
  color: var(--muted);
  padding: 8px 2px 4px;
  font-size: 16px;
  line-height: 1;
  user-select: none;
  touch-action: none;
  flex-shrink: 0;
}
.drag-handle:active { cursor: grabbing; }

.row-check {
  width: 19px;
  height: 19px;
  flex-shrink: 0;
  margin-top: 7px;
  accent-color: var(--blue);
  cursor: pointer;
}

.row-content { flex: 1; min-width: 0; }

.row-text, .row-display {
  width: 100%;
  border: none;
  outline: none;
  font-size: 15px;
  line-height: 1.4;
  padding: 8px 6px;
  background: transparent;
  color: var(--text);
  min-width: 0;
  font-family: inherit;
  font-weight: 400;
  font-style: normal;
  letter-spacing: normal;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  white-space: pre-wrap;
  word-wrap: break-word;
  resize: none;
  overflow: hidden;
  box-sizing: border-box;
  appearance: none;
  -webkit-appearance: none;
  margin: 0;
}
.row-display { cursor: text; border-radius: 6px; }
.row-display.placeholder { color: #b3b7c2; }
.row-display .word {
  position: relative;
  border-radius: 3px;
  padding: 0 1px;
}
.row-display .word:hover {
  background: var(--word-author-color, var(--hover-tint));
}
.row-display .word.word-highlighted:hover {
  filter: brightness(0.94);
}

/* remote user's live cursor position, shown at word granularity */
.word.word-cursor::before {
  content: '';
  position: absolute;
  left: -3px;
  top: -2px;
  bottom: -2px;
  width: 2px;
  background: var(--cursor-color, #666);
  animation: blink-caret 1s step-start infinite;
}
.word.word-cursor-after::after {
  content: '';
  position: absolute;
  right: -3px;
  top: -2px;
  bottom: -2px;
  width: 2px;
  background: var(--cursor-color, #666);
  animation: blink-caret 1s step-start infinite;
}
@keyframes blink-caret { 50% { opacity: 0; } }

.row.checked .row-text, .row.checked .row-display {
  text-decoration: line-through;
  color: var(--muted);
}
.row.fmt-bold .row-text, .row.fmt-bold .row-display { font-weight: 700; }
.row.fmt-italic .row-text, .row.fmt-italic .row-display { font-style: italic; }
.row.fmt-underline .row-text, .row.fmt-underline .row-display { text-decoration: underline; }
.row.fmt-strike .row-text, .row.fmt-strike .row-display { text-decoration: line-through; }
.row.fmt-highlight .row-display { background: transparent; }

.remote-chip {
  display: none;
  position: absolute;
  top: -9px;
  right: 8px;
  font-size: 10px;
  color: white;
  padding: 2px 7px;
  border-radius: 999px;
  white-space: nowrap;
}

.row-del {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
  opacity: 0.5;
  flex-shrink: 0;
  margin-top: 3px;
}
.row-del:hover { opacity: 1; background: #fee2e2; color: var(--red); }

.add-row-btn {
  margin-top: 12px;
  width: 100%;
  padding: 10px;
  border: 1px dashed var(--border);
  border-radius: 10px;
  background: white;
  color: var(--muted);
  font-size: 14px;
  cursor: pointer;
}
.add-row-btn:hover { background: var(--hover-tint); color: var(--blue); }

.settings-version {
  text-align: center;
  color: #c3c6cf;
  font-size: 11px;
  margin-top: 20px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

/* ---------- right-click formatting menu ---------- */
.ctx-menu {
  position: fixed;
  z-index: 55;
  min-width: 180px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(15,18,26,0.22), 0 0 0 1px var(--border);
  padding: 6px;
}
.ctx-menu.hidden { display: none; }
.ctx-scope-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  padding: 6px 8px 4px;
}
.ctx-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  padding: 8px 10px;
  font-size: 14px;
  border-radius: 6px;
  cursor: pointer;
  color: var(--text);
}
.ctx-item:hover { background: var(--hover-tint); }
.ctx-item.active { background: #e0e9ff; color: var(--blue-dark); font-weight: 600; }
.ctx-item.ctx-italic { font-style: italic; }
.ctx-item.ctx-underline { text-decoration: underline; }
.ctx-item.ctx-strike { text-decoration: line-through; }
.ctx-item.ctx-clear { color: var(--red); }
.ctx-item.ctx-clear:hover { background: #fee2e2; }
.ctx-sep { height: 1px; background: var(--border); margin: 6px 2px; }
.ctx-swatch-row { display: flex; gap: 6px; padding: 4px 8px 8px; }
.ctx-swatch {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid rgba(0,0,0,0.08);
  cursor: pointer;
  padding: 0;
}
.ctx-swatch:hover { transform: scale(1.12); box-shadow: 0 0 0 2px var(--hover-tint); }
.ctx-swatch-none {
  background: white;
  color: var(--muted);
  font-size: 13px;
  line-height: 22px;
}

.version-tag { display: none; } /* legacy, superseded by .settings-version */

.undo-toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: #111827;
  color: white;
  padding: 10px 14px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  z-index: 20;
}
.undo-toast.hidden { display: none; }
.undo-toast button {
  background: var(--blue);
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
}
.undo-timer { color: #9ca3af; font-size: 12px; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 18, 26, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 30;
}
.modal-overlay.visible { display: flex; }
.modal {
  background: white;
  border-radius: 14px;
  padding: 24px;
  width: 90%;
  max-width: 340px;
  text-align: center;
}
.modal h2 { margin: 0 0 6px; }
.modal p { color: var(--muted); font-size: 14px; margin: 0 0 16px; }
.modal input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  margin-bottom: 12px;
}
.modal button {
  width: 100%;
  padding: 10px;
  border: none;
  border-radius: 8px;
  background: var(--blue);
  color: white;
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
}
.modal button:hover { background: var(--blue-dark); }

.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 18, 26, 0.4);
  display: none;
  z-index: 39;
}
.settings-overlay.visible { display: block; }

.settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 300px;
  max-width: 85vw;
  background: white;
  box-shadow: -8px 0 24px rgba(0,0,0,0.15);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  z-index: 40;
  padding: 20px;
  display: flex;
  flex-direction: column;
}
.settings-panel.open { transform: translateX(0); }

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.settings-header h2 { margin: 0; font-size: 17px; }
.settings-close {
  border: none;
  background: transparent;
  font-size: 22px;
  cursor: pointer;
  color: var(--muted);
  line-height: 1;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-top: 1px solid var(--border);
}
.settings-row label { font-size: 14px; }
.settings-row input[type="checkbox"] {
  width: 38px;
  height: 22px;
  accent-color: var(--blue);
  cursor: pointer;
}
.settings-hint {
  font-size: 12px;
  color: var(--muted);
  margin-top: 4px;
}
.settings-hint.hint-warning {
  color: var(--orange);
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 8px;
  padding: 8px 10px;
  margin-top: 8px;
}
.settings-hint.hidden { display: none; }

@media (max-width: 480px) {
  header h1 { font-size: 16px; }
  body { padding-bottom: 96px; }

  .header-right { position: static; margin-left: auto; }

  #settingsBtn {
    position: fixed;
    right: 16px;
    bottom: 16px;
    z-index: 25;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    font-size: 20px;
    padding: 0;
    background: var(--blue);
    color: white;
    box-shadow: 0 4px 14px rgba(37,99,235,0.4);
  }
  #settingsBtn:hover { background: var(--blue-dark); }

  #clearBtn {
    position: fixed;
    right: 16px;
    bottom: 74px;
    z-index: 25;
    width: auto;
    box-shadow: 0 4px 14px rgba(0,0,0,0.15);
  }
}
