/* =========================================
   modal.css
   モーダルのUI統合スタイル
   (z-index・レスポンシブ・アクセシビリティ・操作性を最適化)
   ========================================= */

/* ========== Z-INDEX・レイヤー管理 ========== */
.modal-backdrop {
  z-index: 1040 !important;
}

.modal {
  z-index: 1055 !important;
}

/* 複数モーダルの重なり順制御 */
#transactionModal {
  z-index: 1055 !important;
}

#stockSplitModal {
  z-index: 1060 !important;
}

#stockSplitModal ~ .modal-backdrop {
  z-index: 1059 !important;
}

.modal:nth-of-type(1) { z-index: 1050 !important; }
.modal:nth-of-type(2) { z-index: 1055 !important; }
.modal:nth-of-type(3) { z-index: 1060 !important; }

.modal-backdrop:nth-of-type(1) { z-index: 1040 !important; }
.modal-backdrop:nth-of-type(2) { z-index: 1045 !important; }
.modal-backdrop:nth-of-type(3) { z-index: 1050 !important; }

/* ========== モーダルが開いているときの body 設定 ========== */
body.modal-open {
  overflow: hidden !important;
  padding-right: 0 !important;
  position: relative;
  scroll-behavior: auto !important;
}

/* 孤立バックドロップ防止 */
.modal-backdrop + .modal-backdrop,
body:not(.modal-open) .modal-backdrop,
.modal-backdrop:not(.show) {
  display: none !important;
  opacity: 0 !important;
}

/* ========== アニメーション ========== */
.modal.fade .modal-dialog {
  transition: transform 0.3s ease-out, opacity 0.3s ease-out;
  transform: translateY(-50px);
  opacity: 0;
}

.modal.show .modal-dialog {
  transform: none;
  opacity: 1;
  animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
  from { transform: translateY(-50px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal-backdrop.fade {
  transition: opacity 0.15s linear;
  opacity: 0;
}

.modal-backdrop.show {
  opacity: 0.5;
}

/* ========== 基本構造 ========== */
.modal.show {
  display: block !important;
  opacity: 1 !important;
}

.modal-backdrop.show {
  display: block !important;
  opacity: 0.5 !important;
}

/* ========== スクロールバー ========== */
.modal-body::-webkit-scrollbar {
  width: 8px;
}
.modal-body::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}
.modal-body::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 4px;
}
.modal-body::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
.modal-body {
  scrollbar-width: thin;
  scrollbar-color: #c1c1c1 #f1f1f1;
}

/* ========== フォーム要素 ========== */
.modal-body .form-label {
  font-weight: 500;
  color: #374151;
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.modal-body .form-label .text-danger {
  color: #dc2626;
  font-size: 1rem;
}

.modal-body .form-control:focus,
.modal-body .form-select:focus {
  border-color: #5a7ec5;
  box-shadow: 0 0 0 0.2rem rgba(90, 126, 197, 0.25);
  outline: none;
}

.modal-body .form-control:focus-visible,
.modal-body .form-select:focus-visible {
  outline: 2px solid #5a7ec5;
  outline-offset: 2px;
}

.modal-body .form-text {
  color: #6b7280;
  margin-top: 0.25rem;
}

/* バリデーション状態 */
.modal-body .form-control.is-invalid,
.modal-body .form-select.is-invalid {
  border-color: #dc2626;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' ... %3e");
  background-repeat: no-repeat;
  background-position: right calc(0.375em + 0.1875rem) center;
  background-size: calc(0.75em + 0.375rem);
  padding-right: calc(1.5em + 0.75rem);
}

.modal-body .invalid-feedback {
  display: block;
  color: #dc2626;
  font-size: 0.875rem;
  margin-top: 0.25rem;
}

.modal-body .form-control.is-valid,
.modal-body .form-select.is-valid {
  border-color: #10b981;
}

/* ========== ローディング状態 ========== */
.modal-body.loading {
  opacity: 0.6;
  pointer-events: none;
  position: relative;
}

.modal-body .loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

/* ========== アラート・通知 ========== */
.modal-body .alert {
  border-radius: 0.375rem;
  border: 1px solid;
  margin-bottom: 1rem;
}
.modal-body .alert-info {
  background: #e0f2fe; border-color: #bae6fd; color: #075985;
}
.modal-body .alert-warning {
  background: #fef3c7; border-color: #fde68a; color: #92400e;
}
.modal-body .alert-danger {
  background: #fee2e2; border-color: #fecaca; color: #991b1b;
}
.modal-body .alert-success {
  background: #d1fae5; border-color: #a7f3d0; color: #065f46;
}

/* ========== プレビュー・警告表示 ========== */
#transaction-preview,
#quantity-warning {
  border-radius: 0.375rem;
  padding: 0.75rem;
  margin-top: 0.5rem;
}
#transaction-preview {
  background: #dbeafe;
  border: 1px solid #93c5fd;
}
#quantity-warning {
  background: #fee2e2;
  border: 1px solid #fecaca;
  color: #991b1b;
}

