:root {
  --bg: #0f1613;
  --panel: #16201c;
  --panel-2: #1c2823;
  --border: #2a3833;
  --text: #eaf2ee;
  --text-dim: #9fb3ac;
  --accent: #3fae6a;
  --accent-hover: #4fc47c;
  --danger: #d9534f;
  --radius: 10px;
}

* { box-sizing: border-box; }

/* Guarantee the `hidden` attribute always wins, no matter what display value
   a more specific rule below sets on the same element (this was the root
   cause of both the stuck-modal bug and the stuck-login-screen bug). */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

h1, h2, h3 { margin: 0 0 8px; }

.screen { min-height: 100vh; }

/* Login */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  width: 100%;
  max-width: 360px;
  text-align: center;
}
.subtitle { color: var(--text-dim); font-size: 14px; margin-bottom: 20px; }
.login-card input {
  width: 100%;
  padding: 10px 12px;
  margin-bottom: 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 15px;
}
.login-card button { width: 100%; }

/* Buttons */
.primary-btn, .ghost-btn, .danger-btn {
  padding: 9px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
}
.primary-btn { background: var(--accent); color: #06170d; border-color: var(--accent); }
.primary-btn:hover { background: var(--accent-hover); }
.ghost-btn { background: transparent; color: var(--text); }
.ghost-btn:hover { background: var(--panel-2); }
.danger-btn { background: transparent; color: var(--danger); border-color: var(--danger); }
.danger-btn:hover { background: rgba(217,83,79,0.12); }
.error-text { color: var(--danger); font-size: 13px; margin-top: 8px; }

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 28px;
  border-bottom: 1px solid var(--border);
}
.topbar-actions { display: flex; align-items: center; gap: 10px; }

.photo-size-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  margin-right: 4px;
}
.size-toggle-btn {
  padding: 8px 12px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.size-toggle-btn + .size-toggle-btn { border-left: 1px solid var(--border); }
.size-toggle-btn:hover { background: var(--panel-2); color: var(--text); }
.size-toggle-btn.active { background: var(--accent); color: #06170d; }

/* Filter bar */
.filter-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 28px 0;
  flex-wrap: wrap;
}
.filter-label { color: var(--text-dim); font-size: 13px; }
.tag-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.tag-chip {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
}
.tag-chip.active, .tag-chip:hover { border-color: var(--accent); color: var(--text); }

/* Study grid */
.study-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 18px;
  padding: 20px 28px 60px;
}
.study-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  transition: border-color 0.15s;
}
.study-card:hover { border-color: var(--accent); }
.study-card { cursor: grab; }
.study-card.dragging { opacity: 0.4; }
.study-card img {
  width: 100%;
  height: 150px;
  object-fit: cover;
  background: var(--panel-2);
  display: block;
}
.study-card .no-image {
  width: 100%;
  height: 150px;
  background: var(--panel-2);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 13px;
}
.study-card-body { padding: 12px 14px; }
.study-card-title { font-weight: 600; font-size: 15px; margin-bottom: 6px; }
.study-card-meta { color: var(--text-dim); font-size: 12px; margin-bottom: 8px; }
.study-card .tag-chips { gap: 5px; }
.study-card .tag-chip { padding: 2px 9px; font-size: 11px; cursor: default; }

.empty-state { text-align: center; color: var(--text-dim); padding: 60px 20px; }

/* Modals */
.modal-overlay[hidden] {
  display: none;
}
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  overflow-y: auto;
  z-index: 50;
}
.modal-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 520px;
  position: relative;
}
.modal-card label {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin: 14px 0 6px;
}
.modal-card label .hint { font-weight: 400; opacity: 0.8; }
.modal-card input[type="text"],
.modal-card input[type="file"],
.modal-card textarea {
  width: 100%;
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
}
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
.close-x {
  position: absolute;
  top: 14px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 22px;
  cursor: pointer;
  line-height: 1;
}
.close-x:hover { color: var(--text); }

.detail-text { white-space: pre-wrap; color: var(--text); font-size: 14px; line-height: 1.5; margin-bottom: 8px; }

/* Study detail screen */
.topbar-title-group { display: flex; align-items: center; gap: 14px; }
.back-btn { padding: 9px 12px; }
#study-detail-title { font-size: 20px; }

/* Sidebar + single-page layout, Evernote/OneNote-style: the sidebar lists
   every Section (a named group) and, under each, its pages — the main
   pane shows only the one page you've clicked, so a study with a
   "Preflop strat" Section and a "Postflop strat" Section keeps those
   fully separate instead of one long scroll. */
