* {
    box-sizing: border-box;
}

body {
    margin: 0;
    overflow: hidden;
    background: #222;
    font-family: sans-serif;
}

canvas {
    width: 100vw;
    height: 100vh;
    display: block;
    touch-action: none;
}

#ui {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    color: white;
    text-align: center;
    pointer-events: auto;
    width: 100%;
    max-width: 600px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.btn {
    width: 90px;
    height: 40px;
    pointer-events: auto;
    background: rgba(0, 0, 0, 0.2) none;
    appearance: none;
    -moz-appearance: none;
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: white;
    padding: 0 10px;
    border-radius: 5px;
    cursor: pointer;
    margin: 2px;
    backdrop-filter: blur(5px);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
}

button::-moz-focus-inner {
    border: 0;
    padding: 0;
}

@media (hover: hover) {
    .btn:hover {
        background: rgba(255, 255, 255, 0.2);
    }
}

.btn:active {
    background: rgba(255, 255, 255, 0.3);
}

#formula-wrapper {
    display: flex;
    flex: 1;
    position: relative;
    margin: 2px;
}

#formula {
    background: rgba(0, 0, 0, 0.2) none;
    appearance: none;
    -moz-appearance: none;
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: white;
    padding: 0 35px 0 10px;
    height: 40px;
    flex: 1;
    border-radius: 5px;
    outline: none;
    width: 100%;
    min-width: 25%;
    font-size: 14px;
    backdrop-filter: blur(5px);
    pointer-events: auto;
}

#help-btn {
    position: absolute;
    right: 10px;
    top: calc(50% - 2px);
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: white;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background 0.2s;
    z-index: 10;
}

@media (hover: hover) {
    #help-btn:hover {
        background: rgba(255, 255, 255, 0.3);
    }
}

#help-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(10px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50000;
}

#help-content {
    background: rgba(30, 30, 30, 0.8);
    border: 1px solid rgba(255, 255, 255, 0.2);
    padding: 30px;
    border-radius: 10px;
    max-width: 450px;
    width: 90%;
    position: relative;
    color: white;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

#help-close {
    position: absolute;
    top: 10px;
    right: 15px;
    cursor: pointer;
    font-size: 24px;
    opacity: 0.7;
    transition: opacity 0.2s;
}

@media (hover: hover) {
    #help-close:hover {
        opacity: 1;
    }
}

#help-content code {
    background: rgba(255, 255, 255, 0.1);
    padding: 2px 4px;
    border-radius: 3px;
    font-family: monospace;
    font-size: 0.9em;
}

#history {
    position: absolute;
    top: 0;
    right: 0;
    width: 300px;
    height: 100vh;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(5px);
    color: white;
    box-sizing: border-box;
    pointer-events: auto;
    display: flex;
    flex-direction: column;
    font-size: 14px;
    border-left: 1px solid rgba(255, 255, 255, 0.1);
    transition: transform 0.3s ease-in-out;
    z-index: 1000;
}

#audio-controls {
    padding: 15px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
}

.audio-toggle {
    display: flex;
    gap: 2px;
    align-items: center;
    cursor: pointer;
    white-space: nowrap;
}

.audio-toggle input[type='checkbox'] {
    cursor: pointer;
    width: 16px;
    height: 16px;
    accent-color: #4dabff;
}

#audio-controls label[for='audio-volume'] {
    white-space: nowrap;
}

#audio-volume {
    flex: 0.9;
    height: 4px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 2px;
    outline: none;
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
}

#audio-volume::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 14px;
    height: 14px;
    background: #4dabff;
    border-radius: 50%;
    cursor: pointer;
    transition: background 0.2s;
}

#audio-volume::-moz-range-thumb {
    width: 14px;
    height: 14px;
    background: #4dabff;
    border-radius: 50%;
    cursor: pointer;
    border: none;
    transition: background 0.2s;
}

@media (hover: hover) {
    #audio-volume::-webkit-slider-thumb:hover {
        background: #6bc0ff;
    }
    #audio-volume::-moz-range-thumb:hover {
        background: #6bc0ff;
    }
}

#audio-volume-value {
    text-align: right;
    font-size: 12px;
    color: #aaa;
}

#history-header-actions {
    padding: 15px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    justify-content: flex-start;
}

#clear-history-btn,
#reset-camera-btn {
    background-image: none;
    appearance: none;
    -moz-appearance: none;
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
    padding: 6px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    transition: background 0.2s;
}

#clear-history-btn {
    background: rgba(255, 50, 50, 0.2);
}

@media (hover: hover) {
    #clear-history-btn:hover {
        background: rgba(255, 50, 50, 0.4);
    }
}

#clear-history-btn:active {
    background: rgba(255, 50, 50, 0.6);
}

#reset-camera-btn {
    background: rgba(0, 0, 0, 0.2);
    margin-left: auto;
}

@media (hover: hover) {
    #reset-camera-btn:hover {
        background: rgba(255, 255, 255, 0.2);
    }
}

