/* Reset & Base */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --success: #16a34a;
    --warning: #d97706;
    --danger: #dc2626;
    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-500: #6b7280;
    --gray-700: #374151;
    --gray-900: #111827;
    --radius: 6px;
    --shadow: 0 1px 3px rgba(0,0,0,.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Hiragino Sans', 'Noto Sans JP', sans-serif;
    background: var(--gray-50);
    color: var(--gray-900);
    line-height: 1.6;
    font-size: 14px;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Header */
.header {
    background: #fff;
    border-bottom: 1px solid var(--gray-200);
    padding: 0 1.5rem;
    height: 52px;
    display: flex;
    align-items: center;
}
.header-inner {
    display: flex;
    align-items: center;
    width: 100%;
    max-width: 1200px;
    margin: 0 auto;
    gap: 2rem;
}
.logo {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--gray-900);
    letter-spacing: .5px;
}
.logo:hover { text-decoration: none; }
.nav { display: flex; gap: 1.25rem; }
.nav a {
    color: var(--gray-500);
    font-size: .875rem;
    padding: .25rem 0;
    border-bottom: 2px solid transparent;
    transition: all .15s;
}
.nav a:hover { color: var(--gray-900); text-decoration: none; border-bottom-color: var(--primary); }
.user-menu {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: .5rem;
    font-size: .8rem;
}
.user-name { color: var(--gray-700); font-weight: 500; }

/* Main */
.main {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1.5rem;
}
.container { width: 100%; }

/* Page header */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}
.page-header h1 { font-size: 1.5rem; font-weight: 600; }

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: .375rem;
    padding: .5rem 1rem;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    background: #fff;
    color: var(--gray-700);
    font-size: .875rem;
    cursor: pointer;
    transition: all .15s;
    text-decoration: none;
}
.btn:hover { background: var(--gray-50); text-decoration: none; }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover { opacity: .9; }
.btn-secondary { background: var(--gray-100); border-color: var(--gray-300); }
.btn-secondary:hover { background: var(--gray-200); }
.btn-success { background: var(--success); color: #fff; border-color: var(--success); }
.btn-success:hover { opacity: .9; }
.btn-sm { padding: .25rem .625rem; font-size: .8rem; }
.btn-lg { padding: .625rem 1.5rem; font-size: 1rem; }

/* Tables */
.table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    overflow: hidden;
}
.table th, .table td {
    padding: .625rem .75rem;
    text-align: left;
    border-bottom: 1px solid var(--gray-200);
    font-size: .875rem;
}
.table th {
    background: var(--gray-50);
    font-weight: 600;
    color: var(--gray-500);
    font-size: .75rem;
    text-transform: uppercase;
    letter-spacing: .05em;
}
.table tbody tr:hover { background: var(--gray-50); }
.text-right { text-align: right; }
.text-center { text-align: center; }

