:root {

    --history-primary: #0062ff;
    --history-bg: var(--bg-primary, #ffffff);
    --history-bg-secondary: var(--bg-secondary, #f9fafb);
    --history-border: var(--gray-200, #e5e7eb);
    --history-text: var(--text-primary, #111827);
    --history-text-secondary: var(--text-secondary, #4b5563);
    --history-shadow: var(--shadow-lg, 0 10px 15px -3px rgba(0, 0, 0, 0.1));

    --history-transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    --history-transition-slow: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

    --history-btn-size: 56px;
    --history-panel-width: 400px;
    --history-panel-width-mobile: 100vw;
    --history-panel-height-mobile: 100vh;
    --history-z-index: 1000;
}

[data-theme="dark"] {
    --history-bg: var(--gray-800, #1f2937);
    --history-bg-secondary: var(--gray-700, #374151);
    --history-border: var(--gray-600, #4b5563);
    --history-text: var(#f3f4f6);
    --history-text-secondary: var(#d1d5db);
    --history-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3);
}

.history-btn {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    z-index: calc(var(--history-z-index) + 10);

    width: var(--history-btn-size);
    height: var(--history-btn-size);

    background: var(--history-primary);
    border: none;
    border-radius: 50%;
    box-shadow: var(--history-shadow);
    color: white;
    cursor: pointer;

    display: flex;
    align-items: center;
    justify-content: center;

    transition: var(--history-transition);
    transform: scale(1);

    font-family: var(--font-family);
    font-weight: var(--font-weight-medium);
    
    outline: none;
    -webkit-tap-highlight-color: transparent;
}

.history-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.15);
}

.history-btn:active {
    transform: scale(0.95);
}

.history-btn:focus-visible {
    outline: 2px solid var(--history-primary);
    outline-offset: 2px;
}

.history-btn.active {
    background: var(--brand-accent, #8b5cf6);
    transform: scale(1.1);
}

.history-btn__icon {
    width: 24px;
    height: 24px;
    transition: var(--history-transition);
}

.history-btn.active .history-btn__icon {
    transform: rotate(180deg);
}

.history-btn__badge {
    position: absolute;
    top: -6px;
    right: -6px;
    
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    
    background: var(--error, #ef4444);
    border: 2px solid var(--history-bg);
    border-radius: 10px;
    color: white;
    
    display: none;
    align-items: center;
    justify-content: center;
    
    font-size: var(--text-xs);
    font-weight: var(--font-weight-bold);
    line-height: 1;
}

.history-panel {
    position: fixed;
    z-index: var(--history-z-index);

    background: var(--history-bg);
    border: 1px solid var(--history-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--history-shadow);

    display: flex;
    flex-direction: column;

    opacity: 0;
    visibility: hidden;
    transform: translateY(20px) scale(0.95);
    transition: var(--history-transition-slow);

    font-family: var(--font-family);
    color: var(--history-text);
}

.history-panel.visible {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

.history-panel--desktop {
    bottom: 5rem;
    right: 2rem;
    width: var(--history-panel-width);
    max-height: 600px;
}

.history-panel--mobile {
    top: 0;
    left: 0;
    width: var(--history-panel-width-mobile);
    height: var(--history-panel-height-mobile);
    border-radius: 0;
    border: none;
    
    transform: translateY(100%);
}

.history-panel--mobile.visible {
    transform: translateY(0);
}

@media (max-width: 768px) {
  .history-panel--mobile {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    width: 100vw;
    position: fixed;
    top: 0;
    left: 0;
    z-index: calc(var(--history-z-index) + 1);
    background: var(--history-bg);
    overflow: hidden;
  }
  
  .history-content {
    flex: 1 1 auto;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    max-height: calc(100vh - 200px);
    max-height: calc(100dvh - 200px);
    padding-bottom: 1rem;
    margin-bottom: 0;
  }
  
  .history-footer {
    flex-shrink: 0;
    position: sticky;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--history-bg);
    z-index: 999;
    padding: 1.25rem 1rem;
    padding-bottom: max(1.25rem, calc(env(safe-area-inset-bottom) + 1rem));
    box-shadow: 0 -4px 12px rgba(0,0,0,0.08);
    border-top: 2px solid var(--history-border);
    display: flex !important;
    flex-direction: column;
    gap: 0.75rem;
    min-height: 140px;
    max-height: 200px;
  }
  
  .history-export,
  .history-clear-all {
    flex: none;
    width: 100%;
    min-height: 52px;
    margin: 0;
    display: flex !important;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    font-size: 1rem;
    font-weight: 600;
    border-radius: 10px;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }
}

.history-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.5rem 1.5rem 1rem;
    border-bottom: 1px solid var(--history-border);
}

.history-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0;
    font-size: var(--text-lg);
    font-weight: var(--font-weight-semibold);
    color: var(--history-text);
}

.history-title__icon {
    width: 20px;
    height: 20px;
    color: var(--history-primary);
}

.history-close {
    width: 32px;
    height: 32px;
    background: transparent;
    border: none;
    border-radius: var(--radius-base);
    color: var(--history-text-secondary);
    cursor: pointer;
    
    display: flex;
    align-items: center;
    justify-content: center;
    
    transition: var(--history-transition);
}

.history-close:hover {
    background: var(--history-bg-secondary);
    color: var(--history-text);
}

.history-close svg {
    width: 18px;
    height: 18px;
}

.history-search {
    position: relative;
    padding: 0 1.5rem 1rem;
}

.history-search__icon {
    position: absolute;
    left: 2rem;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    color: var(--history-text-secondary);
    pointer-events: none;
}

.history-search__input {
    width: 100%;
    padding: 0.75rem 1rem 0.75rem 2.5rem;
    
    background: var(--history-bg-secondary);
    border: 1px solid var(--history-border);
    border-radius: var(--radius-base);
    color: var(--history-text);
    
    font-family: inherit;
    font-size: var(--text-sm);
    
    transition: var(--history-transition);
}

.history-search__input:focus {
    outline: none;
    border-color: var(--history-primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.history-search__input::placeholder {
    color: var(--history-text-secondary);
}

.history-content {
    flex: 1;
    overflow-y: auto;
    padding: 0 1.5rem;
    
    scrollbar-width: thin;
    scrollbar-color: var(--history-border) transparent;
}

.history-content::-webkit-scrollbar {
    width: 6px;
}

.history-content::-webkit-scrollbar-track {
    background: transparent;
}

.history-content::-webkit-scrollbar-thumb {
    background: var(--history-border);
    border-radius: 3px;
}

.history-content::-webkit-scrollbar-thumb:hover {
    background: var(--gray-400);
}

.history-records {
    min-height: 200px;
}

.history-group {
    margin-bottom: 1.5rem;
}

.history-group:last-child {
    margin-bottom: 0;
}

.history-group__title {
    margin: 0 0 0.75rem;
    font-size: var(--text-sm);
    font-weight: var(--font-weight-semibold);
    color: var(--history-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.025em;
}

.history-group__records {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.history-record {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
    padding: 1rem;
    
    background: var(--history-bg);
    border: 1px solid var(--history-border);
    border-radius: var(--radius-base);
    
    transition: var(--history-transition);
    cursor: pointer;
}

.history-record:hover {
    border-color: var(--history-primary);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.history-record--starred {
    border-color: var(--warning, #f59e0b);
    background: linear-gradient(135deg, rgba(245, 158, 11, 0.05) 0%, transparent 100%);
}

.history-record__content {
    width: 100%;
}

.history-record__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 0.5rem;
}

.history-record__tool {
    font-size: var(--text-xs);
    font-weight: var(--font-weight-medium);
    color: var(--history-primary);
    text-transform: uppercase;
    letter-spacing: 0.025em;
}

.history-record__time {
    font-size: var(--text-xs);
    color: var(--history-text-secondary);
}

.history-record__details {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.history-record__inputs {
    font-size: var(--text-sm);
    color: var(--history-text-secondary);
    line-height: 1.4;
    
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.history-record__result {
    font-size: var(--text-base);
    font-weight: var(--font-weight-medium);
    color: var(--history-text);
    line-height: 1.4;
    word-break: break-word;
}


.history-record__expanded-inputs {
    width: 100%;
    margin-top: 0.75rem;
    padding: 1rem 1.25rem;
    background: var(--history-bg-secondary, #f9fafb);
    border-radius: var(--radius-base);
    border: 1px solid var(--history-border);
    color: var(--history-text-secondary);
    font-size: var(--text-sm);
    box-sizing: border-box;
    box-shadow: 0 1px 4px rgba(0,0,0,0.04);
    position: static;
    display: block;

}

.history-record__expanded-inputs ul {
    margin: 0;
    padding: 0 0 0 1.2em;
    list-style: disc inside;
}

.history-record__expanded-inputs li {
    margin-bottom: 0.25em;
    word-break: break-word;
}

.history-record__collapse {
    display: block;
    margin: 0.75em auto 0 auto;
}

.history-record__expanded-inputs {
    transition: max-height 0.2s ease, opacity 0.2s ease;
    overflow: hidden;
}

.history-record__actions {
    display: flex;
    flex-direction: row;
    gap: 0.5rem;
    margin-top: 0.5rem;
    align-items: center;
}

.history-record__action {
    width: 32px;
    height: 32px;
    
    background: transparent;
    border: 1px solid var(--history-border);
    border-radius: var(--radius-base);
    color: var(--history-text-secondary);
    cursor: pointer;
    
    display: flex;
    align-items: center;
    justify-content: center;
    
    transition: var(--history-transition);
}

.history-record__action:hover {
    background: var(--history-bg-secondary);
    border-color: var(--history-text-secondary);
    color: var(--history-text);
}

.history-record__action svg {
    width: 16px;
    height: 16px;
}

.history-record__star {
    color: var(--warning, #f59e0b);
}

.history-record__star:hover {
    background: rgba(245, 158, 11, 0.1);
    border-color: var(--warning, #f59e0b);
}

.history-record__delete {
    color: var(--error, #ef4444);
}

.history-record__delete:hover {
    background: rgba(239, 68, 68, 0.1);
    border-color: var(--error, #ef4444);
}

.history-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 1rem;
    text-align: center;
}

.history-empty__icon {
    width: 48px;
    height: 48px;
    color: var(--history-text-secondary);
    margin-bottom: 1rem;
}

.history-empty__text {
    margin: 0 0 0.5rem;
    font-size: var(--text-lg);
    font-weight: var(--font-weight-medium);
    color: var(--history-text);
}

.history-empty__subtext {
    margin: 0;
    font-size: var(--text-sm);
    color: var(--history-text-secondary);
}

.history-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 1rem 1.5rem 1.5rem;
    border-top: 1px solid var(--history-border);
}

.history-export,
.history-clear-all {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    
    border: 1px solid var(--history-border);
    border-radius: var(--radius-base);
    background: transparent;
    color: var(--history-text);
    
    font-family: inherit;
    font-size: var(--text-sm);
    font-weight: var(--font-weight-medium);
    cursor: pointer;
    
    transition: var(--history-transition);
}

.history-export:hover {
    background: var(--history-bg-secondary);
    border-color: var(--history-primary);
    color: var(--history-primary);
}

.history-clear-all:hover {
    background: rgba(239, 68, 68, 0.1);
    border-color: var(--error, #ef4444);
    color: var(--error, #ef4444);
}

.history-export svg,
.history-clear-all svg {
    width: 16px;
    height: 16px;
}

.history-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: calc(var(--history-z-index) - 1);
    
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    
    opacity: 0;
    visibility: hidden;
    transition: var(--history-transition-slow);
}

.history-overlay.visible {
    opacity: 1;
    visibility: visible;
}

.history-dialog-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: calc(var(--history-z-index) + 20);
    
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.history-dialog {
    width: 100%;
    max-width: 400px;
    
    background: var(--history-bg);
    border-radius: var(--radius-lg);
    box-shadow: var(--history-shadow);
    
    animation: dialogSlideIn 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes dialogSlideIn {
    from {
        opacity: 0;
        transform: translateY(-20px) scale(0.95);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.history-dialog__header {
    padding: 1.5rem 1.5rem 1rem;
    border-bottom: 1px solid var(--history-border);
}

.history-dialog__title {
    margin: 0;
    font-size: var(--text-lg);
    font-weight: var(--font-weight-semibold);
    color: var(--history-text);
}

.history-dialog__body {
    padding: 1rem 1.5rem;
}

.history-dialog__message {
    margin: 0;
    font-size: var(--text-base);
    color: var(--history-text);
    line-height: 1.5;
}

.history-dialog__options {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
    margin-top: 1rem;
}

.history-dialog__option {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
    padding: 1rem;
    
    background: var(--history-bg-secondary);
    border: 1px solid var(--history-border);
    border-radius: var(--radius-base);
    color: var(--history-text);
    cursor: pointer;
    
    font-family: inherit;
    text-align: center;
    
    transition: var(--history-transition);
}

.history-dialog__option:hover {
    background: var(--history-primary);
    border-color: var(--history-primary);
    color: white;
}

.history-dialog__option svg {
    width: 24px;
    height: 24px;
}

.history-dialog__option span {
    font-size: var(--text-sm);
    font-weight: var(--font-weight-medium);
}

.history-dialog__option small {
    font-size: var(--text-xs);
    opacity: 0.8;
}

.history-dialog__footer {
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
    padding: 1rem 1.5rem 1.5rem;
}

.history-dialog__btn {
    padding: 0.5rem 1rem;
    
    border: 1px solid var(--history-border);
    border-radius: var(--radius-base);
    background: transparent;
    color: var(--history-text);
    
    font-family: inherit;
    font-size: var(--text-sm);
    font-weight: var(--font-weight-medium);
    cursor: pointer;
    
    transition: var(--history-transition);
}

.history-dialog__btn--secondary:hover {
    background: var(--history-bg-secondary);
}

.history-dialog__btn--danger {
    background: var(--error, #ef4444);
    border-color: var(--error, #ef4444);
    color: white;
}

.history-dialog__btn--danger:hover {
    background: #dc2626;
    border-color: #dc2626;
}

@media (max-width: 1024px) {
    .history-panel--desktop {
        width: 350px;
        max-height: 500px;
    }
}

@media (max-width: 768px) {
    .history-btn {
        bottom: 1.5rem;
        right: 1.5rem;
        width: 62px;
        height: 62px;
    }
    
    .history-btn__icon {
        width: 20px;
        height: 20px;
    }
    
    .history-panel--mobile .history-header {
        padding: 1rem 1rem 0.75rem;
    }
    
    .history-panel--mobile .history-search {
        padding: 0 1rem 0.75rem;
    }
    
    .history-panel--mobile .history-content {
        padding: 0 1rem;
    }
    
    .history-panel--mobile .history-footer {
        padding: 0.75rem 1rem 1rem;
    }
    
    .history-record {
        padding: 0.75rem;
    }
    
    .history-record__actions {
        flex-direction: row;
    }
    
    .history-dialog {
        margin: 1rem;
    }
    
    .history-dialog__options {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 480px) {
    .history-btn {
        bottom: 1rem;
        right: 1rem;
        width: 44px;
        height: 44px;
    }
    
    .history-btn__icon {
        width: 18px;
        height: 18px;
    }
    
    .history-footer {
        flex-direction: column !important;
        gap: 0.75rem !important;
        padding: 1rem !important;
        padding-bottom: max(1rem, calc(env(safe-area-inset-bottom) + 0.5rem)) !important;
        min-height: 120px !important;
        max-height: 180px !important;
        overflow: visible !important;
    }
    
    .history-export,
    .history-clear-all {
        width: 100% !important;
        flex: none !important;
        min-height: 50px !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        font-size: 0.9rem !important;
        padding: 0.875rem 1rem !important;
        border-radius: 8px !important;
        margin: 0 !important;
        white-space: nowrap !important;
    }
}

@media (max-width: 360px) {
    .history-panel--mobile {
        height: 100vh !important;
        height: 100dvh !important;
    }
    
    .history-footer {
        padding: 0.75rem !important;
        min-height: 100px !important;
        max-height: 150px !important;
        gap: 0.5rem !important;
    }
    
    .history-export,
    .history-clear-all {
        min-height: 44px !important;
        padding: 0.625rem 0.75rem !important;
        font-size: 0.85rem !important;
    }
    
    .history-export svg,
    .history-clear-all svg {
        width: 14px !important;
        height: 14px !important;
    }
}
@media (max-width: 768px) {
    .history-panel--mobile .history-footer {
        position: sticky !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        background: var(--history-bg) !important;
        border-top: 1px solid var(--history-border) !important;
        z-index: 1000 !important;
        flex-shrink: 0 !important;
        box-sizing: border-box !important;
    }
    
    .history-panel--mobile .history-export,
    .history-panel--mobile .history-clear-all {
        display: flex !important;
        visibility: visible !important;
        opacity: 1 !important;
        position: relative !important;
        flex: none !important;
        width: 100% !important;
        min-height: 48px !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }
    
    .history-panel--mobile .history-content {
        padding-bottom: 120px !important;
        max-height: calc(100vh - 200px) !important;
    }
}

@media (max-width: 480px) {
    .history-panel--mobile .history-footer {
        padding: 0.75rem !important;
        gap: 0.75rem !important;
    }
    
    .history-panel--mobile .history-export,
    .history-panel--mobile .history-clear-all {
        min-height: 44px !important;
        font-size: 14px !important;
    }
}
