/* ==========================================================================
   Agendali — Modal v2
   Bootstrap 3 override. Opt-in: add class "modal-v2" to the .modal element.
   Load AFTER bootstrap.css and any existing app/theme CSS.
   ========================================================================== */

:root {
  --am-primary: #2E2C7E;
  --am-primary-hover: #24226A;
  --am-primary-tint: #ECEBFA;
  --am-focus-ring: rgba(46, 44, 126, 0.14);

  --am-text: #1F2433;
  --am-text-label: #2B3040;
  --am-text-muted: #5E6577;
  --am-text-placeholder: #6F7586;
  --am-text-disabled: #8A90A0;
  --am-text-toolbar: #4A5063;

  --am-border: #D9DCE5;
  --am-border-soft: #ECEEF3;
  --am-border-disabled: #E6E8EE;

  --am-surface: #FFFFFF;
  --am-surface-subtle: #F8F9FB;
  --am-surface-readonly: #F6F7FA;
  --am-surface-context: #F6F7FB;
  --am-surface-track: #F1F2F6;

  --am-danger: #B42318;
  --am-danger-hover: #931B12;
  --am-danger-border: #C42B1C;
  --am-danger-tint: #FDECEC;

  --am-radius-modal: 14px;
  --am-radius-control: 8px;
  --am-field-height: 42px;
  --am-button-height: 40px;
  --am-pad: 24px;
  --am-field-gap: 20px;
}

/* ---------- Backdrop (global: Bootstrap appends it to <body>) ---------- */
.modal-backdrop { background-color: #1F1D4A; }
.modal-backdrop.in { opacity: 0.45; }

/* ---------- Dialog sizes ---------- */
.modal-v2 .modal-dialog { width: 640px; margin: 5vh auto; }
.modal-v2 .modal-dialog.modal-sm { width: 440px; }
.modal-v2 .modal-dialog.modal-lg { width: 860px; }

@media (max-width: 767px) {
  .modal-v2 .modal-dialog,
  .modal-v2 .modal-dialog.modal-sm,
  .modal-v2 .modal-dialog.modal-lg { width: auto; margin: 10px; }
}

/* ---------- Shell ---------- */
.modal-v2 .modal-content {
  border: 0;
  border-radius: var(--am-radius-modal);
  box-shadow: 0 24px 48px rgba(20, 22, 50, 0.18), 0 2px 6px rgba(20, 22, 50, 0.08);
  overflow: hidden;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  font-family: 'Open Sans', 'Segoe UI', Helvetica, sans-serif;
  color: var(--am-text);
}

/* ---------- Header ---------- */
.modal-v2 .modal-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 20px 20px 20px var(--am-pad);
  border-bottom: 1px solid var(--am-border-soft);
  flex-shrink: 0;
}
.modal-v2 .modal-header::before,
.modal-v2 .modal-header::after { display: none; } /* kill BS3 clearfix inside flex */

