/**
 * レスポンシブ対応 - Equus Cloud
 * 完全なモバイル・タブレット対応
 */

/* ===== 全体的なオーバーフロー対策 ===== */

/* すべてのデバイスで横スクロールを防ぐ */
html {
  overflow-x: hidden;
  width: 100%;
}

body {
  overflow-x: hidden;
  position: relative;
  width: 100%;
  max-width: 100vw;
}

/* すべての要素が画面幅を超えないようにする */
* {
  max-width: 100%;
  box-sizing: border-box;
}

img, video, iframe {
  max-width: 100%;
  height: auto;
}

/* テーブルコンテナのオーバーフロー対策 */
.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

@media (max-width: 768px) {
  .content-area,
  .page-container,
  .card,
  .card-body {
    overflow-x: hidden;
  }
}

/* ===== テーブルのレスポンシブ対応 ===== */

/* すべてのテーブルを横スクロール可能にする */
.table-responsive {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin-bottom: 1rem;
}

/* タブレット以下でテーブルを横スクロール可能にする */
@media (max-width: 1024px) {
  .table-container,
  .table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -1rem;
    padding: 0 1rem;
  }

  .table {
    min-width: 800px;
  }

  /* カード内のテーブル */
  .card .table-container,
  .card .table-responsive {
    margin: 0;
    padding: 0;
  }
}

/* iPhone 12以下の小画面では最小幅を縮小 */
@media (max-width: 414px) {
  .table-container {
    margin: 0 -0.75rem;
    padding: 0 0.75rem;
  }

  .table {
    min-width: 500px;
    font-size: 0.8125rem;
  }

  .table thead th,
  .table tbody td {
    padding: 0.625rem 0.5rem;
    font-size: 0.8125rem;
  }
}

/* モバイルでテーブルをカード表示に変更 */
@media (max-width: 768px) {
  /* テーブルをブロック表示に切り替え（オプション） */
  .table.table-responsive-stack {
    display: block;
    min-width: 100%;
  }

  .table.table-responsive-stack thead {
    display: none;
  }

  .table.table-responsive-stack tbody,
  .table.table-responsive-stack tr,
  .table.table-responsive-stack td {
    display: block;
    width: 100%;
  }

  .table.table-responsive-stack tr {
    margin-bottom: 1rem;
    border: 1px solid #e9ecef;
    border-radius: 8px;
    overflow: hidden;
    background: white;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  }

  .table.table-responsive-stack tr:hover {
    transform: none;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }

  .table.table-responsive-stack td {
    text-align: left;
    padding: 0.75rem 1rem;
    border: none;
    position: relative;
    padding-left: 40%;
    min-height: 40px;
    display: flex;
    align-items: center;
  }

  .table.table-responsive-stack td::before {
    content: attr(data-label);
    position: absolute;
    left: 0;
    width: 38%;
    padding-left: 1rem;
    font-weight: 600;
    font-size: 0.8125rem;
    color: var(--color-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
  }

  .table.table-responsive-stack td:first-child {
    background: #f8f9fa;
    font-weight: 600;
  }

  /* アクションボタン用の調整 */
  .table.table-responsive-stack td .btn-group {
    flex-wrap: wrap;
    margin-left: auto;
  }

  /* 横スクロール用のスタイル維持 */
  .table:not(.table-responsive-stack) {
    min-width: 600px;
    font-size: 0.8125rem;
  }

  .table:not(.table-responsive-stack) thead th,
  .table:not(.table-responsive-stack) tbody td {
    padding: 0.75rem 0.5rem;
    font-size: 0.8125rem;
  }
}

/* ===== ボタンとフォームのレスポンシブ ===== */

@media (max-width: 768px) {
  /* ボタングループを縦並びに */
  .btn-group {
    flex-direction: column;
    width: 100%;
  }

  .btn-group .btn {
    width: 100%;
    justify-content: center;
  }

  /* ページアクションボタン */
  .page-actions {
    flex-direction: column;
    width: 100%;
  }

  .page-actions .btn {
    width: 100%;
  }

  /* カードアクション */
  .card-actions {
    flex-direction: column;
    width: 100%;
  }

  .card-actions .btn {
    width: 100%;
  }

  /* フィルターフォーム */
  .filter-row {
    flex-direction: column;
  }

  .filter-row .btn {
    width: 100%;
    margin-top: 0.5rem;
  }
}

/* ===== ページヘッダーのレスポンシブ ===== */

@media (max-width: 768px) {
  .page-title {
    font-size: 1.5rem;
    line-height: 1.3;
    word-break: break-word;
  }

  .page-subtitle {
    font-size: 0.875rem;
    line-height: 1.4;
  }

  /* ページヘッダーを縦並びに */
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
    margin-bottom: 1.5rem;
  }

  .page-header > div {
    width: 100%;
  }
}

