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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background-color: #1a1a1a;
    color: #ffffff;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    overflow-x: hidden;
}

/* Main Layout */
.main-layout {
    width: 100%;
    max-width: 100%;
    position: relative;
}

/* Mobile: prevent scrolling and constrain board size */
@media (max-width: 767px) {
    body {
        height: 100vh;
        overflow-x: hidden;
        overflow-y: auto;
    }

    .container {
        max-height: calc(100vh - 2rem);
        overflow-y: visible;
        max-width: calc(100vw - 2rem);
    }

    #board {
        max-width: 100%;
        width: 100%;
    }
}

/* Container */
.container {
    width: 100%;
    max-width: 500px;
    margin: 0 auto;
}

/* Moves Panel - Hidden on mobile */
.moves-panel {
    display: none;
}

@media (min-width: 1100px) {
    .moves-panel {
        display: block;
        position: fixed;
        right: 2rem;
        top: 50%;
        transform: translateY(-50%);
        width: 300px;
        background-color: #2a2a2a;
        border-radius: 12px;
        padding: 1.5rem;
        height: fit-content;
        max-height: 80vh;
        overflow-y: auto;
    }

    .moves-panel-title {
        margin: 0 0 1rem 0;
        font-size: 1.25rem;
        color: #fff;
        border-bottom: 2px solid #444;
        padding-bottom: 0.5rem;
    }

    .moves-list {
        display: flex;
        flex-direction: column;
        gap: 0;
    }

    .move-pair {
        display: grid;
        grid-template-columns: 40px 1fr 1fr;
        gap: 0.5rem;
        padding: 0.1rem 0.5rem;
        border-radius: 4px;
        transition: background-color 0.2s;
    }

    .move-pair:hover {
        background-color: #333;
    }

    .move-number {
        color: #999;
        font-weight: 600;
        text-align: right;
    }

    .move-white, .move-black {
        color: #fff;
        font-family: 'Courier New', monospace;
        font-size: 0.95rem;
        padding: 0.25rem 0.5rem;
        border-radius: 4px;
        cursor: pointer;
        transition: background-color 0.2s;
    }

    .move-white:hover, .move-black:hover {
        background-color: #444;
    }

    .move-white {
        color: #fff;
    }

    .move-black {
        color: #ccc;
    }

    .move-current {
        background-color: #4a9eff !important;
        color: #fff !important;
    }
}

/* Repertoire Selector */
.repertoire-selector-container {
    margin-bottom: 1rem;
}

/* Mode Toggle */
.mode-toggle-container {
    display: flex;
    gap: 0;
    margin-bottom: 1rem;
    background-color: #2a2a2a;
    border-radius: 8px;
    padding: 4px;
}

.mode-btn {
    flex: 1;
    padding: 0.75rem 1rem;
    font-size: 1rem;
    font-weight: 500;
    background-color: transparent;
    color: #999;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
    white-space: nowrap;
}

.mode-btn-active {
    background-color: #4a9eff;
    color: white;
}

.mode-btn:hover:not(.mode-btn-active) {
    color: #ccc;
}

.repertoire-selector {
    width: 100%;
    padding: 0.75rem 1rem;
    font-size: 1rem;
    background-color: #2a2a2a;
    color: #fff;
    border: 1px solid #444;
    border-radius: 8px;
    cursor: pointer;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
    background-size: 1em;
    padding-right: 2.5rem;
}

.repertoire-selector:focus {
    outline: none;
    border-color: #4a9eff;
}

/* Chessboard */
#board {
    width: 100%;
    margin: 0 0 1rem 0;
    transition: opacity 0.3s ease, filter 0.3s ease;
    touch-action: none;
}

#board.line-complete {
    opacity: 0.6;
    filter: grayscale(30%);
    pointer-events: none;
}

/* Explore Controls */
.explore-controls {
    margin-bottom: 1rem;
}

.btn-back {
    width: 100%;
    background-color: #666;
}

.btn-back:hover {
    background-color: #555;
}

/* Board Container for Arrows */
#board {
    position: relative;
}

