:root {
    --bg: #f4f6f8;
    --surface: #ffffff;
    --soft: #f8fafc;
    --ink: #121826;
    --muted: #667085;
    --faint: #98a2b3;
    --line: #e4e7ec;
    --line-strong: #cbd5e1;
    --blue: #2f5fd7;
    --blue-dark: #244bb0;
    --green: #157f64;
    --danger: #b42318;
    --shadow: 0 20px 54px rgba(17, 24, 39, 0.08);
    --radius: 8px;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html {
    min-width: 320px;
    text-size-adjust: 100%;
}

body {
    margin: 0;
    min-height: 100vh;
    color: var(--ink);
    font-family: Inter, "PingFang SC", "Microsoft YaHei", Arial, sans-serif;
    background: linear-gradient(180deg, #ffffff 0%, var(--bg) 420px, #eef2f6 100%);
}

button,
input,
textarea {
    font: inherit;
}

button,
a,
label {
    -webkit-tap-highlight-color: transparent;
}

a {
    color: inherit;
    text-decoration: none;
}

button {
    border: 0;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    min-height: 64px;
    padding: 12px max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
    border-bottom: 1px solid rgba(226, 229, 234, 0.72);
    background: rgba(255, 255, 255, 0.88);
    backdrop-filter: blur(18px);
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.brand-mark {
    display: grid;
    width: 36px;
    height: 36px;
    flex: 0 0 auto;
    place-items: center;
    border-radius: var(--radius);
    background: var(--ink);
    color: #fff;
    font-size: 15px;
    font-weight: 850;
}

.brand strong {
    display: block;
    font-size: 19px;
    line-height: 1.05;
}

.brand small {
    display: block;
    margin-top: 3px;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.2;
    white-space: normal;
}

.app-shell {
    width: min(980px, 100%);
    margin: 0 auto;
    padding: 18px 14px 34px;
}

.studio {
    display: grid;
    gap: 12px;
    min-width: 0;
}

.composer,
.result-panel,
.history-section {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: rgba(255, 255, 255, 0.96);
    box-shadow: var(--shadow);
}

.composer,
.result-panel {
    display: grid;
    gap: 15px;
    min-width: 0;
    padding: 14px;
}

.prompt-block,
.reference-card {
    display: grid;
    gap: 9px;
    min-width: 0;
}

.label-row,
.reference-head,
.result-head,
.history-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.label-row label,
.reference-head span,
legend,
.result-head span,
.history-head span {
    color: #20242d;
    font-size: 14px;
    font-weight: 850;
}

#charCount,
.reference-head strong,
.result-head small {
    color: var(--faint);
    font-size: 12px;
    font-weight: 700;
}

#charCount {
    display: inline-flex;
    min-height: 24px;
    align-items: center;
    padding: 0 8px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #f8fafc;
    color: #738095;
}

.reference-head strong.ready {
    color: var(--green);
}

textarea {
    width: 100%;
    max-width: 100%;
    min-width: 0;
    min-height: 164px;
    resize: vertical;
    padding: 15px;
    border: 1px solid var(--line-strong);
    border-radius: var(--radius);
    outline: none;
    background: linear-gradient(180deg, #ffffff, #fbfcfe);
    color: var(--ink);
    font-size: 16px;
    line-height: 1.65;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.84);
    transition: border-color 0.18s ease, box-shadow 0.18s ease, background 0.18s ease;
}

textarea::placeholder {
    color: #7d899a;
}

textarea:focus {
    border-color: var(--blue);
    background: #fff;
    box-shadow: 0 0 0 4px rgba(40, 91, 231, 0.12);
}

.upload-box {
    display: grid;
    grid-template-columns: 42px minmax(0, 1fr);
    gap: 12px;
    align-items: center;
    min-height: 74px;
    padding: 11px;
    border: 1px dashed var(--line-strong);
    border-radius: var(--radius);
    background: var(--soft);
    cursor: pointer;
    transition: border-color 0.18s ease, background 0.18s ease;
}

.upload-box:hover,
.upload-box.dragging {
    border-color: var(--blue);
    background: #f7f9fd;
}

.upload-box input {
    display: none;
}

.upload-icon {
    display: grid;
    width: 42px;
    height: 42px;
    place-items: center;
    border-radius: var(--radius);
    background: #eef2f7;
    color: var(--blue);
    font-size: 24px;
    font-weight: 300;
}

.upload-copy {
    display: grid;
    gap: 4px;
    min-width: 0;
}

.upload-copy strong {
    overflow: hidden;
    color: #1f2937;
    font-size: 15px;
    line-height: 1.3;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.upload-copy small {
    color: var(--muted);
    font-size: 13px;
    line-height: 1.45;
}

.reference-list {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
}

.reference-thumb {
    position: relative;
    aspect-ratio: 1;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #eef2f7;
}

.reference-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.reference-thumb span {
    position: absolute;
    right: 5px;
    bottom: 5px;
    min-width: 22px;
    height: 22px;
    padding: 0 6px;
    border-radius: 999px;
    background: rgba(17, 25, 39, 0.78);
    color: #fff;
    font-size: 12px;
    line-height: 22px;
    text-align: center;
}

.remove-reference {
    justify-self: start;
    min-height: 28px;
    padding: 0;
    background: transparent;
    color: var(--danger);
    cursor: pointer;
    font-size: 13px;
    font-weight: 750;
}

.options {
    display: grid;
    gap: 16px;
    min-width: 0;
}

fieldset {
    display: grid;
    gap: 8px;
    margin: 0;
    min-width: 0;
    padding: 0;
    border: 0;
}

legend {
    margin-bottom: 3px;
}

.choice-row {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 7px;
    min-width: 0;
}

fieldset:nth-child(2) .choice-row {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

fieldset input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

fieldset span {
    display: grid;
    min-height: 40px;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #fff;
    color: #334155;
    cursor: pointer;
    font-size: 14px;
    font-weight: 800;
}

fieldset input:checked + span {
    border-color: var(--ink);
    background: var(--ink);
    color: #fff;
}

.action-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    min-width: 0;
}

.primary-button,
.secondary-button,
.secondary-light-button,
.plain-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 48px;
    border-radius: var(--radius);
    cursor: pointer;
    font-weight: 850;
}

.primary-button {
    position: relative;
    overflow: hidden;
    background: var(--ink);
    color: #fff;
}

.primary-button::before {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.18), transparent);
    transform: translateX(-110%);
}

