:root {
    /* Light Theme Colors */
    --bg-body: #f5f3ef;
    --bg-surface: #ffffff;
    --bg-elevated: #fafafa;
    --bg-input: #ffffff;

    --text-primary: #1a1a1a;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;

    --accent-primary: #f97316;
    --accent-secondary: #ea580c;
    --accent-glow: rgba(249, 115, 22, 0.15);
    --accent-subtle: rgba(249, 115, 22, 0.08);

    --border-light: #e5e7eb;
    --border-medium: #d1d5db;

    --font-display: 'Inter', system-ui, -apple-system, sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

    --space-xs: 0.25rem;
    --space-sm: 0.5rem;
    --space-md: 1rem;
    --space-lg: 1.5rem;
    --space-xl: 2rem;
    --space-2xl: 3rem;
    --space-3xl: 4rem;

    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.08);
    --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.1);

    --transition-fast: 150ms ease;
    --transition-base: 250ms ease;
    --transition-slow: 400ms ease;
}

/* Dark Theme */
[data-theme="dark"] {
    --bg-body: #0f0f0f;
    --bg-surface: #1a1a1a;
    --bg-elevated: #262626;
    --bg-input: #1a1a1a;

    --text-primary: #fafafa;
    --text-secondary: #a1a1aa;
    --text-muted: #71717a;

    --accent-primary: #fb923c;
    --accent-secondary: #f97316;
    --accent-glow: rgba(251, 146, 60, 0.2);
    --accent-subtle: rgba(251, 146, 60, 0.1);

    --border-light: #2a2a2a;
    --border-medium: #3a3a3a;

    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.4);
    --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.5);
}

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

body {
    font-family: var(--font-display);
    background: var(--bg-body);
    color: var(--text-primary);
    min-height: 100vh;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    transition: background-color var(--transition-base), color var(--transition-base);
}

/* Container */
.container {
    max-width: 900px;
    margin: 0 auto;
    padding: var(--space-2xl) var(--space-lg);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* Header */
.hero {
    position: relative;
    text-align: center;
    margin-bottom: var(--space-2xl);
    padding: var(--space-xl) 0;
}

.hero h1 {
    font-size: clamp(2rem, 5vw, 3rem);
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.02em;
    margin-bottom: var(--space-sm);
}

/* Theme Toggle - Aligned with Content */
.theme-toggle {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 50;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.theme-toggle:hover {
    border-color: var(--accent-primary);
    color: var(--accent-primary);
    background: var(--accent-subtle);
}

.theme-toggle .icon-moon {
    display: none;
}

[data-theme="dark"] .theme-toggle .icon-sun {
    display: none;
}

[data-theme="dark"] .theme-toggle .icon-moon {
    display: block;
}

.subtitle {
    font-size: 1rem;
    font-weight: 400;
    color: var(--text-secondary);
}

.highlight {
    color: var(--accent-primary);
    font-weight: 600;
}

/* Main Panel */
.optimizer-panel {
    background: var(--bg-surface);
    border-radius: var(--radius-xl);
    padding: var(--space-xl);
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-light);
    flex: 1;
    display: flex;
    flex-direction: column;
    transition: background-color var(--transition-base), border-color var(--transition-base);
}

/* Input Section */
.input-section {
    margin-bottom: var(--space-lg);
    flex: 1;
    display: flex;
    flex-direction: column;
}

.label-group {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--space-md);
}

.label-group label {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-primary);
}

