/* ==========================================================================
    基本設定
    ========================================================================== */

/* HTMLとBODYをビューポートの高さに合わせ、flexコンテナの基準とします */
html {
    font-size: var(--base-font-size, 16px); /* CSS変数を使用して基本フォントサイズを設定 */
    height: 100%; /* ここに高さを追加 */
}
body {
    height: 100%;
    margin: 0;
    padding: 0;
    font-family: "Inter", sans-serif; /* Google FontsのInterを使用 */
    background-color: #f8fafc; /* 非常に明るいグレーの背景 */
    color: #334155; /* 暗いスレートグレーのテキスト色 */
}

/* アプリケーションのメインコンテナ */
#app-container {
    display: flex;
    flex-direction: column;
    height: 100%; /* 親 (body) の高さを継承 */
    padding: 0.25rem; /* 全体的なパディング */
    box-sizing: border-box; /* パディングをボックスサイズに含める */
    width: fit-content; /* 幅をウィンドウいっぱいに広げる */
    max-width: 100%;
    margin-left: auto;
    margin-right: auto;
    padding-top: 0.25rem; /* 必要に応じて調整 */
}

/* ==========================================================================
    一般的なテーブルスタイル
    ========================================================================== */

table {
    border-collapse: separate !important; /* セルの境界線を結合 */
    border-spacing: 0; /* セル間のスペースをなくす */
    table-layout: fixed;
    margin-top: 0; /* 上部のマージンを削除 */
    background-color: white; /* テーブルの背景を白に */
    border-radius: 0.75rem; /* 角を丸くする */
}

/* テーブルヘッダーとセルの共通スタイル */
th, td {
    white-space: nowrap; /* テキストの改行を防ぐ */
    box-sizing: border-box; /* パディングをボックスサイズに含める */
    padding: 0rem 0.25rem 0rem 0.25rem;
    border-width: 0px 1px 1px 0px;
    border-color: #e2e8f0;
}

/* テーブルヘッダーのスタイル */
th {
    background-color: #4338ca; /* 濃いインディゴの背景 */
    color: white; /* 白いテキスト */
    font-weight: 700; /* 太字 */
    cursor: default; /* アイコン上のみポインター、ヘッダー全体ではない */
    position: relative; /* ソート矢印の配置用 */
    user-select: none; /* テキスト選択を無効にする */
    display: table-cell; /* thがテーブルセルとして動作するようにする */
    vertical-align: middle; /* コンテンツを垂直方向中央に配置 */
}

/* 画像セルの特定のパディング */
td.image-cell
, .measured-image-cell {
    padding: 0; /* 画像セルの最小パディング */
}

/* ヘッダーホバー時の背景色変更 */
th:hover {
    background-color: #3730a3; /* 少し濃いインディゴ */
}

/* ソート可能なヘッダーの右パディングを上書き */
.measured-sorted {
    padding-left: 0.25rem; /* ソート可能なカラムの現在の右パディングを維持、影に合わせて調整 */
    padding-right: 2.5rem; /* ソート可能なカラムの現在の右パディングを維持、影に合わせて調整 */
}

/* 偶数行の背景色を設定 */
tbody tr:nth-child(even) {
    background-color: #f1f5f9; /* 非常に薄いスレートグレー */
}

/* 行ホバー時の背景色変更 */
tbody tr:hover {
    background-color: #e0e7ff; /* 薄いインディゴ */
}

/* ==========================================================================
    テーブル要素 (ソート、フィルターアイコン)
    ========================================================================== */

/* フィルター切り替えボタン */
#toggleFilterButton {
    font-size: 1.8rem; /* 大きいアイコン */
    border-radius: 0.5rem; /* 角丸 */
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06); /* 微妙な影 */
    padding: 0.5rem; /* アイコン周りのパディング */
    margin-right: 0.5rem; /* フィルター切り替えと設定ボタンの間のスペース */
    display: flex; /* アイコンを中央に配置するためにflexを使用 */
    align-items: center;
    justify-content: center;
    cursor: pointer;
    border: none;
}

