/* Pathfinding Visualizer styles.

   Colours, type, spacing and the shared components (.btn, .btn.selected,
   .page-title, .section-title, .select, .range, .popover, .badge) come from
   the christophermpalmer.com theme, linked before this file in index.html.
   This file only holds what is specific to this app: layout, the toolbar,
   the movement toggle, dropdown behaviour, path slivers and the algorithm
   tooltip. Use theme tokens (var(--color-...)) rather than raw colours, so
   changes to the theme reach this app too. */

* {
    box-sizing: border-box;
}

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

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    /* Visible viewport height (dvh excludes mobile browser toolbars), minus
       the portfolio bar when hosted on christophermpalmer.com. Standalone,
       --pf-bar-height is unset, so nothing is subtracted. Browsers without
       dvh support keep the 100vh line above. */
    height: calc(100dvh - var(--pf-bar-height, 0px));
    padding: var(--space-5);
    gap: 15px;
}

.pageHeader,
.toolbar {
    flex-shrink: 0;
}

/* The title itself is the theme's .page-title. */
.pageHeader {
    text-align: center;
}

/* Grid */

.gridContainer {
    flex: 1;
    min-height: 0;
    min-width: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

#gridCanvas {
    display: block;
    /* Dragging a finger on the grid paints instead of scrolling or
       zooming the page. */
    touch-action: none;
}

.canvas-stack {
    position: relative;
}

/* Sits exactly over the grid; clicks pass through to the grid canvas. */
#pathCanvas {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
}

/* Toolbar */

.toolbar {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-3);
}

/* Dividers between top-level toolbar parts. */
.toolbar > * + * {
    border-left: var(--border);
    padding-left: var(--space-3);
}

/* A titled section: centred title (the theme's .section-title) above its
   controls. Sections stretch to the toolbar's height so their titles line
   up along the top. */
.toolbar-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    align-self: stretch;
    gap: var(--space-1);
}

/* Paint and Map rows. Same row gap as .algorithm-buttons, so with
   equal-height buttons each row lines up with an algorithm row. */
.terrain-rows {
    display: grid;
    grid-template-columns: auto auto;
    align-items: center;
    gap: var(--space-1-5);
}

/* Centred within the column; the Paint row is the widest, so this
   centres the Map buttons under it. */
.terrain-buttons {
    display: flex;
    justify-content: center;
    gap: var(--space-1-5);
}

.toolbar-label {
    font-size: var(--font-size-label);
    font-weight: var(--font-weight-semibold);
    white-space: nowrap;
    margin-right: var(--space-1);
}

/* Buttons look like the theme's .btn; in the toolbar they keep their
   natural size. */
.toolbar button {
    flex: 0 0 auto;
    width: auto;
}

/* Algorithm buttons */

/* Two rows: 5 buttons on the first, the rest on the second. */
.algorithm-buttons {
    display: grid;
    grid-template-columns: repeat(5, auto);
    gap: var(--space-1-5);
}

/* The most recently run algorithm and the current paint brush use the
   theme's .btn.selected (main.js and gridInteractions.js toggle it). */

/* Can't run in the current direction mode (e.g. Theta* in 4-way). Looks
   disabled, but still gets hover and long press so the tooltip can say why.
   The extra specificity beats .btn.selected and the .btn hover. */
.toolbar .algorithm-btn.unavailable,
.toolbar .algorithm-btn.unavailable:hover:not(:disabled) {
    background: var(--color-surface);
    border-color: var(--color-border);
    color: var(--color-text-ui);
    opacity: 0.45;
    cursor: not-allowed;
}

/* Sliver in the algorithm's path colour (set as --path-color in main.js),
   shown while its path is part of the comparison. */
.toolbar .algorithm-btn.has-path {
    box-shadow: inset 0 calc(-1 * var(--sliver-width)) 0 var(--path-color);
}

/* Movement toggle (the last slot of the algorithm grid).
   Kept compact so it never makes its grid row taller than a button;
   the grid cell stretches it to the row's height and column's width. */

.movement-toggle {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 0;
    min-width: 0;
    padding: 1px 2px;
    border: var(--border);
    border-radius: var(--radius-control);
    background: var(--color-surface-muted);
}

.movement-toggle-label {
    font-size: var(--font-size-caption);
    font-weight: var(--font-weight-semibold);
    line-height: 10px;
    letter-spacing: var(--tracking-caption);
    text-transform: uppercase;
    text-align: center;
    color: var(--color-text-secondary);
}

