@use 'bulma-variables' as bv;
@use 'bulma/sass/utilities/mixins';
@use 'sass:color';
@use 'padding-and-margins';
@use 'utilities';
@use 'home';
@use 'tooltip';

:root {
  color-scheme: light dark;

  --vglist-theme: #5957d6;

  --bulma-scheme-main: hsl(0, 0%, 100%);
  --bulma-scheme-h: 0;
  --bulma-scheme-s: 0%;
  --bulma-scheme-main-l: 100%;
  --bulma-scheme-main-bis-l: 98%;
  --bulma-scheme-main-ter-l: 96%;
  --bulma-background-l: 98%;
  --bulma-text-l: 29%;

  --bulma-scheme-main: hsl(var(--bulma-scheme-h), var(--bulma-scheme-s), var(--bulma-scheme-main-l));
  --bulma-background: hsl(var(--bulma-scheme-h), var(--bulma-scheme-s), var(--bulma-background-l));
  --bulma-text: hsl(var(--bulma-scheme-h), var(--bulma-scheme-s), var(--bulma-text-l));

  --bulma-shadow: 0 2px 3px rgba(10, 10, 10, 0.1), 0 0 0 1px rgba(10, 10, 10, 0.1);

  --home-page-subtitle-text: #fff;
  --home-section-background-color: var(--vglist-theme);
  --home-section-background-brightness: 85%;

  --vglist-shadow: 0 2px 3px rgba(0, 0, 0, 0.1), 0 0 0 1px rgba(0, 0, 0, 0.1);

  --library-edit-bar-border: #dcdfe6;

  .button {
    --bulma-button-background-l-delta: 5%;
    // Force the text to be white on primary buttons.
    --bulma-primary-invert-l: var(--bulma-primary-100-l);
  }

  .card {
    --bulma-card-background-color: var(--bulma-scheme-main);
  }

  .tabs {
    --bulma-tabs-border-bottom-color: var(--bulma-border-hover);
  }

  .control, .input, .textarea, .select {
    --bulma-input-background-l-delta: 6%;
    --bulma-input-border-l-delta: -20%;
  }

  // Differentiate the modal header from the main modal body.
  .modal {
    --bulma-modal-card-head-background-color: var(--bulma-scheme-main-bis);
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bulma-scheme-h: 217;
    --bulma-scheme-s: 28%;
    --bulma-scheme-main-l: 24%;
    --bulma-scheme-main-bis-l: 27%;
    --bulma-scheme-main-ter-l: 30%;

    --bulma-scheme-main: hsl(var(--bulma-scheme-h), var(--bulma-scheme-s), var(--bulma-scheme-main-l));
    --bulma-background-l: 20%;
    --bulma-text-l: 100%;

    --bulma-shadow: 0 2px 3px rgba(12, 12, 12, 0.1), 0 0 0 1px rgba(12, 12, 12, 0.1);

    --home-section-background-color: var(--bulma-scheme-main);
    --home-section-background-brightness: 75%;

    --vglist-shadow: 0 2px 3px rgba(12, 12, 12, 0.1), 0 0 0 1px rgba(12, 12, 12, 0.1);
    --library-edit-bar-border: #435169;

    --text-muted: var(--grey-lighter, #dbdbdb);

    // Fixes light text on light background in dark mode.
    --bulma-light-light-invert-l: 15%;

    .card {
      --bulma-card-background-color: var(--bulma-scheme-main-ter);
    }

    .box {
      --bulma-box-background-color: var(--bulma-scheme-main-ter);
    }

    .control, .input, .textarea, .select {
      --bulma-input-background-l-delta: 6%;
      --bulma-input-border-l-delta: 20%;
    }

    // Override the menu list items in dark mode to match the background color of the menu box container.
    .menu-list a, .menu-list button, .menu-list .menu-item {
      --bulma-menu-item-h: var(--bulma-scheme-h);
      --bulma-menu-item-s: var(--bulma-scheme-s);
      --bulma-menu-item-background-l: var(--bulma-scheme-main-ter-l);
    }
  }
}

nav.navbar {
  background: var(--vglist-theme);
  // Subtle box-shadow for nav bar.
  box-shadow: 0 0 2px 2px rgba(0, 0, 0, 0.3);
  padding: 0 20px;

  // Override burger icon color when hovered.
  // There doesn't seem to be a Bulma variable for this.
  .navbar-burger:hover {
    color: #fff;
  }

  a.navbar-item,
  .navbar-link {
    --bulma-navbar-item-background-a: 0;
    // Limit the gap so the avatar in the navbar is not so far from the username.
    gap: 0.25rem;

    &:focus,
    &:focus-within,
    &:hover {
      // Do not add background on hover of "More" or user dropdown in navbar.
      --bulma-navbar-item-background-a: 0;
    }
  }
  // Show underlines on-hover on desktop.
  @include mixins.from(bv.$desktop) {
    // Don't apply the borders to items with dropdowns or fields (the search bar).
    .navbar-item:not(.has-dropdown),
    .navbar-item:not(.field) {
      // This prevents the navbar items from moving up and down when hovered.
      border-bottom: 2px solid transparent;
      transition: border-bottom 200ms;
      --bulma-navbar-item-background-a: 0;

      &:hover, &:focus {
        --bulma-navbar-item-background-a: 0.05;
        border-bottom: 2px solid rgba(255, 255, 255, 0.8);
      }
    }

    .navbar-dropdown {
      .navbar-item {
        transition: border-bottom 0ms;
        transition: background-color 200ms;
        transition: color 0ms;
        border-bottom: 0;

        &:hover, &:focus {
          border-bottom: 0;
        }

        &:hover, &:focus, &.is-active {
          // On focus (e.g. via arrow key navigation), use dark text in light mode, light text in dark mode.
          --bulma-navbar-item-color: hsl(0, 0%, var(--bulma-text-l));
          --bulma-navbar-item-background-a: 0.10;

          @media (prefers-color-scheme: dark) {
            // Need more of a difference in dark mode for it to be visible.
            --bulma-navbar-item-background-a: 0.25;
          }
        }
      }
    }
  }

  .navbar-dropdown {
    font-size: 1rem;
  }

  .navbar-item img.user-avatar {
    max-height: 1.8rem;
    max-width: 1.8rem;
    object-fit: scale-down;
    border: 1px solid #fff;
  }

  .navbar-dropdown-header {
    font-size: 0.8rem;
    font-weight: 700;
  }

  // For game covers in the search dropdown.
  img.game-cover {
    max-height: 48px;
    overflow: clip;
    object-fit: cover;
  }
}

.navbar-search-dropdown {
  max-height: 400px;
  height: auto;
  overflow: scroll;
  // Don't display a horizontal scrollbar.
  overflow-x: hidden;
}

// Customization for bulma dropdowns.
// Based on https://github.com/jgthms/bulma/pull/2142
// Don't apply these styles to vue-select dropdowns.
.dropdown:not(.v-select) {
  button > .icon {
    fill: var(--bulma-text);
    z-index: 4;
    transition: transform 150ms;
    margin-top: 2px;
  }

  &.is-active {
    button > .icon {
      fill: var(--bulma-text);
      transform: rotate(-180deg);
      margin-top: 0;
    }
  }

  &.is-fullwidth-mobile {
    @include mixins.touch {
      display: flex;
      width: 100%;
    }
  }

  .dropdown-item.no-link-highlight a {
    // TODO: Fix?
    color: var(--dropdown-link-no-highlight);
  }
}

.buttons {
  align-items: initial;

  &.buttons-vertical {
    flex-direction: column;
  }
}

.dropdown-trigger {
  &.is-fullwidth {
    display: flex;
    width: 100%;
  }

  &.is-fullwidth-mobile {
    @include mixins.touch {
      display: flex;
      width: 100%;
      margin-right: 0;
    }
  }

  button {
    &.is-fullwidth {
      display: flex;
      width: 100%;
    }

    &.is-fullwidth-mobile {
      @include mixins.touch {
        display: flex;
        width: 100%;
      }
    }
  }
}

.dropdown-menu {
  &.is-fullwidth {
    width: 100%;
  }

  &.is-fullwidth-mobile {
    @include mixins.touch {
      width: 100%;
    }
  }
}

.file-select > input[type='file'] {
  display: none;
}

.file-select > .select-button {
  padding: 1rem;

  color: white;
  background-color: #2ea169;

  border-radius: 0.3rem;

  text-align: center;
  font-weight: bold;
}

.hero-area {
  display: flex;
  flex-flow: column wrap;

  @include mixins.from(bv.$desktop) {
    flex-flow: row nowrap;

    .text-block {
      flex: 1 1 100%;
      padding-left: 15px;
    }
  }

  @include mixins.touch {
    .text-block {
      text-align: center;
    }
  }

  .hero-image {
    flex: 1 1 200px;

    img {
      max-height: 100%;
      max-width: 100%;
      object-fit: contain;
      object-position: top left;
      
      @include mixins.touch {
        object-position: top center;
        max-height: 300px;
      }
    }

    &.hero-image-150 {
      img {
        height: 200px;
        width: auto;
        max-width: 100%;
      }
    }

    @include mixins.touch {
      margin: auto;
    }
  }
}

.game-card-list,
.user-card-list {
  display: grid;
  grid-gap: 20px;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));

  // Avoid "double dipping" on the margin, so remove the block spacing given we have a grid here.
  --bulma-block-spacing: 0;

  @include mixins.from(bv.$tablet) {
    grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  }
}