/* フィルター切り替えボタンの非アクティブ状態 */
#toggleFilterButton.inactive {
    background-color: white;
    color: #475569; /* スイッチラベルのテキスト色に合わせる */
}

#toggleFilterButton.inactive:hover {
    background-color: #e2e8f0; /* ホバー時の微妙な暗い背景 */
    color: #334155; /* ホバー時に少し暗く */
}

/* フィルター切り替えボタンのアクティブ状態 */
#toggleFilterButton.active {
    background-color: #4338ca; /* 濃いインディゴの背景 */
    color: white; /* 白いアイコン */
}

#toggleFilterButton.active:hover {
    background-color: #3730a3; /* 少し濃いインディゴ */
}

/* タグフィルター設定ボタンの特定のスタイル */
#tagFilterSettingsButton {
    font-size: 1.8rem; /* 大きいアイコン */
    color: #475569; /* スイッチラベルのテキスト色に合わせる */
    background-color: white; /* 白い背景 */
    border-radius: 0.5rem; /* 角丸 */
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06); /* 微妙な影 */
    padding: 0.5rem; /* アイコン周りのパディング */
    /* margin-left: auto; */ /* 削除、flexboxで処理 */
    display: flex; /* 追加: アイコンを中央に配置するためにflexを使用 */
    align-items: center; /* 追加: アイコンを垂直方向中央に配置 */
    justify-content: center; /* 追加: アイコンを水平方向中央に配置 */
    border: none; /* 追加: デフォルトのボーダーがないことを確認 */
}

#tagFilterSettingsButton:hover {
    background-color: #e2e8f0; /* ホバー時の微妙な暗い背景 */
    color: #334155; /* ホバー時に少し暗く */
}

#tagFilterSettingsButton:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* 新規: タイトルとボタンのコンテナ */
.header-container {
    display: flex;
    align-items: center;
    justify-content: space-between; /* タイトルとボタンの間にスペースを分配 */
    margin-bottom: 0.5rem; /* ヘッダーの下のスペース - 1.5remから変更 */
    padding: 0 0.5rem; /* 水平パディングを追加 */
}

/* ソート用のコンテナ */
.sort-container {
    display: flex;
    justify-items: center;
    align-items: center;
}
.sort-name {
    flex: 1;
}
.sort-icon {
    padding-left: 0.25rem;
    color: white; /* テーブルヘッダーは白 */
}
.sort-icon.active {
    font-size: 0.9em; /* 必要に応じてフォントサイズ調整 */
    color: #334155; /* 白い背景でのコントラストを良くするために暗いテキスト */
    font-weight: bold;
    background-color: rgba(255, 255, 255, 0.8); /* 不透明な白い背景 */
    border: 1px solid white; /* 白いボーダーを追加 */
    border-radius: 0.25rem; /* ボックスの角丸 */
    padding-top: 0.1rem; /* ボックス内のパディングを追加 */
    padding-bottom: 0.1rem;
    line-height: 1; /* テキストが適切に収まるようにする */
}

/* 新規: フィルター表示行 (tr3) */
#filterRow th {
    background-color: #6366f1; /* フィルター行は少し明るいインディゴ */
    color: white;
    font-weight: 500;
    /* 一般的なth, tdと一貫性のあるパディング、ボックスシャドウを考慮して調整 */
    padding: 0.25rem; 
    border-bottom: 1px solid #4338ca; /* 暗いボーダー */
    text-align: left; /* フィルターセルのデフォルトテキスト配置 */
    vertical-align: top; /* コンテンツを上部に配置 */
    box-sizing: border-box; /* パディングを高さに含める */
    height: auto; /* コンテンツに合わせて高さを調整 */
    overflow: hidden; /* オーバーフローコンテンツを非表示 */
    position: relative; /* クリアボタンの配置用 */
}