@media (max-width: 414px) {
  .page-title {
    font-size: 1.25rem;
  }

  .page-subtitle {
    font-size: 0.8125rem;
  }

  .page-header {
    margin-bottom: 1rem;
  }
}

/* ===== カードのレスポンシブ ===== */

.card {
  max-width: 100%;
  overflow-x: hidden;
}

.card-body {
  overflow-x: hidden;
}

@media (max-width: 768px) {
  .card {
    border-radius: 8px;
    margin-bottom: 1rem;
    margin-left: 0;
    margin-right: 0;
  }

  .card-header {
    padding: 1rem;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.75rem;
  }

  .card-title {
    font-size: 1rem;
    word-break: break-word;
  }

  .card-body {
    padding: 1rem;
  }

  .card-footer {
    padding: 1rem;
  }
}

/* ===== モーダル・ダイアログのレスポンシブ ===== */

@media (max-width: 640px) {
  .confirm-dialog {
    width: 95%;
    max-width: none;
  }

  .confirm-dialog-header,
  .confirm-dialog-body,
  .confirm-dialog-footer {
    padding: 1rem;
  }

  .confirm-dialog-footer {
    flex-direction: column-reverse;
  }

  .confirm-dialog-footer .btn {
    width: 100%;
  }
}

/* ===== フォントサイズの調整 ===== */

@media (max-width: 640px) {
  body {
    font-size: 0.9375rem;
  }

  h1 {
    font-size: 1.75rem;
  }

  h2 {
    font-size: 1.5rem;
  }

  h3 {
    font-size: 1.25rem;
  }

  h4 {
    font-size: 1.125rem;
  }

  .btn {
    font-size: 0.875rem;
    padding: 0.625rem 1rem;
  }

  .btn-sm {
    font-size: 0.8125rem;
    padding: 0.5rem 0.875rem;
  }
}

/* ===== 小型モバイル対応（iPhone 12, iPhone SE など） ===== */

/* iPhone 12 (390px) 対応 */
@media (max-width: 414px) {
  .app-header {
    padding: 0 0.75rem;
  }

  /* ユーザーメニューのドロップダウンを画面内に収める */
  .dropdown-menu {
    right: 0;
    left: auto;
    min-width: 200px;
    max-width: calc(100vw - 2rem);
  }

  /* サブメニューを下に展開（横にはみ出さないように） */
  .dropdown-submenu-content {
    position: static !important;
    left: auto !important;
    right: auto !important;
    transform: none !important;
    margin-top: 0.5rem;
    margin-left: 1rem;
    background: #f8f9fa;
    border: none;
    border-radius: 6px;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.06);
    padding: 0.5rem 0;
  }

  .dropdown-submenu-content.show {
    display: block !important;
    opacity: 1 !important;
    visibility: visible !important;
  }

  .dropdown-submenu .submenu-arrow {
    transform: rotate(90deg); /* 下向き矢印に */
  }

  .dropdown-item {
    font-size: 0.875rem;
    padding: 0.625rem 1rem;
  }

  /* ロゴテキストは表示を維持 */
  .logo-text {
    font-size: 0.875rem;
  }

  .content-area {
    padding: 0.75rem;
  }

  .page-container {
    padding: 0.75rem;
  }
}

