/* The knapsack problem (/knapsack-problem): the item boxes, the backpack that fills by weight, and the item list in
   the control panel. The table view uses css/apps/dp.css. Tiles, buttons, fields and the callout come from the app kit.
   Loaded only on its page (Views/SeoLanding/_KnapsackProblemDemo.cshtml, filter-engines/knapsack-problem.js). */

.ks-app {
    /* The backpack is an illustration: the same warm colours on both themes. */
    --ks-bag: #f59e0b;
    --ks-bag-flap: #d97706;
    --ks-bag-dark: #92400e;
    --ks-bag-inside: #431407;
    --ks-label: #ffffff;
    --ks-label-text: #0f172a;
}

/* ---- Views and the status line ----------------------------------------------------------------------------------- */

/* One view at a time (data-ks-current, set by the script). */
.ks-app[data-ks-current="bag"] [data-ks-view="table"],
.ks-app[data-ks-current="table"] [data-ks-view="bag"] {
    display: none !important;
}

.ks-app .ks-views {
    align-self: stretch;
}

.ks-app .ks-say {
    min-height: 3.1rem;
    align-items: center;
}

.ks-app .ks-say > .ak-icon {
    align-self: flex-start;
    margin-top: 0.1rem;
}

.ks-app .ks-say-body {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0.25rem 0.6rem;
}

.ks-app .ks-say--success {
    border-color: color-mix(in srgb, var(--ak-success) 45%, var(--ak-border));
    background: color-mix(in srgb, var(--ak-success) 10%, var(--ak-panel));
}

.ks-app .ks-say--success > .ak-icon {
    color: var(--ak-success);
}

.ks-app .ks-see-how {
    margin: 0;
    padding: 0;
    border: 0;
    background: none;
    color: var(--ak-accent);
    font: inherit;
    font-weight: 700;
    text-decoration: underline;
    text-underline-offset: 2px;
    cursor: pointer;
}

.ks-app .ks-see-how:focus-visible {
    outline: 2px solid var(--ak-accent);
    outline-offset: 2px;
    border-radius: 3px;
}

.ks-app .ks-best-tile.is-found {
    --ak-stat-color: var(--ak-success) !important;
}

/* ---- Scene: the shelf of boxes beside the bag ------------------------------------------------------------------- */

.ks-app .ks-scene {
    container-type: inline-size;
}

.ks-app .ks-scene-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(190px, 34%);
    align-items: center;
    gap: var(--ak-space-5);
}


.ks-app .ks-shelf {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
    gap: var(--ak-space-4) var(--ak-space-3);
    margin: 0;
    padding: 0;
    list-style: none;
}

/* Each box stands on a plank; heavier items are bigger boxes (--ks-s, set by the script). */
.ks-app .ks-slot {
    display: flex;
    align-items: flex-end;
    justify-content: center;
    aspect-ratio: 1 / 1;
    padding-bottom: 6px;
    border-bottom: 5px solid color-mix(in srgb, var(--ks-bag-dark) 55%, var(--ak-border));
    border-radius: 0 0 4px 4px;
}

.ks-app .ks-box {
    --ks-s: 1;
    position: relative;
    display: block;
    width: calc(var(--ks-s) * 100%);
    aspect-ratio: 1 / 0.92;
    margin: 0;
    padding: 0;
    border: 0;
    background: none;
    color: var(--ks-label-text);
    font: inherit;
    cursor: pointer;
    transition: transform 0.15s ease, opacity 0.2s ease, filter 0.2s ease;
}

.ks-app .ks-box:hover {
    transform: translateY(-3px);
}

.ks-app .ks-box:focus-visible {
    outline: 3px solid var(--ak-accent);
    outline-offset: 4px;
    border-radius: 6px;
}

/* A box drawn from three faces: the top, the right side and the front, in shades of the item's colour. */
.ks-app .ks-box-top,
.ks-app .ks-box-side,
.ks-app .ks-box-front {
    position: absolute;
    inset: 0;
}

