/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: "Cascadia Code", "Fira Code", "JetBrains Mono", "SF Mono", "Consolas", "Menlo", monospace;
  background: #f0f2f5;
  color: #1a1a2e;
  min-height: 100vh;
  -webkit-text-size-adjust: 100%;
  /* iPhone notch / home-indicator safe areas (0 on desktop) */
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* Force form elements to inherit monospace font */
button, select, input, textarea {
  font-family: inherit;
}

/* ===== Toolbar ===== */
.toolbar {
  display: flex;
  gap: 16px;
  padding: 12px 20px;
  background: #fff;
  border-bottom: 1px solid #d9d9d9;
  align-items: center;
}

.filter-spacer {
  flex: 1;
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.filter-group label {
  font-weight: 600;
  font-size: 14px;
  color: #555;
}

.filter-group select {
  padding: 6px 10px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
  background: #fff;
  min-width: 130px;
  cursor: pointer;
}

.filter-group select[multiple] {
  padding: 2px 4px;
  font-size: 12px;
  min-width: 120px;
  max-width: 160px;
  overflow-y: auto;
}

.filter-group select[multiple] option {
  padding: 2px 6px;
}

/* ── Custom multi-select dropdown ── */
.multi-select-native {
  display: none;
}

.multi-select-wrap {
  position: relative;
  min-width: 130px;
}

.multi-select-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  padding: 6px 10px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 13px;
  background: #fff;
  cursor: pointer;
  min-width: 130px;
  max-width: 180px;
  user-select: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: left !important;
}

.multi-select-trigger:hover {
  border-color: #1890ff;
}

.multi-select-trigger.open {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24,144,255,0.15);
}

.multi-select-arrow {
  font-size: 10px;
  color: #999;
  flex-shrink: 0;
  transition: transform 0.15s;
}

.multi-select-trigger.open .multi-select-arrow {
  transform: rotate(180deg);
}

/* Ensure trigger label is always left-aligned */
.multi-select-trigger > span:first-child {
  text-align: left !important;
  flex: 1;
  min-width: 0;
}

/* Prevent pivot modal from clipping dropdown */
#pivot-modal-overlay .modal,
#study-pivot-modal-overlay .modal {
  overflow: visible;
}

.multi-select-count {
  background: #1890ff;
  color: #fff;
  border-radius: 10px;
  padding: 0 6px;
  font-size: 11px;
  font-weight: 600;
  flex-shrink: 0;
}

.multi-select-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 2px;
  background: #fff;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.12);
  z-index: 2000;
  min-width: 180px;
  max-width: calc(100vw - 16px);
  max-height: 240px;
  overflow-y: auto;
}

.multi-select-dropdown.open {
  display: block;
}

.multi-select-dropdown label {
  display: flex;
  align-items: center;
  justify-content: flex-start !important;
  gap: 8px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.1s;
  white-space: nowrap;
  text-align: left !important;
}

.multi-select-dropdown label:hover {
  background: #e6f7ff;
}

.multi-select-dropdown label input[type="checkbox"] {
  margin: 0;
  flex-shrink: 0;
}

.multi-select-label-text {
  text-align: left !important;
}

/* ===== Two-column Layout ===== */
/* Height comes from flex within .view (100dvh) — no magic toolbar px */
.columns {
  display: flex;
  flex: 1;
  min-height: 0;
  gap: 0;
}

.panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #fff;
  overflow: hidden;
}

.panel-left {
  border-right: 1px solid #e8e8e8;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f0f0;
}

.panel-header h2 {
  font-size: 16px;
  font-weight: 600;
}

.panel-actions {
  display: flex;
  gap: 8px;
}

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

/* ===== Item List ===== */
.item-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.item-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border: 1px solid #e8e8e8;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  background: #fafafa;
}

.item-card:hover {
  background: #e6f7ff;
  border-color: #91d5ff;
}

.item-card.added {
  opacity: 0.4;
  cursor: default;
}

.item-info {
  flex: 1;
  min-width: 0;
}

.item-info .primary {
  font-size: 14px;
  font-weight: 500;
  color: #1a1a2e;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-info .secondary {
  font-size: 12px;
  color: #888;
  margin-top: 2px;
}

.item-actions {
  display: flex;
  gap: 4px;
  margin-left: 8px;
  flex-shrink: 0;
}

.item-actions button {
  padding: 2px 8px;
  font-size: 13px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  background: transparent;
  color: #888;
  transition: color 0.15s, background 0.15s;
}

.item-actions button:hover {
  background: #f0f0f0;
  color: #333;
}

.btn-bsnote {
  color: #bbb;
  background: #f5f5f5;
  border-radius: 50%;
  width: 22px;
  height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  cursor: pointer;
  font-size: 12px;
  vertical-align: middle;
}

.btn-bsnote.has-note {
  color: #fff;
  background: #1890ff;
  font-weight: bold;
}

.btn-bsnote:hover {
  color: #fff;
  background: #40a9ff;
}

.item-actions .btn-copy:hover {
  color: #52c41a;
}

.item-actions .btn-edit:hover {
  color: #1890ff;
}

.item-actions .btn-delete:hover {
  color: #ff4d4f;
}

/* ===== Selected List ===== */
#selected-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

#selected-list .template-group {
  flex-basis: 100%;
}

/* Study editor items list — also needs template group full-width */
#study-editor-study-items {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

#study-editor-study-items .template-group {
  flex-basis: 100%;
}

.selected-item {
  position: relative;
  display: flex;
  align-items: center;
  padding: 6px 24px 6px 8px;
  border: 1px solid #bae7ff;
  border-radius: 4px;
  background: #e6f7ff;
  gap: 4px;
  font-size: 12px;
  max-width: 100%;
}

.selected-item .drag-handle {
  cursor: grab;
  color: #999;
  font-size: 12px;
  user-select: none;
  flex-shrink: 0;
  padding: 0 2px;
}

.selected-item .selected-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 1px;
}