/* iPhone SE (375px以下) 対応 */
@media (max-width: 375px) {
  .app-header {
    padding: 0 0.5rem;
  }

  .logo-text {
    display: none;
  }

  .user-name {
    display: none;
  }

  .content-area {
    padding: 0.5rem;
  }

  .page-container {
    padding: 0.5rem;
  }

  .card {
    border-radius: 6px;
  }

  .card-header,
  .card-body,
  .card-footer {
    padding: 0.75rem;
  }

  .form-input,
  .form-select,
  .form-textarea {
    font-size: 16px; /* iOS zoom prevention */
  }

  .dropdown-menu {
    min-width: 180px;
    max-width: calc(100vw - 1rem);
  }
}

/* ===== タブレット横向き対応 ===== */

@media (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
  .content-area {
    padding: 1.5rem;
  }

  .kpi-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
  }

  .kpi-grid-new {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* ===== タッチデバイス最適化 ===== */

@media (hover: none) and (pointer: coarse) {
  /* タッチターゲットを大きくする */
  .btn {
    min-height: 44px;
    padding: 0.75rem 1.25rem;
  }

  .icon-btn {
    min-width: 44px;
    min-height: 44px;
  }

  .menu-toggle {
    min-width: 44px;
    min-height: 44px;
    padding: 0.75rem;
  }

  .nav-link {
    min-height: 44px;
    padding: 0.875rem 1.5rem;
  }

  /* ホバー効果を無効化 */
  .table tbody tr:hover {
    transform: none;
  }

  .btn:hover {
    transform: none;
  }

  .card:hover {
    transform: none;
  }
}

/* ===== プリント対応 ===== */

@media print {
  /* 共通の印刷マージン設定 */
  @page {
    margin: 10mm;
  }

  .app-header,
  .sidebar,
  .ai-panel,
  .page-actions,
  .btn,
  .card-actions,
  .breadcrumbs,
  .no-print {
    display: none !important;
  }

  /* レイアウト要素のリセット */
  .app-container,
  .main-wrapper {
    margin: 0 !important;
    margin-top: 0 !important;
    padding: 0 !important;
    min-height: auto !important;
  }

  .content-area {
    margin: 0 !important;
    margin-left: 0 !important;
    margin-right: 0 !important;
    padding: 0 !important;
    min-height: auto !important;
  }

  .card {
    box-shadow: none;
    border: 1px solid #000;
    page-break-inside: avoid;
  }

  body {
    background: white;
    margin: 0;
    padding: 0;
  }

  html {
    margin: 0;
    padding: 0;
  }
}

/* ===== ダークモード対応（将来用） ===== */

@media (prefers-color-scheme: dark) {
  /* ダークモードは将来実装予定 */
}

/* ===== 高コントラストモード ===== */

@media (prefers-contrast: high) {
  .btn,
  .card,
  .table {
    border: 2px solid currentColor;
  }
}

/* ===== モーション削減モード ===== */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ===== ログインページのレスポンシブ ===== */

@media (max-width: 640px) {
  .login-container {
    padding: 1rem;
  }

  .login-card {
    padding: 2rem 1.5rem;
    border-radius: 12px;
  }

  .login-header h1 {
    font-size: 1.75rem;
  }

  .login-subtitle {
    font-size: 0.875rem;
  }

  .form-input,
  .form-select {
    font-size: 16px; /* iOS zoom prevention */
  }
}

@media (max-width: 375px) {
  .login-card {
    padding: 1.5rem 1rem;
  }

  .login-header h1 {
    font-size: 1.5rem;
  }
}

/* ===== モーダルのレスポンシブ ===== */

.modal-content {
  max-width: 90vw;
  max-height: 90vh;
  overflow-y: auto;
}

@media (max-width: 768px) {
  .modal-content {
    width: 95%;
    max-width: none;
    max-height: 95vh;
  }

  .modal-header,
  .modal-body,
  .modal-footer {
    padding: 1rem;
  }

  .modal-footer {
    flex-direction: column-reverse;
  }

  .modal-footer .btn {
    width: 100%;
  }

  /* モーダル内のフォームグリッド */
  .modal-body .form-grid,
  .modal-body .form-grid-2,
  .modal-body .form-grid-3 {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .modal-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }

  .modal-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
  }
}
