/* --- Base and Theme Variables (Bright Green Theme) --- */
:root {
    --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --c-bg: #F7FFF9; /* Brighter mint green */
    --c-surface: #ffffff;
    --c-text: #3A5A40; /* Softer dark green */
    --c-text-muted: #829E82; /* Muted green */
    --c-text-disabled: #B8C8B8; /* Lighter disabled color */
    --c-primary: #65A081; /* Brighter green */
    --c-primary-text: #ffffff;
    --c-accent: #88C0A4; /* Even brighter mint green */
    --c-accent-text: #ffffff;
    --c-danger: #E57373; /* Brighter red */
    --c-border: #E8F0E8; /* Lighter green border */
    --c-swipe-blue: rgba(101, 160, 129, 0.2);  /* Primary color with alpha */
    --c-swipe-green: rgba(136, 192, 164, 0.25); /* Accent color with alpha */
    --c-swipe-orange: rgba(210, 180, 140, 0.3); /* Muted tan */
    --shadow: 0 2px 8px rgba(58, 90, 64, 0.08);
    --shadow-strong: 0 4px 12px rgba(58, 90, 64, 0.18);
}

.hidden {
    display: none !important;
}

/* --- General Body Styles --- */
html, body {
  touch-action: manipulation;
}

html { 
    box-sizing: border-box;

}
*, *:before, *:after {
    box-sizing: inherit;
}
body {
    font-family: var(--font-family);
    margin: 0;
    background-color: var(--c-bg);
    color: var(--c-text);
    -webkit-tap-highlight-color: transparent;
    padding-top: 50px; /* 固定タブバーの高さ分 */
}


.tabs {
    position: fixed;
    display: flex;
    top: 0px;
    height: 50px;
    width: 100%;
    border-bottom: 1px solid var(--c-border);
    background-color: var(--c-bg);
    z-index: 1001;
}
.tab-link {
    flex: 1;
    padding: 12px 10px;
    cursor: pointer;
    background: none;
    border: none;
    color: var(--c-text-muted);
    font-size: 16px;
    font-weight: 500;
    text-align: center;
    border-bottom: 3px solid transparent;
    transition: color 0.2s, border-color 0.2s;
}
.tab-link.active {
    color: var(--c-primary);
    border-bottom-color: var(--c-primary);
}
.tab-link[data-tab="byCostco"] span {
    padding-left: 37px;
    background-image: url('/static/icons/costco_icon.webp');
    background-repeat: no-repeat;
    background-size: 36px 36px;
    background-position: left center;
    display: inline-block;
    vertical-align: middle;
}

/* カード表示領域 */
.main-container {
    padding: 8px 16px 50px;
    max-width: 800px;
    margin: 0 auto;
}




/* --- List Controls --- */
.item-list {
    display: flex;
    flex-direction: column;
    margin-top: 0px;
    margin-bottom: 12px;
    gap: 12px;
    padding: 0;
    min-height: 38px;
}

/* --- Item List & Card --- */
.item-list { 
    list-style: none; 
}

.item-card {
    background-color: var(--c-surface);
    border-radius: 12px;
    box-shadow: var(--shadow);
    position: relative;
    user-select: none;
    overflow: hidden;
    transition: transform 0.2s ease-out, box-shadow 0.2s ease-out;
    border: 1px solid var(--c-border);
}

.index-label {
    margin: 0 12px;
    color: var(--c-text-muted);
    scroll-margin-top: 104px; /* 固定ヘッダー(タブ50px + タグフィルター約48px)分のオフセット */
}

.swiping-active {
    transform: scale(0.98);
    box-shadow: var(--shadow-strong);
}

.item-card-content {
    display: flex;
    align-items: center;
    background-color: var(--c-surface);
    z-index: 1;
    position: relative;
    padding: 16px;
}

.item-card-image {
    width: 75px;
    height: 75px;
    border-radius: 5px;
    background-size: cover;
    background-position: center;
    background-color: var(--c-bg);
    margin-right: 16px;
    flex-shrink: 0;
    cursor: pointer;
}
.item-card-details { flex-grow: 1; min-width: 0; }

/* --- Item Card Tags --- */
.item-card-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}

.tag {
    padding: 3px 8px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 500;
    color: var(--c-primary-text);
}

