/* Shared modal shell and utilities */

.modal-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0); z-index: 275; display: none;
  align-items: center; justify-content: center; cursor: pointer;
  transition: background 0.25s ease; overflow: hidden;
  padding: 32px;
}
.modal-overlay.show {
  display: flex;
  background: rgba(0,0,0,0.68);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
.modal {
  position: relative; cursor: default;
  background: var(--bg-secondary); border-radius: var(--radius);
  border: 1px solid var(--border); width: 90%; max-width: 700px;
  max-height: 85vh; overflow-y: auto; overflow-x: hidden; overscroll-behavior: contain; padding: 32px;
  transform: scale(0.96) translateY(8px); opacity: 0;
  transition: transform 0.25s ease, opacity 0.25s ease;
}
.modal-overlay.show .modal { transform: scale(1) translateY(0); opacity: 1; }
.gb-form-modal,
.gb-history-modal,
.marker-detail-modal {
  padding: 0;
  width: min(94vw, 880px);
  max-height: 90vh;
  background: var(--bg-secondary);
  box-shadow: var(--shadow-lg);
}
.gb-form-modal { max-width: 680px; }
.gb-history-modal { max-width: 840px; }
.gb-modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 22px 26px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg-secondary) 92%, var(--bg-card));
}
.gb-modal-kicker {
  font-family: var(--font-mono);
  color: var(--text-muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.gb-modal-title {
  margin-top: 3px;
  color: var(--text-primary);
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0;
}
.gb-modal-head .modal-close {
  position: static;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-card);
  color: var(--text-muted);
  font-size: 22px;
  line-height: 1;
  padding: 0;
  flex-shrink: 0;
}
.gb-modal-head .modal-close:hover {
  color: var(--text-primary);
  border-color: var(--accent);
  background: var(--bg-hover);
}
.gb-form-body {
  padding: 24px 26px 26px;
}
.gb-form-modal .modal-unit,
.gb-history-modal .modal-unit {
  margin-bottom: 18px;
  color: var(--text-secondary);
}
.gb-form-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
.modal:not(.marker-detail-modal):not(.gb-form-modal):not(.gb-history-modal):not(.settings-modal) > h3:first-of-type,
.modal:not(.marker-detail-modal):not(.gb-form-modal):not(.gb-history-modal):not(.settings-modal) > .modal-header:first-child {
  margin: -32px -32px 20px;
  padding: 22px 56px 18px 26px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg-secondary) 92%, var(--bg-card));
}
.modal:not(.marker-detail-modal):not(.gb-form-modal):not(.gb-history-modal):not(.settings-modal) > h3:first-of-type {
  color: var(--text-primary);
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0;
}
.modal:not(.marker-detail-modal):not(.gb-form-modal):not(.gb-history-modal):not(.settings-modal) > .modal-header:first-child h3 {
  margin: 0;
  color: var(--text-primary);
  font-size: 22px;
}
.modal:not(.marker-detail-modal):not(.gb-form-modal):not(.gb-history-modal):not(.settings-modal) > .modal-unit:first-of-type {
  margin-top: -6px;
}
.modal:not(.marker-detail-modal):not(.gb-form-modal):not(.gb-history-modal):not(.settings-modal) .modal-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}
@media (max-width: 720px) {
  .modal-overlay { padding: 12px; }
  .gb-modal-head { padding: 18px 20px; }
  .gb-form-body { padding: 20px; }
  .gb-form-actions {
    justify-content: stretch;
  }
  .gb-form-actions .import-btn {
    flex: 1 1 100%;
  }
}

/* Knowledge Base modal - wider than the default 700px to fit the
   per-backend controls (model picker, library list, document list,
   ingest UI) without horizontal cramping. */
.kb-modal { max-width: 760px; }

.modal-nudge { animation: modal-nudge 0.3s ease; }
@keyframes modal-nudge { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.015); box-shadow: 0 0 0 2px var(--accent); } }

.modal-close {
  position: absolute; top: 16px; right: 16px;
  background: none; border: none; color: var(--text-muted);
  font-size: 24px; cursor: pointer; padding: 0 4px;
}
.modal-close:hover { color: var(--text-primary); }
.modal h3 { font-family: var(--font-display); font-size: 20px; font-weight: 700; margin-bottom: 4px; }

/* Reserve room for the absolutely-positioned close button so long titles
   (e.g. "Log session - Mitochondriak Maxi UVB") don't run under it
   when they wrap on narrow viewports. */
.modal-header { position: relative; padding-right: 40px; }
.modal-header h3 { margin-bottom: 4px; }

