:root {
    --bg: #111315;
    --surface: #181b1e;
    --surface-raised: #202428;
    --surface-hover: #272c30;
    --border: #34393d;
    --border-strong: #484f54;
    --text: #f2f4f5;
    --text-secondary: #a6adb2;
    --text-muted: #747d83;
    --primary: #2f7d67;
    --primary-hover: #389579;
    --primary-soft: #193c33;
    --success: #59b890;
    --danger: #e06b65;
    --danger-soft: #462725;
    --focus: #7eb8ff;
    --mono: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    min-width: 320px;
    height: 100%;
    margin: 0;
}

body {
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.5 Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    letter-spacing: 0;
}

button,
input,
select,
textarea {
    color: inherit;
    font: inherit;
    letter-spacing: 0;
}

button,
select {
    cursor: pointer;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 1px;
}

.app-header {
    height: 56px;
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 0 20px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 9px;
    color: var(--text);
    font-size: 16px;
    font-weight: 700;
    text-decoration: none;
}

.brand-mark {
    width: 20px;
    height: 20px;
    border: 5px solid var(--success);
    border-right-color: #d9ad62;
    border-bottom-color: #d9ad62;
    border-radius: 5px;
}

.product-name {
    padding-left: 14px;
    border-left: 1px solid var(--border);
    color: var(--text-secondary);
}

.connection-state {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    margin-left: auto;
    color: var(--text-secondary);
    font-size: 12px;
}

.connection-state i {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--success);
    box-shadow: 0 0 0 3px rgba(89, 184, 144, .12);
}

.workspace {
    height: calc(100% - 56px);
    display: grid;
    grid-template-columns: minmax(300px, 360px) minmax(0, 1fr);
}

.control-panel {
    min-height: 0;
    overflow-y: auto;
    border-right: 1px solid var(--border);
    background: var(--surface);
}

.control-section {
    padding: 20px;
    border-bottom: 1px solid var(--border);
}

.section-heading,
.panel-header {
    display: flex;
    min-width: 0;
    align-items: center;
    justify-content: space-between;
}

.section-heading {
    margin-bottom: 16px;
}

.section-heading h2,
.panel-header h2 {
    margin: 0;
    font-size: 13px;
    font-weight: 650;
}

.section-heading span,
.panel-header span {
    color: var(--text-muted);
    font-size: 11px;
}

label {
    display: block;
    margin: 0 0 6px;
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 600;
}

input,
select,
textarea {
    width: 100%;
    min-width: 0;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
}

input,
select {
    height: 38px;
    padding: 0 10px;
    margin-bottom: 14px;
}

.endpoint-input {
    display: flex;
    min-width: 0;
    height: 38px;
    margin-bottom: 14px;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
}

.endpoint-input:focus-within {
    outline: 2px solid var(--focus);
    outline-offset: 1px;
}

.endpoint-input input {
    min-width: 72px;
    height: 36px;
    flex: 1;
    margin: 0;
    border: 0;
    border-radius: 0;
}

.endpoint-input input:focus-visible {
    outline: 0;
}

.endpoint-input span {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    padding: 0 9px;
    border-left: 1px solid var(--border);
    background: var(--surface-raised);
    color: var(--text-secondary);
    font: 11px/1 var(--mono);
    white-space: nowrap;
}

textarea {
    min-height: 104px;
    padding: 10px;
    resize: vertical;
}

input:hover,
select:hover,
textarea:hover {
    border-color: var(--border-strong);
}

input::placeholder,
textarea::placeholder {
    color: var(--text-muted);
}

.field-grid {
    display: grid;
    grid-template-columns: minmax(0, 1.65fr) minmax(98px, 1fr);
    gap: 10px;
}

.toggle-row {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin: 0 0 14px;
    color: var(--text-secondary);
    cursor: pointer;
}

.toggle-row input {
    width: 15px;
    height: 15px;
    margin: 0;
    accent-color: var(--primary);
}

.button-row,
.generate-actions {
    display: flex;
    gap: 8px;
}

