/* =========================================================
   Loop — collaborative document editor styles
   ========================================================= */

:root {
  --bg-base:        #ffffff;
  --bg-surface:     #f7f7f8;
  --bg-elevated:    #ffffff;
  --text-primary:   #0f0f10;
  --text-secondary: #5a5a6e;
  --text-muted:     #9898a8;
  --accent:         #6366f1;
  --accent-hover:   #4f46e5;
  --accent-subtle:  #eef2ff;
  --border:         #e4e4e7;
  --border-subtle:  #f0f0f2;
  --danger:         #ef4444;

  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.10);

  --sidebar-width: 260px;
  --header-height: 52px;
  --toolbar-height: 44px;

  color-scheme: light;
}

[data-theme="dark"] {
  --bg-base:        #111113;
  --bg-surface:     #1a1a1f;
  --bg-elevated:    #222228;
  --text-primary:   #ededef;
  --text-secondary: #9898a8;
  --text-muted:     #5a5a6e;
  --accent:         #818cf8;
  --accent-hover:   #6366f1;
  --accent-subtle:  #1e1b4b;
  --border:         #2e2e38;
  --border-subtle:  #232328;
  --danger:         #f87171;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.5);

  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg-base);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button {
  font-family: inherit;
  cursor: pointer;
  background: none;
  border: none;
  color: inherit;
  padding: 0;
}

input {
  font-family: inherit;
}

a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-color: transparent;
  text-underline-offset: 2px;
  transition: text-decoration-color 150ms ease;
}
a:hover { text-decoration-color: var(--accent); }

/* Scrollbars */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 2px;
}

/* ============================================================
   Layout
   ============================================================ */

#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ============================================================
   Sidebar
   ============================================================ */

.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: width 200ms ease, transform 200ms ease;
  overflow: hidden;
  flex-shrink: 0;
}

.sidebar.collapsed {
  width: 0;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border-subtle);
  min-height: var(--header-height);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 15px;
  color: var(--text-primary);
}

.brand-dot {
  width: 10px;
  height: 10px;
  background: var(--accent);
  border-radius: 50%;
  display: inline-block;
}

.sidebar-actions {
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  transition: background 150ms ease, color 150ms ease;
  width: 100%;
  text-align: left;
}
.action-btn:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}
.action-btn.slim {
  padding: 4px 10px;
  font-size: 12px;
}

.tree {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 12px;
}

.tree-item {
  position: relative;
  user-select: none;
}

.tree-row {
  display: flex;
  align-items: center;
  height: 32px;
  padding: 0 8px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text-secondary);
  gap: 6px;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
  position: relative;
}

.tree-row:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.tree-row.active {
  background: var(--accent-subtle);
  color: var(--accent);
  font-weight: 500;
}

.tree-row.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 4px;
  bottom: 4px;
  width: 2px;
  background: var(--accent);
  border-radius: 1px;
}

.tree-chevron {
  width: 14px;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 150ms ease;
  color: var(--text-muted);
  flex-shrink: 0;
}

.tree-chevron.open {
  transform: rotate(90deg);
}

.tree-chevron.placeholder {
  visibility: hidden;
}

.tree-icon {
  flex-shrink: 0;
  color: var(--text-muted);
  display: flex;
  align-items: center;
}

.tree-row.active .tree-icon {
  color: var(--accent);
}

.tree-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tree-label input {
  border: 1px solid var(--accent);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 13px;
  border-radius: var(--radius-sm);
  padding: 2px 6px;
  width: 100%;
  outline: none;
}

.tree-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  opacity: 0;
  transition: opacity 120ms ease;
}

.tree-row:hover .tree-actions {
  opacity: 1;
}

.tree-action {
  width: 22px;
  height: 22px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}
.tree-action:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}

.tree-children {
  display: none;
}

.tree-item.open > .tree-children {
  display: block;
}