/* Badges */
.badge {
    display: inline-block;
    padding: .125rem .5rem;
    border-radius: 9999px;
    font-size: .75rem;
    font-weight: 500;
}
.badge-success { background: #dcfce7; color: var(--success); }
.badge-warning { background: #fef3c7; color: var(--warning); }
.badge-danger { background: #fee2e2; color: var(--danger); }
.badge-info { background: #dbeafe; color: var(--primary); }
.badge-secondary { background: var(--gray-100); color: var(--gray-700); }
.badge-dict { background: #dbeafe; color: var(--primary); }

/* Status select in invoice list */
.status-select {
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    padding: .125rem .375rem;
    font-size: .75rem;
    font-weight: 500;
    cursor: pointer;
    background: var(--gray-50);
}
.status-select:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.status-warning { background: #fef3c7; color: var(--warning); border-color: var(--warning); }
.status-success { background: #dcfce7; color: var(--success); border-color: var(--success); }
.status-danger { background: #fee2e2; color: var(--danger); border-color: var(--danger); }
.status-info { background: #dbeafe; color: var(--primary); border-color: var(--primary); }

/* Tax adjustment bar */
.tax-adjust-bar {
    background: #fffbeb;
    border: 1px solid #fbbf24;
    border-radius: var(--radius);
    padding: .5rem .75rem;
}
.tax-adjust-row {
    display: flex;
    align-items: center;
    gap: .75rem;
    flex-wrap: wrap;
    font-size: .85rem;
}
.tax-adjust-input { display: flex; align-items: center; gap: .375rem; }
.tax-adjust-input input {
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    padding: .25rem .5rem;
    font-size: .85rem;
}
.badge-new { background: #fef3c7; color: var(--warning); }

/* Parse status */
.parse-status {
    padding: .5rem .75rem;
    border-radius: var(--radius);
    font-size: .85rem;
    margin-top: .5rem;
    display: flex;
    align-items: center;
    gap: .5rem;
}
.parse-status.status-loading {
    background: #eff6ff;
    color: var(--primary);
    border: 1px solid #bfdbfe;
}
.parse-status.status-info {
    background: #f0f9ff;
    color: #0369a1;
    border: 1px solid #bae6fd;
}
.parse-status.status-success {
    background: #dcfce7;
    color: var(--success);
    border: 1px solid #bbf7d0;
}
.parse-status.status-error {
    background: #fef2f2;
    color: var(--danger);
    border: 1px solid #fecaca;
}
.parse-status .status-spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin .6s linear infinite;
}

/* Loading button */
.btn-loading {
    position: relative;
    color: transparent !important;
    pointer-events: none;
}
.btn-loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 16px;
    height: 16px;
    margin: -8px 0 0 -8px;
    border: 2px solid #fff;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin .6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Alerts */
.alert {
    padding: .75rem 1rem;
    border-radius: var(--radius);
    margin-bottom: 1rem;
    font-size: .875rem;
}
.alert-success { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
.alert-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }

/* Forms */
.form-group { margin-bottom: 1rem; }
.form-group label {
    display: block;
    font-size: .875rem;
    font-weight: 500;
    margin-bottom: .25rem;
    color: var(--gray-700);
}
.form-control {
    width: 100%;
    padding: .5rem .75rem;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: .875rem;
    font-family: inherit;
    transition: border-color .15s;
}
.form-control:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.1); }
select.form-control { appearance: auto; }
textarea.form-control { resize: vertical; min-height: 120px; }

.form-row { display: flex; gap: 1rem; }
.form-row .form-group { flex: 1; }

/* Cards */
.card {
    background: #fff;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 1.5rem;
}
.card-header {
    padding: .75rem 1rem;
    border-bottom: 1px solid var(--gray-200);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--gray-50);
}
.card-header h2, .card-header h3 {
    font-size: 1rem;
    font-weight: 600;
}
.card-body { padding: 1rem; }

/* Empty state */
.empty-state {
    text-align: center;
    padding: 3rem;
    background: #fff;
    border-radius: var(--radius);
    border: 1px solid var(--gray-200);
}
.empty-state p { color: var(--gray-500); margin-bottom: 1rem; }

/* Actions */
.actions { display: flex; gap: .375rem; flex-wrap: wrap; }

/* Footer */
.footer {
    text-align: center;
    padding: 1.5rem;
    color: var(--gray-500);
    font-size: .75rem;
    border-top: 1px solid var(--gray-200);
    margin-top: 2rem;
}

/* Invoice detail */
.invoice-summary {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}
.summary-item { }
.summary-label { font-size: .75rem; color: var(--gray-500); text-transform: uppercase; letter-spacing: .05em; }
.summary-value { font-size: 1.1rem; font-weight: 600; margin-top: .125rem; }

/* Receipt input area */
.receipt-area {
    border: 2px dashed var(--gray-300);
    border-radius: var(--radius);
    padding: 1rem;
    min-height: 200px;
    position: relative;
    transition: border-color .15s;
}
.receipt-area:focus-within { border-color: var(--primary); }
.receipt-area textarea {
    width: 100%;
    min-height: 180px;
    border: none;
    outline: none;
    resize: vertical;
    font-family: monospace;
    font-size: .875rem;
    line-height: 1.5;
}
.receipt-area .hint {
    color: var(--gray-500);
    font-size: .75rem;
    margin-top: .5rem;
}

/* Items table editable */
.items-table input {
    border: 1px solid transparent;
    padding: .25rem .375rem;
    font-size: .8rem;
    border-radius: 3px;
    width: 100%;
    font-family: inherit;
}
.items-table input:focus {
    border-color: var(--primary);
    outline: none;
}
.items-table input[type="number"] { width: 70px; text-align: right; }
.items-table .item-total { font-weight: 600; white-space: nowrap; }

/* Package form */
.package-row {
    display: flex;
    gap: .75rem;
    align-items: center;
    margin-bottom: .5rem;
}
.package-row input { width: 80px; }
.package-row .pkg-label { font-size: .8rem; color: var(--gray-500); width: 20px; }

/* Preview area */
.preview-items {
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    padding: .75rem;
    margin-top: .5rem;
    max-height: 300px;
    overflow-y: auto;
}

/* Totals */
.totals-section {
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    padding: 1rem;
    margin-top: 1rem;
}
.totals-row {
    display: flex;
    justify-content: space-between;
    padding: .375rem 0;
    font-size: .875rem;
}
.totals-row.total-line {
    border-top: 2px solid var(--gray-900);
    margin-top: .5rem;
    padding-top: .75rem;
    font-size: 1.1rem;
    font-weight: 700;
}

/* Utilities */
.mt-1 { margin-top: .5rem; }
.mt-2 { margin-top: 1rem; }
.mt-3 { margin-top: 1.5rem; }
.mb-1 { margin-bottom: .5rem; }
.mb-2 { margin-bottom: 1rem; }
.gap-1 { gap: .5rem; }
.flex { display: flex; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.hidden { display: none; }

/* Loading spinner */
.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid var(--gray-300);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin .6s linear infinite;
}

/* Receipt group divider */
.receipt-group-divider {
    display: flex;
    align-items: center;
    width: 100%;
    margin: 8px 0;
    padding: 4px 0;
}
.receipt-group-divider span {
    display: block;
    width: 100%;
    text-align: center;
    font-size: .75rem;
    font-weight: 600;
    color: var(--warning);
    border-top: 2px dashed var(--warning);
    padding-top: 4px;
}

/* Receipt group badge on thumbnails */
.receipt-group-badge {
    position: absolute;
    top: 2px;
    left: 4px;
    background: var(--primary);
    color: #fff;
    font-size: 9px;
    font-weight: 700;
    padding: 1px 4px;
    border-radius: 3px;
    line-height: 1.2;
}

/* Batch review panel */
.batch-review-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: .5rem .75rem;
    background: #eff6ff;
    border: 1px solid #bfdbfe;
    border-radius: var(--radius);
    font-size: .9rem;
    font-weight: 600;
    color: var(--primary);
    margin-bottom: .5rem;
}
.batch-review-total {
    font-size: .8rem;
    font-weight: 500;
    color: var(--gray-700);
}

/* Split layout: images left, items table right */
.batch-review-body {
    display: flex;
    gap: .75rem;
    max-height: 420px;
}
.batch-review-images-col {
    flex: 0 0 220px;
    overflow-y: auto;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    background: #fff;
    padding: .5rem;
}
.batch-review-images-label {
    font-size: .7rem;
    font-weight: 600;
    color: var(--gray-500);
    text-transform: uppercase;
    margin-bottom: .375rem;
}
.batch-review-images {
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.batch-review-thumb {
    width: 100%;
    border: 1px solid var(--gray-200);
    border-radius: 4px;
    cursor: pointer;
    transition: border-color .15s;
}
.batch-review-thumb:hover {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(37,99,235,.15);
}
.batch-review-table-col {
    flex: 1;
    overflow-y: auto;
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    padding: .5rem;
}
#batchReviewTable table {
    width: 100%;
    border-collapse: collapse;
    font-size: .8rem;
}
#batchReviewTable th,
#batchReviewTable td {
    padding: .375rem .5rem;
    text-align: left;
    border-bottom: 1px solid var(--gray-200);
}
#batchReviewTable th {
    background: var(--gray-100);
    font-size: .7rem;
    text-transform: uppercase;
    color: var(--gray-500);
    position: sticky;
    top: 0;
}

@media (max-width: 768px) {
    .batch-review-body { flex-direction: column; max-height: none; }
    .batch-review-images-col { flex: none; max-height: 200px; }
}

/* Warning button */
.btn-warning { background: var(--warning); color: #fff; border-color: var(--warning); }
.btn-warning:hover { opacity: .9; }

/* === Receipt Gallery === */
.receipt-gallery-strip {
    display: flex;
    gap: .5rem;
    overflow-x: auto;
    padding: .25rem 0;
}
.receipt-gallery-card {
    flex: 0 0 auto;
    width: 140px;
    border: 2px solid var(--gray-200);
    border-radius: var(--radius);
    padding: .375rem;
    cursor: pointer;
    transition: all .15s;
    background: #fff;
    text-align: center;
}
.receipt-gallery-card:hover { border-color: var(--primary); }
.receipt-gallery-card.active { border-color: var(--primary); background: #eff6ff; }
.receipt-gallery-card.receipt-gallery-current { border-color: var(--warning); border-style: dashed; background: #fffbeb; }
.receipt-gallery-card img {
    width: 100%;
    max-height: 80px;
    object-fit: cover;
    border-radius: 3px;
    margin-bottom: .25rem;
}
.receipt-gallery-card .rg-label {
    font-size: .75rem;
    font-weight: 600;
    color: var(--gray-700);
}
.receipt-gallery-card .rg-info {
    font-size: .65rem;
    color: var(--gray-500);
}

/* Receipt detail panel */
.receipt-detail-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: .5rem .75rem;
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    font-weight: 600;
    font-size: .9rem;
    margin-bottom: .5rem;
}
.receipt-detail-body {
    display: flex;
    gap: .75rem;
    max-height: 450px;
}
.receipt-detail-images {
    flex: 0 0 240px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.receipt-detail-images img {
    width: 100%;
    border: 1px solid var(--gray-200);
    border-radius: 4px;
    cursor: pointer;
}
.receipt-detail-images img:hover {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(37,99,235,.15);
}
.receipt-detail-items {
    flex: 1;
    overflow-y: auto;
    font-size: .8rem;
}
.receipt-detail-items table {
    width: 100%;
    border-collapse: collapse;
}
.receipt-detail-items th,
.receipt-detail-items td {
    padding: .375rem .5rem;
    text-align: left;
    border-bottom: 1px solid var(--gray-200);
}
.receipt-detail-items th {
    background: var(--gray-100);
    font-size: .7rem;
    text-transform: uppercase;
    color: var(--gray-500);
    position: sticky;
    top: 0;
}
@media (max-width: 768px) {
    .receipt-detail-body { flex-direction: column; max-height: none; }
    .receipt-detail-images { flex: none; max-height: 200px; flex-direction: row; overflow-x: auto; }
    .receipt-detail-images img { width: auto; max-height: 160px; }
}

/* Responsive */
@media (max-width: 768px) {
    .form-row { flex-direction: column; }
    .invoice-summary { grid-template-columns: 1fr; }
    .header-inner { gap: 1rem; }
    .nav { gap: .75rem; }
}