/* --- Tag Colors (Bright Green Palette) --- */
.tag-color-0 { background-color: #3A5A40; } /* Deepest Green */
.tag-color-1 { background-color: #588157; }
.tag-color-2 { background-color: #7BA488; }
.tag-color-3 { background-color: #A3C1AD; }
.tag-color-4 { background-color: #D1E2D6; } /* Lightest Green */

.tag-color-5 { background-color: #8A7D6C; } /* Muted Brown */
.tag-color-6 { background-color: #A89988; }
.tag-color-7 { background-color: #C5B6A4; }
.tag-color-8 { background-color: #E3D4C1; }
.tag-color-9 { background-color: #F5F0E9; } /* Lightest Brown */

/* Adjust text color for light backgrounds */
.tag-color-3, .tag-color-4, .tag-color-7, .tag-color-8, .tag-color-9 {
    color: var(--c-text);
}

.item-card-name {
    font-size: 17px;
    font-weight: 500;
    margin: 0 0 6px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.item-card-date {
    font-size: 13px;
    color: var(--c-text-muted);
    margin: 0;
}

/* --- Swipe Background --- */
.swipe-background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    z-index: 0;
    background-color: transparent;
    transition: background-color 0.1s ease-in-out;
}
.swipe-icon {
    font-size: 28px;
    color: var(--c-text);
    opacity: 0;
    transition: opacity 0.1s ease-in-out;
}
.swipe-icon.visible { opacity: 1; }

.swipe-background.bg-inventory { background-color: var(--c-swipe-blue); }
.swipe-background.bg-shopping { background-color: var(--c-swipe-green); }
.swipe-background.bg-costco { background-color: var(--c-swipe-orange); }

.swipe-icon.costco-icon {
    background-image: url('/static/icons/costco_icon.webp');
    background-repeat: no-repeat;
    background-size: contain;
    background-position: center;
    width: 35px;
    height: 35px;
    color: transparent;
}

/* アイテム追加ボタン */
.btn-item-add {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 48px;
    height: 48px;
    border: none;
    border-radius: 24px;
    background-color: var(--c-primary);
    color: var(--c-primary-text);
    font-size: 28px;
    font-weight: 300;
    text-align: center;
    box-shadow: var(--shadow-strong);
    cursor: pointer;
    z-index: 1500;
}

/* アクションボタンの共通スタイル */
.btn-sort {
    border: none;
    border-radius: 24px;
    background-color: var(--c-primary);
    color: var(--c-primary-text);
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-strong);
    cursor: pointer;
    font-weight: bold;
    height: 48px;
    padding: 0 16px;
    white-space: nowrap;
}

#bottom-action-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 16px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    z-index: 1400;
    transition: background-color 0.3s ease;
    pointer-events: none;
}



#bottom-action-bar.is-active {
    background-color: rgba(232, 240, 232, 0.95);
    justify-content: center;
    pointer-events: auto;
    gap: 12px;
}

/* is-active状態のボタンは幅を均等にする */
#bottom-action-bar.is-active .btn-sort {
    flex: 1;
    max-width: 180px;
}

#deselect-all-btn {
    background-color: var(--c-text-muted);
    color: var(--c-primary-text);
}

/* ボタン自体はクリックできるように */
#bottom-action-bar > .btn-sort {
    pointer-events: auto;
}

#move-to-store-btn {
    background-color: var(--c-accent);
    color: var(--c-accent-text);
}

#move-to-costco-btn {
    background-color: #D2B48C; /* Muted tan */
    color: var(--c-text);
}

.btn-not-bought {
    background-color: #C8D8C8; /* Lighter muted green */
    color: var(--c-text);
}

.btn-bought {
    background-color: var(--c-primary);
    color: var(--c-primary-text);
}

.btn-settings-icon {
    position: relative; /* For pseudo-element positioning */
    width: 48px;
    height: 48px;
    padding: 0;
}

.btn-settings-icon::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-image: url('/static/icons/haguruma.png');
    background-size: 32px 32px;
    background-position: center;
    background-repeat: no-repeat;
    filter: brightness(0) invert(1); /* Turn black icon to white */
}

/* モーダル */
.modal {
    display: none;
    position: fixed;
    z-index: 2000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.6);
    align-items: flex-start;
    justify-content: center;
    overflow: auto;
}

.modal.show {
    display: flex;
}