.tree-row[data-indent="1"] { padding-left: 24px; }
.tree-row[data-indent="2"] { padding-left: 40px; }
.tree-row[data-indent="3"] { padding-left: 56px; }
.tree-row[data-indent="4"] { padding-left: 72px; }
.tree-row[data-indent="5"] { padding-left: 88px; }

.tree-empty {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  padding: 16px 8px;
}

/* Context popover from "..." button */
.context-menu {
  position: absolute;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  min-width: 160px;
  padding: 4px;
  z-index: 100;
}
.context-menu button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 7px 10px;
  font-size: 13px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  transition: background 120ms ease, color 120ms ease;
}
.context-menu button:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}
.context-menu button.danger {
  color: var(--danger);
}

.sidebar-foot {
  border-top: 1px solid var(--border-subtle);
  padding: 10px 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.workspace-info {
  display: flex;
  flex-direction: column;
  padding: 0 4px;
}
.ws-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}
.ws-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ============================================================
   Main area
   ============================================================ */

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-base);
}

.header {
  height: var(--header-height);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  flex-shrink: 0;
}

.header-title {
  flex: 1;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  transition: background 120ms ease, color 120ms ease;
}
.icon-btn:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}

/* Hamburger toggles sidebar on all viewports */
.hamburger {
  display: flex;
}

/* Live status pill */
.live-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px 4px 8px;
  border-radius: 999px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  font-size: 11px;
  font-weight: 500;
  color: var(--text-secondary);
  user-select: none;
  cursor: default;
  transition: background 150ms ease, color 150ms ease, border-color 150ms ease;
  white-space: nowrap;
}
.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-muted);
  position: relative;
  flex-shrink: 0;
}
.live-status[data-state="connected"] {
  color: #047857;
  border-color: rgba(16, 185, 129, 0.3);
  background: rgba(16, 185, 129, 0.08);
}
.live-status[data-state="connected"] .live-dot {
  background: #10b981;
  box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.6);
  animation: livePulse 2s ease-out infinite;
}
.live-status[data-state="connecting"] {
  color: #b45309;
  border-color: rgba(245, 158, 11, 0.3);
  background: rgba(245, 158, 11, 0.08);
}
.live-status[data-state="connecting"] .live-dot {
  background: #f59e0b;
  animation: liveBlink 1s ease-in-out infinite;
}
.live-status[data-state="disconnected"] {
  color: #b91c1c;
  border-color: rgba(239, 68, 68, 0.35);
  background: rgba(239, 68, 68, 0.1);
  cursor: pointer;
}
.live-status[data-state="disconnected"] .live-dot {
  background: var(--danger);
  animation: liveBlink 0.8s ease-in-out infinite;
}
[data-theme="dark"] .live-status[data-state="connected"]   { color: #6ee7b7; }
[data-theme="dark"] .live-status[data-state="connecting"]  { color: #fcd34d; }
[data-theme="dark"] .live-status[data-state="disconnected"]{ color: #fca5a5; }

@keyframes livePulse {
  0%   { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.55); }
  70%  { box-shadow: 0 0 0 6px rgba(16, 185, 129, 0);  }
  100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0);    }
}
@keyframes liveBlink {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.4; }
}

@media (max-width: 768px) {
  .live-status .live-label { display: none; }
  .live-status { padding: 6px; }
}

.avatars {
  display: flex;
  align-items: center;
}
.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  color: #fff;
  border: 2px solid var(--bg-base);
  margin-left: -6px;
  cursor: default;
  position: relative;
}
.avatar:first-child { margin-left: 0; }
.avatar[data-tooltip]:hover::after {
  content: attr(data-tooltip);
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-primary);
  color: var(--bg-base);
  font-size: 11px;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  white-space: nowrap;
  pointer-events: none;
  z-index: 50;
}

/* ============================================================
   Toolbar
   ============================================================ */

.toolbar {
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--border);
  padding: 6px 12px;
  gap: 4px;
  flex-shrink: 0;
  overflow-x: auto;
}