/* Changelog */
.changelog-entry { margin-bottom: 20px; }
.changelog-entry + .changelog-entry { padding-top: 20px; border-top: 1px solid var(--border); }
.changelog-header { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.changelog-version {
  display: inline-block; background: var(--accent-gradient); color: #fff;
  font-family: var(--font-display); font-weight: 600; font-size: 14px;
  padding: 4px 12px; border-radius: 20px;
}
.changelog-date { font-size: 13px; color: var(--text-muted); }
.changelog-items { list-style: none; margin: 0; padding: 0 0 0 4px; }
.changelog-item {
  position: relative; padding: 3px 0 3px 16px;
  font-size: 14px; color: var(--text-secondary); line-height: 1.5;
}
.changelog-item::before { content: '\2013'; position: absolute; left: 0; color: var(--accent); font-weight: 600; }
.changelog-item a { color: var(--accent); text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2px; }
.changelog-item a:hover { text-decoration-thickness: 2px; }

.modal .modal-unit {
  color: var(--text-muted); font-size: 14px; margin-bottom: 8px;
  /* Source-name + delta + trend separated by middle-dots; let them wrap
     gracefully on narrow viewports rather than overflow the modal box. */
  display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: center;
}
.modal .modal-unit.modal-unit-alt {
  font-size: 12px; opacity: 0.7; margin-top: -4px; margin-bottom: 12px;
}
.modal .wearable-modal-source-swap {
  margin-left: auto;
  font-size: 11px;
}

.ref-editable { cursor: pointer; border-bottom: 1px dashed var(--text-muted); padding-bottom: 1px; }
.ref-editable:hover { color: var(--accent); border-color: var(--accent); }
.ref-edit-field { display: inline-flex; align-items: center; position: relative; }
.ref-edit-input { width: 60px; padding: 2px 20px 2px 6px; font-size: 13px; font-family: var(--font-mono); background: var(--bg-primary); color: var(--text-primary); border: 1px solid var(--border); border-radius: var(--radius-sm); }
.ref-edit-input:focus { border-color: var(--accent); outline: none; }
.ref-edit-clear { position: absolute; right: 2px; top: 50%; transform: translateY(-50%); background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 14px; padding: 0 3px; line-height: 1; opacity: 0.5; }
.ref-edit-clear:hover { opacity: 1; color: var(--red); }
.ref-edit-save { padding: 2px 8px; font-size: 12px; background: var(--accent-gradient); color: white; border: none; border-radius: var(--radius-sm); cursor: pointer; margin-left: 4px; }
.ref-edited-badge { font-size: 10px; color: var(--yellow); cursor: pointer; margin-left: 4px; opacity: 0.8; }
.ref-edited-badge:hover { opacity: 1; text-decoration: underline; }

.toggle-switch { position: relative; display: inline-block; width: 36px; height: 20px; flex-shrink: 0; }
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.toggle-slider { position: absolute; cursor: pointer; inset: 0; background: var(--border); border-radius: 20px; transition: background 0.2s; }
.toggle-slider::before { content: ''; position: absolute; width: 14px; height: 14px; left: 3px; bottom: 3px; background: var(--text-secondary); border-radius: 50%; transition: transform 0.2s, background 0.2s; }
.toggle-switch input:checked + .toggle-slider { background: var(--accent); }
.toggle-switch input:checked + .toggle-slider::before { transform: translateX(16px); background: #fff; }

.emoji-picker { position: fixed; z-index: 10001; background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow-lg); width: 320px; max-height: 400px; display: flex; flex-direction: column; }
.emoji-picker-search { padding: 8px; border-bottom: 1px solid var(--border); }
.emoji-picker-search input { width: 100%; padding: 6px 10px; border-radius: 6px; border: 1px solid var(--border); background: var(--bg-secondary); color: var(--text-primary); font-size: 14px; outline: none; box-sizing: border-box; }
.emoji-picker-search input:focus { border-color: var(--accent); }
.emoji-picker-cats { display: flex; gap: 2px; padding: 4px 8px; border-bottom: 1px solid var(--border); overflow-x: auto; flex-shrink: 0; }
.emoji-picker-cats button { background: none; border: none; font-size: 16px; padding: 4px 6px; cursor: pointer; border-radius: 4px; opacity: 0.6; }
.emoji-picker-cats button.active, .emoji-picker-cats button:hover { opacity: 1; background: var(--bg-secondary); }
.emoji-picker-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; padding: 8px; overflow-y: auto; flex: 1; }
.emoji-picker-grid span { font-size: 22px; text-align: center; cursor: pointer; padding: 4px; border-radius: 4px; line-height: 1.2; }
.emoji-picker-grid span:hover { background: var(--bg-secondary); }
.emoji-picker-label { grid-column: 1 / -1; font-size: 11px; color: var(--text-secondary); padding: 6px 2px 2px; text-transform: uppercase; letter-spacing: 0.5px; }

@media (max-width: 640px) {
  .modal-overlay { padding: 8px; }
}