/* tr3 th内のインラインフィルター要素のスタイル */
#filterRow th .filter-controls-container {
    display: flex;
    flex-direction: column; /* メインコンテナは要素を垂直に積み重ねる */
    gap: 0.5rem; /* 検索とクリアラッパーとオプションリストの間のスペース */
    position: relative; /* 一般的な配置のために相対位置を維持 */
}

/* 新規: 検索入力とクリアボタンのコンテナ */
#filterRow th .search-and-clear-wrapper {
    position: relative; /* ボタンの配置コンテキストを確立 */
    width: 100%; /* 全幅を占めることを確認 */
}

#filterRow th .filter-search-input {
    width: 100%; /* 検索入力が利用可能なスペースを占めるようにする */
    padding: 0.3rem 0.5rem;
    border: 1px solid rgba(255, 255, 255, 0.4);
    border-radius: 0.375rem;
    box-sizing: border-box;
    font-size: 0.9rem; /* フォントサイズを大きく */
    color: white;
    background-color: rgba(255, 255, 255, 0.1);
    padding-right: 2rem; /* ボタンサイズに基づいて調整 */
}
#filterRow th .filter-search-input::placeholder {
    color: rgba(255, 255, 255, 0.7);
}
#filterRow th .filter-search-input:focus {
    outline: none;
    border-color: white;
    background-color: rgba(255, 255, 255, 0.2);
}

#filterRow th .filter-options-list {
    list-style: none;
    padding: 0;
    margin: 0;
    height: 150px; /* スクロール可能なオプションの固定高さ */
    overflow-y: auto;
    background-color: rgba(255, 255, 255, 0.05); /* リストの背景を少し暗く */
    border-radius: 0.25rem;
}
#filterRow th .filter-options-list li {
    padding: 0; /* liのパディングを0に設定 */
    cursor: default; /* liのカーソルをデフォルトに設定 */
    transition: none; /* liのトランジションを削除 */
    border-radius: 0; /* liのborder-radiusを0に設定 */
}
#filterRow th .filter-options-list label {
    display: flex;
    align-items: center;
    font-size: 0.9rem; /* フォントサイズを大きく */
    color: white;
    cursor: pointer; /* ラベルは常にポインター */
    padding: 0.2rem 0.3rem; /* liからlabelに移動 */
    border-radius: 0.2rem; /* liからlabelに移動 */
    width: 100%; /* labelがliの幅全体を占めるようにする */
    box-sizing: border-box; /* パディングが幅に含める */
}
#filterRow th .filter-options-list label:hover { /* li:hoverからlabel:hoverに移動 */
    background-color: rgba(255, 255, 255, 0.15);
}
#filterRow th .filter-options-list input[type="checkbox"] {
    margin-right: 0.4rem;
    accent-color: white;
}

/* 新規: 利用できないフィルターオプションのスタイル (グレーアウト) */
#filterRow th .filter-options-list li.unavailable-option label {
    color: rgba(255, 255, 255, 0.5); /* テキストをグレーアウト */
    cursor: pointer; /* 利用できない場合でもポインターカーソルを維持 */
}
#filterRow th .filter-options-list li.unavailable-option input[type="checkbox"] {
    opacity: 0.5; /* チェックボックスを薄くする */
    cursor: pointer; /* 利用できない場合でもポインターカーソルを維持 */
}
#filterRow th .filter-options-list li.unavailable-option label:hover {
    background-color: rgba(255, 255, 255, 0.05); /* 利用できないアイテムの微妙なホバー効果 */
}


#filterRow th .filter-image-thumbnail {
    max-width: 30px; /* インライン表示用の小さいサムネイル */
    max-height: 30px;
    object-fit: contain;
    margin-left: 0.4rem;
    border-radius: 0.15rem;
    flex-shrink: 0;
}