.tb-group {
  display: flex;
  align-items: center;
  gap: 2px;
}

.tb-sep {
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 6px;
  flex-shrink: 0;
}

.tb-btn {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  transition: background 120ms ease, color 120ms ease;
}
.tb-btn:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}
.tb-btn.active {
  background: var(--accent-subtle);
  color: var(--accent);
}

/* ============================================================
   Editor
   ============================================================ */

.editor-wrap {
  flex: 1;
  overflow-y: auto;
  position: relative;
  background: var(--bg-surface);
}

.editor-page {
  max-width: 780px;
  margin: 28px auto 80px;
  padding: 56px 64px 96px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  min-height: calc(100vh - var(--header-height) - var(--toolbar-height) - 140px);
}

@media (max-width: 768px) {
  .editor-page {
    margin: 12px;
    padding: 28px 20px 60px;
    border-radius: var(--radius-md);
  }
}

/* Editor host = relative wrapper for cursor overlay */
.editor-host {
  position: relative;
}

/* ===== Remote cursors ===== */
.cursor-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 2;
}
.remote-cursor {
  position: absolute;
  width: 2px;
  pointer-events: none;
  transition: top 100ms linear, left 100ms linear;
}
.remote-cursor-label {
  position: absolute;
  top: -16px;
  left: -1px;
  font-size: 10px;
  font-weight: 600;
  color: #fff;
  padding: 1px 5px;
  border-radius: 3px 3px 3px 0;
  white-space: nowrap;
  line-height: 1.2;
  font-family: 'Inter', sans-serif;
  letter-spacing: 0.01em;
  opacity: 0.95;
  user-select: none;
}
.remote-selection {
  position: absolute;
  pointer-events: none;
  opacity: 0.22;
  border-radius: 2px;
}

/* ===== Sidebar search ===== */
.sidebar-search {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 8px 10px 0;
  padding: 6px 10px;
  border-radius: var(--radius-md);
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  color: var(--text-muted);
  transition: border-color 150ms ease;
}
.sidebar-search:focus-within {
  border-color: var(--accent);
}
.sidebar-search input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  font-size: 12px;
  color: var(--text-primary);
}
.sidebar-search input::placeholder { color: var(--text-muted); }

.tree-row.search-hit {
  background: rgba(245, 158, 11, 0.08);
}
[data-theme="dark"] .tree-row.search-hit {
  background: rgba(245, 158, 11, 0.18);
}

/* ===== Table picker ===== */
.table-picker {
  position: fixed;
  z-index: 95;
  padding: 8px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  user-select: none;
}
.table-picker-grid {
  display: grid;
  grid-template-columns: repeat(10, 18px);
  grid-template-rows: repeat(8, 18px);
  gap: 2px;
}
.tp-cell {
  width: 18px;
  height: 18px;
  border: 1px solid var(--border);
  border-radius: 2px;
  background: var(--bg-base);
  transition: background 80ms ease, border-color 80ms ease;
  cursor: pointer;
}
.tp-cell.active {
  background: var(--accent-subtle);
  border-color: var(--accent);
}
.table-picker-label {
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-top: 6px;
}

/* ===== Table operations bubble ===== */
.table-bubble {
  position: fixed;
  z-index: 80;
  display: flex;
  align-items: center;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: 4px;
  gap: 2px;
}
.table-bubble button {
  min-width: 28px;
  height: 26px;
  padding: 0 6px;
  border-radius: var(--radius-sm);
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  background: none;
  border: none;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
}
.table-bubble button:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}
.table-bubble button.danger {
  color: var(--danger);
}
.table-bubble button.danger:hover {
  background: rgba(239, 68, 68, 0.1);
}

/* ===== Image resize ===== */
.tt-image-wrap {
  position: relative;
  display: inline-block;
  max-width: 100%;
  margin: 0.6em 0;
  line-height: 0;
  vertical-align: top;
}
.tt-image-wrap img {
  max-width: 100%;
  height: auto;
  display: block;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
}
.tt-image-wrap.selected {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-md);
}
.tt-image-handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: var(--accent);
  border: 2px solid #fff;
  border-radius: 50%;
  z-index: 5;
  display: none;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}
