* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: Inter, Arial, sans-serif;
    background: radial-gradient(circle at top left, #dff7ff 0, #f6fbff 35%, #eef4f8 100%);
    color: #102330;
}

.page {
    width: min(1180px, calc(100% - 28px));
    margin: 0 auto;
    padding: 22px 0 40px;
}

.hero {
    min-height: 170px;
    display: flex;
    align-items: center;
    padding: 24px 28px;
    border-radius: 28px;
    background: linear-gradient(135deg, #06364a, #0b7fa3 55%, #36d4c7);
    color: #fff;
    box-shadow: 0 22px 60px rgba(3, 43, 62, .18);
}

.badge {
    width: fit-content;
    padding: 6px 12px;
    border-radius: 999px;
    background: rgba(255,255,255,.14);
    border: 1px solid rgba(255,255,255,.22);
    margin-bottom: 12px;
    font-size: 12px;
    font-weight: 700;
}

.hero h1 {
    max-width: 760px;
    margin: 0 0 10px;
    font-size: clamp(24px, 3.5vw, 42px);
    line-height: 1.02;
    letter-spacing: -1px;
}

.hero p {
    max-width: 720px;
    margin: 0;
    font-size: 14px;
    line-height: 1.45;
    color: rgba(255,255,255,.78);
}

.stats {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 10px;
    margin-top: 16px;
}

.stats div {
    padding: 14px;
    border-radius: 18px;
    background: rgba(255,255,255,.84);
    border: 1px solid rgba(9,78,105,.08);
    box-shadow: 0 12px 30px rgba(21,60,78,.06);
}

.stats strong {
    display: block;
    font-size: 22px;
    line-height: 1;
}

.stats span {
    display: block;
    margin-top: 5px;
    color: #607887;
    font-size: 12px;
    font-weight: 800;
}

.panel {
    margin-top: 22px;
    padding: 24px;
    border-radius: 28px;
    background: rgba(255,255,255,.82);
    border: 1px solid rgba(9,78,105,.08);
    box-shadow: 0 18px 50px rgba(21,60,78,.08);
    backdrop-filter: blur(16px);
}

.panel h2 {
    margin: 0 0 16px;
    font-size: 24px;
}

.upload-form {
    display: grid;
    grid-template-columns: 1.2fr 1fr auto;
    gap: 12px;
}

input, select, button {
    width: 100%;
    border: 0;
    border-radius: 18px;
    font-size: 16px;
}

input, select {
    background: #f1f7fa;
    padding: 16px 18px;
    color: #102330;
    outline: 1px solid rgba(9,78,105,.08);
}

button {
    padding: 16px 22px;
    background: linear-gradient(135deg, #0582a8, #18c8b8);
    color: #fff;
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 12px 28px rgba(0,130,168,.22);
}

button.secondary {
    background: #e9f2f6;
    color: #173545;
    box-shadow: none;
}

button.active {
    background: linear-gradient(135deg, #05a87c, #18c8b8);
    color: #fff;
}

button.ai {
    background: linear-gradient(135deg, #163f8f, #6a5cff);
}

button.danger {
    background: linear-gradient(135deg, #d8244a, #ff647c);
    color: #fff;
    box-shadow: 0 12px 28px rgba(216,36,74,.18);
}

.section-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.section-head span {
    color: #607887;
    font-weight: 700;
}

.empty {
    padding: 30px;
    border-radius: 22px;
    background: #f4f9fb;
    color: #5e7480;
}

.grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 16px;
}

.card {
    display: block;
    overflow: hidden;
    text-decoration: none;
    color: inherit;
    border-radius: 24px;
    background: #fff;
    box-shadow: 0 12px 32px rgba(20,60,80,.08);
}

.card img {
    width: 100%;
    aspect-ratio: 4 / 3;
    object-fit: cover;
    background: #dce8ee;
}

.card-body {
    padding: 14px;
}

.card-body strong, .card-body small, .card-body em {
    display: block;
}

.card-body small {
    margin-top: 5px;
    color: #6b7f8a;
}

.card-body em {
    margin-top: 8px;
    font-style: normal;
    color: #078065;
    font-weight: 800;
}

.topbar {
    height: 62px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.topbar a {
    color: #0d7798;
    text-decoration: none;
    font-weight: 800;
}

.viewer-layout {
    display: grid;
    grid-template-columns: 1fr 340px;
    gap: 20px;
}

.viewer-panel, .side-panel {
    border-radius: 28px;
    background: rgba(255,255,255,.86);
    box-shadow: 0 18px 55px rgba(20,60,80,.1);
    border: 1px solid rgba(9,78,105,.08);
}

.viewer-panel {
    padding: 16px;
}

.viewer-tools {
    display: grid;
    grid-template-columns: 1fr 1fr 58px 58px 86px 58px;
    gap: 10px;
    margin-bottom: 12px;
}

.viewer-tools button {
    padding: 12px 10px;
    border-radius: 14px;
}

.side-panel {
    padding: 22px;
}

.side-panel h1 {
    margin: 0 0 8px;
    font-size: 28px;
    line-height: 1.05;
}

.side-panel p {
    margin: 0 0 22px;
    color: #617885;
}

.side-panel label {
    display: block;
    margin-bottom: 8px;
    color: #385464;
    font-weight: 800;
}

.side-panel select, .side-panel button {
    margin-bottom: 12px;
}

.hint {
    margin-top: 10px;
    padding: 14px;
    border-radius: 18px;
    background: #eef8fb;
    color: #45616e;
    line-height: 1.45;
}

#status {
    margin-top: 12px;
    font-weight: 800;
    color: #078065;
}

.image-wrap {
    position: relative;
    width: 100%;
    min-height: 420px;
    overflow: hidden;
    border-radius: 22px;
    background: #06151d;
    touch-action: none;
}

.image-stage {
    position: relative;
    width: 100%;
    transform-origin: 0 0;
}

.image-stage img {
    display: block;
    width: 100%;
    height: auto;
    user-select: none;
    pointer-events: none;
}

.image-stage canvas {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    touch-action: none;
}

@media (max-width: 900px) {
    .upload-form {
        grid-template-columns: 1fr;
    }

    .grid {
        grid-template-columns: repeat(2, 1fr);
    }

    .viewer-layout {
        grid-template-columns: 1fr;
    }

    .viewer-tools {
        grid-template-columns: 1fr 1fr 52px 52px 78px 52px;
    }

    .side-panel {
        position: sticky;
        bottom: 0;
        z-index: 5;
    }
}

@media (max-width: 520px) {
    .page {
        width: min(100% - 18px, 1180px);
        padding-top: 10px;
    }

    .hero {
        padding: 24px;
        min-height: 230px;
        border-radius: 26px;
    }

    .hero h1 {
        letter-spacing: -1px;
    }

    .stats {
        grid-template-columns: 1fr;
    }

    .panel {
        padding: 16px;
        border-radius: 24px;
    }

    .grid {
        grid-template-columns: 1fr;
    }

    .viewer-panel, .side-panel {
        border-radius: 22px;
    }

    .viewer-tools {
        grid-template-columns: 1fr 1fr 1fr;
    }

    .side-panel h1 {
        font-size: 22px;
    }

    .image-wrap {
        min-height: 300px;
    }
}

button.danger {
    background: linear-gradient(135deg, #d8244a, #ff647c);
    color: #fff;
    box-shadow: 0 12px 28px rgba(216,36,74,.18);
}

.toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.toolbar a,
.download-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 48px;
    padding: 12px 18px;
    border-radius: 16px;
    background: #e9f2f6;
    color: #173545;
    text-decoration: none;
    font-weight: 900;
}

.toolbar a.active-link {
    background: linear-gradient(135deg, #05a87c, #18c8b8);
    color: #fff;
}

.toolbar a.primary-link {
    background: linear-gradient(135deg, #0582a8, #18c8b8);
    color: #fff;
}

.toolbar a.export-link,
.download-btn {
    background: linear-gradient(135deg, #163f8f, #6a5cff);
    color: #fff;
}

@media (max-width: 900px) {
    .viewer-layout {
        display: flex;
        flex-direction: column;
        gap: 16px;
    }

    .viewer-panel {
        order: 1;
    }

    .side-panel {
        order: 2;
        position: static !important;
        bottom: auto !important;
        z-index: auto !important;
    }

    .image-wrap {
        min-height: 0 !important;
        height: auto !important;
        overflow: hidden;
    }

    .image-stage {
        display: block;
    }

    .viewer-tools {
        position: static !important;
        display: grid;
        grid-template-columns: 1fr 1fr 52px 52px 78px;
        gap: 8px;
        margin-bottom: 10px;
    }
}

@media (max-width: 520px) {
    .viewer-tools {
        grid-template-columns: 1fr 1fr 1fr;
    }

    .viewer-tools button {
        min-height: 46px;
        font-size: 14px;
        padding: 10px 8px;
    }

    .side-panel {
        margin-top: 0;
    }
}

@media (max-width: 900px) {
    .stats {
        grid-template-columns: repeat(3, 1fr);
    }
}

@media (max-width: 520px) {
    .hero {
        min-height: 0;
        padding: 18px;
        border-radius: 22px;
    }

    .hero h1 {
        font-size: 24px;
        margin-bottom: 8px;
    }

    .hero p {
        font-size: 12px;
        line-height: 1.35;
    }

    .stats {
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }

    .stats div {
        padding: 12px;
        border-radius: 16px;
    }

    .stats strong {
        font-size: 18px;
    }

    .stats span {
        font-size: 11px;
    }
}