/* ===== CSS Reset & Base Styles ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #4CAF50;
    --primary-dark: #388E3C;
    --primary-light: #81C784;
    --background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    --dice-bg: #ffffff;
    --dice-dot: #222222;
    --text-primary: #ffffff;
    --text-secondary: #f0f0f0;
    --shadow: rgba(0, 0, 0, 0.3);
    --dice-shadow: rgba(0, 0, 0, 0.2);
}

/* Kindle Mode Variables - Black and White Only */
html.kindle-mode {
    --primary-color: #000000;
    --primary-dark: #000000;
    --primary-light: #333333;
    --background: #ffffff;
    --dice-bg: #ffffff;
    --dice-dot: #000000;
    --text-primary: #000000;
    --text-secondary: #333333;
    --shadow: rgba(0, 0, 0, 0.1);
    --dice-shadow: rgba(0, 0, 0, 0.15);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: var(--background);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    color: var(--text-primary);
}

/* Kindle Mode: Override background and colors */
html.kindle-mode body {
    background: var(--background);
    color: var(--text-primary);
}

/* ===== Container ===== */
.container {
    width: 100%;
    max-width: 600px;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    border-radius: 24px;
    padding: 30px;
    box-shadow: 0 8px 32px var(--shadow);
}

/* Kindle Mode: Simplified container */
html.kindle-mode .container {
    background: #ffffff;
    backdrop-filter: none;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    border: 2px solid #000000;
}

/* ===== Header ===== */
header {
    text-align: center;
    margin-bottom: 30px;
}

header h1 {
    font-size: 2.5rem;
    font-weight: 700;
    text-shadow: 2px 2px 4px var(--shadow);
}

/* ===== Main Content Area ===== */
main {
    margin-bottom: 30px;
}

/* ===== Dice Container ===== */
.dice-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 20px;
    min-height: 200px;
    padding: 20px;
    margin-bottom: 20px;
    perspective: 1000px; /* Add 3D perspective */
}

/* ===== Individual Die ===== */
.die {
    width: 80px;
    height: 80px;
    position: relative;
}

/* 2D Die (Classic flat style) */
.die.die-2d {
    background: var(--dice-bg);
    border-radius: 12px;
    box-shadow: 0 4px 8px var(--dice-shadow);
    display: grid;
    grid-template-areas:
        "a . c"
        "e g f"
        "d . b";
    padding: 10px;
    gap: 5px;
}

/* 3D Die (Cube with 6 faces) */
.die.die-3d {
    transform-style: preserve-3d;
    transition: transform 0.3s ease-out;
}

/* Each face of the 3D cube */
.die.die-3d .face {
    position: absolute;
    width: 80px;
    height: 80px;
    background: var(--dice-bg);
    border-radius: 8px;
    box-shadow: inset 0 0 0 2px rgba(0, 0, 0, 0.1);
    display: grid;
    grid-template-areas:
        "a . c"
        "e g f"
        "d . b";
    padding: 10px;
    gap: 5px;
    backface-visibility: hidden;
}

/* Position each face in 3D space */
.die.die-3d .face.front  { transform: translateZ(40px); }
.die.die-3d .face.back   { transform: rotateY(180deg) translateZ(40px); }
.die.die-3d .face.right  { transform: rotateY(90deg) translateZ(40px); }
.die.die-3d .face.left   { transform: rotateY(-90deg) translateZ(40px); }
.die.die-3d .face.top    { transform: rotateX(90deg) translateZ(40px); }
.die.die-3d .face.bottom { transform: rotateX(-90deg) translateZ(40px); }

/* Die dot */
.dot {
    width: 14px;
    height: 14px;
    background: var(--dice-dot);
    border-radius: 50%;
    align-self: center;
    justify-self: center;
}

/* Dot positioning based on grid areas */
.dot:nth-child(1) { grid-area: a; }
.dot:nth-child(2) { grid-area: b; }
.dot:nth-child(3) { grid-area: c; }
.dot:nth-child(4) { grid-area: d; }
.dot:nth-child(5) { grid-area: e; }
.dot:nth-child(6) { grid-area: f; }
.dot:nth-child(7) { grid-area: g; }

/* ===== Roll Animation ===== */
/* 2D roll animation */
@keyframes roll2d {
    0% {
        transform: rotate(0deg) scale(1);
    }
    25% {
        transform: rotate(90deg) scale(1.05);
    }
    50% {
        transform: rotate(180deg) scale(1);
    }
    75% {
        transform: rotate(270deg) scale(1.05);
    }
    100% {
        transform: rotate(360deg) scale(1);
    }
}