.tt-image-wrap.selected .tt-image-handle { display: block; }
.tt-image-handle.tl { top: -7px; left: -7px; cursor: nwse-resize; }
.tt-image-handle.tr { top: -7px; right: -7px; cursor: nesw-resize; }
.tt-image-handle.bl { bottom: -7px; left: -7px; cursor: nesw-resize; }
.tt-image-handle.br { bottom: -7px; right: -7px; cursor: nwse-resize; }

/* ===== Code block with line numbers ===== */
.code-block-wrap {
  position: relative;
  display: flex;
  background: var(--bg-surface);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
  margin: 0.8em 0;
  overflow: hidden;
  font-family: 'JetBrains Mono', Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.6;
}
.code-block-gutter {
  flex-shrink: 0;
  padding: 14px 10px 14px 12px;
  border-right: 1px solid var(--border-subtle);
  background: var(--bg-base);
  color: var(--text-muted);
  text-align: right;
  font-feature-settings: 'tnum';
  user-select: none;
  min-width: 2.5em;
}
.code-block-gutter span {
  display: block;
  height: 1.6em;
}
.code-block-wrap pre {
  flex: 1;
  padding: 14px 16px !important;
  margin: 0 !important;
  background: transparent !important;
  border: none !important;
  border-radius: 0 !important;
  overflow-x: auto;
  white-space: pre;
}
.code-block-wrap pre code {
  background: transparent !important;
  border: none !important;
  padding: 0 !important;
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  color: var(--text-primary);
  display: block;
  white-space: pre;
}

/* Language badge (top right corner) */
.code-block-lang {
  position: absolute;
  top: 4px;
  right: 8px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  user-select: none;
  pointer-events: none;
}

/* Keyboard hint floating panel */
.kbd-hint {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 4px 10px;
  box-shadow: var(--shadow-sm);
  opacity: 0.7;
  pointer-events: none;
  z-index: 50;
}
.kbd-hint kbd {
  background: var(--bg-surface);
  padding: 1px 5px;
  border-radius: 3px;
  border: 1px solid var(--border);
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--text-primary);
}

/* Drag-and-drop overlay for image paste/drop */
.drop-zone-active {
  outline: 2px dashed var(--accent);
  outline-offset: -8px;
  background: var(--accent-subtle);
  border-radius: var(--radius-lg);
}

.page-title {
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  font-size: 36px;
  font-weight: 700;
  color: var(--text-primary);
  font-family: inherit;
  padding: 0 0 16px;
  margin: 0;
  line-height: 1.2;
}
.page-title::placeholder { color: var(--text-muted); }

.editor {
  font-size: 16px;
  line-height: 1.75;
  color: var(--text-primary);
  min-height: 200px;
}

.editor:focus { outline: none; }

.ProseMirror {
  outline: none;
  min-height: 200px;
}

.ProseMirror > * + * {
  margin-top: 0.6em;
}

.ProseMirror p {
  margin: 0.4em 0;
}

.ProseMirror p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: left;
  color: var(--text-muted);
  pointer-events: none;
  height: 0;
}

.ProseMirror h1 {
  font-size: 2rem;
  font-weight: 700;
  line-height: 1.25;
  margin: 1.2em 0 0.4em;
}
.ProseMirror h2 {
  font-size: 1.5rem;
  font-weight: 600;
  line-height: 1.3;
  margin: 1em 0 0.4em;
}
.ProseMirror h3 {
  font-size: 1.2rem;
  font-weight: 600;
  line-height: 1.4;
  margin: 0.9em 0 0.3em;
}

.ProseMirror ul,
.ProseMirror ol {
  padding-left: 1.4em;
  margin: 0.5em 0;
}
.ProseMirror li { margin: 0.2em 0; }