#filterRow th .clear-filter-button {
    position: absolute; /* 検索とクリアラッパー内で絶対位置に配置 */
    right: 0.25rem; /* 入力の右端からの位置 */
    top: 50%; /* 垂直方向中央 */
    transform: translateY(-50%); /* 完璧な垂直中央配置のために調整 */
    background-color: #dc2626; /* 赤い背景 */
    border: 1px solid #dc2626; /* 実線赤ボーダー */
    color: white; /* 白いアイコン色 */
    font-size: 1em; /* アイコンのフォントサイズを大きく */
    cursor: pointer;
    padding: 0.2rem 0.4rem; /* ボタンのような外観のためのパディング */
    border-radius: 0.375rem; /* 角丸 */
    line-height: 1; /* 余分なスペースを防ぐために行の高さを調整 */
}
#filterRow th .clear-filter-button:hover {
    background-color: #b91c1c; /* ホバー時に濃い赤 */
    border-color: #b91c1c; /* ホバー時に濃い赤ボーダー */
    color: white;
}


/* ==========================================================================
    スイッチボタン (現在は設定ダイアログのみ)
    ========================================================================== */

.switch-container {
    display: flex;
    align-items: center;
}

.switch {
    position: relative;
    display: inline-block;
    width: 48px; /* 幅 */
    height: 24px; /* 高さ */
    flex-shrink: 0; /* スイッチが縮むのを防ぐ */
    padding: 0.25rem 0.5rem; /* スイッチラベルにパディングを追加 */
    box-sizing: border-box; /* パディングが幅/高さに含まれることを確認 */
    cursor: pointer; /* スイッチラベルにカーソルを維持 */
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: .4s;
    border-radius: 24px; /* 角丸 */
}

.slider:before {
    position: absolute;
    content: "";
    height: 18px; /* 円の高さ */
    width: 18px; /* 円の幅 */
    left: 3px; /* 左からの位置 */
    bottom: 3px; /* 下からの位置 */
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: #4f46e5; /* ON時の色 */
}

input:checked + .slider:before {
    transform: translateX(24px); /* ON時の移動距離 */
}

.switch-label {
    color: #475569;
}

/* ==========================================================================
    タグ設定ダイアログ (新規)
    ========================================================================== */
.tag-settings-dialog {
    position: fixed; /* ビューポートに対する固定位置 */
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 90vw; /* ビューポート幅の90% */
    height: 90vh; /* ビューポート高さの90% */
    background-color: white;
    border-radius: 0.75rem; /* 角丸 */
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05); /* 影を再追加 */
    z-index: 1001; /* 他の要素より上 */
    padding: 1.5rem;
    display: none; /* デフォルトでは非表示 */
    flex-direction: column;
    max-width: 90vw; /* 全幅を占めることを確認 */
    max-height: 90vh; /* 全高を占めることを確認 */
    overflow-y: auto; /* コンテンツがオーバーフローした場合にスクロール */
}

.tag-settings-dialog.open {
    display: flex; /* 開いているときに表示 */
}

.dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5); /* 半透明の黒いオーバーレイ */
    z-index: 1000; /* ダイアログの下、他のコンテンツの上 */
    display: none; /* デフォルトでは非表示 */
}

.dialog-overlay.open {
    display: block; /* 開いているときに表示 */
}

.dialog-close-btn {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    font-size: 1.5rem;
    cursor: pointer;
    color: #64748b;
    background: none;
    border: none;
    padding: 0.25rem;
    border-radius: 0.25rem;
    transition: background-color 0.2s ease;
}

.dialog-close-btn:hover {
    background-color: #e2e8f0;
}

.tag-settings-options-container {
    flex-grow: 1; /* 利用可能なスペースを占める */
    overflow-y: auto; /* オプションをスクロール */
    /* margin-bottom: 0.75rem; */ /* ボタンがないため、下マージンなし */
}

