/* ========================================
   Diet & Keep Analyzer - Main Stylesheet
   Premium Dark Theme with Glassmorphism
   ======================================== */

/* === CSS Variables === */
:root {
    /* Colors - Warm Notebook Style */
    --color-bg-primary: #fdfbf7;
    /* Creamy paper */
    --color-bg-secondary: #f4f0e6;
    /* Thick paper/Cardboard */
    --color-bg-tertiary: #ebe6da;
    /* Darker paper/Tape */
    --color-bg-surface: #ffffff;
    /* Pure white for overlays/cards */
    --color-bg-glass: rgba(255, 255, 255, 0.65);
    /* Frosted tracing paper */

    --color-border: rgba(92, 85, 78, 0.15);
    /* Pencil grey/brown */
    --color-border-hover: rgba(92, 85, 78, 0.3);

    --color-text-primary: #382e28;
    /* Deep Coffee - Darker for contrast */
    --color-text-secondary: #6e5d50;
    /* Medium Brown */
    --color-text-muted: #948171;
    /* Readable Light Brown */

    /* Accents - Warm & Earthy */
    --color-accent-primary: #d97757;
    /* Terracotta Red */
    --color-accent-secondary: #c86b4e;
    --color-accent-gradient: linear-gradient(135deg, #d97757 0%, #e08e73 100%);

    --color-success: #6b8e23;
    /* Olive Green */
    --color-warning: #cd853f;
    /* Peru */
    --color-error: #cd5c5c;
    /* Indian Red */

    /* Spacing */
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
    --spacing-xl: 32px;
    --spacing-2xl: 48px;

    /* Border Radius - Slightly organic */
    --radius-sm: 6px;
    --radius-md: 12px;
    --radius-lg: 18px;
    --radius-xl: 26px;
    --radius-full: 9999px;

    /* Shadows - Soft & Warm */
    --shadow-sm: 0 2px 4px rgba(74, 62, 53, 0.05);
    --shadow-md: 0 4px 12px rgba(74, 62, 53, 0.08);
    --shadow-lg: 0 8px 24px rgba(74, 62, 53, 0.12);
    --shadow-glow: 0 0 15px rgba(217, 119, 87, 0.3);

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-normal: 250ms ease;
    --transition-slow: 400ms ease;

    /* Layout */
    --sidebar-width: 280px;
    --panel-width: 400px;
    --header-height: 60px;
}

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

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: 'Lora', 'Georgia', 'Microsoft YaHei', '微软雅黑', sans-serif;
    background: var(--color-bg-primary);

    color: var(--color-text-primary);
    line-height: 1.6;
    min-height: 100vh;
    min-height: calc(var(--vh, 1vh) * 100);
}

/* Global Emoji Ink Filter - REMOVED (Replaced by Sticker/Stamp system) */
/* .brand-icon etc removed from here to specialized classes below */

/* === Notebook Decoration System (Image Based) === */

/* Base class for all hand-drawn icons */
.hand-icon {
    display: inline-block;
    vertical-align: middle;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
}

/* 1. Stickers: Colorful, white border feel, shadow, slight chaotic rotation */
.icon-sticker {
    width: 24px;
    height: 24px;
    filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.15));
    transform: rotate(-3deg);
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.icon-sticker.lg {
    width: 32px;
    height: 32px;
}

.icon-sticker.xl {
    width: 48px;
    height: 48px;
}

.icon-sticker.xxl {
    width: 64px;
    height: 64px;
}

.icon-sticker:hover {
    transform: scale(1.15) rotate(4deg);
    filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.2));
}

/* 2. Stamps: Clean hand-drawn feel, original colors */
.icon-stamp {
    width: 20px;
    height: 20px;
    /* Removed heavy filters (sepia, multiply) to avoid 'dirty' look */
    /* Just slight opacity to blend nicely, but keep original colors */
    opacity: 0.9;
    transform: rotate(2deg);
    transition: all 0.2s ease;
}

