:root {
    --bg-color: #0f172a;
    --surface-color: rgba(30, 41, 59, 0.7);
    --border-color: rgba(148, 163, 184, 0.1);
    --primary-color: #8b5cf6;
    --primary-hover: #7c3aed;
    --text-primary: #f8fafc;
    --text-secondary: #94a3b8;
    --radius-lg: 16px;
    --radius-md: 8px;
    --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --glow: 0 0 20px rgba(139, 92, 246, 0.3);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    background-image:
        radial-gradient(at 0% 0%, rgba(139, 92, 246, 0.15) 0px, transparent 50%),
        radial-gradient(at 100% 100%, rgba(56, 189, 248, 0.15) 0px, transparent 50%);
    background-attachment: fixed;
}

.app-container {
    width: 100%;
    padding: 2rem 1rem;
}

.header {
    text-align: center;
    margin-bottom: 2rem;
    animation: fadeIn Down 0.6s ease-out;
}

.logo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}

.logo h1 {
    font-size: 2rem;
    font-weight: 700;
    background: linear-gradient(to right, #c084fc, #6366f1);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.subtitle {
    color: var(--text-secondary);
}

/* Main Content - Centered Single Column */
.main-content {
    width: 100%;
    max-width: 800px;
    /* Comfortable reading width */
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

.glass-panel {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: var(--radius-lg);
    padding: 2.5rem;
    /* More padding */
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

/* Header Actions */
.header-actions {
    position: absolute;
    right: 2rem;
    top: 50%;
    transform: translateY(-50%);
}

.btn-icon-circle {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    font-size: 1.2rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    box-shadow: var(--shadow);
}

.btn-icon-circle:hover {
    background: var(--primary-color);
    border-color: var(--primary-color);
    color: white;
}

@keyframes pulse-glow {
    0% {
        box-shadow: 0 0 0 0 rgba(139, 92, 246, 0.4);
    }

    70% {
        box-shadow: 0 0 0 10px rgba(139, 92, 246, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(139, 92, 246, 0);
    }
}

#toggleHistoryBtn {
    background: rgba(139, 92, 246, 0.1);
    border-color: rgba(139, 92, 246, 0.3);
    color: var(--primary-color);
    /* animation: pulse-glow 2s infinite; */
    /* Optional: too distracting? */
}

#toggleHistoryBtn:hover {
    background: var(--primary-color);
    color: white;
    box-shadow: 0 0 15px rgba(139, 92, 246, 0.5);
}

/* History Drawer (Off-canvas) */
.history-drawer {
    position: fixed;
    top: 0;
    right: -750px;
    /* Hidden (match width + buffer) */
    width: 720px;
    /* Wider drawer */
    max-width: 90vw;
    /* Responsive for mobile */
    height: 100vh;
    background: #0f172a;
    /* Dark solid bg for drawer */
    border-left: 1px solid var(--glass-border);
    z-index: 1000;
    transition: right 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    box-shadow: -10px 0 30px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
}

.history-drawer.open {
    right: 0;
}

.video-preview {
    margin-top: 1rem;
    width: 100%;
    border-radius: var(--radius-md);
    overflow: hidden;
    background: black;
    display: flex;
    justify-content: center;
    border: 1px solid var(--border-color);
}

.video-preview video {
    width: 100%;
    height: auto;
    max-height: 400px;
    /* Prevent overly tall vertical videos */
    object-fit: contain;
}

.drawer-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.drawer-header h2 {
    font-size: 1.25rem;
    margin: 0;
}

.drawer-content {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
}

.drawer-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px);
    z-index: 999;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

.drawer-overlay.open {
    opacity: 1;
    pointer-events: auto;
}


#createForm {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.form-group {
    margin-bottom: 0;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-secondary);
}

.required {
    color: #ef4444;
}

.input-wrapper {
    position: relative;
}

.input-icon {
    position: absolute;
    right: 1rem;
    top: 50%;
    transform: translateY(-50%);
    opacity: 0.5;
}