.toolbar .movement-toggle-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-1);
    padding: 0 var(--space-1);
    border: none;
    border-radius: var(--radius-inner);
    background: var(--color-selected-bg);
    color: var(--color-selected-text);
    font-size: var(--font-size-micro);
    font-weight: var(--font-weight-semibold);
    line-height: 22px;
    white-space: nowrap;
}

/* :not(:disabled) matches the specificity of the theme's
   ".btn:hover:not(:disabled)" rule plus a class, so this one wins. */
.toolbar .movement-toggle-btn:hover:not(:disabled) {
    background: var(--color-selected-bg-hover);
}

/* 22px so the arrows stay distinct at 100% zoom. The panel's padding,
   the caption's line height and the button's vertical padding were
   trimmed to make room: 2 border + 2 padding + 10 caption + 22 button
   = 36px, the same as one algorithm button. */
.movement-icon {
    width: 22px;
    height: 22px;
    flex: 0 0 auto;
}

/* The hidden attribute alone loses to display rules on SVGs in some browsers. */
.movement-icon[hidden] {
    display: none;
}

/* Playback */

/* Takes the toolbar's remaining width so the slider is long. */
.playback-section {
    flex: 1 1 260px;
    min-width: 0;
}

/* Two equal rows filling the space under the title: the slider lines
   up with the first row of algorithm buttons, play/speed with the second. */
.playback-controls {
    flex: 1;
    align-self: stretch;
    display: grid;
    grid-template-rows: 1fr 1fr;
    align-items: center;
    gap: var(--space-1-5);
    min-width: 0;
}

/* Colour and cursor come from the theme's .range. */
.playback-slider {
    width: 100%;
    margin: 0;
}

.playback-buttons {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: var(--space-1-5);
}

.toolbar .play-pause-btn {
    min-width: var(--touch-target);
    padding: var(--space-1) var(--space-3);
}

/* The speed menu is the theme's .select. */

/* Dropdowns (dropdown.js)
   Wide screens: the wrapper and menu don't create boxes, so the real
   buttons lay out exactly as if they weren't wrapped, and the trigger
   is hidden. (The menu's .popover look only shows once it is a box.) */

.dropdown,
.dropdown-menu {
    display: contents;
}

.toolbar .dropdown-trigger {
    display: none;
}

/* Small screens: algorithm and paint buttons collapse into dropdowns. */
@media (max-width: 900px) {
    /* Algorithm dropdown on the first row, Directions on the second,
       lining up with Paint/Map and the playback rows. */
    .algorithm-buttons {
        grid-template-columns: auto;
    }

    .algorithm-buttons .dropdown {
        order: -1;
    }

    .dropdown {
        display: block;
        position: relative;
    }

    .toolbar .dropdown-trigger {
        display: flex;
        align-items: center;
        gap: var(--space-1-5);
        width: 100%;
        white-space: nowrap;
    }

    /* Wide enough for the longest name, so it doesn't jump around. */
    .toolbar .algorithm-trigger {
        min-width: 150px;
    }

    .dropdown-trigger-text {
        flex: 1;
        text-align: left;
    }

    .dropdown-caret {
        font-size: var(--font-size-micro);
        opacity: 0.7;
    }

    /* Paint dropdown fills the column above the Map buttons. */
    .terrain-buttons > .dropdown {
        flex: 1;
    }

    /* Border, background and shadow come from the theme's .popover. */
    .dropdown-menu {
        display: none;
        position: absolute;
        top: calc(100% + var(--space-1));
        left: 0;
        z-index: var(--z-dropdown);
        min-width: 100%;
        flex-direction: column;
        gap: var(--space-1);
        padding: var(--space-1-5);
    }

    .dropdown.open .dropdown-menu {
        display: flex;
    }

    .toolbar .dropdown-menu button {
        text-align: left;
        white-space: nowrap;
    }
}

/* Too narrow for the three sections side by side (they need about
   790px): stack them in one centred column, all the same width, so
   their edges line up. */
@media (max-width: 800px) {
    .toolbar {
        flex-direction: column;
        align-items: center;
        gap: var(--space-2-5);
    }

    /* align-self overrides the stretch used for side-by-side titles,
       which would otherwise pin the column to the left. */
    .toolbar > * {
        width: 100%;
        max-width: 360px;
        align-self: center;
    }

    /* Dividers run between the stacked sections instead of beside them. */
    .toolbar > * + * {
        border-left: none;
        padding-left: 0;
        border-top: var(--border);
        padding-top: var(--space-2-5);
    }

    /* Algorithms and Terrain share the same two columns, so Directions
       lines up with the Paint/Map labels and the algorithm dropdown
       starts where the paint dropdown and Map buttons do. The first
       column fits the Directions toggle. */
    .toolbar {
        --stack-label-column: 76px;
    }

    .algorithm-buttons {
        width: 100%;
        grid-template-columns: var(--stack-label-column) 1fr;
    }

    /* Directions first (as in the markup), then the dropdown. */
    .algorithm-buttons .dropdown {
        order: 0;
    }

    .terrain-rows {
        width: 100%;
        grid-template-columns: var(--stack-label-column) 1fr;
    }

    /* Generate and Clear split the row evenly. */
    .terrain-buttons > button {
        flex: 1;
    }

    .playback-section {
        flex: none;
    }

    .playback-controls {
        width: 100%;
    }
}