/* 3D roll animation */
@keyframes roll3d {
    0% {
        transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg) scale(1);
    }
    25% {
        transform: rotateX(180deg) rotateY(180deg) rotateZ(90deg) scale(1.1);
    }
    50% {
        transform: rotateX(360deg) rotateY(360deg) rotateZ(180deg) scale(1);
    }
    75% {
        transform: rotateX(540deg) rotateY(540deg) rotateZ(270deg) scale(1.1);
    }
    100% {
        transform: rotateX(720deg) rotateY(720deg) rotateZ(360deg) scale(1);
    }
}

@keyframes shake {
    0%, 100% {
        transform: translateY(0) translateX(0) translateZ(0);
    }
    10% {
        transform: translateY(-5px) translateX(3px) translateZ(10px);
    }
    20% {
        transform: translateY(5px) translateX(-3px) translateZ(-10px);
    }
    30% {
        transform: translateY(-4px) translateX(-2px) translateZ(8px);
    }
    40% {
        transform: translateY(4px) translateX(2px) translateZ(-8px);
    }
    50% {
        transform: translateY(-3px) translateX(-2px) translateZ(5px);
    }
    60% {
        transform: translateY(3px) translateX(2px) translateZ(-5px);
    }
    70% {
        transform: translateY(-2px) translateX(-1px) translateZ(3px);
    }
    80% {
        transform: translateY(2px) translateX(1px) translateZ(-3px);
    }
    90% {
        transform: translateY(-1px) translateX(0) translateZ(2px);
    }
}

@keyframes settleRotate {
    0% {
        transform: var(--final-rotation) rotateX(180deg) rotateY(180deg) translateY(-20px) scale(1.15);
        animation-timing-function: ease-out;
    }
    15% {
        transform: var(--final-rotation) rotateX(120deg) rotateY(120deg) translateY(0) scale(1);
        animation-timing-function: ease-in;
    }
    30% {
        transform: var(--final-rotation) rotateX(80deg) rotateY(80deg) translateY(-10px) scale(1.08);
        animation-timing-function: ease-out;
    }
    45% {
        transform: var(--final-rotation) rotateX(40deg) rotateY(40deg) translateY(0) scale(1);
        animation-timing-function: ease-in;
    }
    60% {
        transform: var(--final-rotation) rotateX(20deg) rotateY(20deg) translateY(-5px) scale(1.04);
        animation-timing-function: ease-out;
    }
    75% {
        transform: var(--final-rotation) rotateX(10deg) rotateY(10deg) translateY(0) scale(1);
        animation-timing-function: ease-in;
    }
    85% {
        transform: var(--final-rotation) rotateX(5deg) rotateY(5deg) translateY(-2px) scale(1.02);
        animation-timing-function: ease-out;
    }
    100% {
        transform: var(--final-rotation) rotateX(0deg) rotateY(0deg) translateY(0) scale(1);
    }
}

.die.die-2d.rolling {
    animation: roll2d 0.3s linear infinite, shake 0.6s ease-in-out;
}

.die.die-3d.rolling {
    animation: roll3d 0.15s ease-in-out infinite, shake 0.3s ease-in-out;
    filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.4));
}

