/* General Body and Text Selection */
::selection {
    background-color: #e2e8f0; /* slate-200 */
    color: #1e293b; /* slate-800 */
}
body {
    font-family: 'Noto Sans JP', sans-serif;
    background-color: #f8fafc; /* slate-50 */
    color: #1f2937; /* gray-800 */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Header Buttons (Tab-like) */
.header-btn {
    font-weight: 500;
    font-size: 0.875rem;
    padding: 0.5rem 1rem;
    border-radius: 0.375rem;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    color: #374151; /* gray-700 */
    background-color: transparent;
}
.header-btn:hover {
    background-color: #f3f4f6; /* gray-100 */
    color: #1f2937; /* gray-800 */
}
.header-btn:disabled, .header-btn.disabled {
    color: #9ca3af; /* gray-400 */
    cursor: not-allowed;
    background-color: transparent;
}
.header-btn svg {
    color: #6b7280; /* gray-500 */
}
.header-btn:hover svg {
    color: #374151; /* gray-700 */
}
.header-btn:disabled svg, .header-btn.disabled svg {
    color: #9ca3af; /* gray-400 */
}

/* Table Layout */
.table-wrapper {
    border: 1px solid #e5e7eb;
    border-radius: 0.75rem;
    overflow: hidden;
    background-color: #fff;
    box-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);
}
th {
    background-color: #f9fafb; /* gray-50 */
    font-weight: 500;
    color: #6b7280; /* gray-500 */
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-size: 0.75rem;
    text-align: left;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid #e5e7eb; /* gray-200 */
}
td {
    border-bottom: 1px solid #e5e7eb;
    vertical-align: middle;
    padding: 0;
    font-size: 0.875rem;
}
tr:last-child td {
    border-bottom: none;
}
tr.group-hover td {
    background-color: #f1f5f9; /* A lighter slate-100 */
}

/* Form Elements */
.form-input, .form-select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    width: 100%;
    height: 100%;
    background-color: transparent;
    border: 1px solid transparent;
    padding: 0.75rem 1rem;
    border-radius: 0.375rem;
    transition: all 0.2s ease-in-out;
    line-height: 1.5;
}
.form-input:focus, .form-select:focus {
    background-color: white;
    border-color: #475569; /* slate-600 */
    position: relative;
    z-index: 1;
    box-shadow: 0 0 0 2px rgba(71, 85, 105, 0.2);
    outline: none;
}

/* NEW Account Cell Styles */
.account-cell {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem;
}

.account-cell.full-width {
    /* 互換性のため残存（縦レイアウトでは実質不要） */
}

.account-main-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.375rem;
}

.ai-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.5rem;
    height: 1.5rem;
    font-size: 0.65rem;
    font-weight: 700;
    line-height: 1;
    color: #475569;
    background-color: #e2e8f0;
    border-radius: 50%;
    flex-shrink: 0;
}

.account-cell .form-select {
    width: 100%;
    border: none;
    background-color: transparent;
}

/* 編集モード: 主科目+補助科目の2段 SELECT ラッパー (BUG-3 解消) */
.account-editor-wrapper {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    width: 100%;
    padding: 0.25rem;
}
.account-editor-wrapper .form-select,
.account-editor-wrapper .form-input-inline {
    width: 100%; /* 両 SELECT を同幅に強制 → BUG-3 解消 */
}

/* Specific Cell Styles & Components */
.text-right { text-align: right; }
.text-center { text-align: center; }
.font-mono { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }

.warning-cell {
    position: relative;
}
.warning-icon {
    position: absolute;
    top: 4px;
    left: 4px;
    color: #f59e0b; /* amber-500 */
    cursor: help;
}

