/* Crop toggle */
.crop-btn {
    position: fixed;
    top: 20px;
    left: 15px;
    z-index: 1000;
    background: var(--btn-bg);
    border: none;
    border-radius: 50%;
    width: 44px;
    height: 44px;
    padding: 10px;
    cursor: pointer;
    opacity: 0.8;
    transition: opacity 0.2s, background-color 0.3s;
    display: none;
}

.crop-btn:hover {
    opacity: 1;
    background: var(--btn-hover-bg);
}

.crop-btn .icon {
    width: 100%;
    height: 100%;
    color: var(--text-color);
}

#edit-screen.active .crop-btn {
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Crop canvas */
#crop-screen {
    background-color: var(--bg-color);
    position: relative;
}

#crop-canvas-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: calc(100% - 160px);
    overflow: hidden;
}

#crop-canvas {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    cursor: grab;
}

#crop-canvas:active {
    cursor: grabbing;
}

#crop {
    position: absolute;
    pointer-events: all;
}

.cropcorner {
    position: absolute;
    width: 40px;
    height: 40px;
    border-color: light-dark(black, white);
    border-style: solid;
    z-index: 2 !important;
    pointer-events: all;
}

.cropcorner::before,
#croprect #top_left::before,
#croprect #top_right::before,
#croprect #bottom_left::before,
#croprect #bottom_right::before,
#croprect #top::before,
#croprect #bottom::before,
#croprect #left::before,
#croprect #right::before {
    content: '';
    position: absolute;
    pointer-events: all;
    z-index: -1;
}

.cropcorner::before {
    top: -15px;
    left: -15px;
    right: -15px;
    bottom: -15px;
}

#croprect {
    border: solid 1px light-dark(black, white);
    background-size: 33.33333% 33.33333%;
    background-image:
        linear-gradient(to right, light-dark(rgba(125, 125, 125, 0.45), rgba(255, 255, 255, 0.45)) 1px, transparent 1px),
        linear-gradient(to bottom, light-dark(rgba(125, 125, 125, 0.45), rgba(255, 255, 255, 0.45)) 1px, transparent 1px);
    position: absolute;
    box-shadow: 0 0 0 9999px var(--crop-overlay);
    cursor: move;
    max-height: 100%;
    max-width: 100%;
}

#croprect.grid-refine {
    background-size: 12.5% 12.5%;
}

#croprect #top_left {
    top: 0;
    left: 0;
    border-width: 3px 0 0 3px;
    cursor: nw-resize;
}

#croprect #top_right {
    top: 0;
    right: 0;
    border-width: 3px 3px 0 0;
    cursor: ne-resize;
}

#croprect #bottom_right {
    bottom: 0;
    right: 0;
    border-width: 0 3px 3px 0;
    cursor: nw-resize;
    z-index: 1101 !important;
}

#croprect #bottom_left {
    bottom: 0;
    left: 0;
    border-width: 0 0 3px 3px;
    cursor: ne-resize;
    z-index: 1101 !important;
}

#croprect #top {
    top: 0;
    right: calc(50% - 20px);
    border-width: 3px 0 0 0;
    cursor: ns-resize;
}

#croprect #bottom {
    bottom: 0;
    right: calc(50% - 20px);
    border-width: 0 0 3px 0;
    cursor: ns-resize;
}

#croprect #left {
    top: calc(50% - 20px);
    left: 0;
    border-width: 0 0 0 3px;
    cursor: ew-resize;
}

#croprect #right {
    top: calc(50% - 20px);
    right: 0;
    border-width: 0 3px 0 0;
    cursor: ew-resize;
}

/* Angle control */
#angle-control {
    position: fixed;
    bottom: 160px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1100;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    pointer-events: none;
}

.angle-display {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-color);
    background: var(--ui-bg);
    padding: 6px 12px;
    border-radius: 16px;
    border: 1px solid rgba(128, 128, 128, 0.2);
    cursor: pointer;
    transition: all 0.2s ease;
    user-select: none;
    min-width: 50px;
    width: fit-content;
    text-align: center;
    pointer-events: auto;
}

.angle-display:hover {
    background: var(--btn-hover-bg);
}

.angle-slider-container {
    position: relative;
    width: 100vw;
    height: 60px;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    mask: linear-gradient(
        to right,
        transparent 0%,
        transparent 10%,
        black 25%,
        black 75%,
        transparent 90%,
        transparent 100%
    );
    -webkit-mask: linear-gradient(
        to right,
        transparent 0%,
        transparent 10%,
        black 25%,
        black 75%,
        transparent 90%,
        transparent 100%
    );
}

.angle-pointer {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 3px;
    height: 40px;
    background: var(--accent-color);
    border-radius: 2px;
    z-index: 10;
    box-shadow: 0 0 10px rgba(var(--accent-color-rgb), 0.5);
}

.angle-track-bg {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100vw;
    height: 40px;
    background: var(--ui-bg);
    border: 1px solid rgba(128, 128, 128, 0.2);
    z-index: 1;
    mask: linear-gradient(
        to right,
        transparent 0%,
        transparent 5%,
        black 20%,
        black 80%,
        transparent 95%,
        transparent 100%
    );
    -webkit-mask: linear-gradient(
        to right,
        transparent 0%,
        transparent 5%,
        black 20%,
        black 80%,
        transparent 95%,
        transparent 100%
    );
}