.button {
    min-height: 38px;
    border: 1px solid transparent;
    border-radius: 6px;
    padding: 0 14px;
    font-weight: 650;
}

.button.secondary {
    flex: 1;
    border-color: var(--border-strong);
    background: var(--surface-raised);
}

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

.button.secondary.success {
    border-color: var(--primary);
    color: #a9ddca;
}

.button.primary {
    flex: 1;
    margin-top: 14px;
    background: var(--primary);
    color: white;
}

.button.primary:hover {
    background: var(--primary-hover);
}

.button:disabled {
    cursor: not-allowed;
    opacity: .55;
}

.icon-button {
    width: 30px;
    height: 30px;
    display: inline-grid;
    flex: 0 0 auto;
    place-items: center;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: transparent;
    color: var(--text-secondary);
    font-size: 20px;
    line-height: 1;
}

.icon-button:hover {
    border-color: var(--border-strong);
    background: var(--surface-hover);
    color: var(--text);
}

.upload-area {
    width: 100%;
    min-height: 96px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    margin-bottom: 14px;
    border: 1px dashed var(--border-strong);
    border-radius: 6px;
    background: var(--bg);
    color: var(--text-secondary);
}

.upload-area:hover,
.upload-area.dragover {
    border-color: var(--success);
    background: var(--primary-soft);
    color: var(--text);
}

.upload-area small {
    color: var(--text-muted);
    font-size: 11px;
}

.upload-symbol {
    color: var(--success);
    font-size: 22px;
    line-height: 1;
}

.image-preview {
    position: relative;
    min-height: 100px;
    max-height: 220px;
    margin-bottom: 14px;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: #090a0b;
}

.image-preview[hidden] {
    display: none;
}

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

.reference-card {
    position: relative;
    min-width: 0;
    aspect-ratio: 1;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 5px;
    background: #090a0b;
}

.reference-card img {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: contain;
}

.reference-card .remove-reference {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 24px;
    height: 24px;
    font-size: 16px;
}

.remove-image {
    position: absolute;
    top: 7px;
    right: 7px;
    background: rgba(17, 19, 21, .88);
}

.advanced-field {
    margin-top: 2px;
    border-top: 1px solid var(--border);
    padding-top: 10px;
}

.advanced-field summary {
    margin-bottom: 10px;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 12px;
    font-weight: 600;
}

.advanced-field input[type="file"] {
    height: auto;
    margin-bottom: 5px;
    padding: 6px;
    font-size: 11px;
}

.file-note {
    display: block;
    color: var(--text-muted);
    font-size: 11px;
}

.text-button {
    border: 0;
    padding: 0;
    background: transparent;
    color: var(--danger);
    font-size: 11px;
}

.text-button[hidden] {
    display: none;
}

.cancel {
    width: 38px;
    height: 38px;
    align-self: flex-end;
    border-color: var(--danger);
    color: var(--danger);
}

.cancel[hidden] {
    display: none;
}

.output-panel {
    min-width: 0;
    min-height: 0;
    display: grid;
    grid-template-rows: minmax(260px, 2fr) minmax(160px, 1fr);
    background: var(--bg);
}