.invoice-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    min-height: 4.5rem; /* Match height of account cells */
    padding: 0.5rem 0;
    cursor: default;
}
.invoice-status {
    font-weight: 500;
    font-size: 0.875rem;
}
.invoice-status.qualified { color: #334155; /* slate-700 */ }
.invoice-status.categorized { color: #334155; /* slate-700 */ }
.invoice-t-number {
    font-size: 0.75rem;
    color: #9ca3af; /* gray-400 */
    margin-top: 0.125rem;
    margin-left: 0.5rem; /* Add space */
}

/* Skeleton Loader (from original file) */
.skeleton-loader {
    background-color: #e2e8f0; /* slate-200 */
    border-radius: 0.25rem;
    animation: pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
    display: inline-block;
    width: 100%;
    height: 1.25rem;
}
@keyframes pulse {
    50% { opacity: .5; }
}

/* --- Card-based Scrolling Layout (Final) --- */
body.overflow-hidden {
    overflow: hidden;
}

.results-group {
    border: 1px solid #e5e7eb;
    border-radius: 0.75rem;
    background-color: #fff;
    box-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.07), 0 1px 2px -1px rgb(0 0 0 / 0.07);
    /* flex:1 で results-container の高さを充填。min-height:0 でオーバーフロー許可 */
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.results-group-header {
    flex-shrink: 0;
    background-color: #f9fafb;
}


.results-group-title {
    font-size: 1.125rem;
    font-weight: 600;
    color: #1f2937;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid #e5e7eb;
    height: 3.5rem;
    box-sizing: border-box;
    display: flex; /* <-- Added */
    align-items: center; /* <-- Added */
    gap: 0.75rem; /* <-- Added */
}

.count-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.25rem 1rem;
    font-size: 0.8rem;
    font-weight: 600;
    color: #475569; /* slate-600 */
    background-color: #e2e8f0; /* slate-200 */
    border-radius: 9999px; /* rounded-full */
}

.results-table-container {
    /* テーブル唯一のスクロールコンテナ。<main> はスクロールしない */
    flex-grow: 1;
    overflow-y: auto;
    min-height: 0;
    padding-bottom: 2rem;
}

.results-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

.results-table thead {
    position: sticky;
    top: 0; /* results-table-container がスクロール親なので 0 で正しい */
    z-index: 10;
    background-color: #f8f9fa; /* Match th background */
}

.results-table thead::after {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    bottom: -1px; /* Position it perfectly at the bottom */
    height: 3px;
    background-color: #475569;
}

.results-table th {
    font-weight: 600;
    color: #374151;
    text-align: center;
    padding: 0.75rem 1rem;
    border-right: 1px solid #d1d5db;
}

.results-table td {
    border-bottom: 1px solid #d1d5db; /* gray-300 */
    border-right: 1px solid #d1d5db; /* gray-300 */
    padding: 0; /* Inputs will provide padding */
    vertical-align: middle;
    transition: background-color 0.15s ease-in-out;
}

.results-table tr.group-last-row td {
    border-bottom-width: 1px; /* Keep consistent border */
}

.results-table th:last-child,
.results-table td:last-child {
    border-right: none;
}

/* Zebra-striping for rows */
.results-table tbody tr:nth-child(even) {
    background-color: #f8fafc; /* slate-50 */
}

.group-hover td {
    background-color: #f1f5f9; /* slate-100 */
}

.results-table tr.group-hover td {
    background-color: #e2e8f0; /* slate-200 */
}

.results-table .form-input,
.results-table .form-select {
    border-radius: 0; /* Make inputs flush with cells */
    background-color: transparent;
}

.results-table .form-input:focus,
.results-table .form-select:focus {
    border-radius: 0.375rem; /* Restore radius on focus */
}

/* --- NEW STYLES FOR CONFIDENCE ICONS --- */
.cell-wrapper {
    position: relative;
    display: flex;
    align-items: center;
    width: 100%;
    height: 100%;
}

.cell-wrapper .form-input,
.cell-wrapper .form-select,
.cell-wrapper .form-input-inline,
.cell-wrapper .invoice-container,
.cell-wrapper .account-cell {
    flex-grow: 1;
}

.confidence-icon {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 5;
    /* cursor: help; <-- Removed as requested */
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Adjust padding for inputs that have an icon to prevent text overlap */
.cell-wrapper .form-input,
.cell-wrapper .form-select {
    padding-right: 2rem; /* Make space for the icon */
}

/* --- Alignment Fixes --- */
.results-table tbody td:first-child {
    text-align: center;
}

.results-table .cell-wrapper:has([data-field="taxRate"]),
.results-table .cell-wrapper:has([data-field="transactionDate"]),
.results-table .cell-wrapper:has([data-field="amount"]) {
    justify-content: center;
}
.results-table select[data-field="taxRate"] {
    text-align: center;
    text-align-last: center;
}

/* --- Inline Edit Styles --- */
.form-input-inline {
    display: block;
    appearance: none;
    -webkit-appearance: none;
    background-color: transparent;
    border: 1px solid transparent;
    padding: 0.5rem 0.75rem; /* py-2 px-3 */
    width: 100%;
    font-size: inherit;
    line-height: inherit;
    font-family: inherit;
    color: inherit;
    margin: 0;
    box-sizing: border-box;
    border-radius: 0;
}

.form-input-inline:focus {
    background-color: #ffffff;
    border: 1px solid #475569; /* slate-600 */
    border-radius: 0.375rem; /* rounded-md */
    outline: 2px solid rgba(71, 85, 105, 0.3);
    outline-offset: 0px;
}

.dark .form-input-inline:focus {
    background-color: #1e293b; /* surface-dark */
    border-color: #94a3b8; /* slate-400 */
    outline-color: rgba(148, 163, 184, 0.4);
}

textarea.form-input-inline {
    height: auto;
    min-height: 6rem; /* h-24 */
}

/* --- Editor Sizing Fixes (v2) --- */
/*
 * The inline editors for date and textarea were not respecting container width
 * due to a combination of browser-native styles and CSS specificity.
 * These highly specific rules force the editors to behave correctly.
*/
.results-table td > .cell-wrapper > input.form-input-inline[data-field="transactionDate"] {
    /* Forcing box-sizing and 100% width on the date input specifically */
    box-sizing: border-box;
    width: 100%;
}

.results-table td > .cell-wrapper > textarea.form-input-inline[data-field="description"] {
    /* Forcing the textarea to grow within its flex container */
    flex-grow: 1;
    width: 100%; /* Be explicit */
}

/* --- Editor Sizing Fixes (v3) --- */
.cell-wrapper > .editable-cell {
    flex-grow: 1;
}

/* ===== Modal (dialog) Styles (v4.9 - Final Flexbox Layout) ===== */
#image-modal::backdrop {
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(3px);
}

.detail-heading {
    font-size: 1rem;
    font-weight: 700;
    margin-top: 1.5rem;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid #e2e8f0; /* slate-200 */
}
.dark .detail-heading {
    border-bottom-color: #334155; /* slate-700 */
}
.detail-heading:first-child {
    margin-top: 0;
}

.detail-row {
    display: flex;
    align-items: start; /* Align top for long values */
    gap: 1.5rem;
    padding: 0.85rem 0;
    border-bottom: 1px solid #f1f5f9; /* slate-100 */
    padding-left: 1rem; /* Indent the row */
}
.dark .detail-row {
    border-bottom-color: #334155; /* slate-700 */
}

.detail-label-wrapper {
    position: relative; /* Set positioning context for the icon */
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 140px; /* Fixed width for alignment */
    flex-shrink: 0; /* Prevent this column from shrinking */
    padding-top: 2px; /* Align better with value text */
}

#modal-details-pane .detail-label {
    font-size: 0.8rem;
    font-weight: 600;
    color: #475569; /* slate-600 */
}
.dark #modal-details-pane .detail-label {
    color: #94a3b8; /* slate-400 */
}

