:root {
    --editor-bg: #f3f6fa;
    --editor-panel: rgba(255, 255, 255, 0.94);
    --editor-text: #17283b;
    --editor-muted: #536479;
    --editor-border: #cbd5e1;
    --editor-accent: #08778b;
    --editor-accent-contrast: #fff;
    --editor-shadow: 0 18px 55px rgba(23, 40, 59, 0.08);
    color-scheme: light;
}

html[data-theme="dark"] {
    --editor-bg: #090d14;
    --editor-panel: rgba(19, 26, 38, 0.96);
    --editor-text: #f1f5f9;
    --editor-muted: #a6b3c4;
    --editor-border: #334155;
    --editor-accent: #15a9bd;
    --editor-accent-contrast: #041116;
    --editor-shadow: 0 18px 55px rgba(0, 0, 0, 0.25);
    color-scheme: dark;
}

* {
    box-sizing: border-box;
}

html,
body {
    min-height: 100%;
    margin: 0;
}

body {
    width: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    background:
        radial-gradient(circle at 8% 0%, rgba(0, 190, 210, 0.09), transparent 28rem),
        var(--editor-bg);
    color: var(--editor-text);
    font-family: 'Cairo', system-ui, sans-serif;
    line-height: 1.6;
    user-select: auto;
}

html[dir="ltr"] body {
    font-family: 'Plus Jakarta Sans', system-ui, sans-serif;
}

button,
input,
select {
    font: inherit;
}