/* タグ設定オプションのグリッドレイアウト */
.tag-settings-options-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); /* より良いフィットのためにminmaxを調整 */
    gap: 0.5rem; /* グリッドアイテム間のギャップ */
}

/* 設定ポップアップ内の個々のタグスイッチのスタイル */
.tag-settings-item {
    display: flex; /* flexboxを使用 */
    align-items: center; /* アイテムを垂直方向中央に配置 */
    justify-content: space-between; /* ラベルを左に、スイッチを右に配置 */
    padding: 0; /* LIからのパディングを削除 */
    border-radius: 0.5rem; /* 少し角丸 */
    background-color: #f0f4f8; /* 明るい背景 */
    box-shadow: 0 1px 2px rgba(0,0,0,0.05); /* 微妙な影 */
    transition: background-color 0.2s ease;
    min-height: 40px; /* より良いタッチターゲットのための最小高さ */
    cursor: pointer; /* アイテム全体をクリック可能にする */
}

.tag-settings-item:hover {
    background-color: #e2e8f0;
}

/* 新規: 設定ダイアログ内のタグ名テキストのスタイル */
.tag-name-label {
    flex-grow: 1; /* ラベルが利用可能なスペースを占めるようにする */
    margin-right: 0.5rem; /* ラベルとスイッチの間のスペース */
    font-size: 0.9rem; /* コンパクトにするために少し小さいフォント */
    white-space: nowrap; /* タグ名の改行を防ぐ */
    overflow: hidden; /* オーバーフローコンテンツを非表示 */
    text-overflow: ellipsis; /* 長いタグ名に省略記号を追加 */
    cursor: pointer; /* テキスト部分のラベルにカーソルを維持 */
    color: #475569; /* スイッチラベルの色に合わせる */
    padding: 0.25rem 0.5rem; /* ラベルにパディングを追加 */
    box-sizing: border-box; /* パディングが幅/高さに含まれることを確認 */
    display: flex; /* 必要に応じてテキストを垂直方向中央に配置するためにflexを確保 */
    align-items: center; /* ラベルの高さが異なる場合にテキストを垂直方向中央に配置 */
    height: 100%; /* ラベルが利用可能な高さを埋めるようにする */
}

/* tag-settings-item内のスイッチの特定の調整で右揃えにする */
.tag-settings-item .switch {
    /* margin-left: auto; */ /* justify-contentで処理されるため削除 */
    flex-shrink: 0; /* 縮むのを防ぐ */
}

/* ダイアログアクションボタンのコンテナ */
.dialog-actions {
    display: flex;
    justify-content: flex-end; /* ボタンを右に配置 */
    gap: 0.75rem; /* ボタン間のスペース */
    margin-top: 1.5rem; /* ボタンの上のスペース */
}

.dialog-actions button {
    width: 100px; /* 一貫性のために固定幅を設定 */
    padding: 0.75rem 1.5rem;
    border-radius: 0.5rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease;
    text-align: center; /* ボタン内のテキストを中央に配置 */
}

.dialog-apply-btn {
    background-color: #4338ca;
    color: white;
    border: none;
}

.dialog-apply-btn:hover {
    background-color: #3730a3;
}

.dialog-cancel-btn {
    background-color: #e2e8f0;
    color: #334155;
    border: 1px solid #cbd5e1;
}

.dialog-cancel-btn:hover {
    background-color: #cbd5e1;
}

/* フィルターまたはソートされていないカラムの水平パディングを削除 */
.default-cell-padding {
    padding-left: 0.25rem !important;
    padding-right: 0.25rem !important;
}

/* ==========================================================================
    固定カラム
    ========================================================================== */

/* テーブルの水平/垂直スクロール用コンテナ */
.table-scroll-container {
    overflow: auto; /* xとyの両方のスクロールを処理 */
    flex: 1; /* 残りの高さを埋める */
    background-color: whitesmoke;
}