#modal-details-pane .detail-value {
    font-size: 0.95rem;
    font-weight: 500;
    color: #1e293b; /* slate-800 */
    word-wrap: break-word;
    text-align: left;
    flex-grow: 1;
}
.dark #modal-details-pane .detail-value {
    color: #e2e8f0; /* slate-200 */
}

#modal-details-pane .detail-value .invoice-container {
    padding: 0;
    min-height: auto;
    align-items: flex-start;
    display: flex;
    gap: 0.5rem;
}

/* New styles for image container and image */
#modal-image-pane > div {
    display: flex;
    align-items: center;
    justify-content: center;
}

#modal-image {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

/* Viewer.js custom styles */
.viewer-toolbar > ul > li {
    background-color: rgba(0, 0, 0, 0.5) !important;
    border-radius: 4px;
}
.viewer-toolbar > ul > li:hover {
    background-color: rgba(0, 0, 0, 0.8) !important;
}

/* --- Infinite Scroll --- */
.scroll-sentinel {
    height: 1px;
    width: 100%;
    visibility: hidden;
}

.scroll-loading-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 1.5rem;
    color: #94a3b8;
    font-size: 0.875rem;
}
/* トースト通知 */
.toast {
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 0.25s ease, transform 0.25s ease;
}
.toast.toast-visible {
    opacity: 1;
    transform: translateY(0);
}

/* 履歴プログレスバー */
.history-progress-bar {
    transition: width 0.5s ease-out;
}

/* --- Doc Type Tab Bar (Design A: Segmented Control) --- */
#doc-type-tab-bar {
    display: flex;
    align-items: center;
    flex-shrink: 0;
    background-color: #f9fafb;
    padding: 0.625rem 1rem;
    border-bottom: 1px solid #e5e7eb;
}

/* slate-200 のカプセル容器 */
.doc-type-segment-wrap {
    display: inline-flex;
    background-color: #e2e8f0;
    border-radius: 9999px;
    padding: 0.25rem;
    gap: 0.25rem;
}

/* 各タブボタン（非アクティブ） */
.doc-type-tab {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.375rem 1rem;
    font-size: 0.9rem;
    font-weight: 600;
    color: #64748b; /* slate-500 */
    background: transparent;
    border: none;
    border-radius: 9999px;
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
}
.doc-type-tab:hover {
    color: #475569; /* slate-600 */
}

/* アクティブタブ: 白いピルが浮かぶ */
.doc-type-tab--active {
    background: #ffffff;
    color: #1e293b; /* slate-800 */
    box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.08);
}

/* 非アクティブのバッジ */
.doc-type-tab-badge {
    display: inline-flex;
    align-items: center;
    padding: 0.1rem 0.5rem;
    font-size: 0.7rem;
    font-weight: 700;
    border-radius: 9999px;
    background: #cbd5e1; /* slate-300 */
    color: #475569; /* slate-600 */
    transition: all 0.2s ease;
}

/* アクティブのバッジ: slate-700 + 白文字 */
.doc-type-tab--active .doc-type-tab-badge {
    background: #334155; /* slate-700 */
    color: #ffffff;
}
