:root {
    --bg-color: #0f172a;
    --card-bg: rgba(30, 41, 59, 0.7);
    --accent-primary: #818cf8;
    --accent-secondary: #c084fc;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --glass-border: rgba(255, 255, 255, 0.1);
    --shadow-lg: 0 10px 25px -5px rgba(0, 0, 0, 0.3), 0 8px 10px -6px rgba(0, 0, 0, 0.3);
    --font-heading: 'Outfit', sans-serif;
    --font-body: 'Inter', sans-serif;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background: radial-gradient(circle at top left, #1e1b4b, #0f172a);
    color: var(--text-main);
    font-family: var(--font-body);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 1rem;
}

.app-container {
    width: 100%;
    max-width: 550px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding-bottom: 2rem;
}

/* Tab Navigation */
.tab-nav {
    display: flex;
    background: var(--card-bg);
    backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 1rem;
    padding: 0.5rem;
    gap: 0.5rem;
    box-shadow: var(--shadow-lg);
}

.tab-btn {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: 0.75rem;
    border-radius: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
}

.tab-btn.active {
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    color: white;
}

/* Tab Content Switching */
.tab-content {
    display: none;
    flex-direction: column;
    gap: 1.5rem;
    animation: fadeIn 0.4s ease-out;
}

.tab-content.active {
    display: flex;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

header {
    text-align: center;
    padding: 2rem 1.5rem;
    background: var(--card-bg);
    backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 2rem;
    box-shadow: var(--shadow-lg);
}

.date-selector-container {
    margin-bottom: 1rem;
}

.date-picker {
    background: rgba(15, 23, 42, 0.4);
    border: 1px solid var(--glass-border);
    color: white;
    padding: 0.5rem 1rem;
    border-radius: 2rem;
    font-family: var(--font-heading);
    cursor: pointer;
    outline: none;
}

.total-container {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.currency {
    font-size: 1.5rem;
    font-weight: 400;
    color: var(--accent-primary);
}

#daily-total {
    font-family: var(--font-heading);
    font-size: 3.5rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

#display-date-label {
    margin-top: 0.5rem;
    font-size: 0.875rem;
    color: var(--text-muted);
}

.input-section {
    background: var(--card-bg);
    backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 1.5rem;
    padding: 1.5rem;
    box-shadow: var(--shadow-lg);
}

#expense-form, #budget-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.input-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.input-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.input-group label {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--text-muted);
}

input, select {
    width: 100%; /* 確保寬度不會超出 */
    background: rgba(15, 23, 42, 0.5);
    border: 1px solid var(--glass-border);
    border-radius: 0.75rem;
    padding: 0.75rem 1rem;
    color: white;
    font-size: 0.9375rem;
    transition: border-color 0.3s, box-shadow 0.3s;
    appearance: none;
    box-sizing: border-box; /* 包含 padding 於寬度內 */
}

/* 隱藏數字欄位的上下微調按鈕 */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}
input[type=number] {
    -moz-appearance: textfield;
}

select {
    cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='white'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 1rem center;
    background-size: 1rem;
    padding-right: 2.5rem;
}

input:focus, select:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 2px rgba(129, 140, 248, 0.2);
}

/* 唯讀/停用欄位的樣式 */
input:disabled, select:disabled {
    background-color: rgba(0, 0, 0, 0.05);
    color: #94a3b8;
    cursor: not-allowed;
    border-color: #e2e8f0;
}

select option {
    background-color: var(--bg-color);
    color: white;
}

button {
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    color: white;
    border: none;
    border-radius: 0.75rem;
    padding: 0.875rem;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: transform 0.2s, opacity 0.2s;
}

button:hover {
    transform: translateY(-2px);
    opacity: 0.9;
}

.list-section {
    background: var(--card-bg);
    backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 1.5rem;
    padding: 1.5rem;
    box-shadow: var(--shadow-lg);
}

h2 {
    font-family: var(--font-heading);
    font-size: 1.125rem;
    margin-bottom: 1rem;
    color: var(--text-muted);
}

.expense-list, .budget-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.expense-item, .budget-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(15, 23, 42, 0.3);
    padding: 0.875rem 1rem;
    border-radius: 1rem;
    border: 1px solid transparent;
    transition: all 0.3s ease;
    cursor: default;
    gap: 1rem;
}

/* 支出明細單行顯示 */
.expense-item {
    flex-wrap: nowrap;
}