.selected-item .selected-info .primary {
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.selected-item .selected-info .secondary {
  font-size: 11px;
  color: #888;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.btn-sel-bsnote {
  position: absolute;
  top: 4px;
  right: 26px;
  background: none;
  border: none;
  color: #bbb;
  font-size: 11px;
  cursor: pointer;
  padding: 2px 4px;
  line-height: 1;
}

.btn-sel-bsnote.has-note {
  color: #1890ff;
}

.btn-sel-bsnote:hover {
  color: #40a9ff;
}

.btn-remove {
  position: absolute;
  top: 4px;
  right: 4px;
  background: none;
  border: none;
  color: #ff4d4f;
  font-size: 13px;
  cursor: pointer;
  padding: 0 2px;
  line-height: 1;
}

.btn-remove:hover {
  color: #ff1f1f;
}

/* Drag ghost */
.sortable-ghost {
  opacity: 0.3;
}

.sortable-chosen {
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

/* Multi-drag selected items */
.sortable-selected {
  background: #e6f7ff !important;
  box-shadow: inset 0 0 0 1px #1890ff;
}

/* ===== Buttons ===== */
.btn {
  padding: 6px 14px;
  font-size: 13px;
  border-radius: 4px;
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.15s;
}

.btn-primary {
  background: #1890ff;
  color: #fff;
  border-color: #1890ff;
}

.btn-primary:hover {
  background: #40a9ff;
  border-color: #40a9ff;
}

.btn-outline {
  background: #fff;
  color: #555;
  border-color: #d9d9d9;
}

.btn-outline:hover {
  color: #1890ff;
  border-color: #1890ff;
}

.btn-close {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: #999;
  line-height: 1;
}

.btn-close:hover {
  color: #333;
}

/* Touch devices: remove 300ms tap delay / double-tap zoom / gray flash */
button, .item-card, .apr-item, .multi-select-trigger, .apr-select-list .apr-option {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* ===== Pagination ===== */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 8px 12px;
  border-top: 1px solid #f0f0f0;
}

.page-info {
  font-size: 12px;
  color: #888;
  margin-right: 8px;
}

.btn-page {
  padding: 3px 8px;
  font-size: 12px;
  border: 1px solid #d9d9d9;
  border-radius: 3px;
  background: #fff;
  cursor: pointer;
  font-family: inherit;
  color: #555;
}

.btn-page:hover:not([disabled]) {
  border-color: #1890ff;
  color: #1890ff;
}

.btn-page[disabled] {
  opacity: 0.4;
  cursor: default;
}

.btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.page-current {
  padding: 3px 8px;
  font-size: 12px;
  background: #1890ff;
  color: #fff;
  border-radius: 3px;
}

/* ===== Empty State ===== */
.empty-state {
  display: none;
  text-align: center;
  color: #bbb;
  padding: 40px 0;
  font-size: 14px;
}

.empty-state:only-child {
  display: block;
}

/* ===== Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-overlay.hidden {
  display: none;
}

/* Item edit modal must appear above pivot/other modals */
#modal-overlay {
  z-index: 1100;
}

.modal {
  background: #fff;
  border-radius: 8px;
  width: 480px;
  max-width: 90vw;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  border-bottom: 1px solid #f0f0f0;
}

.modal-header h3 {
  font-size: 14px;
  font-weight: 600;
  margin: 0;
  line-height: 1.3;
}

.modal-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.form-group label {
  font-size: 13px;
  font-weight: 600;
  color: #555;
}

.form-group input {
  padding: 8px 10px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.15s;
}

.form-group input:focus {
  outline: none;
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24,144,255,0.15);
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid #f0f0f0;
}

/* ===== Import Modal ===== */
.modal-wide {
  width: 920px;
  max-width: 95vw;
}

.form-group textarea {
  padding: 10px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 13px;
  font-family: inherit;
  resize: vertical;
  line-height: 1.6;
}

.form-group textarea:focus {
  outline: none;
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24,144,255,0.15);
}

/* Modal selects (Save/Load APR dropdowns) */
.form-group select {
  padding: 8px 10px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 14px;
  background: #fff;
  font-family: inherit;
}

.form-group select:focus {
  outline: none;
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24,144,255,0.15);
}

/* APR summary line */
.apr-summary {
  font-size: 12px;
  color: #888;
  background: #fafafa;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  padding: 8px 12px;
}

/* APR select list (filterable, replaces dropdown) */
.apr-select-list {
  max-height: 200px;
  overflow-y: auto;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  background: #fff;
}

.apr-select-list .apr-option {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid #f0f0f0;
  font-size: 13px;
  transition: background 0.1s;
}

.apr-select-list .apr-option:last-child {
  border-bottom: none;
}

.apr-select-list .apr-option:hover {
  background: #e6f7ff;
}

.apr-select-list .apr-option.selected {
  background: #bae7ff;
  font-weight: 600;
}

.apr-select-list .apr-option .apr-option-name {
  font-weight: 500;
}

.apr-select-list .apr-option .apr-option-count {
  color: #999;
  font-size: 12px;
  margin-left: 4px;
}

.apr-select-list .empty-hint {
  padding: 12px;
  text-align: center;
  color: #bbb;
  font-size: 13px;
}

.btn-sm {
  padding: 4px 10px;
  font-size: 12px;
}

.bsnote-display {
  padding: 10px 12px;
  background: #fafafa;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  min-height: 80px;
  max-height: 60vh;
  overflow-y: auto;
  white-space: pre-wrap;
  font-size: 13px;
  line-height: 1.6;
  color: #333;
}

.bsnote-display table {
  width: 100%;
  border-collapse: collapse;
  margin: 6px 0 12px 0;
  font-size: 12px;
}

.bsnote-display table th {
  background: #e6f7ff;
  padding: 4px 8px;
  text-align: left;
  font-weight: 600;
  border: 1px solid #bae7ff;
}

.bsnote-display table td {
  padding: 3px 8px;
  border: 1px solid #e8e8e8;
  vertical-align: top;
}

#bsnote-edit {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 13px;
  font-family: inherit;
  resize: vertical;
  line-height: 1.6;
  min-height: 200px;
}

#bsnote-edit:focus {
  outline: none;
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24,144,255,0.15);
}

.form-group code {
  background: #f5f5f5;
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 12px;
}

.import-preview {
  margin-top: 4px;
}

.import-preview.hidden {
  display: none;
}

.import-summary {
  font-size: 13px;
  font-weight: 600;
  padding: 8px 12px;
  background: #f6ffed;
  border: 1px solid #b7eb8f;
  border-radius: 4px;
  margin-bottom: 8px;
  color: #389e0d;
}

.import-summary.has-errors {
  background: #fffbe6;
  border-color: #ffe58f;
  color: #d48806;
}

.import-table-wrap {
  max-height: 260px;
  overflow: auto;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
}

#import-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

#import-table th {
  position: sticky;
  top: 0;
  background: #fafafa;
  padding: 6px 8px;
  text-align: left;
  font-weight: 600;
  border-bottom: 2px solid #e8e8e8;
  white-space: nowrap;
}