.modal-dialog {
    background-color: var(--c-surface);
    padding: 20px;
    border-radius: 12px; /* Softer corners */
    box-shadow: var(--shadow-strong);
    width: 90%;
    max-width: 350px;
    position: relative;
    margin-top: 3vh;
}

.btn-delete-text {
    background: none;
    border: none;
    color: var(--c-danger);
    font-size: 16px;
    cursor: pointer;
    margin-left: auto;
    margin-bottom: 20px;
    display: block;
}

#item-form { 
    display: flex;
    flex-direction: column;
    align-items: center;
}

.image-preview-container {
    width: 200px;
    height: 200px;
    border-radius: 8px;
    margin-bottom: 20px;
    background-color: var(--c-bg);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    border: 1px solid var(--c-border);
}

#item-image-preview {
    width: 100%;
    height: 100%;
    object-fit: contain;
}


.form-input {
    width: 100%;
    padding: 12px;
    border-radius: 8px;
    border: 1px solid var(--c-border);
    background-color: var(--c-bg);
    color: var(--c-text);
    font-size: 16px;
    margin-bottom: 20px;
}
.modal-form-actions {
    display: flex;
    gap: 12px;
    width: 100%;
}
.btn-cancel, .btn-submit {
    flex: 1;
    padding: 12px;
    border-radius: 8px;
    border: none;
    font-weight: bold;
    font-size: 16px;
    cursor: pointer;
}
.btn-cancel {
    background-color: var(--c-text-muted);
    color: var(--c-primary-text);
}
.btn-submit {
    background-color: var(--c-accent);
    color: var(--c-accent-text);
}

/* --- Tag Editor in Modal --- */
.tag-editor-container {
    width: 100%;
    margin-bottom: 20px;
}

.tag-input-area {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    width: 100%;
    padding: 8px;
    border-radius: 8px;
    background-color: var(--c-bg);
    border: 1px solid var(--c-border);
    transition: border-color 0.2s;
    min-height: 40px; /* Set a minimum height */
    align-content: flex-start; /* Fix wrapping issue */
}

.tag-input-area:focus-within {
    border-color: var(--c-primary);
}

/* 選択されたタグのスタイルをカード表示と統一 */
.tag-input-area .tag {
    cursor: pointer; /* タップで削除できるように */
}

/* プレースホルダーの表示制御 */
.tag-input-area.is-empty::before {
    content: "タグを選択";
    color: var(--c-text-muted);
    position: absolute;
}

.tag-item .remove-tag {
    margin-left: 6px;
    cursor: pointer;
    font-weight: bold;
}

.tag-input {
    flex-grow: 1;
    border: none;
    background: none;
    outline: none;
    padding: 4px;
    font-size: 16px;
    min-width: 100px;
}

.tag-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 12px;
}

.tag-suggestion-item {
    padding: 6px 12px;
    border-radius: 16px;
    background-color: var(--c-surface);
    border: 1px solid var(--c-border);
    color: var(--c-text);
    cursor: pointer;
    transition: background-color 0.2s, color 0.2s;
    font-size: 14px;
}

.tag-suggestion-item.selected {
    background-color: var(--c-accent);
    color: var(--c-accent-text);
    border-color: var(--c-accent);
}

/* --- Tag Color Overrides --- */
.tag-item.tag-food, .tag-suggestion-item.tag-food { background-color: #005f73; }
.tag-item.tag-daily, .tag-suggestion-item.tag-daily { background-color: #0a9396; }
.tag-item.tag-other, .tag-suggestion-item.tag-other { background-color: #94d2bd; }

.tag-item.tag-food, .tag-item.tag-daily, .tag-item.tag-other,
.tag-suggestion-item.tag-food, .tag-suggestion-item.tag-daily, .tag-suggestion-item.tag-other {
    color: var(--c-primary-text);
    border-color: transparent;
}


#delete-confirm-modal {
    text-align: center;
}

#delete-confirm-modal .modal-dialog {
    margin-top: 18vh;
    text-align: center;
}

#delete-confirm-message {
    margin-top: 20px;
    margin-right: 0;
    margin-bottom: 50px;
    margin-left: 0;
}
.btn-danger {
    padding: 10px 20px;
    border-radius: 8px;
    border: none;
    font-weight: bold;
    font-size: 16px;
    cursor: pointer;
    background-color: var(--c-danger);
    color: var(--c-primary-text);
}