/* The trigger mirrors the current choice: .btn.selected when something is
   selected (last run / current brush, set by dropdown.js), plus its path
   sliver. */
.toolbar .dropdown-trigger.has-path {
    box-shadow: inset 0 calc(-1 * var(--sliver-width)) 0 var(--path-color);
}

/* Path still in the comparison but hidden (press the blue button again
   to show it): a faded sliver. */
.toolbar .algorithm-btn.has-path.path-hidden,
.toolbar .dropdown-trigger.has-path.path-hidden {
    box-shadow: inset 0 calc(-1 * var(--sliver-width)) 0 color-mix(in srgb, var(--path-color) 30%, transparent);
}

.dropdown-dot.path-hidden {
    opacity: 0.3;
}

/* One dot per algorithm whose path is on the grid. */
.dropdown-dots {
    display: flex;
    gap: 3px;
}

.dropdown-dots:empty {
    display: none;
}

.dropdown-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    box-shadow: var(--outline-swatch);
}

/* Algorithm tooltip (algorithmTooltip.js) */

/* Long-pressing a button shows its tooltip; stop phones from selecting
   the label or opening their own long-press menu instead. */
.algorithm-btn {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* Border, background and shadow come from the theme's .popover. */
.algorithm-tooltip {
    position: fixed;
    z-index: var(--z-tooltip);
    max-width: min(360px, calc(100vw - 16px));
    padding: var(--surface-padding);
    color: var(--color-text-ui);
    font-size: var(--font-size-small);
    line-height: var(--leading-ui);
    pointer-events: none;
}

.algorithm-tooltip[hidden] {
    display: none;
}

.tooltip-header {
    display: flex;
    align-items: center;
    gap: var(--space-1-5);
}

.tooltip-title {
    color: var(--color-heading);
    font-size: 14px;
}

.tooltip-swatch {
    flex: none;
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--color-border);
    box-shadow: var(--outline-swatch);
}

/* The guarantee is a theme .badge (.badge-positive when it's a guarantee). */
.tooltip-badge {
    margin-top: 5px;
}

.tooltip-description {
    margin: var(--space-1-5) 0 0;
}

.tooltip-note {
    margin: var(--space-1-5) 0 0;
    color: var(--color-text-secondary);
    font-style: italic;
}

.tooltip-warning {
    margin: var(--space-1-5) 0 0;
    padding: var(--space-1) var(--space-1-5);
    border-left: 3px solid var(--color-warning-border);
    background: var(--color-warning-bg);
    color: var(--color-warning-text);
    font-weight: var(--font-weight-semibold);
}

.tooltip-table {
    width: 100%;
    margin-top: var(--space-2);
    border-collapse: collapse;
    font-size: var(--font-size-label);
    font-variant-numeric: tabular-nums;
}

.tooltip-table th {
    padding: 0 0 3px var(--space-2);
    border-bottom: 1px solid var(--color-rule);
    color: var(--color-text-secondary);
    font-size: var(--font-size-micro);
    font-weight: var(--font-weight-semibold);
    text-align: right;
    white-space: nowrap;
}

.tooltip-table td {
    padding: 2px 0 2px var(--space-2);
    text-align: right;
    white-space: nowrap;
}

.tooltip-table .tooltip-row-name {
    display: flex;
    align-items: center;
    gap: 5px;
    padding-left: 0;
    text-align: left;
}

/* The hovered algorithm's row. */
.tooltip-table tr.current td {
    background: var(--color-highlight-row);
    font-weight: var(--font-weight-bold);
}

/* Lowest value in a column. */
.tooltip-table td.best {
    color: var(--color-success-strong);
    font-weight: var(--font-weight-bold);
}

.tooltip-table .tooltip-muted {
    color: var(--color-text-muted);
    font-style: italic;
}

/* Responsive */

@media (max-width: 700px) {
    .app {
        padding: var(--space-2-5);
        gap: var(--space-2-5);
    }
}