.custom-card {
  background-color: var(--bulma-card-background-color);
  box-shadow: var(--vglist-shadow);
  border-radius: var(--bulma-card-radius);
  width: auto;
  position: relative;
  overflow: hidden;

  .button {
    // No change in background for light mode.
    --bulma-button-background-l-delta: 0;

    &:hover {
      --bulma-button-background-l-delta: -3%;
    }

    @media (prefers-color-scheme: dark) {
      // Match the difference between default and -ter.
      --bulma-button-background-l-delta: 6%;

      &:hover {
        --bulma-button-background-l-delta: 9%;
      }
    }
  }

  &.game-card {
    height: 200px;
  }

  &.user-card {
    height: 80px;

    .card-content {
      overflow: hidden;
    }

    .title {
      text-overflow: ellipsis;
      overflow: hidden;
      word-break: normal;
    }

    img {
      width: 80px;
      min-width: 80px;
      height: 80px;
    }
  }

  .card-container {
    padding: 0;
    display: flex;
    flex-flow: row nowrap;
    height: 100%;
  }

  .card-content {
    padding: 15px;
  }

  .game-cover {
    height: 100%;
    width: 140px;
    max-width: 140px;
    min-width: 140px;

    img {
      height: 100%;
      width: 100%;
      object-fit: cover;
    }
  }
}

