/* 統合販売管理システム - スマホ最適化スタイル
 * 適用範囲: 767.98px以下のみ（768pxちょうどはBootstrap mdと重なるため .98 とする）
 * PC表示には一切影響させないこと。app.css の後に読み込む。 */

@media (max-width: 767.98px) {

  /* ---- レイアウト: サイドバー・ハンバーガーを廃止し下部タブへ ---- */
  #sidebar, #toggle-sidebar { display: none !important; }
  #main { margin-left: 0; }
  #content {
    padding: 12px;
    /* 横画面のノッチ領域対策（viewport-fit=coverのため左右safe areaを考慮） */
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    /* 下部タブ(56px)＋ホームインジケーター(safe area)と重ならない余白 */
    padding-bottom: calc(56px + env(safe-area-inset-bottom) + 16px);
  }
  .section-card { padding: 14px; margin-bottom: 14px; }
  .kpi-card { padding: 14px; }
  /* KPIカードはスマホ2列前提: 数字をやや縮め、装飾アイコンは隠して幅を確保 */
  .kpi-card .kpi-value { font-size: 22px; }
  .kpi-card .kpi-icon { display: none; }
  .table-sm th, .table-sm td { font-size: 14px; }

  /* ---- トップバー ---- */
  #topbar {
    padding: 10px 12px; gap: 8px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
  }
  #topbar h5 { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  /* スマホでは年度・月セレクトとログアウトを隠し、期間チップ＋メニューシートに集約 */
  #year-select, #month-select, #neppan-fy-select, #logout-btn { display: none !important; }
  #period-chip { white-space: nowrap; min-height: 40px; font-size: 13px; }

  /* ---- 期間シート ---- */
  .period-label { font-size: 12px; color: var(--muted); margin: 10px 2px 6px; font-weight: 600; }
  .period-fy-list { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; }
  .period-fy-btn {
    flex: 0 0 auto; border: 1px solid #e6e9ee; border-radius: 20px; background: #fff;
    min-height: 40px; padding: 6px 16px; font-size: 14px; color: var(--text); cursor: pointer;
  }
  .period-fy-btn.selected { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 700; }
  .period-month-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
  .period-month-btn {
    border: 1px solid #e6e9ee; border-radius: 10px; background: #fff;
    min-height: 44px; font-size: 15px; color: var(--text); cursor: pointer;
  }
  .period-month-btn.selected { background: var(--primary); border-color: var(--primary); color: #fff; font-weight: 700; }

  /* ---- タップ領域・iOS自動ズーム防止 ---- */
  /* フォーカス可能なinput/selectを16px未満にするとiOSが自動ズームするため16px固定 */
  input.form-control, select.form-select, textarea.form-control { font-size: 16px; }
  .form-select, .btn:not(.btn-sm) { min-height: 44px; }

  /* ---- ログイン画面（380px固定幅が375px端末ではみ出す問題の修正） ---- */
  .login-box { width: min(380px, calc(100vw - 32px)); padding: 32px 24px; }

  /* ---- 下部タブナビ ---- */
  #tabbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 1039;
    display: flex; background: #fff; border-top: 1px solid #e0e0e0;
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }
  .tab-item {
    flex: 1; min-height: 56px; border: none; background: none; padding: 6px 0 4px;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 2px; font-size: 11px; color: var(--muted); cursor: pointer;
  }
  .tab-item i { font-size: 20px; line-height: 1; }
  .tab-item.active { color: var(--primary); font-weight: 700; }

  /* ---- ボトムシート（Bootstrap Offcanvas bottom） ---- */
  .mobile-sheet {
    height: auto;
    max-height: 85vh;   /* dvh未対応の旧iOS向けfallback */
    max-height: 85dvh;
    border-radius: 16px 16px 0 0;
  }
  .mobile-sheet .offcanvas-header { padding: 14px 16px 6px; }
  .mobile-sheet .offcanvas-body {
    padding: 10px 16px calc(16px + env(safe-area-inset-bottom));
  }
  .sheet-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
  .sheet-item {
    border: 1px solid #e6e9ee; border-radius: 12px; background: #fff;
    min-height: 76px; padding: 8px 4px;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 6px; font-size: 12px; color: var(--text); cursor: pointer; text-align: center;
  }
  .sheet-item i { font-size: 22px; color: var(--secondary); }
  .sheet-item:active { background: #f0f4f8; }
  /* メニューシートのユーザー情報＋ログアウト行 */
  .sheet-user {
    display: flex; align-items: center; justify-content: space-between;
    gap: 10px; padding: 12px 4px 0; border-top: 1px solid #eee; margin-top: 14px;
    font-size: 14px;
  }

  /* ---- Toast: 下部タブより上に表示 ---- */
  #toast-container {
    bottom: calc(56px + env(safe-area-inset-bottom) + 12px);
    left: 12px; right: 12px;
  }
  .toast-msg { min-width: 0; }

  /* ---- グラフ ---- */
  .chart-wrap { height: 220px; }
}

/* ============================================================
 * Phase 3: テーブルのカード化（767.98px以下のみ）
 * 使い方: <table class="table table-sm table-mobile-cards"> とし、
 *         各 <td> に data-label="見出し" を付ける。
 *         列数が権限で変動する画面があるため、data-label は
 *         列インデックスではなく必ず td ごとに直接付けること。
 * ============================================================ */