.icon-stamp.lg {
    width: 24px;
    height: 24px;
}

.icon-stamp.xl {
    width: 64px;
    height: 64px;
}

.icon-stamp:hover {
    opacity: 1;
    transform: rotate(-2deg) scale(1.1);
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
}

/* Specific Adjustments */
.brand-icon img {
    width: 32px;
    height: 32px;
}

/* Hand-drawn Checkbox */
input[type='checkbox'] {
    appearance: none;
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    border: 2px solid var(--color-text-secondary);
    border-radius: 2px 5px 3px 4px;
    /* Organic box */
    background: transparent;
    cursor: pointer;
    position: relative;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

input[type='checkbox']:hover {
    border-color: var(--color-accent-primary);
}

input[type='checkbox']:checked {
    border-color: var(--color-accent-primary);
}

input[type='checkbox']:checked::after {
    content: '✓';
    font-family: 'Patrick Hand', cursive;
    font-size: 24px;
    color: var(--color-accent-primary);
    position: absolute;
    top: -12px;
    left: 2px;
    transform: rotate(-5deg);
}

/* Custom Scrollbar */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(92, 85, 78, 0.2);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(92, 85, 78, 0.4);
}

a {
    color: var(--color-accent-primary);
    text-decoration: none;
    transition: color var(--transition-fast);
}

a:hover {
    color: var(--color-accent-secondary);
}

button {
    font-family: inherit;
    cursor: pointer;
    border: none;
    outline: none;
}

/* === Typography === */
h1,
h2,
h3,
h4,
h5,
h6 {
    font-family: 'Lora', serif;
    font-weight: 500;
    line-height: 1.3;
}

h1 {
    font-size: 2rem;
}

h2 {
    font-size: 1.5rem;
}

h3 {
    font-size: 1.25rem;
}

/* === Utility Classes === */
.text-center {
    text-align: center;
}

.text-muted {
    color: var(--color-text-muted);
}

.text-secondary {
    color: var(--color-text-secondary);
}

.text-success {
    color: var(--color-success);
}

.text-warning {
    color: var(--color-warning);
}

.text-error {
    color: var(--color-error);
}

.hidden {
    display: none !important;
}

.flex {
    display: flex;
}

.flex-col {
    flex-direction: column;
}

.items-center {
    align-items: center;
}

.justify-center {
    justify-content: center;
}

.justify-between {
    justify-content: space-between;
}

.gap-sm {
    gap: var(--spacing-sm);
}

.gap-md {
    gap: var(--spacing-md);
}

/* === Glassmorphism Card === */
/* === Glassmorphism Card (Paper Note Style) === */
.glass-card {
    background: var(--color-bg-glass);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    transition:
        border-color var(--transition-normal),
        box-shadow var(--transition-normal),
        transform var(--transition-normal);

    /* Organic Touch */
    border-top-left-radius: 2px;
    border-bottom-right-radius: 2px;
    box-shadow: 2px 4px 12px rgba(74, 62, 53, 0.08);
    transform: rotate(-0.3deg);
    /* Subtle organic tilt */
}

/* Washi Tape Decoration (Top Center) */
.glass-card::before {
    content: '';
    position: absolute;
    top: -10px;
    left: 50%;
    transform: translateX(-50%) rotate(1.5deg);
    width: 80px;
    height: 24px;
    background: rgba(217, 119, 87, 0.3);
    /* Terracotta Tape */
    opacity: 0.6;
    z-index: 10;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    mask-image: url("data:image/svg+xml,%3Csvg width='100%25' height='100%25' viewBox='0 0 100 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M0,0 L100,0 L95,100 L5,100 Z' fill='black'/%3E%3C/svg%3E");
    /* Rough edge simulation (simple) */
}

.glass-card:hover {
    border-color: var(--color-border-hover);
    box-shadow: var(--shadow-md);
}

