// SPDX-FileCopyrightText: 2024 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later

/* Layout */

.timeline {
  display: flex;
  flex-direction: column;
  overflow-x: hidden;

  .time-grid {
    --sidebarWidth: 12em;
    --stepRowHeight: 4em;
    --primaryRowHeight: 4em;
    --daysHeaderHeight: 3em;
    position: relative;

    &:has(.timescale) {
      margin-right: 1em; // make midnight-right timestamp visible

      .grid,
      .overlay {
        border-bottom-width: 0;
      }
    }

    .time-grid-header {
      box-sizing: border-box;
      position: sticky;
      z-index: 4; // overlap everything
      top: 0;
      height: var(--daysHeaderHeight);
    }

    .row-title {
      display: flex;
      align-items: center;
      justify-content: space-between;

      > a {
        display: flex;
        width: 100%;
        align-items: baseline;
        margin-left: .5em;

        span {
          width: 0;
          flex: 1 0 auto;
          margin-right: 0.25em;
          .text-ellipsis();
        }
      }
    }

    .overlay {
      overflow: visible;
      z-index: 3;

      .entry {
        margin-top: 1em;
        margin-bottom: 1em;
        overflow: visible;
        position: relative;

        .title {
          height: 100%;
          flex-wrap: nowrap;
          align-items: baseline;
          padding: .15em .5em;

          .name {
            .text-ellipsis();
          }
        }

        &.narrow-entry {
          .rotation-info {
            left: ~"calc(50% - 1em)";

            &.is-left {
              right: ~"calc(50% - 1em)";
            }
          }
        }

        .rotation-info {
          display: none;
          position: absolute;
          bottom: ~"calc(100% + 0.5em)";
          width: max-content;
          max-width: 30em;
          height: auto;
          padding: 0.5em;
          left: 1.5em;
          flex-direction: column;
          white-space: nowrap;

          .rotation-info-time {
            flex-direction: column;
            display: flex;
          }

          &::before {
            content: '';
            position: absolute;
            display: inline-block;
            width: 1em;
            height: 1em;
            left: 0.5em;
            bottom: ~"calc(-0.5em - 1px)";
            background-color: @body-bg-color;
            transform: rotate(45deg);
          }

          &::after {
            content: '';
            position: absolute;
            bottom: -0.5em;
            left: 0;
            right: 0;
            height: 0.5em;
            background: transparent;
          }

          &.is-left {
            left: auto;
            right: ~"calc(100% - 3.5em)";

            &::before {
              left: auto;
              right: 0.5em;
              transform: rotate(45deg);
            }
          }

          &.is-bottom {
            top: ~"calc(100% + 0.5em)";
            bottom: auto;

            &::before {
              bottom: auto;
              top: ~"calc(-0.5em - 1px)";
              transform: rotate(225deg);
            }

            &::after {
              top: -0.5em;
            }

            &.is-left:before {
              transform: rotate(225deg);
            }
          }

          &.entry-is-hovered {
            z-index: 1;
            display: flex;
          }
        }
      }

      .future-entry {
        display: flex;
        justify-content: end;
        z-index: 2; // overlap the .clock .time-hand
        pointer-events: all;
        overflow: hidden;

        > div {
          margin-top: 1em;
          margin-bottom: 1em;
          display: flex;
          align-items: center;
          justify-content: end;
          position: relative;
          width: 3em;

          .outline {
            content: '';
            display: block;
            position: absolute;
            border: 1px solid @gray-light;
            border-right-width: 0;
            top: -1px;
            bottom: -1px;
            width: 100%;
            .rounded-corners(0.25em 0 0 0.25em);
          }

          .icon {
            color: @gray;
          }

          .outline:nth-of-type(1) {
            left: -1px; // Dock at the grid border
          }

          .outline:nth-of-type(2) {
            left: 3px; // 4px gap
          }

          .outline:nth-of-type(3) {
            left: 7px; // Same gap
          }
        }
      }
    }

    &::after {
      content: '';
      display: block;
      border-top: 1px solid black;
      position: absolute;
      right: 0;
      left: 0;
      // -1 to exclude result row
      top: ~"calc((var(--stepRowHeight) * calc(var(--primaryRows) - 1)) + var(--daysHeaderHeight))";
    }

    .timescale {
      display: grid;
      grid-template-columns: repeat(
        ~"calc(var(--primaryColumns) * var(--timestampsPerDay))",
        minmax(~"calc(var(--minimumPrimaryColumnWidth) / var(--timestampsPerDay))", 1fr)
      );

      border-left: 1px solid @gray-lighter; // this is required to maintain the grid layout
      grid-area: ~"4 / 2 / 4 / 3";

      .ticks {
        position: relative;
        border-right: 1px solid @gray-light;
        border-left: 1px solid @gray-light;

        &::after { // overlaps the unnecessary part of border-left
          content: '';
          position: absolute;
          top: 0.25em;
          left: -1px; // overlap the border-left
          right: 0;
          bottom: 0;
          background: @body-bg-color;
        }
      }

      .timestamp {
        display: flex;
        flex-direction: column;
        align-items: center;
        margin-top: 0.5em;
        padding-top: 0.5em;
        font-size: .75em;
        position: relative;
        left: -50%;
        line-height: 1;

        &.midnight {
          left: 50%;
        }

        > span:nth-child(2) {
          opacity: 0.5;
        }
      }

      > span.ticks:nth-last-of-type(2), // last .ticks and .midnight
      > .timestamp.midnight {
        grid-area: ~"1 / -2 / 1 / -1";
      }
    }

    .clock {
      display: grid;
      grid-template-columns: repeat(var(--primaryColumns), minmax(var(--minimumPrimaryColumnWidth), 1fr));
      grid-area: ~"3 / 2 / 4 / 3";
      border-top: 1px solid transparent; // left not required, otherwise the .time-hand is not aligned properly

      .current-day {
        display: grid;
        grid-template-columns: repeat(var(--columnsPerStep), minmax(var(--minimumStepColumnWidth), 1fr));
        grid-area: ~"1 / 1 / 2 / 2";

        .time-hand {
          grid-area: ~"1 / var(--timeStartColumn) / 2 / calc(var(--timeStartColumn) + 1)";
          display: flex;
          align-items: flex-end;
          width: 1px;
          border-left: 1px solid red;
          z-index: 1;

          .now {
            .rounded-corners();
            padding: 0 .25em;
            transform: translate(-50%, 50%);
          }
        }
      }
    }

    .new-rotation-container {
      pointer-events: none;
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
      grid-area: ~"3 / 1 / 4 / 3";
      padding-bottom: var(--stepRowHeight);

      .new-rotation-content {
        pointer-events: all;
        height: var(--stepRowHeight);
        display: flex;
        align-items: baseline;
        justify-content: center;
        align-content: center;
        flex-wrap: wrap;
        gap: .417em;

        > span {
          z-index: 2; // Grid gaps must not bleed through, in day mode the time-hand must be below the button
        }
      }
    }
  }
}

