:root {
    --bg: #ffffff;
    --fg: #1f2933;
    --muted: #607080;
    --border: #d4dce5;
    --border-strong: #b8c3cf;
    --accent: #2769aa;
    --accent-hover: #20598f;
    --accent-soft: #eaf2fb;
    --danger: #be3a34;
    --danger-soft: #f8e8e7;
    --surface: #f6f8fb;
    --button-bg: #f5f5f5;
    --button-hover: #e6e6e6;
    --button-border: #d6d6d6;
    --button-disabled-bg: #f8f8f8;
    --runtime-radius: 3px;
    --disabled: #b7c4d0;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: var(--fg);
    background: transparent;
}

button,
input,
select {
    font: inherit;
}

#app {
    position: relative;
    width: 100%;
    height: 100%;
    min-width: 0;
    padding: 4px;
}

body.dialog-mode {
    overflow: auto;
    background: var(--bg);
}

body.dialog-pending #app {
    display: none;
}

body.dialog-mode #app {
    height: auto;
    min-height: 100%;
    padding: 8px;
}

body.dialog-mode .runtime {
    display: none;
}

body.dialog-mode .panel {
    position: static;
    inset: auto;
    display: block;
    overflow: visible;
    background: var(--bg);
}

body.dialog-mode .panel.hidden {
    display: block;
}

body.dialog-mode .panel-card {
    max-width: none;
    max-height: none;
    overflow: visible;
    border: none;
    border-radius: 0;
    box-shadow: none;
    padding: 0;
}

.runtime {
    width: 100%;
    min-width: 0;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 6px;
    align-items: center;
}

.runtime.runtime-icons-1 {
    grid-template-columns: minmax(0, 1fr) 30px;
}

.runtime.runtime-icons-2 {
    grid-template-columns: minmax(0, 1fr) 30px 30px;
}

.runtime.runtime-icons-1 .runtime-icon:not([hidden]) {
    grid-column: 2;
}

.runtime.runtime-icons-2 #info-btn {
    grid-column: 2;
}

.runtime.runtime-icons-2 #reset-btn {
    grid-column: 3;
}

.apply-btn {
    grid-column: 1;
    width: 100%;
    min-width: 0;
    height: 32px;
    padding: 0 10px;
    overflow: hidden;
    color: var(--fg);
    background: var(--button-bg);
    border: 1px solid var(--button-border);
    border-radius: var(--runtime-radius);
    font-size: 13px;
    font-weight: 650;
    line-height: 30px;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
    transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.apply-btn:hover:not(:disabled) {
    background: var(--button-hover);
    border-color: #cfcfcf;
}

.apply-btn:disabled {
    color: #9aa4ad;
    background: var(--button-disabled-bg);
    border-color: #e2e2e2;
    cursor: not-allowed;
}

.runtime-icon {
    width: 30px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    color: var(--muted);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--runtime-radius);
    font-size: 13px;
    font-weight: 700;
    line-height: 1;
    cursor: default;
}

.runtime-icon.action {
    cursor: pointer;
}

.runtime-icon.action:hover:not(:disabled) {
    color: var(--accent);
    border-color: var(--accent);
}

.runtime-icon.danger {
    color: var(--danger);
    background: var(--danger-soft);
    border-color: #e2aca8;
}

.runtime-icon:disabled {
    color: var(--disabled);
    cursor: not-allowed;
}

.runtime-config {
    grid-column: 1 / -1;
    min-height: 28px;
    padding: 5px 8px;
    color: var(--accent);
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: var(--runtime-radius);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
}

.runtime-config:hover {
    border-color: var(--accent);
}

.runtime-status {
    grid-column: 1 / -1;
    min-width: 0;
    color: var(--muted);
    font-size: 11px;
    line-height: 1.3;
    overflow-wrap: anywhere;
}

.runtime-status:empty {
    display: none;
}

.panel {
    position: absolute;
    inset: 4px;
    z-index: 10;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    min-width: 0;
    padding: 0;
    overflow: hidden;
    background: rgba(31, 41, 51, 0.28);
}

.panel.hidden {
    display: none;
}

.panel-card {
    width: 100%;
    max-width: 520px;
    min-width: 0;
    max-height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    background: var(--bg);
    border: 1px solid var(--border-strong);
    border-radius: 7px;
    box-shadow: 0 12px 32px rgba(31, 41, 51, 0.18);
    padding: 8px;
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
}