/* --- Login Page Styles --- */
.login-page {
    display: flex;
    align-items: flex-start; /* 上寄せに変更 */
    justify-content: center;
    height: 100vh;
    padding-top: 5vh; /* 上部に余白を追加 */
}
.login-container {
    width: 90%;
    max-width: 400px;
    padding: 32px;
    background-color: var(--c-surface);
    border-radius: 12px;
    box-shadow: var(--shadow-strong);
}
.login-container h1 {
    text-align: center;
    margin-top: 0;
    margin-bottom: 24px;
}
.form-group {
    margin-bottom: 20px;
}
.form-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: bold;
}
.form-group-inline {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 24px;
}
.btn-submit-full {
    width: 100%;
    padding: 12px;
    border-radius: 8px;
    border: none;
    font-weight: bold;
    font-size: 16px;
    cursor: pointer;
    background-color: var(--c-primary);
    color: var(--c-primary-text);
}

/* --- Index Navigation --- */
.index-nav {
    position: fixed;
    top: 50%;
    right: 4px;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 1000;
    background-color: rgba(232, 240, 232, 0.9);
    backdrop-filter: blur(4px);
    box-shadow: var(--shadow);
    display: none; /* Initially hidden by JS */
    padding: 10px 6px;
    border-radius: 12px;
}

.index-nav a {
    text-decoration: none;
    color: var(--c-text);
    background-color: transparent;
    font-size: 14px;
    font-weight: bold;
    padding: 2px 4px;
    border-radius: 4px;
    /* The transition will now handle both the fade-in and fade-out */
    transition: background-color 0.3s ease-out, color 0.3s ease-out;
    text-shadow: none;
}

.index-nav a.disabled {
    color: var(--c-text-disabled);
    pointer-events: none;
}

.index-nav a:hover {
    background-color: transparent;
    color: var(--c-text);
}

/* Style for the moment it's tapped */
.index-nav a.is-tapped {
    background-color: var(--c-primary); /* Use a solid, visible color */
    /* The transition back to transparent is handled by the base 'a' style */
}

/* --- Tag Filter Bar --- */
.tag-filter-bar {
    display: none; /* JSで表示制御 */
    padding: 8px 16px; /* 上下のpaddingを調整 */
    background-color: var(--c-bg);
    display: flex;
    flex-wrap: nowrap; /* 折り返しを無効化 */
    gap: 8px;
    position: sticky;
    top: 50px; /* タブ(50px)のすぐ下 */
    z-index: 999;
    overflow-x: auto; /* 横スクロールを有効化 */
    -ms-overflow-style: none;  /* IE, Edge */
    scrollbar-width: none;  /* Firefox */
}

/* Webkit系ブラウザのスクロールバーを非表示に */
.tag-filter-bar::-webkit-scrollbar {
    display: none;
}

.filter-tag-btn {
    padding: 4px 10px; /* サイズをタグに近づける */
    border-radius: 6px; /* 角丸をタグに合わせる */
    border: 1px solid var(--c-border);
    background-color: var(--c-surface);
    color: var(--c-text);
    font-size: 13px; /* フォントサイズを少し小さく */
    font-weight: 500;
    cursor: pointer;
    transition: background-color 0.2s, color 0.2s, border-color 0.2s;
    white-space: nowrap; /* 文字列の改行を禁止 */
}

/* アクティブ時は文字色を白に */
.filter-tag-btn.active {
    color: var(--c-primary-text); 
    border-color: transparent; /* ボーダーを非表示に */
}

/* 「すべて」ボタンのアクティブ時のスタイル */
.filter-tag-btn[data-tag=""].active {
    background-color: var(--c-primary);
    color: var(--c-primary-text);
    border-color: var(--c-primary);
}

