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

:root {
    --primary: #ff6b6b;
    --primary-light: #ff8e8e;
    --secondary: #4ecdc4;
    --bg: #f8f9fa;
    --surface: #ffffff;
    --text: #2d3436;
    --text-muted: #636e72;
    --border: #e9ecef;
    --radius: 12px;
    --shadow: 0 2px 12px rgba(0,0,0,0.08);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

/* ===== Header ===== */
header {
    background: var(--surface);
    box-shadow: var(--shadow);
    padding: 0 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 60px;
    position: sticky;
    top: 0;
    z-index: 100;
}

header h1 {
    font-size: 1.2rem;
    color: var(--primary);
    font-weight: 700;
}

nav { display: flex; gap: 4px; }

nav a {
    padding: 6px 14px;
    border-radius: 20px;
    text-decoration: none;
    color: var(--text-muted);
    font-size: 0.9rem;
    transition: all 0.2s;
}

nav a.active {
    background: var(--primary);
    color: white;
}

/* ===== Main ===== */
main {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px 16px;
    display: flex;
    flex-direction: column;
    gap: 24px;
}

section {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: var(--shadow);
}

section h2 {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 16px;
    color: var(--text);
}

/* ===== Category Buttons ===== */
.category-buttons {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.cat-btn {
    padding: 8px 20px;
    border: 2px solid var(--border);
    background: var(--surface);
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: all 0.2s;
    color: var(--text-muted);
}

.cat-btn.active, .cat-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: #fff5f5;
}

/* ===== Dish Grid ===== */
.dish-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 12px;
}

.dish-card {
    border: 2px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 12px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s;
    background: var(--surface);
}

.dish-card:hover, .dish-card.selected {
    border-color: var(--primary);
    background: #fff5f5;
    transform: translateY(-2px);
    box-shadow: var(--shadow);
}

.dish-card .dish-emoji { font-size: 2rem; display: block; margin-bottom: 8px; }
.dish-card .dish-label { font-size: 0.85rem; color: var(--text); font-weight: 500; }
.dish-card .dish-cal { font-size: 0.75rem; color: var(--text-muted); margin-top: 4px; }

/* ===== Result Card ===== */
.result-card {
    display: flex;
    gap: 20px;
    align-items: flex-start;
}

.dish-image {
    width: 200px;
    min-height: 200px;
    border-radius: var(--radius);
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    flex-shrink: 0;
    overflow: hidden;
}

.dish-image img { width: 100%; height: 100%; object-fit: cover; }

.dish-info { flex: 1; }
.dish-info h3 { font-size: 1.2rem; margin-bottom: 8px; }
.dish-info .calories { color: var(--primary); font-weight: 600; margin-bottom: 12px; }

.dish-info h4 { font-size: 0.9rem; color: var(--text-muted); margin: 12px 0 8px; }
.dish-info ul { padding-left: 16px; }
.dish-info li { font-size: 0.9rem; margin-bottom: 4px; color: var(--text); }

/* ===== Today's Order ===== */
.order-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}

.order-item:last-child { border-bottom: none; }
.order-item .order-name { font-weight: 500; }
.order-item .order-cal { font-size: 0.85rem; color: var(--text-muted); }
.order-right { display: flex; align-items: center; gap: 8px; }

.order-delete {
    width: 24px; height: 24px;
    border: none; border-radius: 50%;
    background: transparent;
    color: var(--text-muted);
    font-size: 1.1rem; line-height: 1;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}
.order-delete:hover { background: #ffe0e0; color: var(--primary); }

.order-total { font-weight: 600; border-top: 2px solid var(--border); margin-top: 4px; padding-top: 10px; }

/* ===== Upload Area ===== */
.upload-area {
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    min-height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    overflow: hidden;
    background: #fafafa;
    margin-bottom: 12px;
}

.upload-area:hover, .upload-area.drag-over {
    border-color: var(--primary);
    background: #fff5f5;
}

.upload-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    color: var(--text-muted);
}

.upload-placeholder svg { opacity: 0.4; }
.upload-placeholder p { font-size: 0.9rem; }

#uploadPreview {
    width: 100%;
    max-height: 300px;
    object-fit: contain;
}

.upload-controls {
    display: flex;
    gap: 12px;
    align-items: center;
}

.upload-controls select {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    background: white;
    color: var(--text);
}

/* ===== Analysis Card ===== */
.food-name {
    font-size: 1.3rem;
    font-weight: 700;
    margin-bottom: 16px;
    color: var(--text);
}

.macro-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin-bottom: 16px;
}

.macro-item {
    background: var(--bg);
    border-radius: 10px;
    padding: 12px 8px;
    text-align: center;
}

.macro-value { display: block; font-size: 1.4rem; font-weight: 700; color: var(--text); }
.macro-label { display: block; font-size: 0.72rem; color: var(--text-muted); margin-top: 2px; }
.calories-item .macro-value { color: var(--primary); }