.chat-section,
.log-section {
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.chat-section {
    border-bottom: 1px solid var(--border);
}

.panel-header {
    min-height: 44px;
    padding: 0 18px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}

.chat-window {
    min-height: 0;
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 14px;
    overflow-y: auto;
    padding: 22px;
}

.empty-state {
    display: flex;
    flex: 1;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: var(--text-muted);
}

.empty-state strong {
    font-size: 13px;
    font-weight: 600;
}

.empty-mark {
    width: 44px;
    height: 34px;
    position: relative;
    border: 1px solid var(--border-strong);
    border-radius: 6px;
    background: var(--surface);
}

.empty-mark::before,
.empty-mark::after {
    content: "";
    position: absolute;
    background: var(--primary);
}

.empty-mark::before {
    width: 18px;
    height: 1px;
    left: 6px;
    bottom: 9px;
    transform: rotate(-34deg);
}

.empty-mark::after {
    width: 16px;
    height: 1px;
    right: 5px;
    bottom: 11px;
    transform: rotate(38deg);
}

.msg {
    max-width: min(82%, 780px);
    padding: 10px 13px;
    overflow-wrap: anywhere;
    border: 1px solid var(--border);
    border-radius: 7px;
    line-height: 1.6;
}

.msg.user {
    align-self: flex-end;
    border-color: #376456;
    background: var(--primary-soft);
}

.msg.ai {
    align-self: flex-start;
    background: var(--surface-raised);
}

.msg img {
    width: auto;
    max-width: 100%;
    max-height: 70vh;
    display: block;
    margin-top: 10px;
    border-radius: 5px;
    cursor: zoom-in;
}

.log-window {
    min-height: 0;
    flex: 1;
    overflow: auto;
    padding: 12px 18px;
    color: #bdc5ca;
    font: 12px/1.55 var(--mono);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.log-entry {
    display: grid;
    grid-template-columns: minmax(130px, auto) 1fr;
    gap: 14px;
    padding: 7px 0;
    border-bottom: 1px solid #24282b;
}

.log-time {
    margin-right: 8px;
    color: var(--text-muted);
}

.log-key {
    color: var(--success);
    font-weight: 700;
}

.log-err,
.log-err .log-key {
    color: var(--danger);
}

::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-thumb {
    border: 2px solid transparent;
    border-radius: 8px;
    background: #454b50;
    background-clip: padding-box;
}

@media (max-width: 760px) {
    body {
        height: auto;
        min-height: 100%;
        overflow: auto;
    }

    .product-name {
        display: none;
    }

    .workspace {
        height: auto;
        grid-template-columns: 1fr;
    }

    .control-panel {
        overflow: visible;
        border-right: 0;
    }

    .output-panel {
        min-height: 680px;
        grid-template-rows: minmax(420px, 2fr) minmax(220px, 1fr);
        border-top: 1px solid var(--border);
    }

    .msg {
        max-width: 92%;
    }

   .log-entry {
       grid-template-columns: 1fr;
       gap: 3px;
   }
}

.panel-actions {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.small-button {
    min-height: 28px;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0 10px;
    background: var(--surface-raised);
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 600;
}

.small-button:hover {
    background: var(--surface-hover);
    color: var(--text);
}

.mask-preview {
    margin-top: 8px;
}

.mask-preview img {
    max-width: 100%;
    max-height: 160px;
    border: 1px solid var(--border);
    border-radius: 5px;
}

.mask-preview[hidden],
.advanced-field[hidden] {
    display: none;
}

.revised-prompt {
    margin-top: 6px;
    color: var(--text-muted);
    font-size: 12px;
    line-height: 1.5;
    overflow-wrap: anywhere;
}

.history-dialog {
    width: min(90vw, 920px);
    max-height: 86vh;
    display: flex;
    flex-direction: column;
    padding: 0;
    margin: auto;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--surface);
    color: var(--text);
}

.history-dialog::backdrop {
    background: rgba(0, 0, 0, .55);
}

.dialog-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

.dialog-header h2 {
    margin: 0;
    font-size: 14px;
    font-weight: 650;
}

.history-gallery {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 16px 20px;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 14px;
    align-content: start;
}

.history-empty {
    grid-column: 1 / -1;
    margin: 40px 0;
    text-align: center;
    color: var(--text-muted);
}

.history-card {
    margin: 0;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
}

.history-card img {
    width: 100%;
    max-height: 180px;
    display: block;
    object-fit: cover;
    cursor: zoom-in;
}

.history-card figcaption {
    padding: 8px 10px;
    color: var(--text-secondary);
    font-size: 11px;
    line-height: 1.4;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
}

.dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 14px 20px;
    border-top: 1px solid var(--border);
}

.dialog-footer .button {
    margin-top: 0;
}

@media (max-width: 760px) {
    .history-dialog {
        width: 96vw;
        max-width: none;
    }
}