.die.die-3d.settling {
    animation: settleRotate 0.8s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* ===== Kindle Mode: Disable ALL Animations ===== */
html.kindle-mode * {
    animation: none !important;
    transition: none !important;
}

html.kindle-mode .die.die-2d.rolling,
html.kindle-mode .die.die-3d.rolling,
html.kindle-mode .die.die-3d.settling {
    animation: none !important;
}

html.kindle-mode .container {
    animation: none !important;
}

/* ===== Total Display ===== */
.total-container {
    text-align: center;
    padding: 20px;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 16px;
    margin-bottom: 20px;
}

.total-label {
    font-size: 1.5rem;
    font-weight: 600;
    margin-right: 10px;
}

.total-value {
    font-size: 3rem;
    font-weight: 700;
    text-shadow: 2px 2px 4px var(--shadow);
}

/* ===== Controls ===== */
.controls {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* ===== Dice Counter ===== */
.dice-counter {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 20px;
    padding: 20px;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 16px;
}

.counter-btn {
    width: 50px;
    height: 50px;
    font-size: 2rem;
    font-weight: 700;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 8px var(--shadow);
    display: flex;
    align-items: center;
    justify-content: center;
}

.counter-btn:hover:not(:disabled) {
    background: var(--primary-dark);
    transform: scale(1.1);
}

.counter-btn:active:not(:disabled) {
    transform: scale(0.95);
}

.counter-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.dice-count-display {
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 80px;
}

.dice-count {
    font-size: 2.5rem;
    font-weight: 700;
    text-shadow: 2px 2px 4px var(--shadow);
}

.dice-label {
    font-size: 1rem;
    font-weight: 500;
    opacity: 0.9;
}

/* ===== Mode Toggles Row ===== */
.mode-toggles-row {
    display: flex;
    gap: 15px;
}

/* ===== 3D Mode Toggle ===== */
.mode-toggle {
    display: flex;
    flex: 1;
    justify-content: center;
    align-items: center;
    padding: 15px 20px;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 16px;
}

.toggle-label {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    user-select: none;
}

.toggle-label input[type="checkbox"] {
    width: 50px;
    height: 26px;
    appearance: none;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 13px;
    position: relative;
    cursor: pointer;
    transition: background 0.3s ease;
    outline: none;
}

.toggle-label input[type="checkbox"]:checked {
    background: var(--primary-color);
}

.toggle-label input[type="checkbox"]::before {
    content: '';
    position: absolute;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: white;
    top: 2px;
    left: 2px;
    transition: transform 0.3s ease;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.toggle-label input[type="checkbox"]:checked::before {
    transform: translateX(24px);
}

.toggle-label input[type="checkbox"]:focus {
    outline: 3px solid rgba(255, 255, 255, 0.5);
    outline-offset: 2px;
}

.toggle-text {
    font-size: 1.1rem;
    font-weight: 600;
    text-shadow: 1px 1px 2px var(--shadow);
}

/* ===== Roll Button ===== */
.roll-btn {
    width: 100%;
    padding: 20px;
    font-size: 1.5rem;
    font-weight: 700;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 16px;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 6px 12px var(--shadow);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.roll-btn:hover:not(:disabled) {
    background: var(--primary-dark);
    transform: translateY(-2px);
    box-shadow: 0 8px 16px var(--shadow);
}

.roll-btn:active:not(:disabled) {
    transform: translateY(0);
    box-shadow: 0 4px 8px var(--shadow);
}

.roll-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* ===== Footer ===== */
footer {
    text-align: center;
    margin-top: 20px;
    padding: 10px;
}

/* Install Button */
.install-btn {
    padding: 12px 24px;
    font-size: 1rem;
    font-weight: 600;
    background: rgba(255, 255, 255, 0.2);
    color: var(--text-primary);
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.install-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.5);
    transform: translateY(-2px);
}

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

.install-btn.hidden {
    display: none;
}

/* Install Guide Modal */
.install-guide {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    color: #333;
    padding: 30px;
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    max-width: 90%;
    width: 400px;
    z-index: 1000;
}

.install-guide h3 {
    margin: 0 0 20px 0;
    font-size: 1.5rem;
    color: #333;
}

.install-guide ol {
    text-align: left;
    margin: 0 0 20px 20px;
    line-height: 1.8;
}

.install-guide li {
    margin-bottom: 10px;
    font-size: 1rem;
}

.ios-icon {
    display: inline-block;
    font-size: 1.2rem;
    font-weight: bold;
}

.close-guide-btn {
    padding: 10px 20px;
    font-size: 1rem;
    font-weight: 600;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
}

.close-guide-btn:hover {
    background: var(--primary-dark);
}

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

/* Kindle Mode: Install Button */
html.kindle-mode .install-btn {
    background: #ffffff;
    color: #000000;
    border: 2px solid #000000;
}

html.kindle-mode .install-btn:hover {
    background: #f0f0f0;
}

html.kindle-mode .install-guide {
    background: #ffffff;
    border: 2px solid #000000;
}

html.kindle-mode .close-guide-btn {
    background: #000000;
    color: #ffffff;
}

/* ===== Responsive Design ===== */

/* Mobile (default styles above) */

/* Tablet */
@media (min-width: 768px) {
    .container {
        padding: 40px;
    }

    header h1 {
        font-size: 3rem;
    }

    .die {
        width: 90px;
        height: 90px;
    }

    .die.die-3d .face {
        width: 90px;
        height: 90px;
    }

    .die.die-3d .face.front  { transform: translateZ(45px); }
    .die.die-3d .face.back   { transform: rotateY(180deg) translateZ(45px); }
    .die.die-3d .face.right  { transform: rotateY(90deg) translateZ(45px); }
    .die.die-3d .face.left   { transform: rotateY(-90deg) translateZ(45px); }
    .die.die-3d .face.top    { transform: rotateX(90deg) translateZ(45px); }
    .die.die-3d .face.bottom { transform: rotateX(-90deg) translateZ(45px); }

    .dot {
        width: 16px;
        height: 16px;
    }

    .total-value {
        font-size: 3.5rem;
    }
}

/* Desktop */
@media (min-width: 1024px) {
    .container {
        padding: 50px;
    }

    header h1 {
        font-size: 3.5rem;
    }

    .die {
        width: 100px;
        height: 100px;
    }

    .die.die-3d .face {
        width: 100px;
        height: 100px;
    }

    .die.die-3d .face.front  { transform: translateZ(50px); }
    .die.die-3d .face.back   { transform: rotateY(180deg) translateZ(50px); }
    .die.die-3d .face.right  { transform: rotateY(90deg) translateZ(50px); }
    .die.die-3d .face.left   { transform: rotateY(-90deg) translateZ(50px); }
    .die.die-3d .face.top    { transform: rotateX(90deg) translateZ(50px); }
    .die.die-3d .face.bottom { transform: rotateX(-90deg) translateZ(50px); }

    .dot {
        width: 18px;
        height: 18px;
    }

    .dice-container {
        min-height: 250px;
    }

    .roll-btn {
        font-size: 1.75rem;
        padding: 24px;
    }
}

/* ===== Accessibility ===== */
.counter-btn:focus,
.roll-btn:focus {
    outline: 3px solid rgba(255, 255, 255, 0.5);
    outline-offset: 2px;
}

/* ===== Dark Mode Support (Optional) ===== */
@media (prefers-color-scheme: dark) {
    /* Already dark by default, but can add adjustments if needed */
}

/* ===== Print Styles ===== */
@media print {
    body {
        background: white;
        color: black;
    }

    .container {
        box-shadow: none;
        background: white;
    }

    .roll-btn,
    .controls {
        display: none;
    }
}

/* ===== Animation for initial load ===== */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.container {
    animation: fadeIn 0.5s ease-out;
}

/* ===== Kindle Mode: Black and White Dice ===== */
html.kindle-mode .die.die-2d,
html.kindle-mode .die.die-3d .face {
    background: #ffffff;
    border: 2px solid #000000;
    box-shadow: none;
}

html.kindle-mode .dot {
    background: #000000;
}

html.kindle-mode .counter-btn,
html.kindle-mode .roll-btn {
    background: #000000;
    color: #ffffff;
    box-shadow: none;
    border: 2px solid #000000;
}

html.kindle-mode .counter-btn:hover:not(:disabled),
html.kindle-mode .roll-btn:hover:not(:disabled) {
    background: #333333;
    transform: none;
    box-shadow: none;
}

html.kindle-mode .counter-btn:active:not(:disabled),
html.kindle-mode .roll-btn:active:not(:disabled) {
    transform: none;
    box-shadow: none;
}

html.kindle-mode .total-container,
html.kindle-mode .dice-counter,
html.kindle-mode .mode-toggle {
    background: #ffffff;
    border: 1px solid #000000;
}

html.kindle-mode .toggle-label input[type="checkbox"] {
    background: #ffffff;
    border: 2px solid #000000;
}

html.kindle-mode .toggle-label input[type="checkbox"]:checked {
    background: #000000;
}

html.kindle-mode .toggle-label input[type="checkbox"]::before {
    background: #000000;
}

html.kindle-mode .toggle-label input[type="checkbox"]:checked::before {
    background: #ffffff;
}

/* ===== Windows High Contrast Mode Support ===== */
/* Modern standard for forced colors mode (Windows High Contrast) */
@media (forced-colors: active) {
    .die.die-2d {
        /* Add visible border using system colors */
        border: 3px solid CanvasText;
        background: Canvas;
        /* Remove shadow as it won't be visible */
        box-shadow: none;
        /* Force background to ensure visibility */
        forced-color-adjust: none;
        background-color: Canvas;
    }

    .die.die-3d .face {
        /* Add visible border using system colors */
        border: 3px solid CanvasText;
        background: Canvas;
        /* Remove shadow as it won't be visible */
        box-shadow: none;
        /* Force background to ensure visibility */
        forced-color-adjust: none;
        background-color: Canvas;
    }

    .dot {
        /* Use system text color for dots */
        background: CanvasText;
        /* Ensure dots are visible */
        forced-color-adjust: none;
        background-color: CanvasText;
    }

    /* Ensure buttons are visible */
    .counter-btn,
    .roll-btn {
        border: 2px solid ButtonText;
        forced-color-adjust: auto;
    }

    /* Ensure containers have borders */
    .total-container,
    .dice-counter {
        border: 2px solid CanvasText;
        background: Canvas;
    }

    .container {
        border: 2px solid CanvasText;
        background: Canvas;
    }
}

/* Legacy support for older IE/Edge high contrast mode */
@media (-ms-high-contrast: active) {
    .die.die-2d {
        border: 3px solid windowText;
        background: window;
        box-shadow: none;
    }

    .die.die-3d .face {
        border: 3px solid windowText;
        background: window;
        box-shadow: none;
    }

    .dot {
        background: windowText;
    }

    .counter-btn,
    .roll-btn {
        border: 2px solid buttonText;
    }

    .total-container,
    .dice-counter,
    .container {
        border: 2px solid windowText;
        background: window;
    }
}