.char-counter {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

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

.icon-btn.small {
    width: 28px;
    height: 28px;
    opacity: 0.6;
}

.icon-btn.small:hover {
    opacity: 1;
}

/* Textarea */
textarea {
    width: 100%;
    min-height: 180px;
    flex: 1;
    padding: var(--space-lg);
    background: var(--bg-input);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-family: var(--font-display);
    font-size: 0.9375rem;
    line-height: 1.7;
    resize: none;
    transition: all var(--transition-base);
}

textarea::placeholder {
    color: var(--text-muted);
}

textarea:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

/* Custom Scrollbar */
textarea::-webkit-scrollbar,
#result-display::-webkit-scrollbar,
.history-list::-webkit-scrollbar {
    width: 8px;
}

textarea::-webkit-scrollbar-track,
#result-display::-webkit-scrollbar-track,
.history-list::-webkit-scrollbar-track {
    background: var(--bg-elevated);
    border-radius: var(--radius-sm);
}

textarea::-webkit-scrollbar-thumb,
#result-display::-webkit-scrollbar-thumb,
.history-list::-webkit-scrollbar-thumb {
    background: var(--border-medium);
    border-radius: var(--radius-sm);
}

textarea::-webkit-scrollbar-thumb:hover,
#result-display::-webkit-scrollbar-thumb:hover,
.history-list::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* Action Bar */
.action-bar {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-sm);
    padding-top: var(--space-lg);
    border-top: 1px solid var(--border-light);
    margin-top: auto;
}

/* Buttons */
.primary-btn {
    position: relative;
    padding: 0.875rem 2rem;
    background: var(--accent-primary);
    border: none;
    border-radius: var(--radius-md);
    color: white;
    font-family: var(--font-display);
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition-base);
    box-shadow: var(--shadow-md);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-sm);
    min-width: 140px;
}

.primary-btn:hover {
    background: var(--accent-secondary);
    transform: translateY(-1px);
    box-shadow: var(--shadow-lg);
}

.primary-btn:active {
    transform: translateY(0);
}

.primary-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

.secondary-btn {
    padding: 0.875rem 1.25rem;
    background: var(--bg-elevated);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    font-family: var(--font-display);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-base);
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.secondary-btn:hover {
    border-color: var(--accent-primary);
    color: var(--accent-primary);
    background: var(--accent-subtle);
}

.text-btn {
    padding: 0.5rem 0.75rem;
    background: transparent;
    border: none;
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    font-family: var(--font-display);
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.text-btn:hover {
    color: var(--accent-primary);
    background: var(--accent-subtle);
}

.danger-btn {
    padding: 0.5rem 0.75rem;
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.3);
    border-radius: var(--radius-sm);
    color: #ef4444;
    font-family: var(--font-display);
    font-size: 0.75rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    gap: var(--space-xs);
}

.danger-btn:hover {
    background: rgba(239, 68, 68, 0.2);
    border-color: #ef4444;
}

.loader {
    display: none;
    width: 18px;
    height: 18px;
    border: 2px solid transparent;
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

.is-loading .btn-text {
    visibility: hidden;
}

.is-loading .loader {
    display: block;
    position: absolute;
}

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

/* Output Section */
.output-section {
    margin-top: var(--space-xl);
    animation: fadeIn 0.3s ease;
}

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

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

.output-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--space-md);
    padding-bottom: var(--space-sm);
    border-bottom: 1px solid var(--border-light);
}

.output-header>span {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-primary);
}

.output-actions {
    display: flex;
    gap: var(--space-xs);
}

.icon-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.icon-btn:hover {
    border-color: var(--accent-primary);
    color: var(--accent-primary);
    background: var(--accent-subtle);
}

.icon-btn.is-spinning svg {
    animation: spin 0.8s linear infinite;
}

#result-display {
    padding: var(--space-lg);
    background: var(--bg-elevated);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    font-size: 0.9375rem;
    line-height: 1.8;
    color: var(--text-primary);
    max-height: 400px;
    overflow-y: auto;
}

#result-display.markdown-body {
    background: var(--bg-elevated);
    color: var(--text-primary);
}

#result-display.markdown-body h1,
#result-display.markdown-body h2,
#result-display.markdown-body h3 {
    border-bottom-color: var(--border-light);
    color: var(--text-primary);
}

#result-display.markdown-body code {
    background: var(--bg-surface);
    color: var(--accent-primary);
    padding: 0.2em 0.4em;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border-light);
}