#import-table td {
  padding: 4px 8px;
  border-bottom: 1px solid #f0f0f0;
  white-space: nowrap;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
}

#import-table .status-ok {
  color: #52c41a;
}

#import-table .status-dup {
  color: #faad14;
}

#import-table .status-err {
  color: #ff4d4f;
}

/* ===== Pivot ===== */
.pivot-toolbar {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 10px;
}

/* Scoped to direct-child text inputs only — a bare `input` selector here would
   also hit the multi-select dropdown checkboxes (they are descendants of
   .pivot-toolbar), stretching them with flex:1 and breaking row alignment */
.pivot-toolbar > input[type="text"] {
  flex: 1;
  padding: 6px 10px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 13px;
  font-family: inherit;
}

.pivot-toolbar > input[type="text"]:focus {
  outline: none;
  border-color: #1890ff;
}

.pivot-stats {
  font-size: 12px;
  color: #888;
  white-space: nowrap;
}

#pivot-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

#pivot-table th {
  position: sticky;
  top: 0;
  background: #fafafa;
  padding: 8px 10px;
  text-align: left;
  font-weight: 600;
  border-bottom: 2px solid #e8e8e8;
  white-space: nowrap;
}

#pivot-table td {
  padding: 6px 10px;
  border-bottom: 1px solid #f0f0f0;
  vertical-align: top;
}

#pivot-table .oem-col {
  white-space: nowrap;
  width: 8%;
}
#pivot-table .bs-col {
  font-weight: 600;
  white-space: nowrap;
  width: 6%;
}

#pivot-table .count-col {
  text-align: center;
  color: #1890ff;
}

#pivot-table .remarks-col {
  color: #555;
  line-height: 1.7;
}

.pivot-remark-row {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 1px 0;
}

.pivot-remark-text {
  flex: 1;
}

.pivot-remark-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
  opacity: 0.4;
  transition: opacity 0.15s;
}

.pivot-remark-row:hover .pivot-remark-actions {
  opacity: 1;
}

.btn-pivot-copy, .btn-pivot-edit, .btn-pivot-delete {
  padding: 1px 5px;
  font-size: 11px;
  border: none;
  border-radius: 3px;
  cursor: pointer;
  background: transparent;
  color: #888;
  line-height: 1.4;
}

.btn-pivot-copy:hover { color: #52c41a; background: #f0f0f0; }
.btn-pivot-edit:hover { color: #1890ff; background: #f0f0f0; }
.btn-pivot-delete:hover { color: #ff4d4f; background: #f0f0f0; }

#pivot-table .note-col {
  white-space: nowrap;
  text-align: center;
}

#pivot-table .btn-note-edit {
  background: #f5f5f5;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  font-size: 13px;
  color: #bbb;
  width: 24px;
  height: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#pivot-table .btn-note-edit.has-note {
  color: #fff;
  background: #1890ff;
}

#pivot-table .btn-note-edit:hover {
  background: #40a9ff;
  color: #fff;
}

/* ===== View System ===== */
.view {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh; /* dynamic viewport — excludes iOS Safari chrome */
}

.view.hidden {
  display: none;
}

/* ===== APR Manager View ===== */
.aprmgr-left {
  max-width: 380px;
}

.aprmgr-right {
  flex: 1;
}

/* Mobile-only APR dropdown (hidden on desktop — list panel is used instead) */
.aprmgr-select-bar {
  display: none;
}

.aprmgr-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

/* APR item (left panel — clickable row) */
.apr-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}

.apr-item:hover {
  background: #f5f5f5;
  border-color: #e8e8e8;
}

.apr-item-selected {
  background: #e6f7ff;
  border-color: #91d5ff;
}

.apr-item-selected:hover {
  background: #d4efff;
}

.apr-item-archived {
  opacity: 0.55;
}

.apr-item-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 2px;
}

.apr-item-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.apr-item-meta {
  font-size: 11px;
  color: #999;
}

.apr-item-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  margin-left: 8px;
}

.apr-item-date {
  font-size: 11px;
  color: #bbb;
}

/* APR detail bar (right panel — info + actions) */
.apr-detail-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: #fafafa;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  margin-bottom: 10px;
}

.apr-detail-meta {
  font-size: 12px;
  color: #888;
}

.aprmgr-detail-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* Study row (right panel) */
.study-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: #fff;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  font-size: 13px;
}

.study-row.study-row-archived {
  opacity: 0.55;
}

.study-row:hover {
  filter: brightness(0.96);
}

.study-row.status-upcoming { border-left: 3px solid #1890ff; background: #f0f8ff; }
.study-row.status-in-progress { border-left: 3px solid #fa8c16; background: #fffaf0; }
.study-row.status-on-hold { border-left: 3px solid #8c8c8c; background: #f5f5f5; }
.study-row.status-completed { border-left: 3px solid #389e0d; background: #f0fff0; }
.study-row.status-cancelled { border-left: 3px solid #8c8c8c; background: #f5f5f5; }

.study-row-name {
  font-weight: 600;
  flex: 1;
}

.study-row-count {
  font-size: 12px;
  color: #888;
}

.study-row-date {
  font-size: 11px;
  color: #bbb;
}
.study-row-due {
  white-space: nowrap;
}
.study-row.due-date-overdue { border-left: 3px solid #cf1322; background: #fff0f0; }
#study-pivot-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
#study-pivot-table th {
  position: sticky;
  top: 0;
  background: #fafafa;
  padding: 8px 10px;
  text-align: left;
  font-weight: 600;
  border-bottom: 2px solid #e8e8e8;
  white-space: nowrap;
}
#study-pivot-table td {
  padding: 6px 10px;
  border-bottom: 1px solid #f0f0f0;
  vertical-align: top;
}
#study-pivot-table tr.due-date-overdue { background: #fff0f0; }
.due-date-overdue {
  color: #cf1322 !important;
  font-weight: 600;
}
.due-date-soon {
  color: #fa8c16 !important;
  font-weight: 600;
}

/* ===== Study View (read-only table) ===== */
.study-view-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
}

#study-view-table, #apr-view-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

#study-view-table th, #apr-view-table th {
  position: sticky;
  top: 0;
  background: #fafafa;
  padding: 8px 10px;
  text-align: left;
  font-weight: 600;
  border-bottom: 2px solid #e8e8e8;
  white-space: nowrap;
}

#study-view-table td, #apr-view-table td {
  padding: 6px 10px;
  border-bottom: 1px solid #f0f0f0;
}

#apr-view-table tbody tr:nth-child(even),
#study-view-table tbody tr:nth-child(even) {
  background: #fafafa;
}