.ks-app .ks-box-top {
    background: color-mix(in srgb, var(--ks-c) 62%, #ffffff);
    clip-path: polygon(0 16%, 14% 0, 100% 0, 86% 16%);
}

.ks-app .ks-box-side {
    background: color-mix(in srgb, var(--ks-c) 68%, #000000);
    clip-path: polygon(86% 16%, 100% 0, 100% 84%, 86% 100%);
}

.ks-app .ks-box-front {
    inset: 16% 14% 0 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.05rem;
    padding: 10% 6%;
    background: var(--ks-c);
    box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--ks-c) 70%, #000000);
}

/* The sticker on the front: the item number, the dollars and the kilograms. */
/* The item number hangs on the top left corner, the check mark (in the bag) on the top right one. */
.ks-app .ks-box-name {
    position: absolute;
    top: -0.45rem;
    inset-inline-start: -0.45rem;
    min-width: 1.4rem;
    height: 1.4rem;
    padding: 0 0.3rem;
    border-radius: 999px;
    background: color-mix(in srgb, var(--ks-c) 70%, #000000);
    box-shadow: 0 0 0 2px var(--ak-panel);
    color: #ffffff;
    font-size: 0.75rem;
    font-weight: 800;
    line-height: 1.4rem;
    text-align: center;
}

.ks-app .ks-box-value,
.ks-app .ks-box-weight {
    padding: 0.05rem 0.4rem;
    border-radius: 5px;
    background: var(--ks-label);
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
    line-height: 1.25;
}

.ks-app .ks-box-value {
    font-size: 1rem;
    font-weight: 800;
}

.ks-app .ks-box-weight {
    font-size: 0.78rem;
    font-weight: 700;
    color: color-mix(in srgb, var(--ks-label-text) 75%, #ffffff);
}

.ks-app .ks-box-check {
    position: absolute;
    top: -0.35rem;
    inset-inline-end: -0.35rem;
    display: none;
    width: 1.6rem;
    height: 1.6rem;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    background: var(--ak-success);
    color: #ffffff;
    box-shadow: 0 0 0 2px var(--ak-panel);
}

.ks-app .ks-box-check svg {
    width: 1rem;
    height: 1rem;
}

/* In the bag: the box leaves a faded outline on the shelf, with a check mark. */
.ks-app .ks-box.is-in .ks-box-top,
.ks-app .ks-box.is-in .ks-box-side,
.ks-app .ks-box.is-in .ks-box-front {
    opacity: 0.22;
}

.ks-app .ks-box.is-in .ks-box-front {
    outline: 2px dashed var(--ks-c);
    outline-offset: -2px;
}

.ks-app .ks-box.is-in .ks-box-check {
    display: flex;
}

/* Too heavy for the space left. */
.ks-app .ks-box.is-too-heavy {
    opacity: 0.5;
    filter: saturate(0.35);
    cursor: not-allowed;
}

.ks-app .ks-box.is-too-heavy:hover {
    transform: none;
}

.ks-app .ks-box.ks-shake {
    animation: ks-shake 0.42s ease;
}

@keyframes ks-shake {
    0%, 100% { transform: translateX(0); }
    20% { transform: translateX(-6px); }
    40% { transform: translateX(6px); }
    60% { transform: translateX(-4px); }
    80% { transform: translateX(3px); }
}

/* ---- The backpack ------------------------------------------------------------------------------------------------ */

.ks-app .ks-bag {
    position: relative;
    justify-self: center;
    width: 100%;
    max-width: 280px;
    aspect-ratio: 220 / 300;
}

.ks-app .ks-bag-art {
    display: block;
    width: 100%;
    height: 100%;
    overflow: visible;
}

.ks-app .ks-art-handle {
    fill: none;
    stroke: var(--ks-bag-dark);
    stroke-width: 10;
    stroke-linecap: round;
}

.ks-app .ks-art-strap { fill: var(--ks-bag-dark); }
.ks-app .ks-art-body { fill: var(--ks-bag); }
.ks-app .ks-art-shade { fill: var(--ks-bag-flap); }
.ks-app .ks-art-flap { fill: var(--ks-bag-flap); }
.ks-app .ks-art-buckle { fill: var(--ks-bag-dark); }
.ks-app .ks-art-window { fill: var(--ks-bag-inside); }

.ks-app .ks-art-stitch {
    fill: none;
    stroke: color-mix(in srgb, var(--ks-bag-dark) 70%, transparent);
    stroke-width: 1.5;
    stroke-dasharray: 5 4;
}

/* The capacity on the flap (the SVG's flap is y 40 to 118 of 300). */
.ks-app .ks-bag-cap {
    position: absolute;
    top: 15%;
    left: 50%;
    transform: translateX(-50%);
    color: #fff7ed;
    font-size: clamp(1.1rem, 4.2cqi, 1.6rem);
    font-weight: 800;
    letter-spacing: 0.01em;
    white-space: nowrap;
    text-shadow: 0 1px 0 color-mix(in srgb, var(--ks-bag-dark) 80%, transparent);
    pointer-events: none;
}

/* The opening of the bag (the SVG's window: x 40, y 132, 140 x 146 of 220 x 300). The free space sits on top and the
   packed items below it, each as tall as its share of the capacity. */
.ks-app .ks-inside {
    position: absolute;
    left: calc(40 / 220 * 100%);
    top: calc(132 / 300 * 100%);
    width: calc(140 / 220 * 100%);
    height: calc(146 / 300 * 100%);
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 5px;
    border-radius: 11%/10%;
    overflow: hidden;
}

.ks-app .ks-free {
    flex: 1 1 0;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    border: 2px dashed color-mix(in srgb, #ffffff 30%, transparent);
    border-radius: 8px;
    color: color-mix(in srgb, #ffffff 78%, transparent);
    font-size: 0.8rem;
    font-weight: 700;
    white-space: nowrap;
}

.ks-app .ks-bag.is-full .ks-inside {
    box-shadow: inset 0 0 0 2px var(--ak-success);
}

.ks-app .ks-layer {
    flex: 1 1 0;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    margin: 0;
    padding: 0 0.35rem;
    overflow: hidden;
    border: 0;
    border-radius: 6px;
    background: var(--ks-c);
    box-shadow: inset 0 -3px 0 color-mix(in srgb, var(--ks-c) 65%, #000000);
    color: var(--ks-label-text);
    font: inherit;
    cursor: pointer;
}

.ks-app .ks-layer:hover {
    filter: brightness(1.08);
}

.ks-app .ks-layer:focus-visible {
    outline: 2px solid #ffffff;
    outline-offset: -2px;
}

.ks-app .ks-layer-name {
    flex: none;
    min-width: 1.2rem;
    height: 1.2rem;
    border-radius: 999px;
    background: color-mix(in srgb, #000000 28%, transparent);
    color: #ffffff;
    font-size: 0.7rem;
    font-weight: 800;
    line-height: 1.2rem;
    text-align: center;
}

.ks-app .ks-layer-text {
    min-width: 0;
    overflow: hidden;
    padding: 0.05rem 0.35rem;
    border-radius: 4px;
    background: var(--ks-label);
    font-size: 0.74rem;
    font-weight: 800;
    font-variant-numeric: tabular-nums;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ks-app .ks-layer.is-thin .ks-layer-text { display: none; }
.ks-app .ks-layer.is-tiny .ks-layer-name { display: none; }

.ks-app .ks-layer--new {
    animation: ks-drop 0.38s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}

@keyframes ks-drop {
    from { transform: translateY(-120%); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.ks-app .ks-actions {
    margin-top: var(--ak-space-4);
}

.ks-app .ks-problem {
    margin-top: var(--ak-space-3);
}

/* A narrow stage (phones): the bag on top, smaller boxes below it. */
@container (max-width: 560px) {
    .ks-app .ks-scene .ks-scene-grid {
        grid-template-columns: minmax(0, 1fr);
        gap: var(--ak-space-4);
    }

    .ks-app .ks-scene .ks-bag {
        order: -1;
        width: min(210px, 64%);
    }

    .ks-app .ks-scene .ks-shelf {
        grid-template-columns: repeat(auto-fill, minmax(78px, 1fr));
        gap: var(--ak-space-4) var(--ak-space-2);
    }

    .ks-app .ks-scene .ks-box-value { font-size: 0.9rem; }
    .ks-app .ks-scene .ks-box-weight { font-size: 0.72rem; }
}

/* While Best packing fills the bag, the boxes wait. */
.ks-app.ks-is-solving .ks-box,
.ks-app.ks-is-solving .ks-layer {
    pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
    .ks-app .ks-box,
    .ks-app .ks-layer--new,
    .ks-app .ks-box.ks-shake {
        transition: none;
        animation: none;
    }
}

/* ---- Item list in the control panel ------------------------------------------------------------------------------ */

.ks-app .ks-item-list {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin: 0.15rem 0 0.5rem;
    padding: 0;
    list-style: none;
}

.ks-app .ks-item {
    display: grid;
    grid-template-columns: 1.6rem minmax(0, 1fr) minmax(0, 1fr) auto;
    align-items: center;
    gap: 0.4rem;
}

.ks-app .ks-item-swatch {
    width: 1.6rem;
    height: 1.6rem;
    border-radius: 0.4rem;
    background: var(--ks-c);
    box-shadow: inset 0 -3px 0 color-mix(in srgb, var(--ks-c) 65%, #000000);
    color: #ffffff;
    font-size: 0.75rem;
    font-weight: 800;
    line-height: 1.6rem;
    text-align: center;
}

.ks-app .ks-item-affix .ks-unit {
    position: absolute;
    inset-inline-end: 0.55rem;
    color: var(--ak-muted);
    font-size: var(--ak-fs-sm);
    pointer-events: none;
}

.ks-app .ks-item-affix--pre .ks-unit {
    inset-inline-start: 0.6rem;
    inset-inline-end: auto;
}

.ks-app .ks-item-affix > input.ak-field.ks-item-field:not([type="range"]) {
    width: 100%;
    min-width: 0;
    padding-inline-end: 2rem;
}

.ks-app .ks-item-affix--pre > input.ak-field.ks-item-field:not([type="range"]) {
    padding-inline-start: 1.5rem;
    padding-inline-end: 0.5rem;
}

.ks-app .ks-item-remove {
    flex: none;
}

.ks-app .ks-add {
    justify-self: start;
    align-self: flex-start;
}