button,
a,
input,
select {
    -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
a:focus-visible,
input:focus-visible,
select:focus-visible {
    outline: 3px solid var(--editor-accent);
    outline-offset: 3px;
}

.editor-header {
    position: sticky;
    top: 0;
    z-index: 5;
    min-height: 74px;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 1rem;
    padding: 0.65rem clamp(1rem, 4vw, 3.5rem);
    border-bottom: 1px solid var(--editor-border);
    background: color-mix(in srgb, var(--editor-bg) 92%, transparent);
    backdrop-filter: blur(14px);
}

.editor-brand,
.back-link {
    display: inline-flex;
    align-items: center;
    gap: 0.65rem;
    color: var(--editor-text);
    font-weight: 800;
    text-decoration: none;
}

.editor-brand {
    justify-self: start;
}

.editor-brand img {
    width: 38px;
    height: 38px;
    object-fit: contain;
}

.back-link {
    justify-self: center;
    color: var(--editor-accent);
    font-size: 0.9rem;
}

.header-controls {
    display: flex;
    justify-self: end;
    gap: 0.5rem;
}

.icon-button {
    width: 42px;
    height: 42px;
    display: inline-grid;
    place-items: center;
    border: 1px solid var(--editor-border);
    border-radius: 50%;
    background: var(--editor-panel);
    color: var(--editor-text);
    cursor: pointer;
}

.editor-main {
    width: min(calc(100% - 2rem), 1180px);
    margin: 0 auto;
    padding: clamp(1.5rem, 4vw, 3.5rem) 0 2rem;
}

.intro {
    margin: 0 auto 1.75rem;
    text-align: center;
}

.eyebrow {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    margin: 0 0 0.4rem;
    color: var(--editor-accent);
    font-size: 0.86rem;
    font-weight: 700;
}

.intro h1 {
    margin: 0;
    font-size: clamp(2rem, 5vw, 3.2rem);
    line-height: 1.2;
}

.intro-copy {
    margin: 0.65rem 0 0;
    color: var(--editor-muted);
}

.upload-panel,
.preview-panel,
.tools-panel {
    border: 1px solid var(--editor-border);
    border-radius: 22px;
    background: var(--editor-panel);
    box-shadow: var(--editor-shadow);
}

.upload-panel {
    display: grid;
    justify-items: center;
    padding: clamp(1.5rem, 5vw, 3rem) 1.25rem;
    text-align: center;
    transition: border-color 160ms ease, background-color 160ms ease;
}

.upload-panel.is-dragging {
    border-color: var(--editor-accent);
    background: color-mix(in srgb, var(--editor-panel) 86%, var(--editor-accent));
}

.upload-icon {
    width: 68px;
    height: 68px;
    display: grid;
    place-items: center;
    border-radius: 20px;
    background: color-mix(in srgb, var(--editor-accent) 13%, transparent);
    color: var(--editor-accent);
    font-size: 1.7rem;
}

.upload-panel h2 {
    margin: 0.9rem 0 0;
    font-size: 1.35rem;
}

.upload-panel > p:not(.privacy-note) {
    max-width: 620px;
    margin: 0.4rem 0 1rem;
    color: var(--editor-muted);
}

.primary-button,
.tool-button,
.text-button {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.55rem;
    border-radius: 12px;
    cursor: pointer;
    transition: transform 140ms ease, opacity 140ms ease, background-color 140ms ease;
}

.primary-button:hover:not(:disabled),
.tool-button:hover:not(:disabled) {
    transform: translateY(-1px);
}

.primary-button {
    padding: 0.7rem 1.15rem;
    border: 1px solid var(--editor-accent);
    background: var(--editor-accent);
    color: var(--editor-accent-contrast);
    font-weight: 800;
}

.primary-button:disabled,
.tool-button:disabled {
    cursor: not-allowed;
    opacity: 0.48;
}

.privacy-note,
.ai-note {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    color: var(--editor-muted);
    font-size: 0.82rem;
}

.privacy-note {
    margin: 1rem 0 0;
}

.privacy-note i,
.ai-note i {
    margin-top: 0.3rem;
    color: var(--editor-accent);
}

.workspace {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(285px, 340px);
    align-items: start;
    gap: 1rem;
}

.workspace[hidden],
[hidden] {
    display: none !important;
}

.preview-panel,
.tools-panel {
    min-width: 0;
    padding: 1rem;
}

.preview-heading,
.panel-heading,
.range-control > span,
.format-control {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.preview-meta {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.preview-heading {
    margin-bottom: 0.75rem;
}

.dimensions {
    color: var(--editor-muted);
    font-size: 0.8rem;
    direction: ltr;
}

.canvas-stage {
    min-height: 280px;
    display: grid;
    place-items: center;
    overflow: hidden;
    border: 1px dashed var(--editor-border);
    border-radius: 14px;
    background-color: color-mix(in srgb, var(--editor-bg) 88%, #607080);
    background-image:
        linear-gradient(45deg, rgba(120, 140, 160, 0.08) 25%, transparent 25%),
        linear-gradient(-45deg, rgba(120, 140, 160, 0.08) 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, rgba(120, 140, 160, 0.08) 75%),
        linear-gradient(-45deg, transparent 75%, rgba(120, 140, 160, 0.08) 75%);
    background-size: 20px 20px;
    background-position: 0 0, 0 10px, 10px -10px, -10px 0;
}

.canvas-wrap {
    position: relative;
    max-width: 100%;
    max-height: min(68vh, 760px);
}

#previewCanvas {
    display: block;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: min(68vh, 760px);
    object-fit: contain;
}

.crop-overlay {
    position: absolute;
    z-index: 1;
    cursor: crosshair;
    touch-action: none;
}

.crop-selection {
    position: absolute;
    border: 2px solid #fff;
    box-shadow: 0 0 0 1px #08778b, 0 0 0 9999px rgba(5, 12, 20, 0.5);
    pointer-events: none;
}

.crop-hint {
    margin: 0.6rem 0 0;
    color: var(--editor-accent);
    font-size: 0.85rem;
}

.tools-panel {
    position: sticky;
    top: 90px;
}

.panel-heading {
    margin-bottom: 0.4rem;
}

.panel-heading h2,
.tool-group h3 {
    margin: 0;
    font-size: 1rem;
}

.text-button {
    min-height: 36px;
    padding: 0.2rem 0.45rem;
    border: 0;
    background: transparent;
    color: var(--editor-accent);
    font-size: 0.82rem;
    font-weight: 700;
}

.text-button:disabled {
    cursor: not-allowed;
    opacity: 0.45;
}

.tool-group {
    display: grid;
    gap: 0.7rem;
    padding: 0.85rem 0;
    border-top: 1px solid var(--editor-border);
}

.tool-buttons {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.4rem;
}

.tool-button {
    min-width: 0;
    min-height: 42px;
    padding: 0.45rem;
    border: 1px solid var(--editor-border);
    background: transparent;
    color: var(--editor-text);
    font-size: 0.78rem;
    font-weight: 700;
}

.tool-buttons .tool-button {
    flex-direction: column;
    gap: 0.1rem;
}

.crop-button {
    width: 100%;
}

.crop-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.5rem;
}

.range-control {
    display: grid;
    gap: 0.3rem;
    color: var(--editor-text);
    font-size: 0.84rem;
}

.range-control output {
    color: var(--editor-accent);
    font-variant-numeric: tabular-nums;
    direction: ltr;
}

.range-control input {
    width: 100%;
    accent-color: var(--editor-accent);
}

.format-control {
    color: var(--editor-text);
    font-size: 0.84rem;
}

.format-control select {
    min-height: 40px;
    min-width: 100px;
    padding: 0.35rem 0.6rem;
    border: 1px solid var(--editor-border);
    border-radius: 9px;
    background: var(--editor-panel);
    color: var(--editor-text);
}

.download-button {
    width: 100%;
}

.quality-control.is-disabled {
    opacity: 0.45;
}

.status-message {
    min-height: 1.5em;
    margin: 0.85rem 0 0;
    color: var(--editor-accent);
    text-align: center;
    font-size: 0.9rem;
}

.status-message.is-error {
    color: #c0392b;
}

.ai-note {
    justify-content: center;
    margin: 1rem auto 0;
    text-align: center;
}

@media (max-width: 820px) {
    .workspace {
        grid-template-columns: minmax(0, 1fr);
    }
    .tools-panel {
        position: static;
    }
    .canvas-wrap {
        max-height: 55vh;
    }
    #previewCanvas {
        max-height: 55vh;
    }
}

@media (max-width: 520px) {
    .editor-header {
        grid-template-columns: 1fr auto;
        gap: 0.5rem;
        padding-inline: 0.75rem;
    }
    .editor-brand {
        font-size: 0.9rem;
    }
    .editor-brand img {
        width: 32px;
        height: 32px;
    }
    .back-link {
        grid-column: 1;
        grid-row: 2;
        justify-self: start;
        margin-inline-start: 2.55rem;
        font-size: 0.78rem;
    }

    html[dir="ltr"] .back-link i {
        transform: rotate(180deg);
    }
    .header-controls {
        grid-column: 2;
        grid-row: 1 / span 2;
    }
    .editor-main {
        width: min(calc(100% - 1rem), 1180px);
        padding-top: 1.25rem;
    }
    .workspace {
        gap: 0.65rem;
    }
    .preview-panel,
    .tools-panel {
        padding: 0.75rem;
    }
    .canvas-stage {
        min-height: 220px;
    }
    .canvas-wrap {
        max-height: 48vh;
    }
    #previewCanvas {
        max-height: 48vh;
    }
}