.primary-button.loading::before {
    animation: buttonSheen 1.3s ease-in-out infinite;
}

.button-text {
    position: relative;
    z-index: 1;
}

.button-loader {
    position: absolute;
    right: 16px;
    width: 18px;
    height: 18px;
    border: 2px solid rgba(255, 255, 255, 0.34);
    border-top-color: #fff;
    border-radius: 999px;
    opacity: 0;
    animation: spin 0.75s linear infinite;
}

.primary-button.loading .button-loader {
    opacity: 1;
}

.plain-button {
    min-width: 66px;
    padding: 0 12px;
    border: 1px solid var(--line);
    background: #fff;
    color: #334155;
}

.notice {
    display: flex;
    align-items: center;
    gap: 9px;
    min-width: 0;
    min-height: 42px;
    margin: -2px 0 0;
    padding: 10px 12px;
    border: 1px solid #dbe3ee;
    border-radius: var(--radius);
    background: #f8fbff;
    color: #405064;
    font-size: 13px;
    font-weight: 700;
    line-height: 1.5;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.74);
    animation: noticeIn 0.18s ease-out;
}

.notice:empty {
    display: none;
}

.notice::before {
    content: "i";
    display: grid;
    width: 20px;
    height: 20px;
    flex: 0 0 auto;
    place-items: center;
    border-radius: 999px;
    background: #e8eef7;
    color: var(--blue);
    font-size: 12px;
    font-weight: 900;
    line-height: 1;
}

