:root {
    --bg-color: #0d0d12;
    --surface-color: #1a1b26;
    --surface-color-2: #242636;
    --primary-color: #6366f1;
    --primary-gradient: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
    --text-primary: #ffffff;
    --text-secondary: #a1a1aa;
    --danger-color: #ef4444;
    --success-color: #22c55e;
    --radius-lg: 16px;
    --radius-md: 12px;
    --radius-sm: 8px;
    --spacing-xs: 8px;
    --spacing-sm: 16px;
    --spacing-md: 24px;
    --spacing-lg: 32px;
    --font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.4), 0 4px 6px -2px rgba(0, 0, 0, 0.2);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

body {
    background-color: var(--bg-color);
    color: var(--text-primary);
    font-family: var(--font-family);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    min-height: 100vh;
    display: flex;
    justify-content: center;
}

.app-container {
    width: 100%;
    max-width: 600px;
    /* Mobile focused max-width */
    background-color: var(--bg-color);
    min-height: 100vh;
    position: relative;
    overflow-x: hidden;
}

/* Typography */
h1 {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
}

h2 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-primary);
}

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

/* Header */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--spacing-md);
    position: sticky;
    top: 0;
    z-index: 10;
    background: rgba(13, 13, 18, 0.8);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

.header-actions {
    display: flex;
    gap: var(--spacing-sm);
    align-items: center;
}

/* Buttons */
button {
    cursor: pointer;
    border: none;
    outline: none;
    font-family: var(--font-family);
    transition: all 0.2s ease;
}

.icon-btn {
    background: transparent;
    color: var(--text-primary);
    padding: var(--spacing-xs);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.icon-btn:active {
    transform: scale(0.95);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 12px 24px;
    border-radius: var(--radius-md);
    font-weight: 600;
    font-size: 1rem;
    gap: 8px;
    width: auto;
}

.btn.primary {
    background: var(--primary-gradient);
    color: white;
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.btn.primary:hover {
    opacity: 0.9;
    box-shadow: 0 6px 16px rgba(99, 102, 241, 0.4);
}

.btn.primary:active {
    transform: translateY(1px);
}

.btn.secondary {
    background: var(--surface-color-2);
    color: var(--text-primary);
}

.btn.secondary:hover {
    background: #2f3245;
}

.btn.huge {
    width: 64vw;
    height: 64vw;
    max-width: 250px;
    max-height: 250px;
    border-radius: 50%;
    flex-direction: column;
    font-size: 1.25rem;
    gap: 16px;
    border: 4px solid rgba(255, 255, 255, 0.1);
    position: relative;
    overflow: hidden;
}

.btn.huge::after {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at center, rgba(255, 255, 255, 0.2) 0%, transparent 70%);
    opacity: 0;
    transition: opacity 0.3s;
}

.btn.huge:hover::after {
    opacity: 1;
}

.huge svg {
    width: 48px;
    height: 48px;
}

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

textarea {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid #e2e8f0;
    border-radius: var(--border-radius);
    font-family: inherit;
    font-size: 1rem;
    resize: vertical;
    margin-bottom: 1rem;
}

.error-message {
    color: #ef4444;
    font-size: 0.9rem;
    margin-bottom: 1rem;
    padding: 0.5rem;
    background-color: #fef2f2;
    border: 1px solid #fee2e2;
    border-radius: 6px;
    text-align: center;
}

.full-width {
    width: 100%;
}

/* Views */
.view {
    padding: var(--spacing-md);
    animation: fadeIn 0.3s ease-out;
}

.view.hidden {
    display: none !important;
}

.hidden {
    display: none !important;
}

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

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Scan View */
#scan-view {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    min-height: calc(100vh - 80px);
    justify-content: center;
    gap: var(--spacing-lg);
}

.hero h2 {
    font-size: 2rem;
    margin-bottom: var(--spacing-xs);
    background: var(--primary-gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

/* Image Preview */
#image-preview-container {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
    animation: slideUp 0.4s ease-out;
}

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

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

#image-preview {
    width: 100%;
    max-height: 400px;
    object-fit: cover;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

/* Settings View (Overlay) */
#settings-view {
    position: fixed;
    inset: 0;
    background: var(--bg-color);
    z-index: 100;
    padding: var(--spacing-md);
    display: flex;
    flex-direction: column;
}

#settings-view.hidden {
    display: none;
}

.view-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--spacing-lg);
}

.settings-form {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
    overflow-y: auto;
    padding-bottom: 20px;
    min-height: 0;
    /* Important for flex scrolling */
    /* Add some padding for scroll end */
}

.input-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

label {
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-primary);
}