.panel-title {
    min-width: 0;
    color: var(--fg);
    font-size: 14px;
    font-weight: 700;
    line-height: 1.25;
}

.icon-btn {
    width: 28px;
    height: 28px;
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    color: var(--muted);
    background: transparent;
    border: 1px solid transparent;
    border-radius: 6px;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
}

.icon-btn:hover {
    color: var(--fg);
    background: var(--surface);
    border-color: var(--border);
}

.config-section {
    padding: 8px 0;
    border-top: 1px solid var(--border);
}

.config-section:first-of-type {
    border-top: none;
    padding-top: 0;
}

.section-title {
    margin-bottom: 6px;
    color: var(--muted);
    font-size: 10px;
    font-weight: 750;
    letter-spacing: 0;
    line-height: 1.2;
    text-transform: uppercase;
}

.settings-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 6px;
}

.field,
.check-field {
    min-width: 0;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.field-label {
    color: var(--muted);
    font-size: 10px;
    font-weight: 650;
    line-height: 1.2;
}

.field select,
.field input {
    width: 100%;
    min-width: 0;
    height: 28px;
    padding: 3px 7px;
    color: var(--fg);
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 12px;
    line-height: 18px;
}

.field select:focus,
.field input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-soft);
}

.check-field {
    display: flex;
    align-items: center;
    gap: 7px;
    min-height: 24px;
    color: var(--fg);
    font-size: 12px;
    line-height: 1.3;
}

.check-field input {
    width: 16px;
    height: 16px;
    margin: 0;
    flex: 0 0 auto;
}

.rows {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.mapping-row,
.extra-row {
    position: relative;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 6px;
    min-width: 0;
    padding: 6px 34px 6px 6px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 7px;
}

.extra-row .field,
.mapping-row .check-field {
    grid-column: 1 / -1;
}

.row-remove {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 22px;
    height: 22px;
    padding: 0;
    color: var(--muted);
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 15px;
    line-height: 1;
    cursor: pointer;
}

.row-remove:hover {
    color: var(--danger);
    border-color: #e2aca8;
}

.btn-primary,
.btn-ghost {
    min-height: 28px;
    padding: 4px 9px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 650;
    cursor: pointer;
}

.btn-primary {
    color: #ffffff;
    background: var(--accent);
    border: 1px solid var(--accent);
}

.btn-primary:hover:not(:disabled) {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
}

.btn-primary:disabled {
    color: #f7f9fb;
    background: var(--disabled);
    border-color: var(--disabled);
    cursor: not-allowed;
}

.btn-ghost {
    color: var(--accent);
    background: #ffffff;
    border: 1px solid var(--border);
}

.btn-ghost:hover:not(:disabled) {
    border-color: var(--accent);
}

.btn-ghost:disabled {
    color: var(--disabled);
    cursor: not-allowed;
}

#add-row,
#add-extra-row {
    width: 100%;
    margin-top: 6px;
}

.sql-toolbar {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
    margin-bottom: 6px;
}

.sql-toolbar .btn-ghost {
    flex: 0 0 auto;
}

.sql-copy-status {
    min-width: 0;
    color: var(--muted);
    font-size: 11px;
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sql-template {
    width: 100%;
    min-width: 0;
    min-height: 112px;
    resize: vertical;
    padding: 7px;
    color: var(--fg);
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
    font-size: 10.5px;
    line-height: 1.45;
    white-space: pre;
    overflow: auto;
}

.sql-template:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-soft);
}

.panel-footer {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 6px;
    padding-top: 8px;
    border-top: 1px solid var(--border);
}

@media (min-width: 420px) {
    #app {
        padding: 8px;
    }

    .settings-grid {
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    }

    .mapping-row {
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 68px 96px;
        align-items: end;
    }

    .mapping-row .field,
    .mapping-row .check-field {
        grid-column: auto;
    }

    .mapping-row .check-field {
        min-height: 32px;
    }

    .extra-row {
        grid-template-columns: 96px minmax(0, 1fr) minmax(0, 1fr);
        align-items: end;
    }

    .extra-row .field {
        grid-column: auto;
    }
}