input[type="text"],
input[type="password"],
input[type="url"],
textarea,
select {
    width: 100%;
    background: rgba(15, 23, 42, 0.6);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0.75rem 1rem;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 1rem;
    transition: all 0.2s;
}

input:focus,
textarea:focus,
select:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgba(139, 92, 246, 0.2);
}

.readonly-input {
    background: rgba(15, 23, 42, 0.4);
    cursor: not-allowed;
    opacity: 0.8;
}

.hint {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 0.25rem;
}

/* Image List */
.image-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.image-item {
    display: flex;
    align-items: flex-start;
    /* Align to top */
    gap: 1rem;
    background: rgba(255, 255, 255, 0.05);
    /* Slightly lighter */
    padding: 1rem;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    transition: border-color 0.2s;
}

.image-item:hover {
    border-color: rgba(255, 255, 255, 0.2);
}

.img-preview-box {
    width: 120px;
    /* Larger preview */
    height: 120px;
    background: var(--bg-hover);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    flex-shrink: 0;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.img-preview-box img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    /* or contain if full image matters */
}

.img-preview-box .icon {
    font-size: 2rem;
    opacity: 0.5;
}

.img-input-group {
    flex: 1;
    display: flex;
    flex-direction: column;
    /* Stack input and controls */
    gap: 0.75rem;
    justify-content: center;
    height: 120px;
    /* Match preview height */
}

.img-input-group input {
    width: 100%;
    background: var(--bg-main);
    padding: 0.75rem;
    font-size: 0.9rem;
}

.img-controls {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.btn-icon-text {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
    height: 36px;
    white-space: nowrap;
}

.btn-icon-text:hover {
    background: rgba(255, 255, 255, 0.2);
}

.btn-icon-text.primary {
    background: var(--primary-color);
    color: #fff;
}

.btn-icon-text.primary:hover {
    background: var(--primary-hover);
}

.btn-icon-text.danger:hover {
    background: rgba(239, 68, 68, 0.2);
    color: #ef4444;
}

/* Standard Icon Button */
.btn-icon {
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 1.25rem;
    padding: 0.5rem;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    width: 36px;
    /* Explicit size */
    height: 36px;
}

.btn-icon:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.1);
}

.btn-icon.danger {
    color: #ef4444;
}

.btn-icon.danger:hover {
    background: rgba(239, 68, 68, 0.1);
    border-color: rgba(239, 68, 68, 0.3);
}

.btn-icon:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.btn-secondary {
    background: transparent;
    border: 1px dashed var(--border-color);
    color: var(--primary-color);
    padding: 0.5rem 1rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    font-size: 0.9rem;
    width: 100%;
    transition: all 0.2s;
}

.btn-secondary:hover {
    background: rgba(139, 92, 246, 0.05);
    border-color: var(--primary-color);
}



/* Grid Settings */
.settings-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1rem;
}

.settings-grid .form-group {
    margin-bottom: 0;
}

@media (max-width: 768px) {
    .settings-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 480px) {
    .settings-grid {
        grid-template-columns: 1fr;
    }
}

/* Switch */
.switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 26px;
    margin-right: 0.75rem;
}

.switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #334155;
    transition: .4s;
}

.slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 4px;
    bottom: 4px;
    background-color: white;
    transition: .4s;
}

input:checked+.slider {
    background-color: var(--primary-color);
}

input:checked+.slider:before {
    transform: translateX(24px);
}

.slider.round {
    border-radius: 34px;
}

.slider.round:before {
    border-radius: 50%;
}

.checkbox-group {
    display: flex;
    align-items: center;
}

/* Buttons */
.btn-primary {
    width: 100%;
    background: linear-gradient(135deg, var(--primary-color), var(--primary-hover));
    color: white;
    border: none;
    padding: 1rem;
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: var(--glow);
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.5rem;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 0 30px rgba(139, 92, 246, 0.5);
}

.btn-primary:active {
    transform: translateY(0);
}