#apr-view-table tbody tr:nth-child(odd),
#study-view-table tbody tr:nth-child(odd) {
  background: #fff;
}

#study-view-table tbody tr:hover, #apr-view-table tbody tr:hover {
  background: #fafafa;
}

th.sortable {
  cursor: pointer;
  user-select: none;
}

th.sortable:hover {
  background: #e6f7ff;
}

th.sort-asc::after {
  content: ' ▲';
  font-size: 10px;
}

th.sort-desc::after {
  content: ' ▼';
  font-size: 10px;
}

/* ===== Template Expansion Groups ===== */
.template-group {
  display: flex;
  flex-direction: column;
  border: 1px solid #bae7ff;
  border-radius: 6px;
  overflow: hidden;
  background: #fff;
}

.template-parent {
  position: relative;
  display: flex;
  align-items: center;
  padding: 6px 50px 6px 8px;
  background: #e6f7ff;
  gap: 6px;
  cursor: pointer;
  user-select: none;
}

.template-parent .drag-handle {
  cursor: grab;
  color: #999;
  font-size: 12px;
  flex-shrink: 0;
  padding: 0 2px;
}

.template-chevron {
  font-size: 10px;
  color: #666;
  transition: transform 0.2s;
  flex-shrink: 0;
  width: 16px;
  text-align: center;
}

.template-chevron.expanded {
  transform: rotate(90deg);
}

.template-parent-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.template-parent-info .primary {
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.template-parent-info .secondary {
  font-size: 11px;
  color: #888;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.template-badge {
  background: #1890ff;
  color: #fff;
  border-radius: 10px;
  padding: 0 6px;
  font-size: 11px;
  font-weight: 600;
  flex-shrink: 0;
}

.template-children {
  display: none;
  flex-direction: column;
}

.template-children.expanded {
  display: flex;
}

.template-child {
  display: flex;
  align-items: center;
  padding: 4px 8px 4px 28px;
  background: #fafafa;
  border-top: 1px solid #f0f0f0;
  font-size: 11px;
  gap: 6px;
}

.template-child .child-index {
  color: #999;
  font-size: 10px;
  flex-shrink: 0;
  min-width: 18px;
}

.template-child .child-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.template-child .child-info .primary {
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
}

.template-child .child-info .secondary {
  font-size: 10px;
  color: #aaa;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%);
  background: #1a1a2e;
  color: #fff;
  padding: 10px 24px;
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  z-index: 2000;
  pointer-events: none;
  transition: opacity 0.3s, transform 0.3s;
  opacity: 1;
}

.toast.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(12px);
}


/* ===== Badges ===== */
.badge-archived {
  display: inline-flex;
  align-items: center;
  padding: 1px 8px;
  border-radius: 10px;
  font-size: 11px;
  line-height: 1.6;
  background: #fff1f0;
  color: #cf1322;
}

.badge-active {
  display: inline-flex;
  align-items: center;
  padding: 1px 8px;
  border-radius: 10px;
  font-size: 11px;
  line-height: 1.6;
  background: #f6ffed;
  color: #389e0d;
}

/* Study status badges */
.status-upcoming { background: #e6f7ff; color: #1890ff; }
.status-in-progress { background: #fff7e6; color: #fa8c16; }
.status-on-hold { background: #f5f5f5; color: #8c8c8c; }
.status-completed { background: #f6ffed; color: #389e0d; }
.status-cancelled { background: #f5f5f5; color: #8c8c8c; }

.study-status-select {
  font-size: 11px;
  padding: 2px 6px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-family: inherit;
  cursor: pointer;
  background: #fff;
}

.study-status-select:focus {
  outline: none;
  border-color: #1890ff;
}

/* Study Notes & APR Notes fullscreen edit */
#study-notes-modal-overlay.fullscreen,
#apr-notes-modal-overlay.fullscreen {
  align-items: stretch;
  padding: 0;
}

#study-notes-modal-overlay.fullscreen .modal,
#apr-notes-modal-overlay.fullscreen .modal {
  width: 100vw !important;
  max-width: 100vw !important;
  height: 100vh !important;
  height: 100dvh !important;
  max-height: 100vh !important;
  max-height: 100dvh !important;
  border-radius: 0;
  display: flex;
  flex-direction: column;
}

/* Hide attachments while editing notes fullscreen */
#study-notes-modal-overlay.fullscreen .note-attachments,
#apr-notes-modal-overlay.fullscreen .note-attachments {
  display: none;
}

/* ═══════════════ Note Attachments ═══════════════ */
.note-attachments {
  margin-top: 12px;
  border-top: 1px solid #f0f0f0;
  padding-top: 10px;
}

.note-attachments-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.note-attachments-title {
  font-size: 11px;
  font-weight: 600;
  color: #999;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.note-attachment-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 140px;
  overflow-y: auto;
}

.note-attachment-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 10px;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  background: #fafafa;
  font-size: 12px;
}

.note-attachment-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #1a1a2e;
}

.note-attachment-size {
  color: #999;
  flex-shrink: 0;
}

.note-attachment-empty {
  font-size: 12px;
  color: #bbb;
  padding: 4px 2px;
}

/* ═══════════════ Login View ═══════════════ */
#view-login {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  min-height: 100dvh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

#view-login.hidden {
  display: none;
}

.login-container {
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.15);
  padding: 40px;
  width: 400px;
  max-width: 90vw;
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.login-header h1 {
  font-size: 28px;
  font-weight: 700;
  color: #1a1a2e;
  margin: 0 0 4px;
}

.login-header p {
  font-size: 14px;
  color: #888;
  margin: 0;
}

.login-form .form-group {
  margin-bottom: 16px;
}

.login-form .form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: #555;
  margin-bottom: 4px;
}

.login-form .form-group input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
  transition: border-color 0.2s;
}

.login-form .form-group input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102,126,234,0.2);
}

.login-error.hidden {
  display: none;
}

.login-error {
  color: #ff4d4f;
  font-size: 13px;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: #fff2f0;
  border: 1px solid #ffccc7;
  border-radius: 4px;
}

.login-btn {
  width: 100%;
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
  border-radius: 6px;
  cursor: pointer;
  margin-top: 8px;
}

