/* ─── Cognito design tokens ─────────────────────────────────────────────── */
:root {
  /* Brand */
  --yellow:       #FFC629;
  --yellow-light: #FFF4D4;
  --yellow-dim:   #CC9E21;
  --blue:         #1992A3;
  --blue-dim:     #137080;
  --blue-light:   #ECF7F7;
  --blue-mid:     #ACC5C7;
  --off-white:    #FFFCF8;

  /* Grey scale */
  --grey-surface: #F8F8F8;
  --grey-light:   #E8E8E8;
  --grey-2:       #D2D2D2;
  --grey-mid:     #A5A5A5;
  --grey-dark:    #636E73;
  --grey-ink:     #2B3032;

  /* Semantic */
  --surface:      #FFFFFF;
  --border:       #E8E8E8;
  --border-2:     #D2D2D2;

  --font: 'DM Sans', sans-serif;

  --r-pill: 100px;
  --r-card: 16px;
  --r-input: 14px;
  --r-sm: 6px;

  --shadow-sm: 0 1px 4px rgba(0,0,0,0.06);
  --shadow:    0 4px 16px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.12);

  --header-h: 52px;
}

/* ─── Base ───────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }

html { scroll-padding-top: var(--header-h); }

body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  color: var(--grey-ink);
  background: var(--off-white);
  margin: 0;
  -webkit-font-smoothing: antialiased;
}

/* ─── App header ─────────────────────────────────────────────────────────── */
.app-header {
  background: var(--yellow);
  height: var(--header-h);
  display: flex;
  align-items: center;
  padding: 0 28px;
  position: sticky;
  top: 0;
  z-index: 300;
  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
}

.brand {
  display: flex;
  align-items: center;
  gap: 32px;
  text-decoration: none;
}

.brand-logo {
  height: 28px;
  width: auto;
}

.brand-name {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--grey-ink);
}

.app-nav {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 16px;
}
.nav-user {
  font-size: 13px;
  font-weight: 500;
  color: var(--grey-ink);
  opacity: 0.7;
}
.nav-link {
  font-size: 13px;
  font-weight: 500;
  color: var(--grey-ink);
  text-decoration: none;
  padding: 4px 12px;
  border-radius: var(--r-pill);
  background: rgba(0,0,0,0.08);
  transition: background 0.15s;
}
.nav-link:hover { background: rgba(0,0,0,0.15); }

/* ─── Main ───────────────────────────────────────────────────────────────── */
.app-main {
  padding: 32px 28px 60px;
}

.welcome-msg {
  font-size: 1rem;
  font-weight: 500;
  color: var(--grey-dark);
  margin: 0 0 12px;
}

/* ─── Toolbar ────────────────────────────────────────────────────────────── */
.toolbar {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  gap: 16px;
}

.paper-count {
  font-size: 20px;
  font-weight: 600;
  color: var(--grey-ink);
  letter-spacing: -0.01em;
  flex: 1;
}

.paper-count-sub {
  font-size: 13px;
  font-weight: 400;
  color: var(--grey-dark);
  margin-left: 8px;
}

/* Search — centered, wide */
.search-wrap {
  position: relative;
  width: 480px;
  flex-shrink: 0;
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--grey-mid);
  pointer-events: none;
  width: 15px;
  height: 15px;
}

.search-input {
  width: 100%;
  border: 2px solid var(--blue-mid);
  border-radius: var(--r-input);
  padding: 9px 16px 9px 40px;
  font-family: var(--font);
  font-size: 13px;
  background: var(--surface);
  color: var(--grey-ink);
  outline: none;
  transition: border-color 180ms ease, box-shadow 180ms ease;
}

.search-input::placeholder { color: var(--grey-mid); }

.search-input:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(25, 146, 163, 0.12);
}

.toolbar-right { flex: 1; display: flex; justify-content: flex-end; }

/* ─── Table card ─────────────────────────────────────────────────────────── */
.table-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-card);
  box-shadow: var(--shadow-sm);
  /* No overflow here — keeps sticky thead working against the window */
}