/* Arrow SVG Overlay */
.arrow-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 10;
}

.arrow-overlay svg {
    width: 100%;
    height: 100%;
}

/* Arrow paths */
.move-arrow {
    fill: none;
    stroke: rgb(74, 158, 255);
    stroke-width: 12;
    stroke-linecap: round;
    pointer-events: none;
}

.arrow-head {
    fill: rgb(74, 158, 255);
    pointer-events: none;
}

/* Progress Indicator */
.progress-container {
    margin-bottom: 1rem;
}

.progress-text {
    text-align: center;
    font-size: 0.9rem;
    color: #ccc;
    margin-bottom: 0.5rem;
}

.progress-text span {
    font-weight: 600;
    color: #4a9eff;
}

.progress-bar {
    width: 100%;
    height: 8px;
    background-color: #2a2a2a;
    border-radius: 4px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #4a9eff 0%, #3a8eef 100%);
    transition: width 0.3s ease;
    border-radius: 4px;
}

/* Controls */
.controls {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
    width: 100%;
}

.btn {
    flex: 1;
    padding: 0.875rem 1rem;
    font-size: 1rem;
    font-weight: 500;
    background-color: #4a9eff;
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s;
    -webkit-tap-highlight-color: transparent;
    white-space: nowrap;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn:hover {
    background-color: #3a8eef;
}

.btn:active {
    transform: scale(0.98);
}

.btn-secondary {
    background-color: #666;
}

.btn-secondary:hover {
    background-color: #555;
}

/* Feedback */
.feedback {
    text-align: center;
    padding: 1rem;
    border-radius: 8px;
    font-size: 0.95rem;
    min-height: 3rem;
    width: 100%;
}

.feedback.success {
    background-color: rgba(74, 158, 255, 0.2);
    color: #4a9eff;
}

.feedback.error {
    background-color: rgba(255, 74, 74, 0.2);
    color: #ff4a4a;
}

.feedback.info {
    background-color: rgba(255, 193, 7, 0.2);
    color: #ffc107;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
    animation: fadeIn 0.2s;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal-content {
    background-color: #2a2a2a;
    margin: 5% auto;
    padding: 2rem;
    border-radius: 12px;
    width: 90%;
    max-width: 500px;
    animation: slideIn 0.3s;
}

@keyframes slideIn {
    from {
        transform: translateY(-50px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
    line-height: 20px;
}

.close:hover,
.close:focus {
    color: #fff;
}

.modal-content h2,
.modal-content h3 {
    margin-bottom: 1rem;
    color: #fff;
}

.modal-content label {
    display: block;
    margin-bottom: 0.5rem;
    color: #ccc;
    font-size: 0.9rem;
    font-weight: 500;
}

#repertoireName {
    width: 100%;
    padding: 0.75rem 1rem;
    background-color: #1a1a1a;
    color: #fff;
    border: 1px solid #444;
    border-radius: 8px;
    font-size: 0.9rem;
    margin-bottom: 1rem;
}

#repertoireName:focus {
    outline: none;
    border-color: #4a9eff;
}

#pgnInput {
    width: 100%;
    padding: 1rem;
    background-color: #1a1a1a;
    color: #fff;
    border: 1px solid #444;
    border-radius: 8px;
    font-family: 'Courier New', monospace;
    font-size: 0.9rem;
    resize: vertical;
    margin-bottom: 1rem;
}

#pgnInput:focus {
    outline: none;
    border-color: #4a9eff;
}

.modal-actions {
    display: flex;
    gap: 0.5rem;
}

.modal-actions .btn {
    flex: 1;
}

/* Repertoires List */
.repertoires-list {
    margin-bottom: 1rem;
    max-height: 60vh;
    overflow-y: auto;
}

.repertoire-item {
    background-color: #1a1a1a;
    border: 1px solid #444;
    border-radius: 8px;
    margin-bottom: 0.75rem;
    overflow: hidden;
}

.repertoire-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem;
}

.repertoire-name {
    flex: 1;
    font-weight: 500;
    color: #fff;
    font-size: 1rem;
    word-break: break-word;
}