.game-card-dropdown {
  position: absolute;
  top: 0;
  right: 0;
}

// Center the game cover if the sidebar is full-width (specifically useful for mobile).
.game-sidebar .game-cover img {
  display: block;
  margin: auto;
}

// Remove the radius and modify padding on notifications so they can be used
// for Rails flashes.
.notification.flash-notification {
  border-radius: 0;
  padding: 1rem 2.5rem 1rem 1.5rem;
  display: flex;

  // Make sure it wraps so the user can always see the notification text
  // without it running over the 'close' button.
  > p {
    flex-grow: 1;
    white-space: normal;
  }

  // Reset the positioning of the delete element within the
  // notification container.
  > .delete {
    position: relative;
    right: auto;
    top: auto;
  }
}

.notification.errors-notification {
  li {
    list-style: initial;
    margin-left: 20px;
  }
}

.modal-card {
  max-height: calc(100vh - 120px);
  overflow: scroll;
  overflow-x: hidden;

  // When the modal card title is too long, make sure it
  // gets a trailing ellipsis instead of causing horizontal scrolling.
  // Also add bottom padding to prevent characters that go below the baseline
  // from being clipped (e.g. the letter 'g').
  .modal-card-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: calc(100% - 20px);
    padding-right: 10px;
    padding-bottom: 4px;
  }
}

.modal-card-body-allow-overflow {
  overflow: initial;
}

// Make input placeholder text darker by default.
.input::placeholder {
  color: var(--bulma-input-placeholder-color);
  opacity: 0.5;
}

.infobox {
  width: 100%;

  column-count: 3;
  column-width: 250px;
  column-gap: 20px;
  column-fill: balance;

  // Make it so the infobox sections aren't split between columns.
  .infobox-section {
    break-inside: avoid;
  }

  @include mixins.touch {
    column-count: 1;
  }
}

.stats-card {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-evenly;
  padding: 15px;
}

.button.is-fullwidth-mobile {
  @include mixins.touch {
    display: flex;
    width: 100%;
  }
}