.ProseMirror blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text-secondary);
  font-style: italic;
  margin: 0.8em 0;
}

.ProseMirror pre {
  background: var(--bg-surface);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  font-family: 'JetBrains Mono', Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-primary);
  overflow-x: auto;
  margin: 0.8em 0;
  border: 1px solid var(--border-subtle);
}

.ProseMirror code {
  background: var(--bg-surface);
  border-radius: 3px;
  padding: 1px 5px;
  font-family: 'JetBrains Mono', Menlo, Consolas, monospace;
  font-size: 0.9em;
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
}
.ProseMirror pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: inherit;
}

.ProseMirror hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.5em 0;
}

.ProseMirror img {
  max-width: 100%;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm);
  display: block;
  margin: 0.6em 0;
}

/* Tables */
.ProseMirror table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.8em 0;
  table-layout: fixed;
  overflow: hidden;
}
.ProseMirror th,
.ProseMirror td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  vertical-align: top;
  position: relative;
  min-width: 60px;
}
.ProseMirror th {
  background: var(--bg-surface);
  font-weight: 600;
  text-align: left;
}
.ProseMirror .selectedCell::after {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--accent-subtle);
  opacity: 0.5;
  pointer-events: none;
}
.ProseMirror .tableWrapper {
  overflow-x: auto;
}

/* Task lists */
.ProseMirror ul[data-type="taskList"] {
  list-style: none;
  padding-left: 0;
}
.ProseMirror ul[data-type="taskList"] li {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}
.ProseMirror ul[data-type="taskList"] li > label {
  display: inline-flex;
  margin-top: 4px;
  user-select: none;
}
.ProseMirror ul[data-type="taskList"] li > div {
  flex: 1;
}
.ProseMirror ul[data-type="taskList"] li input[type="checkbox"] {
  cursor: pointer;
  accent-color: var(--accent);
}
.ProseMirror ul[data-type="taskList"] li[data-checked="true"] > div {
  color: var(--text-muted);
  text-decoration: line-through;
}

/* Collaboration cursors */
.collaboration-cursor__caret {
  position: relative;
  margin-left: -1px;
  margin-right: -1px;
  border-left: 2px solid;
  border-right: 2px solid;
  word-break: normal;
  pointer-events: none;
}
.collaboration-cursor__label {
  position: absolute;
  top: -1.4em;
  left: -2px;
  font-size: 11px;
  font-weight: 600;
  font-style: normal;
  line-height: 1;
  user-select: none;
  color: #fff;
  padding: 2px 6px;
  border-radius: 3px 3px 3px 0;
  white-space: nowrap;
}

/* Char count */
.char-count {
  position: sticky;
  bottom: 12px;
  margin-left: auto;
  margin-right: 24px;
  width: fit-content;
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 3px 8px;
  pointer-events: none;
}

/* ============================================================
   Bubble menu
   ============================================================ */

.bubble-menu {
  position: fixed;
  display: flex;
  align-items: center;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: 4px;
  gap: 2px;
  z-index: 80;
}
.bubble-menu button {
  width: 26px;
  height: 26px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  font-size: 12px;
  transition: background 120ms ease, color 120ms ease;
}
.bubble-menu button:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}
.bubble-menu button.active {
  background: var(--accent-subtle);
  color: var(--accent);
}
.bm-sep {
  width: 1px;
  height: 16px;
  background: var(--border);
  margin: 0 4px;
}

/* ============================================================
   Slash command menu
   ============================================================ */

.slash-menu {
  position: fixed;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  width: 260px;
  max-height: 320px;
  overflow-y: auto;
  padding: 4px;
  z-index: 90;
}

.slash-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
  min-height: 36px;
}
.slash-item:hover { background: var(--bg-surface); color: var(--text-primary); }
.slash-item.active {
  background: var(--accent-subtle);
  color: var(--accent);
}