.hint {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

input[type="text"],
input[type="number"],
input[type="password"] {
    background: var(--surface-color);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: var(--radius-md);
    padding: 14px;
    color: var(--text-primary);
    font-size: 1rem;
    font-family: var(--font-family);
    transition: border-color 0.2s;
}

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

/* Results View */
.result-card {
    background: var(--surface-color);
    border-radius: var(--radius-lg);
    padding: var(--spacing-md);
    margin-bottom: var(--spacing-sm);
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.summary-card {
    background: var(--primary-gradient);
    border-radius: var(--radius-lg);
    padding: var(--spacing-md);
    text-align: center;
    color: white;
    margin-bottom: var(--spacing-md);
    margin-bottom: var(--spacing-md);
    box-shadow: var(--shadow-lg);
}

.split-bolus-card {
    background: rgba(249, 115, 22, 0.15);
    border: 1px solid rgba(249, 115, 22, 0.4);
    border-radius: var(--radius-lg);
    padding: var(--spacing-md);
    margin-bottom: var(--spacing-sm);
    animation: slideUp 0.4s ease-out;
}

.split-header {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 12px;
}

.split-icon {
    font-size: 1.5rem;
}

.split-bolus-card h3 {
    margin: 0;
    color: #fb923c;
    /* Orange-400 */
    font-size: 1.1rem;
}

.split-details p {
    color: var(--text-primary);
    margin-bottom: 4px;
    font-size: 0.95rem;
}

.split-reason {
    color: var(--text-secondary) !important;
    font-size: 0.85rem !important;
    margin-top: 8px !important;
    font-style: italic;
}

.summary-card h3 {
    font-size: 1rem;
    opacity: 0.9;
    font-weight: 500;
    margin-bottom: 4px;
    color: white;
}

.insulin-dose {
    font-size: 2.5rem;
    font-weight: 800;
    line-height: 1.2;
}

.unit {
    font-size: 1rem;
    font-weight: 600;
    opacity: 0.8;
}

.food-item {
    display: flex;
    justify-content: space-between;
    padding: 12px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}

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

.food-name {
    font-weight: 500;
}

.food-macros {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.warning-box {
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.2);
    color: #fca5a5;
    padding: 12px;
    border-radius: var(--radius-md);
    font-size: 0.9rem;
    margin-top: 16px;
    display: flex;
    gap: 8px;
    align-items: start;
}

/* Loading */
#loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;
    gap: 16px;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    border-top-color: var(--primary-color);
    animation: spin 1s linear infinite;
}

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

#loading-fact {
    margin-top: 16px;
    font-size: 0.9rem;
    color: var(--text-secondary);
    font-style: italic;
    max-width: 80%;
    text-align: center;
    min-height: 1.5em;
    opacity: 1;
    transition: opacity 0.3s ease;
}

/* History View */
.history-list {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
    padding-bottom: 80px;
}

.history-item {
    background: var(--surface-color);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: var(--radius-md);
    padding: var(--spacing-sm);
    cursor: pointer;
    transition: background 0.2s;
}

.history-item:hover {
    background: var(--surface-color-2);
}

.history-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

.history-main {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.history-food {
    font-weight: 600;
    font-size: 1.1rem;
    color: var(--text-primary);
}

.history-dose {
    background: rgba(99, 102, 241, 0.1);
    color: var(--primary-color);
    padding: 4px 8px;
    border-radius: 6px;
    font-weight: 700;
    font-size: 0.9rem;
}

.history-stats {
    display: flex;
    gap: 12px;
    font-size: 0.85rem;
    color: var(--text-secondary);
    border-top: 1px solid rgba(255, 255, 255, 0.05);
    padding-top: 8px;
}

.stat-glucose {
    display: flex;
    gap: 4px;
    align-items: center;
}

.stat-glucose.high {
    color: var(--danger-color);
}

.stat-glucose.good {
    color: var(--success-color);
}

/* Modal Styles */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    opacity: 1;
    transition: opacity 0.3s ease;
}

.modal.hidden {
    opacity: 0;
    pointer-events: none;
    display: none;
}

.modal-content {
    background: linear-gradient(145deg, rgba(30, 30, 30, 0.9), rgba(20, 20, 20, 0.95));
    border: 1px solid rgba(255, 255, 255, 0.1);
    padding: 24px;
    border-radius: 20px;
    width: 90%;
    max-width: 340px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
    text-align: center;
    animation: slideUp 0.3s ease-out;
}

.modal-content h3 {
    margin-top: 0;
    margin-bottom: 8px;
    font-size: 1.25rem;
    color: var(--text-primary);
}

.modal-content p {
    color: var(--text-secondary);
    margin-bottom: 20px;
    font-size: 0.95rem;
}

.modal-content .input-group {
    position: relative;
    margin-bottom: 24px;
}

.modal-content input {
    width: 100%;
    padding: 12px 16px;
    font-size: 1.5rem;
    text-align: center;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    color: var(--text-primary);
    outline: none;
    font-weight: 600;
}

.modal-content input:focus {
    border-color: var(--primary-color);
    background: rgba(255, 255, 255, 0.1);
}

.modal-content .unit {
    display: block;
    margin-top: 6px;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.modal-actions {
    display: flex;
    gap: 12px;
}

.modal-actions .btn {
    flex: 1;
    padding: 12px;
    border-radius: 12px;
    font-size: 1rem;
    cursor: pointer;
    border: none;
    transition: transform 0.1s;
}

.modal-actions .btn.secondary {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
}

.modal-actions .btn.primary {
    background: var(--primary-color);
    color: #fff;
}

.modal-actions .btn:active {
    transform: scale(0.96);
}