:root{
  --sidebar-w:300px;
  --gap:12px;
  --accent-blue:#1e90ff;
  --accent-green:#28a745;
  --bg:#f6f7fb;
  --muted:#777;
}

*{box-sizing:border-box;font-family:Inter, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial;}

body,html,#app{height:100%;margin:0;padding:0;background:var(--bg);color:#111}
#app{display:flex;min-height:100vh}
aside#sidebar{
  width:var(--sidebar-w);
  background:#fff;
  border-right:1px solid #e6e7ee;
  padding:12px;
  display:flex;
  flex-direction:column;
  gap:12px;
}
.stories, .entities, .story-actions { background:transparent; }

/* Sidebar panes and splitters */
.pane {
  display: flex;
  flex-direction: column;
  /* allow vertical scrolling only; prevent horizontal scrollbar */
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 40px;
}

/* thin draggable splitter between panes */
.splitter {
  height: 8px;
  background: transparent;
  cursor: row-resize;
  margin: 4px 0;
  border-radius: 4px;
  transition: background 120ms;
}
.splitter:hover { background: rgba(0,0,0,0.04); }
body.resizing { user-select: none; }
/* ensure sidebar keeps column flow (already set but reinforce) */
aside#sidebar { display: flex; flex-direction: column; }
.stories h3, .entities h3 { margin:6px 0;font-size:14px;display:flex;align-items:center;gap:8px;justify-content:space-between; }
.story-controls{display:flex;gap:8px;margin-bottom:8px}
.story-controls input{flex:1;padding:6px;border:1px solid #ddd;border-radius:4px}
.story-controls button{padding:6px 8px;border-radius:4px;border:1px solid #ddd;background:#f2f3f7}

/* Story list item layout: align name and actions (delete) similar to entity sort controls.
   Make the delete button visually match the sort/select button size and spacing. */
#storyList li.story-item{display:flex;align-items:center;justify-content:space-between;gap:8px;padding:6px;border-radius:4px;cursor:pointer}
#storyList li.story-item span{flex:1;min-width:0}
#storyList li.story-item .story-delete{padding:6px 8px;border-radius:4px;border:1px solid #ddd;background:#f2f3f7;font-size:13px;line-height:1;cursor:pointer}
#storyList li.story-item .story-delete:hover{background:#eef4ff}

#storyList{list-style:none;margin:0;padding:0;overflow-y:auto;overflow-x:hidden;min-height:40px}
#highlightList, #locationList{list-style:none;margin:0;padding:0;overflow:auto;min-height:40px}
#storyList li, #highlightList li, #locationList li{padding:6px;border-radius:4px;cursor:pointer}

/* Ensure highlight list items align name, count and actions consistently.
   Use flex layout so the actions container (set to margin-left:auto in JS)
   stays right-justified and aligned across all items. */
#highlightList li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
#storyList li:hover, #highlightList li:hover, #locationList li:hover{background:#f5f7ff}

.story-actions{margin-top:auto;display:flex;flex-direction:column;gap:8px}
.story-actions input{padding:6px;border:1px solid #ddd;border-radius:4px}
.story-actions button{padding:6px;border-radius:4px;border:1px solid #ddd;background:#fff}
.upload{display:flex;flex-direction:column;gap:6px}

main#main{flex:1;display:flex;flex-direction:column;min-width:0;padding:16px}
header{display:flex;align-items:center;justify-content:space-between;gap:12px}
header h1{margin:0;font-size:18px}
#editorArea{display:flex;gap:12px;flex:1;min-height:0;margin-top:12px}
#editor{flex:1;min-width:0;resize:none;padding:12px;border:1px solid #e6e7ee;border-radius:6px;background:#fff;font-family:ui-monospace,SFMono-Regular,Menlo,monospace}
.preview{flex:1;min-width:0;overflow:auto;padding:12px;border:1px solid #e6e7ee;border-radius:6px;background:#fff}
.preview p{margin:0 0 8px 0}

/* Ensure rendered images fit their container and never overflow the preview or other UI panels.
   This constrains image width to the width of the element they're rendered into (responsive). */
.preview img,
.modal-content img,
.story-content img {
  max-width: 100%;
  height: auto;
  display: block;
}

/* Ensure editor column and preview align bottoms:
   keep both sides as flex children with min-height:0 so they stretch equally.
   Make the textarea flex to fill the editor column (counters sit below it). */
#editorArea > .editor-column { display:flex; flex-direction:column; flex:1; min-height:0; }
#editorArea > .editor-column textarea { flex:1; min-height:0; height:auto; }
#editorArea > .preview { align-self:stretch; min-height:0; }
button{cursor:pointer}

a.entity-hl{color:var(--accent-blue);text-decoration:underline;cursor:default}
a.entity-loc{color:var(--accent-green);text-decoration:underline;cursor:pointer}

/* tooltip for hover */
.entity-tooltip{
  position:fixed;
  z-index:2000;
  background:#fff;
  border:1px solid #ddd;
  padding:8px;
  border-radius:6px;
  max-width:280px;
  box-shadow:0 4px 16px rgba(0,0,0,0.08);
  font-size:13px;
}
.entity-tooltip img{max-width:100%;height:auto;border-radius:4px;margin-bottom:6px}

/* modal */
.modal{position:fixed;left:0;top:0;right:0;bottom:0;display:flex;align-items:center;justify-content:center;z-index:3000;background:rgba(0,0,0,0.35)}
.modal.hidden{display:none}
.modal-content{width:720px;max-width:95%;background:#fff;border-radius:8px;padding:12px;border:1px solid #ddd}
.modal-controls{display:flex;gap:8px;align-items:center;margin-top:8px}

/* custom context menu */
.custom-context{position:fixed;z-index:4000;background:#fff;border:1px solid #ddd;border-radius:6px;padding:6px;box-shadow:0 6px 18px rgba(0,0,0,0.12)}
.custom-context button{display:block;width:100%;text-align:left;padding:6px;border:none;background:transparent}

/* small helpers */
.small{font-size:13px;color:var(--muted)}

/* tag pills (rendered in preview) */
.tag{
  display:inline-block;
  padding:0.15em 0.6em;
  border-radius:999px;
  font-size:0.85em;
  margin:0 6px 6px 0;
  line-height:1;
  vertical-align:middle;
  box-shadow:0 1px 2px rgba(0,0,0,0.04);
}
/* fallback cursor and spacing */
.tag[data-tag]{cursor:pointer}

/* selected tag state (when a tag filter is active) */
.tag.selected{
  box-shadow:0 4px 12px rgba(0,0,0,0.12);
  transform:translateY(-1px);
  border:1px solid rgba(0,0,0,0.06);
}

/* explicit classes for sidebar story states so JS can toggle them reliably */
.story-item--muted {
  color: var(--muted) !important;
  opacity: 0.9;
}
.story-item--active {
  font-weight: 600 !important;
  color: #111 !important;
  background: rgba(30,144,255,0.06);
  border-radius: 4px;
  padding: 2px 4px;
}

/* When no story is open, allow the stories pane to expand to fill the sidebar.
   The sidebar gains/removes the "story-open" class from JS when opening/closing a story.
   These rules override the splitter-imposed heights so the stories list can populate
   the full left menu when nothing is open. */
#sidebar:not(.story-open) #storiesPane {
  height: auto !important;
  flex: 1 1 auto;
  min-height: 0;
}

/* collapse tiles/highlights panes visually when no story is open so they don't consume fixed height */
#sidebar:not(.story-open) #tilesPane,
#sidebar:not(.story-open) #highlightsPane {
  height: auto !important;
  flex: 0 0 auto;
  min-height: 0;
}

/* Tiles drag-and-drop insertion indicators */
.tile-item {
  position: relative;
}
.tile-item.drop-before::before,
.tile-item.drop-after::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background: var(--accent-blue);
  transform: translateY(-50%);
  pointer-events: none;
}
.tile-item.drop-before::before {
  top: 0;
}
.tile-item.drop-after::after {
  bottom: 0;
}

/* subtle hover state for tile items */
.tile-item:hover {
  background: rgba(30,144,255,0.03);
}

/* Highlights with no tags: make them visually obvious (fluorescent yellow) */
.tag.no-tags {
  background: #fff55a;
  color: #111;
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}

/* entity words in the preview that correspond to highlights with no tags */
a.entity-hl.no-tags {
  background: #fff55a;
  color: #111;
  padding: 0.08em 0.35em;
  border-radius: 6px;
  text-decoration: underline;
}

/* Local Mode label (displayed next to the Log Out button in local/dev mode) */
.local-mode {
  color: #c00;
  font-weight: 700;
  margin-left: 8px;
}