.study-body {
  display: flex;
  align-items: flex-start;
  gap: 24px;
  padding: 20px 28px 60px;
}
.sections-sidebar-wrap {
  flex: 0 0 240px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: sticky;
  top: 20px;
  max-height: calc(100vh - 40px);
}
.add-group-btn { width: 100%; justify-content: center; }
.sections-sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}
.group-block { display: flex; flex-direction: column; gap: 2px; }
.group-block.dragging { opacity: 0.4; }
.group-block.collapsed .group-pages { display: none; }
.group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 4px;
  border-radius: 6px;
  cursor: grab;
  user-select: none;
}
.group-header:hover .group-actions { opacity: 1; }
.group-header.active-group { background: rgba(63, 174, 106, 0.16); }
.group-header-main { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1; }
.group-toggle {
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-dim);
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  padding: 0;
}
.group-toggle:hover { background: var(--panel-2); color: var(--text); }
.group-title {
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  /* Deliberately its own accent color (not the neutral text-dim/text used
     by pages below) so a Section's own heading is visually distinct from
     the pages nested under it at a glance. */
  color: var(--accent-hover);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.group-header.active-group .group-title { color: var(--accent-hover); }
.group-actions { display: flex; gap: 4px; flex-shrink: 0; opacity: 0; transition: opacity 0.1s; }
.tiny-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 11px;
  cursor: pointer;
  padding: 2px 5px;
  border-radius: 4px;
}
.tiny-btn:hover { background: var(--panel-2); color: var(--text); }
.tiny-btn.danger:hover { color: #ff8080; }
.group-pages { display: flex; flex-direction: column; gap: 3px; padding-left: 6px; }
.group-pages-empty { color: var(--text-dim); font-size: 12px; padding: 6px 10px; font-style: italic; }
.section-nav-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid transparent;
  /* Brighter than the old text-dim default so page names read clearly
     even when not hovered/active — active/hover still stand out further
     via background + border + bold, not color alone. */
  color: var(--text);
  cursor: grab;
  user-select: none;
}
.section-nav-item:hover { background: var(--panel-2); color: var(--text); }
.section-nav-item.active { background: var(--panel-2); color: var(--text); border-color: var(--border); font-weight: 600; }
.section-nav-item.dragging { opacity: 0.4; }
.section-nav-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.section-nav-count { flex-shrink: 0; font-size: 11px; color: var(--text-dim); }
.sections-sidebar-empty { color: var(--text-dim); font-size: 13px; padding: 10px 12px; }

.section-content { flex: 1; min-width: 0; max-width: 3400px; }

.section-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.section-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.section-card-header h3 { font-size: 22px; }
.section-actions { display: flex; gap: 8px; flex-shrink: 0; }
.small-btn { padding: 5px 10px; font-size: 12px; }

/* Photo feed: each screenshot renders at its own real size (see the
   applyNaturalWidth logic in app.js), and the row just wraps as many as
   actually fit — nothing is stretched or shrunk to match a fixed number of
   columns. Each photo has its own caption underneath. */
.photo-feed {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 24px 20px;
}
.photo-block {
  display: flex;
  flex-direction: column;
  flex: 0 0 auto;
  width: 280px; /* fallback until the image loads and app.js sets its real width */
  max-width: 100%;
  min-width: 0;
}
/* A full-width divider entry — forces a 100%-width row in the wrapping flex
   feed above, so it visually splits whatever comes before it from whatever
   comes after, regardless of how many photos/notes are on each side. A
   generous top margin makes it read clearly as a fresh blank line rather
   than something tacked onto the entry above. It can optionally carry its
   own heading (.divider-title) — shown BELOW the line, so it reads as the
   heading of what follows, not a caption on what came before — turning it
   into the start of a new named block of entries within the page. */
.photo-feed-divider {
  flex: 1 1 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 34px 0 2px;
}
.divider-title {
  margin: 2px 0 0;
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}
.divider-line-row {
  display: flex;
  align-items: center;
  gap: 14px;
}
.divider-line-row hr {
  flex: 1;
  border: none;
  border-top: 2px solid var(--border);
  margin: 0;
}
.divider-line-row .ghost-btn {
  flex-shrink: 0;
  opacity: 0.6;
}
.divider-line-row .ghost-btn:hover { opacity: 1; }
/* Drag-to-reorder / drag-to-move handle — deliberately the ONLY draggable
   element inside an entry (the entry itself is not draggable), so starting
   a drag never fights with selecting text in a note's edit box or clicking
   its buttons. */
.entry-drag-handle {
  display: inline-block;
  cursor: grab;
  opacity: 0.55;
  margin-bottom: 8px;
  padding: 3px 8px;
  font-size: 11px;
}
.entry-drag-handle:hover { opacity: 1; }
.photo-block.dragging,
.photo-feed-divider.dragging {
  opacity: 0.35;
}
/* Highlighted while a dragged entry is hovering over this feed — a visual
   cue that dropping here is valid, whether reordering within it or moving
   an entry in from a different block/page. */