.slash-icon {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--text-secondary);
}
.slash-item.active .slash-icon {
  background: var(--bg-elevated);
  color: var(--accent);
}
.slash-label {
  flex: 1;
  font-weight: 500;
  color: var(--text-primary);
}
.slash-item.active .slash-label { color: var(--accent); }
.slash-desc {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 400;
  margin-top: 1px;
}
.slash-empty {
  padding: 14px 12px;
  color: var(--text-muted);
  font-size: 12px;
  text-align: center;
}

/* ============================================================
   Modals
   ============================================================ */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  backdrop-filter: blur(2px);
}

.modal {
  background: var(--bg-elevated);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 400px;
  padding: 24px;
  margin: 16px;
  border: 1px solid var(--border);
}

.modal h2 {
  margin: 0 0 8px;
  font-size: 18px;
  font-weight: 600;
}

.modal .muted {
  font-size: 13px;
  color: var(--text-secondary);
  margin: 0 0 18px;
  line-height: 1.5;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}
.field span {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
}
.field input {
  border: 1px solid var(--border);
  background: var(--bg-base);
  color: var(--text-primary);
  border-radius: var(--radius-md);
  padding: 9px 12px;
  font-size: 14px;
  outline: none;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}
.field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-subtle);
}

.login-error {
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger);
  border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent);
  border-radius: var(--radius-md);
  padding: 8px 11px;
  font-size: 13px;
  margin-bottom: 12px;
}

.history-list {
  max-height: 52vh;
  overflow: auto;
  margin: 4px 0 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}
.history-meta { display: flex; flex-direction: column; gap: 2px; }
.history-when { font-size: 13px; font-weight: 500; color: var(--text-primary); }
.history-detail { font-size: 11px; color: var(--text-secondary); }
.btn.slim { padding: 5px 10px; font-size: 12px; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

.btn {
  padding: 8px 14px;
  border-radius: var(--radius-md);
  font-size: 13px;
  font-weight: 500;
  transition: background 150ms ease, color 150ms ease, border-color 150ms ease;
  border: 1px solid transparent;
}
.btn.primary {
  background: var(--accent);
  color: #fff;
}
.btn.primary:hover { background: var(--accent-hover); }
.btn.ghost {
  background: transparent;
  color: var(--text-secondary);
  border-color: var(--border);
}
.btn.ghost:hover {
  background: var(--bg-surface);
  color: var(--text-primary);
}
.btn.danger {
  background: var(--danger);
  color: #fff;
}
.btn.danger:hover {
  background: #dc2626;
}

/* ============================================================
   Toast
   ============================================================ */

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-primary);
  color: var(--bg-base);
  padding: 10px 16px;
  border-radius: var(--radius-md);
  font-size: 13px;
  font-weight: 500;
  z-index: 300;
  box-shadow: var(--shadow-md);
  animation: toastIn 200ms ease;
}
@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* ============================================================
   Responsive
   ============================================================ */

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 150;
    width: var(--sidebar-width);
    box-shadow: var(--shadow-md);
    transform: translateX(0);
    transition: transform 200ms ease;
  }
  .sidebar.collapsed {
    transform: translateX(-100%);
    width: var(--sidebar-width); /* keep width — just slide it off-screen */
  }
  .page-title { font-size: 28px; }
  #sidebar-toggle { display: none; }
}

/* =====================================================================
   Loop launcher, administration, sharing, read-only
   ===================================================================== */

.launcher {
  position: fixed;
  inset: 0;
  background: var(--bg-base);
  overflow-y: auto;
  z-index: 60;
}

.launcher-inner {
  max-width: 900px;
  margin: 0 auto;
  padding: 28px 24px 64px;
}

.launcher-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 48px;
}

.launcher-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.launcher-who {
  font-size: 13px;
  color: var(--text-secondary);
  margin-right: 4px;
}

.launcher-title {
  font-size: 30px;
  font-weight: 650;
  margin: 0 0 6px;
  letter-spacing: -0.02em;
}