@media (max-width: 767.98px) {
  /* --- カード化 --- */
  .table-mobile-cards { border: none; background: transparent; }
  .table-mobile-cards thead { display: none; }
  .table-mobile-cards tbody, .table-mobile-cards tr, .table-mobile-cards td { display: block; width: 100%; }
  .table-mobile-cards tr {
    background: #fff; border: 1px solid #e6e9ee; border-radius: 10px;
    margin-bottom: 8px; padding: 6px 12px;
  }
  .table-mobile-cards td {
    display: flex; justify-content: space-between; align-items: baseline; gap: 12px;
    border: none; padding: 5px 0; font-size: 14px;
    text-align: right;             /* 値は右寄せ（金額の桁を読みやすく） */
  }
  .table-mobile-cards td + td { border-top: 1px solid #f2f4f7; }
  /* 見出しはCSSの擬似要素で出す。VoiceObject対策として重要な表では
     DOM側にも見出しを残すこと（台帳の注意点参照） */
  .table-mobile-cards td::before {
    content: attr(data-label);
    flex: 0 0 auto; color: var(--muted); font-size: 12px; font-weight: 600;
    text-align: left; white-space: nowrap;
  }
  /* data-label が無い td（合計行など）は通常表示に戻す */
  .table-mobile-cards td:not([data-label]) { display: block; text-align: left; }
  /* 1列目を見出し扱いにするパターン */
  .table-mobile-cards td.card-title-cell {
    display: block; text-align: left; font-weight: 700; font-size: 15px;
    border-bottom: 1px solid #eee; padding-bottom: 6px; margin-bottom: 2px;
  }
  .table-mobile-cards td.card-title-cell::before { content: none; }
  /* 合計行（tfoot）はカードとして色を変えて強調 */
  .table-mobile-cards tfoot tr { background: #f4f6f9; border-color: #d9dee6; font-weight: 700; }

  /* --- 横スクロール維持の表につけるヒント --- */
  .table-scroll-hint {
    font-size: 11px; color: var(--muted); margin-bottom: 4px;
  }
  .table-scroll-hint::before { content: "← 横にスクロールできます"; }
}

/* ============================================================
 * Phase 3: 列数の多い明細の「主要項目＋詳細展開」（767.98px以下のみ）
 * 使い方: <table class="table-mobile-expand"> とし、行を
 *   <tr class="row-summary">（主要項目＋開閉ボタン）と
 *   <tr class="row-detail">（残りの項目・既定で非表示）のペアで出す。
 * 開閉は行タップではなく <button aria-expanded aria-controls> で行う
 *   （スクリーンリーダーで状態が読めるようにするため）。
 * ============================================================ */
@media (max-width: 767.98px) {
  .table-mobile-expand { border: none; background: transparent; }
  .table-mobile-expand thead { display: none; }
  .table-mobile-expand tbody, .table-mobile-expand tr, .table-mobile-expand td { display: block; width: 100%; }
  .table-mobile-expand tr.row-summary {
    background: #fff; border: 1px solid #e6e9ee; border-radius: 10px;
    margin-bottom: 8px; padding: 8px 12px;
  }
  /* 詳細行は直前のサマリーカードと繋げて見せる */
  .table-mobile-expand tr.row-detail {
    background: #fff; border: 1px solid #e6e9ee; border-top: none;
    border-radius: 0 0 10px 10px; margin: -8px 0 8px; padding: 0 12px 8px;
  }
  .table-mobile-expand tr.row-summary.is-open { border-radius: 10px 10px 0 0; margin-bottom: 0; }
  .table-mobile-expand tr.row-detail[hidden] { display: none; }
  .table-mobile-expand td { border: none; padding: 0; }
  /* サマリー: 見出し行＋主要項目 */
  .expand-head {
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
    font-weight: 700; font-size: 15px;
  }
  .expand-toggle {
    border: 1px solid #e6e9ee; background: #fff; border-radius: 8px;
    min-width: 44px; min-height: 32px; color: var(--secondary); cursor: pointer;
    font-size: 12px; padding: 4px 8px; flex: 0 0 auto;
  }
  .expand-toggle[aria-expanded="true"] .bi::before { content: "\f2ea"; } /* chevron-up */
  .expand-main {
    display: flex; flex-wrap: wrap; gap: 4px 16px; margin-top: 6px; font-size: 14px;
  }
  .expand-main .kv { display: flex; gap: 6px; align-items: baseline; }
  .expand-main .kv .k { color: var(--muted); font-size: 12px; font-weight: 600; }
  /* 詳細: 2列グリッド */
  .expand-detail-grid {
    display: grid; grid-template-columns: 1fr 1fr; gap: 4px 12px;
    border-top: 1px solid #f2f4f7; padding-top: 8px; font-size: 14px;
  }
  .expand-detail-grid .kv { display: flex; justify-content: space-between; gap: 8px; }
  .expand-detail-grid .kv .k { color: var(--muted); font-size: 12px; font-weight: 600; }
}
/* PCでは展開用のモバイル専用セルを出さない */
@media (min-width: 768px) {
  .mobile-only-cell { display: none !important; }
}

@media (max-width: 767.98px) {
  /* rowspan でカテゴリを結合している表のスマホ用カテゴリ見出し */
  .table-mobile-expand tr.cat-header-row { display: block; margin: 10px 0 6px; }
  .table-mobile-expand tr.cat-header-row td {
    display: block; border: none; padding: 0 2px;
    font-size: 13px; font-weight: 700; color: var(--primary);
    border-left: 3px solid var(--secondary); padding-left: 8px;
  }
}

@media (max-width: 767.98px) {
  /* 検索・絞り込みの横並び行（日付＋セレクト＋ボタン）が画面幅を超えるため折り返す。
     .flex-nowrap を明示している箇所は意図的な横並びとみなし対象外にする */
  #content .d-flex:not(.flex-nowrap) { flex-wrap: wrap; }
  /* 幅を固定しているフォーム部品が画面からはみ出さないようにする */
  #content .form-control, #content .form-select, #content .input-group { max-width: 100%; }
}