.is-fullwidth-mobile {
  @include mixins.touch {
    width: 100%;
  }
}

.level {
  .level-item {
    white-space: nowrap;
    flex-basis: 100px;
    margin: 0;
    padding: 5px;
  }

  // Fix alignment of close icon in notification.
  &.notification {
    flex-direction: row;
  }
}

.more-from-this-series-card {
  max-width: 500px;

  .games {
    display: grid;
    grid-gap: 10px;
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));

    a {
      display: inline-block;
      position: relative;
    }
  }

  .game-cover {
    height: 200px;
    width: 140px;
    max-width: 140px;
    min-width: 140px;

    @include mixins.touch {
      width: 100%;
      max-width: 100%;
      min-width: 100%;
    }

    img {
      height: 100%;
      width: 100%;
      object-fit: cover;
      position: absolute;
    }

    p {
      color: #fff;
      padding: 8px;
      position: absolute;
      bottom: 0;
      background-color: rgba(#333, 0.7);
      width: 100%;
    }
  }
}

// Hide pagination except Previous/Next on mobile.
@include mixins.touch {
  .pagination-list {
    display: none;
  }
}

.stats-card {
  .level {
    width: 100%;
  }
}

.percentage-bar {
  height: 18px;
  width: 100%;
  border-radius: 4px;
  overflow: hidden;

  .percentage-bar-portion {
    display: inline-block;
    height: 100%;
    width: 100%;
    overflow: hidden;
    color: #fff;

    &.color-1 {
      background-color: #03a9f4;
    }
    &.color-2 {
      background-color: #ff5252;
    }
    &.color-3 {
      background-color: #e91e63;
    }
    &.color-4 {
      background-color: #ff5722;
    }
    &.color-5 {
      background-color: #8bc34a;
    }
    &.color-6 {
      background-color: #3f51b5;
    }
    &.color-7 {
      background-color: #7c4dff;
    }
    // Last color is 'Unknown'
    &.color-8 {
      background-color: #d1cfcf;
    }
  }
}