/* Loader */
.loader {
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top: 3px solid white;
    width: 20px;
    height: 20px;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.hidden {
    display: none !important;
}

/* Result Panel */
.result-panel {
    margin-top: 2rem;
    background: rgba(16, 185, 129, 0.1);
    border: 1px solid rgba(16, 185, 129, 0.3);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    animation: slideUp 0.4s ease-out;
}

.result-panel h3 {
    color: #34d399;
    margin-bottom: 1rem;
    font-size: 1.2rem;
}

.detail-item {
    display: flex;
    align-items: center;
    margin-bottom: 0.5rem;
}

.detail-item .label {
    width: 80px;
    color: var(--text-secondary);
}

.code-block {
    background: rgba(0, 0, 0, 0.3);
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
    font-family: monospace;
    color: #e2e8f0;
}

.status-badge {
    background: rgba(59, 130, 246, 0.2);
    color: #60a5fa;
    padding: 0.2rem 0.6rem;
    border-radius: 9999px;
    font-size: 0.85rem;
    font-weight: 500;
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* History Panel */
.history-section {
    margin-top: 2rem;
    animation: fadeIn 0.8s ease-out;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.history-header h2 {
    font-size: 1.5rem;
    color: var(--text-primary);
}

.btn-refresh {
    background: transparent;
    border: 1px solid var(--border-color);
    color: var(--primary-color);
    padding: 0.5rem 1rem;
    border-radius: var(--radius-md);
    cursor: pointer;
    font-size: 0.9rem;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.btn-refresh:hover {
    background: rgba(139, 92, 246, 0.1);
    border-color: var(--primary-color);
}

.task-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.task-card {
    background: rgba(30, 41, 59, 0.5);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1rem;
    transition: transform 0.2s, box-shadow 0.2s;
}

.task-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.2);
}

.task-meta {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
}

.task-main {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
}

.task-prompt {
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 70%;
}

.task-actions {
    display: flex;
    gap: 0.5rem;
}

.btn-sm {
    padding: 0.3rem 0.8rem;
    border-radius: 4px;
    border: 1px solid var(--border-color);
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.8rem;
}

.btn-sm:hover {
    border-color: var(--primary-color);
    color: var(--primary-color);
}

/* Modal */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 23, 42, 0.8);
    backdrop-filter: blur(4px);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    animation: fadeIn 0.2s ease-out;
}

.modal-content {
    background: #1e293b;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    width: 90%;
    max-width: 600px;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.5), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    animation: scaleIn 0.2s ease-out;
}

#userModal .modal-content,
#keyModal .modal-content {
    max-width: 900px;
}

