/*
 * KivTools file and media workbench
 *
 * A quiet three-stage surface: local files enter on the left, explicit
 * controls sit in the middle, and inspectable output appears on the right.
 * White is reserved for interactive surfaces; the page canvas remains calm.
 */

.kiv-files-tool {
    --fm-accent: var(--kt-accent, #178b58);
    --fm-accent-ink: var(--kt-accent-ink, #315e48);
    --fm-canvas: var(--kt-canvas, #f4f7f5);
    --fm-surface: var(--kt-surface, #fff);
    --fm-soft: var(--kt-surface-soft, #f7f9f8);
    --fm-line: var(--kt-line, #d8e2dc);
    --fm-line-strong: var(--kt-line-strong, #c4d3ca);
    --fm-ink: var(--kt-ink, #273a40);
    --fm-muted: var(--kt-muted, #74847f);
}

.kiv-files-tool .fm-tabs {
    margin-bottom: 18px;
}

.kiv-files-tool .fm-rail {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 18px;
    min-height: 42px;
    margin: 0 0 16px;
    padding: 9px 12px;
    border-left: 3px solid var(--fm-accent);
    background: rgba(255,255,255,.52);
    color: var(--fm-ink);
    font-size: 12px;
}

.kiv-files-tool .fm-rail.is-network {
    border-left-color: #2b8491;
}

.kiv-files-tool .fm-rail strong {
    font-weight: 750;
}

.kiv-files-tool .fm-rail small {
    color: var(--fm-muted);
    text-align: right;
}

.kiv-files-tool .fm-status {
    min-height: 0;
    margin: 0 0 12px;
    padding: 0;
    color: var(--fm-muted);
    font-size: 12px;
    font-weight: 650;
}

.kiv-files-tool .fm-status:not(:empty) {
    min-height: 38px;
    padding: 10px 12px;
    border-left: 3px solid var(--fm-accent);
    background: #edf6f0;
    color: #315e48;
}

.kiv-files-tool .fm-status.is-error:not(:empty) {
    border-left-color: #b85a5a;
    background: #fbefef;
    color: #874141;
}

.kiv-files-tool .fm-workflow {
    display: grid;
    grid-template-columns: minmax(250px, .84fr) minmax(310px, 1fr) minmax(280px, .92fr);
    gap: 14px;
    align-items: start;
}

.kiv-files-tool .fm-workflow.fm-two-column {
    grid-template-columns: minmax(320px, 1fr) minmax(320px, 1fr);
}

.kiv-files-tool .fm-panel {
    min-width: 0;
    overflow: hidden;
    border: 1px solid var(--fm-line);
    border-radius: 5px;
    background: var(--fm-surface);
    box-shadow: 0 8px 20px rgba(31,55,43,.035);
}

.kiv-files-tool .fm-panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 44px;
    padding: 10px 13px;
    border-bottom: 1px solid var(--fm-line);
    background: var(--fm-soft);
}

.kiv-files-tool .fm-panel-head strong {
    color: var(--fm-ink);
    font-size: 12px;
    font-weight: 780;
    letter-spacing: .015em;
}

.kiv-files-tool .fm-panel-head small {
    color: var(--fm-muted);
    font-size: 10px;
    text-align: right;
}

.kiv-files-tool .fm-panel-body {
    padding: 13px;
}

.kiv-files-tool .fm-stage {
    position: relative;
}

.kiv-files-tool .fm-stage::before {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    margin-right: 8px;
    border-radius: 50%;
    background: #e9f4ed;
    color: var(--fm-accent-ink);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 10px;
    font-weight: 800;
    content: attr(data-stage);
}

.kiv-files-tool .fm-dropzone {
    position: relative;
    display: flex;
    min-height: 138px;
    padding: 22px 16px;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border: 1px dashed #aac9b8;
    border-radius: 4px;
    background: #fbfdfc;
    color: var(--fm-ink);
    cursor: pointer;
    text-align: center;
    transition: border-color .16s ease, background .16s ease;
}

.kiv-files-tool .fm-dropzone:hover,
.kiv-files-tool .fm-dropzone:focus,
.kiv-files-tool .fm-dropzone.is-dragging {
    border-color: var(--fm-accent);
    background: #f1f8f4;
    outline: 0;
}

.kiv-files-tool .fm-dropzone::before {
    color: var(--fm-accent);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 22px;
    line-height: 1;
    content: "⇣";
}

.kiv-files-tool .fm-dropzone input[type="file"] {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
}

.kiv-files-tool .fm-dropzone strong {
    font-size: 13px;
    font-weight: 760;
}

.kiv-files-tool .fm-dropzone small,
.kiv-files-tool .fm-note {
    color: var(--fm-muted);
    font-size: 10px;
    line-height: 1.55;
}

.kiv-files-tool .fm-file-list {
    display: grid;
    gap: 6px;
    margin: 10px 0 0;
    padding: 0;
    list-style: none;
}

.kiv-files-tool .fm-file-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    align-items: center;
    min-height: 38px;
    padding: 7px 9px;
    border-left: 2px solid var(--fm-line-strong);
    background: var(--fm-soft);
}

.kiv-files-tool .fm-file-name {
    min-width: 0;
    overflow: hidden;
    color: var(--fm-ink);
    font-size: 11px;
    font-weight: 680;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.kiv-files-tool .fm-file-meta {
    color: var(--fm-muted);
    font-size: 9px;
}

.kiv-files-tool .fm-row-actions {
    display: inline-flex;
    gap: 3px;
}

.kiv-files-tool .fm-icon-button {
    width: 28px;
    min-width: 28px;
    height: 28px;
    padding: 0;
    border: 0;
    border-radius: 3px;
    background: #e9efeb;
    color: #52675d;
    font-size: 12px;
    line-height: 28px;
    text-align: center;
}

.kiv-files-tool .fm-icon-button:hover,
.kiv-files-tool .fm-icon-button:focus {
    background: #dce9e1;
    color: var(--fm-accent-ink);
    outline: 0;
}

.kiv-files-tool .fm-field {
    display: grid;
    gap: 5px;
    margin-bottom: 12px;
}

.kiv-files-tool .fm-field-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.kiv-files-tool .fm-field-row.fm-field-row-3 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.kiv-files-tool .fm-field-row.fm-field-row-4 {
    grid-template-columns: repeat(4, minmax(0, 1fr));
}

.kiv-files-tool [hidden] {
    display: none !important;
}

.kiv-files-tool .fm-field label,
.kiv-files-tool .fm-check-label {
    margin: 0;
    color: #465c52;
    font-size: 10px;
    font-weight: 760;
    letter-spacing: .02em;
}

.kiv-files-tool .fm-choice-stack {
    display: grid;
    gap: 2px;
    margin-top: 12px;
    padding: 8px 11px;
    border-left: 2px solid var(--fm-line-strong);
    background: var(--fm-soft);
}

.kiv-files-tool .fm-inline-check {
    margin-top: 12px;
}

.kiv-files-tool input[type="color"].fm-input {
    min-height: 39px;
    padding: 4px;
}

.kiv-files-tool .fm-input,
.kiv-files-tool .fm-select,
.kiv-files-tool .fm-textarea {
    width: 100%;
    min-height: 39px;
    padding: 8px 10px;
    border: 1px solid var(--fm-line-strong);
    border-radius: 4px;
    background: #fff;
    color: var(--fm-ink);
    font-size: 12px;
    box-shadow: none;
}

.kiv-files-tool .fm-textarea {
    min-height: 180px;
    resize: vertical;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    line-height: 1.55;
}

.kiv-files-tool .fm-input:focus,
.kiv-files-tool .fm-select:focus,
.kiv-files-tool .fm-textarea:focus {
    border-color: var(--fm-accent);
    outline: 0;
    box-shadow: 0 0 0 2px rgba(23,139,88,.11);
}

.kiv-files-tool .fm-check-label {
    display: flex;
    align-items: center;
    gap: 7px;
    min-height: 34px;
}

.kiv-files-tool .fm-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 7px;
    margin-top: 12px;
}

.kiv-files-tool .fm-button {
    min-height: 38px;
    padding: 8px 13px;
    border: 1px solid transparent;
    border-radius: 4px;
    background: var(--fm-accent);
    color: #fff;
    font-size: 11px;
    font-weight: 760;
    line-height: 1.2;
}

.kiv-files-tool .fm-button:hover,
.kiv-files-tool .fm-button:focus {
    background: #117447;
    color: #fff;
    outline: 0;
}

.kiv-files-tool .fm-button:disabled {
    cursor: not-allowed;
    opacity: .55;
}

.kiv-files-tool .fm-button.fm-secondary {
    border-color: var(--fm-line-strong);
    background: #fff;
    color: #496056;
}

.kiv-files-tool .fm-button.fm-secondary:hover,
.kiv-files-tool .fm-button.fm-secondary:focus {
    border-color: #9bbbaa;
    background: #f1f6f3;
    color: var(--fm-accent-ink);
}

.kiv-files-tool .fm-metrics {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
}

.kiv-files-tool .fm-metric {
    min-width: 0;
    padding: 9px 10px;
    border-left: 2px solid var(--fm-line-strong);
    background: var(--fm-soft);
}

.kiv-files-tool .fm-metric strong {
    display: block;
    overflow: hidden;
    color: var(--fm-ink);
    font-size: 14px;
    font-weight: 780;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.kiv-files-tool .fm-metric span {
    color: var(--fm-muted);
    font-size: 9px;
}

.kiv-files-tool .fm-empty,
.kiv-files-tool .fm-output {
    min-height: 118px;
    padding: 13px;
    border: 1px solid var(--fm-line);
    border-radius: 4px;
    background: #fbfdfc;
    color: var(--fm-muted);
    font-size: 11px;
    line-height: 1.6;
}

.kiv-files-tool .fm-output {
    overflow: auto;
    color: var(--fm-ink);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    white-space: pre-wrap;
    word-break: break-word;
}

.kiv-files-tool .fm-preview {
    display: grid;
    min-height: 180px;
    place-items: center;
    overflow: hidden;
    border: 1px solid var(--fm-line);
    border-radius: 4px;
    background-color: #f8faf9;
    background-image: linear-gradient(45deg,#e9eeeb 25%,transparent 25%),linear-gradient(-45deg,#e9eeeb 25%,transparent 25%),linear-gradient(45deg,transparent 75%,#e9eeeb 75%),linear-gradient(-45deg,transparent 75%,#e9eeeb 75%);
    background-position: 0 0,0 6px,6px -6px,-6px 0;
    background-size: 12px 12px;
}

.kiv-files-tool .fm-preview img,
.kiv-files-tool .fm-preview canvas {
    display: block;
    max-width: 100%;
    max-height: 420px;
    object-fit: contain;
}

.kiv-files-tool .fm-progress {
    height: 4px;
    margin-top: 10px;
    overflow: hidden;
    background: #e6eee9;
}

.kiv-files-tool .fm-progress > span {
    display: block;
    width: 0;
    height: 100%;
    background: var(--fm-accent);
    transition: width .18s ease;
}

.kiv-files-tool .fm-table-wrap {
    max-height: 480px;
    overflow: auto;
    border: 1px solid var(--fm-line);
}

.kiv-files-tool .fm-table {
    width: 100%;
    margin: 0;
    border-collapse: collapse;
    font-size: 10px;
}

.kiv-files-tool .fm-table th,
.kiv-files-tool .fm-table td {
    padding: 8px 9px;
    border-bottom: 1px solid var(--fm-line);
    color: var(--fm-ink);
    text-align: left;
    vertical-align: top;
}

.kiv-files-tool .fm-table th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--fm-soft);
    color: #52675d;
    font-size: 9px;
    text-transform: uppercase;
    letter-spacing: .045em;
}

.kiv-files-tool .fm-risk-high { color: #a43d3d; font-weight: 760; }
.kiv-files-tool .fm-risk-medium { color: #9a6a20; font-weight: 760; }
.kiv-files-tool .fm-risk-clear { color: #27724a; font-weight: 760; }

.kiv-files-tool .fm-form-workflow {
    grid-template-columns: minmax(290px, .72fr) minmax(520px, 1.55fr);
}

.kiv-files-tool .fm-form-fields {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    column-gap: 18px;
    row-gap: 0;
}

.kiv-files-tool .fm-form-field {
    display: grid;
    gap: 6px;
    min-width: 0;
    padding: 11px 0 13px;
    border-bottom: 1px solid var(--fm-line);
}

.kiv-files-tool .fm-form-field label {
    overflow: hidden;
    margin: 0;
    color: #465c52;
    font-size: 10px;
    font-weight: 760;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.kiv-files-tool .fm-form-field textarea,
.kiv-files-tool .fm-form-field select {
    width: 100%;
    min-height: 38px;
    padding: 8px 9px;
    border: 1px solid var(--fm-line-strong);
    border-radius: 4px;
    background: var(--fm-surface);
    color: var(--fm-ink);
    font-size: 11px;
    resize: vertical;
}

.kiv-files-tool .fm-form-field.is-checkbox {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
}

.kiv-files-tool .fm-readonly-field {
    color: var(--fm-muted);
    font-size: 10px;
}

.kiv-files-tool .fm-compare-workflow {
    grid-template-columns: minmax(250px, .72fr) minmax(250px, .66fr) minmax(430px, 1.35fr);
}

.kiv-files-tool .fm-compare-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 9px;
}

.kiv-files-tool .fm-compare-card {
    min-width: 0;
    overflow: hidden;
    background: var(--fm-soft);
}

.kiv-files-tool .fm-compare-card header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 9px;
}

.kiv-files-tool .fm-compare-card header strong {
    color: var(--fm-ink);
    font-size: 10px;
}

.kiv-files-tool .fm-compare-card header span {
    padding: 2px 6px;
    border-radius: 999px;
    background: #f8dddd;
    color: #923c46;
    font-size: 8px;
    font-weight: 780;
}

.kiv-files-tool .fm-compare-card header span.is-clear {
    background: #e1f1e7;
    color: #2d744e;
}

.kiv-files-tool .fm-compare-card img {
    display: block;
    width: 100%;
    max-height: 260px;
    object-fit: contain;
    background: #fff;
}

.kiv-files-tool .fm-compare-card > small {
    display: block;
    padding: 7px 9px 9px;
    color: var(--fm-muted);
    font-size: 9px;
}

.kiv-files-tool .fm-analysis-preview {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 9px;
    min-height: 0;
    margin-top: 10px;
    padding: 9px;
    place-items: stretch;
}

.kiv-files-tool .fm-canvas-card {
    display: grid;
    min-width: 0;
    margin: 0;
    overflow: hidden;
    border-radius: 3px;
    background: var(--fm-surface);
}

.kiv-files-tool .fm-canvas-card figcaption {
    overflow: hidden;
    padding: 7px 9px;
    border-bottom: 1px solid var(--fm-line);
    color: var(--fm-muted);
    font-size: 9px;
    font-weight: 720;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.kiv-files-tool .fm-canvas-card canvas {
    width: 100%;
    height: auto;
    min-height: 110px;
    max-height: 360px;
    object-fit: contain;
}

.kiv-files-tool .fm-ratio-result {
    display: grid;
    grid-template-columns: minmax(180px, .72fr) minmax(260px, 1.28fr);
    gap: 12px;
    align-items: start;
    margin-top: 10px;
}

.kiv-files-tool .fm-ratio-visual {
    display: grid;
    width: min(100%, 420px);
    min-height: 130px;
    place-items: center;
    border-radius: 4px;
    background: linear-gradient(145deg, #1d9b65, #126f49);
    color: #fff;
    font: 750 18px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    box-shadow: inset 0 0 0 1px rgba(255,255,255,.18);
}

.kiv-files-tool .fm-code-output {
    min-height: 150px;
    max-height: 360px;
    margin: 0;
    overflow: auto;
    border: 1px solid var(--fm-line);
    border-radius: 4px;
    background: #111a16;
    color: #bfe8d1;
    font: 10px/1.62 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    white-space: pre-wrap;
}

.kiv-files-tool .fm-sprite-output {
    display: grid;
    grid-template-columns: minmax(220px, .9fr) minmax(300px, 1.1fr);
    gap: 10px;
    margin-top: 10px;
}

.kiv-files-tool .fm-sprite-output .fm-analysis-preview {
    margin: 0;
}

html.kiv-shell-dark .kiv-files-tool {
    --fm-surface: #17221d;
    --fm-soft: #1c2923;
    --fm-line: #31423a;
    --fm-line-strong: #41554b;
    --fm-ink: #dce9e2;
    --fm-muted: #92a69b;
}

html.kiv-shell-dark .kiv-files-tool .fm-rail,
html.kiv-shell-dark .kiv-files-tool .fm-dropzone,
html.kiv-shell-dark .kiv-files-tool .fm-input,
html.kiv-shell-dark .kiv-files-tool .fm-select,
html.kiv-shell-dark .kiv-files-tool .fm-textarea,
html.kiv-shell-dark .kiv-files-tool .fm-empty,
html.kiv-shell-dark .kiv-files-tool .fm-output,
html.kiv-shell-dark .kiv-files-tool .fm-preview,
html.kiv-shell-dark .kiv-files-tool .fm-button.fm-secondary {
    background-color: #111a16;
    color: var(--fm-ink);
}

html.kiv-shell-dark .kiv-files-tool .fm-form-field textarea,
html.kiv-shell-dark .kiv-files-tool .fm-form-field select {
    background: #111a16;
    color: var(--fm-ink);
}

html.kiv-shell-dark .kiv-files-tool .fm-form-field label {
    color: var(--fm-ink);
}

html.kiv-shell-dark .kiv-files-tool .fm-field label,
html.kiv-shell-dark .kiv-files-tool .fm-check-label {
    color: #b8c9c0;
}

html.kiv-shell-dark .kiv-files-tool .fm-input::placeholder,
html.kiv-shell-dark .kiv-files-tool .fm-textarea::placeholder {
    color: #71877c;
    opacity: 1;
}

html.kiv-shell-dark .kiv-files-tool .fm-select {
    color-scheme: dark;
}

html.kiv-shell-dark .kiv-files-tool .fm-select option {
    background: #111a16;
    color: var(--fm-ink);
}

html.kiv-shell-dark .kiv-files-tool input[type="checkbox"],
html.kiv-shell-dark .kiv-files-tool input[type="radio"] {
    accent-color: #4eaf77;
}

html.kiv-shell-dark .kiv-files-tool .fm-icon-button {
    background: #26382f;
    color: #a9c2b4;
}

html.kiv-shell-dark .kiv-files-tool .fm-icon-button:hover,
html.kiv-shell-dark .kiv-files-tool .fm-icon-button:focus {
    background: #315043;
    color: #d9f2e3;
}

html.kiv-shell-dark .kiv-files-tool .fm-preview {
    background-color: #111a16;
    background-image:
        linear-gradient(45deg, #1d2c25 25%, transparent 25%),
        linear-gradient(-45deg, #1d2c25 25%, transparent 25%),
        linear-gradient(45deg, transparent 75%, #1d2c25 75%),
        linear-gradient(-45deg, transparent 75%, #1d2c25 75%);
}

html.kiv-shell-dark .kiv-files-tool .fm-progress {
    background: #293b32;
}

html.kiv-shell-dark .kiv-files-tool .fm-table-wrap {
    background: #111a16;
}

html.kiv-shell-dark .kiv-files-tool .fm-status:not(:empty) {
    background: #193126;
    color: #a9d5bd;
}

html.kiv-shell-dark .kiv-files-tool .fm-status.is-error:not(:empty) {
    background: #3b2525;
    color: #e7b0b0;
}

html.kiv-shell-dark .kiv-files-tool .fm-canvas-card {
    background: #17221d;
}

@media (max-width: 1080px) {
    .kiv-files-tool .fm-workflow,
    .kiv-files-tool .fm-workflow.fm-two-column {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .kiv-files-tool .fm-workflow > :last-child:nth-child(3) {
        grid-column: 1 / -1;
    }
    .kiv-files-tool .fm-form-workflow,
    .kiv-files-tool .fm-compare-workflow {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
    .kiv-files-tool .fm-form-workflow > :last-child,
    .kiv-files-tool .fm-compare-workflow > :last-child {
        grid-column: 1 / -1;
    }
}

@media (max-width: 767px) {
    .kiv-files-tool .fm-rail {
        align-items: flex-start;
        flex-direction: column;
        gap: 4px;
    }
    .kiv-files-tool .fm-rail small { text-align: left; }
    .kiv-files-tool .fm-workflow,
    .kiv-files-tool .fm-workflow.fm-two-column {
        grid-template-columns: 1fr;
    }
    .kiv-files-tool .fm-workflow > :last-child:nth-child(3) { grid-column: auto; }
    .kiv-files-tool .fm-field-row,
    .kiv-files-tool .fm-field-row.fm-field-row-3,
    .kiv-files-tool .fm-field-row.fm-field-row-4 { grid-template-columns: 1fr; }
    .kiv-files-tool .fm-form-workflow,
    .kiv-files-tool .fm-compare-workflow { grid-template-columns: 1fr; }
    .kiv-files-tool .fm-form-workflow > :last-child,
    .kiv-files-tool .fm-compare-workflow > :last-child { grid-column: auto; }
    .kiv-files-tool .fm-form-fields,
    .kiv-files-tool .fm-compare-grid { grid-template-columns: 1fr; }
    .kiv-files-tool .fm-ratio-result,
    .kiv-files-tool .fm-sprite-output { grid-template-columns: 1fr; }
    .kiv-files-tool .fm-analysis-preview { grid-template-columns: 1fr; }
    .kiv-files-tool .fm-metrics { grid-template-columns: 1fr 1fr; }
    .kiv-files-tool .fm-panel-body { padding: 11px; }
    .kiv-files-tool .fm-dropzone { min-height: 118px; }
}

@media (prefers-reduced-motion: reduce) {
    .kiv-files-tool .fm-dropzone,
    .kiv-files-tool .fm-progress > span { transition: none; }
}