.health-score-container {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 16px;
    font-size: 0.9rem;
}

.health-bar {
    flex: 1;
    height: 8px;
    background: var(--border);
    border-radius: 4px;
    overflow: hidden;
}

.health-fill {
    height: 100%;
    border-radius: 4px;
    background: linear-gradient(90deg, #ff6b6b, #ffd93d, #6bcb77);
    transition: width 0.8s ease;
}

#healthScore { font-weight: 700; color: var(--secondary); width: 36px; text-align: right; }

.suggestions h4 { font-size: 0.9rem; color: var(--text-muted); margin-bottom: 8px; }
.suggestions li { font-size: 0.9rem; margin-bottom: 6px; padding-left: 4px; }

/* ===== History ===== */
.history-tabs { display: flex; gap: 4px; margin-bottom: 16px; }

.tab-btn {
    padding: 6px 16px;
    border: none;
    background: var(--bg);
    border-radius: 20px;
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--text-muted);
    transition: all 0.2s;
}

.tab-btn.active {
    background: var(--secondary);
    color: white;
}

.history-summary {
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
    border-radius: var(--radius);
    padding: 16px;
    color: white;
    margin-bottom: 16px;
    display: flex;
    justify-content: space-around;
    text-align: center;
}

.summary-item .value { font-size: 1.6rem; font-weight: 700; display: block; }
.summary-item .label { font-size: 0.8rem; opacity: 0.85; }

.history-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
}

.history-item:last-child { border-bottom: none; }

.history-item-img {
    width: 56px;
    height: 56px;
    border-radius: 10px;
    object-fit: cover;
    background: var(--border);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.8rem;
}

.history-item-info { flex: 1; }
.history-item-name { font-weight: 600; font-size: 0.95rem; }
.history-item-meta { font-size: 0.8rem; color: var(--text-muted); margin-top: 2px; }
.history-item-cal { font-weight: 600; color: var(--primary); font-size: 0.9rem; }

/* ===== AI Chat ===== */
#ai-chat-section { display: flex; flex-direction: column; gap: 12px; }

.chat-window {
    background: var(--bg);
    border-radius: var(--radius);
    padding: 14px;
    min-height: 120px;
    max-height: 320px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.chat-msg {
    max-width: 82%;
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 0.9rem;
    line-height: 1.5;
    word-break: break-word;
}

.chat-msg.ai {
    background: var(--surface);
    border: 1px solid var(--border);
    align-self: flex-start;
    border-bottom-left-radius: 4px;
}

.chat-msg.user {
    background: var(--primary);
    color: white;
    align-self: flex-end;
    border-bottom-right-radius: 4px;
}

.chat-msg.typing {
    background: var(--surface);
    border: 1px solid var(--border);
    align-self: flex-start;
    border-bottom-left-radius: 4px;
    color: var(--text-muted);
}

.chat-dish-card {
    background: var(--surface);
    border: 2px solid var(--secondary);
    border-radius: var(--radius);
    padding: 14px;
    align-self: flex-start;
    max-width: 90%;
    cursor: pointer;
    transition: all 0.2s;
}

.chat-dish-card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.chat-dish-card .chat-dish-name { font-weight: 700; font-size: 1rem; margin-bottom: 6px; }
.chat-dish-card .chat-dish-desc { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 8px; }
.chat-dish-card .chat-dish-meta { font-size: 0.82rem; color: var(--secondary); font-weight: 600; }
.chat-dish-card .chat-dish-select { font-size: 0.8rem; color: var(--primary); margin-top: 6px; }

.chat-input-row {
    display: flex;
    gap: 8px;
}

.chat-input-row input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.95rem;
    outline: none;
    transition: border-color 0.2s;
}

.chat-input-row input:focus { border-color: var(--primary); }

.chat-input-row button {
    padding: 10px 20px;
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.2s;
}

.chat-input-row button:hover { background: var(--primary-light); }
.chat-input-row button:disabled { background: var(--border); color: var(--text-muted); cursor: not-allowed; }

/* ===== Shared ===== */
.btn-primary {
    background: var(--primary);
    color: white;
    border: none;
    border-radius: var(--radius);
    padding: 12px 24px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    width: 100%;
    margin-top: 16px;
}

.btn-primary:hover { background: var(--primary-light); transform: translateY(-1px); }
.btn-primary:disabled { background: var(--border); color: var(--text-muted); cursor: not-allowed; transform: none; }

.hidden { display: none !important; }
.empty-state { color: var(--text-muted); text-align: center; padding: 20px; font-size: 0.9rem; }

.loading-spinner {
    color: white;
    font-size: 0.9rem;
    animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }

/* ===== Responsive ===== */
@media (max-width: 600px) {
    .result-card { flex-direction: column; }
    .dish-image { width: 100%; min-height: 200px; }
    .macro-grid { grid-template-columns: repeat(2, 1fr); }
    .history-summary { flex-direction: column; gap: 8px; }
}