.expense-item .item-info {
    flex-direction: row;
    align-items: center;
    gap: 1rem;
    overflow: hidden;
    flex: 1;
}

.expense-item .item-subject {
    white-space: nowrap;
    flex-shrink: 0;
    max-width: 150px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.expense-item .item-desc {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

.expense-item .actions {
    flex-shrink: 0;
}

/* 預算階層顯示 */
.budget-item {
    cursor: grab;
    position: relative;
}

.budget-item.level-1 { margin-left: 1.5rem; }
.budget-item.level-2 { margin-left: 3rem; }
.budget-item.level-3 { margin-left: 4.5rem; }

.budget-item.is-parent .budget-val {
    color: var(--text-muted);
    font-style: italic;
}

.budget-item.is-parent input[type="number"] {
    background: rgba(255, 255, 255, 0.05);
    cursor: not-allowed;
    opacity: 0.6;
}

.budget-item.drop-before {
    box-shadow: 0 -4px 0 var(--accent-primary);
}

.budget-item.drop-nest {
    background: rgba(129, 140, 248, 0.25);
    transform: scale(1.01);
}

.budget-item.drop-after {
    box-shadow: 0 4px 0 var(--accent-primary);
}

.toggle-btn {
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-muted);
    transition: transform 0.2s;
    flex-shrink: 0;
}

.toggle-btn.collapsed {
    transform: rotate(-90deg);
}

.item-info {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    flex: 1;
    overflow: hidden;
}

.budget-name-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.item-subject, .budget-name {
    font-weight: 600;
    font-size: 1rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.item-desc-group {
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
}

.item-desc {
    font-size: 0.8125rem;
    color: var(--text-muted);
}

.item-amount, .budget-val {
    font-weight: 700;
    color: var(--accent-primary);
    font-size: 1.125rem;
    margin-right: 0.5rem;
}

.budget-edit-input {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid var(--accent-primary);
    border-radius: 0.5rem;
    padding: 0.25rem 0.5rem;
    color: white;
    font-size: 0.875rem;
    width: 100%;
    margin-bottom: 0.25rem;
}

.budget-edit-input:last-child {
    margin-bottom: 0;
}

.budget-edit-input.formula-edit {
    font-family: monospace;
    color: var(--accent-secondary);
    font-size: 0.8rem;
}

.actions {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.icon-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 0.5rem;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: white;
}

.delete-btn:hover {
    color: #ef4444;
}

/* Modal Styles */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(8px);
    z-index: 1000;
    justify-content: center;
    align-items: center;
    padding: 1rem;
}

.modal.active {
    display: flex;
    animation: fadeIn 0.3s ease;
}

.modal-content {
    width: 100%;
    max-width: 450px;
    padding: 2rem;
    position: relative;
    animation: slideUp 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
    
    /* 增加明顯的邊框與發光效果 */
    border: 2px solid var(--accent-primary);
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5), 0 0 15px rgba(129, 140, 248, 0.3);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

.modal-header h3 {
    font-family: var(--font-heading);
    font-size: 1.5rem;
    background: linear-gradient(135deg, var(--text-main), var(--accent-primary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.close-modal-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.5rem;
    cursor: pointer;
    transition: color 0.2s;
}

.close-modal-btn:hover {
    color: var(--text-main);
}

.modal-actions {
    display: flex;
    gap: 1rem;
    margin-top: 2rem;
}

.modal-actions .primary-btn, 
.modal-actions .secondary-btn {
    flex: 1;
}

.action-section {
    margin-bottom: 1.5rem;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes slideUp {
    from { transform: translateY(20px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

@media (max-width: 480px) {
    body {
        padding: 0.5rem;
    }

    header {
        padding: 1.5rem 1rem;
    }

    #daily-total { 
        font-size: 2.75rem; 
    }

    .input-row {
        flex-direction: column;
        gap: 0.75rem;
    }

    .budget-item.level-1 { margin-left: 0.75rem; }
    .budget-item.level-2 { margin-left: 1.5rem; }
    .budget-item.level-3 { margin-left: 2.25rem; }

    .expense-item .item-subject {
        max-width: 100px;
    }

    .modal-content {
        padding: 1.5rem;
        margin: 0.5rem;
    }
}

@media (max-width: 350px) {
    #daily-total { 
        font-size: 2.25rem; 
    }
    
    .tab-btn {
        padding: 0.6rem;
        font-size: 0.875rem;
    }
}