.login-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* ═══════════════ User Menu (docked into visible view's toolbar) ═══════════════ */
.user-menu.hidden {
  display: none;
}

.user-menu {
  position: relative;
  flex-shrink: 0;
  margin-left: 8px;
}

.user-menu-trigger {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  background: #fff;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  color: #333;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.user-menu-trigger:hover {
  border-color: #1890ff;
  box-shadow: 0 2px 8px rgba(24,144,255,0.15);
}

.user-avatar {
  font-size: 16px;
  line-height: 1;
}

#user-menu-name {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-menu-arrow {
  font-size: 10px;
  color: #888;
  margin-left: 2px;
}

.user-menu-dropdown.hidden {
  display: none;
}

.user-menu-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  background: #fff;
  border: 1px solid #d9d9d9;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  min-width: 200px;
  overflow: hidden;
  z-index: 1001;
}

.user-menu-info {
  padding: 12px 16px 8px;
  border-bottom: 1px solid #f0f0f0;
  font-size: 12px;
  color: #888;
}

.user-menu-info strong {
  display: block;
  font-size: 14px;
  color: #1a1a2e;
  margin-top: 2px;
}

.user-menu-dropdown button {
  display: block;
  width: 100%;
  padding: 10px 16px;
  border: none;
  background: none;
  text-align: left;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  color: #333;
  transition: background 0.15s;
}

.user-menu-dropdown button:hover {
  background: #f5f5f5;
}

#user-menu-logout-btn {
  color: #ff4d4f;
  border-top: 1px solid #f0f0f0;
}

/* ═══════════════ User Settings Modal ═══════════════ */
/* ═══════════════ User Settings Modal ═══════════════ */
.settings-body {
  padding: 16px 20px;
  background: #fafafa;
  max-height: 72vh;
  overflow-y: auto;
}

.settings-identity {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 2px 2px 14px;
}

.settings-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: #1890ff;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: 600;
  flex-shrink: 0;
  text-transform: uppercase;
}

.settings-identity-name {
  font-size: 14px;
  font-weight: 600;
  color: #1a1a2e;
}

.settings-identity-sub {
  font-size: 11px;
  color: #999;
  margin-top: 1px;
}

.settings-group {
  margin-bottom: 14px;
}

.settings-group-title {
  font-size: 11px;
  font-weight: 600;
  color: #999;
  letter-spacing: 1px;
  text-transform: uppercase;
  margin: 0 2px 6px;
}

.settings-card {
  background: #fff;
  border: 1px solid #e8e8e8;
  border-radius: 6px;
  overflow: hidden;
}

.settings-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
}

.settings-row + .settings-row,
.settings-row + .settings-edit-area,
.settings-edit-area + .settings-row,
.settings-edit-area + .settings-edit-area {
  border-top: 1px solid #f0f0f0;
}

.settings-row-label {
  width: 110px;
  flex-shrink: 0;
  font-size: 12px;
  color: #888;
}

.settings-row-value {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  color: #1a1a2e;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.settings-edit-area {
  padding: 12px 14px;
  background: #fcfcfc;
}

.settings-edit-area label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: #888;
  margin: 10px 0 4px;
}

.settings-edit-area label:first-of-type {
  margin-top: 0;
}

.settings-edit-area input {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 13px;
  font-family: inherit;
  background: #fff;
}

.settings-edit-area input:focus {
  outline: none;
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24,144,255,0.15);
}

.settings-inline-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.settings-edit-area .settings-inline-row input {
  flex: 1;
  width: auto;
  min-width: 0;
}

.settings-edit-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
}

.settings-edit-actions .settings-msg,
.settings-inline-row .settings-msg {
  margin-left: 0;
}

.settings-hint {
  font-size: 12px;
  color: #888;
  margin: 0 0 8px;
}

.settings-badge {
  display: inline-flex;
  align-items: center;
  padding: 1px 8px;
  border-radius: 10px;
  font-size: 11px;
  line-height: 1.6;
  border: 1px solid transparent;
}

.settings-badge-on {
  background: #f6ffed;
  color: #389e0d;
  border-color: #b7eb8f;
}

.settings-badge-off {
  background: #fff1f0;
  color: #cf1322;
  border-color: #ffa39e;
}

.btn-danger {
  background: #fff;
  color: #ff4d4f;
  border-color: #ffa39e;
}

.btn-danger:hover {
  background: #fff1f0;
  color: #cf1322;
  border-color: #ff4d4f;
}

/* Hidden states (JS toggles .hidden on these) */
.settings-edit-area.hidden,
#profile-display-row.hidden,
#password-display-row.hidden,
#totp-status-display.hidden,
#settings-enable-totp.hidden {
  display: none;
}

.settings-msg {
  font-size: 12px;
  margin-left: 8px;
  vertical-align: middle;
}

.api-key-display {
  flex: 1;
  padding: 8px 12px;
  background: #f5f5f5;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 12px;
  font-family: "Cascadia Code", "Fira Code", "JetBrains Mono", monospace;
  word-break: break-all;
  user-select: all;
}

/* ═══════════════ TOTP ═══════════════ */
#login-totp-group.hidden {
  display: none;
}

#login-totp {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  font-size: 18px;
  font-family: inherit;
  letter-spacing: 6px;
  text-align: center;
  transition: border-color 0.2s;
}

#login-totp:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102,126,234,0.2);
}

#totp-qr-container {
  text-align: center;
  margin: 10px 0;
}

#totp-qr-container img {
  width: 180px;
  height: 180px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  image-rendering: pixelated;
}

#totp-verify-code {
  letter-spacing: 4px;
  text-align: center;
}