/* === Buttons === */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--spacing-sm);
    padding: var(--spacing-sm) var(--spacing-md);
    border-radius: var(--radius-md);
    font-size: 0.875rem;
    font-weight: 500;
    font-weight: 500;
    transition: all var(--transition-fast);
}

@media (max-width: 640px) {
    .btn {
        padding: 6px 10px;
        font-size: 0.8rem;
        gap: 4px;
        white-space: nowrap;
        /* Prevent wrapping */
    }
}

.btn-primary {
    background: var(--color-accent-gradient);
    color: white;
    box-shadow: var(--shadow-sm);
}

.btn-primary:hover {
    box-shadow: var(--shadow-glow);
    transform: translateY(-1px);
}

.btn-secondary {
    background: var(--color-bg-tertiary);
    color: var(--color-text-primary);
    border: 1px solid var(--color-border);
}

.btn-secondary:hover {
    background: var(--color-bg-secondary);
    border-color: var(--color-border-hover);
}

.btn-ghost {
    background: transparent;
    color: var(--color-text-secondary);
}

.btn-ghost:hover {
    background: var(--color-bg-glass);
    color: var(--color-text-primary);
}

.btn-lg {
    padding: var(--spacing-md) var(--spacing-lg);
    font-size: 1rem;
}

.btn:disabled,
.btn[disabled] {
    opacity: 0.6;
    cursor: not-allowed;
    filter: grayscale(0.2);
    box-shadow: none !important;
    transform: none !important;
    background-color: var(--color-bg-tertiary);
    /* Optional fallback */
    color: var(--color-text-muted);
}

/* === Input Fields === */
.input-field {
    width: 100%;
    padding: var(--spacing-sm) var(--spacing-md);
    background: var(--color-bg-tertiary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    color: var(--color-text-primary);
    /* Ensure consistent font rendering */
    font-family: inherit;
    font-size: 1rem;
    transition:
        border-color var(--transition-fast),
        box-shadow var(--transition-fast);
}

.input-field:focus {
    border-color: var(--color-accent-primary);
    box-shadow: 0 0 0 3px rgba(217, 119, 87, 0.2);
    outline: none;
}

.input-field::placeholder {
    color: var(--color-text-muted);
    opacity: 0.7;
    /* Make placeholder visually lighter than input text */
}

textarea.input-field {
    resize: vertical;
    min-height: 100px;
}

/* === Login Page === */
.login-container {
    min-height: 100vh;
    min-height: calc(var(--vh, 1vh) * 100);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-lg);
    background:
        radial-gradient(ellipse at top, rgba(217, 119, 87, 0.15) 0%, transparent 50%),
        radial-gradient(ellipse at bottom right, rgba(224, 142, 115, 0.1) 0%, transparent 50%), var(--color-bg-primary);
}

.login-card {
    width: 100%;
    max-width: 420px;
    padding: var(--spacing-2xl);
}

.login-logo {
    width: 64px;
    height: 64px;
    margin: 0 auto var(--spacing-lg);
    background: var(--color-accent-gradient);
    border-radius: var(--radius-lg);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
}

.login-title {
    text-align: center;
    margin-bottom: var(--spacing-sm);
}

.login-subtitle {
    text-align: center;
    color: var(--color-text-secondary);
    margin-bottom: var(--spacing-xl);
}

.login-divider {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    margin: var(--spacing-lg) 0;
    color: var(--color-text-muted);
    font-size: 0.75rem;
}

.login-divider::before,
.login-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--color-border);
}

/* === Dashboard Layout === */
.dashboard {
    display: flex;
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: var(--sidebar-width);
    background: var(--color-bg-secondary);
    border-right: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.sidebar-header {
    padding: var(--spacing-lg);
    border-bottom: 1px solid var(--color-border);
}

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    font-weight: 600;
}

.sidebar-brand-icon {
    width: 32px;
    height: 32px;
    background: var(--color-accent-gradient);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
}