.modal-v2 .am-header-text { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.modal-v2 .modal-title { margin: 0; font-size: 18px; font-weight: 700; line-height: 1.3; color: var(--am-text); }
.modal-v2 .am-subtitle {
  font-size: 13px; color: var(--am-text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.modal-v2 .modal-header .close {
  float: none; order: 99; margin: 0;
  width: 36px; height: 36px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  color: var(--am-text-muted); opacity: 1; text-shadow: none;
  font-size: 20px; font-weight: 400;
}
.modal-v2 .modal-header .close:hover,
.modal-v2 .modal-header .close:focus { background: var(--am-surface-track); color: var(--am-text); opacity: 1; }

/* Icon tile — tone per record type */
.am-icon {
  width: 40px; height: 40px; border-radius: 10px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; line-height: 1;
  background: var(--am-surface-track); color: var(--am-text-toolbar);
}
.am-icon--challenge { background: #FEF3E2; color: #B45309; }
.am-icon--action    { background: var(--am-primary-tint); color: var(--am-primary); }
.am-icon--goal      { background: #E3F4EA; color: #15803D; }
.am-icon--session   { background: #E4F1FB; color: #1D6FA8; }
.am-icon--note      { background: var(--am-surface-track); color: var(--am-text-toolbar); }
.am-icon--danger    { background: var(--am-danger-tint); color: var(--am-danger); border-radius: 50%; width: 44px; height: 44px; }

/* ---------- Body ---------- */
.modal-v2 .modal-body {
  padding: var(--am-pad);
  overflow-y: auto;
  flex: 1 1 auto;
}
.modal-v2 .modal-body .form-group { margin-bottom: var(--am-field-gap); }
.modal-v2 .modal-body > .form-group:last-child,
.modal-v2 .modal-body .row:last-child .form-group { margin-bottom: 0; }

/* Two-column field rows use BS3 .row/.col-sm-6 — tighten gutter to 16px */
.modal-v2 .modal-body .row { margin-left: -8px; margin-right: -8px; }
.modal-v2 .modal-body .row > [class*="col-"] { padding-left: 8px; padding-right: 8px; }

/* ---------- Labels ---------- */
.modal-v2 label,
.modal-v2 .control-label {
  display: block;
  margin: 0 0 6px;
  font-size: 13px; font-weight: 600; line-height: 1.4;
  color: var(--am-text-label);
  text-transform: none; letter-spacing: normal;
}
.modal-v2 .am-required { color: #C2410C; margin-left: 2px; }

/* ---------- Inputs ---------- */
.modal-v2 .form-control {
  height: var(--am-field-height);
  padding: 0 12px;
  border: 1px solid var(--am-border);
  border-radius: var(--am-radius-control);
  background-color: var(--am-surface);
  box-shadow: none;
  font-size: 14px;
  color: var(--am-text);
  transition: border-color .15s ease, box-shadow .15s ease;
}
.modal-v2 textarea.form-control { height: auto; min-height: 96px; padding: 10px 12px; line-height: 1.6; resize: vertical; }
.modal-v2 .form-control::placeholder { color: var(--am-text-placeholder); }
.modal-v2 .form-control:focus {
  border-color: var(--am-primary);
  box-shadow: 0 0 0 3px var(--am-focus-ring);
  outline: none;
}

.modal-v2 .form-control[readonly] {
  background-color: var(--am-surface-readonly);
  border-color: var(--am-border-soft);
  color: var(--am-text-label);
  cursor: default;
}
.modal-v2 .form-control[readonly]:focus { box-shadow: none; border-color: var(--am-border-soft); }

.modal-v2 .form-control[disabled],
.modal-v2 fieldset[disabled] .form-control {
  background-color: var(--am-surface-readonly);
  border-color: var(--am-border-disabled);
  color: var(--am-text-disabled);
  cursor: not-allowed;
}
.modal-v2 .form-group.is-disabled label { color: var(--am-text-placeholder); }

/* Native select — custom chevron */
.modal-v2 select.form-control {
  -webkit-appearance: none; -moz-appearance: none; appearance: none;
  padding-right: 36px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%235E6577' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 9l6 6l6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}
.modal-v2 select.form-control::-ms-expand { display: none; }

/* Error */
.modal-v2 .has-error .form-control { border-color: var(--am-danger-border); box-shadow: 0 0 0 3px rgba(196, 43, 28, 0.12); }
.modal-v2 .has-error .control-label,
.modal-v2 .has-error label { color: var(--am-text-label); }
.modal-v2 .has-error .help-block { color: var(--am-danger); }

.modal-v2 .help-block { margin: 6px 0 0; font-size: 12px; color: var(--am-text-muted); }

/* Leading icon inside an input (dates, search) */
.am-input-icon { position: relative; }
.am-input-icon > .ti {
  position: absolute; left: 12px; top: 50%; transform: translateY(-50%);
  font-size: 18px; color: var(--am-text-muted); pointer-events: none;
}
.modal-v2 .am-input-icon > .form-control { padding-left: 40px; }

/* ---------- Segmented control (radio-based) ---------- */
.am-segmented {
  display: flex; gap: 3px; padding: 3px;
  height: var(--am-field-height); box-sizing: border-box;
  background: var(--am-surface-track); border-radius: var(--am-radius-control);
}
.am-segmented input { position: absolute; opacity: 0; pointer-events: none; }
.modal-v2 .am-segmented label {
  flex: 1 1 0; margin: 0;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  border-radius: 6px; cursor: pointer;
  font-size: 13px; font-weight: 600; color: var(--am-text-muted);
}
.am-segmented input:checked + label {
  background: var(--am-surface); color: var(--am-text);
  font-weight: 700; box-shadow: 0 1px 3px rgba(20, 22, 50, 0.14);
}
.am-segmented input:focus-visible + label { box-shadow: 0 0 0 3px var(--am-focus-ring); }
.am-segmented input.am-priority-high:checked + label   { color: #B42318; }
.am-segmented input.am-priority-medium:checked + label { color: #B45309; }
.am-segmented input.am-priority-low:checked + label    { color: #15803D; }

/* Status / priority dot */
.am-dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.am-dot--low, .am-dot--closed { background: #15803D; }
.am-dot--medium, .am-dot--in-progress { background: #D97706; }
.am-dot--high { background: #DC2626; }
.am-dot--open { background: #2563EB; }

/* ---------- Toggle switch (checkbox-based) ---------- */
.am-switch { display: inline-flex; align-items: center; gap: 10px; margin: 0; cursor: pointer; }
.am-switch input { position: absolute; opacity: 0; pointer-events: none; }
.am-switch__track {
  width: 40px; height: 24px; border-radius: 999px; padding: 3px; box-sizing: border-box;
  background: #D3D6DF; transition: background .15s ease; display: flex; align-items: center;
}
.am-switch__track::after {
  content: ""; width: 18px; height: 18px; border-radius: 50%; background: #FFFFFF;
  box-shadow: 0 1px 2px rgba(20, 22, 50, 0.25); transition: transform .15s ease;
}
.am-switch input:checked + .am-switch__track { background: var(--am-primary); }
.am-switch input:checked + .am-switch__track::after { transform: translateX(16px); }
.am-switch input:focus-visible + .am-switch__track { box-shadow: 0 0 0 3px var(--am-focus-ring); }
.modal-v2 .am-switch__label { font-size: 13px; font-weight: 600; color: var(--am-text-label); }

/* ---------- Read-only context card (e.g. "From session") ---------- */
.am-context {
  display: flex; gap: 14px; padding: 14px 16px; margin-bottom: var(--am-field-gap);
  background: var(--am-surface-context); border: 1px solid var(--am-border-soft); border-radius: 10px;
}
.am-context__icon {
  width: 32px; height: 32px; border-radius: 8px; flex-shrink: 0;
  background: #FFFFFF; border: 1px solid #E3E5EC; color: var(--am-primary);
  display: flex; align-items: center; justify-content: center; font-size: 18px;
}
.am-context__body { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.am-context__meta  { font-size: 12px; font-weight: 600; color: var(--am-text-muted); }
.am-context__title { font-size: 14px; font-weight: 600; color: var(--am-text); line-height: 1.4; }
.am-context__sub   { font-size: 13px; color: var(--am-text-muted); line-height: 1.45; }
.am-context__link  { font-size: 13px; font-weight: 600; color: var(--am-primary); white-space: nowrap; align-self: flex-start; }

/* ---------- Section divider inside body ---------- */
.am-section {
  display: flex; align-items: center; gap: 12px;
  margin: 4px 0 16px;
  font-size: 12px; font-weight: 700; color: var(--am-text-muted);
}
.am-section::after { content: ""; flex: 1 1 auto; height: 1px; background: var(--am-border-soft); }

/* ---------- TinyMCE inside modals ---------- */
.modal-v2 .tox.tox-tinymce {
  border: 1px solid var(--am-border);
  border-radius: var(--am-radius-control);
  margin: 0;
}
.modal-v2 .tox.tox-tinymce.am-rte-focus {
  border-color: var(--am-primary);
  box-shadow: 0 0 0 3px var(--am-focus-ring);
}
.modal-v2 .tox .tox-editor-header {
  box-shadow: none !important;
  border-bottom: 1px solid var(--am-border-soft) !important;
  padding: 0 !important;
}
.modal-v2 .tox .tox-toolbar,
.modal-v2 .tox .tox-toolbar__primary,
.modal-v2 .tox .tox-toolbar__overflow { background: var(--am-surface-subtle) !important; }
.modal-v2 .tox .tox-tbtn { color: var(--am-text-toolbar); border-radius: 6px; }
.modal-v2 .tox .tox-tbtn svg { fill: var(--am-text-toolbar) !important; }

/* ---------- Footer ---------- */
.modal-v2 .modal-footer {
  display: flex; align-items: center; gap: 12px;
  padding: 16px var(--am-pad);
  background: var(--am-surface-subtle);
  border-top: 1px solid var(--am-border-soft);
  text-align: left;
  flex-shrink: 0;
}
.modal-v2 .modal-footer::before,
.modal-v2 .modal-footer::after { display: none; }
.modal-v2 .modal-footer .btn + .btn { margin-left: 0; }
.modal-v2 .am-footer-start { flex: 1 1 auto; min-width: 0; display: flex; align-items: center; gap: 10px; font-size: 12px; color: var(--am-text-muted); }

/* ---------- Buttons (scoped to modal) ---------- */
.modal-v2 .btn {
  height: var(--am-button-height);
  padding: 0 18px;
  border-radius: var(--am-radius-control);
  font-size: 14px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  box-shadow: none;
}
.modal-v2 .btn-primary { background: var(--am-primary); border-color: var(--am-primary); color: #FFFFFF; padding: 0 20px; }
.modal-v2 .btn-primary:hover,
.modal-v2 .btn-primary:focus,
.modal-v2 .btn-primary:active { background: var(--am-primary-hover); border-color: var(--am-primary-hover); color: #FFFFFF; }
.modal-v2 .btn-primary[disabled] { background: #A9A8CC; border-color: #A9A8CC; opacity: 1; }

.modal-v2 .btn-default { background: #FFFFFF; border: 1px solid var(--am-border); color: var(--am-text-label); }
.modal-v2 .btn-default:hover,
.modal-v2 .btn-default:focus { background: var(--am-surface-subtle); border-color: #C6CAD6; color: var(--am-text); }

.modal-v2 .btn-danger { background: var(--am-danger); border-color: var(--am-danger); color: #FFFFFF; padding: 0 20px; }
.modal-v2 .btn-danger:hover,
.modal-v2 .btn-danger:focus { background: var(--am-danger-hover); border-color: var(--am-danger-hover); color: #FFFFFF; }

.modal-v2 .btn-link { color: var(--am-primary); padding: 0 12px; }
.modal-v2 .btn-link.am-text-danger { color: var(--am-danger); }

.modal-v2 .btn:focus-visible { outline: none; box-shadow: 0 0 0 3px var(--am-focus-ring); }

/* Saving state spinner: add .is-loading to the button */
.modal-v2 .btn.is-loading::before {
  content: ""; width: 14px; height: 14px; border-radius: 50%; box-sizing: border-box;
  border: 2px solid rgba(255, 255, 255, 0.35); border-top-color: #FFFFFF;
  animation: am-spin .8s linear infinite;
}
@keyframes am-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .modal-v2 .btn.is-loading::before { animation-duration: 2s; } }

/* ---------- Confirm (small) modal ---------- */
.modal-v2 .am-confirm { display: flex; gap: 16px; }
.modal-v2 .am-confirm p { margin: 8px 0 0; font-size: 14px; line-height: 1.6; color: var(--am-text-toolbar); }
.modal-v2 .am-confirm strong { color: var(--am-text); font-weight: 600; }
.modal-v2 .am-confirm .js-confirm-type { margin: 14px 0 0; }
.modal-v2 .am-confirm .js-confirm-type label { display: block; margin-bottom: 6px; font-size: 13px; font-weight: 600; color: var(--am-text); }
.modal-v2 .modal-footer.am-footer-end { justify-content: flex-end; }

/* App modals use z-index 999999 (styles_v1.css), not Bootstrap's 1050.
   Only the open Chosen list gets that layer. A closed .chosen-container
   with this z-index paints over page menus such as Session Details > More. */
.datepicker.dropdown-menu,
.ui-datepicker,
.select2-container--open .select2-dropdown,
.bootstrap-select .dropdown-menu,
.bootstrap-datetimepicker-widget,
.chosen-container.chosen-with-drop { z-index: 1000001 !important; }
.tox-tinymce-aux { z-index: 1000002 !important; }

/* Org branding rules in the layout use !important on every focus ring and
   primary button. Keep v2 colours inside migrated modals. */
.modal-v2 .form-control:focus {
  border-color: var(--am-primary) !important;
  box-shadow: 0 0 0 3px var(--am-focus-ring) !important;
}
.modal-v2 .btn-primary,
.modal-v2 .btn-primary:hover,
.modal-v2 .btn-primary:focus,
.modal-v2 .btn-primary:active {
  background-color: var(--am-primary) !important;
  border-color: var(--am-primary) !important;
  color: #FFFFFF !important;
}
.modal-v2 .btn-primary:hover,
.modal-v2 .btn-primary:focus,
.modal-v2 .btn-primary:active {
  background-color: var(--am-primary-hover) !important;
  border-color: var(--am-primary-hover) !important;
}