/* ═══════════════ Mobile (≤768px — covers iPhone 15 Pro Max 430px portrait) ═══════════════ */
@media (max-width: 768px) {
  /* iOS Safari auto-zooms on focus when an input's font is under 16px.
     !important beats the few inline font-size styles (status select, rename inputs). */
  input, select, textarea {
    font-size: 16px !important;
  }

  /* ── Toolbars: ordered rows instead of ragged wrapping ──
     Row 1: back/title … user menu · Row 2: filters/meta · Row 3: actions (even widths) */
  .toolbar {
    flex-wrap: wrap;
    gap: 8px 6px;
    padding: 8px 10px;
  }

  /* The spacer becomes an invisible full-width line break: basis 100% makes
     it wrap onto its own 0-height line, so the header row (back/title/user)
     always stays separate from the filter/button rows below it. NOTE: items
     after it must have a NON-ZERO flex-basis — a 0-basis item would still
     fit on the spacer's line (100% + 0 ≤ 100%) and collapse to zero width. */
  .filter-spacer {
    display: block;
    order: 3;
    flex: 0 0 100%;
    height: 0;
  }

  /* .btn in the attribute selector — needs higher specificity than the
     .toolbar > .btn stretch rule below (same 0-2-0 would lose on order) */
  .toolbar > .btn[id$="-back"] {
    order: 0;
    flex: 0 0 auto;
  }

  .toolbar > h2 {
    order: 1;
    min-width: 0;
    max-width: 46vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .toolbar > span,
  .toolbar > select {
    order: 1;
    min-width: 0;
  }

  .toolbar > .user-menu {
    order: 2;
    margin-left: auto;
  }

  /* Meta spans in the study detail view drop to their own row */
  #study-view-apr-ref,
  #study-view-due-date,
  #study-view-updated {
    order: 3;
  }

  .toolbar > .filter-group {
    order: 4;
    flex: 1 1 90px; /* non-zero basis — see .filter-spacer note */
    min-width: 0;
    gap: 4px;
  }

  .toolbar > .filter-group label {
    font-size: 11px;
  }

  .toolbar > .filter-group .multi-select-wrap {
    flex: 1;
    min-width: 0;
  }

  /* All direct-child action buttons share rows evenly */
  .toolbar > .btn {
    order: 10;
    flex: 1 1 80px; /* non-zero basis — see .filter-spacer note */
    min-width: 0;
  }

  /* Items view toolbar: 3 action buttons get two roomier rows —
     Pivot/Import share one row, primary + Add Item spans the next */
  #view-items .toolbar > .btn {
    flex: 1 1 40%;
  }

  #view-items .toolbar > #items-btn-add {
    order: 11;
  }

  .toolbar .btn {
    min-height: 36px;
    padding: 6px 8px;
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .multi-select-wrap {
    min-width: 0;
  }

  .multi-select-trigger {
    min-width: 0;
    width: 100%;
    max-width: none;
    min-height: 36px;
    padding: 6px 8px;
    font-size: 12px;
  }

  .user-menu-trigger {
    padding: 6px 8px;
    min-height: 36px;
  }

  #user-menu-name {
    max-width: 88px;
    font-size: 12px;
  }

  /* ── Segmented controls: Active/Archived/All triplets ──
     (APR Manager toolbar div + study filter span, both inline-styled) */
  #view-aprs .toolbar > div:not(.filter-spacer),
  .apr-detail-bar > span:last-child {
    display: flex;
    gap: 0 !important; /* beats inline gap */
  }

  #view-aprs .toolbar > div:not(.filter-spacer) {
    order: 6;
    flex: 1 1 100%;
  }

  #view-aprs .toolbar > div:not(.filter-spacer) .btn,
  .apr-detail-bar > span:last-child .btn {
    flex: 1;
    border-radius: 0;
    margin-left: -1px;
  }

  #view-aprs .toolbar > div:not(.filter-spacer) .btn:first-child,
  .apr-detail-bar > span:last-child .btn:first-child {
    border-radius: 6px 0 0 6px;
    margin-left: 0;
  }

  #view-aprs .toolbar > div:not(.filter-spacer) .btn:last-child,
  .apr-detail-bar > span:last-child .btn:last-child {
    border-radius: 0 6px 6px 0;
  }

  /* Active segment sits above neighbours so its blue border isn't eaten */
  #view-aprs .toolbar > div:not(.filter-spacer) .btn-primary,
  .apr-detail-bar > span:last-child .btn-primary {
    position: relative;
    z-index: 1;
  }

  /* ── Two-column views stack vertically ──
     Space follows content instead of a rigid 50/50 split: source lists
     take the remaining space, target panels size to content but are
     capped so they never swallow the screen (slim strip when empty) */
  .columns {
    flex-direction: column;
  }

  .panel-left {
    border-right: none;
    border-bottom: 1px solid #e8e8e8;
  }

  .aprmgr-left {
    max-width: none;
  }

  /* Editors: items list gets the space; selected/study items cap at 45% */
  #view-apr-editor .panel-left,
  #view-study-editor .panel-left {
    flex: 1 1 auto;
    min-height: 0;
  }

  #view-apr-editor .panel-right,
  #view-study-editor .panel-right {
    flex: 0 1 auto;
    max-height: 45%;
  }

  /* APR Manager: APR list caps at 42%, studies detail gets the rest */
  #view-aprs .aprmgr-left {
    flex: 0 1 auto;
    max-height: 42%;
  }

  #view-aprs .aprmgr-right {
    flex: 1 1 auto;
    min-height: 0;
  }

  /* ── APR picker: native <select> replaces the list on phones ── */
  .aprmgr-select-bar {
    display: block;
    padding: 8px 10px;
  }

  .aprmgr-select-bar select {
    width: 100%;
    min-height: 40px;
    padding: 8px 10px;
    border: 1px solid #d9d9d9;
    border-radius: 6px;
    background: #fff;
    font-family: inherit;
  }

  /* The dropdown replaces them — hide the header and the list panel body */
  #view-aprs .aprmgr-left .panel-header,
  #view-aprs .aprmgr-left .panel-body {
    display: none;
  }

  .panel-header {
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 10px;
  }

  /* Button groups (Selected panel 5 btns, APR detail 7 btns) take their own
     full-width row and form an even 3-per-row grid */
  .panel-actions {
    flex: 1 1 100%;
    flex-wrap: wrap;
    gap: 6px;
  }

  .panel-actions .btn {
    flex: 1 1 30%;
    min-width: 0;
    min-height: 34px;
    padding: 5px 6px;
    font-size: 12px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* APR info bar: meta on top, segmented study filter below */
  .apr-detail-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  /* Study rows: name on row 1, meta chips row 2, buttons in a 3-col grid row 3 */
  .study-row {
    flex-wrap: wrap;
    gap: 6px 10px;
  }

  .study-row-name {
    flex: 1 1 100%;
  }

  .study-row > div {
    flex: 1 1 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
  }

  .study-row > div .btn {
    flex: 1 1 28%;
    min-height: 34px;
    font-size: 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Study pivot status filter buttons wrap evenly */
  .pivot-toolbar .study-pivot-filter {
    flex: 1 1 28%;
    min-height: 34px;
  }

  /* Item card action buttons: 36px targets with real spacing between them */
  .item-actions {
    gap: 8px;
  }

  .item-actions button {
    padding: 6px;
    font-size: 15px;
    min-width: 36px;
    min-height: 36px;
  }

  /* Keep the ⓘ note button a circle — the padding/min-width above would
     otherwise squash it into a pill */
  .item-actions .btn-bsnote {
    width: 32px;
    min-width: 32px;
    height: 32px;
    min-height: 32px;
    padding: 0;
    border-radius: 50%;
    font-size: 13px;
  }

  .pagination {
    flex-wrap: wrap;
  }

  /* ── Modals: near full-width, body scrolls internally ──
     !important overrides inline widths (1500px pivot, 1200px bsnote, …) */
  .modal {
    width: calc(100vw - 16px) !important;
    max-width: calc(100vw - 16px) !important;
    max-height: calc(100dvh - 24px);
    display: flex;
    flex-direction: column;
  }

  .modal-header {
    padding: 10px 14px;
    flex-shrink: 0;
  }

  .modal-body {
    padding: 14px;
    overflow-y: auto;
    flex: 1;
    min-height: 0;
  }

  .modal-footer {
    padding: 10px 14px;
    flex-shrink: 0;
    flex-wrap: wrap;
  }

  /* Footer actions split the row evenly (Cancel/Save = half each) */
  .modal-footer .btn {
    flex: 1;
    min-height: 40px;
    font-size: 14px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .btn-close {
    padding: 4px 10px;
    font-size: 26px;
  }

  /* Pivot modal filter groups share the row like the main toolbars */
  .pivot-toolbar .filter-group {
    flex: 1 1 40%;
    min-width: 0;
  }

  .pivot-toolbar .filter-group .multi-select-wrap {
    flex: 1;
    min-width: 0;
  }

  .pivot-toolbar {
    flex-wrap: wrap;
  }

  #pivot-modal-overlay .import-table-wrap,
  #study-pivot-modal-overlay .import-table-wrap {
    height: 50dvh !important;
  }

  /* ── Read-only detail tables (APR/Study view): wrap scrolls both axes ── */
  .study-view-body {
    padding: 8px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  #view-apr-view .import-table-wrap,
  #view-study-view .import-table-wrap {
    flex: 1;
    min-height: 0;
    max-height: none !important; /* overrides inline calc(100vh - 70px) */
  }

  #apr-view-table,
  #study-view-table,
  #pivot-table,
  #study-pivot-table,
  #import-table {
    font-size: 12px;
  }

  /* Inline-width rename inputs (300px) — cap so they fit */
  #study-view-name-edit,
  #study-notes-title-edit {
    max-width: 55vw;
  }

  /* ── Settings modal ── */
  .settings-row {
    flex-wrap: wrap;
  }

  .settings-row-label {
    width: auto;
    min-width: 90px;
  }

  .settings-inline-row {
    flex-wrap: wrap;
  }

  #totp-qr-container img {
    width: 150px;
    height: 150px;
  }

  /* ── Login ── */
  .login-container {
    padding: 24px 20px;
  }

  .bsnote-display {
    max-height: 45vh;
  }

  /* ── Easier touch operation ── */

  /* Tap feedback — touch devices have no :hover */
  .btn:active {
    opacity: 0.7;
  }

  .item-card:active {
    background: #e6f7ff;
    border-color: #91d5ff;
  }

  .apr-item:active {
    background: #f0f0f0;
  }

  /* No accidental text selection / long-press callout while tapping & dragging */
  .item-card,
  .apr-item,
  .selected-item,
  .template-parent,
  .study-row {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
  }

  /* Taller item cards — the main tap surface */
  .item-card {
    padding: 12px;
  }

  .item-info .primary {
    font-size: 15px;
  }

  .item-info .secondary {
    font-size: 13px;
  }

  /* ⓘ note button sizing lives in the .item-actions .btn-bsnote rule above;
     study-pivot table instances carry their own inline sizes */

  /* Selected chips: bigger ✕ / ⓘ hit areas, text kept clear of them */
  .selected-item {
    padding: 8px 58px 8px 8px;
  }

  .btn-remove,
  .btn-sel-bsnote {
    min-width: 30px;
    min-height: 30px;
    padding: 4px;
    font-size: 15px;
    text-align: center;
  }

  .btn-sel-bsnote {
    right: 30px;
  }

  .template-parent {
    padding: 10px 64px 10px 8px;
  }

  /* Multi-select dropdown rows: finger-sized */
  .multi-select-dropdown {
    max-height: 50vh;
  }

  .multi-select-dropdown label {
    padding: 11px 14px;
    font-size: 14px;
  }

  .multi-select-dropdown input[type="checkbox"] {
    width: 18px;
    height: 18px;
  }

  /* Pagination buttons */
  .btn-page,
  .page-current {
    min-height: 36px;
    padding: 6px 12px;
    font-size: 13px;
  }

  /* Save-APR modal option rows */
  .apr-select-list .apr-option {
    padding: 12px;
  }

  /* Form inputs & toolbar selects: comfortable tap height */
  .form-group input,
  .form-group select {
    min-height: 40px;
  }

  .toolbar select {
    min-height: 36px;
  }

  /* Note attachment rows & their buttons */
  .note-attachment-row {
    padding: 8px 10px;
  }

  .note-attachment-row .btn {
    min-height: 34px;
  }

  /* User menu dropdown rows */
  .user-menu-dropdown button {
    padding: 12px 16px;
    font-size: 14px;
  }

  /* Pivot remark actions: no hover on touch — always visible, bigger */
  .pivot-remark-actions {
    opacity: 1;
  }

  .btn-pivot-copy,
  .btn-pivot-edit,
  .btn-pivot-delete {
    padding: 5px 8px;
    font-size: 13px;
    min-height: 30px;
  }

  /* Keep scroll inside the list being touched (no chaining to page) */
  .panel-body,
  .modal-body,
  .import-table-wrap,
  .multi-select-dropdown,
  .apr-select-list {
    overscroll-behavior: contain;
  }

  /* Home-indicator safe area: last row must not hide under it */
  .panel-body {
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  }

  .study-view-body {
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  }

  .modal-footer {
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  }

  .toast {
    max-width: calc(100vw - 32px);
    bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  }
}