.loop-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
  margin-top: 28px;
}

.loop-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--bg-elevated);
  padding: 16px;
  cursor: pointer;
  transition: border-color .12s, transform .12s, box-shadow .12s;
  min-height: 116px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.loop-card:hover,
.loop-card:focus-visible {
  border-color: var(--accent);
  transform: translateY(-1px);
  box-shadow: var(--shadow-sm);
  outline: none;
}

.loop-card-top { display: flex; justify-content: flex-end; }

.loop-card-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg-surface);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.loop-card-badge.level-owner  { background: var(--accent-subtle); color: var(--accent); border-color: transparent; }
.loop-card-badge.level-viewer { font-style: italic; }

.loop-card-name {
  font-size: 16px;
  font-weight: 600;
  margin-top: 12px;
  word-break: break-word;
}

.loop-card-meta { font-size: 12px; color: var(--text-muted); margin-top: 4px; }

.loop-card.new {
  border-style: dashed;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
}
.loop-card.new .loop-card-plus { font-size: 26px; line-height: 1; }
.loop-card.new .loop-card-name { margin-top: 6px; font-weight: 500; }

.launcher-empty { margin-top: 28px; max-width: 520px; }

/* --- loop switcher in the sidebar --- */
.loop-switch {
  display: flex;
  align-items: center;
  gap: 8px;
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  padding: 4px 8px;
  margin: -4px -8px;
  cursor: pointer;
  color: var(--text-primary);
  font: inherit;
  max-width: 190px;
}
.loop-switch:hover { background: var(--bg-surface); border-color: var(--border); }
.loop-switch .brand-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* --- read-only --- */
.readonly-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  margin-right: 8px;
  white-space: nowrap;
}

body.read-only #page-title { pointer-events: none; opacity: .85; }

/* --- wide modal + tables --- */
.modal.wide { max-width: 760px; }

.table-wrap { overflow-x: auto; margin-top: 4px; }

.admin-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.admin-table th {
  text-align: left;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
  padding: 8px 10px 8px 0;
  white-space: nowrap;
}
.admin-table td {
  padding: 9px 10px 9px 0;
  border-bottom: 1px solid var(--border-subtle);
  vertical-align: middle;
}
.admin-user strong { display: block; font-weight: 600; }
.admin-user .muted { font-size: 12px; color: var(--text-muted); }
.admin-actions { display: flex; gap: 6px; justify-content: flex-end; }

.admin-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 14px 0 10px;
}

.admin-form {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px;
  margin-bottom: 14px;
  background: var(--bg-surface);
}
.admin-form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
}
.admin-form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

.check { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-size: 13px; }
.check input { width: auto; }

.mini-select {
  font: inherit;
  font-size: 12px;
  padding: 4px 6px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text-primary);
}
.mini-select:disabled { opacity: .55; cursor: not-allowed; }

.danger-text { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* --- sharing --- */
.share-add { display: flex; gap: 8px; margin: 14px 0; flex-wrap: wrap; }
.share-add select,
.share-add input { flex: 1 1 120px; min-width: 0; }

.member-list { list-style: none; padding: 0; margin: 0; }
.member-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border-subtle);
}
.member-name { flex: 1; font-size: 13px; overflow: hidden; text-overflow: ellipsis; }

@media (max-width: 640px) {
  .launcher-inner { padding: 20px 16px 48px; }
  .launcher-title { font-size: 24px; }
  .admin-actions { flex-direction: column; align-items: stretch; }
}

.share-rename { display: flex; gap: 8px; margin: 14px 0 4px; }
.share-rename input { flex: 1; min-width: 0; }

/* =====================================================================
   Kanban boards
   ===================================================================== */

.board-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.board-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 18px 28px 10px;
  flex-wrap: wrap;
}

.board-title { flex: 1; min-width: 200px; margin: 0; }

.board-head-actions { display: flex; align-items: center; gap: 10px; }