.repertoire-name-input {
    flex: 1;
    padding: 0.5rem;
    background-color: #2a2a2a;
    color: #fff;
    border: 1px solid #4a9eff;
    border-radius: 4px;
    font-size: 1rem;
}

.repertoire-name-input:focus {
    outline: none;
    border-color: #4a9eff;
}

.repertoire-controls {
    display: flex;
    gap: 0.25rem;
}

.btn-icon {
    padding: 0.5rem;
    min-width: 44px;
    min-height: 44px;
    background-color: #4a9eff;
    color: white;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.875rem;
    font-weight: 500;
    transition: background-color 0.2s, transform 0.1s;
    -webkit-tap-highlight-color: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-icon:hover {
    background-color: #3a8eef;
}

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

.btn-icon.btn-delete {
    background-color: #ff4a4a;
}

.btn-icon.btn-delete:hover {
    background-color: #ee3a3a;
}

.btn-icon.btn-save {
    background-color: #4caf50;
}

.btn-icon.btn-save:hover {
    background-color: #45a049;
}

.btn-icon.btn-cancel {
    background-color: #666;
}

.btn-icon.btn-cancel:hover {
    background-color: #555;
}

.repertoire-content {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
}

.repertoire-content.expanded {
    max-height: 400px;
    border-top: 1px solid #444;
}

.repertoire-content-inner {
    padding: 0.75rem;
}

.repertoire-pgn {
    width: 100%;
    padding: 0.75rem;
    background-color: #0f0f0f;
    color: #fff;
    border: 1px solid #444;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    font-size: 0.85rem;
    resize: vertical;
    min-height: 150px;
}

.repertoire-pgn:focus {
    outline: none;
    border-color: #4a9eff;
}

.repertoire-pgn-display {
    background-color: #0f0f0f;
    color: #ccc;
    padding: 0.75rem;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    font-size: 0.85rem;
    white-space: pre-wrap;
    word-break: break-all;
    max-height: 200px;
    overflow-y: auto;
}

.btn-add-repertoire {
    width: 100%;
    background-color: #4caf50;
}

.btn-add-repertoire:hover {
    background-color: #45a049;
}

/* Error Modal */
.error-modal {
    text-align: center;
}

.error-modal h3 {
    color: #ff4a4a;
}

.error-modal p {
    margin-bottom: 1.5rem;
    color: #ccc;
}

.error-actions {
    display: flex;
    gap: 0.5rem;
}

/* Small phones */
@media (max-width: 360px) {
    body {
        padding: 0.5rem;
    }

    .container {
        width: calc(100vw - 1rem);
    }

    .btn {
        width: 100%;
    }

    .feedback {
        font-size: 0.9rem;
        padding: 0.75rem;
        min-height: 2.5rem;
    }

    .modal-content {
        width: 95%;
        padding: 1.5rem;
    }

    .modal-content h2,
    .modal-content h3 {
        font-size: 1.2rem;
    }

    #pgnInput {
        font-size: 0.9rem;
        padding: 0.75rem;
    }

    .error-actions,
    .modal-actions {
        flex-direction: column;
    }

    .modal-actions .btn {
        width: 100%;
    }
}

/* Mobile landscape */
@media (max-width: 768px) and (orientation: landscape) {
    body {
        padding: 0.5rem;
    }
}

/* Tablet and Desktop */
@media (min-width: 768px) {
    body {
        padding: 2rem;
    }

    .container {
        width: min(calc(100vw - 4rem), 600px);
        max-width: 600px;
    }

    .modal-content {
        margin: 10% auto;
    }
}


/* Hide board coordinates on mobile for cleaner look */
@media (max-width: 480px) {
    .notation-322f9 {
        display: none;
    }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
    .close {
        font-size: 32px;
        padding: 0.5rem;
        min-width: 44px;
        min-height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
}

/* Prevent overscroll/bounce on iOS */
@supports (-webkit-touch-callout: none) {
    body {
        overscroll-behavior: none;
    }
}