/* ═══════════════ Study Gantt ═══════════════ */
#view-gantt .view-body {
  flex: 1;
  min-height: 0;
  overflow: hidden;
  padding: 0;
}

.gantt-scroll {
  width: 100%;
  height: 100%;
  overflow-x: auto;
  overflow-y: auto;
  background: #fff;
  position: relative;
}

.gantt-canvas {
  position: relative;
  min-width: 100%;
}

.gantt-header,
.gantt-row {
  display: flex;
  align-items: center;
}

.gantt-header {
  position: sticky;
  top: 0;
  z-index: 6;
  background: #fff;
  border-bottom: 1px solid #e8e8e8;
}

.gantt-header-label,
.gantt-row-label {
  flex: 0 0 var(--label-w, 210px);
  position: sticky;
  left: 0;
  z-index: 4;
  background: #fff;
  border-right: 1px solid #e8e8e8;
  padding: 0 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  box-sizing: border-box;
}

.gantt-header-label {
  z-index: 7;
  font-weight: 600;
  font-size: 12px;
  line-height: 40px;
}

.gantt-row-label {
  font-size: 12px;
  line-height: 40px;
}

.gantt-group-row {
  background: #f7f8fa;
}

.gantt-group-label {
  font-weight: 600;
  background: #f7f8fa;
}