.sidebar-nav {
    flex: 1;
    padding: var(--spacing-md);
    overflow-y: auto;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    padding: var(--spacing-sm) var(--spacing-md);
    border-radius: var(--radius-md);
    color: var(--color-text-secondary);
    transition: all var(--transition-fast);
    margin-bottom: var(--spacing-xs);
}

.nav-item:hover,
.nav-item.active {
    background: var(--color-bg-glass);
    color: var(--color-text-primary);
}

.nav-item.active {
    background: rgba(217, 119, 87, 0.1);
    color: var(--color-accent-primary);
}

.sidebar-footer {
    padding: var(--spacing-md);
    border-top: 1px solid var(--color-border);
}

/* Main Content */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.main-header {
    height: var(--header-height);
    padding: 0 var(--spacing-lg);
    border-bottom: 1px solid var(--color-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--color-bg-secondary);
}

.main-body {
    flex: 1;
    display: flex;
    overflow: hidden;
}

/* Chat Area */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-messages {
    flex: 1;
    padding: var(--spacing-lg);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

.message {
    max-width: 80%;
    padding: var(--spacing-md);
    border-radius: var(--radius-lg);
}

.message.user {
    align-self: flex-end;
    background: var(--color-accent-gradient);
    color: white;
    border-bottom-right-radius: var(--radius-sm);
}

.message.assistant {
    align-self: flex-start;
    background: var(--color-bg-tertiary);
    border: 1px solid var(--color-border);
    border-bottom-left-radius: var(--radius-sm);
}

.chat-input-area {
    padding: var(--spacing-lg);
    border-top: 1px solid var(--color-border);
    background: var(--color-bg-secondary);
}

.chat-input-wrapper {
    display: flex;
    gap: var(--spacing-sm);
    align-items: flex-end;
}

.chat-input {
    flex: 1;
    min-height: 48px;
    max-height: 200px;
    padding: var(--spacing-md);
    background: var(--color-bg-tertiary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    color: var(--color-text-primary);
    font-size: 0.9375rem;
    resize: none;
    transition: border-color var(--transition-fast);
}

.chat-input:focus {
    border-color: var(--color-accent-primary);
    outline: none;
}

.chat-actions {
    display: flex;
    gap: var(--spacing-xs);
}

/* Side Panel */
.side-panel {
    width: var(--panel-width);
    background: var(--color-bg-secondary);
    border-left: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.panel-header {
    padding: var(--spacing-md) var(--spacing-lg);
    border-bottom: 1px solid var(--color-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.panel-title {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--color-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.panel-content {
    flex: 1;
    padding: var(--spacing-lg);
    overflow-y: auto;
}

/* Upload Zone */
.upload-zone {
    border: 2px dashed var(--color-border);
    border-radius: var(--radius-lg);
    padding: var(--spacing-xl);
    text-align: center;
    cursor: pointer;
    transition: all var(--transition-normal);
}

.upload-zone:hover,
.upload-zone.dragover {
    border-color: var(--color-accent-primary);
    background: rgba(217, 119, 87, 0.05);
}

.upload-icon {
    font-size: 2rem;
    margin-bottom: var(--spacing-md);
    color: var(--color-text-muted);
}

.upload-text {
    color: var(--color-text-secondary);
    font-size: 0.875rem;
}

.upload-text strong {
    color: var(--color-accent-primary);
}

/* Preview Images */
.preview-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: var(--spacing-sm);
    margin-top: var(--spacing-md);
}

.preview-item {
    position: relative;
    aspect-ratio: 1;
    border-radius: var(--radius-md);
    overflow: hidden;
}

.preview-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.preview-remove {
    position: absolute;
    top: var(--spacing-xs);
    right: var(--spacing-xs);
    width: 24px;
    height: 24px;
    background: rgba(0, 0, 0, 0.7);
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 0.75rem;
}

/* Result Card */
.result-card {
    background: var(--color-bg-tertiary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: var(--spacing-lg);
    margin-top: var(--spacing-md);
}

.result-header {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    margin-bottom: var(--spacing-md);
}

.result-icon {
    width: 32px;
    height: 32px;
    background: var(--color-accent-gradient);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
}

.result-title {
    font-size: 0.875rem;
    font-weight: 600;
}

.result-subtitle {
    font-size: 0.75rem;
    color: var(--color-text-muted);
}

/* Loading States */
.loading-spinner {
    width: 20px;
    height: 20px;
    border: 2px solid var(--color-border);
    border-top-color: var(--color-accent-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

.skeleton {
    background: linear-gradient(
        90deg,
        var(--color-bg-tertiary) 0%,
        var(--color-bg-secondary) 50%,
        var(--color-bg-tertiary) 100%
    );
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
    border-radius: var(--radius-sm);
}

@keyframes shimmer {
    0% {
        background-position: 200% 0;
    }

    100% {
        background-position: -200% 0;
    }
}

/* === Responsive === */
@media (max-width: 1024px) {
    .sidebar {
        position: fixed;
        left: -100%;
        top: 0;
        height: 100vh;
        z-index: 100;
        transition: left var(--transition-normal);
    }

    .sidebar.open {
        left: 0;
    }

    .side-panel {
        position: fixed;
        right: -100%;
        top: 0;
        height: 100vh;
        z-index: 100;
        transition: right var(--transition-normal);
    }

    .side-panel.open {
        right: 0;
    }
}

@media (max-width: 640px) {
    :root {
        --panel-width: 100%;
        --sidebar-width: 100%;
    }

    .login-card {
        padding: var(--spacing-lg);
    }

    .message {
        max-width: 90%;
    }
}

/* Profile specific inputs */
.profile-field-input {
    font-family: inherit;
    /* Critical for consistency */
    color: var(--color-text-primary);
}

/* Ensure select elements also inherit font */
select {
    font-family: inherit;
}

/* === Search Popup === */
.search-results-popup {
    position: absolute;
    background: white;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 1000;
    max-height: 300px;
    overflow-y: auto;
    min-width: 200px;
    font-size: 14px;
}

.search-results-popup.hidden {
    display: none;
}

.search-results-popup .search-result-item {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    cursor: pointer;
    border-bottom: 1px solid var(--color-bg-secondary);
    transition: background-color 0.1s;
}

.search-results-popup .search-result-item:last-child {
    border-bottom: none;
}

.search-results-popup .search-result-item:hover {
    background-color: var(--color-bg-secondary);
}

.search-results-popup .search-icon {
    margin-right: 10px;
    font-size: 1.2em;
}

.search-results-popup .search-content {
    flex: 1;
    min-width: 0;
}

.search-results-popup .search-title {
    font-weight: 500;
    color: var(--color-text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.search-results-popup .search-meta {
    font-size: 12px;
    color: var(--color-text-muted);
}

.search-results-popup .search-section-title {
    padding: 6px 12px;
    font-size: 12px;
    font-weight: 600;
    color: var(--color-text-muted);
    background-color: var(--color-bg-secondary);
    border-bottom: 1px solid var(--color-border);
}

.search-results-popup .search-empty {
    padding: 12px;
    color: var(--color-text-muted);
    text-align: center;
    font-style: italic;
}

.search-results-popup .search-empty-hint {
    padding: 8px 12px;
    color: var(--color-text-muted);
    font-size: 12px;
}

/* === Global Language Switcher === */
.global-lang-switcher {
    position: absolute;
    top: 16px;
    right: 16px;
    z-index: 9999;
}

.global-lang-switcher select {
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    border: 1px solid rgba(92, 85, 78, 0.2);
    border-radius: 20px;
    padding: 6px 30px 6px 12px;
    font-family: inherit;
    font-size: 0.85rem;
    color: #4a3e35;
    outline: none;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
    appearance: none;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%234a3e35' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 8px center;
    background-size: 14px;
    transition: all 0.2s ease;
}

.global-lang-switcher select:hover {
    background-color: white;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
}