#result-display.markdown-body pre {
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
}

/* History Panel */
.history-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 380px;
    max-width: 100%;
    background: var(--bg-surface);
    border-left: 1px solid var(--border-light);
    box-shadow: var(--shadow-xl);
    z-index: 100;
    transform: translateX(100%);
    transition: transform var(--transition-base);
}

.history-panel:not(.hidden) {
    transform: translateX(0);
}

.history-content {
    height: 100%;
    display: flex;
    flex-direction: column;
}

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

.history-header h3 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
}

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

.history-list {
    flex: 1;
    padding: var(--space-md);
    overflow-y: auto;
}

.history-empty {
    text-align: center;
    color: var(--text-muted);
    padding: var(--space-2xl);
    font-size: 0.875rem;
}

.history-item {
    display: flex;
    align-items: flex-start;
    gap: var(--space-sm);
    padding: var(--space-md);
    background: var(--bg-elevated);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-md);
    margin-bottom: var(--space-sm);
    transition: all var(--transition-fast);
}

.history-item:hover {
    border-color: var(--accent-primary);
    background: var(--accent-subtle);
}

.history-item-content {
    flex: 1;
    min-width: 0;
    cursor: pointer;
}

.history-item-prompt {
    font-size: 0.875rem;
    color: var(--text-primary);
    margin-bottom: var(--space-xs);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.history-item-time {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.history-item-delete {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-sm);
    color: var(--text-muted);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.history-item-delete:hover {
    background: rgba(239, 68, 68, 0.15);
    border-color: #ef4444;
    color: #ef4444;
}

/* Footer */
footer {
    margin-top: var(--space-2xl);
    padding-top: var(--space-lg);
    text-align: center;
}

.footer-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.dot {
    width: 8px;
    height: 8px;
    background: #22c55e;
    border-radius: 50%;
    animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

/* Modal */
.modal-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-base);
}

.modal-overlay:not(.hidden) {
    opacity: 1;
    pointer-events: auto;
}

.modal-content {
    max-width: 400px;
    width: 90%;
    padding: var(--space-xl);
    background: var(--bg-surface);
    border: 1px solid var(--border-light);
    border-radius: var(--radius-lg);
    text-align: center;
    transform: scale(0.95);
    transition: transform var(--transition-base);
    box-shadow: var(--shadow-xl);
}

.modal-overlay:not(.hidden) .modal-content {
    transform: scale(1);
}

.modal-header {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-md);
    margin-bottom: var(--space-lg);
}

.modal-icon {
    color: var(--accent-primary);
}

.modal-content h3 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
}

.modal-content p {
    margin-bottom: var(--space-xl);
    color: var(--text-secondary);
    line-height: 1.6;
}

.modal-btn {
    padding: 0.75rem 1.5rem;
    background: var(--accent-primary);
    border: none;
    border-radius: var(--radius-md);
    color: white;
    font-family: var(--font-display);
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition-fast);
}

.modal-btn:hover {
    background: var(--accent-secondary);
}

.hidden {
    display: none !important;
}

/* History panel slide animation override */
.history-panel.hidden {
    display: block !important;
    transform: translateX(100%);
    pointer-events: none;
}

/* Responsive */
@media (max-width: 640px) {
    .container {
        padding: var(--space-lg) var(--space-md);
    }

    .hero h1 {
        font-size: 1.75rem;
    }

    .hero {
        margin-bottom: var(--space-xl);
        padding: var(--space-md) 0;
    }

    .theme-toggle {
        width: 40px;
        height: 40px;
        top: -4px;
    }

    .optimizer-panel {
        padding: var(--space-lg);
    }

    .label-group {
        gap: var(--space-xs);
    }

    .action-bar {
        flex-direction: column;
    }

    .secondary-btn,
    .primary-btn {
        width: 100%;
        justify-content: center;
    }

    .footer-content {
        flex-direction: column;
        gap: var(--space-sm);
    }

    .history-panel {
        width: 100%;
    }
}