.photo-feed.feed-drop-target {
  outline: 2px dashed var(--accent);
  outline-offset: 6px;
  border-radius: 8px;
}
.feed-empty-state {
  flex: 1 1 100%;
  text-align: left;
  padding: 16px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  margin: 0;
}
.photo-block-img {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 8px;
  border: 1px solid var(--border);
  cursor: pointer;
  margin-bottom: 10px;
}
.photo-block-img:hover { border-color: var(--accent); }
.photo-caption-text {
  white-space: pre-wrap;
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  margin: 0 0 8px;
  overflow-wrap: break-word;
}
.photo-caption-text:empty::before {
  content: 'No note yet.';
  color: var(--text-dim);
  font-style: italic;
}
.photo-caption-text hr,
.photo-caption-edit-input hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 10px 0;
}
.photo-note-title {
  margin: 0 0 6px;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}
.photo-note-title-input {
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 8px;
}
.edit-hint {
  font-size: 11px;
  color: var(--text-dim);
  margin: 0 0 8px;
}
/* A row added with just text and no photo attached — no image placeholder,
   just the caption itself, with a little breathing room at the top since
   there's no photo above it. */
.note-only-block .photo-caption-view,
.note-only-block .photo-caption-edit {
  padding-top: 4px;
}
.photo-block-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.caption-style-controls {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.caption-style-controls select {
  padding: 5px 6px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 12px;
  max-width: 100%;
}
.bold-btn {
  font-weight: 700;
  min-width: 32px;
}
.bold-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #06150d;
}

/* Simple click-a-swatch color picker — no hex/RGB entry needed. */
.caption-color-swatches {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}
.color-swatch {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2px solid var(--border);
  cursor: pointer;
  padding: 0;
}
.color-swatch:hover { border-color: var(--text-dim); }
.color-swatch.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--panel), 0 0 0 4px var(--accent);
}
/* A contenteditable div, not a textarea, so a selection made inside it can
   be individually styled (see the note-editor JS) instead of every style
   control always applying to the whole note. */
.photo-caption-edit-input {
  width: 100%;
  min-height: 44px;
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  margin-bottom: 4px;
  cursor: text;
}
.photo-caption-edit-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.photo-caption-edit-input:empty:before {
  content: attr(data-placeholder);
  color: var(--text-dim);
}

/* Blocks — self-contained sub-feeds nested inside a page, each with its own
   heading and its own full toolbar (Rename / + Photos/Notes / + Divider /
   Delete block), stacked below the page's own feed. Visually inset (its own
   panel background, a left accent border) so it reads as nested inside the
   page rather than as another top-level page. */
.blocks-container {
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.blocks-container:not(:empty) { margin-top: 18px; }
.block-card {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.block-card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.block-heading {
  margin: 0;
  font-size: 17px;
  color: var(--text);
}
.add-block-btn { align-self: flex-start; margin-top: 16px; }

/* Add-photo rows inside the section modal */
#photo-rows-container {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 8px;
}
.photo-row {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.photo-row-caption { min-height: 44px; }
.add-row-btn { margin-top: 12px; }

/* Lightbox */
.lightbox {
  background: rgba(0,0,0,0.9);
  align-items: center;
  padding: 40px;
}
.lightbox img {
  max-width: 90vw;
  max-height: 85vh;
  border-radius: 6px;
  object-fit: contain;
}
.lightbox-nav {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255,255,255,0.08);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 32px;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  cursor: pointer;
  line-height: 1;
}
.lightbox-nav:hover { background: rgba(255,255,255,0.16); }
.lightbox-prev { left: 20px; }
.lightbox-next { right: 20px; }
.lightbox-counter {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  color: var(--text-dim);
  font-size: 13px;
}
.lightbox .close-x { position: fixed; top: 20px; right: 24px; font-size: 32px; }

/* The photo feed wraps naturally based on each photo's own width, so no
   fixed-column rules are needed here — just shrink the fallback width on
   narrow screens so a photo isn't wider than the screen before it loads. */
@media (max-width: 560px) {
  .photo-block { width: 100%; }
  .section-card-header h3 { font-size: 19px; }
}

@media (max-width: 600px) {
  .topbar { padding: 16px; flex-direction: column; align-items: flex-start; gap: 10px; }
  .filter-bar, .study-grid, .study-body { padding-left: 16px; padding-right: 16px; }
  .topbar-title-group { flex-direction: column; align-items: flex-start; gap: 8px; }
  .study-body { flex-direction: column; }
  .sections-sidebar-wrap { position: static; max-height: none; width: 100%; flex: 0 0 auto; }
  .sections-sidebar { max-height: 260px; }
}