.notice-text {
    min-width: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.notice.error {
    border-color: #ffd0c9;
    background: #fff7f6;
    color: #98251c;
}

.notice.success {
    border-color: #bde8d8;
    background: #f2fbf7;
    color: #0f6b55;
}

.notice.error::before {
    content: "!";
    background: var(--danger);
    color: #fff;
}

.notice.success::before {
    content: "\2713";
    background: var(--green);
    color: #fff;
}

.result-head small {
    display: block;
    margin-top: 3px;
}

.status-pill {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 56px;
    min-height: 28px;
    padding: 0 9px;
    border-radius: 999px;
    background: #eef1f5;
    color: var(--muted);
    font-size: 12px;
}

.status-pill.working {
    background: #eef4ff;
    color: var(--blue);
}

.status-pill.done {
    background: #edf8f4;
    color: var(--green);
}

.status-pill.error {
    background: rgba(180, 35, 24, 0.10);
    color: var(--danger);
}

.preview-stage {
    position: relative;
    display: grid;
    min-width: 0;
    min-height: 420px;
    place-items: center;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background:
        linear-gradient(45deg, #f2f4f7 25%, transparent 25%),
        linear-gradient(-45deg, #f2f4f7 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #f2f4f7 75%),
        linear-gradient(-45deg, transparent 75%, #f2f4f7 75%),
        #fff;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0;
    background-size: 20px 20px;
}

#resultImage {
    width: 100%;
    height: 100%;
    max-height: 760px;
    object-fit: contain;
}

.empty-state {
    display: grid;
    width: min(310px, calc(100% - 40px));
    gap: 14px;
    justify-items: center;
    color: #475569;
    text-align: center;
}

.empty-state img {
    width: min(230px, 78%);
    height: auto;
    border-radius: 12px;
    filter: drop-shadow(0 20px 42px rgba(30, 45, 70, 0.12));
}

.empty-state strong {
    font-size: 15px;
}

.loading-state {
    display: grid;
    width: min(420px, calc(100% - 28px));
    gap: 14px;
}

.loading-box {
    display: grid;
    min-height: 230px;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: #f8fafc;
}

.loading-spinner {
    width: 76px;
    height: 76px;
    border: 4px solid #e6ebf1;
    border-top-color: var(--blue);
    border-radius: 999px;
    animation: spin 0.85s linear infinite;
}

.progress-info {
    display: grid;
    gap: 10px;
    min-width: 0;
    padding: 13px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: rgba(255, 255, 255, 0.94);
}

.progress-info > div:first-child {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

#progressTitle {
    font-size: 15px;
}

#elapsedTime {
    color: var(--muted);
    font-size: 12px;
    white-space: nowrap;
}

.progress-track {
    height: 7px;
    overflow: hidden;
    border-radius: 999px;
    background: #e6ebf1;
}

.progress-track span {
    display: block;
    width: 4%;
    height: 100%;
    border-radius: inherit;
    background: var(--blue);
    transition: width 0.55s ease;
}

.progress-steps {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    margin: 0;
    padding: 0;
    list-style: none;
}

.progress-steps li {
    overflow: hidden;
    color: var(--faint);
    font-size: 12px;
    font-weight: 750;
    text-align: center;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.progress-steps li.active {
    color: var(--blue);
}

.progress-steps li.done {
    color: var(--green);
}

.result-actions {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
}

.secondary-button,
.secondary-light-button {
    min-height: 44px;
    padding: 0 10px;
    font-size: 14px;
}

.secondary-button {
    background: var(--blue);
    color: #fff;
}

.secondary-button:hover {
    background: var(--blue-dark);
}

.secondary-light-button {
    border: 1px solid var(--line);
    background: #fff;
    color: #334155;
}

.history-section {
    display: grid;
    gap: 12px;
    margin-top: 12px;
    min-width: 0;
    padding: 14px;
}

.history-head button {
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    font-size: 13px;
    font-weight: 750;
}

.history-list {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 8px;
}

.history-item {
    display: block;
    aspect-ratio: 1;
    overflow: hidden;
    padding: 0;
    border: 2px solid transparent;
    border-radius: var(--radius);
    background: #eef2f7;
    cursor: pointer;
}

.history-item:hover,
.history-item.active {
    border-color: var(--blue);
}

.history-item img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.site-footer {
    display: flex;
    justify-content: center;
    width: min(980px, calc(100% - 28px));
    margin: 0 auto;
    padding: 4px 0 28px;
    color: var(--faint);
    font-size: 12px;
    text-align: center;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

@keyframes buttonSheen {
    100% {
        transform: translateX(110%);
    }
}

@keyframes noticeIn {
    from {
        opacity: 0;
        transform: translateY(-3px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@media (min-width: 720px) {
    .topbar {
        padding-right: clamp(24px, 5vw, 56px);
        padding-left: clamp(24px, 5vw, 56px);
    }

    .app-shell {
        padding: 24px clamp(22px, 4vw, 44px) 38px;
    }

    .composer,
    .result-panel {
        padding: 16px;
    }

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

    .preview-stage {
        min-height: 560px;
    }

    .history-list {
        grid-template-columns: repeat(8, minmax(0, 1fr));
    }
}

@media (min-width: 1040px) {
    textarea {
        min-height: 178px;
    }

    .preview-stage {
        min-height: 620px;
    }
}

@media (max-width: 560px) {
    .app-shell {
        padding-top: 14px;
        padding-right: 0;
        padding-left: 0;
        width: calc(100% - 28px);
    }

    .composer,
    .result-panel {
        padding: 13px;
    }

    .choice-row {
        gap: 6px;
    }

    fieldset span {
        min-height: 38px;
        font-size: 13px;
    }

    .preview-stage {
        min-height: 330px;
    }

    .action-row {
        position: sticky;
        bottom: 10px;
        z-index: 5;
        padding-top: 4px;
        background: linear-gradient(180deg, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.96) 38%);
    }

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

    .secondary-button,
    .secondary-light-button {
        width: 100%;
    }
}