.library-edit-bar.level {
  position: sticky;
  top: 0;
  width: 100%;
  // TODO: change to match the current color scheme.
  background-color: var(--bulma-scheme-main, #fff);
  border: 1px solid var(--library-edit-bar-border);
  margin-bottom: 5px;
  // Modal overlay is 40, make sure to stay below it.
  z-index: 39;
  padding: 8px 5px 8px 15px;

  // Set width to auto to make the input field display the placeholder value.
  // By default, width is set to 0 for some reason.
  .vs__search,
  .vs__search:focus {
    width: auto;
  }
}

// Turn the button red and add a shaking animation when there's an error.
.js-submit-button-error {
  background-color: #ff3860 !important;
  animation: shake 0.82s cubic-bezier(.36, .07, .19, .97) both;
  transform: translate3d(0, 0, 0);
  backface-visibility: hidden;
}

@keyframes shake {
  10%, 90% {
    transform: translate3d(-1px, 0, 0);
  }
  
  20%, 80% {
    transform: translate3d(2px, 0, 0);
  }

  30%, 50%, 70% {
    transform: translate3d(-4px, 0, 0);
  }

  40%, 60% {
    transform: translate3d(4px, 0, 0);
  }
}

// Spacing between elements and make sure the dropdowns are fullwidth.
.games-search-sidebar {
  .games-filters > div {
    margin-top: 10px;
  }

  .dropdown {
    display: block;
  }

  .dropdown-menu {
    position: relative;
  }
}

// For easily selecting the whole piece of text, e.g. for easy copy-paste.
.user-select-all {
  user-select: all;
}

// Limit the width of the sign-in container.
.sign-in-container {
  max-width: 500px;

  @include mixins.touch {
    box-shadow: none;
  }
}

// Toggles the display of icon-1 and icon-2 child elements when hovering on this element.
.toggle-icon-on-hover {
  &:hover > .svg-icon.icon-1 {
    display: none;
  }

  > .svg-icon.icon-2 {
    display: none;
  }

  &:hover > .svg-icon.icon-2 {
    display: initial;
  }
}

// Styling for the navbar search field, mostly the icon.
// The navbar always uses the vglist theme color, so the search
// should stay the same regardless of color scheme.
.navbar-search-input {
  --bulma-input-color: #fff;
  --bulma-input-color-l: 90%;

  background-color: rgba(255, 255, 255, 0.25);
  border: 0;
  border-radius: 3px;
  transition: background-color 250ms, color 250ms;

  &::placeholder {
    // Force it to be while when not-focused so it's visible on the light-purple background.
    color: #fff;
  }

  + span.icon {
    fill: rgba(255, 255, 255, 0.5);
    transition: fill 250ms;
  }

  &:focus {
    --bulma-input-color-l: 10%;

    background-color: rgba(255, 255, 255, 1);

    + span.icon {
      fill: color.adjust(bv.$grey-light, $lightness: -30%);
    }

    // Make the input field text dark when focused because the background color is white in both schemes.
    &::placeholder {
      color: hsl(var(--bulma-text-h), var(--bulma-text-s), 10%);
    }
  }
}

.is-vertical-align-middle {
  vertical-align: middle;
}

.is-borderless {
  border: 0px;
}

// Custom vue-select styles to match Bulma.
.v-select {
  display: block;

  .vs__search {
    color: hsl(var(--bulma-input-h), var(--bulma-input-s), var(--bulma-input-color-l));

    &::placeholder {
      color: var(--bulma-input-placeholder-color);
    }
  }

  .vs__dropdown-menu {
    background-color: var(--bulma-scheme-main-ter);
    color: var(--bulma-text);
  }

  .vs__dropdown-toggle {
    border: 1px solid var(--bulma-input-border-color);
    border-radius: var(--bulma-input-radius);
    background-color: hsl(var(--bulma-input-h), var(--bulma-input-s), calc(var(--bulma-input-background-l) + var(--bulma-input-background-l-delta)));

    &:hover {
      --bulma-input-border-l-delta: var(--bulma-input-hover-border-l-delta);
    }
    
    &:focus,
    &.vs--open {
      border: 1px solid hsl(var(--bulma-input-focus-h), var(--bulma-input-focus-s), var(--bulma-input-focus-l));
    }
  }

  .vs__selected {
    color: var(--bulma-text);
  }

  &:not(.vs--single) .vs__selected-options .vs__selected {
    color: var(--bulma-text);
    background-color: var(--bulma-scheme-main-ter);
  }

  .vs__dropdown-option {
    color: var(--bulma-text);

    // When highlighted, it should have white text since it always has a blue
    // background.
    &.vs__dropdown-option--highlight {
      color: #fff;
    }
  }

  // Set the fill of the icons in v-select inputs.
  .vs__open-indicator,
  .vs__clear,
  .vs__spinner,
  .vs__deselect {
    fill: var(--bulma-text);
  }

  &.vs--disabled {
    background-color: var(--bulma-input-disabled-background-color);
    border-color: var(--bulma-input-disabled-border-color);
  }

  &.vs--disabled .vs__clear,
  &.vs--disabled .vs__dropdown-toggle,
  &.vs--disabled .vs__open-indicator,
  &.vs--disabled .vs__search,
  &.vs--disabled .vs__selected {
    color: var(--bulma-input-disabled-color);
    background-color: transparent;
  }
}

// For text that should have a more muted color, e.g. extra information in
// a card.
.has-text-muted {
  color: var(--text-muted);
}

// FIXME: A temporary hack until the vue-good-table dark theme styling is
// fixed. Without this, it'll try to use a light grey background for the
// headers on the compare library page.
.vgt-table th.vgt-row-header {
  @media (prefers-color-scheme: dark) {
    background-color: var(--bulma-scheme-main) !important;
  }
}

// Grid of avatars on the Game page.
.avatar-image-grid {
  display: grid;
  grid-gap: 5px;
  grid-template-columns: repeat(auto-fill, 64px);

  @include mixins.touch {
    // Display rows of three on mobile.
    grid-template-columns: 1fr 1fr 1fr;
  }

  .avatar-image-grid-item {
    // Special styling for the "+123" element.
    &.avatar-image-grid-item-overflow {
      display: inline-grid; // this is necessary for centering the text to work.
      background-color: var(--bulma-scheme-main-ter);
      width: 100%;
      height: 100%;
      border: 1px solid var(--bulma-border);

      // No borders if the user has dark theme.
      @media (prefers-color-scheme: dark) {
        border: 0;
      }

      p {
        margin: auto;
        font-size: 1.2rem;
      }
    }

    a {
      width: 100%;
      height: 100%;
    }
  }
}