#reset-camera-btn:active {
    background: rgba(255, 255, 255, 0.3);
}

#history-list {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

#hamburger {
    display: none;
    position: absolute;
    top: 20px;
    right: 20px;
    z-index: 1001;
    cursor: pointer;
    color: white;
    background: rgba(0, 0, 0, 0.5) none;
    appearance: none;
    -moz-appearance: none;
    padding: 10px 15px;
    border-radius: 5px;
    backdrop-filter: blur(5px);
    font-size: 20px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    transition: transform 0.3s ease-in-out;
}

@media (hover: hover) {
    #hamburger:hover {
        background: rgba(255, 255, 255, 0.2);
    }
}

#hamburger:active {
    background: rgba(255, 255, 255, 0.3);
}

@media (max-width: 1200px) {
    #hamburger {
        display: block;
    }
    #history {
        transform: translateX(100%);
    }
    #history.open {
        transform: translateX(0);
    }
    body.history-open #hamburger {
        transform: translateX(-300px);
    }
}

@media (max-width: 800px) {
    #history {
        width: calc(100% - 15vw);
    }
    body.history-open #hamburger {
        transform: translateX(-85vw);
    }
    #hamburger {
        width: calc(min(10vw, 45px));
        height: calc(min(10vw, 45px));
        padding: 0;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}

#history-list::-webkit-scrollbar {
    width: 8px;
}

#history-list::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.2);
}

#history-list::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 4px;
}

@media (hover: hover) {
    #history-list::-webkit-scrollbar-thumb:hover {
        background: rgba(255, 255, 255, 0.3);
    }
}

.history-item {
    display: flex;
    flex-direction: column;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    padding-bottom: 12px;
    gap: 5px;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.history-result {
    font-size: 18px;
    font-weight: bold;
    color: #4dabff;
}

.history-breakdown {
    font-size: 13px;
    color: #aaa;
    word-wrap: break-word;
    font-style: italic;
}

.history-breakdown del {
    text-decoration: line-through;
    opacity: 0.5;
    margin: 0 1px;
}

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

.re-roll-btn {
    background: rgba(0, 0, 0, 0.2) none;
    appearance: none;
    -moz-appearance: none;
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 11px;
    align-self: flex-start;
}

@media (hover: hover) {
    .re-roll-btn:hover {
        background: rgba(255, 255, 255, 0.2);
    }
}

.re-roll-btn:active {
    background: rgba(255, 255, 255, 0.3);
}

.dice-button-row {
    display: flex;
    flex-direction: row;
    flex-basis: fit-content;
    flex-wrap: wrap;
    justify-content: space-between;
    width: 100%;
    margin-bottom: 5px;
}

.dice-button-row > .btn {
    width: 100px;
    white-space: nowrap;
}

.btn-danger {
    background: rgba(255, 50, 50, 0.3) !important;
}

.dice-btn-group {
    display: flex;
    margin: 2px;
    pointer-events: auto;
    width: calc(50% - 6px);
}

.dice-btn-group .btn {
    margin: 0;
}

.dice-btn-main {
    border-radius: 5px 0 0 5px !important;
}

.dice-btn-subs {
    display: flex;
    flex-direction: column;
    width: 55px;
}

.sub-btn {
    background: rgba(0, 0, 0, 0.2) none;
    appearance: none;
    -moz-appearance: none;
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: white;
    padding: 2px 4px;
    cursor: pointer;
    font-size: 10px;
    border-left: none;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(5px);
}

@media (hover: hover) {
    .sub-btn:hover {
        background: rgba(255, 255, 255, 0.2);
    }
}

.sub-btn:active {
    background: rgba(255, 255, 255, 0.3);
}

.sub-btn.adv {
    border-radius: 0 5px 0 0;
}

.sub-btn.dis {
    border-radius: 0 0 5px 0;
    border-top: none;
}

#error-message {
    background: rgba(220, 53, 69, 0.3);
    color: white;
    padding: 8px 16px;
    border-radius: 5px;
    margin-bottom: 10px;
    font-size: 14px;
    font-weight: bold;
    backdrop-filter: blur(5px);
    border: 1px solid rgba(255, 255, 255, 0.2);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    z-index: 2000;
}

#webgl-error {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(220, 53, 69, 0.9);
    color: white;
    padding: 40px;
    border-radius: 10px;
    text-align: center;
    z-index: 99999;
    font-size: 20px;
    max-width: 80%;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.attribution {
    position: absolute;
    right: 10px;
    bottom: 10px;
    color: dimgray;
    font-size: 12px;
    z-index: 10000;
}

.github-link {
    position: absolute;
    left: 5px;
    bottom: 5px;
    opacity: 0.8;
    z-index: 10000;
}

.github-link img {
    width: 28px;
    height: 28px;
}

.visually-hidden {
    border: 0;
    padding: 0;
    margin: 0;
    position: absolute !important;
    height: 1px;
    width: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}