/* theadを固定 */
thead {
    position: sticky;
    top: 0;
    z-index: 10 !important; /* ヘッダー行全体を固定。他のthより低いz-indexから開始 */
}

/* キーカラムを固定するためのスタイル */
.sticky-key-column {
    position: sticky;
    z-index: 5 !important; /* stickyボディセルを通常のボディセルより上に表示 */
    background-color: white; /* デフォルトの背景色 */
    will-change: transform, scroll-position;
}

/* キーカラムのヘッダーセルを他のヘッダーセルよりも上に表示し、上部に固定 */
thead th.sticky-key-column {
    position: sticky; /* Explicitly add position: sticky here */
    top: 0; /* ヘッダー内のキーカラムセルも上部に固定 */
    background-color: #4338ca; /* ヘッダーの背景色と合わせる */
    will-change: transform, scroll-position;
}

/* フィルター行のセルにもsticky-key-columnを適用 */
#filterRow th.sticky-key-column {
    position: sticky;
    background-color: #6366f1; /* フィルター行の背景色と一致させる */
    will-change: transform, scroll-position;
}

/* 偶数行のstickyキーカラムの背景色を上書きして、ストライプ効果を維持 */
tbody tr:nth-child(even) .sticky-key-column {
    background-color: #f1f5f9;
}
/* 奇数行のstickyキーカラムの背景色を上書きして、ストライプ効果を維持 */
tbody tr:nth-child(odd) .sticky-key-column {
    background-color: white;
}

/* 行ホバー時の固定キーカラムの背景色変更 */
tbody tr:hover .sticky-key-column {
    background-color: #e0e7ff; /* 薄いインディゴ */
}

/* 測定用フィルターコンテナのスタイル */
#measurementFilterContainer {
    display: flex; /* 測定用として表示され、スペースを占める必要がある */
    position: absolute; /* レイアウトに影響を与えないように絶対位置を維持 */
    visibility: hidden; /* デフォルトでは非表示 */
    pointer-events: none; /* ポインターイベントなし */
}

/* 測定コンテナ内の個々のフィルターコントロールのスタイル */
#measurementFilterContainer .filter-controls-container {
    background-color: white; /* 白い背景 */
    border: 1px solid #cbd5e1; /* 薄いグレーのボーダー */
    border-radius: 0.5rem; /* 角丸 */
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06); /* 微妙な影 */
    display: flex; /* ulを囲むため */
    flex-direction: column; /* 要素を垂直に積み重ねる */
    gap: 0.5rem; /* 要素間のスペース */
    overflow: visible; /* コンテンツがクリップされないことを確認 */
    max-height: none; /* 継承された最大高さを削除 */
    width: fit-content; /* コンテンツに合わせて幅を調整 */
}

#measurementFilterContainer .filter-search-input,
#measurementFilterContainer .clear-filter-button {
    display: none; /* 測定モードでは非表示 */
}

#measurementFilterContainer .filter-options-list {
    padding: 0;
    margin: 0;
    height: 150px; /* スクロールバーを強制表示するための固定高さ */
    overflow-y: scroll; /* 常に垂直スクロールバーを表示 */
    background-color: transparent; /* リストの背景を透明に */
    border: 1px dashed #e2e8f0; /* 測定リストの視覚的な手がかり */
    border-radius: 0.25rem;
}

#measurementFilterContainer .filter-options-list li {
    padding: 0; /* liのパディングを0に設定 */
}

#measurementFilterContainer .filter-options-list label {
    font-size: 0.9rem;
    color: #334155; /* 白い背景でのコントラストを良くするために暗いテキスト */
    padding: 0.2rem 0.3rem; /* liからlabelに移動 */
    border-radius: 0.2rem; /* liからlabelに移動 */
}

#measurementFilterContainer .filter-options-list input[type="checkbox"] {
    accent-color: #4f46e5;
}