.gantt-study-label {
  padding-left: calc(8px + 2ch);
  line-height: normal;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.gantt-study-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.gantt-study-meta {
  font-size: 10px;
  color: #999;
  line-height: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.gantt-study-row {
  cursor: pointer;
}

.gantt-study-row:hover {
  background: rgba(24, 144, 255, 0.05);
}

.gantt-study-row:hover .gantt-row-label {
  background: #f0f7ff;
}

.gantt-row-label .gantt-apr {
  color: #999;
  font-size: 10px;
  margin-left: 6px;
}

.gantt-header-days,
.gantt-row-track {
  flex: 0 0 auto;
  position: relative;
  height: 100%;
}

.gantt-header-days {
  border-bottom: 1px solid #f0f0f0;
}

.gantt-row {
  position: relative;
  z-index: 1;
  border-bottom: 1px solid #f5f5f5;
}

.gantt-row-track {
  background: transparent;
}

/* One shared grid layer behind all rows — weekend shading (Sat+Sun at
   offsets 5-6 of each Mon-start 7-day period), day separators, and stronger
   week-start lines are pure CSS gradients: zero extra DOM nodes */
.gantt-grid {
  position: absolute;
  bottom: 0;
  z-index: 0;
  pointer-events: none;
  background-image:
    repeating-linear-gradient(to right, rgba(0,0,0,0.035) 0, rgba(0,0,0,0.035) calc(var(--day-w) * 2), transparent calc(var(--day-w) * 2), transparent calc(var(--day-w) * 7)),
    repeating-linear-gradient(to right, #d9d9d9 0, #d9d9d9 1px, transparent 1px, transparent calc(var(--day-w) * 7)),
    repeating-linear-gradient(to right, transparent 0, transparent calc(var(--day-w) - 1px), #f0f1f4 calc(var(--day-w) - 1px), #f0f1f4 var(--day-w));
  background-position: calc(var(--day-w) * 5) 0, 0 0, 0 0;
}

.gantt-month {
  position: absolute;
  top: 0;
  height: 16px;
  line-height: 16px;
  font-size: 10px;
  font-weight: 600;
  color: #888;
  border-left: 1px solid #e8e8e8;
  border-bottom: 1px solid #f0f0f0;
  padding-left: 4px;
  box-sizing: border-box;
  overflow: hidden;
  white-space: nowrap;
}

.gantt-day {
  position: absolute;
  top: 16px;
  bottom: 0;
  font-size: 10px;
  color: #999;
  padding-top: 5px;
  text-align: center;
  border-left: 1px solid #f0f0f0;
  box-sizing: border-box;
}

.gantt-day.today {
  color: #ff4d4f;
  font-weight: 600;
}

.gantt-bar {
  position: absolute;
  top: 10px;
  height: 20px;
  border-radius: 9px;
  z-index: 1;
  cursor: pointer;
  box-sizing: border-box;
}

/* Okabe-Ito colorblind-safe palette + shape coding per status */
.gantt-bar-upcoming {
  background: transparent;          /* hollow = planned, not started */
  border: 2px solid #56B4E9;
}

.gantt-bar-in-progress {
  background: #E69F00;              /* solid amber = active */
}

.gantt-bar-completed {
  background: #009E73;              /* solid teal-green, slightly settled */
  opacity: 0.85;
}

.gantt-bar-cancelled {
  background: #757575;              /* faded grey + dashed outline = void */
  opacity: 0.55;
  border: 1.5px dashed #424242;
}

/* Overdue: red ring around the bar (box-shadow — no layout shift) */
.gantt-bar-overdue {
  box-shadow: 0 0 0 2px #ff4d4f;
}

.gantt-overdue-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #ff4d4f;
  margin-right: 4px;
  vertical-align: 1px;
}

/* Date range label trailing the bar */
.gantt-bar-dates {
  position: absolute;
  top: 12px;
  font-size: 10px;
  color: #999;
  white-space: nowrap;
  z-index: 1;
  pointer-events: none;
}

.gantt-today-line {
  position: absolute;
  bottom: 0;
  width: 2px;
  background: #ff4d4f;
  opacity: 0.55;
  z-index: 3;
  pointer-events: none;
}

.gantt-today-flag {
  position: absolute;
  top: 1px;
  transform: translateX(-50%);
  font-size: 9px;
  line-height: 14px;
  color: #fff;
  background: #ff4d4f;
  border-radius: 3px;
  padding: 0 4px;
  z-index: 5;
  pointer-events: none;
  white-space: nowrap;
}

/* Toolbar legend */
.gantt-legend {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-left: 12px;
  font-size: 11px;
  color: #666;
  white-space: nowrap;
}

.gantt-legend span {
  display: inline-flex;
  align-items: center;
}

.gantt-legend i {
  display: inline-block;
  width: 14px;
  height: 8px;
  border-radius: 4px;
  margin-right: 4px;
  box-sizing: border-box;
}

.gantt-legend .gantt-legend-overdue {
  background: #fff;
  box-shadow: 0 0 0 2px #ff4d4f;
}

/* Global request progress bar — indeterminate slide, shown on API activity */
#loading-bar {
  position: fixed;
  top: 0;
  left: 0;
  height: 2px;
  width: 100%;
  z-index: 10000;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease;
  overflow: hidden;
}
#loading-bar.active {
  opacity: 1;
}
#loading-bar.active::before {
  content: '';
  position: absolute;
  top: 0;
  height: 100%;
  width: 40%;
  background: #1890ff;
  animation: loading-bar-slide 1.1s ease-in-out infinite;
}
@keyframes loading-bar-slide {
  0%   { left: -40%; }
  100% { left: 100%; }
}

@media (max-width: 1100px) {
  .gantt-legend { display: none; }
}