.angle-track {
    position: relative;
    width: 400vw;
    height: 40px;
    background: transparent;
    overflow: visible;
    margin-left: -150vw;
    z-index: 2;
    /* 当たり判定を左右に拡張 */
    padding-left: 50vw;
    padding-right: 50vw;
    margin-right: -50vw;
}

.angle-marks {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

#angle-slider {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: transparent;
    cursor: grab;
    -webkit-appearance: none;
    appearance: none;
    outline: none;
    border: none;
    opacity: 0;
}

#angle-slider:active {
    cursor: grabbing;
}

#angle-input {
    position: absolute;
    top: -50px;
    left: 50%;
    transform: translateX(-50%);
    width: 80px;
    padding: 8px 12px;
    border: 2px solid var(--accent-color);
    border-radius: 8px;
    background: var(--bg-color);
    color: var(--text-color);
    text-align: center;
    font-size: 16px;
    outline: none;
}

.angle-mark {
    position: absolute;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    padding: 8px 0;
    font-size: 10px;
    color: var(--text-color);
    opacity: 0.7;
    pointer-events: none;
}

.angle-mark::before {
    content: '';
    width: 1px;
    background: var(--text-color);
    opacity: 0.3;
}

.angle-mark.major::before {
    height: 20px;
    opacity: 0.6;
}

.angle-mark.minor::before {
    height: 10px;
    opacity: 0.3;
}

.angle-mark.major {
    font-weight: 500;
    opacity: 0.8;
}

#crop-ui {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 10;
    background-color: var(--ui-bg);
    transition: opacity 0.3s, visibility 0.3s, background-color 0.3s;
    padding-bottom: env(safe-area-inset-bottom);
}

#crop-sub-nav-container {
    display: flex;
    align-items: center;
    position: relative;
}

.crop-close-fixed {
    position: sticky;
    left: 0;
    z-index: 20;
    background: var(--ui-bg);
    margin-right: 10px;
    padding: 5px 15px !important;
    flex-shrink: 0;
}

.crop-add-fixed {
    position: sticky;
    left: calc(0px + 80px);
    z-index: 19;
    background: var(--ui-bg);
    margin-right: 10px;
    padding: 5px 15px !important;
    flex-shrink: 0;
}

#crop-sub-nav {
    display: flex;
    padding: 15px 0 15px 15px;
    overflow-x: auto;
    white-space: nowrap;
    flex: 1;
    scroll-behavior: smooth;
    min-height: 60px;
    align-items: center;
}

#crop-main-nav {
    display: flex;
    justify-content: space-around;
    padding: 15px 0;
    border-top: 1px solid rgba(128, 128, 128, 0.2);
    align-items: center;
    min-height: 60px;
}

.aspect-btn.nav-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: none;
    border: none;
    color: var(--text-color);
    cursor: pointer;
    padding: 5px;
    border-radius: 8px;
    width: 80px;
    flex-shrink: 0;
    margin-right: 8px;
}

.aspect-btn.nav-btn .aspect-icon {
    margin-bottom: 5px;
    border: none;
    border-radius: 2px;
    position: relative;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 28px;
}

.aspect-btn.nav-btn span {
    font-size: 12px;
    height: 16px;
    line-height: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.aspect-btn.nav-btn.active {
    color: var(--accent-color);
}

#crop-close-btn.nav-btn {
    display: flex;
    align-items: center;
    background: none;
    border: none;
    color: var(--text-color);
    padding: 5px 15px;
    cursor: pointer;
    border-radius: 8px;
    font-size: 14px;
    width: auto;
}

#crop-close-btn.nav-btn .icon {
    width: 20px;
    height: 20px;
    margin-right: 8px;
}

#crop-sub-nav-container {
    -ms-overflow-style: none;
    scrollbar-width: none;
}

#crop-sub-nav-container::-webkit-scrollbar,
#crop-sub-nav::-webkit-scrollbar {
    display: none;
}

#crop-sub-nav {
    -ms-overflow-style: none;
    scrollbar-width: none;
}

@media (max-width: 600px) {
    #angle-control {
        bottom: 155px;
    }

    .angle-display {
        font-size: 14px;
        padding: 5px 10px;
        min-width: 45px;
    }

    .angle-slider-container {
        height: 50px;
    }

    .angle-pointer {
        height: 30px;
    }

    .angle-track {
        height: 30px;
    }

    #angle-input {
        width: 70px;
        padding: 6px 10px;
        font-size: 14px;
    }

    .angle-mark {
        font-size: 9px;
    }

    .angle-mark.major::before {
        height: 15px;
    }

    .angle-mark.minor::before {
        height: 8px;
    }

    #crop-sub-nav {
        padding: 12px 0 12px 15px;
        min-height: 50px;
    }

    .crop-close-fixed {
        padding: 5px 10px !important;
        margin-right: 8px;
    }

    .aspect-btn.nav-btn {
        width: 65px;
        margin-right: 6px;
    }

    .aspect-btn.nav-btn .aspect-icon {
        margin-bottom: 4px;
        max-width: 24px !important;
        max-height: 24px !important;
    }

    .aspect-btn.nav-btn span {
        font-size: 10px;
        height: 14px;
        line-height: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    #crop-main-nav {
        padding: 12px 0;
        min-height: 50px;
    }
}