.timeline:has(.empty-notice) {
  position: relative;

  .empty-notice {
    position: absolute;
    width: 100%;
    line-height: 1.2;
    text-align: center;
    z-index: 1
  }
}

#layout.minimal-layout,
#layout.poor-layout,
#layout.twocols:not(.wide-layout) {
  .rotation-info {
    .days-interval-wrapper {
      display: flex;
      flex-direction: column;
    }
  }

  .medium-entry {
    .rotation-info {
      left: ~"calc(50% - 1em)";

      &.is-left {
        right: ~"calc(50% - 1em)";
        left:auto;
      }
    }
  }
}

/* Design */

.timeline {
  .time-grid-header {
    background: @body-bg-color;
  }

  .rotation-name {
    font-size: 1.25em;
    font-weight: bold;

    > .icon[data-drag-initiator] {
      color: @gray-light;

      &:hover {
        cursor: grab;
        color: @list-item-title-hover-color;
      }
    }
    > .icon:not([data-drag-initiator]) {
      display: none;
    }

    // :not(:empty) is only necessary to increase specificity
    &:not(:empty).sortable-drag {
      border: 1px dashed @gray;
    }

    &.sortable-ghost {
      opacity: .5;
    }

    > a {
      text-decoration: none;

      &:hover {
        color: @list-item-title-hover-color;
      }

      &:not(:hover) .icon {
        display: none;
      }
    }
  }

  .entry .icon {
    font-size: .75em;
    opacity: .8;
  }

  .timescale .timestamp {
    color: @gray-semilight;
    background: @body-bg-color;
  }

  .clock .now {
    background-color: @gray-light;
    font-size: 0.75em;
    color: red;
    .user-select(none);
  }

  .new-rotation-content {
    background: @gray-lighter;
    color: @text-color-light;

    a {
      &:not(.btn-primary) {
        .button(@gray-lighter, @text-color-light);
      }

      &.btn-primary {
        .button(@gray-lighter);
      }
    }
  }

  .rotation-info {
    background-color: @body-bg-color;
    border: 1px solid @gray-light;
    .rounded-corners();

    &::before {
      border-right: 1px solid @gray-light;
      border-bottom: 1px solid @gray-light;
    }
  }

  .rotation-info-name {
    font-weight: bold;
  }

  .rotation-info-member-count,
  .rotation-info-mode,
  .rotation-info-start,
  .rotation-info-on-duty,
  .rotation-info-members {
    color: @text-color-light;
    font-weight: normal;
  }

  .rotation-info-start time,
  .rotation-info-active-member-name,
  .rotation-info-member-list {
    color: @default-text-color;
  }
}

.timeline .empty-notice {
  font-size: 1.25em;
}

.days-header .column-title .date {
  font-size: .75em;
  color: @text-color-light;
}

#layout.twocols:not(.wide-layout) .days-header .column-title {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  line-height: 1.25;
  padding-bottom: .25em;
}

#layout.twocols .schedule-detail .timescale:has(:nth-child(n+62)) { // month view (--timestampsPerDay * --primaryColumns = 62)
  display: none;
}