.board-stats { font-size: 12px; color: var(--text-muted); white-space: nowrap; }

/* Horizontal scroll for columns; each column scrolls its own cards. */
.board-columns {
  flex: 1;
  display: flex;
  align-items: flex-start;
  gap: 14px;
  padding: 8px 28px 28px;
  overflow-x: auto;
  overflow-y: hidden;
  min-height: 0;
}

.board-column {
  flex: 0 0 290px;
  max-width: 290px;
  display: flex;
  flex-direction: column;
  max-height: 100%;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px;
}

.column-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 4px 10px;
}

.column-title {
  font-weight: 600;
  font-size: 13px;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: text;
  border-radius: var(--radius-sm);
  padding: 2px 4px;
  margin: -2px -4px;
}
body:not(.read-only) .column-title:hover { background: var(--bg-elevated); }

.column-count {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 7px;
}

.column-rename {
  flex: 1;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 2px 4px;
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  background: var(--bg-elevated);
  color: var(--text-primary);
  min-width: 0;
}

.column-cards {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  min-height: 24px;
  padding: 2px;
  border-radius: var(--radius-md);
}
.column-cards.drop-target {
  background: var(--accent-subtle);
  outline: 2px dashed var(--accent);
  outline-offset: -2px;
}

.board-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 9px 11px;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: border-color .12s, transform .08s;
}
.board-card:hover { border-color: var(--accent); }
.board-card.dragging { opacity: .45; transform: rotate(1deg); }

.card-title { font-size: 13px; font-weight: 500; line-height: 1.35; word-break: break-word; }

.card-notes {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 5px;
  white-space: pre-wrap;
  word-break: break-word;
}

.card-assignee {
  display: inline-block;
  margin-top: 8px;
  font-size: 11px;
  font-weight: 600;
  color: #fff;
  padding: 1px 8px;
  border-radius: 999px;
}

.column-add {
  margin-top: 8px;
  width: 100%;
  background: none;
  border: 1px dashed var(--border);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font: inherit;
  font-size: 12px;
  padding: 7px;
  cursor: pointer;
}
.column-add:hover { border-color: var(--accent); color: var(--accent); }

.card-meta-row { display: flex; gap: 10px; }
.card-meta-row .field { flex: 1; min-width: 0; }

#card-notes {
  font: inherit;
  font-size: 13px;
  width: 100%;
  resize: vertical;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg-elevated);
  color: var(--text-primary);
}

@media (max-width: 640px) {
  .board-head { padding: 14px 16px 8px; }
  .board-columns { padding: 8px 16px 20px; gap: 10px; }
  .board-column { flex-basis: 82vw; max-width: 82vw; }
}

/* --- returning to the launcher from inside a loop --- */
.back-to-loops {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  min-width: 0;
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  padding: 4px 8px;
  margin: -4px -8px;
  cursor: pointer;
  color: var(--text-secondary);
  font: inherit;
  font-size: 12px;
  font-weight: 500;
}
.back-to-loops:hover {
  background: var(--bg-surface);
  border-color: var(--border);
  color: var(--text-primary);
}
.back-to-loops span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* The loop name sits under the back row and switches loops. */
.sidebar .loop-switch {
  width: calc(100% - 24px);
  margin: 2px 12px 10px;
  padding: 6px 8px;
  font-size: 14px;
  font-weight: 600;
}

/* --- loop switcher dropdown --- */
.loop-menu { max-height: 60vh; overflow-y: auto; }

.loop-menu-head {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-muted);
  padding: 6px 10px 4px;
}

.loop-menu-item {
  display: flex !important;
  align-items: center;
  gap: 8px;
}
.loop-menu-item.current { background: var(--accent-subtle); color: var(--accent); }

.loop-menu-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex: 0 0 auto;
}

.loop-menu-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: left;
}

.loop-menu-level {
  font-size: 11px;
  color: var(--text-muted);
  flex: 0 0 auto;
}

.loop-menu-sep {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}