/* アクティブなタグフィルターボタンの色を上書き */
.filter-tag-btn.active.tag-color-0 { background-color: #3A5A40 !important; }
.filter-tag-btn.active.tag-color-1 { background-color: #588157 !important; }
.filter-tag-btn.active.tag-color-2 { background-color: #7BA488 !important; }
.filter-tag-btn.active.tag-color-3 { background-color: #A3C1AD !important; }
.filter-tag-btn.active.tag-color-4 { background-color: #D1E2D6 !important; }
.filter-tag-btn.active.tag-color-5 { background-color: #8A7D6C !important; }
.filter-tag-btn.active.tag-color-6 { background-color: #A89988 !important; }
.filter-tag-btn.active.tag-color-7 { background-color: #C5B6A4 !important; }
.filter-tag-btn.active.tag-color-8 { background-color: #E3D4C1 !important; }
.filter-tag-btn.active.tag-color-9 { background-color: #F5F0E9 !important; }

/* 明るい背景のアクティブボタンの文字色を調整 */
.filter-tag-btn.active.tag-color-3,
.filter-tag-btn.active.tag-color-4,
.filter-tag-btn.active.tag-color-7,
.filter-tag-btn.active.tag-color-8,
.filter-tag-btn.active.tag-color-9 {
    color: var(--c-text) !important;
}


/* --- Card Selection Styles --- */
.item-card .checkmark {
    display: none; /* Hide by default */
    position: absolute;
    top: 5px;
    left: 5px;
    width: 24px; /* Set width for the icon */
    height: 24px; /* Set height for the icon */
    background-image: url('/static/icons/checkmark.png');
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
}

.item-card.selected .item-card-content {
    background-color: #F0F5F0; /* Light green background for selected card */
}

.item-card.selected .checkmark {
    display: block; /* Show checkmark when selected */
}

/* --- Settings Page --- */
.settings-page-container {
    background-color: var(--c-bg);
    min-height: 100vh;
}

.settings-header {
    display: flex;
    align-items: center;
    padding: 0 16px;
    height: 50px;
    border-bottom: 1px solid var(--c-border);
    background-color: var(--c-surface);
}

.settings-header h1 {
    font-size: 18px;
    margin: 0;
    text-align: center;
    flex-grow: 1;
}

.back-button {
    font-size: 28px;
    font-weight: bold;
    color: var(--c-primary);
    text-decoration: none;
    padding-right: 16px; /* Increase tappable area */
}

.settings-main {
    padding-top: 16px;
}

.settings-list {
    list-style: none;
    padding: 0;
    margin: 0;
    background-color: var(--c-surface);
    border-top: 1px solid var(--c-border);
    border-bottom: 1px solid var(--c-border);
}

.settings-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
}

.settings-item:not(:last-child) {
    border-bottom: 1px solid var(--c-border);
}

.settings-item label {
    font-size: 16px;
}

.settings-select {
    font-size: 16px;
    padding: 4px 8px;
    border-radius: 6px;
    border: 1px solid var(--c-border);
}

.settings-button-danger {
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: 8px;
    background-color: var(--c-danger);
    color: var(--c-primary-text);
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
}

.settings-button-logout {
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: 8px;
    background-color: var(--c-text-muted);
    color: var(--c-primary-text);
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
}

/* --- Tag Management Page --- */
.tag-management-container {
    padding: 16px;
}

.tag-add-form {
    display: flex;
    gap: 8px;
    margin-bottom: 24px;
}

.tag-add-form .form-input {
    flex-grow: 1;
    margin-bottom: 0;
    min-width: 0; /* Prevent the input from growing too large */
}

.tag-add-form .btn-submit {
    flex-shrink: 0;
    padding: 12px 24px; /* Increase padding */
    white-space: nowrap; /* Prevent text wrapping */
}

.btn-delete-tag {
    background: none;
    border: 1px solid var(--c-danger);
    color: var(--c-danger);
    border-radius: 8px; /* Increase border-radius */
    padding: 8px 12px; /* Increase padding */
    font-size: 14px;
    cursor: pointer;
}

.tag-actions {
    display: flex;
    align-items: center;
    gap: 12px; /* Increase gap */
}

.btn-reorder {
    background: none;
    border: 1px solid var(--c-border);
    color: var(--c-text);
    border-radius: 8px; /* Increase border-radius */
    padding: 6px 10px; /* Increase padding */
    font-size: 20px; /* Increase font size */
    cursor: pointer;
    line-height: 1;
    width: 40px; /* Set a fixed width */
    height: 40px; /* Set a fixed height */
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-reorder:disabled {
    visibility: hidden; /* Hide disabled buttons */
}

#new-tag-name {
    background-color: var(--c-surface);
    border: 1px solid var(--c-border);
}

.tag-list-item {
    padding: 8px 16px;
    font-size: 16px;
    border-radius: 8px;
    cursor: pointer; /* Add this */
}