/* Clip horizontal overflow without creating a vertical scroll container */
.table-scroll {
  overflow-x: auto;
  border-radius: var(--r-card);
  transform: rotateX(180deg);
}

.table-scroll > table {
  transform: rotateX(180deg);
}

/* ─── Table ──────────────────────────────────────────────────────────────── */
table.papers-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

/* Sticky thead — sticks below the app header */
table.papers-table thead th {
  background: var(--grey-surface);
  color: var(--grey-dark);
  font-family: var(--font);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 10px 14px;
  white-space: nowrap;
  border-bottom: 1px solid var(--border-2);
}

table.papers-table thead th.col-editable {
  color: var(--blue);
}

table.papers-table tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background 0.12s;
}

table.papers-table tbody tr:last-child { border-bottom: none; }
table.papers-table tbody tr:hover { background: var(--off-white); }

table.papers-table td {
  padding: 11px 14px;
  vertical-align: top;
  color: var(--grey-ink);
}

/* ─── Column types ───────────────────────────────────────────────────────── */
.col-title {
  min-width: 220px;
  max-width: 320px;
  font-weight: 500;
  line-height: 1.45;
  font-size: 13px;
}

.col-authors {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  color: var(--grey-dark);
}

.col-year {
  font-size: 12px;
  color: var(--grey-dark);
  white-space: nowrap;
}

.col-journal {
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12px;
  color: var(--grey-dark);
}

.col-abstract {
  max-width: 200px;
  font-size: 12px;
  color: var(--grey-mid);
  line-height: 1.45;
}

.col-url {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.col-url a {
  font-size: 12px;
  color: var(--blue);
  text-decoration: none;
}

.col-url a:hover { text-decoration: underline; }

.col-source {
  font-size: 11px;
  color: var(--grey-mid);
  white-space: nowrap;
}

.col-date {
  font-size: 11px;
  color: var(--grey-mid);
  white-space: nowrap;
}

.col-study {
  font-size: 12px;
  color: var(--grey-dark);
  white-space: nowrap;
}

/* ─── Editable cells ─────────────────────────────────────────────────────── */
.editable-cell {
  position: relative;
  padding-bottom: 9px;
}

/* Yellow underline slides in on hover/active */
.editable-cell::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 14px;
  right: 14px;
  height: 2px;
  background: var(--yellow);
  transform: scaleX(0);
  transform-origin: left center;
  transition: transform 0.18s ease;
  border-radius: 1px;
}

.editable-cell:hover::after,
.editable-cell.active::after { transform: scaleX(1); }

.editable-cell:hover { background: var(--yellow-light); }
.editable-cell.active { background: var(--yellow-light); }

.cell-placeholder {
  color: var(--grey-mid);
  font-style: italic;
  font-size: 12px;
  user-select: none;
}

/* ─── Stars ──────────────────────────────────────────────────────────────── */
.star-cell {
  cursor: pointer;
  white-space: nowrap;
  min-width: 110px;
}

.star {
  font-size: 17px;
  color: var(--grey-2);
  transition: color 0.1s;
  cursor: pointer;
  display: inline-block;
  padding: 0 1px;
  user-select: none;
  line-height: 1;
}

.star.filled  { color: var(--yellow); }
.star.preview { color: var(--yellow-dim); }

/* ─── Notes (contenteditable) ────────────────────────────────────────────── */
.notes-cell {
  cursor: text;
  min-width: 190px;
  max-width: 340px;
  min-height: 38px;
  font-size: 13px;
  word-break: break-word;
  overflow-wrap: break-word;
}

.notes-cell[contenteditable="true"] { outline: none; }

/* ─── Tags ───────────────────────────────────────────────────────────────── */
.tag-cell {
  cursor: pointer;
  min-width: 160px;
}