@keyframes scaleIn {
    from {
        transform: scale(0.95);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.modal-header {
    padding: 1.5rem;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h2 {
    font-size: 1.25rem;
    color: var(--text-primary);
}

.modal-body {
    padding: 1.5rem;
    overflow-y: auto;
}

.key-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 2rem;
}

.key-item {
    background: rgba(15, 23, 42, 0.5);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1rem;
    display: grid;
    grid-template-columns: 1fr 120px;
    align-items: center;
    gap: 1rem;
}

.key-info h4 {
    margin-bottom: 0.25rem;
    color: var(--text-primary);
}

.key-info p {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.status-active {
    color: #4ade80;
    font-size: 0.8rem;
    margin-left: 0.5rem;
}

.tag-badge {
    display: inline-block;
    vertical-align: middle;
}

.status-paused {
    color: #facc15;
    font-size: 0.8rem;
    margin-left: 0.5rem;
}

.key-actions {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
}

.key-actions .btn-icon {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    transition: all 0.2s;
    background: rgba(255, 255, 255, 0.05);
}

.key-actions .btn-icon:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: translateY(-1px);
}

.key-actions .edit-key:hover {
    color: var(--primary-color);
    background: rgba(139, 92, 246, 0.15);
}

.key-actions .toggle-key:hover {
    color: #facc15;
    background: rgba(250, 204, 21, 0.15);
}

/* Toggle Switch Small */
.switch-sm {
    position: relative;
    display: inline-block;
    width: 34px;
    height: 18px;
}

.switch-sm input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider-sm {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #334155;
    transition: .4s;
    border-radius: 34px;
}

.slider-sm:before {
    position: absolute;
    content: "";
    height: 14px;
    width: 14px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked+.slider-sm {
    background-color: var(--primary-color);
}

input:checked+.slider-sm:before {
    transform: translateX(16px);
}

.model-config-row {
    display: grid;
    /* name, price, desc, toggle, actions */
    grid-template-columns: 150px 80px 1fr 50px 40px;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
    align-items: center;
    background: rgba(255, 255, 255, 0.03);
    padding: 0.5rem;
    border-radius: var(--radius-md);
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.btn-add-model {
    margin-top: 0.5rem;
    font-size: 0.85rem;
    margin-bottom: 0.85rem;
}

.section-title {
    font-size: 1rem;
    color: var(--text-primary);
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
}

.input-group-append {
    display: flex;
    gap: 0.5rem;
}

.select-wrapper {
    position: relative;
    width: 100%;
}

.select-wrapper::after {
    content: '▼';
    position: absolute;
    right: 1rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-secondary);
    pointer-events: none;
    font-size: 0.8rem;
}

/* Model Info Display (Full Width) */
.model-info-display {
    background: rgba(var(--primary-rgb), 0.1);
    border: 1px solid rgba(var(--primary-rgb), 0.2);
    border-radius: var(--radius-md);
    padding: 0.75rem 1rem;
    margin: 0;
    font-size: 0.9rem;
    transition: all 0.3s ease;
    width: 100%;
    box-sizing: border-box;
}

.model-info-row {
    display: flex;
    gap: 1.5rem;
    align-items: flex-start;
}

.info-price {
    white-space: nowrap;
    font-weight: bold;
    color: var(--primary-color);
    flex-shrink: 0;
}

.info-desc {
    opacity: 0.8;
    line-height: 1.4;
    flex: 1;
}

.checkbox-group {
    margin-top: 1.5rem;
    margin-bottom: 2rem;
    display: flex;
    align-items: center;
    gap: 10px;
}

/* Polling Indicator */
.polling-indicator {
    pointer-events: none;
    font-size: 0.8rem;
}

select {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
}

.model-info-display {
    background: rgba(139, 92, 246, 0.1);
    border: 1px solid rgba(139, 92, 246, 0.2);
    border-radius: var(--radius-md);
    padding: 0.75rem;
    margin-top: 0.5rem;
    font-size: 0.9rem;
}

.model-price {
    font-weight: 600;
    color: #34d399;
}

.model-desc {
    color: var(--text-secondary);
    margin-top: 0.25rem;
    font-size: 0.85rem;
}

/* User Management */
.user-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-bottom: 2rem;
}

.user-item {
    background: rgba(15, 23, 42, 0.5);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 1rem;
    display: grid;
    /* Use fixed widths for middle columns to ensure vertical alignment across rows */
    /* Increased last column for 4 buttons (SetBal, Toggle, Reset, Delete) */
    grid-template-columns: 1fr 80px 90px 90px 80px 160px;
    align-items: center;
    gap: 1rem;
}

.user-item .username {
    font-weight: 600;
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-item .role-badge {
    padding: 0.2rem 0.5rem;
    border-radius: 4px;
    font-size: 0.8rem;
    background: rgba(255, 255, 255, 0.1);
    text-align: center;
    display: inline-block;
}

/* User Action Buttons Styling */
.user-item .actions {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
}

.user-item .actions .btn-icon {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-sm);
    transition: all 0.2s;
    background: rgba(255, 255, 255, 0.05);
    /* Subtle background for visibility */
}

.user-item .actions .btn-icon:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: translateY(-1px);
}

.btn-icon.warn:hover {
    color: #fbbf24;
    /* Amber */
    background: rgba(251, 191, 36, 0.15) !important;
}

.user-item .role-badge.admin {
    background: rgba(139, 92, 246, 0.2);
    color: #a78bfa;
}

.user-form {
    display: grid;
    gap: 1rem;
    margin-bottom: 2rem;
    background: rgba(0, 0, 0, 0.2);
    padding: 1rem;
    border-radius: var(--radius-md);
}