/* ========== ボタン ========== */
.modal-footer .btn {
  font-weight: 500;
  transition: all 0.2s ease;
  border-radius: 0.375rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
}
.modal-footer .btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}
.modal-footer .btn:active {
  transform: translateY(0);
}
.modal-footer .btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

/* ========== レスポンシブ ========== */
/* デスクトップ */
@media (min-width: 992px) {
  .modal-dialog { max-width: 600px; margin: 1.75rem auto; }
  .modal-content {
    border-radius: 0.5rem;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  }
  .modal-body {
    padding: 1.5rem;
    max-height: calc(100vh - 200px);
    overflow-y: auto;
  }
}

/* タブレット */
@media (min-width: 768px) and (max-width: 991px) {
  .modal-dialog { max-width: 90%; margin: 1.5rem auto; }
  .modal-body {
    padding: 1.25rem;
    max-height: calc(100vh - 180px);
    overflow-y: auto;
  }
}

/* スマートフォン */
@media (max-width: 767px) {
  .modal-dialog {
    max-width: 95%;
    margin: 0.75rem auto;
  }
  .modal-content {
    border-radius: 0.5rem;
    display: flex;
    flex-direction: column;
    max-height: calc(100vh - 1rem);
  }
  .modal-header {
    padding: 1rem;
    border-bottom: 1px solid #e5e7eb;
  }
  .modal-body {
    padding: 1rem;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    flex: 1;
  }
  .modal-footer {
    padding: 0.75rem 1rem;
    border-top: 1px solid #e5e7eb;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
  }
  .modal-footer .btn {
    width: 100%;
    padding: 0.625rem 1rem;
    min-height: 44px;
  }
}

/* 横向きスマホ */
@media (max-width: 767px) and (orientation: landscape) {
  .modal-body {
    max-height: calc(95vh - 120px);
    overflow-y: auto;
  }
}

/* ========== タッチデバイス最適化 ========== */
@media (hover: none) and (pointer: coarse) {
  .modal-footer .btn {
    min-height: 44px;
    padding: 0.75rem 1.25rem;
  }
  .modal-header .btn-close {
    min-width: 44px;
    min-height: 44px;
  }
  .modal-footer .btn:active {
    background-color: rgba(0, 0, 0, 0.1);
  }
}

/* ========== ハイコントラスト & ダークモード ========== */
@media (prefers-contrast: high) {
  .modal-content,
  .modal-header,
  .modal-footer {
    border: 2px solid #000;
  }
}

@media (prefers-color-scheme: dark) {
  .modal-header,
  .modal-footer {
    border-color: #374151;
    background-color: rgb(23, 27, 43);
  }
  .modal-backdrop {
    background-color: rgba(0, 0, 0, 0.8);
  }
}

/* ========== パフォーマンス & iOS 対応 ========== */
.modal,
.modal-backdrop {
  will-change: opacity;
  backface-visibility: hidden;
}
.modal-dialog {
  will-change: transform;
}

@supports (-webkit-touch-callout: none) {
  body.modal-open {
    position: fixed;
    width: 100%;
    overflow: hidden;
  }
  .modal-body {
    -webkit-overflow-scrolling: touch;
  }
}