/* ─── Tags / Source headers with filter icon ─────────────────────────────── */
.th-tags, .th-source {
  white-space: nowrap;
  display: flex !important;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.filter-icon-btn {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  color: var(--grey-ink);
  display: inline-flex;
  align-items: center;
  line-height: 1;
  transition: color 0.15s;
}

.filter-icon-btn:hover { color: var(--grey-dark); }
.filter-icon-btn.filter-on { color: var(--blue); }

.th-sortable { user-select: none; white-space: nowrap; }
.th-sortable:hover { color: var(--blue); }
.sort-arrow { font-size: 10px; color: var(--blue); }

/* Tag column cells when filter is active */
.tag-cell.filter-highlight {
  background: var(--blue-light) !important;
}
.tag-cell.filter-highlight:hover {
  background: #d8eef1 !important;
}

/* Any / All segmented toggle */
.filter-mode-toggle {
  display: flex;
  border: 1.5px solid var(--border-2);
  border-radius: var(--r-pill);
  overflow: hidden;
  margin-bottom: 4px;
}

.filter-mode-btn {
  flex: 1;
  border: none;
  background: transparent;
  padding: 5px 0;
  font-size: 12px;
  font-family: var(--font);
  font-weight: 400;
  color: var(--grey-dark);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.filter-mode-btn.active {
  background: var(--blue);
  color: #fff;
}

.tag-pill {
  display: inline-flex;
  align-items: center;
  border-radius: var(--r-sm);
  padding: 2px 8px;
  font-size: 11px;
  font-weight: 500;
  margin: 1px 2px;
  white-space: nowrap;
  background: var(--grey-light);
  color: var(--grey-ink);
}

/* Tag group colour tokens */
.tag-pill--subject_type      { background: #dbeafe; color: #1e40af; }
.tag-pill--study_design      { background: #ede9fe; color: #5b21b6; }
.tag-pill--stimulation_modality { background: #d1fae5; color: #065f46; }
.tag-pill--exposure_duration { background: #ffedd5; color: #9a3412; }
.tag-pill--has_sham_control  { background: #f1f5f9; color: #475569; }
.tag-pill--is_40hz           { background: var(--yellow-light); color: var(--grey-ink); }
.tag-pill--disease_category  { background: #ffe4e6; color: #9f1239; }
.tag-pill--disease           { background: #fce7f3; color: #9d174d; }
.tag-pill--outcome           { background: #ecfdf5; color: #166534; }
.tag-pill--measurement       { background: #e0f2fe; color: #0369a1; }
.tag-pill--custom            { background: var(--yellow); color: var(--grey-ink); }

/* ─── Tag picker group header ────────────────────────────────────────────── */
.tag-group-header {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--grey-mid);
  padding: 8px 4px 3px;
  margin-top: 4px;
  border-top: 1px solid var(--border);
  grid-column: 1 / -1;
}
.tag-group-header:first-child { border-top: none; margin-top: 0; padding-top: 2px; }

/* Radio option for exclusive groups */
.tag-picker-radio {
  display: flex;
  flex-direction: column;
  gap: 2px;
  grid-column: 1 / -1;
}
.tag-picker-radio .form-check { padding: 3px 4px 3px 24px; margin: 0; }

/* ─── Tag picker ─────────────────────────────────────────────────────────── */
.tag-picker-dropdown {
  position: absolute;
  z-index: 1050;
  min-width: 260px;
  max-width: 360px;
  max-height: 480px;
  overflow-y: auto;
  background: var(--surface);
  border: 1.5px solid var(--blue-mid);
  border-radius: var(--r-card);
  box-shadow: var(--shadow-lg);
  padding: 12px;
}

.tag-picker-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
}

.tag-picker-dropdown .form-check {
  padding: 4px 4px 4px 24px;
  margin: 0;
}

.tag-picker-dropdown .form-check-label {
  cursor: pointer;
  font-size: 13px;
  font-family: var(--font);
  color: var(--grey-ink);
}

.tag-picker-dropdown .form-check-label .tag-pill {
  display: inline-block;
  white-space: normal;
  word-break: break-word;
}

.tag-picker-dropdown .form-check-input:checked {
  background-color: var(--blue);
  border-color: var(--blue);
}

.tag-picker-dropdown .form-check-input:focus {
  box-shadow: 0 0 0 3px rgba(25, 146, 163, 0.18);
  border-color: var(--blue);
}

.tag-picker-footer {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  position: sticky;
  bottom: -12px;
  background: var(--surface);
  padding-bottom: 2px;
}

/* Pill buttons per Cognito design system */
.btn-brand {
  flex: 1;
  background: var(--blue);
  border: none;
  color: #fff;
  font-family: var(--font);
  font-weight: 400;
  font-size: 13px;
  border-radius: var(--r-pill);
  padding: 7px 18px;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-brand:hover { background: var(--blue-dim); }

.btn-ghost {
  background: transparent;
  border: 1.5px solid var(--border-2);
  color: var(--grey-dark);
  font-family: var(--font);
  font-size: 13px;
  font-weight: 400;
  border-radius: var(--r-pill);
  padding: 7px 16px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

.btn-ghost:hover { border-color: var(--grey-dark); color: var(--grey-ink); }

/* ─── Pagination ─────────────────────────────────────────────────────────── */
.pagination-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 16px;
  padding: 0 2px;
  min-height: 36px;
}

.pagination-info {
  font-size: 13px;
  color: var(--grey-dark);
}

.pagination-controls {
  display: flex;
  align-items: center;
  gap: 4px;
}

.page-btn {
  min-width: 34px;
  height: 34px;
  padding: 0 10px;
  border: 1.5px solid var(--border-2);
  border-radius: var(--r-pill);
  background: var(--surface);
  color: var(--grey-ink);
  font-family: var(--font);
  font-size: 13px;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.page-btn:hover:not(.disabled):not(.active) {
  border-color: var(--blue);
  color: var(--blue);
}

.page-btn.active {
  background: var(--yellow);
  border-color: var(--yellow-dim);
  color: var(--grey-ink);
  font-weight: 600;
  cursor: default;
}

.page-btn.disabled {
  color: var(--grey-mid);
  border-color: var(--border);
  cursor: not-allowed;
}

.page-ellipsis {
  font-size: 13px;
  color: var(--grey-mid);
  padding: 0 4px;
  user-select: none;
}

/* ─── Toast ──────────────────────────────────────────────────────────────── */
#save-toast .toast {
  background: var(--grey-ink) !important;
  color: #fff;
  border: none !important;
  border-radius: var(--r-card) !important;
  box-shadow: var(--shadow-lg);
  font-family: var(--font);
  font-size: 13px;
}

#save-toast .toast-body {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
}

#save-toast .toast-body::before {
  content: '';
  display: inline-block;
  width: 8px;
  height: 8px;
  background: var(--yellow);
  border-radius: 50%;
  flex-shrink: 0;
}

#save-toast .btn-close { filter: invert(1); opacity: 0.5; }

/* ─── Row selection (admin delete) ──────────────────────────────────────── */
#papers-table tbody tr { cursor: default; user-select: none; }

#papers-table tbody tr.row-selected td {
  background: #fff3cd !important;
}

.btn-delete-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #c0392b;
  color: #fff;
  border: none;
  border-radius: 5px;
  width: 26px;
  height: 26px;
  cursor: pointer;
  padding: 0;
  vertical-align: middle;
}
.btn-delete-action:hover { background: #a93226; }

.btn-deselect-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #222;
  color: #fff;
  border: none;
  border-radius: 5px;
  width: 26px;
  height: 26px;
  cursor: pointer;
  font-size: 13px;
  vertical-align: middle;
  margin-left: 4px;
}
.btn-deselect-action:hover { background: #444; }

#row-context-menu .ctx-item:hover,
#link-context-menu .ctx-item:hover {
  background: #f3f3f3;
}

#row-context-menu #ctx-remove:hover {
  background: rgba(192, 57, 43, 0.08);
}

